diff --git a/Inc/mmr_can.h b/Inc/mmr_can.h index f4c4c8b..b85fa92 100644 --- a/Inc/mmr_can.h +++ b/Inc/mmr_can.h @@ -230,9 +230,20 @@ HalStatus MMR_CAN_Send(CanHandle *hcan, MmrCanPacket packet); */ HalStatus MMR_CAN_SendNoTamper(CanHandle *hcan, MmrCanPacket packet); + /** * @brief - * Receives a can message from the network. + * Tries to receive a message. + */ +MmrResult MMR_CAN_TryReceive(CanHandle *hcan, MmrCanMessage *result); + +/** + * @brief + * Reads a CAN message and stores it inside the + * given MmrCanMessage struct. + * + * If a multi-frame message is received, this function will block + * and read every frame for that particular message. */ HalStatus MMR_CAN_Receive(CanHandle *hcan, MmrCanMessage *result); diff --git a/Inc/mmr_can_includes.h b/Inc/mmr_can_includes.h index b2a735d..1f2a21f 100644 --- a/Inc/mmr_can_includes.h +++ b/Inc/mmr_can_includes.h @@ -11,6 +11,8 @@ #ifndef INC_MMR_CAN_INCLUDES_H_ #define INC_MMR_CAN_INCLUDES_H_ +#include "main.h" + #ifndef CAN #define CAN #endif diff --git a/Inc/mmr_can_message_id.h b/Inc/mmr_can_message_id.h index 3d9598e..cdeb956 100644 --- a/Inc/mmr_can_message_id.h +++ b/Inc/mmr_can_message_id.h @@ -10,6 +10,10 @@ * MMR_CAN_MESSAGE_ID_POINT might be interpreted as * a message carrying a struct Point { int x; int y; };, * and thus deserialized accordingly. + * + * You can find a table with more in-depth explanations on + * Google Drive: + * https://docs.google.com/spreadsheets/d/1GIC0_FuhCjXfBOg_dUWzoEsl5TG7SEOR */ #ifndef INC_MMR_CAN_MESSAGE_ID_H_ @@ -22,8 +26,8 @@ /** * @brief * Message ids are 10 bits wide - * The first 3 bits determine the message id type, while - * the other 7 determine the subtype. + * The first 5 bits determine the message id type, while + * the other 5 determine the subtype. * * Ids scheme: * @@ -117,6 +121,7 @@ enum MmrCanMessageId { // !AUTONOMOUS_MISSION_CONTROL // DRIVE MMR_CAN_MESSAGE_ID_D_STEERING_ANGLE = 160, + MMR_CAN_MESSAGE_ID_D_BREAKING_PERCENTAGE, // !DRIVE // ECU_BOSCH MMR_CAN_MESSAGE_ID_ECU_BOSCH_ = 192, diff --git a/Inc/mmr_can_msg_conversions.h b/Inc/mmr_can_msg_conversions.h new file mode 100644 index 0000000..c27f010 --- /dev/null +++ b/Inc/mmr_can_msg_conversions.h @@ -0,0 +1,23 @@ +/* + * mmr_can_msg_conversions.h + * + * Created on: Apr 11, 2022 + * Author: Stefa + */ + +#ifndef INC_MMR_CAN_MSG_CONVERSIONS_H_ +#define INC_MMR_CAN_MSG_CONVERSIONS_H_ + +#include + +#define MMR_CAN_MsgToByte(msg) (*(uint8_t*)(msg.store)) +#define MMR_CAN_MsgToInt16(msg) (*(int16_t*)(msg.store)) +#define MMR_CAN_MsgToUint16(msg) (*(uint16_t*)(msg.store)) +#define MMR_CAN_MsgToInt32(msg) (*(int32_t*)(msg.store)) +#define MMR_CAN_MsgToUint32(msg) (*(uint32_t*)(msg.store)) +#define MMR_CAN_MsgToInt64(msg) (*(int64_t*)(msg.store)) +#define MMR_CAN_MsgToUint64(msg) (*(uint64_t*)(msg.store)) +#define MMR_CAN_MsgToFloat(msg) (*(float*)(msg.store)) +#define MMR_CAN_MsgToDouble(msg) (*(double*)(msg.store)) + +#endif /* INC_MMR_CAN_MSG_CONVERSIONS_H_ */ diff --git a/Inc/mmr_can_util.h b/Inc/mmr_can_util.h index 6c3790e..c9382fe 100644 --- a/Inc/mmr_can_util.h +++ b/Inc/mmr_can_util.h @@ -73,11 +73,11 @@ * * Asynchronous logic can be easily implemented using State Machines */ -typedef enum MmrAsyncResult { - MMR_ASYNC_RESULT_ERROR, - MMR_ASYNC_RESULT_PENDING, - MMR_ASYNC_RESULT_COMPLETED, -} MmrAsyncResult; +typedef enum MmrResult { + MMR_RESULT_ERROR, + MMR_RESULT_PENDING, + MMR_RESULT_COMPLETED, +} MmrResult; #endif /* INC_MMR_CAN_UTIL_H_ */ diff --git a/Src/mmr_can_receive.c b/Src/mmr_can_receive.c index a3639ff..8480b85 100644 --- a/Src/mmr_can_receive.c +++ b/Src/mmr_can_receive.c @@ -20,14 +20,20 @@ static HalStatus receiveAll(ReceptionParams *rp); static bool headerIsMultiFrame(MmrCanHeader header, CanId targetId); -/** - * @brief - * Reads a CAN message and stores it inside the - * given MmrCanMessage struct. - * - * If a multi-frame message is received, this function will block - * and read every frame for that particular message. - */ +MmrResult MMR_CAN_TryReceive(CanHandle *hcan, MmrCanMessage *result) { + size_t pendingMessages = + HAL_CAN_GetRxFifoFillLevel(hcan, MMR_CAN_RX_FIFO); + + if (pendingMessages > 0) { + return MMR_CAN_Receive(hcan, result) != HAL_OK + ? MMR_RESULT_COMPLETED + : MMR_RESULT_ERROR; + } + + return MMR_RESULT_PENDING; +} + + HalStatus MMR_CAN_Receive(CanHandle *hcan, MmrCanMessage *result) { ReceptionParams rp = { .handle = hcan,