Page 1 of 1
Problem extending MicroPython in C on Pico
Posted: Thu Apr 01, 2021 10:06 am
by smoorby
I'm trying to extend MicroPython with some C code. I've tried building .../examples/usercmodule but the build breaks. Changing the target to .../ports/stm32 and the build works ok so something wrong in the RP2 build tree.
I also tried using native code in an '.mpy' module, again starting with the example from the documentation, factorial.c. No trouble building but it just crashes when it runs, 'factorial.factorial(0)'.
Re: Problem extending MicroPython in C on Pico
Posted: Thu Apr 01, 2021 2:10 pm
by sonnybalut
Make sure you have the current micropython github.
for user c module, there's updated docs for rp2 in this link -->
https://docs.micropython.org/en/latest/ ... dules.html
for mpy check this link -->
https://github.com/micropython/micropyt ... VGP1qBGGVE
Thanks,
Sonny
Re: Problem extending MicroPython in C on Pico
Posted: Thu Apr 01, 2021 4:32 pm
by smoorby
Thank you Sonny, Got the mpy module working with the cortex-m0 change. Been bitten by that one before with an NXP processor containing an M3 and and M0

Just pulled from GIT today, still can't build the examples/usercmodule for RP2. Will look more closely later
Re: Problem extending MicroPython in C on Pico
Posted: Fri Apr 02, 2021 3:41 am
by sonnybalut
OK I just tried the usercmodule example and it works for me.
Steps I took:
1. Follow instruction from Chapter 1.3 on this link ->
https://datasheets.raspberrypi.org/pico ... on-sdk.pdf
2. When you get to the last step, instead of just typing make, type this instead -> make USER_C_MODULES=../../examples/usercmodule/micropython.cmake
3. Assumption - you're in the micropython/ports/rp2 directory
4. Make sure you type make clean if you encounter an error then repeat step 2
Thanks,
Sonny
Re: Problem extending MicroPython in C on Pico
Posted: Fri Apr 02, 2021 8:56 pm
by smoorby
Hi Sonny, that works for me too but it's not what the instructions say. They explicitly say to the directory containing all modules :-
"an extra make flag named USER_C_MODULES set to the directory containing all modules you want included (not to the module itself). For building the example modules which come with MicroPython, set USER_C_MODULES to the examples/usercmodule directory."
and the example command line :-
cd micropython/ports/rp2
make USER_C_MODULES=../../examples/usercmodule all
It's interesting the RP2 port is mentioned as "a little different" but then they show it the same.
"For a CMake-based port such as rp2, this will look a little different:"
Perhaps it's supposed to be different but the documentation doesn't reflect that? As mentioned previously it does work as documented for 'ports/stm32'. Anyway, I have workarounds for both methods of including C code

My preferred method is the '.mpy' as it's quick to change code whilst in development without re-flashing. I may build into the micropython image when stable.