Skip to content

Commit e7c6bd3

Browse files
sago35deadprogram
authored andcommitted
atsame5x: add support for CAN
1 parent 9f5066a commit e7c6bd3

File tree

12 files changed

+682
-5
lines changed

12 files changed

+682
-5
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,12 @@ smoketest:
343343
@$(MD5SUM) test.hex
344344
$(TINYGO) build -size short -o test.hex -target=atsame54-xpro examples/blinky1
345345
@$(MD5SUM) test.hex
346+
$(TINYGO) build -size short -o test.hex -target=atsame54-xpro examples/can
347+
@$(MD5SUM) test.hex
346348
$(TINYGO) build -size short -o test.hex -target=feather-m4-can examples/blinky1
347349
@$(MD5SUM) test.hex
350+
$(TINYGO) build -size short -o test.hex -target=feather-m4-can examples/caninterrupt
351+
@$(MD5SUM) test.hex
348352
# test pwm
349353
$(TINYGO) build -size short -o test.hex -target=itsybitsy-m0 examples/pwm
350354
@$(MD5SUM) test.hex

src/examples/can/feather-m4-can.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// +build feather_m4_can
2+
3+
package main
4+
5+
import (
6+
"machine"
7+
)
8+
9+
func init() {
10+
// power on the CAN Transceiver
11+
// https://learn.adafruit.com/adafruit-feather-m4-can-express/pinouts#can-bus-3078990-8
12+
boost_en := machine.BOOST_EN
13+
boost_en.Configure(machine.PinConfig{Mode: machine.PinOutput})
14+
boost_en.High()
15+
}

