tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/mcp2515/main.go (about) 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 }