more detail tutorial for external C modules?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
starter111
Posts: 40
Joined: Wed Mar 08, 2017 7:24 am

more detail tutorial for external C modules?

Post by starter111 » Wed Aug 07, 2019 10:26 pm

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!

User avatar
mattyt
Posts: 246
Joined: Mon Jan 23, 2017 6:39 am

Re: more detail tutorial for external C modules?

Post by mattyt » 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

OutoftheBOTS_
Posts: 661
Joined: Mon Nov 20, 2017 10:18 am

Re: more detail tutorial for external C modules?

Post by OutoftheBOTS_ » Thu Aug 08, 2019 6:57 am

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??

User avatar
mattyt
Posts: 246
Joined: Mon Jan 23, 2017 6:39 am

Re: more detail tutorial for external C modules?

Post by mattyt » 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?

OutoftheBOTS_
Posts: 661
Joined: Mon Nov 20, 2017 10:18 am

Re: more detail tutorial for external C modules?

Post by OutoftheBOTS_ » Thu Aug 08, 2019 7:59 am

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.

User avatar
jimmo
Posts: 791
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: more detail tutorial for external C modules?

Post by jimmo » Thu Aug 08, 2019 9:30 am

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.

starter111
Posts: 40
Joined: Wed Mar 08, 2017 7:24 am

Re: more detail tutorial for external C modules?

Post by starter111 » Thu Aug 08, 2019 7:11 pm

Stub Generator is good.. and I think I need to read more code...

Post Reply