Add event handlers (#1)
* Add rx event handlers * Fix compile errors and add constness * Pass sender id * Change api interface * Revert changes * Add filter fifo
This commit is contained in:
committed by
GitHub
parent
503b333478
commit
90d1836dfa
@@ -11,6 +11,12 @@
|
||||
#define MMR_CAN_RX_FIFO CAN_RX_FIFO0
|
||||
#endif
|
||||
|
||||
#if MMR_CAN_RX_FIFO == CAN_RX_FIFO0
|
||||
#define MMR_CAN_FILTER_FIFO CAN_FILTER_FIFO0
|
||||
#else
|
||||
#define MMR_CAN_FILTER_FIFO CAN_FILTER_FIFO1
|
||||
#endif
|
||||
|
||||
#if MMR_CAN_RX_FIFO == CAN_RX_FIFO0
|
||||
#define MMR_CAN_RX_INTERRUPT CAN_IT_RX_FIFO0_MSG_PENDING
|
||||
#else
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef INC_MMR_CAN_EVENTS_H_
|
||||
#define INC_MMR_CAN_EVENTS_H_
|
||||
|
||||
#include "mmr_can.h"
|
||||
|
||||
typedef struct {
|
||||
CanId senderId;
|
||||
uint8_t *message;
|
||||
} MmrCanEvent;
|
||||
|
||||
typedef void (*MmrCanEventHandler)(MmrCanEvent *event);
|
||||
|
||||
typedef struct {
|
||||
const MmrCanEventHandler *handlers;
|
||||
const size_t count;
|
||||
} MmrCanEventList;
|
||||
|
||||
|
||||
void MMR_CAN_InitRxHandlers(const MmrCanEventList *rxEvents);
|
||||
|
||||
|
||||
#endif /* INC_MMR_CAN_EVENTS_H_ */
|
||||
+1
-1
@@ -33,7 +33,7 @@ typedef uint8_t CanFilterBank;
|
||||
|
||||
typedef HAL_StatusTypeDef HalStatus;
|
||||
typedef CAN_HandleTypeDef CanHandle;
|
||||
typedef CAN_TxHeaderTypeDef CanTxHeader;
|
||||
typedef CAN_RxHeaderTypeDef CanRxHeader;
|
||||
typedef CAN_TxHeaderTypeDef CanTxHeader;
|
||||
|
||||
#endif /* INC_MMR_CAN_TYPES_H_ */
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define arrayLength(array) \
|
||||
(sizeof(array) / sizeof(*(array)))
|
||||
|
||||
#define stringArrayLength(array) \
|
||||
stringBufferLength((array), sizeof(array))
|
||||
|
||||
|
||||
+1
-19
@@ -27,7 +27,7 @@ CanFilterMask MMR_CAN_AlignStandardMask(CanFilterMask baseMask) {
|
||||
MmrCanFilterSettings MMR_CAN_GetDefaultFilterSettings() {
|
||||
return (MmrCanFilterSettings) {
|
||||
.enabled = true,
|
||||
.fifo = MMR_CAN_RX_FIFO,
|
||||
.fifo = MMR_CAN_FILTER_FIFO,
|
||||
.idMask = 0,
|
||||
.bank = 0,
|
||||
.slaveBankStart = 14,
|
||||
@@ -46,21 +46,3 @@ HalStatus MMR_CAN_Send(CanHandle *hcan, MmrCanPacket packet) {
|
||||
|
||||
return HAL_CAN_AddTxMessage(hcan, &header, packet.data, packet.mailbox);
|
||||
}
|
||||
|
||||
|
||||
static void __handleCanRxInterrupt(CAN_HandleTypeDef *hcan) {
|
||||
static CanRxHeader rxHeader = {};
|
||||
static CanRxBuffer rxData = {};
|
||||
|
||||
HAL_CAN_GetRxMessage(hcan, MMR_CAN_RX_FIFO, &rxHeader, rxData);
|
||||
}
|
||||
|
||||
#if MMR_CAN_RX_FIFO == CAN_RX_FIFO0
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
|
||||
__handleCanRxInterrupt(hcan);
|
||||
}
|
||||
#elif MMR_CAN_RX_FIFO == CAN_RX_FIFO1
|
||||
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) {
|
||||
__handleCanRxInterrupt(hcan);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "mmr_can.h"
|
||||
#include "mmr_can_events.h"
|
||||
|
||||
|
||||
static const MmrCanEventList *_rxEvents = NULL;
|
||||
|
||||
|
||||
static void __handleCanRxInterrupt(CanHandle *hcan);
|
||||
static void __invokeAll(const MmrCanEventList *events, MmrCanEvent *event);
|
||||
static void __maybeInvoke(const MmrCanEventHandler handler, MmrCanEvent *event);
|
||||
|
||||
|
||||
void MMR_CAN_InitRxHandlers(const MmrCanEventList *rxEvents) {
|
||||
_rxEvents = rxEvents;
|
||||
}
|
||||
|
||||
|
||||
static void __handleCanRxInterrupt(CanHandle *hcan) {
|
||||
static CanRxHeader rxHeader = {};
|
||||
static CanRxBuffer rxData = {};
|
||||
|
||||
HAL_CAN_GetRxMessage(hcan, MMR_CAN_RX_FIFO, &rxHeader, rxData);
|
||||
|
||||
MmrCanEvent event = {
|
||||
.senderId = rxHeader.StdId,
|
||||
.message = rxData,
|
||||
};
|
||||
|
||||
__invokeAll(_rxEvents, &event);
|
||||
}
|
||||
|
||||
static void __invokeAll(const MmrCanEventList *events, MmrCanEvent *event) {
|
||||
if (!events) {
|
||||
return;
|
||||
}
|
||||
|
||||
short i;
|
||||
for (i = 0; i < events->count; i++) {
|
||||
__maybeInvoke(events->handlers[i], event);
|
||||
}
|
||||
}
|
||||
|
||||
static always_inline void __maybeInvoke(const MmrCanEventHandler handler, MmrCanEvent *event) {
|
||||
if (handler) {
|
||||
handler(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if MMR_CAN_RX_FIFO == CAN_RX_FIFO0
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CanHandle *hcan) {
|
||||
__handleCanRxInterrupt(hcan);
|
||||
}
|
||||
#elif MMR_CAN_RX_FIFO == CAN_RX_FIFO1
|
||||
void HAL_CAN_RxFifo1MsgPendingCallback(CanHandle *hcan) {
|
||||
__handleCanRxInterrupt(hcan);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user