How to add c driver to stm build

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
miltmobley
Posts: 30
Joined: Mon Mar 07, 2016 11:44 pm

How to add c driver to stm build

Post by miltmobley » Wed Jul 06, 2016 11:02 pm

I want to make a driver in c/c++ and get it included in the stmhal build. What do I have to change?

I see in mpconfigport.h:
(1) There is a section: "#define MICROPY_PORT_BUILTIN_MODULES"
(2) mp_modusocket is listed in the section
(3) modusocket.c is listed in the Makefile
(4) modusocket.o is built

(5) There is also a section: "#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS"
(6) mp_modujson is listed in the section
(7) modujson.c is NOT listed in the Makefile
(8) modujson.o is NOT built

The code will do too many small writes to give an efficient implementation in Python.

My first thought was to modify an existing c module, e.g stmhal/lcd.c. But the only stm specific part of the code is spi access methods.
So it would be better to locate most of the code in drivers folder, where any port could use it.

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

Re: How to add c driver to stm build

Post by dhylands » Thu Jul 07, 2016 1:14 am

I created a branch in my micropython repository called c_sample.

It creates a new module called c_sample (as part of stmhal), which a couple of functions which show registering a python callback function and then calling that function from C.

You can find the changeset here:
https://github.com/dhylands/micropython ... 0d64ef7172

The link to modujson.o can be found here: https://github.com/micropython/micropyt ... py.mk#L197
and py.mk is included by the Makefile. extmod contains modules which are essentially port independent (and currently I don't believe that the socket stuff is port independent - which is why each port has their own). Some modules may be in the wrong location due to historical reasons (the source tree gets restructured a bit over time as new code is added).

miltmobley
Posts: 30
Joined: Mon Mar 07, 2016 11:44 pm

Re: How to add c driver to stm build

Post by miltmobley » Sat Jul 09, 2016 9:55 pm

modujson and modusocket were just examples I thought might indicate something relevant to my need.
Since original post I did a git pull, and noticed a comparatively new drivers file, dht.c. It exposes a single c function
to the micropython layer, and the function is used in the esp8266 module, which of course has a c implementation,
but a python interface. This looks like an approach I could use, but I will look at your example too.

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

Re: How to add c driver to stm build

Post by dhylands » Sat Jul 09, 2016 10:11 pm

I often use modmachine.c https://github.com/micropython/micropyt ... dmachine.c to look at since it has quite a few functions with different numbers of arguments/return types.

Post Reply