Adding new Python Commands

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
felix.letkemann
Posts: 10
Joined: Sat Mar 21, 2015 1:29 pm

Adding new Python Commands

Post by felix.letkemann » Tue Mar 31, 2015 3:32 pm

Hi there! :)
You may have seen my post asking about accelerating the ADC using DMA. Damien solved my issue by changing the firmware so that sampling can be done much faster now. Nevertheless I would like to try to implement ADC sampling using DMA by changing the firmware.
Unfortunately I haven't been able to add any functions that are accecible from python. I changed the stmhal/adc.c, I forked the function read_timed and added the new function to the mapping:

Code: Select all

STATIC const mp_map_elem_t adc_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj},
    { MP_OBJ_NEW_QSTR(MP_QSTR_read_timed), (mp_obj_t)&adc_read_timed_obj},
    { MP_OBJ_NEW_QSTR(MP_QSTR_read_timed_dma), (mp_obj_t)&adc_read_timed_dma_obj}
};
Unfortunately, I could not compile, because the mapping fails:

Code: Select all

CC adc.c
In file included from ../py/mpstate.h:34:0,
                 from ../py/runtime.h:29,
                 from adc.c:32:
adc.c:347:23: error: 'MP_QSTR_read_timed_dma' undeclared here (not in a function)
     { MP_OBJ_NEW_QSTR(MP_QSTR_read_timed_dma), (mp_obj_t)&adc_read_timed_dma_obj}
                       ^
../py/obj.h:90:56: note: in definition of macro 'MP_OBJ_NEW_QSTR'
 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
Searching for the problem, I noticed that the only occurence of the string "MP_QSTR_read_timed" (which seems to be the thing that is missing for my function) is in an automaticly generated file called qstrdefs.generated.h - but I have no idea how to trigger the auto generation.
The only post about this I could find is: http://forum.micropython.org/viewtopic.php?t=303

.... but I think this is not my case because I am trying to add a function that is specific for the pyboard.
How did you (developers) add functions? Is there any tutorial about this?

Thank you very much!
Felix

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Adding new Python Commands

Post by blmorris » Tue Mar 31, 2015 4:34 pm

I'm in the midst of this myself, glad to see another user getting in to hacking / enhancing the C code.

There is a (very incomplete) guide here: http://wiki.micropython.org/Creating-Pyb-Modules
Another user was kind enough to share his experience before moving on; I've found it useful even in this minimal form and hope to add to it soon.

For now, try adding your function name 'adc_read_timed_dma' to 'stmhal/qstrdefsport.h' and see if that helps.

-Bryan

felix.letkemann
Posts: 10
Joined: Sat Mar 21, 2015 1:29 pm

Re: Adding new Python Commands

Post by felix.letkemann » Tue Mar 31, 2015 6:52 pm

Thank you very much Bryan,
I will try it out and tell you if that worked :)

felix.letkemann
Posts: 10
Joined: Sat Mar 21, 2015 1:29 pm

Re: Adding new Python Commands

Post by felix.letkemann » Tue Mar 31, 2015 8:12 pm

It works! Thank you very much!

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Adding new Python Commands

Post by blmorris » Tue Mar 31, 2015 9:15 pm

Great! I'll be interested to see how you handle DMA, I will be using the SPI implementation as my reference as I get to that part of I2S.


Post Reply