github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/can/main.go (about)

     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", rxMsg.ID, rxMsg.DLC)
    41  				for j := byte(0); j < rxMsg.Length(); j++ {
    42  					fmt.Printf(" %02X", rxMsg.DB[j])
    43  				}
    44  				fmt.Printf("\r\n")
    45  			}
    46  		}
    47  
    48  		sz1 := can1.RxFifoSize()
    49  		if sz1 > 0 {
    50  			fmt.Printf("CAN1 %d\r\n", sz1)
    51  			for i := 0; i < sz1; i++ {
    52  				can1.RxRaw(&rxMsg)
    53  				fmt.Printf("-> %08X %X", rxMsg.ID, rxMsg.DLC)
    54  				for j := byte(0); j < rxMsg.Length(); j++ {
    55  					fmt.Printf(" %02X", rxMsg.DB[j])
    56  				}
    57  				fmt.Printf("\r\n")
    58  			}
    59  		}
    60  	}
    61  }