CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Hey!
I have to do CAN communication between OpenMV and AT90CAN128. Is there anyone who can help me out. Any sample code or built in commands.

@Ibrahim? Was CAN enabled?

Yes CAN was enabled in 2.2, never tested though. We don’t have examples but you can use MicroPython’s examples.

Here’s an exemple working (don’t forget to add a transceiver on an external PCB or protoboard).

Init

import pyb

can = pyb.CAN(2)                        #The bus 2(PB12, PB13) is the only can bus on the Open MVCam

can.init(pyb.CAN.NORMAL, extframe=False, prescaler=18, sjw=1, bs1=8, bs2=3) #250kb/s
# APB1 = 54MHz, Prescaler 18 et 250kB/s --> 12 time quanta par bit (1 + 8 + 3 = 12)
can.setfilter(0, pyb.CAN.LIST16, 0, [0x700, 0x700, 0x701, 0x702], rtr = [0, 1, 0, 0])

Emission

data = b'\x01\x50'
can.send(data, id = 0x702, timeout=0, rtr=False)

Be careful if you want to convert int to bytearray, the functions to convert from and to byte are set in little endian, even if you specify big in the function

Reception

 while(can.any(0)):
        message = can.recv(0)
        # print("can : message received") # [Debug]
        if(message[0] == 0x701):
            manual_launch           = False
            cam_has_control         = False
            stop_linetracking       = False
            mother_error            = False
            if(message[3] == b'\x00'):
                cam_has_control     = True
            elif(message[3] == b'\x01'):
                stop_linetracking   = True
            elif(message[3] == b'\x02'):
                mother_error        = True
            elif(message[3] == b'\x03'):
                manual_launch       = True
        elif(message[0] == 0x700):
            if(message[1]):		#RTR bit active
                print('id sent')
            if(message[3] == b'\x01'):
                print('detected')

Be careful on the reception, you can’t use interruption because can.recv() allocates memory, which is not currently allowed in an interruption.

1 Like