Skip to content

Commit 3c82d8a

Browse files
committed
fixup examples based on default pin #define-s and also support them being undefined
1 parent 82c7a50 commit 3c82d8a

File tree

10 files changed

+101
-43
lines changed

10 files changed

+101
-43
lines changed

dma/channel_irq/channel_irq.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ void dma_handler() {
5252
}
5353

5454
int main() {
55+
#ifndef PICO_DEFAULT_LED_PIN
56+
#warning dma/channel_irq example requires a board with a regular LED
57+
#else
5558
// Set up a PIO state machine to serialise our bits
5659
uint offset = pio_add_program(pio0, &pio_serialiser_program);
5760
pio_serialiser_program_init(pio0, 0, offset, PICO_DEFAULT_LED_PIN, PIO_SERIAL_CLKDIV);
@@ -87,5 +90,5 @@ int main() {
8790
// time to sit and think about its early retirement -- maybe open a bakery?
8891
while (true)
8992
tight_loop_contents();
90-
93+
#endif
9194
}

dma/control_blocks/control_blocks.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const struct {uint32_t len; const char *data;} control_blocks[] = {
4747
};
4848

4949
int main() {
50+
#ifndef uart_default
51+
#warning dma/control_blocks example requires a UART
52+
#else
5053
stdio_init_all();
5154
puts("DMA control block example:");
5255

@@ -81,7 +84,7 @@ int main() {
8184

8285
c = dma_channel_get_default_config(data_chan);
8386
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
84-
channel_config_set_dreq(&c, DREQ_UART0_TX + 2 * PICO_DEFAULT_UART);
87+
channel_config_set_dreq(&c, DREQ_UART0_TX + 2 * uart_get_index(uart_default));
8588
// Trigger ctrl_chan when data_chan completes
8689
channel_config_set_chain_to(&c, ctrl_chan);
8790
// Raise the IRQ flag when 0 is written to a trigger register (end of chain):
@@ -90,7 +93,7 @@ int main() {
9093
dma_channel_configure(
9194
data_chan,
9295
&c,
93-
&(PICO_DEFAULT_UART ? uart1_hw : uart0_hw)->dr,
96+
&uart_get_hw(uart_default)->dr,
9497
NULL, // Initial read address and transfer count are unimportant;
9598
0, // the control channel will reprogram them each time.
9699
false // Don't start yet.
@@ -108,4 +111,5 @@ int main() {
108111
dma_hw->ints0 = 1u << data_chan;
109112

110113
puts("DMA finished.");
114+
#endif
111115
}

flash/nuke/nuke.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@
2626
#include "pico/bootrom.h"
2727

2828
int main() {
29-
flash_range_erase(0, PICO_FLASH_SIZE_BYTES);
29+
uint flash_size_bytes;
30+
#ifndef PICO_FLASH_SIZE_BYTES
31+
#warning PICO_FLASH_SIZE_BYTES not set, assuming 16M
32+
flash_size_bytes = 16 * 1024 * 1024;
33+
#else
34+
flash_size_bytes = PICO_FLASH_SIZE_BYTES;
35+
#endif
36+
flash_range_erase(0, flash_size_bytes);
3037
// Leave an eyecatcher pattern in the first page of flash so picotool can
3138
// more easily check the size:
3239
static const uint8_t eyecatcher[FLASH_PAGE_SIZE] = "NUKE";
3340
flash_range_program(0, eyecatcher, FLASH_PAGE_SIZE);
3441

42+
#ifdef PICO_DEFAULT_LED_PIN
3543
// Flash LED for success
3644
gpio_init(PICO_DEFAULT_LED_PIN);
3745
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
@@ -41,6 +49,7 @@ int main() {
4149
gpio_put(PICO_DEFAULT_LED_PIN, 0);
4250
sleep_ms(100);
4351
}
52+
#endif
4453

4554
// Pop back up as an MSD drive
4655
reset_usb_boot(0, 0);

gpio/dht_sensor/dht.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
#include "pico/stdlib.h"
1010
#include "hardware/gpio.h"
1111

12-
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
12+
#ifdef PICO_DEFAULT_LED_PIN
13+
#define LED_PIN PICO_DEFAULT_LED_PIN
14+
#endif
15+
1316
const uint DHT_PIN = 15;
1417
const uint MAX_TIMINGS = 85;
1518

@@ -22,9 +25,11 @@ void read_from_dht(dht_reading *result);
2225

2326
int main() {
2427
stdio_init_all();
25-
gpio_init(LED_PIN);
2628
gpio_init(DHT_PIN);
29+
#ifdef LED_PIN
30+
gpio_init(LED_PIN);
2731
gpio_set_dir(LED_PIN, GPIO_OUT);
32+
#endif
2833
while (1) {
2934
dht_reading reading;
3035
read_from_dht(&reading);
@@ -46,7 +51,9 @@ void read_from_dht(dht_reading *result) {
4651
sleep_ms(20);
4752
gpio_set_dir(DHT_PIN, GPIO_IN);
4853

54+
#ifdef LED_PIN
4955
gpio_put(LED_PIN, 1);
56+
#endif
5057
for (uint i = 0; i < MAX_TIMINGS; i++) {
5158
uint count = 0;
5259
while (gpio_get(DHT_PIN) == last) {
@@ -63,7 +70,9 @@ void read_from_dht(dht_reading *result) {
6370
j++;
6471
}
6572
}
73+
#ifdef LED_PIN
6674
gpio_put(LED_PIN, 0);
75+
#endif
6776

6877
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
6978
result->humidity = (float) ((data[0] << 8) + data[1]) / 10;

i2c/bus_scan/bus_scan.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <stdio.h>
2424
#include "pico/stdlib.h"
25+
#include "pico/binary_info.h"
2526
#include "hardware/i2c.h"
2627

2728
// I2C reserves some addresses for special purposes. We exclude these from the scan.
@@ -33,13 +34,18 @@ bool reserved_addr(uint8_t addr) {
3334
int main() {
3435
// Enable UART so we can print status output
3536
stdio_init_all();
36-
37-
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL)
38-
i2c_init(i2c0, 100 * 1000);
39-
gpio_set_function(4, GPIO_FUNC_I2C);
40-
gpio_set_function(5, GPIO_FUNC_I2C);
41-
gpio_pull_up(4);
42-
gpio_pull_up(5);
37+
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
38+
#warning i2c/bus_scane example requires a board with I2C pins
39+
puts("Default I2C pins were not defined");
40+
#else
41+
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
42+
i2c_init(i2c_default, 100 * 1000);
43+
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
44+
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
45+
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
46+
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
47+
// Make the I2C pins available to picotool
48+
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
4349

4450
printf("\nI2C Bus Scan\n");
4551
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
@@ -67,4 +73,5 @@ int main() {
6773
}
6874
printf("Done.\n");
6975
return 0;
76+
#endif
7077
}

i2c/lcd_1602_i2c/lcd_1602_i2c.c

+14-9
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const int LCD_BACKLIGHT = 0x08;
5858

5959
const int LCD_ENABLE_BIT = 0x04;
6060

61-
#define I2C_PORT i2c0
6261
// By default these LCD display drivers are on bus address 0x27
6362
static int addr = 0x27;
6463

@@ -71,7 +70,9 @@ static int addr = 0x27;
7170

7271
/* Quick helper function for single byte transfers */
7372
void i2c_write_byte(uint8_t val) {
74-
i2c_write_blocking(I2C_PORT, addr, &val, 1, false);
73+
#ifdef i2c_default
74+
i2c_write_blocking(i2c_default, addr, &val, 1, false);
75+
#endif
7576
}
7677

7778
void lcd_toggle_enable(uint8_t val) {
@@ -129,14 +130,17 @@ void lcd_init() {
129130
}
130131

131132
int main() {
132-
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL)
133-
i2c_init(I2C_PORT, 100 * 1000);
134-
gpio_set_function(4, GPIO_FUNC_I2C);
135-
gpio_set_function(5, GPIO_FUNC_I2C);
136-
gpio_pull_up(4);
137-
gpio_pull_up(5);
133+
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
134+
#warning i2c/bus_scane example requires a board with I2C pins
135+
#else
136+
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
137+
i2c_init(i2c_default, 100 * 1000);
138+
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
139+
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
140+
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
141+
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
138142
// Make the I2C pins available to picotool
139-
bi_decl( bi_2pins_with_func(4, 5, GPIO_FUNC_I2C));
143+
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
140144

141145
lcd_init();
142146

@@ -161,4 +165,5 @@ int main() {
161165
}
162166

163167
return 0;
168+
#endif
164169
}

i2c/mpu6050_i2c/mpu6050_i2c.c

+25-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdio.h>
88
#include <string.h>
99
#include "pico/stdlib.h"
10+
#include "pico/binary_info.h"
1011
#include "hardware/i2c.h"
1112

1213
/* Example code to talk to a MPU6050 MEMS accelerometer and gyroscope
@@ -23,22 +24,21 @@
2324
2425
Connections on Raspberry Pi Pico board, other boards may vary.
2526
26-
GPIO 4 (pin 6)-> SDA on MPU6050 board
27-
GPIO 5 (pin 7)-> SCL on MPU6050 board
27+
GPIO PICO_DEFAULT_I2C_SDA_PIN (On Pico this is 4 (pin 6)) -> SDA on MPU6050 board
28+
GPIO PICO_DEFAULT_I2C_SCK_PIN (On Pico this is 5 (pin 7)) -> SCL on MPU6050 board
2829
3.3v (pin 36) -> VCC on MPU6050 board
2930
GND (pin 38) -> GND on MPU6050 board
3031
*/
3132

3233
// By default these devices are on bus address 0x68
3334
static int addr = 0x68;
3435

35-
#define I2C_PORT i2c0
36-
36+
#ifdef i2c_default
3737
static void mpu6050_reset() {
3838
// Two byte reset. First byte register, second byte data
3939
// There are a load more options to set up the device in different ways that could be added here
4040
uint8_t buf[] = {0x6B, 0x00};
41-
i2c_write_blocking(I2C_PORT, addr, buf, 2, false);
41+
i2c_write_blocking(i2c_default, addr, buf, 2, false);
4242
}
4343

4444
static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
@@ -50,8 +50,8 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
5050

5151
// Start reading acceleration registers from register 0x3B for 6 bytes
5252
uint8_t val = 0x3B;
53-
i2c_write_blocking(I2C_PORT, addr, &val, 1, true); // true to keep master control of bus
54-
i2c_read_blocking(I2C_PORT, addr, buffer, 6, false);
53+
i2c_write_blocking(i2c_default, addr, &val, 1, true); // true to keep master control of bus
54+
i2c_read_blocking(i2c_default, addr, buffer, 6, false);
5555

5656
for (int i = 0; i < 3; i++) {
5757
accel[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);
@@ -60,8 +60,8 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
6060
// Now gyro data from reg 0x43 for 6 bytes
6161
// The register is auto incrementing on each read
6262
val = 0x43;
63-
i2c_write_blocking(I2C_PORT, addr, &val, 1, true);
64-
i2c_read_blocking(I2C_PORT, addr, buffer, 6, false); // False - finished with bus
63+
i2c_write_blocking(i2c_default, addr, &val, 1, true);
64+
i2c_read_blocking(i2c_default, addr, buffer, 6, false); // False - finished with bus
6565

6666
for (int i = 0; i < 3; i++) {
6767
gyro[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);;
@@ -70,23 +70,29 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
7070
// Now temperature from reg 0x41 for 2 bytes
7171
// The register is auto incrementing on each read
7272
val = 0x41;
73-
i2c_write_blocking(I2C_PORT, addr, &val, 1, true);
74-
i2c_read_blocking(I2C_PORT, addr, buffer, 2, false); // False - finished with bus
73+
i2c_write_blocking(i2c_default, addr, &val, 1, true);
74+
i2c_read_blocking(i2c_default, addr, buffer, 2, false); // False - finished with bus
7575

7676
*temp = buffer[0] << 8 | buffer[1];
7777
}
78+
#endif
7879

7980
int main() {
8081
stdio_init_all();
81-
82+
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
83+
#warning i2c/bus_scane example requires a board with I2C pins
84+
puts("Default I2C pins were not defined');
85+
#else
8286
printf("Hello, MPU6050! Reading raw data from registers...\n");
8387

84-
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL) running at 400kHz.
85-
i2c_init(I2C_PORT, 400 * 1000);
86-
gpio_set_function(4, GPIO_FUNC_I2C);
87-
gpio_set_function(5, GPIO_FUNC_I2C);
88-
gpio_pull_up(4);
89-
gpio_pull_up(5);
88+
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
89+
i2c_init(i2c_default, 400 * 1000);
90+
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
91+
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
92+
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
93+
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
94+
// Make the I2C pins available to picotool
95+
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
9096

9197
mpu6050_reset();
9298

@@ -106,5 +112,6 @@ int main() {
106112
sleep_ms(100);
107113
}
108114

115+
#endif
109116
return 0;
110117
}

picoboard/button/button.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "hardware/structs/ioqspi.h"
1212
#include "hardware/structs/sio.h"
1313

14-
// This example blinks the Picoboard LED when the BOOTSEL button is pressed.
14+
// This example blinks the Pico LED when the BOOTSEL button is pressed.
1515
//
1616
// Picoboard has a button attached to the flash CS pin, which the bootrom
1717
// checks, and jumps straight to the USB bootcode if the button is pressed
@@ -53,10 +53,14 @@ bool __no_inline_not_in_flash_func(get_bootsel_button)() {
5353
}
5454

5555
int main() {
56+
#ifndef PICO_DEFAULT_LED_PIN
57+
#warning picobooard/button example requires a board with a regular LED
58+
#else
5659
gpio_init(PICO_DEFAULT_LED_PIN);
5760
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
5861
while (true) {
59-
gpio_put(PICO_DEFAULT_LED_PIN, get_bootsel_button());
62+
gpio_put(PICO_DEFAULT_LED_PIN, get_bootsel_button() ^ PICO_DEFAULT_LED_PIN_INVERTED);
6063
sleep_ms(10);
6164
}
62-
}
65+
#endif
66+
}

pio/hello_pio/hello.c

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "hello.pio.h"
1111

1212
int main() {
13+
#ifndef PICO_DEFAULT_LED_PIN
14+
#warning pio/hello_pio example requires a board with a regular LED
15+
#else
1316
// Choose which PIO instance to use (there are two instances)
1417
PIO pio = pio0;
1518

@@ -35,4 +38,5 @@ int main() {
3538
pio_sm_put_blocking(pio, sm, 0);
3639
sleep_ms(500);
3740
}
41+
#endif
3842
}

pwm/led_fade/pwm_led_fade.c

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "hardware/irq.h"
1414
#include "hardware/pwm.h"
1515

16+
#ifdef PICO_DEFAULT_LED_PIN
1617
void on_pwm_wrap() {
1718
static int fade = 0;
1819
static bool going_up = true;
@@ -36,8 +37,12 @@ void on_pwm_wrap() {
3637
// Note this range matches with the wrap value
3738
pwm_set_gpio_level(PICO_DEFAULT_LED_PIN, fade * fade);
3839
}
40+
#endif
3941

4042
int main() {
43+
#ifndef PICO_DEFAULT_LED_PIN
44+
#warning pwm/led_fade example requires a board with a regular LED
45+
#else
4146
// Tell the LED pin that the PWM is in charge of its value.
4247
gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM);
4348
// Figure out which slice we just connected to the LED pin
@@ -62,4 +67,5 @@ int main() {
6267
// can twiddle our thumbs
6368
while (1)
6469
tight_loop_contents();
70+
#endif
6571
}

0 commit comments

Comments
 (0)