How to compile native C extension (that uses pico sdk) into .mpy file

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
MightyNate
Posts: 1
Joined: Fri Apr 09, 2021 12:25 pm

How to compile native C extension (that uses pico sdk) into .mpy file

Post by MightyNate » Fri Apr 09, 2021 12:29 pm

Greetings,

I'm writing a C extension for micropython on the Pico.
The goal is to be able to interface a certain sensor from micropython.
The sensor API is written in C.
As described in the micropython documentation https://docs.micropython.org/en/latest/ ... ython.html,
there are two ways to do this.
The first is to write an external C module and recompile it into the micropython firmware.
The second is to dynamically link native machine code in .mpy files
I decided for the second approach, since it is more user friendly in the end.
The problem I'm facing, is that the C module requires the pico-sdk, but to compile the module into a .mpy file I need to write a Makefile.
That is not really workig because to build pico-sdk applications you need to use cmake.
So here is my question:
Is there a way to use the pico-sdk in a Makefile? (other than manually including every single dependency)
Could I for example use cmake to compile the pico-sdk dependecies into a static library which I can link when building the .mpy?
What other approaches could I try? (Merge the Makefile for the .mpy into the Makefile created by cmake, ...)

Thank you in advance for any advise on how to deal with this problem
Nathan

taPIQoLEHUMA
Posts: 15
Joined: Thu Mar 04, 2021 2:59 am

Re: How to compile native C extension (that uses pico sdk) into .mpy file

Post by taPIQoLEHUMA » Fri Apr 09, 2021 10:17 pm

I followed this: https://docs.micropython.org/en/latest/ ... tml#natmod
The tools needed some tweaks (at the time) to generate the proper code for ARMv6 thumb.
I didn't use a 'cmake' file, but I did build micropython first - so it knew where everything was before doing this.

Code: Select all

# makefile for Micropython factorial mpy 

# location of top-level MicroPython directory
#MPY_DIR = ../../..
MPY_DIR = ../micropython

# name of module
MOD = factorial

# source files (.c or .py)
SRC = factorial.c

# architecture of target processor
# None x86 x64 armv6 armv6m armv7m armv7em armv7emsp armv7emdp xtensa xtensawin
ARCH = armv6m

CFLAGS = -v

# get the rules for compiling and linking the module
include $(MPY_DIR)/py/dynruntime.mk
HTH.

Post Reply