Skip to content

Commit 83f0eed

Browse files
lurchkilograham
authored andcommitted
Update some of the SPI examples to use PICO_DEFAULT_SPI and binary_info
1 parent a5363d1 commit 83f0eed

File tree

5 files changed

+92
-65
lines changed

5 files changed

+92
-65
lines changed

pio/spi/spi_flash.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
#include <stdio.h>
88

99
#include "pico/stdlib.h"
10+
#include "pico/binary_info.h"
1011
#include "pio_spi.h"
1112

1213
// This example uses PIO to erase, program and read back a SPI serial flash
1314
// memory.
1415

15-
#define PIN_MISO 16
16-
#define PIN_MOSI 17
17-
#define PIN_SCK 18
18-
#define PIN_CS 19
19-
2016
// ----------------------------------------------------------------------------
2117
// Generic serial flash code
2218

@@ -102,17 +98,24 @@ void printbuf(const uint8_t buf[FLASH_PAGE_SIZE]) {
10298

10399
int main() {
104100
stdio_init_all();
101+
#if !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)
102+
#warning pio/spi/spi_flash example requires a board with SPI pins
103+
puts("Default SPI pins were not defined");
104+
#else
105+
105106
puts("PIO SPI Example");
106107

107108
pio_spi_inst_t spi = {
108109
.pio = pio0,
109110
.sm = 0,
110-
.cs_pin = PIN_CS
111+
.cs_pin = PICO_DEFAULT_SPI_CSN_PIN
111112
};
112113

113-
gpio_init(PIN_CS);
114-
gpio_put(PIN_CS, 1);
115-
gpio_set_dir(PIN_CS, GPIO_OUT);
114+
gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
115+
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
116+
gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
117+
// Make the CS pin available to picotool
118+
bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS"));
116119

117120
uint offset = pio_add_program(spi.pio, &spi_cpha0_program);
118121
printf("Loaded program at %d\n", offset);
@@ -122,10 +125,12 @@ int main() {
122125
31.25f, // 1 MHz @ 125 clk_sys
123126
false, // CPHA = 0
124127
false, // CPOL = 0
125-
PIN_SCK,
126-
PIN_MOSI,
127-
PIN_MISO
128+
PICO_DEFAULT_SPI_SCK_PIN,
129+
PICO_DEFAULT_SPI_TX_PIN,
130+
PICO_DEFAULT_SPI_RX_PIN
128131
);
132+
// Make the SPI pins available to picotool
133+
bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_PIO0));
129134

130135
uint8_t page_buf[FLASH_PAGE_SIZE];
131136

@@ -152,4 +157,5 @@ int main() {
152157
printbuf(page_buf);
153158

154159
return 0;
160+
#endif
155161
}

spi/bme280_spi/bme280_spi.c

Lines changed: 26 additions & 18 deletions
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/spi.h"
1112

1213
/* Example code to talk to a bme280 humidity/temperature/pressure sensor.
@@ -36,12 +37,6 @@
3637
https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
3738
*/
3839

39-
#define PIN_MISO 16
40-
#define PIN_CS 17
41-
#define PIN_SCK 18
42-
#define PIN_MOSI 19
43-
44-
#define SPI_PORT spi0
4540
#define READ_BIT 0x80
4641

4742
int32_t t_fine;
@@ -110,24 +105,27 @@ uint32_t compensate_humidity(int32_t adc_H) {
110105
return (uint32_t) (v_x1_u32r >> 12);
111106
}
112107

108+
#ifdef PICO_DEFAULT_SPI_CSN_PIN
113109
static inline void cs_select() {
114110
asm volatile("nop \n nop \n nop");
115-
gpio_put(PIN_CS, 0); // Active low
111+
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0); // Active low
116112
asm volatile("nop \n nop \n nop");
117113
}
118114

119115
static inline void cs_deselect() {
120116
asm volatile("nop \n nop \n nop");
121-
gpio_put(PIN_CS, 1);
117+
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
122118
asm volatile("nop \n nop \n nop");
123119
}
120+
#endif
124121

122+
#if defined(spi_default) && defined(PICO_DEFAULT_SPI_CSN_PIN)
125123
static void write_register(uint8_t reg, uint8_t data) {
126124
uint8_t buf[2];
127125
buf[0] = reg & 0x7f; // remove read bit as this is a write
128126
buf[1] = data;
129127
cs_select();
130-
spi_write_blocking(SPI_PORT, buf, 2);
128+
spi_write_blocking(spi_default, buf, 2);
131129
cs_deselect();
132130
sleep_ms(10);
133131
}
@@ -138,9 +136,9 @@ static void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) {
138136
// so we don't need to keep sending the register we want, just the first.
139137
reg |= READ_BIT;
140138
cs_select();
141-
spi_write_blocking(SPI_PORT, &reg, 1);
139+
spi_write_blocking(spi_default, &reg, 1);
142140
sleep_ms(10);
143-
spi_read_blocking(SPI_PORT, 0, buf, len);
141+
spi_read_blocking(spi_default, 0, buf, len);
144142
cs_deselect();
145143
sleep_ms(10);
146144
}
@@ -184,22 +182,31 @@ static void bme280_read_raw(int32_t *humidity, int32_t *pressure, int32_t *tempe
184182
*temperature = ((uint32_t) buffer[3] << 12) | ((uint32_t) buffer[4] << 4) | (buffer[5] >> 4);
185183
*humidity = (uint32_t) buffer[6] << 8 | buffer[7];
186184
}
185+
#endif
187186

