How to use malloc in user c module? (repl freezes) #17592
Replies: 2 comments 1 reply
-
MicroPython has its own heap and GC, standard c Take a look at "py/misc.h" for "/** memory allocation " void *m_malloc(size_t num_bytes);
void m_free(void *ptr); In my implementation, I use static mp_obj_t camera_capture(){
//acquire a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
ESP_LOGE(TAG, "Camera Capture Failed");
return mp_const_false;
}
mp_obj_t image = mp_obj_new_bytes(fb->buf, fb->len);
//return the frame buffer back to the driver for reuse
esp_camera_fb_return(fb);
return image;
}
static MP_DEFINE_CONST_FUN_OBJ_0(camera_capture_obj, camera_capture); and let the GC handle the memory housekeeping. |
Beta Was this translation helpful? Give feedback.
-
I haven't worked much with the RP2350. I can't offer any advice about DMA. However, port/rp2/rp2_dma.c might give you some ideas. Perhaps others can provide assistance here. You can use DMA from Python: https://docs.micropython.org/en/latest/library/rp2.DMA.html#rp2.DMA |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm porting a camera module over to micropython as a user c module.
The camera is using a 153kB malloced variable to store the picture. The module compiles fine, but when I try to call this function, the micropython shell freezes.
I reduced the problem using a simple function inside the official
cexample
user C module, I have a variable there using malloc, and it freezes the same way. Here is the full modified examplemodule.c file:(cam_ptr variable, and init_cam function)
This is the problematic line, where the mciropython repl freezes:
cam_ptr = (uint8_t *)malloc(2); //320*240*2 =153600
How am I supposed to use the malloc function in a C module? What am I missing?
((I'm only allocate 2 bytes as test, so the size should not be a problem.))
Any help is much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions