Table of Contents

CAN


  • Can.Initialize(baudrate) - Initialize CAN with special baudrate
    baudrate: Accepts 125000, 250000, 500000, 1000000

  • Can.InitializeExt(phase1, phase2, prescaler, synchronizationJumpWidth) - User defined baudrate. Max 1000000 (1Mbs)
    phase1: Time segment 1. Max is 15
    phase2: Time segment 2. Max is 7
    prescaler: Baudrate prescaler, this value defines the length of a time quanta. Max is 1023
    synchronizationJumpWidth: Synchronization jump width. Max is 3

  • Can.Available() - Count
    Returns: How many messages have been buffered and ready to be read

  • Can.Write(message) - Write message
    message: Message to send on CAN

  • Can.Read() - Read message
    Return: Message from CAN

Note

CAN peripheral clock is 80MHz

CanMessage

To send or received a message, CanMessage class is used

  • Id - CAN id
  • Extended - Extended flag
  • RemoteRequest - RemoteRequest flag
  • Length - Length of data, max is 8
  • Data - Array, max 8 bytes

The example below initialize CAN at 500Kbs, check any message available, read, increase ID by 1 and write it back to sender

duelink.Can.Initialize(500000)

while True :
    if (duelink.Can.Available() > 0):    
        message = duelink.Can.Read()

        # print received message detail
        print(f"id: {message.Id}, ext: {message.Extended}, rtr: {message.RemoteRequest}, data: {message.Data[0]}, {message.Data[1]}, {message.Data[2]}, {message.Data[3]}, {message.Data[4]}, {message.Data[5]}, {message.Data[6]}, {message.Data[7]}")
        
        # Increase message.Id by 1
        message.Id = message.Id + 1
        
        # send back to sender
        duelink.Can.Write(message)
    
    time.sleep(1)
Tip

To use CanMessage class, need to import:
from DUELink.Can import CanMessage