GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

A new way to use Pico with C/C++

Fri Jul 16, 2021 7:40 pm

I am not sure how many people are as stubborn as me that I want to use Raspberry Pi Pico in a familiar working environment.
To be honest, I am not good at cmake, and for 99% of my working time (and the spare time when doing some coding), I am using those old IDEs for MCU development, for example, MDK and IAR.

Pico-sdk, for now, is written in GCC and the official support for clang is still hanging there. I don't say the current cmake build system isn't good, but arm GCC, for now, is kind of behind others, i.e. LLVM, armcc, armclang and IAR when dealing with Cortex-M processors. So I am eager to introduce some diversity: with some help from the pico-sdk team, I manage to create an MDK project template for pico-sdk and it has the following features:

- Using the latest Arm Compiler 6 (I am using AC6.16)
- Automatically convert the generated axf file to uf2 file after each compilation.
- Support Debug inside MDK with the help of debug-probe which turns one core of Pico into a CMSIS-DAP.
- Using the CMSIS from RTE
- Apache 2.0

If you happen to have MDK, or you want to try Arm Compiler 6, then I invite you to try my template and leave your thoughts here:

https://github.com/GorgonMeducer/Pico_Template

https://github.com/GorgonMeducer/Pico_Template/releases

By the way, please don't worry about the license too much, MDK provides an evaluation license that allows you to compile (and debug) code that is smaller than a certain size (if my memory is correct, it should be <=32K).

As a reference, here is the size for the Blinking LED example:

Program Size: Code=10606 RO-data=750 RW-data=256 ZI-data=39732

Enjoy.
Last edited by GorgonMeducer on Sun Sep 26, 2021 9:39 am, edited 4 times in total.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 8:07 am

Very interesting as I have been using uVision for years for STM32 and consider this IDE really nice.

What MDK version should I use. Tried on 5.32 and got many errors. Will update to 5.35.
Have not studied where the ARM Compiler 6 is embedded.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 8:11 am

Checked:

*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'

And got many errors like this:

