Movatterモバイル変換


[0]ホーム

URL:


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

Commit8031008

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 parentse594a0e +f4cad9f commit8031008

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+
voidsetup()
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+
voidloop()
24+
{
25+
if (CAN.available())
26+
{
27+
CanMsgconst 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+
staticuint32_tconst CAN_ID =0x20;
12+
13+
/**************************************************************************************
14+
* SETUP/LOOP
15+
**************************************************************************************/
16+
17+
voidsetup()
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+
staticuint32_t msg_cnt =0;
30+
31+
voidloop()
32+
{
33+
/* Assemble a CAN message with the format of
34+
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
35+
*/
36+
uint8_tconst msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};
37+
memcpy((void *)(msg_data +4), &msg_cnt,sizeof(msg_cnt));
38+
CanMsgmsg(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 (intconst 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 linkset can0type can bitrate 250000
3+
sudo ip linkset 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+
CANKEYWORD1
10+
CAN1KEYWORD1
11+
CanMsgKEYWORD1
12+
CanBitRateKEYWORD1
13+
14+
#######################################
15+
# Methods and Functions (KEYWORD2)
16+
#######################################
17+
18+
beginKEYWORD2
19+
endKEYWORD2
20+
writeKEYWORD2
21+
availableKEYWORD2
22+
readKEYWORD2
23+
24+
#######################################
25+
# Constants (LITERAL1)
26+
#######################################
27+
28+
BR_100kLITERAL1
29+
BR_125kLITERAL1
30+
BR_250kLITERAL1
31+
BR_500kLITERAL1
32+
BR_1000kLITERAL1
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+
namespacearduino
22+
{
23+
24+
/**************************************************************************************
25+
* CTOR/DTOR
26+
**************************************************************************************/
27+
28+
Arduino_CAN::Arduino_CAN(PinNameconst can_tx_pin, PinNameconst can_rx_pin)
29+
: _can(can_rx_pin, can_tx_pin)
30+
{
31+
32+
}
33+
34+
/**************************************************************************************
35+
* PUBLIC MEMBER FUNCTIONS
36+
**************************************************************************************/
37+
38+
boolArduino_CAN::begin(CanBitRateconst can_bitrate)
39+
{
40+
intconst rc = _can.frequency(static_cast<int>(can_bitrate));
41+
return (rc ==1);
42+
}
43+
44+
voidArduino_CAN::end()
45+
{
46+
/* Nothing to do.*/
47+
}
48+
49+
intArduino_CAN::write(CanMsgconst & msg)
50+
{
51+
mbed::CANMessageconstcan_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_tArduino_CAN::available()
62+
{
63+
mbed::CANMessage can_msg;
64+
boolconst msg_read = _can.read(can_msg) >0;
65+
66+
if (msg_read)
67+
{
68+
CanMsgconstmsg(
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+
CanMsgArduino_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_CANCAN(PIN_CAN0_TX, PIN_CAN0_RX);
96+
#endif
97+
98+
#if CAN_HOWMANY > 1
99+
arduino::Arduino_CANCAN1(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+
#defineARDUINO_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+
namespacearduino
42+
{
43+
44+
/**************************************************************************************
45+
* CLASS DECLARATION
46+
**************************************************************************************/
47+
48+
classArduino_CANfinal : public HardwareCAN
49+
{
50+
public:
51+
Arduino_CAN(PinNameconst can_tx_pin, PinNameconst can_rx_pin);
52+
virtual~Arduino_CAN() { }
53+
54+
55+
boolbegin(CanBitRateconst can_bitrate)override;
56+
voidend()override;
57+
58+
59+
intwrite(CanMsgconst & msg)override;
60+
size_tavailable()override;
61+
CanMsgread()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
#defineUSB_MAX_POWER(500)
265265

266+
#defineCAN_HOWMANY 2
267+
268+
#definePIN_CAN0_TX (PB_13)
269+
#definePIN_CAN0_RX (PB_12)
270+
271+
#definePIN_CAN1_TX (PH_13)
272+
#definePIN_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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp