Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b4868d

Browse files
Provide CAN library for enabling useage of CAN for ArduinoCore-mbed enabled boards.
Caveat: which also make CAN available on their GPIOs.
1 parent 0504d34 commit 9b4868d

File tree

10 files changed

+318
-4
lines changed

10 files changed

+318
-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+
}

‎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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
CanMsg const msg(
67+
can_msg.id,
68+
can_msg.len,
69+
can_msg.data);
70+
71+
_rx_msg_buf.enqueue(msg);
72+
return _rx_msg_buf.available();
73+
}
74+
75+
CanMsg Arduino_CAN::read()
76+
{
77+
return _rx_msg_buf.dequeue();
78+
}
79+
80+
/**************************************************************************************
81+
* NAMESPACE
82+
**************************************************************************************/
83+
84+
} /* arduino */
85+
86+
/**************************************************************************************
87+
* OBJECT INSTANTIATION
88+
**************************************************************************************/
89+
90+
#if CAN_HOWMANY > 0
91+
arduino::Arduino_CAN CAN(PIN_CAN0_TX, PIN_CAN0_RX);
92+
#endif
93+
94+
#if CAN_HOWMANY > 1
95+
arduino::Arduino_CAN CAN1(PIN_CAN1_TX, PIN_CAN1_RX);
96+
#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))
28+
# error "CAN only available on Arduino Portenta H7 (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/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)
170+
#define PIN_CAN0_RX (PB_8)
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 },// CAN1 RX
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 },// CAN1 TX
183183
{ PH_14, NULL, NULL, NULL },
184184
{ PH_15, NULL, NULL, NULL },
185185
{ PI_0, NULL, NULL, NULL },

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /