diff --git a/src/CAN.c b/src/CAN.c index 826f9db..121b722 100644 --- a/src/CAN.c +++ b/src/CAN.c @@ -276,6 +276,20 @@ int CAN_write_frame(const CAN_frame_t *p_frame) { return 0; } +int CAN_write_frame_timeout(const CAN_frame_t *p_frame, int waitTicks) { + if (sem_tx_complete == NULL) { + return -1; + } + + // Write the frame to the controller + CAN_write_frame_phy(p_frame); + + // wait for the frame tx to complete + xSemaphoreTake(sem_tx_complete, waitTicks); + + return 0; +} + int CAN_stop() { // enter reset mode MODULE_CAN->MOD.B.RM = 1; diff --git a/src/CAN.h b/src/CAN.h index aebde46..bc4531a 100644 --- a/src/CAN.h +++ b/src/CAN.h @@ -108,6 +108,15 @@ int CAN_init(void); */ int CAN_write_frame(const CAN_frame_t *p_frame); +/** + * \brief Send a can frame + * + * \param p_frame Pointer to the frame to be send, see #CAN_frame_t + * \param waitTicks Wait for write completion ticks + * \return 0 Frame has been written to the module + */ +int CAN_write_frame_timeout(const CAN_frame_t *p_frame, int waitTicks); + /** * \brief Stops the CAN Module * diff --git a/src/ESP32CAN.cpp b/src/ESP32CAN.cpp index 1412b4a..c9a39e1 100644 --- a/src/ESP32CAN.cpp +++ b/src/ESP32CAN.cpp @@ -8,6 +8,11 @@ int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame) { return CAN_write_frame(p_frame); } + +int ESP32CAN::CANWriteFrame(const CAN_frame_t *p_frame, int waitMs) { + return CAN_write_frame_timeout(p_frame, waitMs * portTICK_PERIOD_MS); +} + int ESP32CAN::CANStop() { return CAN_stop(); diff --git a/src/ESP32CAN.h b/src/ESP32CAN.h index 0cbe156..dab75fa 100644 --- a/src/ESP32CAN.h +++ b/src/ESP32CAN.h @@ -10,6 +10,7 @@ class ESP32CAN int CANInit(); int CANConfigFilter(const CAN_filter_t* p_filter); int CANWriteFrame(const CAN_frame_t* p_frame); + int CANWriteFrame(const CAN_frame_t* p_frame, int waitMs); int CANStop(); };