Skip to content

Commit 0f195c9

Browse files
committed
Improved Raspberry Pi Pico support
Fixes and improvements for RP2040 and added RP2350 support: * Made UART `printf` output optional * Made WiFi support optional * Added compile support for RP2350 * Added compile support for RP2350 in RISC-V mode * Re-wrote README * Fixed compile issues
1 parent 14dfeeb commit 0f195c9

5 files changed

Lines changed: 210 additions & 50 deletions

File tree

RPi-Pico/CMakeLists.txt

Lines changed: 109 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cmake_minimum_required(VERSION 3.13)
22

33
# Pull in Pico and FreeRTOS
4-
include(pico_sdk_import.cmake)
5-
include(pico_extras_import_optional.cmake)
4+
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
5+
#include(pico_extras_import_optional.cmake)
66
#include($ENV{FREERTOS_KERNEL_PATH}/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake)
77

88

@@ -17,6 +17,15 @@ set(CMAKE_CXX_STANDARD 17)
1717
# Initialize the SDK
1818
pico_sdk_init()
1919

20+
option(USE_UART "Output over UART instead of USB" OFF)
21+
option(USE_WIFI "Enable WiFi" OFF)
22+
option(WIFI_SSID "The WiFi SSID to connect to" "")
23+
option(WIFI_PASSWORD "The WiFi password" "")
24+
option(TEST_TCP_SERVER_IP "The TCP test server IP" "")
25+
26+
if (USE_WIFI AND NOT PICO_CYW43_SUPPORTED)
27+
message(FATAL_ERROR "You can only set USE_WIFI when a PICO_BOARD with wifi is used")
28+
endif()
2029

2130
### Global Include Path
2231
include_directories(config)
@@ -32,9 +41,10 @@ pico_sdk_init()
3241

3342

3443
### wolfSSL/wolfCrypt library
35-
file(GLOB_RECURSE WOLFSSL_SRC
44+
file(GLOB WOLFSSL_SRC
3645
"${WOLFSSL_ROOT}/src/*.c"
3746
"${WOLFSSL_ROOT}/wolfcrypt/src/*.c"
47+
"${WOLFSSL_ROOT}/wolfcrypt/src/port/rpi_pico/*"
3848
)
3949
list(REMOVE_ITEM WOLFSSL_SRC EXCLUDE REGEX
4050
"${WOLFSSL_ROOT}/src/bio.c"
@@ -56,25 +66,48 @@ pico_sdk_init()
5666
target_compile_definitions(wolfssl PUBLIC
5767
WOLFSSL_USER_SETTINGS
5868
)
69+
if (${PICO_PLATFORM} STREQUAL "rp2350")
70+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM)
71+
elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv")
72+
add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32)
73+
else()
74+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM)
75+
endif()
76+
77+
target_link_libraries(wolfssl
78+
pico_stdlib
79+
pico_rand
80+
)
5981
### End of wolfSSL/wolfCrypt library
6082

6183

6284
### Test wolfCrypt algorithms
6385
add_executable(testwolfcrypt
6486
src/test_main.c
65-
src/blink.c
6687
${WOLFSSL_ROOT}/wolfcrypt/test/test.c
6788
)
6889

6990
target_link_libraries(testwolfcrypt
7091
wolfssl
7192
pico_stdlib
72-
pico_cyw43_arch_none
7393
pico_rand
7494
)
7595

76-
pico_enable_stdio_usb(testwolfcrypt 1)
77-
pico_enable_stdio_uart(testwolfcrypt 0)
96+
if (USE_UART)
97+
pico_enable_stdio_usb(testwolfcrypt 0)
98+
pico_enable_stdio_uart(testwolfcrypt 1)
99+
else()
100+
pico_enable_stdio_usb(testwolfcrypt 1)
101+
pico_enable_stdio_uart(testwolfcrypt 0)
102+
endif()
103+
104+
if (${PICO_PLATFORM} STREQUAL "rp2350")
105+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM)
106+
elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv")
107+
add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32)
108+
else()
109+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM)
110+
endif()
78111

79112
pico_add_extra_outputs(testwolfcrypt)
80113
### End of Test wolfCrypt algorithms
@@ -83,29 +116,42 @@ pico_sdk_init()
83116
### Benchmark wolfCrypt algorithms
84117
add_executable(benchmark
85118
src/bench_main.c
86-
src/blink.c
87119
${WOLFSSL_ROOT}/wolfcrypt/benchmark/benchmark.c
88120
)
89121