188187
int main() {
189188
stdio_init_all();
189+
#if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)
190+
#warning spi/bme280_spi example requires a board with SPI pins
191+
puts("Default SPI pins were not defined");
192+
#else
190193

191194
printf("Hello, bme280! Reading raw data from registers via SPI...\n");
192195

193196
// This example will use SPI0 at 0.5MHz.
194-
spi_init(SPI_PORT, 500 * 1000);
195-
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
196-
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
197-
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
197+
spi_init(spi_default, 500 * 1000);
198+
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
199+
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
200+
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
201+
// Make the SPI pins available to picotool
202+
bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI));
198203

199204
// Chip select is active-low, so we'll initialise it to a driven-high state
200-
gpio_init(PIN_CS);
201-
gpio_set_dir(PIN_CS, GPIO_OUT);
202-
gpio_put(PIN_CS, 1);
205+
gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
206+
gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
207+
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
208+
// Make the CS pin available to picotool
209+
bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS"));
203210

204211
// See if SPI is working - interrograte the device for its I2C ID number, should be 0x60
205212
uint8_t id;
@@ -230,4 +237,5 @@ int main() {
230237
}
231238

232239
return 0;
240+
#endif
233241
}

spi/mpu9250_spi/mpu9250_spi.c

Lines changed: 5 additions & 0 deletions
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/spi.h"
1112

