Add TryReceive

This commit is contained in:
Stefano Calabretti
2022-04-11 15:50:22 +02:00
parent 94ecc5ceeb
commit 49b541775a
4 changed files with 37 additions and 16 deletions
+12 -1
View File
@@ -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);
+6 -2
View File
@@ -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:
*
+5 -5
View File
@@ -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_ */
+14 -8
View File
@@ -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,