Improvements (#11)

* Use default include

* Add conversions

* Add TryReceive
This commit was merged in pull request #11.
This commit is contained in:
Stefano Calabretti
2022-04-11 15:50:56 +02:00
committed by GitHub
parent aec32cb58e
commit efdd4655a3
6 changed files with 63 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);
+2
View File
@@ -11,6 +11,8 @@
#ifndef INC_MMR_CAN_INCLUDES_H_
#define INC_MMR_CAN_INCLUDES_H_
#include "main.h"
#ifndef CAN
#define CAN
#endif
+7 -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:
*
@@ -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,
+23
View File
@@ -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 <stdint.h>
#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_ */
+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,