Problem extending MicroPython in C on Pico

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
smoorby
Posts: 7
Joined: Thu Apr 01, 2021 9:29 am
Location: Basingstoke, UK

Problem extending MicroPython in C on Pico

Post by smoorby » Thu Apr 01, 2021 10:06 am

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)'.

sonnybalut
Posts: 11
Joined: Tue Dec 22, 2020 7:19 pm

Re: Problem extending MicroPython in C on Pico

Post by sonnybalut » Thu Apr 01, 2021 2:10 pm

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

smoorby
Posts: 7
Joined: Thu Apr 01, 2021 9:29 am
Location: Basingstoke, UK

Re: Problem extending MicroPython in C on Pico

Post by smoorby » Thu Apr 01, 2021 4:32 pm

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

sonnybalut
Posts: 11
Joined: Tue Dec 22, 2020 7:19 pm

Re: Problem extending MicroPython in C on Pico

Post by sonnybalut » Fri Apr 02, 2021 3:41 am

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

smoorby
Posts: 7
Joined: Thu Apr 01, 2021 9:29 am
Location: Basingstoke, UK

Re: Problem extending MicroPython in C on Pico

Post by smoorby » Fri Apr 02, 2021 8:56 pm

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 :D 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.

Post Reply