Problem with MicroPython external C modules

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
ales.coppelli
Posts: 34
Joined: Wed Aug 08, 2018 9:15 am

Problem with MicroPython external C modules

Post by ales.coppelli » Sat Oct 24, 2020 8:57 am

Hi to all.

I'm trying to learn how to use extenal C modules in MicroPython.
I have read and implemented the example external C module.
All almost ok.

I prepared the same directoy structure, I copied from the web page the 'example.c' and 'micropython.mk'
in my example.c and micropython.mk.
I modified mine 'micropython.mk' adding ( to the start )

----------- micropython.mk ----------
USERMOD_DIR='the path of my modules/example directory '
EXAMPLE_MOD_DIR := $(USERMOD_DIR)
...
...
-------------------------------------

I'm using a STM32F411 board so I used this command:

$ cd 'where_is_/micropython/ports/stm32

$ make BOARD=STM32F411DISC
USER_C_MODULES=../../../modules
CFLAGS_EXTRA=-DMODULE_EXAMPLE_ENABLED=1
all
deploy-stlink

All ok, the compilation is ok, the firmare is uploaded.
But when I link to the board I obtain

MicroPython v1.13-126-g05f95682e-dirty on 2020-10-24; F411DISC with STM32F411
Type "help()" for more information.
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'example'
>>>

I tryied to put

#define MODULE_EXAMPLE_ENABLED (1)

in 'mpconfigport.h' ( micropython/ports/stm32 )
or in
'mpconfigboard.h' ( micropython/ports/stm32/boards/STM32F411)

(When I used these two cases I launch

$ make BOARD=STM32F411DISC
USER_C_MODULES=../../../modules
all
deploy-stlink
)
but the result is the same:ImportError: no module named 'example'

Where I wrong ?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Problem with MicroPython external C modules

Post by stijn » Sat Oct 24, 2020 11:19 am

Hard to tell without seeing all code/directory structure exactly as you have it.
When building, does it show that it's compiling your code (can also test this by making a syntax error in your code, if there's no compiler error following that it doesn't get built)? One of the first lines should be

Code: Select all

...
Including User C Module from ../../mydirectory
...
Aternatively get the code from https://github.com/micropython/micropython/pull/6525 which has working examples in the source code itself, and start from there

ales.coppelli
Posts: 34
Joined: Wed Aug 08, 2018 9:15 am

Re: Problem with MicroPython external C modules

Post by ales.coppelli » Sat Oct 24, 2020 5:39 pm

Thank you very much stijn for your suggestion.

This is what I obtain when compile the program ( in the second line there is the include) :

-----------------------------------------Compilation --------------------------------------------------------
$ make BOARD=STM32F411DISC USER_C_MODULES=../../../modules CFLAGS_EXTRA=-DMODULE_EXAMPLE_ENABLED=1 all

Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
Including User C Module from ../../../modules/example
mkdir -p build-STM32F411DISC/genhdr
GEN build-STM32F411DISC/genhdr/pins.h
...
...
mkdir -p build-STM32F411DISC/py/
mkdir -p build-STM32F411DISC/usbdev/class/src/
mkdir -p build-STM32F411DISC/usbdev/core/src/
mkdir -p build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/
CC ../../py/mpstate.c
CC ../../py/nlr.c
...
...
CC usbdev/core/src/usbd_ioreq.c
CC usbdev/class/src/usbd_cdc_msc_hid.c
CC usbdev/class/src/usbd_msc_bot.c
CC usbdev/class/src/usbd_msc_scsi.c
CC /home/coppelli/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.c
CC ../../lib/oofatfs/ff.c
CC ../../lib/oofatfs/ffunicode.c
CC build-STM32F411DISC/pins_STM32F411DISC.c
LINK build-STM32F411DISC/firmware.elf
text data bss dec hex filename
322192 24 43456 365672 59468 build-STM32F411DISC/firmware.elf
GEN build-STM32F411DISC/firmware.dfu
GEN build-STM32F411DISC/firmware.hex
--------------------------------------------------------------------------
How to can see the program is compiled without error !


