Skip to content

Commit eefec28

Browse files
authored
Merge pull request #3429 from cesanta/w55rp20
add W55RP20 example
2 parents 564c352 + e83e13d commit eefec28

File tree

8 files changed

+300
-0
lines changed

8 files changed

+300
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
include(pico-sdk/pico_sdk_init.cmake)
3+
4+
project(firmware)
5+
pico_sdk_init()
6+
7+
add_executable(firmware
8+
main.c
9+
mongoose.c
10+
)
11+
12+
target_include_directories(firmware PUBLIC
13+
.
14+
)
15+
16+
target_link_libraries(firmware hardware_pio pico_stdlib pico_rand)
17+
pico_add_extra_outputs(firmware) # create map/bin/hex file etc.
18+
19+
pico_enable_stdio_usb(firmware 1) # Route stdio
20+
pico_enable_stdio_uart(firmware 0) # to USB
21+
22+
# Mongoose build flags in mongoose_config.h
23+
24+
# Example build options
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
RM = rm -rf
2+
MKBUILD = test -d build || mkdir build
3+
ifeq ($(OS),Windows_NT)
4+
RM = cmd /C del /Q /F /S
5+
MKBUILD = if not exist build mkdir build
6+
endif
7+
8+
all example:
9+
true
10+
11+
build build/firmware.uf2: pico-sdk main.c
12+
$(MKBUILD)
13+
cd build && cmake -G "Unix Makefiles" .. && make
14+
15+
pico-sdk:
16+
git clone --depth 1 -b 2.1.0 https://github.com/raspberrypi/pico-sdk $@
17+
cd $@ && git submodule update --init
18+
19+
clean:
20+
$(RM) pico-sdk build
21+
22+
.PHONY: build
23+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) 2026 Cesanta Software Limited
2+
// All rights reserved
3+
4+
#include "pico/stdlib.h"
5+
#include "hardware/pio.h"
6+
7+
extern void mg_delayms(unsigned int ms);
8+
9+
#define SPIPIO pio0
10+
#define SPISM 0
11+
#define MOSIPIN 23
12+
#define MISOPIN 22
13+
#define CLKPIN 21
14+
#define CSPIN 20
15+
#define RSTPIN 25
16+
17+
// Try it on a bare Pico !
18+
// #define MOSIPIN 3
19+
// #define MISOPIN 4
20+
// #define CLKPIN 2
21+
// #define CSPIN 5
22+
23+
#define piospi_wrap_target 0
24+
#define piospi_wrap 3
25+
#define piospi_pio_version 0
26+
27+
static const uint16_t piospi_program_instructions[] = {
28+
// .wrap_target
29+
0xe027, // 0: set x, 7 side 0
30+
0x6101, // 1: out pins, 1 side 0 [1]
31+
0x5001, // 2: in pins, 1 side 1
32+
0x1041, // 3: jmp x--, 1 side 1
33+
// .wrap
34+
};
35+
36+
static const struct pio_program piospi_program = {
37+
.instructions = piospi_program_instructions,
38+
.length = 4,
39+
.origin = -1,
40+
.pio_version = 0,
41+
#if PICO_PIO_VERSION > 0
42+
.used_gpio_ranges = 0x0
43+
#endif
44+
};
45+
46+
static inline pio_sm_config piospi_program_get_default_config(uint offset) {
47+
pio_sm_config c = pio_get_default_sm_config();
48+
sm_config_set_wrap(&c, offset + piospi_wrap_target, offset + piospi_wrap);
49+
sm_config_set_sideset(&c, 1, false, false);
50+
return c;
51+
}
52+
53+
static inline pio_sm_config piospi_init(PIO pio, uint sm, uint addr, uint mosi, uint miso, uint clk) {
54+
pio_gpio_init(pio, mosi);
55+
pio_gpio_init(pio, miso);
56+
pio_gpio_init(pio, clk);
57+
pio_sm_set_pins_with_mask(pio, sm, 0, (1 << mosi) | (1 << clk));
58+
pio_sm_config c = piospi_program_get_default_config(addr);
59+
sm_config_set_out_pins(&c, mosi, 1);
60+
sm_config_set_in_pins(&c, miso);
61+
sm_config_set_set_pins(&c, mosi, 1);
62+
sm_config_set_sideset_pins(&c, clk);
63+
pio_sm_set_pindirs_with_mask(pio, sm, (1 << mosi) | (1 << clk), (1 << mosi) | (1 << clk));
64+
sm_config_set_out_shift(&c, false, true, 8); // pull bytes, MSB first, auto
65+
sm_config_set_in_shift(&c, false, true, 8); // push bytes, MSB first, auto
66+
#if PICO_RP2040
67+
sm_config_set_clkdiv(&c, 18); // Run at 133/1 = <33MHz (4x data rate)
68+
#else
69+
sm_config_set_clkdiv(&c, 20); // Run at 150/2 = <19MHz (4x data rate)
70+
#endif
71+
return c;
72+
}
73+
74+
75+
static pio_sm_config s_piospi_sm_config;
76+
static uint s_piospi_sm_addr;
77+
78+
uint8_t hwspecific_spi_txn(void *arg, uint8_t data) {
79+
(void) arg;
80+
pio_sm_put(SPIPIO, SPISM, data << 24);
81+
return pio_sm_get_blocking(SPIPIO, SPISM);
82+
}
83+
84+
void hwspecific_spi_init(void) {
85+
gpio_init(RSTPIN);
86+
gpio_set_dir(RSTPIN, GPIO_OUT);
87+
gpio_put(RSTPIN, 0);
88+
gpio_init(CSPIN);
89+
gpio_set_dir(CSPIN, GPIO_OUT);
90+
gpio_put(CSPIN, 1);
91+
s_piospi_sm_addr = pio_add_program(SPIPIO, &piospi_program);
92+
s_piospi_sm_config = piospi_init(SPIPIO, SPISM, s_piospi_sm_addr, MOSIPIN, MISOPIN, CLKPIN);
93+
mg_delayms(2);
94+
gpio_put(RSTPIN, 1);
95+
}
96+
97+
void hwspecific_spi_begin(void *arg) {
98+
pio_sm_init(SPIPIO, SPISM, s_piospi_sm_addr, &s_piospi_sm_config);
99+
pio_sm_set_enabled(SPIPIO, SPISM, true);
100+
gpio_put(CSPIN, 0);
101+
(void) arg;
102+
}
103+
104+
void hwspecific_spi_end(void *arg) {
105+
gpio_put(CSPIN, 1);
106+
pio_sm_set_enabled(SPIPIO, SPISM, false);
107+
(void) arg;
108+
}
109+
110+
111+
// Construct MAC address from the unique board ID
112+
#include "pico/unique_id.h"
113+
static inline void hal_ethernet_genmac(unsigned char *mac) {
114+
pico_unique_board_id_t board_id;
115+
pico_get_unique_board_id(&board_id);
116+
mac[0] = 2;
117+
memcpy(&mac[1], &board_id.id[3], 5);
118+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2025 Cesanta Software Limited
2+
// All rights reserved
3+
4+
#include "mongoose.h"
5+
#include "hal.h"
6+
7+
8+
static const struct mg_tcpip_spi spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
9+
10+
11+
int main(void) {
12+
// initialize stdio
13+
stdio_init_all();
14+
15+
hwspecific_spi_init();
16+
17+
struct mg_mgr mgr; // Initialise Mongoose event manager
18+
mg_mgr_init(&mgr); // and attach it to the interface
19+
mg_log_set(MG_LL_DEBUG); // Set log level
20+
21+
// Initialise Mongoose network stack
22+
// Either set use_dhcp or enter a static config.
23+
// For static configuration, specify IP/mask/GW in network byte order
24+
struct mg_tcpip_if mif = {
25+
.ip = 0,
26+
.driver = &mg_tcpip_driver_w5500,
27+
.driver_data = (void *) &spi
28+
};
29+
30+
hal_ethernet_genmac(mif.mac);
31+
mg_tcpip_init(&mgr, &mif);
32+
MG_INFO(("Init done, starting main loop"));
33+
34+
MG_INFO(("Initialising application..."));
35+
36+
MG_INFO(("Starting event loop"));
37+
for (;;) {
38+
mg_mgr_poll(&mgr, 0);
39+
}
40+
41+
return 0;
42+
}
43+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../mongoose.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../mongoose.h
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define MG_ARCH MG_ARCH_PICOSDK
2+
3+
#define MG_ENABLE_TCPIP 1
4+
#define MG_ENABLE_DRIVER_W5500 1
5+
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
6+
#define MG_ENABLE_PACKED_FS 1
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
2+
3+
# This can be dropped into an external project to help locate this SDK
4+
# It should be include()ed prior to project()
5+
6+
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
7+
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
8+
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
9+
endif ()
10+
11+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
12+
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
13+
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
14+
endif ()
15+
16+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
17+
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
18+
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
19+
endif ()
20+
21+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
22+
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
23+
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
24+
endif ()
25+
26+
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
27+
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
28+
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
29+
endif()
30+
31+
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
32+
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
33+
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
34+
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
35+
36+
if (NOT PICO_SDK_PATH)
37+
if (PICO_SDK_FETCH_FROM_GIT)
38+
include(FetchContent)
39+
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
40+
if (PICO_SDK_FETCH_FROM_GIT_PATH)
41+
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
42+
endif ()
43+
# GIT_SUBMODULES_RECURSE was added in 3.17
44+
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
45+
FetchContent_Declare(
46+
pico_sdk
47+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
48+
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
49+
GIT_SUBMODULES_RECURSE FALSE
50+
)
51+
else ()
52+
FetchContent_Declare(
53+
pico_sdk
54+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
55+
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
56+
)
57+
endif ()
58+
59+
if (NOT pico_sdk)
60+
message("Downloading Raspberry Pi Pico SDK")
61+
FetchContent_Populate(pico_sdk)
62+
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
63+
endif ()
64+
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
65+
else ()
66+
message(FATAL_ERROR
67+
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
68+
)
69+
endif ()
70+
endif ()
71+
72+
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
73+
if (NOT EXISTS ${PICO_SDK_PATH})
74+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
75+
endif ()
76+
77+
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
78+
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
79+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
80+
endif ()
81+
82+
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
83+
84+
include(${PICO_SDK_INIT_CMAKE_FILE})

0 commit comments

Comments
 (0)