Skip to content

Commit 62ac782

Browse files
committed
CAN Filter and Mask
Added CAN Filter and Mask
1 parent 9a838ba commit 62ac782

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
CANRead
3+
4+
Receive and read CAN Bus messages
5+
6+
7+
Mailbox Groups 0-3 (Mailbox 0-15) -> TRANSMIT
8+
Mailbox Groups 4-5 (Mailbox 16-23) -> RECEIVE EXTENDED
9+
Mailbox Groups 6-7 (Mailbox 24-31) -> RECEIVE STANDARD
10+
11+
See the full documentation here:
12+
https://docs.arduino.cc/tutorials/uno-r4-wifi/can
13+
*/
14+
15+
/**************************************************************************************
16+
* INCLUDE
17+
**************************************************************************************/
18+
19+
#include <Arduino_CAN.h>
20+
21+
/**************************************************************************************
22+
* SETUP/LOOP
23+
**************************************************************************************/
24+
25+
void setup()
26+
{
27+
Serial.begin(115200);
28+
while (!Serial) { }
29+
30+
CAN.setMailboxMask(4, 0x1FFFFFFF);
31+
CAN.setMailboxMask(6, 0x1FFFFFFF);
32+
33+
for (int c=16; c <= 31; c++) {
34+
CAN.setMailboxID(c, 0x10);
35+
}
36+
37+
if (!CAN.begin(CanBitRate::BR_250k))
38+
{
39+
Serial.println("CAN.begin(...) failed.");
40+
for (;;) {}
41+
}
42+
}
43+
44+
void loop()
45+
{
46+
if (CAN.available())
47+
{
48+
CanMsg const msg = CAN.read();
49+
Serial.println(msg);
50+
}
51+
}

libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ void R7FA4M1_CAN::end()
187187
R_CAN_Close(&_can_ctrl);
188188
}
189189

190+
191+
void R7FA4M1_CAN::setMailboxMask(int mailbox_group, uint32_t mask)
192+
{
193+
_can_mailbox_mask[mailbox_group] = mask;
194+
}
195+
196+
void R7FA4M1_CAN::setMailboxID(int mailbox, int id)
197+
{
198+
_can_mailbox[mailbox].mailbox_id = id;
199+
}
200+
201+
190202
int R7FA4M1_CAN::enableInternalLoopback()
191203
{
192204
if(fsp_err_t const rc = R_CAN_ModeTransition(&_can_ctrl, CAN_OPERATION_MODE_NORMAL, CAN_TEST_MODE_LOOPBACK_EXTERNAL); rc != FSP_SUCCESS)

libraries/Arduino_CAN/src/R7FA4M1_CAN.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class R7FA4M1_CAN final : public HardwareCAN
5454
bool begin(CanBitRate const can_bitrate) override;
5555
void end() override;
5656

57+
void setMailboxMask(int mailbox_group,uint32_t mask);
58+
void setMailboxID(int mailbox,int id);
5759

5860
int enableInternalLoopback();
5961
int disableInternalLoopback();

0 commit comments

Comments
 (0)