Calling Python code from C

42 views
Skip to first unread message

pir...@gmail.com

unread,
Feb 24, 2012, 9:25:02 PM2/24/12
to python-o...@googlegroups.com
There's any way to call Python code from C with PyMite? I wanted to
develop an interrupt handler but the only information i have found is
http://groups.google.com/group/python-on-a-chip/browse_thread/thread/23a4a56e3b259660
It's a good option, i would be able to buffer the interruptions in C
and later recolect them from inside the VM, but i wanted to develop a
more real-time event-driven system... :-/

--
"Si quieres viajar alrededor del mundo y ser invitado a hablar en un
monton de sitios diferentes, simplemente escribe un sistema operativo
Unix."
– Linus Tordvals, creador del sistema operativo Linux

Dean Hall

unread,
Feb 25, 2012, 1:35:41 AM2/25/12
to python-o...@googlegroups.com
Within the interrupt, you could launch a thread. If your ISR code lasts too long, the scheduler will swap back to your main thread in a round-robin fashion.

Look at src/tests/system/runtests.c:77 (t075()) to see how two threads can be arranged. Here's how I would adapt this for your case:

In your main.c, after pm_init() but before pm_run():
pPmObj_t pIsrMod;
uint8_t const *pIsrModStr = (uint8_t const *)"yourIsr";

retval = string_new(& pIsrModStr, &pstring);
PM_RETURN_IF_ERROR(retval);
retval = mod_import(pstring, & pIsrMod);
PM_RETURN_IF_ERROR(retval);

In your C ISR:
retval = interp_addThread((pPmFunc_t) pIsrMod);
interp_setRescheduleFlag(C_TRUE);

Note 1: If you get an exception from interp_addThread(), you are in a bad spot because the ISR doesn't get propagated outside the ISR and the VM won't know that the exception occurred.

Note 2: This example runs the whole module as the ISR. So put your interrupt-handling py code straight into the "yourIsr.py" module. Avoid importing anything or creating functions in yourIsr.py because it is a significant time and memory penalty.

Note 3: A module gets a blank globals dictionary during mode_new() called by mode_import() the first time it is run. The module will keep its globals for all subsequent times it is called by the ISR. However, this globals dictionary is different from the globals being used by your main program. So there will be no way to share python variables from ISR back to your main using the method described above.

There is a way to have the python ISR and your main python code share the same globals namespace, but it would require more time to create this code (sorry I don't have that time right now).


!!Dean

> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google Group.
> Site: http://groups.google.com/group/python-on-a-chip

pir...@gmail.com

unread,
Feb 25, 2012, 4:11:27 AM2/25/12
to python-o...@googlegroups.com

> Within the interrupt, you could launch a thread.

Oh, pretty interesting way, i didn't think about it :-D Maybe it's not the most optimus (at least compared to a traditional ISR handler) but it's pretty elegant and simple to understand! Thanks for the idea! :-D

> If your ISR code lasts too long, the scheduler will swap back to your main thread in a round-robin fashion.
>

Hum... Are threads preemtible? If i understood correctly, PyMite is able to drop them out from the CPU if they last too long without launchind a yield and later come back, is it true?

> Note 3: A module gets a blank globals dictionary during mode_new() called by mode_import() the first time it is run.  The module will keep its globals for all subsequent times it is called by the ISR.  However, this globals dictionary is different from the globals being used by your main program.  So there will be no way to share python variables from ISR back to your main using the method described above.
>
> There is a way to have the python ISR and your main python code share the same globals namespace, but it would require more time to create this code (sorry I don't have that time right now).
>

Preemtible, no shared memory... Really they are not process? :-P It's an interesting feature to take in acount, althought at this point is really a problem :-/ Reading about this i'm thinking about launch the ISR by hand with a null interruption to init its structures setting them to the module namespace and have a reference from the main program, and since they are set at the module namespace i would be able to access them from the ISR and viceversa, am i correct?

Reply all
Reply to author
Forward
0 new messages