Skip to content

Commit

Permalink
STM32 FDCAN: Get all messages from FIFO in RX ISR
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Pluzhnikov authored and rleh committed May 9, 2023
1 parent df7fcce commit 78a9435
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/modm/platform/can/stm32-fdcan/can.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,33 @@ MODM_ISR({{ reg }}_IT0)
MODM_ISR({{ reg }}_IT1)
{
%% if options["buffer.rx"] > 0
int_fast16_t msgRetrieveLimit = rxQueue.getMaxSize() - rxQueue.getSize();

if (msgRetrieveLimit == 0) {
modm_assert_continue_ignore(false, "fdcan.rx.buffer",
"CAN receive software buffer full, not reading new message(s)!");
NVIC_DisableIRQ({{ reg }}_IT1_IRQn);
return;
}

RxMessage rxMessage;

if (rxFifo0HasMessage()) {
while (rxFifo0HasMessage() && (msgRetrieveLimit > 0)) {
readMsg(rxMessage.message, 0, &rxMessage.filter_id, &rxMessage.timestamp);
// acknowledge interrupt flag
{{ reg }}->IR = FDCAN_IR_RF0N;

modm_assert_continue_ignore(rxQueue.push(rxMessage), "fdcan.rx.buffer",
"CAN receive software buffer overflowed!", {{ id }});
rxQueue.push(rxMessage);
msgRetrieveLimit--;
}

if (rxFifo1HasMessage()) {
while (rxFifo1HasMessage() && (msgRetrieveLimit > 0)) {
readMsg(rxMessage.message, 1, &rxMessage.filter_id, &rxMessage.timestamp);
// acknowledge interrupt flag
{{ reg }}->IR = FDCAN_IR_RF1N;

modm_assert_continue_ignore(rxQueue.push(rxMessage), "fdcan.rx.buffer",
"CAN receive software buffer overflowed!", {{ id }});
rxQueue.push(rxMessage);
msgRetrieveLimit--;
}

// Note: When leaving the ISR there still could be more messages available in the FIFO
// (if the rxQueue is full). They are still available on the next ISR entrance.

{{ reg }}->IR = FDCAN_IR_RF0N | FDCAN_IR_RF1N; // acknowledge interrupt flags
%% endif
}

Expand Down Expand Up @@ -369,6 +377,7 @@ modm::platform::Fdcan{{ id }}::getMessage(can::Message& message, uint8_t *filter
(*timestamp) = rxMessage.timestamp;
}
rxQueue.pop();
NVIC_EnableIRQ({{ reg }}_IT1_IRQn);
return true;
}
%% else
Expand Down
2 changes: 2 additions & 0 deletions src/modm/platform/can/stm32-fdcan/can.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef MODM_STM32_FDCAN{{ id }}_HPP
#define MODM_STM32_FDCAN{{ id }}_HPP

#include <optional>

#include <modm/architecture/interface/can.hpp>
#include <modm/architecture/interface/can_filter.hpp>
#include <modm/platform/gpio/connector.hpp>
Expand Down

0 comments on commit 78a9435

Please sign in to comment.