Skip to content

Commit 428db3c

Browse files
akif999deadprogram
authored andcommitted
mcp2515: add support for mcp2515 CAN device
1 parent d7f619c commit 428db3c

File tree

5 files changed

+1267
-2
lines changed

5 files changed

+1267
-2
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ smoke-test:
8989
@md5sum ./build/test.hex
9090
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
9191
@md5sum ./build/test.hex
92+
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go
93+
@md5sum ./build/test.hex
9294
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go
9395
@md5sum ./build/test.hex
9496
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go
@@ -194,7 +196,7 @@ DRIVERS = $(wildcard */)
194196
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
195197
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
196198
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x dht keypad4x4 max72xx p1am tone tm1637 \
197-
pcf8563
199+
pcf8563 mcp2515
198200
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
199201

200202
unit-test:

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252

5353
## Currently supported devices
5454

55-
The following 63 devices are supported.
55+
The following 64 devices are supported.
5656

5757
| Device Name | Interface Type |
5858
|----------|-------------|
@@ -92,6 +92,7 @@ The following 63 devices are supported.
9292
| [MAX7219 & MAX7221 display driver](https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf) | SPI |
9393
| [MCP23017 port expander](https://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf) | I2C |
9494
| [MCP3008 analog to digital converter (ADC)](http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf) | SPI |
95+
| [MCP2515 Stand-Alone CAN Controller with SPI Interface](https://ww1.microchip.com/downloads/en/DeviceDoc/MCP2515-Family-Data-Sheet-DS20001801K.pdf) | SPI |
9596
| [Microphone - PDM](https://cdn-learn.adafruit.com/assets/assets/000/049/977/original/MP34DT01-M.pdf) | I2S/PDM |
9697
| [MMA8653 accelerometer](https://www.nxp.com/docs/en/data-sheet/MMA8653FC.pdf) | I2C |
9798
| [MPU6050 accelerometer/gyroscope](https://store.invensense.com/datasheets/invensense/MPU-6050_DataSheet_V3%204.pdf) | I2C |

examples/mcp2515/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"machine"
6+
"time"
7+
8+
"tinygo.org/x/drivers/mcp2515"
9+
)
10+
11+
var (
12+
spi = machine.SPI0
13+
csPin = machine.D5
14+
)
15+
16+
func main() {
17+
spi.Configure(machine.SPIConfig{
18+
Frequency: 115200,
19+
SCK: machine.SPI0_SCK_PIN,
20+
SDO: machine.SPI0_SDO_PIN,
21+
SDI: machine.SPI0_SDI_PIN,
22+
Mode: 0})
23+
can := mcp2515.New(spi, csPin)
24+
can.Configure()
25+
err := can.Begin(mcp2515.CAN500kBps, mcp2515.Clock8MHz)
26+
if err != nil {
27+
failMessage(err.Error())
28+
}
29+
30+
for {
31+
err := can.Tx(0x111, 8, []byte{0x00, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA})
32+
if err != nil {
33+
failMessage(err.Error())
34+
}
35+
if can.Received() {
36+
msg, err := can.Rx()
37+
if err != nil {
38+
failMessage(err.Error())
39+
}
40+
fmt.Printf("CAN-ID: %03X dlc: %d data: ", msg.ID, msg.Dlc)
41+
for _, b := range msg.Data {
42+
fmt.Printf("%02X ", b)
43+
}
44+
fmt.Print("\r\n")
45+
}
46+
time.Sleep(time.Millisecond * 500)
47+
}
48+
}
49+
50+
func failMessage(msg string) {
51+
for {
52+
println(msg)
53+
time.Sleep(1 * time.Second)
54+
}
55+
}

0 commit comments

Comments
 (0)