uncaught exception in Timer(1) interrupt handler MemoryError:
Hello, here is my problem:
import pyb
import ps2
import encoder
from pyb import LED, Timer
from machine import I2C,Pin
from oled import SSD1306_I2C
def mainTimer_cb(cb):
'''
#函数功能:用于总体程序的逻辑定时功能
'''
global count
LED(3).toggle()
count += 1
print(count)
if (count%8) == 0:
LED(2).toggle()
elif (count%20) == 0:
count = 0
elif (count%5):
oled_draw(count)
def oled_draw(text):
oled.text(str(text),20,0)
count = 0
mainTimer = Timer(1, freq=2, callback=mainTimer_cb)
i2c = I2C(scl = Pin("X9"),sda = Pin("X10"),freq = 10000)
oled = SSD1306_I2C(128, 64, i2c) #创建oled对象
oled.text("count:",0,0)
oled.text("i love u!",20,40)
oled.show()
while True:
i = 0
when i ran this code in pyb, it always got the memory error. So i wanna ask how can i write my main code in callback function? Because i can do this by C in STM32, just so confused to the error.
Integer math exceptions in interrupt handlers
This issue may be a bug. It may also just be something that users need to be aware of when writing interrupt handlers.
I have been having issues on several projects where performing simple math in interrupt handlers causes "MemoryError: memory allocation failed, heap is locked" exceptions. In my cases, I have been using pyb.micros() timer values to make relative timing measurements between events. The problem is appeared to be intermittent but is actually based on the timer values when pyb.micros() is near maxint (0x3fffffff).
The problem shows with both variable and array integers:
x = 0
or
x = array.array('i',[0])
Repro:
import intException
intException.doit()
This should yield this error message:
Uncaught exception in ExtInt interrupt handler line 4
Traceback (most recent call last):
File "intException.py", line 17, in testint
MemoryError: memory allocation failed, heap is locked