pass bytearray from C code to mpy in timer ISR

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

pass bytearray from C code to mpy in timer ISR

Post by ajie_dirgantara » Tue Oct 17, 2017 6:24 am

I am trying to return a bytearray from C code as below :

Code: Select all

STATIC mp_obj_t prog_getAllData(void) {
	return mp_obj_new_bytearray(sizeof(dataDI.bytes), (mp_obj_t*)&dataDI.bytes);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(prog_getAllData_obj, prog_getAllData);
when I used it as :

Code: Select all

import prog
prog.getAllData()
it works fine,

but when I use it in timer callback, it return memory error as :

Code: Select all

>>>uncaught exception in Timer(3) interrupt handler
Traceback (most recent call last):
  File "<stdin>", line 2, in test
MemoryError: memory allocation failed, heap is locked
Is there really no other way to use list/bytearray in ISR?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: pass bytearray from C code to mpy in timer ISR

Post by pythoncoder » Tue Oct 17, 2017 9:47 am

You need to pre-allocate the bytearray and have the ISR populate it. For the ISR to access the bytearray it can be global. Alternatively the bytearray can be a class variable and the ISR a bound method.

See http://docs.micropython.org/en/latest/p ... rules.html.
Peter Hinch
Index to my micropython libraries.

Post Reply