If I watch in firmaware.map I see this:

-------------------------------------------------- Firmware.map --------------------------------------------
$ grep "example" firmware.map

.text 0x0000000000000000 0x0 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.data 0x0000000000000000 0x0 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.bss 0x0000000000000000 0x0 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.text.example_add_ints
0x0000000000000000 0x1a build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.rodata.example_add_ints_obj
0x0000000000000000 0x8 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.rodata.example_module_globals
0x0000000000000000 0x10 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.rodata.example_module_globals_table
0x0000000000000000 0x10 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.rodata.example_user_cmodule
0x0000000000000000 0x8 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
.comment 0x0000000000000000 0x4d build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
0x0000000000000000 0x34 build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
LOAD build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
example_user_cmodule build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
build-STM32F411DISC/~/GIT_CLONE/HACKING_MICROPYTHON/modules/example/example.o
------------------------------------------------------------------------------------

But if I try to see my module ( example ) in 'firmware.elf' it doesn't exist !

$ readelf -a firmware.elf | grep example ( nothing answer )

Why the linker doesn't 'link' "example.o" to the program ?

What can I do ? What tests can I do to understand what's going on?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Problem with MicroPython external C modules

Post by stijn » Sat Oct 24, 2020 6:57 pm

Hmm, no idea honestly. The .map shows the module has been built succesfully - moreover, if I'm not mistaken the .map gets created by the linker, no? But I don't know why it's not in the .elf (or maybe it is, but your grep command doesn't reveal that). Don't have an STM to test so cannot help you any further.

ales.coppelli
Posts: 34
Joined: Wed Aug 08, 2018 9:15 am

Re: Problem with MicroPython external C modules

Post by ales.coppelli » Sun Oct 25, 2020 11:53 am

I have been trying to fix this :-(
( I'm sorry if I ask you any more questions )

I would like to ask you:

Do you know where/when in the [micropython/ports/stm32/]Makefile
(a) it's called/included [myproject/modules/example/]micropython.mk
(b) it's called the linker to 'link' the file example.o
(c) where
( in the [micropython/ports/stm32/]Makefile or maybe in [myproject/modules/example/]micropython.mk )
it's possible to add manually the linking command ?
( for example maybe something like :
...
...
OBJ += $(WHERE_IS_MY_EXAMPLE.O)
...
...

P.S. = my project structure is the same like in the MicroPython site

myproject/ -->
modules/ -->
example.c
micropython.mk
micropython/

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

Re: Problem with MicroPython external C modules

Post by jimmo » Sun Oct 25, 2020 11:51 pm

ales.coppelli wrote:
Sat Oct 24, 2020 8:57 am
I modified mine 'micropython.mk' adding ( to the start )

----------- micropython.mk ----------
USERMOD_DIR='the path of my modules/example directory '
EXAMPLE_MOD_DIR := $(USERMOD_DIR)
You shouldn't need to set USERMOD_DIR -- this is set by py.mk before importing your makefile fragment.

I think the first thing to check is that example.c is really getting compiled. Try adding

Code: Select all

#error hello
inside one of the functions. And ensure that when compiling that error actually is emitted.

(I know it's in the compile log output, but ensure that the preprocessor has the right state to actually enable the contents).

ales.coppelli
Posts: 34
Joined: Wed Aug 08, 2018 9:15 am

Re: Problem with MicroPython external C modules

Post by ales.coppelli » Mon Oct 26, 2020 2:28 pm

Thank you thank you very much Jimmo !
It works !

( I eliminated in mine [project/modules/example/micropython.mk] file the
added line ( USERMOD_DIR='the path of my modules/example directory ' )).

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Problem with MicroPython external C modules

Post by stijn » Wed Oct 28, 2020 7:53 am

@Jimmo do you know where the discrepancy between the .map and the .elf comes from?

Post Reply