Build started: Project: template
*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
<built-in>(1): warning: In file included from...
./wrapper\env_wrapper.h(157): warning: In file included from...
./wrapper/pico/platform.h(76): error: expected identifier or '('
static inline void __breakpoint(void) {
^
C:\Keil_v5\ARM\ARMCLANG\Bin\..\include\arm_compat.h(14): note: expanded from macro '__breakpoint'
#define __breakpoint(v) __asm__ __volatile__ ("bkpt %0" : : "i"(v) )

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 11:34 am

I am using MDK 5.35 with Arm Compiler 6.16. I will check the problem you have encountered.
Don't worry.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 11:38 am

bgolab wrote:
Sat Jul 17, 2021 8:11 am
Checked:

*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'

And got many errors like this:

Build started: Project: template
*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
<built-in>(1): warning: In file included from...
./wrapper\env_wrapper.h(157): warning: In file included from...
./wrapper/pico/platform.h(76): error: expected identifier or '('
static inline void __breakpoint(void) {
^
C:\Keil_v5\ARM\ARMCLANG\Bin\..\include\arm_compat.h(14): note: expanded from macro '__breakpoint'
#define __breakpoint(v) __asm__ __volatile__ ("bkpt %0" : : "i"(v) )
The problem comes with the CMSIS 5.7.0. There will be no problem if you use CMSIS 5.8.0 (recently released and comes with MDK 5.35).
Let me see how to improve this compatibility issue.

Stay tuned.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 11:39 am

Thanks a lot! Will update the mdk
I'll keep you updated.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 11:58 am

bgolab wrote:
Sat Jul 17, 2021 8:11 am
Checked:

*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'

And got many errors like this:

Build started: Project: template
*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
<built-in>(1): warning: In file included from...
./wrapper\env_wrapper.h(157): warning: In file included from...
./wrapper/pico/platform.h(76): error: expected identifier or '('
static inline void __breakpoint(void) {
^
C:\Keil_v5\ARM\ARMCLANG\Bin\..\include\arm_compat.h(14): note: expanded from macro '__breakpoint'
#define __breakpoint(v) __asm__ __volatile__ ("bkpt %0" : : "i"(v) )
problem solved. Now you can use the old CMSIS 5.7.0 and also the latest CMSIS 5.8.0.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:00 pm

What part of your deliverables I shoud update?

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:01 pm

Either get the latest version from the github, or use the
https://github.com/GorgonMeducer/Pico_T ... tag/v0.9.1

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:03 pm

Thanks for the quick help. Will do.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:05 pm

bgolab wrote:
Sat Jul 17, 2021 12:00 pm
What part of your deliverables I shoud update?
Give me 5mins, I will create a release.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:06 pm

No rush.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:12 pm

bgolab wrote:
Sat Jul 17, 2021 12:03 pm
Thanks for the quick help. Will do.
Hi bro,

It looks like there is a typo in CMSIS 5.8.0:

__CM_CMSIS_VERSION_SUB

It should be 5 rather than 4. I will report this. But this bug leads to a problem, I cannot use this macro to do a conditional compilation.

It is sad...

Let me think about how to fix this.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:16 pm

Yeah. Replicating something on different computer is a real pain. There is so many dependencies. We live in a really complex world.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:32 pm

bgolab wrote:
Sat Jul 17, 2021 12:06 pm
No rush.
Done, bro

https://github.com/GorgonMeducer/Pico_T ... tag/v0.9.1

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:34 pm

bgolab wrote:
Sat Jul 17, 2021 12:16 pm
Yeah. Replicating something on different computer is a real pain. There is so many dependencies. We live in a really complex world.
True! the tinyUSB submodule inside the pico-sdk takes you 1.3GB space... and for most of the time, no use really use it.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:55 pm

bgolab wrote:
Sat Jul 17, 2021 12:00 pm
What part of your deliverables I shoud update?
In general, if you want to keep it simple, copy the wrapper and override the old one should work.

https://github.com/GorgonMeducer/Pico_T ... dk/wrapper

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 12:59 pm

MDK 5.32 definitely fails also with the new template (0.9.1).

Build started: Project: template
*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
compiling platform.c...
../../pico-sdk/src/rp2_common/hardware_gpio/gpio.c(8): warning: In file included from...
./wrapper\hardware/sync.h(144): error: definition of builtin function '__builtin_arm_dmb'
__force_inline static void __dmb(void) {

Can provide full log if needed.


I am trying to download the MDK 5.35 but working from a hotel with really poor Internet connection.
-------------------------------------------
An update:
MDK 5.35 works!
Build started: Project: template
*** Using Compiler 'V6.16', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
compiling platform.c...
compiling watchdog.c...
compiling gpio.c...
compiling multicore.c...
compiling startup_RP2040.c...
compiling xosc.c...
compiling system_RP2040.c...
compiling pll.c...
compiling clocks.c...
compiling irq.c...
compiling sync.c...
assembling irq_handler_chain.S...
compiling lock_core.c...
compiling critical_section.c...
compiling mutex.c...
compiling claim.c...
compiling timer.c...
compiling timeout_helper.c...
compiling sem.c...
compiling queue.c...
compiling pheap.c...
compiling time.c...
compiling datetime.c...
compiling stdlib.c...
compiling uart.c...
compiling runtime.c...
compiling i2c.c...
compiling spi.c...
compiling env_wrapper.c...
compiling main.c...
assembling compile_time_choice.S...
linking...
Program Size: Code=10566 RO-data=882 RW-data=256 ZI-data=39728
After Build - User command #1: .\axf2uf2.bat
C:\Users\abg015\Desktop\pico-keil\Pico_Template-0.9.1\project\mdk>"..\..\tool\elf2uf2.exe" .\Objects\template.axf .\template.uf2
".\Objects\template.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:01:18

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 2:16 pm

bgolab wrote:
Sat Jul 17, 2021 12:59 pm
MDK 5.32 definitely fails also with the new template (0.9.1).

Build started: Project: template
*** Using Compiler 'V6.15', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
compiling platform.c...
../../pico-sdk/src/rp2_common/hardware_gpio/gpio.c(8): warning: In file included from...
./wrapper\hardware/sync.h(144): error: definition of builtin function '__builtin_arm_dmb'
__force_inline static void __dmb(void) {

Can provide full log if needed.


I am trying to download the MDK 5.35 but working from a hotel with really poor Internet connection.
-------------------------------------------
An update:
MDK 5.35 works!
Build started: Project: template
*** Using Compiler 'V6.16', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
compiling platform.c...
compiling watchdog.c...
compiling gpio.c...
compiling multicore.c...
compiling startup_RP2040.c...
compiling xosc.c...
compiling system_RP2040.c...
compiling pll.c...
compiling clocks.c...
compiling irq.c...
compiling sync.c...
assembling irq_handler_chain.S...
compiling lock_core.c...
compiling critical_section.c...
compiling mutex.c...
compiling claim.c...
compiling timer.c...
compiling timeout_helper.c...
compiling sem.c...
compiling queue.c...
compiling pheap.c...
compiling time.c...
compiling datetime.c...
compiling stdlib.c...
compiling uart.c...
compiling runtime.c...
compiling i2c.c...
compiling spi.c...
compiling env_wrapper.c...
compiling main.c...
assembling compile_time_choice.S...
linking...
Program Size: Code=10566 RO-data=882 RW-data=256 ZI-data=39728
After Build - User command #1: .\axf2uf2.bat
C:\Users\abg015\Desktop\pico-keil\Pico_Template-0.9.1\project\mdk>"..\..\tool\elf2uf2.exe" .\Objects\template.axf .\template.uf2
".\Objects\template.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:01:18
Congrats.

Let me try Arm Compiler 6.15 and reproduce the problem you encountered. This might help others.

-------
Update: I can reproduce the same problem. It comes with Arm Compiler 6.15. I should fix it as most people are still using 6.15.

Update: Problem Fixed.
https://github.com/GorgonMeducer/Pico_T ... tag/v0.9.2

Copy the wrapper folder and override your old version could fix the problem.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 2:53 pm

I won't revert the mdk to 5.32, at least now.

Will try to compile bigger project.

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 3:13 pm

bgolab wrote:
Sat Jul 17, 2021 2:53 pm
I won't revert the mdk to 5.32, at least now.

Will try to compile bigger project.
Yeah, I have already tried a bigger one. My experience told me:
a. don't forget to check the size of Stack and Heap
b. if any peripheral drivers are missing, add them and don't forget to add the path.

If you have any problem which you believe it is about the environment, please raise an issue in the Pico_Template repo.

Enjoy.

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 3:48 pm

Just trying to make my coolest project build (~100kB of source code).


Thinking about useful features: did you think how to build code to run in from SRAM?

When doing this for STM32 I simply used to set IROM/IRAM to put the code into the SRAM range and of course I needed to set VTOR to relocate the vector table (if interrupts were used).

bgolab
Posts: 344
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 4:12 pm

I was missing: adc.h, bootrom.h - added paths to include. No issues.

I was also missing 'getchar_timeout' so added the stdio.c (from the rp2_common folder - not sure if this was the right directory because two were to choose) to the sdk c file collection and got some errors:

Build started: Project: template
*** Using Compiler 'V6.16', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): error: expected parameter declarator
int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
^
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): error: expected ')'
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): note: to match this '('
int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
^
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): error: expected function body after function declarator
int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
^
./wrapper\pico/platform.h(173): note: expanded from macro 'WRAPPER_FUNC'
#define WRAPPER_FUNC(x) __wrap_ ## x
^
<scratch space>(66): note: expanded from here
__wrap_printf
^
3 errors generated.
compiling stdio.c...
".\Objects\template.axf" - 3 Error(s), 0 Warning(s).
Target not created.
Build Time Elapsed: 00:00:11



------------
It won't an easy process to sort all issues out;)

GorgonMeducer
Posts: 55
Joined: Fri Jul 16, 2021 7:22 pm

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 4:47 pm

bgolab wrote:
Sat Jul 17, 2021 4:12 pm
I was missing: adc.h, bootrom.h - added paths to include. No issues.

I was also missing 'getchar_timeout' so added the stdio.c (from the rp2_common folder - not sure if this was the right directory because two were to choose) to the sdk c file collection and got some errors:

Build started: Project: template
*** Using Compiler 'V6.16', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin'
Build target 'AC6'
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): error: expected parameter declarator
int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
^
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): error: expected ')'
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): note: to match this '('
int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
^
../../pico-sdk/src/rp2_common/pico_stdio/stdio.c(237): error: expected function body after function declarator
int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
^
./wrapper\pico/platform.h(173): note: expanded from macro 'WRAPPER_FUNC'
#define WRAPPER_FUNC(x) __wrap_ ## x
^
<scratch space>(66): note: expanded from here
__wrap_printf
^
3 errors generated.
compiling stdio.c...
".\Objects\template.axf" - 3 Error(s), 0 Warning(s).
Target not created.
Build Time Elapsed: 00:00:11