90122
target_link_libraries(benchmark
91123
wolfssl
92124
pico_stdlib
93-
pico_cyw43_arch_none
94125
pico_rand
95126
)
96127

97-
pico_enable_stdio_usb(benchmark 1)
98-
pico_enable_stdio_uart(benchmark 0)
128+
if (USE_UART)
129+
pico_enable_stdio_usb(benchmark 0)
130+
pico_enable_stdio_uart(benchmark 1)
131+
else()
132+
pico_enable_stdio_usb(benchmark 1)
133+
pico_enable_stdio_uart(benchmark 0)
134+
endif()
135+
136+
137+
if (${PICO_PLATFORM} STREQUAL "rp2350")
138+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM)
139+
elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv")
140+
add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32)
141+
else()
142+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM)
143+
endif()
99144

100145
pico_add_extra_outputs(benchmark)
101146
### End of Benchmark wolfCrypt algorithms
102147

103148

149+
if (USE_WIFI)
104150
### Wifi connection
105151
add_executable(Wifi
106152
src/blink.c
107153
src/wifi.c
108-
src/Wifi_main.c
154+
src/wifi_main.c
109155
)
110156

111157
target_compile_definitions(Wifi PRIVATE
@@ -124,14 +170,27 @@ pico_sdk_init()
124170
pico_async_context_poll
125171
)
126172
127-
128-
pico_enable_stdio_usb(Wifi 1)
129-
pico_enable_stdio_uart(Wifi 0)
173+
if (USE_UART)
174+
pico_enable_stdio_usb(Wifi 0)
175+
pico_enable_stdio_uart(Wifi 1)
176+
else()
177+
pico_enable_stdio_usb(Wifi 1)
178+
pico_enable_stdio_uart(Wifi 0)
179+
endif()
180+
181+
if (${PICO_PLATFORM} STREQUAL "rp2350")
182+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM)
183+
elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv")
184+
add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32)
185+
else()
186+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM)
187+
endif()
130188
131189
pico_add_extra_outputs(Wifi)
132190
### End of Wifi connection
191+
endif()
133192
134-
193+
if (USE_WIFI)
135194
### TCP Client
136195
add_executable(tcp_Client
137196
src/blink.c
@@ -157,14 +216,27 @@ pico_sdk_init()
157216
pico_async_context_poll
158217
)
159218
160-
161-
pico_enable_stdio_usb(tcp_Client 1)
162-
pico_enable_stdio_uart(tcp_Client 0)
219+
if (USE_UART)
220+
pico_enable_stdio_usb(tcp_Client 0)
221+
pico_enable_stdio_uart(tcp_Client 1)
222+
else()
223+
pico_enable_stdio_usb(tcp_Client 1)
224+
pico_enable_stdio_uart(tcp_Client 0)
225+
endif()
226+
227+
if (${PICO_PLATFORM} STREQUAL "rp2350")
228+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM)
229+
elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv")
230+
add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32)
231+
else()
232+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM)
233+
endif()
163234
164235
pico_add_extra_outputs(tcp_Client)
165236
### End of TCP Client
237+
endif()
166238
167-
239+
if (USE_WIFI)
168240
### TLS Client
169241
add_executable(tls_Client
170242
src/blink.c
@@ -191,8 +263,22 @@ pico_sdk_init()
191263
wolfssl
192264
)
193265
194-
pico_enable_stdio_usb(tls_Client 1)
195-
pico_enable_stdio_uart(tls_Client 0)
266+
if (USE_UART)
267+
pico_enable_stdio_usb(tls_Client 0)
268+
pico_enable_stdio_uart(tls_Client 1)
269+
else()
270+
pico_enable_stdio_usb(tls_Client 1)
271+
pico_enable_stdio_uart(tls_Client 0)
272+
endif()
273+
274+
if (${PICO_PLATFORM} STREQUAL "rp2350")
275+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM)
276+
elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv")
277+
add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32)
278+
else()
279+
add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM)
280+
endif()
196281
197282
pico_add_extra_outputs(tls_Client)
198-
### End of TLS Client
283+
### End of TLS Client
284+
endif()

RPi-Pico/README.md

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,89 @@
11
## Getting Started
22

3-
1. Put wolfSSL source files under this directory.
4-
RPi-Pico/wolfssl
3+
Details of the Pi Pico support in wolfSSL can be found in the
4+
`wolfcrypt/src/port/pi_pico/README.md`.
55

