Page 1 of 1

CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Posted: Tue Aug 01, 2017 3:26 am
by uzair mazhar awan
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.

Re: CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Posted: Tue Aug 01, 2017 12:33 pm
by kwagyeman
@Ibrahim? Was CAN enabled?

Re: CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Posted: Thu Aug 03, 2017 8:47 pm
by iabdalkader
Yes CAN was enabled in 2.2, never tested though. We don't have examples but you can use MicroPython's examples.

Re: CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Posted: Fri Aug 11, 2017 5:30 am
by Apollo22
Here's an exemple working (don't forget to add a transceiver on an external PCB or protoboard).

Init

Code: Select all

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

Code: Select all

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

Code: Select all

 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.