diff --git a/libraries/Arduino_CAN/examples/CANReadFilter/CANReadFilter.ino b/libraries/Arduino_CAN/examples/CANReadFilter/CANReadFilter.ino new file mode 100644 index 00000000..23f9c868 --- /dev/null +++ b/libraries/Arduino_CAN/examples/CANReadFilter/CANReadFilter.ino @@ -0,0 +1,69 @@ +/* + CANRead + + Receive and read CAN Bus messages + Read ID 0x100 EXTENDED and ID 0x10 STANDARD + + + Mailbox Groups 0-3 (Mailbox 0-15) -> TRANSMIT + Mailbox Groups 4-5 (Mailbox 16-23) -> RECEIVE EXTENDED + Mailbox Groups 6-7 (Mailbox 24-31) -> RECEIVE STANDARD + + Mailbox Mask 5,7 are preset with 0x1FFFFFFF + Mailbox Mask 4,6 are preset with 0x00 + + + See the full documentation here: + https://docs.arduino.cc/tutorials/uno-r4-wifi/can +*/ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +/************************************************************************************** + * CONST + **************************************************************************************/ + +static uint32_t const CAN_FILTER_MASK_STANDARD = 0x1FFC0000; +static uint32_t const CAN_FILTER_MASK_EXTENDED = 0x1FFFFFFF; + +/************************************************************************************** + * SETUP/LOOP + **************************************************************************************/ + +void setup() +{ + Serial.begin(115200); + while (!Serial) { } + + CAN.setFilterMask_Extended(CAN_FILTER_MASK_EXTENDED); + CAN.setFilterMask_Standard(CAN_FILTER_MASK_STANDARD); + + for (int mailbox = 0; mailbox < R7FA4M1_CAN::CAN_MAX_NO_EXTENDED_MAILBOXES; mailbox++) + { + CAN.setFilterId_Extended(mailbox, 0x0100); + } + + for (int mailbox = 0; mailbox < R7FA4M1_CAN::CAN_MAX_NO_STANDARD_MAILBOXES; mailbox++) + { + CAN.setFilterId_Standard(mailbox, 0x10); + } + + if (!CAN.begin(CanBitRate::BR_250k)) + { + Serial.println("CAN.begin(...) failed."); + for (;;) {} + } +} + +void loop() +{ + if (CAN.available()) + { + CanMsg const msg = CAN.read(); + Serial.println(msg); + } +} diff --git a/libraries/Arduino_CAN/keywords.txt b/libraries/Arduino_CAN/keywords.txt index 18ddb001..fd410d9c 100644 --- a/libraries/Arduino_CAN/keywords.txt +++ b/libraries/Arduino_CAN/keywords.txt @@ -26,6 +26,10 @@ available KEYWORD2 read KEYWORD2 isError KEYWORD2 clearError KEYWORD2 +setFilterMask_Standard +setFilterMask_Extended +setFilterId_Standard +setFilterId_Extended getStandardId KEYWORD2 getExtendedId KEYWORD2 @@ -41,3 +45,5 @@ BR_125k LITERAL1 BR_250k LITERAL1 BR_500k LITERAL1 BR_1000k LITERAL1 +CAN_MAX_NO_STANDARD_MAILBOXES LITERAL1 +CAN_MAX_NO_EXTENDED_MAILBOXES LITERAL1 diff --git a/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp b/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp index 37e2f6fd..95938e80 100644 --- a/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp +++ b/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp @@ -187,6 +187,37 @@ void R7FA4M1_CAN::end() R_CAN_Close(&_can_ctrl); } + +void R7FA4M1_CAN::setFilterMask_Standard(uint32_t const mask) +{ + _can_mailbox_mask[6] = mask; + _can_mailbox_mask[7] = mask; +} + +void R7FA4M1_CAN::setFilterMask_Extended(uint32_t const mask) +{ + _can_mailbox_mask[4] = mask; + _can_mailbox_mask[5] = mask; +} + +void R7FA4M1_CAN::setFilterId_Standard(size_t const mailbox, uint32_t const id) +{ + if (mailbox > CAN_MAX_NO_STANDARD_MAILBOXES) + return; + + size_t const mailbox_idx = CAN_MAX_STANDARD_MAILBOX_OFFSET + mailbox; + _can_mailbox[mailbox_idx].mailbox_id = id; +} + +void R7FA4M1_CAN::setFilterId_Extended(size_t const mailbox, uint32_t const id) +{ + if (mailbox > CAN_MAX_NO_EXTENDED_MAILBOXES) + return; + + size_t const mailbox_idx = CAN_MAX_EXTENDED_MAILBOX_OFFSET + mailbox; + _can_mailbox[mailbox_idx].mailbox_id = id; +} + int R7FA4M1_CAN::enableInternalLoopback() { if(fsp_err_t const rc = R_CAN_ModeTransition(&_can_ctrl, CAN_OPERATION_MODE_NORMAL, CAN_TEST_MODE_LOOPBACK_EXTERNAL); rc != FSP_SUCCESS) diff --git a/libraries/Arduino_CAN/src/R7FA4M1_CAN.h b/libraries/Arduino_CAN/src/R7FA4M1_CAN.h index d1ac3517..f60252f6 100644 --- a/libraries/Arduino_CAN/src/R7FA4M1_CAN.h +++ b/libraries/Arduino_CAN/src/R7FA4M1_CAN.h @@ -54,6 +54,10 @@ class R7FA4M1_CAN final : public HardwareCAN bool begin(CanBitRate const can_bitrate) override; void end() override; + void setFilterMask_Standard(uint32_t const mask); + void setFilterMask_Extended(uint32_t const mask); + void setFilterId_Standard(size_t const mailbox, uint32_t const id); + void setFilterId_Extended(size_t const mailbox, uint32_t const id); int enableInternalLoopback(); int disableInternalLoopback(); @@ -70,9 +74,13 @@ class R7FA4M1_CAN final : public HardwareCAN /* This function is used by the library and should NOT be called by the user. */ void onCanCallback(can_callback_args_t * p_args); + static size_t constexpr CAN_MAX_NO_STANDARD_MAILBOXES = 8U; + static size_t constexpr CAN_MAX_NO_EXTENDED_MAILBOXES = 8U; private: static size_t constexpr CAN_MAX_NO_MAILBOXES = 32U; + static size_t constexpr CAN_MAX_STANDARD_MAILBOX_OFFSET = 24U; + static size_t constexpr CAN_MAX_EXTENDED_MAILBOX_OFFSET = 16U; int const _can_tx_pin; int const _can_rx_pin;