Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 772d87b3db | |||
| bf81796125 | |||
| f34e2a1fc6 |
+36
-3
@@ -46,6 +46,15 @@ typedef uint32_t (*MmrCanTickProvider)();
|
|||||||
typedef uint8_t CanRxBuffer[MMR_CAN_MAX_DATA_LENGTH];
|
typedef uint8_t CanRxBuffer[MMR_CAN_MAX_DATA_LENGTH];
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct MmrCan {
|
||||||
|
CanId id;
|
||||||
|
CanHandle *handle;
|
||||||
|
CanFifo fifo;
|
||||||
|
CanMailbox mailboxes[MAILBOXES_COUNT];
|
||||||
|
uint8_t currentMailbox;
|
||||||
|
} MmrCan;
|
||||||
|
|
||||||
|
|
||||||
typedef struct MmrCanFilterSettings {
|
typedef struct MmrCanFilterSettings {
|
||||||
bool enabled;
|
bool enabled;
|
||||||
|
|
||||||
@@ -115,7 +124,7 @@ typedef struct MmrCanFilterSettings {
|
|||||||
*/
|
*/
|
||||||
typedef struct MmrCanPacket {
|
typedef struct MmrCanPacket {
|
||||||
MmrCanHeader header;
|
MmrCanHeader header;
|
||||||
uint8_t *data;
|
void *data;
|
||||||
uint8_t length;
|
uint8_t length;
|
||||||
} MmrCanPacket;
|
} MmrCanPacket;
|
||||||
|
|
||||||
@@ -173,6 +182,22 @@ typedef struct MmrCanMessage {
|
|||||||
extern MmrCanTickProvider __mmr_can_tickProvider;
|
extern MmrCanTickProvider __mmr_can_tickProvider;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
MmrCan MMR_CAN_Create(CanHandle *hcan, CanFifo rxFifo);
|
||||||
|
HalStatus MMR_CAN_Start(MmrCan *can);
|
||||||
|
HalStatus MMR_CAN_EnableFilter(MmrCan *can, CanFilterMask mask, CanFilterBank bank);
|
||||||
|
|
||||||
|
HalStatus MMR_CAN_SendString(MmrCan *can, MmrCanMessageId msgId, const char *data);
|
||||||
|
HalStatus MMR_CAN_SendInt(MmrCan *can, MmrCanMessageId msgId, int data);
|
||||||
|
HalStatus MMR_CAN_SendFloat(MmrCan *can, MmrCanMessageId msgId, float data);
|
||||||
|
HalStatus MMR_CAN_SendRaw(MmrCan *can, MmrCanMessageId msgId, void *data, uint8_t length);
|
||||||
|
HalStatus MMR_CAN_SendPacket(MmrCan *can, MmrCanPacket packet);
|
||||||
|
|
||||||
|
|
||||||
|
#define MMR_CAN_SendStruct(pcan, messageId, pdata) \
|
||||||
|
MMR_CAN_SendRaw(pcan, messageId, pdata, sizeof(*pdata))
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
* Sets the tick provider.
|
* Sets the tick provider.
|
||||||
@@ -209,6 +234,14 @@ HalStatus MMR_CAN_FilterConfig(CanHandle *hcan, MmrCanFilterSettings settings);
|
|||||||
*/
|
*/
|
||||||
MmrCanFilterSettings MMR_CAN_GetDefaultFilterSettings();
|
MmrCanFilterSettings MMR_CAN_GetDefaultFilterSettings();
|
||||||
|
|
||||||
|
|
||||||
|
HalStatus MMR_CAN_SendString(MmrCan *can, MmrCanMessageId msgId, const char *data);
|
||||||
|
HalStatus MMR_CAN_SendInt(MmrCan *can, MmrCanMessageId msgId, int data);
|
||||||
|
HalStatus MMR_CAN_SendFloat(MmrCan *can, MmrCanMessageId msgId, float data);
|
||||||
|
HalStatus MMR_CAN_SendRaw(MmrCan *can, MmrCanMessageId msgId, void *data, uint8_t length);
|
||||||
|
HalStatus MMR_CAN_SendPacket(MmrCan *can, MmrCanPacket packet);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
* Sends a can packet over the network.
|
* Sends a can packet over the network.
|
||||||
@@ -235,7 +268,7 @@ HalStatus MMR_CAN_SendNoTamper(CanHandle *hcan, MmrCanPacket packet);
|
|||||||
* @brief
|
* @brief
|
||||||
* Tries to receive a message.
|
* Tries to receive a message.
|
||||||
*/
|
*/
|
||||||
MmrResult MMR_CAN_TryReceive(CanHandle *hcan, MmrCanMessage *result);
|
MmrResult MMR_CAN_TryReceive(MmrCan *can, MmrCanMessage *result);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
@@ -245,6 +278,6 @@ MmrResult MMR_CAN_TryReceive(CanHandle *hcan, MmrCanMessage *result);
|
|||||||
* If a multi-frame message is received, this function will block
|
* If a multi-frame message is received, this function will block
|
||||||
* and read every frame for that particular message.
|
* and read every frame for that particular message.
|
||||||
*/
|
*/
|
||||||
HalStatus MMR_CAN_Receive(CanHandle *hcan, MmrCanMessage *result);
|
HalStatus MMR_CAN_Receive(MmrCan *can, MmrCanMessage *result);
|
||||||
|
|
||||||
#endif /* INC_MMR_CAN_H_ */
|
#endif /* INC_MMR_CAN_H_ */
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef MMR_CAN_ECU_MESSAGES_H_
|
||||||
|
#define MMR_CAN_ECU_MESSAGES_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct EcuPedalThrottle {
|
||||||
|
float apsAVoltage;
|
||||||
|
float apsBVoltage;
|
||||||
|
float throttleAVoltage;
|
||||||
|
float throttleBVoltage;
|
||||||
|
} EcuPedalThrottle;
|
||||||
|
|
||||||
|
typedef struct EcuTemperatures {
|
||||||
|
int16_t oil;
|
||||||
|
int16_t engine;
|
||||||
|
int16_t intake;
|
||||||
|
int16_t ambient;
|
||||||
|
} EcuTemperatures;
|
||||||
|
|
||||||
|
typedef struct EcuEngineFn1 {
|
||||||
|
float engineRpm;
|
||||||
|
float gear;
|
||||||
|
uint16_t vehicleSpeed;
|
||||||
|
uint16_t throttle;
|
||||||
|
} EcuEngineFn1;
|
||||||
|
|
||||||
|
typedef struct EcuPressures {
|
||||||
|
float oil;
|
||||||
|
float fuel;
|
||||||
|
float intake;
|
||||||
|
float ambient;
|
||||||
|
} EcuPressures;
|
||||||
|
|
||||||
|
typedef struct EcuEngineFn2 {
|
||||||
|
float batteryVoltage;
|
||||||
|
float acceleratorPedal;
|
||||||
|
float fanControl;
|
||||||
|
} EcuEngineFn2;
|
||||||
|
|
||||||
|
#endif // !MMR_CAN_ECU_MESSAGES_H_
|
||||||
@@ -79,7 +79,5 @@ MmrCanHeader MMR_CAN_HeaderFromBits(uint32_t bits);
|
|||||||
* Tells wether the given header represents an SCS.
|
* Tells wether the given header represents an SCS.
|
||||||
*/
|
*/
|
||||||
bool MMR_CAN_IsHeaderScs(MmrCanHeader header);
|
bool MMR_CAN_IsHeaderScs(MmrCanHeader header);
|
||||||
bool MMR_CAN_IsMultiFrame(MmrCanHeader header);
|
|
||||||
bool MMR_CAN_IsMultiFrameEnd(MmrCanHeader header);
|
|
||||||
|
|
||||||
#endif /* INC_MMR_CAN_HEADER_H_ */
|
#endif /* INC_MMR_CAN_HEADER_H_ */
|
||||||
|
|||||||
@@ -126,8 +126,28 @@ enum MmrCanMessageId {
|
|||||||
MMR_CAN_MESSAGE_ID_D_SPEED_ODOMETRY,
|
MMR_CAN_MESSAGE_ID_D_SPEED_ODOMETRY,
|
||||||
// !DRIVE
|
// !DRIVE
|
||||||
// ECU_BOSCH
|
// ECU_BOSCH
|
||||||
MMR_CAN_MESSAGE_ID_ECU_BOSCH_ = 192,
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_SPEED = 192,
|
||||||
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_GEAR,
|
||||||
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_RPM,
|
||||||
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_THROTTLE,
|
||||||
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_TEMP_OIL,
|
||||||
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_TEMP_WATER,
|
||||||
|
MMR_CAN_MESSAGE_ID_ECU_BOSCH_TORQUE,
|
||||||
// !ECU_BOSCH
|
// !ECU_BOSCH
|
||||||
|
// CONTROL_SIGNAL
|
||||||
|
MMR_CAN_MESSAGE_ID_CS_CLUTCH_SIGNAL = 224,
|
||||||
|
MMR_CAN_MESSAGE_ID_CS_CLUTCH_FEEDBACK,
|
||||||
|
// !CONTROL_SIGNAL
|
||||||
|
// STEERING
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ERROR_LEFT_X = 256,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ERROR_RIGHT_X,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ODOMETRY_MIN_SPEED_LEFT_X,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ODOMETRY_MIN_SPEED_LEFT_Y,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ODOMETRY_MIN_SPEED_RIGHT_X,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ODOMETRY_MIN_SPEED_RIGHT_Y,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ODOMETRY_MAX_SPEED_LEFT_Y,
|
||||||
|
MMR_CAN_MESSAGE_ID_ST_PROPORTIONAL_ODOMETRY_MAX_SPEED_RIGHT_Y,
|
||||||
|
// !STEERING
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !INC_MMR_CAN_MESSAGE_ID_H_
|
#endif // !INC_MMR_CAN_MESSAGE_ID_H_
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
typedef uint32_t CanId;
|
typedef uint32_t CanId;
|
||||||
typedef uint32_t CanMailbox;
|
typedef uint32_t CanMailbox;
|
||||||
|
typedef uint32_t CanFifo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#include "mmr_can_ecu_messages.h"
|
||||||
|
#include "mmr_can.h"
|
||||||
|
|
||||||
|
#define ecuToHost(byteA, byteB) ((byteA << 8) | (byteB))
|
||||||
|
|
||||||
|
|
||||||
|
EcuPedalThrottle MMR_CAN_GetEcuPedalThrottle(MmrCanMessage *message) {
|
||||||
|
uint8_t *buffer = message->store
|
||||||
|
return (EcuPedalThrottle) {
|
||||||
|
.apsAVoltage = ecuToHost(buffer[0], buffer[1]) / 1000,
|
||||||
|
.apsBVoltage = ecuToHost(buffer[2], buffer[3]) / 1000,
|
||||||
|
.throttleAVoltage = ecuToHost(buffer[4], buffer[5]) / 1000,
|
||||||
|
.throttleBVoltage = ecuToHost(buffer[6], buffer[7]) / 1000,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
EcuTemperatures MMR_CAN_GetEcuTemperatures(MmrCanMessage *message) {
|
||||||
|
uint8_t *buffer = message->store
|
||||||
|
return (EcuTemperatures) {
|
||||||
|
.oil = ecuToHost(buffer[0], buffer[1]) - 40,
|
||||||
|
.engine = ecuToHost(buffer[2], buffer[3]) - 40,
|
||||||
|
.intake = ecuToHost(buffer[4], buffer[5]) - 40,
|
||||||
|
.ambient = ecuToHost(buffer[6], buffer[7]) - 40,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
EcuEngineFn1 MMR_CAN_GetEcuEngineFn1(MmrCanMessage *message) {
|
||||||
|
uint8_t *buffer = message->store
|
||||||
|
return (EcuEngineFn1) {
|
||||||
|
.engineRpm = ecuToHost(buffer[0], buffer[1]),
|
||||||
|
.gear = ecuToHost(buffer[2], buffer[3])
|
||||||
|
.vehicleSpeed = ecuToHost(buffer[4], buffer[5]) / 100,
|
||||||
|
.throttle = ecuToHost(buffer[6], buffer[7]) / 100,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
EcuPressures MMR_CAN_GetEcuPressures(MmrCanMessage *message) {
|
||||||
|
uint8_t *buffer = message->store
|
||||||
|
return (EcuPressures) {
|
||||||
|
.oil = ecuToHost(buffer[0], buffer[1]) / 100,
|
||||||
|
.fuel = ecuToHost(buffer[2], buffer[3]) / 100,
|
||||||
|
.intake = ecuToHost(buffer[4], buffer[5]) / 10,
|
||||||
|
.ambient = ecuToHost(buffer[6], buffer[7]) / 10,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
EcuEngineFn2 MMR_CAN_GetEcuEngineFn2(MmrCanMessage *message) {
|
||||||
|
uint8_t *buffer = message->store
|
||||||
|
return (EcuEngineFn2) {
|
||||||
|
.batteryVoltage = ecuToHost(buffer[0], buffer[1]) / 1000,
|
||||||
|
.acceleratorPedal = ecuToHost(buffer[2], buffer[3]) / 100,
|
||||||
|
.fanControl = ecuToHost(buffer[4], buffer[5]) / 100,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,6 +2,11 @@
|
|||||||
#include "mmr_can_util.h"
|
#include "mmr_can_util.h"
|
||||||
#include "mmr_can_optimize.h"
|
#include "mmr_can_optimize.h"
|
||||||
|
|
||||||
|
struct MmrCanHeader {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
uint32_t MMR_CAN_HeaderToBits(MmrCanHeader header) {
|
uint32_t MMR_CAN_HeaderToBits(MmrCanHeader header) {
|
||||||
return 0
|
return 0
|
||||||
| ((uint32_t)header.priority << 26)
|
| ((uint32_t)header.priority << 26)
|
||||||
@@ -25,11 +30,3 @@ MmrCanHeader MMR_CAN_HeaderFromBits(uint32_t bits) {
|
|||||||
bool MMR_CAN_IsHeaderScs(MmrCanHeader header) {
|
bool MMR_CAN_IsHeaderScs(MmrCanHeader header) {
|
||||||
return header.messageType == MMR_CAN_MESSAGE_TYPE_SCS;
|
return header.messageType == MMR_CAN_MESSAGE_TYPE_SCS;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MMR_CAN_IsMultiFrame(MmrCanHeader header) {
|
|
||||||
return header.messageType == MMR_CAN_MESSAGE_TYPE_MULTI_FRAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MMR_CAN_IsMultiFrameEnd(MmrCanHeader header) {
|
|
||||||
return header.messageType == MMR_CAN_MESSAGE_TYPE_MULTI_FRAME_END;
|
|
||||||
}
|
|
||||||
|
|||||||
+25
-65
@@ -1,26 +1,29 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include "mmr_can.h"
|
#include "mmr_can.h"
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
#define cast(pmsg, to) (*(to*)(pmsg->store))
|
||||||
CanHandle *handle;
|
|
||||||
uint8_t *result;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
CanRxHeader rx;
|
|
||||||
MmrCanHeader mmr;
|
|
||||||
} headers;
|
|
||||||
|
|
||||||
uint8_t fifo;
|
|
||||||
} ReceptionParams;
|
|
||||||
|
|
||||||
|
|
||||||
static HalStatus receiveOne(ReceptionParams *rp);
|
uint16_t MMR_CAN_GetUInt16(MmrCanMessage *message) {
|
||||||
static HalStatus receiveAll(ReceptionParams *rp);
|
return cast(message, uint16_t);
|
||||||
static bool headerIsMultiFrame(MmrCanHeader header, CanId targetId);
|
}
|
||||||
|
|
||||||
|
uint32_t MMR_CAN_GetUInt32(MmrCanMessage *message) {
|
||||||
|
return cast(message, uint32_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t MMR_CAN_GetUInt64(MmrCanMessage *message) {
|
||||||
|
return cast(message, uint64_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
float MMR_CAN_GetFloat(MmrCanMessage *message) {
|
||||||
|
return cast(message, float);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
MmrResult MMR_CAN_TryReceive(CanHandle *hcan, MmrCanMessage *result) {
|
MmrResult MMR_CAN_TryReceive(MmrCan *can, MmrCanMessage *result) {
|
||||||
size_t pendingMessages =
|
size_t pendingMessages =
|
||||||
HAL_CAN_GetRxFifoFillLevel(hcan, MMR_CAN_RX_FIFO);
|
HAL_CAN_GetRxFifoFillLevel(hcan, MMR_CAN_RX_FIFO);
|
||||||
|
|
||||||
@@ -34,58 +37,15 @@ MmrResult MMR_CAN_TryReceive(CanHandle *hcan, MmrCanMessage *result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
HalStatus MMR_CAN_Receive(CanHandle *hcan, MmrCanMessage *result) {
|
HalStatus MMR_CAN_Receive(MmrCan *can, MmrCanMessage *result) {
|
||||||
ReceptionParams rp = {
|
CanRxHeader rxHeader = {};
|
||||||
.handle = hcan,
|
|
||||||
.result = (uint8_t*)result->store,
|
|
||||||
.fifo = MMR_CAN_RX_FIFO,
|
|
||||||
};
|
|
||||||
|
|
||||||
HalStatus status = receiveOne(&rp);
|
|
||||||
if (status != HAL_OK) {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
result->header = rp.headers.mmr;
|
|
||||||
if (MMR_CAN_IsMultiFrame(rp.headers.mmr)) {
|
|
||||||
status |= receiveAll(&rp);
|
|
||||||
}
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static HalStatus receiveAll(ReceptionParams *rp) {
|
|
||||||
CanId targetId = rp->headers.mmr.senderId;
|
|
||||||
HalStatus status = HAL_OK;
|
|
||||||
do {
|
|
||||||
rp->result += MMR_CAN_MAX_DATA_LENGTH;
|
|
||||||
status |= receiveOne(rp);
|
|
||||||
}
|
|
||||||
while (
|
|
||||||
headerIsMultiFrame(rp->headers.mmr, targetId) && status == HAL_OK
|
|
||||||
);
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static HalStatus receiveOne(ReceptionParams *rp) {
|
|
||||||
HalStatus status = HAL_CAN_GetRxMessage(
|
HalStatus status = HAL_CAN_GetRxMessage(
|
||||||
rp->handle,
|
can->handle,
|
||||||
rp->fifo,
|
can->fifo,
|
||||||
&rp->headers.rx,
|
&rxHeader,
|
||||||
rp->result
|
result->store
|
||||||
);
|
);
|
||||||
|
|
||||||
rp->headers.mmr = MMR_CAN_HeaderFromBits(rp->headers.rx.ExtId);
|
result->header = MMR_CAN_HeaderFromBits(rxHeader.ExtId);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool headerIsMultiFrame(MmrCanHeader header, CanId targetId) {
|
|
||||||
return
|
|
||||||
MMR_CAN_IsMultiFrame(header) &&
|
|
||||||
!MMR_CAN_IsMultiFrameEnd(header) &&
|
|
||||||
header.senderId == targetId;
|
|
||||||
}
|
|
||||||
|
|||||||
+59
-151
@@ -2,174 +2,82 @@
|
|||||||
#include "mmr_can.h"
|
#include "mmr_can.h"
|
||||||
#include "mmr_can_util.h"
|
#include "mmr_can_util.h"
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
CanHandle *handle;
|
|
||||||
MmrCanPacket *packet;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
CanTxHeader tx;
|
|
||||||
MmrCanHeader mmr;
|
|
||||||
} headers;
|
|
||||||
} TransmissionParams;
|
|
||||||
|
|
||||||
|
|
||||||
static TransmissionParams buildParams(CanHandle *hcan, MmrCanPacket *packet);
|
|
||||||
static HalStatus send(TransmissionParams *tp);
|
|
||||||
static HalStatus sendNormal(TransmissionParams *tp);
|
|
||||||
static HalStatus sendMulti(TransmissionParams *tp);
|
|
||||||
static HalStatus sendSingleMultiFrame(TransmissionParams *tp);
|
|
||||||
static uint8_t computeFramesToSend(MmrCanPacket *packet);
|
|
||||||
static uint8_t computeNextMessageLength(MmrCanPacket *packet);
|
|
||||||
static void setMessageType(TransmissionParams *header, MmrCanMessageType type);
|
|
||||||
static void syncHeaders(TransmissionParams *tp);
|
|
||||||
static CanMailbox *getNextMailbox();
|
|
||||||
|
|
||||||
|
|
||||||
#define MAILBOXES_COUNT 3
|
#define MAILBOXES_COUNT 3
|
||||||
|
|
||||||
static CanMailbox __mailboxes[MAILBOXES_COUNT] = {};
|
static MmrCanHeader buildDefaultHeader(MmrCan *can, MmrCanMessageId msgId);
|
||||||
static uint8_t __currentMailbox = 0;
|
static CanTxHeader buildTxHeader(MmrCan *can, MmrCanPacket *packet);
|
||||||
|
static CanMailbox *getNextMailbox(MmrCan *can);
|
||||||
|
|
||||||
|
|
||||||
HalStatus MMR_CAN_Send(CanHandle *hcan, MmrCanPacket packet) {
|
HalStatus MMR_CAN_SendString(MmrCan *can, MmrCanMessageId msgId, const char *data) {
|
||||||
TransmissionParams tp =
|
uint8_t len =
|
||||||
buildParams(hcan, &packet);
|
strnlen(data, MMR_CAN_MAX_DATA_LENGTH);
|
||||||
|
|
||||||
syncHeaders(&tp);
|
return MMR_CAN_SendRaw(can, msgId, &data, len);
|
||||||
return packet.length <= MMR_CAN_MAX_DATA_LENGTH
|
|
||||||
? sendNormal(&tp)
|
|
||||||
: sendMulti(&tp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
HalStatus MMR_CAN_SendNoTamper(CanHandle *hcan, MmrCanPacket packet) {
|
HalStatus MMR_CAN_SendInt(MmrCan *can, MmrCanMessageId msgId, int data) {
|
||||||
TransmissionParams tp =
|
return MMR_CAN_SendRaw(can, msgId, &data, sizeof(data))
|
||||||
buildParams(hcan, &packet);
|
|
||||||
|
|
||||||
return send(&tp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static TransmissionParams buildParams(CanHandle *hcan, MmrCanPacket *packet) {
|
HalStatus MMR_CAN_SendFloat(MmrCan *can, MmrCanMessageId msgId, float data) {
|
||||||
return (TransmissionParams) {
|
return MMR_CAN_SendRaw(can, msgId, &data, sizeof(data));
|
||||||
.handle = hcan,
|
}
|
||||||
.packet = packet,
|
|
||||||
.headers.mmr = packet->header,
|
|
||||||
.headers.tx = {
|
HalStatus MMR_CAN_SendRaw(
|
||||||
.IDE = CAN_ID_EXT,
|
MmrCan *can,
|
||||||
.RTR = CAN_RTR_DATA,
|
MmrCanMessageId msgId,
|
||||||
.DLC = packet->length,
|
void *data,
|
||||||
.TransmitGlobalTime = DISABLE,
|
uint8_t length
|
||||||
},
|
) {
|
||||||
|
return MMR_CAN_SendPacket(can, (MmrCanPacket){
|
||||||
|
.header = buildDefaultHeader(can, msgId),
|
||||||
|
.data = data,
|
||||||
|
.length = length,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
HalStatus MMR_CAN_SendPacket(MmrCan *can, MmrCanPacket packet) {
|
||||||
|
CanTxHeader txHeader =
|
||||||
|
buildTxHeader(can, &packet);
|
||||||
|
|
||||||
|
return HAL_CAN_AddTxMessage(
|
||||||
|
can->handle,
|
||||||
|
&txHeader,
|
||||||
|
(uint8_t*)packet.data,
|
||||||
|
getNextMailbox(can)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static MmrCanHeader buildDefaultHeader(MmrCan *can, MmrCanMessageId msgId) {
|
||||||
|
return (MmrCanHeader){
|
||||||
|
.messageId = msgId,
|
||||||
|
.senderId = can->id,
|
||||||
|
.priority = MMR_CAN_MESSAGE_PRIORITY_NORMAL,
|
||||||
|
.messageType = MMR_CAN_MESSAGE_TYPE_NORMAL,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static HalStatus sendNormal(TransmissionParams *tp) {
|
static CanTxHeader buildTxHeader(MmrCan *can, MmrCanPacket *packet) {
|
||||||
setMessageType(tp, MMR_CAN_MESSAGE_TYPE_NORMAL);
|
return (CanTxHeader){
|
||||||
return send(tp);
|
.ExtId = MMR_CAN_HeaderToBits(packet->header),
|
||||||
|
.IDE = CAN_ID_EXT,
|
||||||
|
.RTR = CAN_RTR_DATA,
|
||||||
|
.DLC = packet->length,
|
||||||
|
.TransmitGlobalTime = DISABLE,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static HalStatus sendMulti(TransmissionParams *tp) {
|
static CanMailbox *getNextMailbox(MmrCan *can) {
|
||||||
HalStatus status = HAL_OK;
|
can->currentMailbox++;
|
||||||
uint8_t framesToSend = computeFramesToSend(tp->packet);
|
can->currentMailbox %= MAILBOXES_COUNT;
|
||||||
|
|
||||||
setMessageType(tp, MMR_CAN_MESSAGE_TYPE_MULTI_FRAME);
|
return can->mailboxes + can->currentMailbox;
|
||||||
do {
|
|
||||||
bool isLastFrame = framesToSend <= 1;
|
|
||||||
if (isLastFrame) {
|
|
||||||
setMessageType(tp, MMR_CAN_MESSAGE_TYPE_MULTI_FRAME_END);
|
|
||||||
}
|
|
||||||
|
|
||||||
status |= sendSingleMultiFrame(tp);
|
|
||||||
}
|
|
||||||
while (framesToSend-- > 1 && status == HAL_OK);
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static HalStatus sendSingleMultiFrame(TransmissionParams *tp) {
|
|
||||||
uint8_t length =
|
|
||||||
computeNextMessageLength(tp->packet);
|
|
||||||
|
|
||||||
tp->headers.tx.DLC = length;
|
|
||||||
tp->packet->length -= length;
|
|
||||||
|
|
||||||
HalStatus result = send(tp);
|
|
||||||
|
|
||||||
tp->packet->data += length;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static HalStatus send(TransmissionParams *tp) {
|
|
||||||
syncHeaders(tp);
|
|
||||||
|
|
||||||
return HAL_CAN_AddTxMessage(
|
|
||||||
tp->handle,
|
|
||||||
&tp->headers.tx,
|
|
||||||
tp->packet->data,
|
|
||||||
getNextMailbox()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static CanMailbox *getNextMailbox() {
|
|
||||||
__currentMailbox++;
|
|
||||||
__currentMailbox %= MAILBOXES_COUNT;
|
|
||||||
|
|
||||||
return __mailboxes + __currentMailbox;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void syncHeaders(TransmissionParams *tp) {
|
|
||||||
tp->headers.tx.ExtId = MMR_CAN_HeaderToBits(tp->headers.mmr);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief
|
|
||||||
* Computes the frames that will need to be sent for a packet.
|
|
||||||
*
|
|
||||||
* For example, if a packet has 15 bytes, the function will return
|
|
||||||
* 2, as one packet is needed for the first 8 bytes and another is
|
|
||||||
* needed for the remaining 7.
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* The computation is pretty simple
|
|
||||||
* Given
|
|
||||||
* length = 17 (bytes)
|
|
||||||
* We'll have
|
|
||||||
* framesToSend = 17 / 8 = 2 (frames)
|
|
||||||
* remainder = 17 % 8 = 1 (bytes)
|
|
||||||
* maybeOneForRemainder = 1 > 0 = 1 (frame)
|
|
||||||
* -------------------------------------------
|
|
||||||
* result = 2 + 1 = 3 (frames)
|
|
||||||
*/
|
|
||||||
static uint8_t computeFramesToSend(MmrCanPacket *packet) {
|
|
||||||
uint8_t length = packet->length;
|
|
||||||
uint8_t framesToSend = length / MMR_CAN_MAX_DATA_LENGTH;
|
|
||||||
uint8_t remainder = length % MMR_CAN_MAX_DATA_LENGTH;
|
|
||||||
uint8_t maybeOneForRemainder = remainder > 0;
|
|
||||||
|
|
||||||
return framesToSend + maybeOneForRemainder;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief
|
|
||||||
* Returns the length for the next message, either
|
|
||||||
* 8 bytes or a lower value.
|
|
||||||
*/
|
|
||||||
static uint8_t computeNextMessageLength(MmrCanPacket *packet) {
|
|
||||||
return min(
|
|
||||||
packet->length, MMR_CAN_MAX_DATA_LENGTH
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static always_inline void setMessageType(TransmissionParams *tp, MmrCanMessageType type) {
|
|
||||||
tp->headers.mmr.messageType = type;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user