Page 1 of 1

more detail tutorial for external C modules?

Posted: Wed Aug 07, 2019 10:26 pm
by starter111
I'm trying to write a software bit banging c modules for an non standard protocol, which is similar to I2C.
I follow this tutorial, looks like working but I still have a lot of questions. I'm not good in C, detail tutorial will be very help!
https://docs.micropython.org/en/latest/ ... l-c-module

what I need..
configure 2 IO pins as push-pull, able to pull them low/high and read.
Questions...
1. How do I make above example into class?
I'd like to have something can take constructor arguments like i2c = I2C(scl='X1', sda='X2', freq=100000)
2. I'm trying to modify existing files to meet what I need, which one better?
micropython\ports\stm32\machine_i2c.c
micropython\ports\stm32\pin.c
micropython\extmod\machine_i2c.c

Thanks!

Re: more detail tutorial for external C modules?

Posted: Thu Aug 08, 2019 2:49 am
by mattyt
Hi starter111,

At the recent PyCon AU I just gave a talk on Extending MicroPython by using C modules. I intend to put together a github repo with the example code but the slides are available, though it was mostly about "here's what can be done" rather than the actual details. I did - very briefly - provide an example of how to define a class and this will be in the repo when I finish it (this weekend hopefully).

I also intend to write a forum post here with links to the 5-6 talks that covered MicroPython topics as well as some of the work undertaken during the sprints. Stay tuned!

Although it doesn't support classes yet, the MicroPython C Stub Generator may also be of use. During the sprints an alternative stub generator - uStubby - was also built; the advantage here is that it works offline and uses Python (with type hints) to define the interface. It was inspired by some Cython concepts.

Cheers,
Matt

Re: more detail tutorial for external C modules?

Posted: Thu Aug 08, 2019 6:57 am
by OutoftheBOTS_
mattyt wrote:
Thu Aug 08, 2019 2:49 am
Hi starter111,

At the recent PyCon AU I just gave a talk on Extending MicroPython by using C modules. I intend to put together a github repo with the example code but the slides are available, though it was mostly about "here's what can be done" rather than the actual details. I did - very briefly - provide an example of how to define a class and this will be in the repo when I finish it (this weekend hopefully).

I also intend to write a forum post here with links to the 5-6 talks that covered MicroPython topics as well as some of the work undertaken during the sprints. Stay tuned!

Although it doesn't support classes yet, the MicroPython C Stub Generator may also be of use. During the sprints an alternative stub generator - uStubby - was also built; the advantage here is that it works offline and uses Python (with type hints) to define the interface. It was inspired by some Cython concepts.

Cheers,
Matt
This certainly takes MP to a new level. The stub generator makes it so much easier for someone like me learning. The talk very much showed why someone would want to implement certain parts (heavy computational) in C and then wrap them up for MP. It covered the wrapping up part quite a lot but didn't show the compiling part. Did you need to recompile the MP firmware with the new QR module in it or was the QR code compiled separately then the standard MP called the module at run time??

Re: more detail tutorial for external C modules?

Posted: Thu Aug 08, 2019 7:47 am
by mattyt
We actually (sort-of) did both.

The 'standard' way to do it is to build the MP firmware and include the (unchanged) QR C code, adding a wrapper to expose an interface to MicroPython that maps to the C functionality. This way the MicroPython firmware, deployed to the device, contains a QR module. It looks and behaves like a Python module but C code is called under the hood.

Since 1.10 it is also possible to use 'native' modules. This allows you to build something very similar - you still need to compile the QR C code and a wrapper - but it is separate from the MicroPython firmware. Instead of a complete firmware blob, the output of this is an .mpy file that contains native code. Again, when you load this in MicroPython is appears like a regular Python module but is calling C code internally. There are some technical reasons why native modules are difficult to get right and they're not quite working on all ports, nor is the tooling to construct them quite ready for prime-time. So this is more of a 'preview'!

I will try and pull together better descriptions - and the examples I used - soon! Sorry it's not available right now... Hopefully that's a little clearer?

Re: more detail tutorial for external C modules?

Posted: Thu Aug 08, 2019 7:59 am
by OutoftheBOTS_
mattyt wrote:
Thu Aug 08, 2019 7:47 am
We actually (sort-of) did both.

The 'standard' way to do it is to build the MP firmware and include the (unchanged) QR C code, adding a wrapper to expose an interface to MicroPython that maps to the C functionality. This way the MicroPython firmware, deployed to the device, contains a QR module. It looks and behaves like a Python module but C code is called under the hood.

Since 1.10 it is also possible to use 'native' modules. This allows you to build something very similar - you still need to compile the QR C code and a wrapper - but it is separate from the MicroPython firmware. Instead of a complete firmware blob, the output of this is an .mpy file that contains native code. Again, when you load this in MicroPython is appears like a regular Python module but is calling C code internally. There are some technical reasons why native modules are difficult to get right and they're not quite working on all ports, nor is the tooling to construct them quite ready for prime-time. So this is more of a 'preview'!

I will try and pull together better descriptions - and the examples I used - soon! Sorry it's not available right now... Hopefully that's a little clearer?
Thanks Matt and all the other that are contributing to this great platform. Mp is developing in to a very powerful tool and it is easy to see its future becoming very wide spread.

Re: more detail tutorial for external C modules?

Posted: Thu Aug 08, 2019 9:30 am
by jimmo
starter111 wrote:
Wed Aug 07, 2019 10:26 pm
what I need..
configure 2 IO pins as push-pull, able to pull them low/high and read.
You'll need to use mp_hal_pin_input / _output / _high / _low /_read.
starter111 wrote:
Wed Aug 07, 2019 10:26 pm

Questions...
1. How do I make above example into class?
I'd like to have something can take constructor arguments like i2c = I2C(scl='X1', sda='X2', freq=100000)
I'd recommend taking a look at a simpler class as an example. extmod/modframebuf.c is probably one good example.
starter111 wrote:
Wed Aug 07, 2019 10:26 pm
2. I'm trying to modify existing files to meet what I need, which one better?
micropython\ports\stm32\machine_i2c.c
micropython\ports\stm32\pin.c
micropython\extmod\machine_i2c.c
i2c probably isn't a good place to start because as well as defining a relatively complicated API, it's also defining a hardware peripheral abstraction for a wide range of microcontrollers.

Perhaps modonewire would be a better example to look at.

Re: more detail tutorial for external C modules?

Posted: Thu Aug 08, 2019 7:11 pm
by starter111
Stub Generator is good.. and I think I need to read more code...