项目作者: knieriem

项目描述 :
Go package providing an experimental driver for the MCP2515 CAN controller
高级语言: Go
项目地址: git://github.com/knieriem/mcp2515.git
创建时间: 2020-07-19T14:52:26Z
项目社区:https://github.com/knieriem/mcp2515

开源协议:MIT License

下载


This package provides a driver written in Go for the MCP2515 CAN controller.

Although still lacking configuration options (a bitrate of 500kBit/s is hard-coded),
and interrupt support, the driver — as part of a development tool,
a CAN-SPI-gateway written in TinyGo and running on the BBC microbit —
was sufficient for developing an SPI bootloader.

Creating a driver instance

  1. import "github.com/knieriem/mcp2515"
  2. ...
  3. // Setup an SPI connection to the MCP2515 device,
  4. // which may be anything satisfying the interface:
  5. // type Conn interface {
  6. // TxRx(tx, rx []byte) error
  7. // }
  8. //
  9. spiConn := ...
  10. d := mcp2515.NewDevice(spiConn)
  11. // Init performs a Reset, sets the Bitrate to 500kBit/s,
  12. // configures filters to allow any message,
  13. // and enables rollover mode.
  14. // It would be preferable to have functional configuration options,
  15. // but they have not been implemented yet.
  16. err := d.Init()
  17. if err != nil {
  18. return err
  19. }
  20. ...

Sending a message

  1. import "github.com/knieriem/can"
  2. ...
  3. var msg can.Msg
  4. msg.Id = 0x1234567
  5. msg.Flags |= can.ExtFrame
  6. msg.Len = 2
  7. msg.Data[0] = ...
  8. msg.Data[1] = ...
  9. err = d.Write(&msg)
  10. if err != nil {
  11. if err == mcp2515.ErrTxBufNotEmpty {
  12. // ...
  13. }
  14. return err
  15. }

Receiving a message

  1. var m can.Msg
  2. err = d.Read(&m)
  3. if err != nil {
  4. if err == mcp2515.ErrNoMsg {
  5. // time.Sleep(300 * time.Microsecond)
  6. // try again
  7. }
  8. return err
  9. }
  10. println("->", m.Id, m.Len, m.Data[0])