How to call C function in python code

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
soibac35
Posts: 5
Joined: Thu Aug 24, 2017 2:48 pm

How to call C function in python code

Post by soibac35 » Wed Sep 06, 2017 8:38 pm

Hi everyone,

I am a bit new to micropython and python is general.

So i have successfully call python functions in C code using micropython library.

Now i want to do the other way around, calling C function in python code using micropython library.

How would i do that ? Can someone give me an extremely simple example on that ? I am aware that i can use Ctypes in python and load cdll file but it wont work when I load it on a MCU chip since the chip wont have any system file.

thanks

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

Re: How to call C function in python code

Post by dhylands » Wed Sep 06, 2017 10:44 pm

You need to create a C module which exports functions which are callable from python.

See this thread here: viewtopic.php?f=16&t=2861&p=19206#p19206

soibac35
Posts: 5
Joined: Thu Aug 24, 2017 2:48 pm

Re: How to call C function in python code

Post by soibac35 » Thu Sep 07, 2017 5:36 pm

so for the c_sample.c, do i need to flash it onto a chip before i can run it on terminal ?

if so, is there a way just to test it on console output without flashing it on chip ?

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

Re: How to call C function in python code

Post by dhylands » Thu Sep 07, 2017 6:30 pm

Currently, you'll need to build and flash custom firmware. The unix port is the only port which supports dynamic linking of code (via FFI).

Post Reply