Link C Based .mpy file to Pico SDK #13626
Unanswered
AshbyGeek
asked this question in
RP2040 / Pico
Replies: 1 comment 1 reply
-
Unfortunately dynamic native modules can't link against / reuse any C libraries like Pico SDK directly because every time a firmware is built they'll likely be in sightly different places in flash. The dynamic modules can only use functions exposed in dynruntime because micropython builds and exposes a lookup table the module can use to access the functions. I've made a dynamic C module for stm that uses stm SDK (HAL) functions by compiling and linking the Hal C files directly during the native module build. This works fine for me, basically meaning my module has its own copy of the functions, not reusing the copy in micropython. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to do some PWM audio with the Pi Pico, all the examples I've found do this through the C SDK and some of the functions required aren't available through micropython (In particular, they use the PWM IRQ with a callback to process the next sample). I really want to be able to use python though because I have still need to write the MQTT interface for managing the sound clip being played and don't want to have to do that through C.
So it looks like I have two options:
I like option number 2 because that way I can continue to use off-the-shelf firmware. Less future upgrade headache for me. So I tried to implement that, even though there's basically no examples that interface this with the pi pico C sdk. I hit a snag though, when I tried to link my compiled code to objects from the Pico SDK (after I compiled it) I got a LinkError:
LinkError: .../pico-sdk/src/rp2_common/hardware_sync/sync.c.obj:
.data.striped_spin_lock_num non-empty"`I found the file in question and sure enough theres a line at the top that is not allowed by the .mpy linker:
static uint8_t striped_spin_lock_num = PICO_SPINLOCK_ID_STRIPED_FIRST;
My question is this: am I doing this wrong or does this limitation honestly prevent me from referencing large parts of the Pico C SDK from a native .mpy module?
More details about my .mpy module compilation process in case it becomes pertinent to the question:
The example native modules all use a very simple Makefile, but I couldn't figure out how to get all the proper include and linker options set up with the makefile as written, so I got my code compiling (and tested) using the straight C sdk.For the record, my C code used https://github.com/rgrosset/pico-pwm-audio as a starting point. I need to be able to output the same audio sample to multiple devices, with the option to turn any single off, and I wanted to make it more of a library so my code has diverged significantly from his. You can follow his instructions for setup, and then I added
main.c
:and
pico-pwm-audio.h
:and modified
pico-pwm-audio.c
:Once that was compiling I borrowed .config.h file from native module example and tried to manually run the mpy_ld.py script on it:
python3 $PICO_SDK/tools/mpy_ld.py --arch armv6m --qstrs build/pico-pwm-audio.config.h -o build/pico-pwm-audio.native.mpy build/main.o pico-pwm-audio.c.obj
which failed with
because it needs to link against the various referenced Pi Pico SDK objects.
So I dug into the CMake cache and found the link.txt file for pico-pwm-audio.c and grabbed the list of files it links against so I could feed them to mpy_ld.py which failed with
Beta Was this translation helpful? Give feedback.
All reactions