1213
/* Example code to talk to a MPU9250 MEMS accelerometer and gyroscope.
@@ -117,11 +118,15 @@ int main() {
117118
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
118119
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
119120
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
121+
// Make the SPI pins available to picotool
122+
bi_decl(bi_3pins_with_func(PIN_MISO, PIN_MOSI, PIN_SCK, GPIO_FUNC_SPI));
120123

121124
// Chip select is active-low, so we'll initialise it to a driven-high state
122125
gpio_init(PIN_CS);
123126
gpio_set_dir(PIN_CS, GPIO_OUT);
124127
gpio_put(PIN_CS, 1);
128+
// Make the CS pin available to picotool
129+
bi_decl(bi_1pin_with_name(PIN_CS, "CS"));
125130

126131
mpu9250_reset();
127132

spi/spi_dma/spi_dma.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,39 @@
99
#include <stdio.h>
1010
#include <stdlib.h>
1111
#include "pico/stdlib.h"
12+
#include "pico/binary_info.h"
1213
#include "hardware/spi.h"
1314
#include "hardware/dma.h"
1415

15-
#define PIN_MISO 16
16-
#define PIN_CS 17
17-
#define PIN_SCK 18
18-
#define PIN_MOSI 19
19-
20-
#define SPI_INST spi0
21-
2216
#define TEST_SIZE 1024
2317

2418
int main() {
2519
// Enable UART so we can print status output
2620
stdio_init_all();
21+
#if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)
22+
#warning spi/spi_dma example requires a board with SPI pins
23+
puts("Default SPI pins were not defined");
24+
#else
2725

2826
printf("SPI DMA example\n");
2927

3028
// Enable SPI at 1 MHz and connect to GPIOs
31-
spi_init(SPI_INST, 1000 * 1000);
32-
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
33-
gpio_init(PIN_CS);
34-
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
35-
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
29+
spi_init(spi_default, 1000 * 1000);
30+
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
31+
gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
32+
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
33+
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
34+
// Make the SPI pins available to picotool
35+
bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI));
36+
// Make the CS pin available to picotool
37+
bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS"));
3638

3739
// Grab some unused dma channels
3840
const uint dma_tx = dma_claim_unused_channel(true);
3941
const uint dma_rx = dma_claim_unused_channel(true);
4042

4143
// Force loopback for testing (I don't have an SPI device handy)
42-
hw_set_bits(&spi_get_hw(SPI_INST)->cr1, SPI_SSPCR1_LBM_BITS);
44+
hw_set_bits(&spi_get_hw(spi_default)->cr1, SPI_SSPCR1_LBM_BITS);
4345

4446
static uint8_t txbuf[TEST_SIZE];
4547
static uint8_t rxbuf[TEST_SIZE];
@@ -54,9 +56,9 @@ int main() {
5456
printf("Configure TX DMA\n");
5557
dma_channel_config c = dma_channel_get_default_config(dma_tx);
5658
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
57-
channel_config_set_dreq(&c, spi_get_index(SPI_INST) ? DREQ_SPI1_TX : DREQ_SPI0_TX);
59+
channel_config_set_dreq(&c, spi_get_index(spi_default) ? DREQ_SPI1_TX : DREQ_SPI0_TX);
5860
dma_channel_configure(dma_tx, &c,
59-
&spi_get_hw(SPI_INST)->dr, // write address
61+
&spi_get_hw(spi_default)->dr, // write address
6062
txbuf, // read address
6163
TEST_SIZE, // element count (each element is of size transfer_data_size)
6264
false); // don't start yet
@@ -68,12 +70,12 @@ int main() {
6870
// address to increment (so data is written throughout the buffer)
6971
c = dma_channel_get_default_config(dma_rx);
7072
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
71-
channel_config_set_dreq(&c, spi_get_index(SPI_INST) ? DREQ_SPI1_RX : DREQ_SPI0_RX);
73+
channel_config_set_dreq(&c, spi_get_index(spi_default) ? DREQ_SPI1_RX : DREQ_SPI0_RX);
7274
channel_config_set_read_increment(&c, false);
7375
channel_config_set_write_increment(&c, true);
7476
dma_channel_configure(dma_rx, &c,
7577
rxbuf, // write address
76-
&spi_get_hw(SPI_INST)->dr, // read address
78+
&spi_get_hw(spi_default)->dr, // read address
7779
TEST_SIZE, // element count (each element is of size transfer_data_size)
7880
false); // don't start yet
7981

@@ -100,4 +102,5 @@ int main() {
100102
dma_channel_unclaim(dma_tx);
101103
dma_channel_unclaim(dma_rx);
102104
return 0;
105+
#endif
103106
}

spi/spi_flash/spi_flash.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <stdio.h>
1010
#include "pico/stdlib.h"
11+
#include "pico/binary_info.h"
1112
#include "hardware/spi.h"
1213

1314
#define FLASH_PAGE_SIZE 256
@@ -21,11 +22,6 @@
2122

2223
#define FLASH_STATUS_BUSY_MASK 0x01
2324

24-
#define PIN_MISO 4
25-
#define PIN_CS 5
26-
#define PIN_SCK 6
27-
#define PIN_MOSI 7
28-
2925
static inline void cs_select(uint cs_pin) {
3026
asm volatile("nop \n nop \n nop"); // FIXME
3127
gpio_put(cs_pin, 0);
@@ -110,45 +106,54 @@ void printbuf(uint8_t buf[FLASH_PAGE_SIZE]) {
110106
int main() {
111107
// Enable UART so we can print status output
112108
stdio_init_all();
109+
#if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)
110+
#warning spi/spi_flash example requires a board with SPI pins
111+
puts("Default SPI pins were not defined");
112+
#else
113113

114114
printf("SPI flash example\n");
115115

116116
// Enable SPI 0 at 1 MHz and connect to GPIOs
117-
spi_init(spi0, 1000 * 1000);
118-
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
119-
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
120-
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
117+
spi_init(spi_default, 1000 * 1000);
118+
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
119+
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
120+
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
121+
// Make the SPI pins available to picotool
122+
bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI));
121123

122124
// Chip select is active-low, so we'll initialise it to a driven-high state
123-
gpio_init(PIN_CS);
124-
gpio_put(PIN_CS, 1);
125-
gpio_set_dir(PIN_CS, GPIO_OUT);
125+
gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
126+
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
127+
gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
128+
// Make the CS pin available to picotool
129+
bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS"));
126130

127131
printf("SPI initialised, let's goooooo\n");
128132

129133
uint8_t page_buf[FLASH_PAGE_SIZE];
130134

131135
const uint32_t target_addr = 0;
132136

133-
flash_sector_erase(spi0, PIN_CS, target_addr);
134-
flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE);
137+
flash_sector_erase(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr);
138+
flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf, FLASH_PAGE_SIZE);
135139

136140
printf("After erase:\n");
137141
printbuf(page_buf);
138142

139143
for (int i = 0; i < FLASH_PAGE_SIZE; ++i)
140144
page_buf[i] = i;
141-
flash_page_program(spi0, PIN_CS, target_addr, page_buf);
142-
flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE);
145+
flash_page_program(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf);
146+
flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf, FLASH_PAGE_SIZE);
143147

144148
printf("After program:\n");
145149
printbuf(page_buf);
146150

147-
flash_sector_erase(spi0, PIN_CS, target_addr);
148-
flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE);
151+
flash_sector_erase(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr);
152+
flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf, FLASH_PAGE_SIZE);
149153

150154
printf("Erase again:\n");
151155
printbuf(page_buf);
152156

153157
return 0;
158+
#endif
154159
}

0 commit comments

Comments
 (0)