6-
2. Setup pico-sdk and set PICO_SDK_PATH
7-
export PICO_SDK_PATH=/your/pico-sdk/path
6+
This demonstration compiles several different utilities, including the wolfCrypt
7+
benchmark and test suite.
88

9-
3. cmake and make
10-
$ cd RPi-Pico
11-
$ mkdir build
12-
$ cd build
13-
$ cmake -DPICO_BOARD=pico_w ..
14-
$ make
9+
### Prerequisites
1510

16-
4. Output is to USB serial
11+
You of course need a Pi Pico based board. Any RP2040 / RP2350 based board should
12+
work, as long as it has a USB port to upload firmware to.
1713

18-
14+
You need to have the [Raspberry Pi Pico SDK GitHub repository](https://github.com/raspberrypi/pico-sdk)
15+
somewhere on your system. You also need the ARM compiler and CMake installed,
16+
in Debian / Ubuntu you can do this using:
17+
18+
```
19+
sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
20+
```
21+
22+
If you wish to use RISC-V with the RP2350 (ARM mode is default), you will need a
23+
`riscv32-unknown-elf-gcc` compiler. You can search for binaries for this, or
24+
compile the [RISC-V GNU Toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain)
25+
in multilib mode. You will also need to add symlinks from `riscv64-unknown-*` to
26+
`riscv32-unknown-elf-*` if you build the toolchain from source. This is because
27+
the multilib mode compiler is both 32bit and 64bit.
28+
29+
### 1. Set an export to the wolfSSL source directory.
30+
31+
```
32+
export WOLFSSL_ROOT=/path/to/wolfssl/source
33+
```
34+
35+
### 2. Setup pico-sdk and set `PICO_SDK_PATH`
36+
37+
```
38+
export PICO_SDK_PATH=/path/to/pico-sdk
39+
```
40+
41+
### 3. cmake and make
42+
43+
The following CMAKE options are available:
44+
45+
* `PICO_BOARD` - This should be set to `pico` for a Pi Pico, `pico_w` for a Pi Pico with WiFi or `pico2` for a Pi Pico 2. A full list of boards for this option can be found [here](https://github.com/raspberrypi/pico-sdk/tree/master/src/boards/include/boards), just ignore the `.h` at the end.
46+
* `USE_WIFI` - Build the tests that use WiFi, only works when `PICO_BOARD` defined has a CYW43 WiFi chip.
47+
* `USE_UART` - Output to UART instead of USB, for the Pi Debug Probe.
48+
* `WIFI_SSID` - The SSID to connect to (if `USE_WIFI` is set).
49+
* `WIFI_PASSWORD` - The password used for the WiFi network (if `USE_WIFI` is set).
50+
* `TEST_TCP_SERVER_IP` - The test server to connect to for the TCP client test (if `USE_WIFI` is set).
51+
52+
To use the RP2350 in RISC-V mode, add `-DPICO_PLATFORM=rp2350-riscv`.
53+
54+
```
55+
$ cd RPi-Pico
56+
$ mkdir build
57+
$ cd build
58+
$ cmake -DPICO_BOARD=pico_w ..
59+
$ make
60+
```
61+
62+
### 4. Upload to the Pico
63+
64+
Hold the boot button and plug the Pico into your computer, you can then
65+
drag/drop a `.uf2` to the Pico. It will stop becoming a USB mass storage device
66+
and run immediately once the upload is complete. Alternatively, you can use
67+
[picotool](https://github.com/raspberrypi/picotool) to upload a file:
68+
69+
```
70+
sudo picotool load benchmark.uf2
71+
sudo picotool reboot
72+
```
73+
74+
### 5. Serial output
75+
76+
If you have not set `USE_UART`, once rebooted the USB port will turn into an
77+
"Abstract Control Module" serial port. On Linux this will likely be
78+
`/dev/ttyACM0`, or a number higher than `0` if you already have one. On macOS
79+
this will be something like `/dev/cu.usbmodemXXXX`. The baud rate of this port
80+
is 115200.
81+
82+
In Linux, most repositories have `minicom`. Install this using your package
83+
manager and run:
84+
85+
```
86+
minicom -b 115200 -o -D /dev/ttyACM0
87+
```
88+
89+
If you need to exit at any time, it is CTRL-A followed by CTRL-X.

0 commit comments

Comments
 (0)