Skip to content
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

CAN bus implementation #5120

Draft
wants to merge 23 commits into
base: mega
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a14254a
CAN Hardware Implementation
bruno-clima Sep 11, 2024
66680c6
CAN controller implementation
bruno-clima Sep 11, 2024
2d9e6ce
Plugin implementation
bruno-clima Sep 11, 2024
612180e
Fix Markup
bruno-clima Sep 11, 2024
0b51184
Merge branch 'mega' into can_implementation
TD-er Sep 22, 2024
9548e18
[CAN] Make it compile
TD-er Sep 22, 2024
29f10cf
[CAN] Rename plugin and controller to reserved IDs
TD-er Sep 22, 2024
0bc2b45
[CAN] Fix command decoder for CAN commands
TD-er Sep 22, 2024
280de6c
[CAN] Make lib compile for ESP-IDF5.1
TD-er Sep 22, 2024
f4f2758
[CAN] Limit to ESP32-classic only
TD-er Sep 22, 2024
86d8d1a
[CAN] custom_IR_ESP32c3_4M316k_CDC did not ignore CAN lib
TD-er Sep 22, 2024
9d37835
[CAN] Some more envs with their own lib_ignore not ignoring CAN lib
TD-er Sep 22, 2024
41d5b37
[CAN] FIX ESP8266 builds
TD-er Sep 22, 2024
a554806
[CAN] Fix numerous other ESP8266 builds lib_ignore CAN library
TD-er Sep 22, 2024
da18373
[CAN] Fix settings struct size check
TD-er Sep 22, 2024
9c713ba
[CAN] And another few envs needing CAN lib_ignore
TD-er Sep 22, 2024
fec5a72
Merge branch 'mega' into can_implementation
TD-er Sep 26, 2024
46a2b50
Merge branch 'mega' into can_implementation
TD-er Sep 28, 2024
12603d7
Merge branch 'mega' into can_implementation
TD-er Sep 28, 2024
2431e69
Merge branch 'mega' into can_implementation
TD-er Oct 2, 2024
741fe66
Merge branch 'mega' into can_implementation
TD-er Oct 2, 2024
284e889
Merge remote-tracking branch 'letscontrolit/mega' into can_implementa…
TD-er Oct 7, 2024
6760c24
Merge branch 'mega' into can_implementation
TD-er Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ docs/source/Plugin/_plugin_sets_overview.repl

.platformio/
.pio/

*.Identifier
31 changes: 31 additions & 0 deletions lib/arduino-CAN/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: generic
env:
global:
- IDE_VERSION=1.8.5
matrix:
- BOARD="arduino:avr:uno"
- BOARD="arduino:samd:mkrzero"
- BOARD="espressif:esp32:esp32"
before_install:
- wget http://downloads.arduino.cc/arduino-$IDE_VERSION-linux64.tar.xz
- tar xf arduino-$IDE_VERSION-linux64.tar.xz
- mv arduino-$IDE_VERSION $HOME/arduino-ide
- export PATH=$PATH:$HOME/arduino-ide
- if [[ "$BOARD" =~ "arduino:samd:" ]]; then
arduino --install-boards arduino:samd;
fi
- if [[ "$BOARD" =~ "espressif:esp32:" ]]; then
mkdir -p $HOME/Arduino/hardware/espressif;
git clone https://github.com/espressif/arduino-esp32.git $HOME/Arduino/hardware/espressif/esp32;
pushd $HOME/Arduino/hardware/espressif/esp32/tools/;
python get.py;
popd;
fi
- buildExampleSketch() { arduino --verbose-build --verify --board $BOARD $PWD/examples/$1/$1.ino; }
install:
- mkdir -p $HOME/Arduino/libraries
- ln -s $PWD $HOME/Arduino/libraries/CAN
script:
- buildExampleSketch CANReceiver
- buildExampleSketch CANReceiverCallback
- buildExampleSketch CANSender
272 changes: 272 additions & 0 deletions lib/arduino-CAN/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
# CAN API

## Include Library

```arduino
#include <CAN.h>
```

## Setup

### Begin

Initialize the library with the specified bit rate.

```arduino
CAN.begin(bitrate);
```
* `bitrate` - bit rate in bits per seconds (bps) (`1000E3`, `500E3`, `250E3`, `200E3`, `125E3`, `100E3`, `80E3`, `50E3`, `40E3`, `20E3`, `10E3`, `5E3`)

Returns `1` on success, `0` on failure.

### Set pins

#### MCP2515

Override the default `CS` and `INT` pins used by the library. **Must** be called before `CAN.begin(...)`.

