Skip to content

Commit e934677

Browse files
authored
Add binary info examples of configurable binaries (#567)
1 parent deeb6d8 commit e934677

File tree

8 files changed

+226
-0
lines changed

8 files changed

+226
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ endif()
5959

6060
# Hardware-specific examples in subdirectories:
6161
add_subdirectory(adc)
62+
add_subdirectory(binary_info)
6263
add_subdirectory(bootloaders)
6364
add_subdirectory(clocks)
6465
add_subdirectory(cmake)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ App|Description
4040
[dma_capture](adc/dma_capture) | Use the DMA to capture many samples from the ADC.
4141
[read_vsys](adc/read_vsys) | Demonstrates how to read VSYS to get the voltage of the power supply.
4242

43+
### Binary Info
44+
45+
App|Description
46+
---|---
47+
[blink_any](binary_info/blink_any) | Uses `bi_ptr` variables to create a configurable blink binary - see the separate [README](binary_info/README.md) for more details
48+
[hello_anything](binary_info/hello_anything) | Uses `bi_ptr` variables to create a configurable hello_world binary - see the separate [README](binary_info/README.md) for more details
49+
4350
### Bootloaders (RP2350 Only)
4451
App|Description
4552
---|---

binary_info/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory_exclude_platforms(blink_any)
2+
add_subdirectory_exclude_platforms(hello_anything)

binary_info/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
These programs demonstrate use of `bi_ptr` variables, which can be configured in a binary post-compilation using the `picotool config` command.
2+
3+
You can view the configurable variables with
4+
```
5+
$ picotool blink_any.uf2
6+
File blink_any.uf2:
7+
8+
LED Configuration:
9+
LED_PIN = 25
10+
LED_TYPE = 0
11+
12+
$ picotool config hello_anything.uf2
13+
File hello_anything.uf2:
14+
15+
text = "Hello, world!"
16+
Enabled Interfaces:
17+
use_usb = 1
18+
use_uart = 1
19+
UART Configuration:
20+
uart_baud = 115200
21+
uart_rx = 1
22+
uart_tx = 0
23+
uart_num = 0
24+
```
25+
26+
For example, to blink the LED on pin 7 instead of 25 use
27+
```
28+
$ picotool config blink_any.uf2 -s LED_PIN 7
29+
File blink_any.uf2:
30+
31+
LED_PIN = 25
32+
setting LED_PIN -> 7
33+
```
34+
35+
Or to change the printed string use
36+
```
37+
$ picotool config hello_anything.uf2 -s text "Goodbye, world!"
38+
File hello_anything.uf2:
39+
40+
text = "Hello, world!"
41+
setting text -> "Goodbye, world!"
42+
```
43+
44+
The binaries can also be configured after being loaded onto the device with
45+
```
46+
$ picotool config
47+
text = "Hello, world!"
48+
Enabled Interfaces:
49+
use_usb = 1
50+
use_uart = 1
51+
UART Configuration:
52+
uart_baud = 115200
53+
uart_rx = 1
54+
uart_tx = 0
55+
uart_num = 0
56+
57+
$ picotool config -s use_uart 0
58+
use_uart = 1
59+
setting use_uart -> 0
60+
```

binary_info/blink_any/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
if (NOT PICO_CYW43_SUPPORTED)
2+
message("Only building blink_any for non W boards as PICO_CYW43_SUPPORTED is not set")
3+
endif()
4+
5+
add_executable(blink_any
6+
blink_any.c
7+
)
8+
9+
# pull in common dependencies
10+
target_link_libraries(blink_any pico_stdlib)
11+
12+
if (PICO_CYW43_SUPPORTED)
13+
target_link_libraries(blink_any pico_cyw43_arch_none)
14+
endif()
15+
16+
# create map/bin/hex file etc.
17+
pico_add_extra_outputs(blink_any)
18+
19+
# add url via pico_set_program_url
20+
example_auto_set_url(blink_any)

binary_info/blink_any/blink_any.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/stdlib.h"
8+
#include "pico/binary_info.h"
9+
10+
#ifdef CYW43_WL_GPIO_LED_PIN
11+
#include "pico/cyw43_arch.h"
12+
#endif
13+
14+
// Set an LED_TYPE variable - 0 is default, 1 is connected to WIFI chip
15+
// Note that LED_TYPE == 1 is only supported when initially compiled for
16+
// a board with PICO_CYW43_SUPPORTED (eg pico_w), else the required
17+
// libraries won't be present
18+
bi_decl(bi_program_feature_group(0x1111, 0, "LED Configuration"));
19+
#if defined(PICO_DEFAULT_LED_PIN)
20+
// the tag and id are not important as picotool filters based on the
21+
// variable name, so just set them to 0
22+
bi_decl(bi_ptr_int32(0x1111, 0, LED_TYPE, 0));
23+
bi_decl(bi_ptr_int32(0x1111, 0, LED_PIN, PICO_DEFAULT_LED_PIN));
24+
#elif defined(CYW43_WL_GPIO_LED_PIN)
25+
bi_decl(bi_ptr_int32(0x1111, 0, LED_TYPE, 1));
26+
bi_decl(bi_ptr_int32(0x1111, 0, LED_PIN, CYW43_WL_GPIO_LED_PIN));
27+
#else
28+
bi_decl(bi_ptr_int32(0x1111, 0, LED_TYPE, 0));
29+
bi_decl(bi_ptr_int32(0x1111, 0, LED_PIN, 25));
30+
#endif
31+
32+
#ifndef LED_DELAY_MS
33+
#define LED_DELAY_MS 250
34+
#endif
35+
36+
// Perform initialisation
37+
int pico_led_init(void) {
38+
if (LED_TYPE == 0) {
39+
// A device like Pico that uses a GPIO for the LED so we can
40+
// use normal GPIO functionality to turn the led on and off
41+
gpio_init(LED_PIN);
42+
gpio_set_dir(LED_PIN, GPIO_OUT);
43+
return PICO_OK;
44+
#ifdef CYW43_WL_GPIO_LED_PIN
45+
} else if (LED_TYPE == 1) {
46+
// For Pico W devices we need to initialise the driver etc
47+
return cyw43_arch_init();
48+
#endif
49+
} else {
50+
return PICO_ERROR_INVALID_DATA;
51+
}
52+
}
53+
54+
// Turn the led on or off
55+
void pico_set_led(bool led_on) {
56+
if (LED_TYPE == 0) {
57+
// Just set the GPIO on or off
58+
gpio_put(LED_PIN, led_on);
59+
#ifdef CYW43_WL_GPIO_LED_PIN
60+
} else if (LED_TYPE == 1) {
61+
// Ask the wifi "driver" to set the GPIO on or off
62+
cyw43_arch_gpio_put(LED_PIN, led_on);
63+
#endif
64+
}
65+
}
66+
67+
int main() {
68+
int rc = pico_led_init();
69+
hard_assert(rc == PICO_OK);
70+
while (true) {
71+
pico_set_led(true);
72+
sleep_ms(LED_DELAY_MS);
73+
pico_set_led(false);
74+
sleep_ms(LED_DELAY_MS);
75+
}
76+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
if (TARGET tinyusb_device)
2+
add_executable(hello_anything
3+
hello_anything.c
4+
)
5+
6+
# pull in common dependencies
7+
target_link_libraries(hello_anything pico_stdlib)
8+
9+
# enable usb and uart output
10+
pico_enable_stdio_usb(hello_anything 1)
11+
pico_enable_stdio_uart(hello_anything 1)
12+
13+
# create map/bin/hex/uf2 file etc.
14+
pico_add_extra_outputs(hello_anything)
15+
16+
# add url via pico_set_program_url
17+
example_auto_set_url(hello_anything)
18+
elseif(PICO_ON_DEVICE)
19+
message("Skipping hello_anything because TinyUSB submodule is not initialized in the SDK")
20+
endif()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include "pico/stdlib.h"
9+
#include "pico/binary_info.h"
10+
#include "hardware/uart.h"
11+
12+
int main() {
13+
// create feature groups to group configuration settings
14+
// these will also show up in picotool info, not just picotool config
15+
bi_decl(bi_program_feature_group(0x1111, 0, "UART Configuration"));
16+
bi_decl(bi_program_feature_group(0x1111, 1, "Enabled Interfaces"));
17+
// stdio_uart configuration and initialisation
18+
bi_decl(bi_ptr_int32(0x1111, 1, use_uart, 1));
19+
bi_decl(bi_ptr_int32(0x1111, 0, uart_num, 0));
20+
bi_decl(bi_ptr_int32(0x1111, 0, uart_tx, 0));
21+
bi_decl(bi_ptr_int32(0x1111, 0, uart_rx, 1));
22+
bi_decl(bi_ptr_int32(0x1111, 0, uart_baud, 115200));
23+
if (use_uart) {
24+
stdio_uart_init_full(UART_INSTANCE(uart_num), uart_baud, uart_tx, uart_rx);
25+
}
26+
27+
// stdio_usb initialisation
28+
bi_decl(bi_ptr_int32(0x1111, 1, use_usb, 1));
29+
if (use_usb) {
30+
stdio_usb_init();
31+
}
32+
33+
// default printed string
34+
bi_decl(bi_ptr_string(0, 0, text, "Hello, world!", 256));
35+
36+
while (true) {
37+
printf("%s\n", text);
38+
sleep_ms(1000);
39+
}
40+
}

0 commit comments

Comments
 (0)