------------
It won't an easy process to sort all issues out;)
Arm Compiler 6 and MDK has a different strategy to handle stdio functions, such as printf, scanf, stdin and stdout. Pico-SDK is using gcc way to handle these, which doesn't work in AC6 environment.

In fact, MDK provides a very simple solution, you don't have to add stdio.c or add any other header file. If you did, please remove them.

Please do not use the stdio solution in the pico-sdk but the RTE.

Open "Manage Run-Time Environment" window, expand Compiler->I/O and select STDIN if you want to use scanf and STDOUT if you want to use printf. In the variant list, select "User":

Recompile, then compiler report:

.\Objects\template.axf: Error: L6218E: Undefined symbol stdout_putchar (referred from retarget_io.o).

And this is the function you should implement to send char (to USART for example).

int stdout_putchar(int ch)
{
return ch;
}

It is the same for stdin_getchar, if you select the STDIN (User).

If you want to retarget printf (in arm compiler 6) to usart, you need to initialise a USART and use stdout_putchar function to send a char to that USART.

I hope this helps.

ejolson
Posts: 8240
Joined: Tue Mar 18, 2014 11:47 am

Re: A new way to use Pico with C/C++

Sat Jul 17, 2021 4:52 pm

GorgonMeducer wrote:
Sat Jul 17, 2021 12:34 pm
bgolab wrote:
Sat Jul 17, 2021 12:16 pm
Yeah. Replicating something on different computer is a real pain. There is so many dependencies. We live in a really complex world.
True! the tinyUSB submodule inside the pico-sdk takes you 1.3GB space... and for most of the time, no use really use it.
How could it take that much space when the Pico itself only has 256KB RAM and 2MB flash? How much does it take?

Return to “General”