From 49b541775a8ad74c8944ca3fe8de92f3cb066a7a Mon Sep 17 00:00:00 2001 From: Stefano Calabretti Date: Mon, 11 Apr 2022 15:50:22 +0200 Subject: [PATCH] Add TryReceive --- Inc/mmr_can.h | 13 ++++++++++++- Inc/mmr_can_message_id.h | 8 ++++++-- Inc/mmr_can_util.h | 10 +++++----- Src/mmr_can_receive.c | 22 ++++++++++++++-------- 4 files changed, 37 insertions(+), 16 deletions(-) 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_message_id.h b/Inc/mmr_can_message_id.h index 5dde863..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: * 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,