src/examples/can/main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"machine"
6+
"time"
7+
)
8+
9+
func main() {
10+
can1 := machine.CAN1
11+
can1.Configure(machine.CANConfig{
12+
TransferRate: machine.CANTransferRate500kbps,
13+
TransferRateFD: machine.CANTransferRate1000kbps,
14+
Rx: machine.CAN1_RX,
15+
Tx: machine.CAN1_TX,
16+
Standby: machine.CAN1_STANDBY,
17+
})
18+
19+
can0 := machine.CAN0
20+
can0.Configure(machine.CANConfig{
21+
TransferRate: machine.CANTransferRate500kbps,
22+
TransferRateFD: machine.CANTransferRate1000kbps,
23+
Rx: machine.CAN0_RX,
24+
Tx: machine.CAN0_TX,
25+
Standby: machine.NoPin,
26+
})
27+
28+
rxMsg := machine.CANRxBufferElement{}
29+
30+
for {
31+
can1.Tx(0x123, []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, false, false)
32+
can1.Tx(0x789, []byte{0x02, 0x24, 0x46, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, true, false)
33+
time.Sleep(time.Millisecond * 1000)
34+
35+
sz0 := can0.RxFifoSize()
36+
if sz0 > 0 {
37+
fmt.Printf("CAN0 %d\r\n", sz0)
38+
for i := 0; i < sz0; i++ {
39+
can0.RxRaw(&rxMsg)
40+
fmt.Printf("-> %08X %X %#v\r\n", rxMsg.ID, rxMsg.DLC, rxMsg.Data())
41+
}
42+
}
43+
44+
sz1 := can1.RxFifoSize()
45+
if sz1 > 0 {
46+
fmt.Printf("CAN1 %d\r\n", sz1)
47+
for i := 0; i < sz1; i++ {
48+
can1.RxRaw(&rxMsg)
49+
fmt.Printf("-> %08X %X %#v\r\n", rxMsg.ID, rxMsg.DLC, rxMsg.Data())
50+
}
51+
}
52+
}
53+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// +build feather_m4_can
2+
3+
package main
4+
5+
import (
6+
"machine"
7+
)
8+
9+
func init() {
10+
// power on the CAN Transceiver
11+
// https://learn.adafruit.com/adafruit-feather-m4-can-express/pinouts#can-bus-3078990-8
12+
boost_en := machine.BOOST_EN
13+
boost_en.Configure(machine.PinConfig{Mode: machine.PinOutput})
14+
boost_en.High()
15+
}

src/examples/caninterrupt/main.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"device/sam"
5+
"fmt"
6+
"machine"
7+
"time"
8+
)
9+
10+
type canMsg struct {
11+
ch byte
12+
id uint32
13+
dlc byte
14+
data []byte
15+
}
16+
17+
func main() {
18+
ch := make(chan canMsg, 10)
19+
go func() {
20+
for {
21+
select {
22+
case m := <-ch:
23+
fmt.Printf("%d %03X %X ", m.ch, m.id, m.dlc)
24+
for _, d := range m.data {
25+
fmt.Printf("%02X ", d)
26+
}
27+
fmt.Printf("\r\n")
28+
}
29+
30+
}
31+
}()
32+
33+
can1 := machine.CAN1
34+
can1.Configure(machine.CANConfig{
35+
TransferRate: machine.CANTransferRate500kbps,
36+
TransferRateFD: machine.CANTransferRate1000kbps,
37+
Rx: machine.CAN1_RX,
38+
Tx: machine.CAN1_TX,
39+
Standby: machine.CAN1_STANDBY,
40+
})
41+
// RF0NE : Rx FIFO 0 New Message Interrupt Enable
42+
can1.SetInterrupt(sam.CAN_IE_RF0NE, func(*machine.CAN) {
43+
rxMsg := machine.CANRxBufferElement{}
44+
can1.RxRaw(&rxMsg)
45+
m := canMsg{ch: 1, id: rxMsg.ID, dlc: rxMsg.DLC, data: rxMsg.Data()}
46+
select {
47+
case ch <- m:
48+
}
49+
})
50+
51+
can0 := machine.CAN0
52+
can0.Configure(machine.CANConfig{
53+
TransferRate: machine.CANTransferRate500kbps,
54+
TransferRateFD: machine.CANTransferRate1000kbps,
55+
Rx: machine.CAN0_RX,
56+
Tx: machine.CAN0_TX,
57+
Standby: machine.NoPin,
58+
})
59+
// RF0NE : Rx FIFO 0 New Message Interrupt Enable
60+
can0.SetInterrupt(sam.CAN_IE_RF0NE, func(*machine.CAN) {
61+
rxMsg := machine.CANRxBufferElement{}
62+
can0.RxRaw(&rxMsg)
63+
m := canMsg{ch: 2, id: rxMsg.ID, dlc: rxMsg.DLC, data: rxMsg.Data()}
64+
select {
65+
case ch <- m:
66+
}
67+
})
68+
69+
for {
70+
can0.Tx(0x123, []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, false, false)
71+
time.Sleep(time.Millisecond * 500)
72+
can1.Tx(0x456, []byte{0xAA, 0xBB, 0xCC}, false, false)
73+
time.Sleep(time.Millisecond * 1000)
74+
}
75+
}

src/machine/board_atsame54-xpro.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,14 @@ var (
328328
SERCOM: 6,
329329
}
330330
)
331+
332+
// CAN on the SAM E54 Xplained Pro
333+
var (
334+
CAN0 = CAN{
335+
Bus: sam.CAN0,
336+
}
337+
338+
CAN1 = CAN{
339+
Bus: sam.CAN1,
340+
}
341+
)

src/machine/board_feather-m4-can.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,29 @@ func init() {
125125
D7.High()
126126
}
127127

128-
// I2C on the Feather M4.
128+
// I2C on the Feather M4 CAN.
129129
var (
130130
I2C0 = &I2C{
131131
Bus: sam.SERCOM2_I2CM,
132132
SERCOM: 2,
133133
}
134134
)
135135

136-
// SPI on the Feather M4.
136+
// SPI on the Feather M4 CAN.
137137
var (
138138
SPI0 = SPI{
139139
Bus: sam.SERCOM1_SPIM,
140140
SERCOM: 1,
141141
}
142142
)
143+
144+
// CAN on the Feather M4 CAN.
145+
var (
146+
CAN0 = CAN{
147+
Bus: sam.CAN0,
148+
}
149+
150+
CAN1 = CAN{
151+
Bus: sam.CAN1,
152+
}
153+
)

0 commit comments

Comments
 (0)