Skip to content

Commit

Permalink
Merge pull request #170 from Woodwarth/main
Browse files Browse the repository at this point in the history
CAN Filter and Mask
  • Loading branch information
aentinger authored Nov 10, 2023
2 parents 9a838ba + 8249937 commit 3a1b8a3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
69 changes: 69 additions & 0 deletions libraries/Arduino_CAN/examples/CANReadFilter/CANReadFilter.ino
Original file line number Diff line number Diff line change
@@ -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 <Arduino_CAN.h>

/**************************************************************************************
* 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);
}
}
6 changes: 6 additions & 0 deletions libraries/Arduino_CAN/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ available KEYWORD2
read KEYWORD2
isError KEYWORD2
clearError KEYWORD2
setFilterMask_Standard
setFilterMask_Extended
setFilterId_Standard
setFilterId_Extended

getStandardId KEYWORD2
getExtendedId KEYWORD2
Expand All @@ -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
31 changes: 31 additions & 0 deletions libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions libraries/Arduino_CAN/src/R7FA4M1_CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit 3a1b8a3

Please sign in to comment.