Canbus

CANBUS is used in many places: cars, satellites, control system

It is supported by many controllers as you can see below.

code update 27 feb 24 13:01




CANBUS and Teensy

We have for years used version 3.6 with success.

  • Teensy is an ARM based microcontroller. teensy info

  • Version 3.6 has two CAN interfaces.

  • We are using FlexCAN libary lib here

  • The github version by Pawelsky

See more about the interfacing of canbus here

In the folderlist below is some coding examples - tested.

Src folder list: here




Please note the Teensy is without any realtime kernel so you have to stick to old fashion non-preemptive coding style.

Borrowed from Pawelsky github

from by Pawelsky

begin() Enable the CAN to start actively participating on the CANbus.

end() Disable the CAN from participating on the CANbus. Pins remain assigned to the alternate function CAN0.

write(message) Send a frame of up to 8 bytes using the given identifier. write() will return 0 if no buffer was available for sending (see “Caller blocking” below).

message is a CAN_message_t type buffer structure.

read(message) Receive a frame into “message” if available. read() will return 1 if a frame was copied into the callers buffer, or 0 if no frame is available (see “Caller blocking” below).

available() Returns 1 if at least one receive frame is waiting, or 0 if no frame is available. Use of Optional RX Filtering

begin(mask) Enable the CAN to start actively participating on the CANbus. Enable reception of all messages that fit the mask. This is a global mask that applies to all the receive filters.

setFilter(filter, number) Set the receive filter selected by number, 0-7. When using filters it is required to set them all. If the application uses less than 8 filters, duplicate one filter for the unused ones.

The mask and filter are CAN_filter_t type structures. Caller Blocking Support

Support has been included for wait / blocking in both the read() and write() calls.

When the CAN_message_t field timeout is given, the read() and write() calls will wait if needed until the frame transfer can take place. The maximum wait for transfer is specified by timeout in milliseconds. If the call times out, it will return 0 as in the non-blocking case.

Setting the timeout field to 0 will make the calls non-blocking.

The timeout monitoring mechanism calls yield() until a buffer is found or the timeout time is exceeded. In-order Transmission Caller blocking can be used to write() frames guaranteed in-order to the bus. When caller blocking is selected for write() (non-zero timeout specified), a single hardware transmit buffer is used.