```arduino
CAN.setPins(cs, irq);
```
* `cs` - new chip select pin to use, defaults to `10`
* `irq` - new INT pin to use, defaults to `2`. **Must** be interrupt capable via [attachInterrupt(...)](https://www.arduino.cc/en/Reference/AttachInterrupt).

This call is optional and only needs to be used if you need to change the default pins used.

#### ESP32

Override the default `CTX` and `CRX` pins used by the library. **Must** be called before `CAN.begin(...)`.

```arduino
CAN.setPins(rx, tx);
```
* `rx` - new CRX pin to use, defaults to `4`
* `tx` - new CTX pin to use, defaults to `5`.

This call is optional and only needs to be used if you need to change the default pins used.

### Set SPI Frequency

**MCP2515 only**

Override the default SPI frequency of 10 MHz used by the library. **Must** be called before `CAN.begin(...)`.

```arduino
CAN.setSPIFrequency(frequency);
```
* `frequency` - new SPI frequency to use, defaults to `10E6`

This call is optional and only needs to be used if you need to change the default SPI frequency used. Some logic level converters cannot support high speeds such as 10 MHz, so a lower SPI frequency can be selected with `CAN.setSPIFrequency(frequency)`.

### Set Clock Frequency

**MCP2515 only**

Override the default clock source frequency that is connected to the MCP2515. **Must** be called before `CAN.begin(...)`.

```arduino
CAN.setClockFrequency(clockFrequency);
```
* `clockFrequency` - new clock frequency to use (`8E6`, `16E6`) connected to MCP2515, defaults to `16 Mhz`

This call is optional and only needs to be used if you need to change the clock source frequency connected to the MCP2515. Most shields have a 16 MHz clock source on board, some breakout boards have a 8 MHz source.

### End

Stop the library

```arduino
CAN.end()
```

## Sending data

### Begin packet

Start the sequence of sending a packet.

```arduino
CAN.beginPacket(id);
CAN.beginPacket(id, dlc);
CAN.beginPacket(id, dlc, rtr);

CAN.beginExtendedPacket(id);
CAN.beginExtendedPacket(id, dlc);
CAN.beginExtendedPacket(id, dlc, rtr);
```

* `id` - 11-bit id (standard packet) or 29-bit packet id (extended packet)
* `dlc` - (optional) value of Data Length Code (DLC) field of packet, default is size of data written in packet
* `rtr` - (optional) value of Remote Transmission Request (RTR) field of packet (`false` or `true`), defaults to `false`. RTR packets contain no data, the DLC field of the packet represents the requested length.

Returns `1` on success, `0` on failure.

### Writing

Write data to the packet. Each packet can contain up to 8 bytes.

```arduino
CAN.write(byte);

CAN.write(buffer, length);
```
* `byte` - single byte to write to packet

or

* `buffer` - data to write to packet
* `length` - size of data to write

Returns the number of bytes written.

**Note:** Other Arduino `Print` API's can also be used to write data into the packet

### End packet

End the sequence of sending a packet.

```arduino
CAN.endPacket()
```

Returns `1` on success, `0` on failure.

## Receiving data

### Parsing packet

Check if a packet has been received.

```arduino
int packetSize = CAN.parsePacket();
```

Returns the packet size in bytes or `0` if no packet was received. For RTR packets the size reflects the DLC field of the packet.

### Register callback

Register a callback function for when a packet is received.

```arduino
CAN.onReceive(onReceive);

void onReceive(int packetSize) {
// ...
}
```

* `onReceive` - function to call when a packet is received.

### Packet ID

```arduino
long id = CAN.packetId();
```

Returns the id (11-bit or 29 bit) of the received packet. Standard packets have an 11-bit id, extended packets have an 29-bit id.

### Packet Extended

```arduino
bool extended = CAN.packetExtended();
```

Returns `true` if the received packet is extended, `false` otherwise.

### Packet RTR

```arduino
bool rtr = CAN.packetRtr();
```

Returns the value of the Remote Transmission Request (RTR) field of the packet `true`/`false`. RTR packets contain no data, the DLC field is the requested data length.

### Packet DLC

```arduino
int DLC = CAN.packetDlc();
```

Returns the value of the Data Length Code (DLC) field of the packet.


### Available

```arduino
int availableBytes = CAN.available()
```

Returns number of bytes available for reading.

### Peeking

Peek at the next byte in the packet.

```arduino
int b = CAN.peek();
```

Returns the next byte in the packet or `-1` if no bytes are available.

### Reading

Read the next byte from the packet.

```arduino
int b = CAN.read();
```

Returns the next byte in the packet or `-1` if no bytes are available.

**Note:** Other Arduino [`Stream` API's](https://www.arduino.cc/en/Reference/Stream) can also be used to read data from the packet

### Filtering

Filter packets that meet the desired criteria.

```
CAN.filter(id);
CAN.filter(id, mask);

CAN.filterExtended(id);
CAN.filterExtended(id, mask);
```

* `id` - 11-bit id (standard packet) or 29-bit packet id (extended packet)
* `mask` - (optional) 11-bit mask (standard packet) or 29-bit mask (extended packet), defaults to `0x7ff` or `0x1fffffff` (extended)

Only packets that meet the following criteria are acknowleged and received, other packets are ignored:

```
if ((packetId & mask) == id) {
// acknowleged and received
} else {
// ignored
}
```

Returns `1` on success, `0` on failure.

## Other modes

### Loopback mode

Put the CAN controller in loopback mode, any outgoing packets will also be received.

```arduino
CAN.loopback();
```

### Sleep mode

Put the CAN contoller in sleep mode.

```arduino
CAN.sleep();
```

Wake up the CAN contoller if it was previously in sleep mode.

```arduino
CAN.wakeup();
```
21 changes: 21 additions & 0 deletions lib/arduino-CAN/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Sandeep Mistry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading