interface to handle callback from C code

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

interface to handle callback from C code

Post by ajie_dirgantara » Wed Sep 20, 2017 4:45 am

I've understand about creating an interface in C to be called from python. What I need now is how if I wanted to set callback from python, and then call those callback is from C. or maybe create some kind like software interrupt from C. how can I achieve this?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: interface to handle callback from C code

Post by dhylands » Wed Sep 20, 2017 5:33 pm

The c_sample code mentioned in this thread: viewtopic.php?f=16&t=2861&p=19206#p19206 shows calling C code from python and having the C code call a callback written in python.

In particular, this line: https://github.com/dhylands/micropython ... c46acdfR24

ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

Re: interface to handle callback from C code

Post by ajie_dirgantara » Thu Sep 21, 2017 5:53 am

dhylands wrote:The c_sample code mentioned in this thread: viewtopic.php?f=16&t=2861&p=19206#p19206 shows calling C code from python and having the C code call a callback written in python.

In particular, this line: https://github.com/dhylands/micropython ... c46acdfR24

Ok, is it safe to do this?

Code: Select all




mp_obj_t cb1_obj;
mp_obj_t cb2_obj;

STATIC mp_obj_t c_sample_set_callback1(mp_obj_t callback_obj) {
    cb1_obj = callback_obj;
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(c_sample_set_callback1_obj, c_sample_set_callback1);

STATIC mp_obj_t c_sample_set_callback2(mp_obj_t callback_obj) {
    cb2_obj = callback_obj;
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(c_sample_set_callback2_obj, c_sample_set_callback2);


then in an ISR to do :

Code: Select all


extern mp_obj_t cb1_obj;
extern mp_obj_t cb2_obj;

mp_call_function_0(cb1_obj);
mp_call_function_0(cb2_obj);


User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: interface to handle callback from C code

Post by dhylands » Fri Sep 22, 2017 4:43 pm

That looks reasonable. Note, that when calling code from within an ISR you should wrap the call in a try/except. In C code this would look something like this:
https://github.com/micropython/micropyt ... 1376-L1396
That also disables memory allocations (since ISRs aren't allowed to perform memory allocations).

The nlr_push returns 0 if the code runs with no exception and returns non-zero if an exception was raised.

ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

Re: interface to handle callback from C code

Post by ajie_dirgantara » Wed Sep 27, 2017 2:48 am

dhylands wrote:That looks reasonable. Note, that when calling code from within an ISR you should wrap the call in a try/except. In C code this would look something like this:
https://github.com/micropython/micropyt ... 1376-L1396
That also disables memory allocations (since ISRs aren't allowed to perform memory allocations).

The nlr_push returns 0 if the code runs with no exception and returns non-zero if an exception was raised.

I've try your sample code, but it fails to run the callback (no exception is raised but it just reset and back to repl), do you think below mp_obj_t should be initialized into callback object before called?

Code: Select all

mp_obj_t cb1_obj;
mp_obj_t cb2_obj;
and what is MP_STATE_PORT() macros do? in the very first example of yours, how can I create two different callbacks?
Last edited by ajie_dirgantara on Wed Sep 27, 2017 3:06 am, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: interface to handle callback from C code

Post by dhylands » Wed Sep 27, 2017 3:03 am

ajie_dirgantara wrote:
dhylands wrote:I've try your sample code, but it fails to run the callback (no exception is raised but it just reset and back to repl), do you think below mp_obj_t should be initialized into callback object before called?

Code: Select all

mp_obj_t cb1_obj;
mp_obj_t cb2_obj;
Yes. If you don't call set_callback and then try to call call_callback you'll get a crash. You should probably initialize them to the none object (mp_obj_t)&mp_const_none_obj; and then you can test if they're set before trying to call them.

Post Reply