-
-
Notifications
You must be signed in to change notification settings - Fork 213
Provide CAN library for enabling useage of CAN for ArduinoCore-mbed enabled boards. #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9b4868d
Provide CAN library for enabling useage of CAN for ArduinoCore-mbed e…
aentinger 74d3064
Adding minimal script for setting up SocketCAN, i.e. for a PEAKCAN USB.
aentinger 10319d0
Fix: write returns either 0 or a negative error code on failure.
aentinger 2bb0f46
Add documentation on how to display received frames, view transmitted…
aentinger 6092e06
Fix: only store message in receive buffer if a message had been recei…
aentinger 4533778
Fix: CAN pins for H7 (both are available).
aentinger 55ed9a8
Add pins useable for CAN on GIGA.
aentinger 317fcb4
Amend compile time check to include GIGA.
aentinger f4cad9f
Fix: only one CAN bus connector is exposed on the H7.
aentinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <Arduino_CAN.h> | ||
|
||
/************************************************************************************** | ||
* SETUP/LOOP | ||
**************************************************************************************/ | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial) { } | ||
|
||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <Arduino_CAN.h> | ||
|
||
/************************************************************************************** | ||
* CONSTANTS | ||
**************************************************************************************/ | ||
|
||
static uint32_t const CAN_ID = 0x20; | ||
|
||
/************************************************************************************** | ||
* SETUP/LOOP | ||
**************************************************************************************/ | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial) { } | ||
|
||
if (!CAN.begin(CanBitRate::BR_250k)) | ||
{ | ||
Serial.println("CAN.begin(...) failed."); | ||
for (;;) {} | ||
} | ||
} | ||
|
||
static uint32_t msg_cnt = 0; | ||
|
||
void loop() | ||
{ | ||
/* Assemble a CAN message with the format of | ||
* 0xCA 0xFE 0x00 0x00 [4 byte message counter] | ||
*/ | ||
uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0}; | ||
memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt)); | ||
CanMsg msg(CAN_ID, sizeof(msg_data), msg_data); | ||
|
||
/* Transmit the CAN message, capture and display an | ||
* error core in case of failure. | ||
*/ | ||
if (int const rc = CAN.write(msg); rc <= 0) | ||
{ | ||
Serial.print ("CAN.write(...) failed with error code "); | ||
Serial.println(rc); | ||
for (;;) { } | ||
} | ||
|
||
/* Increase the message counter. */ | ||
msg_cnt++; | ||
|
||
/* Only send one message per second. */ | ||
delay(1000); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
`extras/scripts` | ||
================ | ||
This directory contains helpful shell scripts when working with CAN. | ||
|
||
### How-to-`SocketCAN` | ||
```bash | ||
sudo ./setup_scan.sh | ||
``` | ||
Display received CAN frames via [`candump`](https://manpages.ubuntu.com/manpages/jammy/man1/candump.1.html): | ||
```bash | ||
candump can0 | ||
``` | ||
Transmit CAN frames via [`cansend`](https://manpages.ubuntu.com/manpages/jammy/man1/cansend.1.html): | ||
```bash | ||
cansend can0 00001234#DEADBEEF | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
sudo ip link set can0 type can bitrate 250000 | ||
sudo ip link set can0 up | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
####################################### | ||
# Syntax Coloring Map for CAN | ||
####################################### | ||
|
||
####################################### | ||
# Class (KEYWORD1) | ||
####################################### | ||
|
||
CAN KEYWORD1 | ||
CAN1 KEYWORD1 | ||
CanMsg KEYWORD1 | ||
CanBitRate KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
begin KEYWORD2 | ||
end KEYWORD2 | ||
write KEYWORD2 | ||
available KEYWORD2 | ||
read KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
||
BR_100k LITERAL1 | ||
BR_125k LITERAL1 | ||
BR_250k LITERAL1 | ||
BR_500k LITERAL1 | ||
BR_1000k LITERAL1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=Arduino_CAN | ||
version=1.0.0 | ||
author=Arduino | ||
maintainer=Arduino <info@arduino.cc> | ||
sentence=CAN communication library for ArduinoCore-mbed enabled boards. | ||
paragraph=This library provides CAN for ArduinoCore-mbed enabled boards which expose CAN on their connectors. | ||
category=Other | ||
url= | ||
architectures=mbed,mbed_portenta | ||
include=Arduino_CAN.h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2022 by Alexander Entinger <a.entinger@arduino.cc> | ||
* Arduino_CAN library for Arduino. | ||
* | ||
* This file is free software; you can redistribute it and/or modify | ||
* it under the terms of either the GNU General Public License version 2 | ||
* or the GNU Lesser General Public License version 2.1, both as | ||
* published by the Free Software Foundation. | ||
*/ | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include "Arduino_CAN.h" | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
namespace arduino | ||
{ | ||
|
||
/************************************************************************************** | ||
* CTOR/DTOR | ||
**************************************************************************************/ | ||
|
||
Arduino_CAN::Arduino_CAN(PinName const can_tx_pin, PinName const can_rx_pin) | ||
: _can(can_rx_pin, can_tx_pin) | ||
{ | ||
|
||
} | ||
|
||
/************************************************************************************** | ||
* PUBLIC MEMBER FUNCTIONS | ||
**************************************************************************************/ | ||
|
||
bool Arduino_CAN::begin(CanBitRate const can_bitrate) | ||
{ | ||
int const rc = _can.frequency(static_cast<int>(can_bitrate)); | ||
return (rc == 1); | ||
} | ||
|
||
void Arduino_CAN::end() | ||
{ | ||
/* Nothing to do. */ | ||
} | ||
|
||
int Arduino_CAN::write(CanMsg const & msg) | ||
{ | ||
mbed::CANMessage const can_msg( | ||
msg.id, | ||
msg.data, | ||
msg.data_length, | ||
CANData, | ||
CANStandard); | ||
|
||
return _can.write(can_msg); | ||
} | ||
|
||
size_t Arduino_CAN::available() | ||
{ | ||
mbed::CANMessage can_msg; | ||
bool const msg_read = _can.read(can_msg) > 0; | ||
|
||
if (msg_read) | ||
{ | ||
CanMsg const msg( | ||
can_msg.id, | ||
can_msg.len, | ||
can_msg.data); | ||
|
||
_rx_msg_buf.enqueue(msg); | ||
} | ||
|
||
return _rx_msg_buf.available(); | ||
} | ||
|
||
CanMsg Arduino_CAN::read() | ||
{ | ||
return _rx_msg_buf.dequeue(); | ||
} | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
} /* arduino */ | ||
|
||
/************************************************************************************** | ||
* OBJECT INSTANTIATION | ||
**************************************************************************************/ | ||
|
||
#if CAN_HOWMANY > 0 | ||
arduino::Arduino_CAN CAN(PIN_CAN0_TX, PIN_CAN0_RX); | ||
#endif | ||
|
||
#if CAN_HOWMANY > 1 | ||
arduino::Arduino_CAN CAN1(PIN_CAN1_TX, PIN_CAN1_RX); | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2022 by Alexander Entinger <a.entinger@arduino.cc> | ||
* CAN library for Arduino. | ||
* | ||
* This file is free software; you can redistribute it and/or modify | ||
* it under the terms of either the GNU General Public License version 2 | ||
* or the GNU Lesser General Public License version 2.1, both as | ||
* published by the Free Software Foundation. | ||
*/ | ||
|
||
#ifndef ARDUINO_CORE_MBED_CAN_H_ | ||
#define ARDUINO_CORE_MBED_CAN_H_ | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <Arduino.h> | ||
#include <mbed.h> | ||
|
||
#include "api/HardwareCAN.h" | ||
|
||
/************************************************************************************** | ||
* COMPILE TIME CHECKS | ||
**************************************************************************************/ | ||
|
||
#if !(defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_GIGA)) | ||
# error "CAN only available on Arduino Portenta H7 and Arduino Giga (of all ArduinoCore-mbed enabled boards)." | ||
#endif | ||
|
||
/************************************************************************************** | ||
* TYPEDEF | ||
**************************************************************************************/ | ||
|
||
typedef arduino::CanMsg CanMsg; | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
namespace arduino | ||
{ | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
class Arduino_CAN final : public HardwareCAN | ||
{ | ||
public: | ||
Arduino_CAN(PinName const can_tx_pin, PinName const can_rx_pin); | ||
virtual ~Arduino_CAN() { } | ||
|
||
|
||
bool begin(CanBitRate const can_bitrate) override; | ||
void end() override; | ||
|
||
|
||
int write(CanMsg const & msg) override; | ||
size_t available() override; | ||
CanMsg read() override; | ||
|
||
private: | ||
mbed::CAN _can; | ||
CanMsgRingbuffer _rx_msg_buf; | ||
}; | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
} /* arduino */ | ||
|
||
/************************************************************************************** | ||
* EXTERN DECLARATION | ||
**************************************************************************************/ | ||
|
||
#if CAN_HOWMANY > 0 | ||
extern arduino::Arduino_CAN CAN; | ||
#endif | ||
|
||
#if CAN_HOWMANY > 1 | ||
extern arduino::Arduino_CAN CAN1; | ||
#endif | ||
|
||
#endif /* ARDUINO_CORE_MBED_CAN_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
#include "../GIGA/pins_arduino.h" | ||
#endif | ||
|
||
#undef SERIAL_CDC | ||
#undef SERIAL_CDC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ | |
|
||
#undef initVariant | ||
|
||
void initVariant() {} | ||
void initVariant() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.