diff --git a/pio/spi/spi_flash.c b/pio/spi/spi_flash.c index bc9192e..1f2fdec 100644 --- a/pio/spi/spi_flash.c +++ b/pio/spi/spi_flash.c @@ -7,16 +7,12 @@ #include #include "pico/stdlib.h" +#include "pico/binary_info.h" #include "pio_spi.h" // This example uses PIO to erase, program and read back a SPI serial flash // memory. -#define PIN_MISO 16 -#define PIN_MOSI 17 -#define PIN_SCK 18 -#define PIN_CS 19 - // ---------------------------------------------------------------------------- // Generic serial flash code @@ -102,17 +98,24 @@ void printbuf(const uint8_t buf[FLASH_PAGE_SIZE]) { int main() { stdio_init_all(); +#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) +#warning pio/spi/spi_flash example requires a board with SPI pins + puts("Default SPI pins were not defined"); +#else + puts("PIO SPI Example"); pio_spi_inst_t spi = { .pio = pio0, .sm = 0, - .cs_pin = PIN_CS + .cs_pin = PICO_DEFAULT_SPI_CSN_PIN }; - gpio_init(PIN_CS); - gpio_put(PIN_CS, 1); - gpio_set_dir(PIN_CS, GPIO_OUT); + gpio_init(PICO_DEFAULT_SPI_CSN_PIN); + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1); + gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT); + // Make the CS pin available to picotool + bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS")); uint offset = pio_add_program(spi.pio, &spi_cpha0_program); printf("Loaded program at %d\n", offset); @@ -122,10 +125,12 @@ int main() { 31.25f, // 1 MHz @ 125 clk_sys false, // CPHA = 0 false, // CPOL = 0 - PIN_SCK, - PIN_MOSI, - PIN_MISO + PICO_DEFAULT_SPI_SCK_PIN, + PICO_DEFAULT_SPI_TX_PIN, + PICO_DEFAULT_SPI_RX_PIN ); + // Make the SPI pins available to picotool + bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_PIO0)); uint8_t page_buf[FLASH_PAGE_SIZE]; @@ -152,4 +157,5 @@ int main() { printbuf(page_buf); return 0; +#endif } diff --git a/spi/bme280_spi/bme280_spi.c b/spi/bme280_spi/bme280_spi.c index 9eff1c2..69ad74a 100644 --- a/spi/bme280_spi/bme280_spi.c +++ b/spi/bme280_spi/bme280_spi.c @@ -7,6 +7,7 @@ #include #include #include "pico/stdlib.h" +#include "pico/binary_info.h" #include "hardware/spi.h" /* Example code to talk to a bme280 humidity/temperature/pressure sensor. @@ -36,12 +37,6 @@ https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf */ -#define PIN_MISO 16 -#define PIN_CS 17 -#define PIN_SCK 18 -#define PIN_MOSI 19 - -#define SPI_PORT spi0 #define READ_BIT 0x80 int32_t t_fine; @@ -110,24 +105,27 @@ uint32_t compensate_humidity(int32_t adc_H) { return (uint32_t) (v_x1_u32r >> 12); } +#ifdef PICO_DEFAULT_SPI_CSN_PIN static inline void cs_select() { asm volatile("nop \n nop \n nop"); - gpio_put(PIN_CS, 0); // Active low + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0); // Active low asm volatile("nop \n nop \n nop"); } static inline void cs_deselect() { asm volatile("nop \n nop \n nop"); - gpio_put(PIN_CS, 1); + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1); asm volatile("nop \n nop \n nop"); } +#endif +#if defined(spi_default) && defined(PICO_DEFAULT_SPI_CSN_PIN) static void write_register(uint8_t reg, uint8_t data) { uint8_t buf[2]; buf[0] = reg & 0x7f; // remove read bit as this is a write buf[1] = data; cs_select(); - spi_write_blocking(SPI_PORT, buf, 2); + spi_write_blocking(spi_default, buf, 2); cs_deselect(); sleep_ms(10); } @@ -138,9 +136,9 @@ static void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) { // so we don't need to keep sending the register we want, just the first. reg |= READ_BIT; cs_select(); - spi_write_blocking(SPI_PORT, ®, 1); + spi_write_blocking(spi_default, ®, 1); sleep_ms(10); - spi_read_blocking(SPI_PORT, 0, buf, len); + spi_read_blocking(spi_default, 0, buf, len); cs_deselect(); sleep_ms(10); } @@ -184,22 +182,31 @@ static void bme280_read_raw(int32_t *humidity, int32_t *pressure, int32_t *tempe *temperature = ((uint32_t) buffer[3] << 12) | ((uint32_t) buffer[4] << 4) | (buffer[5] >> 4); *humidity = (uint32_t) buffer[6] << 8 | buffer[7]; } +#endif int main() { stdio_init_all(); +#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) +#warning spi/bme280_spi example requires a board with SPI pins + puts("Default SPI pins were not defined"); +#else printf("Hello, bme280! Reading raw data from registers via SPI...\n"); // This example will use SPI0 at 0.5MHz. - spi_init(SPI_PORT, 500 * 1000); - gpio_set_function(PIN_MISO, GPIO_FUNC_SPI); - gpio_set_function(PIN_SCK, GPIO_FUNC_SPI); - gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI); + spi_init(spi_default, 500 * 1000); + gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI); + gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI); + gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI); + // Make the SPI pins available to picotool + bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI)); // Chip select is active-low, so we'll initialise it to a driven-high state - gpio_init(PIN_CS); - gpio_set_dir(PIN_CS, GPIO_OUT); - gpio_put(PIN_CS, 1); + gpio_init(PICO_DEFAULT_SPI_CSN_PIN); + gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT); + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1); + // Make the CS pin available to picotool + bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS")); // See if SPI is working - interrograte the device for its I2C ID number, should be 0x60 uint8_t id; @@ -230,4 +237,5 @@ int main() { } return 0; +#endif } diff --git a/spi/mpu9250_spi/mpu9250_spi.c b/spi/mpu9250_spi/mpu9250_spi.c index 97e221c..5714cb7 100644 --- a/spi/mpu9250_spi/mpu9250_spi.c +++ b/spi/mpu9250_spi/mpu9250_spi.c @@ -7,6 +7,7 @@ #include #include #include "pico/stdlib.h" +#include "pico/binary_info.h" #include "hardware/spi.h" /* Example code to talk to a MPU9250 MEMS accelerometer and gyroscope. @@ -117,11 +118,15 @@ int main() { gpio_set_function(PIN_MISO, GPIO_FUNC_SPI); gpio_set_function(PIN_SCK, GPIO_FUNC_SPI); gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI); + // Make the SPI pins available to picotool + bi_decl(bi_3pins_with_func(PIN_MISO, PIN_MOSI, PIN_SCK, GPIO_FUNC_SPI)); // Chip select is active-low, so we'll initialise it to a driven-high state gpio_init(PIN_CS); gpio_set_dir(PIN_CS, GPIO_OUT); gpio_put(PIN_CS, 1); + // Make the CS pin available to picotool + bi_decl(bi_1pin_with_name(PIN_CS, "CS")); mpu9250_reset(); diff --git a/spi/spi_dma/spi_dma.c b/spi/spi_dma/spi_dma.c index 2ee2af3..1354b34 100644 --- a/spi/spi_dma/spi_dma.c +++ b/spi/spi_dma/spi_dma.c @@ -9,37 +9,39 @@ #include #include #include "pico/stdlib.h" +#include "pico/binary_info.h" #include "hardware/spi.h" #include "hardware/dma.h" -#define PIN_MISO 16 -#define PIN_CS 17 -#define PIN_SCK 18 -#define PIN_MOSI 19 - -#define SPI_INST spi0 - #define TEST_SIZE 1024 int main() { // Enable UART so we can print status output stdio_init_all(); +#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) +#warning spi/spi_dma example requires a board with SPI pins + puts("Default SPI pins were not defined"); +#else printf("SPI DMA example\n"); // Enable SPI at 1 MHz and connect to GPIOs - spi_init(SPI_INST, 1000 * 1000); - gpio_set_function(PIN_MISO, GPIO_FUNC_SPI); - gpio_init(PIN_CS); - gpio_set_function(PIN_SCK, GPIO_FUNC_SPI); - gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI); + spi_init(spi_default, 1000 * 1000); + gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI); + gpio_init(PICO_DEFAULT_SPI_CSN_PIN); + gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI); + gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI); + // Make the SPI pins available to picotool + bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI)); + // Make the CS pin available to picotool + bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS")); // Grab some unused dma channels const uint dma_tx = dma_claim_unused_channel(true); const uint dma_rx = dma_claim_unused_channel(true); // Force loopback for testing (I don't have an SPI device handy) - hw_set_bits(&spi_get_hw(SPI_INST)->cr1, SPI_SSPCR1_LBM_BITS); + hw_set_bits(&spi_get_hw(spi_default)->cr1, SPI_SSPCR1_LBM_BITS); static uint8_t txbuf[TEST_SIZE]; static uint8_t rxbuf[TEST_SIZE]; @@ -54,9 +56,9 @@ int main() { printf("Configure TX DMA\n"); dma_channel_config c = dma_channel_get_default_config(dma_tx); channel_config_set_transfer_data_size(&c, DMA_SIZE_8); - channel_config_set_dreq(&c, spi_get_index(SPI_INST) ? DREQ_SPI1_TX : DREQ_SPI0_TX); + channel_config_set_dreq(&c, spi_get_index(spi_default) ? DREQ_SPI1_TX : DREQ_SPI0_TX); dma_channel_configure(dma_tx, &c, - &spi_get_hw(SPI_INST)->dr, // write address + &spi_get_hw(spi_default)->dr, // write address txbuf, // read address TEST_SIZE, // element count (each element is of size transfer_data_size) false); // don't start yet @@ -68,12 +70,12 @@ int main() { // address to increment (so data is written throughout the buffer) c = dma_channel_get_default_config(dma_rx); channel_config_set_transfer_data_size(&c, DMA_SIZE_8); - channel_config_set_dreq(&c, spi_get_index(SPI_INST) ? DREQ_SPI1_RX : DREQ_SPI0_RX); + channel_config_set_dreq(&c, spi_get_index(spi_default) ? DREQ_SPI1_RX : DREQ_SPI0_RX); channel_config_set_read_increment(&c, false); channel_config_set_write_increment(&c, true); dma_channel_configure(dma_rx, &c, rxbuf, // write address - &spi_get_hw(SPI_INST)->dr, // read address + &spi_get_hw(spi_default)->dr, // read address TEST_SIZE, // element count (each element is of size transfer_data_size) false); // don't start yet @@ -100,4 +102,5 @@ int main() { dma_channel_unclaim(dma_tx); dma_channel_unclaim(dma_rx); return 0; +#endif } diff --git a/spi/spi_flash/spi_flash.c b/spi/spi_flash/spi_flash.c index e3df7e8..4e214f5 100644 --- a/spi/spi_flash/spi_flash.c +++ b/spi/spi_flash/spi_flash.c @@ -8,6 +8,7 @@ #include #include "pico/stdlib.h" +#include "pico/binary_info.h" #include "hardware/spi.h" #define FLASH_PAGE_SIZE 256 @@ -21,11 +22,6 @@ #define FLASH_STATUS_BUSY_MASK 0x01 -#define PIN_MISO 4 -#define PIN_CS 5 -#define PIN_SCK 6 -#define PIN_MOSI 7 - static inline void cs_select(uint cs_pin) { asm volatile("nop \n nop \n nop"); // FIXME gpio_put(cs_pin, 0); @@ -110,19 +106,27 @@ void printbuf(uint8_t buf[FLASH_PAGE_SIZE]) { int main() { // Enable UART so we can print status output stdio_init_all(); +#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) +#warning spi/spi_flash example requires a board with SPI pins + puts("Default SPI pins were not defined"); +#else printf("SPI flash example\n"); // Enable SPI 0 at 1 MHz and connect to GPIOs - spi_init(spi0, 1000 * 1000); - gpio_set_function(PIN_MISO, GPIO_FUNC_SPI); - gpio_set_function(PIN_SCK, GPIO_FUNC_SPI); - gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI); + spi_init(spi_default, 1000 * 1000); + gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI); + gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI); + gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI); + // Make the SPI pins available to picotool + bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI)); // Chip select is active-low, so we'll initialise it to a driven-high state - gpio_init(PIN_CS); - gpio_put(PIN_CS, 1); - gpio_set_dir(PIN_CS, GPIO_OUT); + gpio_init(PICO_DEFAULT_SPI_CSN_PIN); + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1); + gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT); + // Make the CS pin available to picotool + bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "CS")); printf("SPI initialised, let's goooooo\n"); @@ -130,25 +134,26 @@ int main() { const uint32_t target_addr = 0; - flash_sector_erase(spi0, PIN_CS, target_addr); - flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE); + flash_sector_erase(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr); + flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf, FLASH_PAGE_SIZE); printf("After erase:\n"); printbuf(page_buf); for (int i = 0; i < FLASH_PAGE_SIZE; ++i) page_buf[i] = i; - flash_page_program(spi0, PIN_CS, target_addr, page_buf); - flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE); + flash_page_program(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf); + flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf, FLASH_PAGE_SIZE); printf("After program:\n"); printbuf(page_buf); - flash_sector_erase(spi0, PIN_CS, target_addr); - flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE); + flash_sector_erase(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr); + flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, target_addr, page_buf, FLASH_PAGE_SIZE); printf("Erase again:\n"); printbuf(page_buf); return 0; +#endif }