Skip to content

Commit 5c63839

Browse files
chgabriel79henrikbrixandersen
authored andcommitted
modules: canopennode: add rxmsg callback
Implement callback for incoming CAN messages for any of the configured filters for CANopenNode. This can be used to wake the loop calling CO_process when a new message was received which needs processing. Signed-off-by: Christian Gabriel <[email protected]>
1 parent ede954c commit 5c63839

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

modules/canopennode/CO_driver.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ K_MUTEX_DEFINE(canopen_send_mutex);
3131
K_MUTEX_DEFINE(canopen_emcy_mutex);
3232
K_MUTEX_DEFINE(canopen_co_mutex);
3333

34+
static canopen_rxmsg_callback_t rxmsg_callback;
35+
3436
inline void canopen_send_lock(void)
3537
{
3638
k_mutex_lock(&canopen_send_mutex, K_FOREVER);
@@ -61,6 +63,11 @@ inline void canopen_od_unlock(void)
6163
k_mutex_unlock(&canopen_co_mutex);
6264
}
6365

66+
void canopen_set_rxmsg_callback(canopen_rxmsg_callback_t callback)
67+
{
68+
rxmsg_callback = callback;
69+
}
70+
6471
static void canopen_detach_all_rx_filters(CO_CANmodule_t *CANmodule)
6572
{
6673
uint16_t i;
@@ -83,6 +90,7 @@ static void canopen_rx_callback(const struct device *dev, struct can_frame *fram
8390
CO_CANmodule_t *CANmodule = (CO_CANmodule_t *)user_data;
8491
CO_CANrxMsg_t rxMsg;
8592
CO_CANrx_t *buffer;
93+
canopen_rxmsg_callback_t callback = rxmsg_callback;
8694
int i;
8795

8896
ARG_UNUSED(dev);
@@ -105,6 +113,9 @@ static void canopen_rx_callback(const struct device *dev, struct can_frame *fram
105113
rxMsg.DLC = frame->dlc;
106114
memcpy(rxMsg.data, frame->data, frame->dlc);
107115
buffer->pFunct(buffer->object, &rxMsg);
116+
if (callback != NULL) {
117+
callback();
118+
}
108119
break;
109120
}
110121
}

modules/canopennode/canopennode.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ void canopen_leds_init(CO_NMT_t *nmt,
144144
*/
145145
void canopen_leds_program_download(bool in_progress);
146146

147+
/**
148+
* @brief Callback for incoming CAN message
149+
*
150+
* This callback will be called from interrupt context and should therefore
151+
* return quickly.
152+
*
153+
* It can be used to e.g. wake the loop polling calling CO_process.
154+
*/
155+
typedef void (*canopen_rxmsg_callback_t)(void);
156+
157+
/**
158+
* @brief Set callback for incoming CAN message
159+
*
160+
* Set up callback to be called on incoming CAN message on any of
161+
* the configured filters for CANopenNode.
162+
*
163+
* This can be used to wake the loop calling CO_process when an incoming
164+
* message needs to be processed.
165+
*
166+
* Setting a new callback will overwrite any existing callback.
167+
*
168+
* @param callback the callback to set
169+
*/
170+
void canopen_set_rxmsg_callback(canopen_rxmsg_callback_t callback);
171+
147172
#ifdef __cplusplus
148173
}
149174
#endif

0 commit comments

Comments
 (0)