catching an CAN bus interrupt in a class and calling a scheduled function leads to a MemoryError.
I create a class which inherits from pyb.CAN. I initialize Can, and attach a callback function to it. In the callback function I schedule antoher function uising micropython.schedule(). When the schedule function is part of the class, I get an MemoryError. When it is not part of the class, it works:
Code: Select all
class CanInterface(CAN):
# ------------------------------------------------------------------
def __init__(self, itf):
self.can = CAN(itf)
self.init_can_bus()
# ------------------------------------------------------------------
def close_me(self):
can = self.can
can.rxcallback(0, None)
can.deinit()
# ------------------------------------------------------------------
def init_can_bus(self):
can = self.can
can.init(mode=CAN.NORMAL, extframe=False, prescaler=27, sjw=1, bs1=13, bs2=2)
can.setfilter(bank=0, mode=CAN.MASK16, fifo=0, params=(0x0600, 0xFF80, 0x0580, 0xFF80))
can.rxcallback(0, self.cb)
# ------------------------------------------------------------------
def cb(self, bus, reason):
#micropython.schedule(test_func, bus) # works
micropython.schedule(self.test_func, bus) # raises MemoryError: memory allocation faild, heap is locked
# ------------------------------------------------------------------
def test_func(self, bus):
print ("hello")
# ------------------------------------------------------------------
def test_func(bus):
print ("hello")
Code: Select all
# ------------------------------------------------------------------
def cb(self, bus, reason):
micropython.heap_unlock()
micropython.schedule(self.test_func, bus) # works
micropython.heap_lock()
Thanks in advance.