CAN ISR - MemoryError due to can.recv(0)

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
Apollo22
Posts: 6
Joined: Fri Jul 21, 2017 2:27 pm

CAN ISR - MemoryError due to can.recv(0)

Post by Apollo22 » Fri Jul 21, 2017 2:57 pm

1) What I want to do

I am currently working on an OpenMV Cam M7 and trying to implement CAN communication with another STM32 card that transmits a lot of messages (some every 10ms). As the microcontroller can only store 3 messages in its FIFO and as the CAN bus 2 (the only one available on the OpenMV Cam) only has 2 FIFO (that I can specify through the Filters), I want to implement a software FIFO.

2) The problem

The problem is that the function to extract a message from the FIFO (CAN.recv(fifo)) cannot be used in an ISR because it allocates some memory and that allocating memory cannot be done in an ISR. Has anyone managed to solve this problem ?

3) Places where this question was already answered

I found some people that already had this problem but it wasn't resolved or can't / don't know how to do it on the OpenMV Cam

viewtopic.php?f=14&t=2099&p=11871&hilit ... ecv#p11871

viewtopic.php?t=1967

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: CAN ISR - MemoryError due to can.recv(0)

Post by dbc » Mon Jul 24, 2017 5:00 pm

Kind of a sticky one. It would be nice if there were a version of CAN.recv() that wrote to a pre-allocated buffer.

My first thought would be to use array.array() to pre-allocate a buffer, and then write the call-back as inline assembly that read from the hardware registers and wrote to the pre-allocated buffer. Kind of painful but that's the best suggestion I have right now.

Apollo22
Posts: 6
Joined: Fri Jul 21, 2017 2:27 pm

Re: CAN ISR - MemoryError due to can.recv(0)

Post by Apollo22 » Wed Aug 09, 2017 7:58 am

Thank you for the reply.

I ended up going through another microcontroller (programmed in C++) to control my peripherals and limiting the amount of message going to the micropython board.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: CAN ISR - MemoryError due to can.recv(0)

Post by dbc » Sat Aug 12, 2017 9:47 pm

Note that "softirq" functionality as mentioned in one of your linked threads became micropython.schedule(), which is parhaps another solution to this problem.

Post Reply