Skip to content

Commit 8031008

Browse files
authored
Merge pull request #633 from arduino/mbed-can-support
Provide CAN library for enabling useage of CAN for ArduinoCore-mbed enabled boards.
2 parents e594a0e + f4cad9f commit 8031008

File tree

13 files changed

+350
-4
lines changed

13 files changed

+350
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include <Arduino_CAN.h>
6+
7+
/**************************************************************************************
8+
* SETUP/LOOP
9+
**************************************************************************************/
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
while (!Serial) { }
15+
16+
if (!CAN.begin(CanBitRate::BR_250k))
17+
{
18+
Serial.println("CAN.begin(...) failed.");
19+
for (;;) {}
20+
}
21+
}
22+
23+
void loop()
24+
{
25+
if (CAN.available())
26+
{
27+
CanMsg const msg = CAN.read();
28+
Serial.println(msg);
29+
}
30+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include <Arduino_CAN.h>
6+
7+
/**************************************************************************************
8+
* CONSTANTS
9+
**************************************************************************************/
10+
11+
static uint32_t const CAN_ID = 0x20;
12+
13+
/**************************************************************************************
14+
* SETUP/LOOP
15+
**************************************************************************************/
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
while (!Serial) { }
21+
22+
if (!CAN.begin(CanBitRate::BR_250k))
23+
{
24+
Serial.println("CAN.begin(...) failed.");
25+
for (;;) {}
26+
}
27+
}
28+
29+
static uint32_t msg_cnt = 0;
30+
31+
void loop()
32+
{
33+
/* Assemble a CAN message with the format of
34+
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
35+
*/
36+
uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};
37+
memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt));
38+
CanMsg msg(CAN_ID, sizeof(msg_data), msg_data);
39+
40+
/* Transmit the CAN message, capture and display an
41+
* error core in case of failure.
42+
*/
43+
if (int const rc = CAN.write(msg); rc <= 0)
44+
{
45+
Serial.print ("CAN.write(...) failed with error code ");
46+
Serial.println(rc);
47+
for (;;) { }
48+
}
49+
50+
/* Increase the message counter. */
51+
msg_cnt++;
52+
53+
/* Only send one message per second. */
54+
delay(1000);
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
`extras/scripts`
2+
================
3+
This directory contains helpful shell scripts when working with CAN.
4+
5+
### How-to-`SocketCAN`
6+
```bash
7+
sudo ./setup_scan.sh
8+
```
9+
Display received CAN frames via [`candump`](https://manpages.ubuntu.com/manpages/jammy/man1/candump.1.html):
10+
```bash
11+
candump can0
12+
```
13+
Transmit CAN frames via [`cansend`](https://manpages.ubuntu.com/manpages/jammy/man1/cansend.1.html):
14+
```bash
15+
cansend can0 00001234#DEADBEEF
16+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
sudo ip link set can0 type can bitrate 250000
3+
sudo ip link set can0 up
4+

libraries/Arduino_CAN/keywords.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#######################################
2+
# Syntax Coloring Map for CAN
3+
#######################################
4+
5+
#######################################
6+
# Class (KEYWORD1)
7+
#######################################
8+
9+
CAN KEYWORD1
10+
CAN1 KEYWORD1
11+
CanMsg KEYWORD1
12+
CanBitRate KEYWORD1
13+
14+
#######################################
15+
# Methods and Functions (KEYWORD2)
16+
#######################################
17+
18+
begin KEYWORD2
19+
end KEYWORD2
20+
write KEYWORD2
21+
available KEYWORD2
22+
read KEYWORD2
23+
24+
#######################################
25+
# Constants (LITERAL1)
26+
#######################################
27+
28+
BR_100k LITERAL1
29+
BR_125k LITERAL1
30+
BR_250k LITERAL1
31+
BR_500k LITERAL1
32+
BR_1000k LITERAL1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Arduino_CAN
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=CAN communication library for ArduinoCore-mbed enabled boards.
6+
paragraph=This library provides CAN for ArduinoCore-mbed enabled boards which expose CAN on their connectors.
7+
category=Other
8+
url=
9+
architectures=mbed,mbed_portenta
10+
include=Arduino_CAN.h
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2022 by Alexander Entinger <a.entinger@arduino.cc>
3+
* Arduino_CAN library for Arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
/**************************************************************************************
12+
* INCLUDE
13+
**************************************************************************************/
14+
15+
#include "Arduino_CAN.h"
16+
17+
/**************************************************************************************
18+
* NAMESPACE
19+
**************************************************************************************/
20+
21+
namespace arduino
22+
{
23+
24+
/**************************************************************************************
25+
* CTOR/DTOR
26+
**************************************************************************************/
27+
28+
Arduino_CAN::Arduino_CAN(PinName const can_tx_pin, PinName const can_rx_pin)
29+
: _can(can_rx_pin, can_tx_pin)
30+
{
31+
32+
}
33+
34+
/**************************************************************************************
35+
* PUBLIC MEMBER FUNCTIONS
36+
**************************************************************************************/
37+
38+
bool Arduino_CAN::begin(CanBitRate const can_bitrate)
39+
{
40+
int const rc = _can.frequency(static_cast<int>(can_bitrate));
41+
return (rc == 1);
42+
}
43+
44+
void Arduino_CAN::end()
45+
{
46+
/* Nothing to do. */
47+
}
48+
49+
int Arduino_CAN::write(CanMsg const & msg)
50+
{
51+
mbed::CANMessage const can_msg(
52+
msg.id,
53+
msg.data,
54+
msg.data_length,
55+
CANData,
56+
CANStandard);
57+
58+
return _can.write(can_msg);
59+
}
60+
61+
size_t Arduino_CAN::available()
62+
{
63+
mbed::CANMessage can_msg;
64+
bool const msg_read = _can.read(can_msg) > 0;
65+
66+
if (msg_read)
67+
{
68+
CanMsg const msg(
69+
can_msg.id,
70+
can_msg.len,
71+
can_msg.data);
72+
73+
_rx_msg_buf.enqueue(msg);
74+
}
75+
76+
return _rx_msg_buf.available();
77+
}
78+
79+
CanMsg Arduino_CAN::read()
80+
{
81+
return _rx_msg_buf.dequeue();
82+
}
83+
84+
/**************************************************************************************
85+
* NAMESPACE
86+
**************************************************************************************/
87+
88+
} /* arduino */
89+
90+
/**************************************************************************************
91+
* OBJECT INSTANTIATION
92+
**************************************************************************************/
93+
94+
#if CAN_HOWMANY > 0
95+
arduino::Arduino_CAN CAN(PIN_CAN0_TX, PIN_CAN0_RX);
96+
#endif
97+
98+
#if CAN_HOWMANY > 1
99+
arduino::Arduino_CAN CAN1(PIN_CAN1_TX, PIN_CAN1_RX);
100+
#endif
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2022 by Alexander Entinger <a.entinger@arduino.cc>
3+
* CAN library for Arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#ifndef ARDUINO_CORE_MBED_CAN_H_
12+
#define ARDUINO_CORE_MBED_CAN_H_
13+
14+
/**************************************************************************************
15+
* INCLUDE
16+
**************************************************************************************/
17+
18+
#include <Arduino.h>
19+
#include <mbed.h>
20+
21+
#include "api/HardwareCAN.h"
22+
23+
/**************************************************************************************
24+
* COMPILE TIME CHECKS
25+
**************************************************************************************/
26+
27+
#if !(defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_GIGA))
28+
# error "CAN only available on Arduino Portenta H7 and Arduino Giga (of all ArduinoCore-mbed enabled boards)."
29+
#endif
30+
31+
/**************************************************************************************
32+
* TYPEDEF
33+
**************************************************************************************/
34+
35+
typedef arduino::CanMsg CanMsg;
36+
37+
/**************************************************************************************
38+
* NAMESPACE
39+
**************************************************************************************/
40+
41+
namespace arduino
42+
{
43+
44+
/**************************************************************************************
45+
* CLASS DECLARATION
46+
**************************************************************************************/
47+
48+
class Arduino_CAN final : public HardwareCAN
49+
{
50+
public:
51+
Arduino_CAN(PinName const can_tx_pin, PinName const can_rx_pin);
52+
virtual ~Arduino_CAN() { }
53+
54+
55+
bool begin(CanBitRate const can_bitrate) override;
56+
void end() override;
57+
58+
59+
int write(CanMsg const & msg) override;
60+
size_t available() override;
61+
CanMsg read() override;
62+
63+
private:
64+
mbed::CAN _can;
65+
CanMsgRingbuffer _rx_msg_buf;
66+
};
67+
68+
/**************************************************************************************
69+
* NAMESPACE
70+
**************************************************************************************/
71+
72+
} /* arduino */
73+
74+
/**************************************************************************************
75+
* EXTERN DECLARATION
76+
**************************************************************************************/
77+
78+
#if CAN_HOWMANY > 0
79+
extern arduino::Arduino_CAN CAN;
80+
#endif
81+
82+
#if CAN_HOWMANY > 1
83+
extern arduino::Arduino_CAN CAN1;
84+
#endif
85+
86+
#endif /* ARDUINO_CORE_MBED_CAN_H_ */

variants/GIGA/pins_arduino.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,12 @@ void _ontouch1200bps_();
263263

264264
#define USB_MAX_POWER (500)
265265

266+
#define CAN_HOWMANY 2
267+
268+
#define PIN_CAN0_TX (PB_13)
269+
#define PIN_CAN0_RX (PB_12)
270+
271+
#define PIN_CAN1_TX (PH_13)
272+
#define PIN_CAN1_RX (PB_8)
273+
266274
#endif //__PINS_ARDUINO__

variants/PORTENTA_H7_M4/pins_arduino.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
#include "../GIGA/pins_arduino.h"
99
#endif
1010

11-
#undef SERIAL_CDC
11+
#undef SERIAL_CDC

variants/PORTENTA_H7_M4/variant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

1313
#undef initVariant
1414

15-
void initVariant() {}
15+
void initVariant() {}

variants/PORTENTA_H7_M7/pins_arduino.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ void _ontouch1200bps_();
164164

165165
#define USB_MAX_POWER (500)
166166

167+
#define CAN_HOWMANY 1
168+
169+
#define PIN_CAN0_TX (PH_13) /* Labeled CAN1_TX on high-density connector. */
170+
#define PIN_CAN0_RX (PB_8) /* Labeled CAN1_RX on high-density connector. */
171+
167172
#endif //__PINS_ARDUINO__

variants/PORTENTA_H7_M7/variant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ PinDescription g_APinDescription[] = {
7878
{ PB_5, NULL, NULL, NULL },
7979
{ PB_6, NULL, NULL, NULL },
8080
{ PB_7, NULL, NULL, NULL },
81-
{ PB_8, NULL, NULL, NULL },
81+
{ PB_8, NULL, NULL, NULL }, // HD-connector: CAN1_RX -> software object: CAN
8282
{ PB_9, NULL, NULL, NULL },
8383
{ PB_10, NULL, NULL, NULL },
8484
{ PB_11, NULL, NULL, NULL },
@@ -179,7 +179,7 @@ PinDescription g_APinDescription[] = {
179179
{ PH_10, NULL, NULL, NULL },
180180
{ PH_11, NULL, NULL, NULL },
181181
{ PH_12, NULL, NULL, NULL },
182-
{ PH_13, NULL, NULL, NULL },
182+
{ PH_13, NULL, NULL, NULL }, // HD-connector: CAN1_TX -> software object: CAN
183183
{ PH_14, NULL, NULL, NULL },
184184
{ PH_15, NULL, NULL, NULL },
185185
{ PI_0, NULL, NULL, NULL },

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy