This repository has been archived on 2026-02-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
can/Src/mmr_can_events.c
T
Stefano Calabretti ed2a26fbc9 Better parameter handling (#6)
* Add rx event handlers

* Fix compile errors and add constness

* Pass sender id

* Change api interface

* Revert changes

* Add filter fifo

* Add default setup

* Add receive function

* Remove interrupt activation

* Activate RX interrupts

* Merge remote changes

* Add event list constructor

* Fix compilation bug

* Send and receive multiple frames

* Refactor

* Refactor

* Fix compile error

* Use known syntax

* Refactor

* Provide storage

* Add frame end

* Add some documentation

* Turn macros into functions

* Refactor

* Refactor

* Fix compile error

* Use ExtendedIds

* Fix reception bug (fixes #4)

* Add packet headers

* Refactor

Co-authored-by: Riccardo998 <riccardo.storchi98@gmail.com>
2021-12-20 15:12:57 +01:00

74 lines
1.6 KiB
C

#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, const MmrCanMessage *event);
static void __maybeInvoke(const MmrCanEventHandler handler, const MmrCanMessage *event);
/**
* @brief
* Activates the CAN rx interrupts
*
* When one is fired, the callbacks provided inside the
* MmrCanEventList will be invoked
*/
HalStatus MMR_CAN_InitRxHandlers(CanHandle *hcan, const MmrCanEventList *rxEvents) {
_rxEvents = rxEvents;
return HAL_CAN_ActivateNotification(hcan, MMR_CAN_RX_INTERRUPT);
}
/**
* @brief
* Stores the message inside a byte buffer,
* and then sends it to every registered event handler
*/
static void __handleCanRxInterrupt(CanHandle *hcan) {
static CanRxBuffer buffer = {};
static MmrCanMessage event = {
.store = buffer,
};
MMR_CAN_Receive(hcan, &event);
__invokeAll(_rxEvents, &event);
}
static void __invokeAll(
const MmrCanEventList *events,
const MmrCanMessage *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,
const MmrCanMessage *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