diff --git a/drivers/spi/spi_rpi_pico_pio.c b/drivers/spi/spi_rpi_pico_pio.c index c7eb014d9d5782e..e33664c1b7611c7 100644 --- a/drivers/spi/spi_rpi_pico_pio.c +++ b/drivers/spi/spi_rpi_pico_pio.c @@ -12,6 +12,7 @@ LOG_MODULE_REGISTER(spi_pico_pio); #include #include +#include #include #include #include @@ -90,78 +91,31 @@ RPI_PICO_PIO_DEFINE_PROGRAM(spi_mode_1_1, SPI_MODE_1_1_WRAP_TARGET, SPI_MODE_1_1 /* ------------------- */ #define SPI_SIO_MODE_0_0_TX_WRAP_TARGET 0 -#define SPI_SIO_MODE_0_0_TX_WRAP 2 +#define SPI_SIO_MODE_0_0_TX_WRAP 1 #define SPI_SIO_MODE_0_0_TX_CYCLES 2 RPI_PICO_PIO_DEFINE_PROGRAM(spi_sio_mode_0_0_tx, SPI_SIO_MODE_0_0_TX_WRAP_TARGET, SPI_SIO_MODE_0_0_TX_WRAP, /* .wrap_target */ - 0x80a0, /* 0: pull block side 0 */ - 0x6001, /* 1: out pins, 1 side 0 */ - 0x10e1, /* 2: jmp !osre, 1 side 1 */ + 0x6001, /* 0: out pins, 1 side 0 */ + 0xb042, /* 1: nop side 1 */ /* .wrap */ ); /* ------------------------- */ -/* spi_sio_mode_0_0_8_bit_rx */ +/* spi_sio_mode_0_0_rx */ /* ------------------------- */ -#define SPI_SIO_MODE_0_0_8_BIT_RX_WRAP_TARGET 0 -#define SPI_SIO_MODE_0_0_8_BIT_RX_WRAP 6 -#define SPI_SIO_MODE_0_0_8_BIT_RX_CYCLES 2 +#define SPI_SIO_MODE_0_0_RX_WRAP_TARGET 0 +#define SPI_SIO_MODE_0_0_RX_WRAP 2 +#define SPI_SIO_MODE_0_0_RX_CYCLES 2 -RPI_PICO_PIO_DEFINE_PROGRAM(spi_sio_mode_0_0_8_bit_rx, SPI_SIO_MODE_0_0_8_BIT_RX_WRAP_TARGET, - SPI_SIO_MODE_0_0_8_BIT_RX_WRAP, +RPI_PICO_PIO_DEFINE_PROGRAM(spi_sio_mode_0_0_rx, SPI_SIO_MODE_0_0_RX_WRAP_TARGET, + SPI_SIO_MODE_0_0_RX_WRAP, /* .wrap_target */ - 0x80a0, /* 0: pull block side 0 */ - 0x6020, /* 1: out x, 32 side 0 */ - 0xe047, /* 2: set y, 7 side 0 */ - 0x5001, /* 3: in pins, 1 side 1 */ - 0x0083, /* 4: jmp y--, 3 side 0 */ - 0x8020, /* 5: push block side 0 */ - 0x0042, /* 6: jmp x--, 2 side 0 */ - /* .wrap */ -); - -/* -------------------------- */ -/* spi_sio_mode_0_0_16_bit_rx */ -/* -------------------------- */ - -#define SPI_SIO_MODE_0_0_16_BIT_RX_WRAP_TARGET 0 -#define SPI_SIO_MODE_0_0_16_BIT_RX_WRAP 6 -#define SPI_SIO_MODE_0_0_16_BIT_RX_CYCLES 2 - -RPI_PICO_PIO_DEFINE_PROGRAM(spi_sio_mode_0_0_16_bit_rx, SPI_SIO_MODE_0_0_16_BIT_RX_WRAP_TARGET, - SPI_SIO_MODE_0_0_16_BIT_RX_WRAP, - /* .wrap_target */ - 0x80a0, /* 0: pull block side 0 */ - 0x6020, /* 1: out x, 32 side 0 */ - 0xe04f, /* 2: set y, 15 side 0 */ - 0x5001, /* 3: in pins, 1 side 1 */ - 0x0083, /* 4: jmp y--, 3 side 0 */ - 0x8020, /* 5: push block side 0 */ - 0x0042, /* 6: jmp x--, 2 side 0 */ - /* .wrap */ -); - -/* -------------------------- */ -/* spi_sio_mode_0_0_32_bit_rx */ -/* -------------------------- */ - -#define SPI_SIO_MODE_0_0_32_BIT_RX_WRAP_TARGET 0 -#define SPI_SIO_MODE_0_0_32_BIT_RX_WRAP 6 -#define SPI_SIO_MODE_0_0_32_BIT_RX_CYCLES 2 - -RPI_PICO_PIO_DEFINE_PROGRAM(spi_sio_mode_0_0_32_bit_rx, SPI_SIO_MODE_0_0_32_BIT_RX_WRAP_TARGET, - SPI_SIO_MODE_0_0_32_BIT_RX_WRAP, - /* .wrap_target */ - 0x80a0, /* 0: pull block side 0 */ - 0x6020, /* 1: out x, 32 side 0 */ - 0xe05f, /* 2: set y, 31 side 0 */ - 0x5001, /* 3: in pins, 1 side 1 */ - 0x0083, /* 4: jmp y--, 3 side 0 */ - 0x8020, /* 5: push block side 0 */ - 0x0042, /* 6: jmp x--, 2 side 0 */ + 0x6020, /* 0: out x, 32 side 0 */ + 0x5001, /* 1: in pins, 1 side 1 */ + 0x0041, /* 2: jmp x--, 1 side 0 */ /* .wrap */ ); #endif /* SPI_RPI_PICO_PIO_HALF_DUPLEX_ENABLED */ @@ -237,7 +191,8 @@ static inline uint32_t spi_pico_pio_sm_get32(PIO pio, uint sm) static inline int spi_pico_pio_sm_complete(struct spi_pico_pio_data *data) { - return (data->pio->sm[data->pio_sm].addr == data->pio_tx_offset); + return ((data->pio->sm[data->pio_sm].addr == data->pio_tx_offset) + && pio_sm_is_tx_fifo_empty(data->pio, data->pio_sm)); } static int spi_pico_pio_configure(const struct spi_pico_pio_config *dev_cfg, @@ -355,49 +310,21 @@ static int spi_pico_pio_configure(const struct spi_pico_pio_config *dev_cfg, data->pio_tx_offset = pio_add_program(data->pio, RPI_PICO_PIO_GET_PROGRAM(spi_sio_mode_0_0_tx)); - switch (data->dfs) { - case 4: - data->pio_rx_offset = pio_add_program( - data->pio, RPI_PICO_PIO_GET_PROGRAM(spi_sio_mode_0_0_32_bit_rx)); - data->pio_rx_wrap_target = - data->pio_rx_offset + - RPI_PICO_PIO_GET_WRAP_TARGET(spi_sio_mode_0_0_32_bit_rx); - data->pio_rx_wrap = data->pio_rx_offset + - RPI_PICO_PIO_GET_WRAP(spi_sio_mode_0_0_32_bit_rx); - break; - - case 2: - data->pio_rx_offset = pio_add_program( - data->pio, RPI_PICO_PIO_GET_PROGRAM(spi_sio_mode_0_0_16_bit_rx)); - data->pio_rx_wrap_target = - data->pio_rx_offset + - RPI_PICO_PIO_GET_WRAP_TARGET(spi_sio_mode_0_0_16_bit_rx); - data->pio_rx_wrap = data->pio_rx_offset + - RPI_PICO_PIO_GET_WRAP(spi_sio_mode_0_0_16_bit_rx); - break; - - case 1: - data->pio_rx_offset = pio_add_program( - data->pio, RPI_PICO_PIO_GET_PROGRAM(spi_sio_mode_0_0_8_bit_rx)); - data->pio_rx_wrap_target = - data->pio_rx_offset + - RPI_PICO_PIO_GET_WRAP_TARGET(spi_sio_mode_0_0_8_bit_rx); - data->pio_rx_wrap = data->pio_rx_offset + - RPI_PICO_PIO_GET_WRAP(spi_sio_mode_0_0_8_bit_rx); - break; - - default: - LOG_ERR("Support for %d transfer size not enabled", (data->dfs * 8)); - return -EINVAL; - } + data->pio_rx_offset = pio_add_program( + data->pio, RPI_PICO_PIO_GET_PROGRAM(spi_sio_mode_0_0_rx)); + data->pio_rx_wrap_target = + data->pio_rx_offset + + RPI_PICO_PIO_GET_WRAP_TARGET(spi_sio_mode_0_0_rx); + data->pio_rx_wrap = data->pio_rx_offset + + RPI_PICO_PIO_GET_WRAP(spi_sio_mode_0_0_rx); sm_config = pio_get_default_sm_config(); sm_config_set_clkdiv(&sm_config, clock_div); sm_config_set_in_pins(&sm_config, sio->pin); - sm_config_set_in_shift(&sm_config, lsb, false, data->bits); + sm_config_set_in_shift(&sm_config, lsb, true, data->bits); sm_config_set_out_pins(&sm_config, sio->pin, 1); - sm_config_set_out_shift(&sm_config, lsb, false, data->bits); + sm_config_set_out_shift(&sm_config, lsb, true, data->bits); hw_set_bits(&data->pio->input_sync_bypass, 1u << sio->pin); sm_config_set_sideset_pins(&sm_config, clk->pin); @@ -496,8 +423,8 @@ static void spi_pico_pio_txrx_4_wire(const struct device *dev) { struct spi_pico_pio_data *data = dev->data; const size_t chunk_len = spi_context_max_continuous_chunk(&data->spi_ctx); - const void *txbuf = data->spi_ctx.tx_buf; - void *rxbuf = data->spi_ctx.rx_buf; + const uint8_t *txbuf = data->spi_ctx.tx_buf; + uint8_t *rxbuf = data->spi_ctx.rx_buf; uint32_t txrx; size_t fifo_cnt = 0; @@ -516,18 +443,16 @@ static void spi_pico_pio_txrx_4_wire(const struct device *dev) switch (data->dfs) { case 4: { if (txbuf) { - txrx = ((uint32_t *)txbuf)[data->tx_count]; + txrx = sys_get_be32(txbuf + (data->tx_count * 4)); } spi_pico_pio_sm_put32(data->pio, data->pio_sm, txrx); - data->tx_count += 4; } break; case 2: { if (txbuf) { - txrx = ((uint16_t *)txbuf)[data->tx_count]; + txrx = sys_get_be16(txbuf + (data->tx_count * 2)); } spi_pico_pio_sm_put16(data->pio, data->pio_sm, txrx); - data->tx_count += 2; } break; case 1: { @@ -535,13 +460,13 @@ static void spi_pico_pio_txrx_4_wire(const struct device *dev) txrx = ((uint8_t *)txbuf)[data->tx_count]; } spi_pico_pio_sm_put8(data->pio, data->pio_sm, txrx); - data->tx_count++; } break; default: LOG_ERR("Support fot %d bits not enabled", (data->dfs * 8)); break; } + data->tx_count++; fifo_cnt++; } @@ -553,9 +478,8 @@ static void spi_pico_pio_txrx_4_wire(const struct device *dev) /* Discard received data if rx buffer not assigned */ if (rxbuf) { - ((uint32_t *)rxbuf)[data->rx_count] = (uint32_t)txrx; + sys_put_be32(txrx, rxbuf + (data->rx_count * 4)); } - data->rx_count += 4; } break; case 2: { @@ -563,9 +487,8 @@ static void spi_pico_pio_txrx_4_wire(const struct device *dev) /* Discard received data if rx buffer not assigned */ if (rxbuf) { - ((uint16_t *)rxbuf)[data->rx_count] = (uint16_t)txrx; + sys_put_be16(txrx, rxbuf + (data->rx_count * 2)); } - data->rx_count += 2; } break; case 1: { @@ -575,13 +498,13 @@ static void spi_pico_pio_txrx_4_wire(const struct device *dev) if (rxbuf) { ((uint8_t *)rxbuf)[data->rx_count] = (uint8_t)txrx; } - data->rx_count++; } break; default: LOG_ERR("Support fot %d bits not enabled", (data->dfs * 8)); break; } + data->rx_count++; fifo_cnt--; } } @@ -592,8 +515,8 @@ static void spi_pico_pio_txrx_3_wire(const struct device *dev) #if SPI_RPI_PICO_PIO_HALF_DUPLEX_ENABLED struct spi_pico_pio_data *data = dev->data; const struct spi_pico_pio_config *dev_cfg = dev->config; - const void *txbuf = data->spi_ctx.tx_buf; - void *rxbuf = data->spi_ctx.rx_buf; + const uint8_t *txbuf = data->spi_ctx.tx_buf; + uint8_t *rxbuf = data->spi_ctx.rx_buf; uint32_t txrx; int sio_pin = dev_cfg->sio_gpio.pin; uint32_t tx_size = data->spi_ctx.tx_len; /* Number of WORDS to send */ @@ -622,27 +545,25 @@ static void spi_pico_pio_txrx_3_wire(const struct device *dev) switch (data->dfs) { case 4: { - txrx = ((uint32_t *)txbuf)[data->tx_count]; + txrx = sys_get_be32(txbuf + (data->tx_count * 4)); spi_pico_pio_sm_put32(data->pio, data->pio_sm, txrx); - data->tx_count += 4; } break; case 2: { - txrx = ((uint16_t *)txbuf)[data->tx_count]; + txrx = sys_get_be16(txbuf + (data->tx_count * 2)); spi_pico_pio_sm_put16(data->pio, data->pio_sm, txrx); - data->tx_count += 2; } break; case 1: { txrx = ((uint8_t *)txbuf)[data->tx_count]; spi_pico_pio_sm_put8(data->pio, data->pio_sm, txrx); - data->tx_count++; } break; default: LOG_ERR("Support fot %d bits not enabled", (data->dfs * 8)); break; } + data->tx_count++; } } while ((!pio_sm_is_tx_fifo_empty(data->pio, data->pio_sm)) || @@ -651,6 +572,8 @@ static void spi_pico_pio_txrx_3_wire(const struct device *dev) } } + k_sleep(K_TICKS(data->tx_period_ticks)); + if (rxbuf) { pio_sm_set_enabled(data->pio, data->pio_sm, false); pio_sm_set_wrap(data->pio, data->pio_sm, data->pio_rx_wrap_target, @@ -659,8 +582,7 @@ static void spi_pico_pio_txrx_3_wire(const struct device *dev) pio_sm_set_pindirs_with_mask(data->pio, data->pio_sm, 0, BIT(sio_pin)); pio_sm_restart(data->pio, data->pio_sm); pio_sm_clkdiv_restart(data->pio, data->pio_sm); - pio_sm_put(data->pio, data->pio_sm, rx_size - 1); - pio_sm_exec(data->pio, data->pio_sm, pio_encode_out(pio_x, 32)); + pio_sm_put(data->pio, data->pio_sm, (rx_size * data->bits) - 1); pio_sm_exec(data->pio, data->pio_sm, pio_encode_jmp(data->pio_rx_offset)); pio_sm_set_enabled(data->pio, data->pio_sm, true); @@ -671,26 +593,24 @@ static void spi_pico_pio_txrx_3_wire(const struct device *dev) switch (data->dfs) { case 4: { txrx = spi_pico_pio_sm_get32(data->pio, data->pio_sm); - ((uint32_t *)rxbuf)[data->rx_count] = (uint32_t)txrx; - data->rx_count += 4; + sys_put_be32(txrx, rxbuf + (data->rx_count * 4)); } break; case 2: { txrx = spi_pico_pio_sm_get16(data->pio, data->pio_sm); - ((uint16_t *)rxbuf)[data->rx_count] = (uint16_t)txrx; - data->rx_count += 2; + sys_put_be16(txrx, rxbuf + (data->rx_count * 2)); } break; case 1: { txrx = spi_pico_pio_sm_get8(data->pio, data->pio_sm); - ((uint8_t *)rxbuf)[data->rx_count] = (uint8_t)txrx; - data->rx_count++; + rxbuf[data->rx_count] = (uint8_t)txrx; } break; default: LOG_ERR("Support fot %d bits not enabled", (data->dfs * 8)); break; } + data->rx_count++; } } } diff --git a/samples/sensor/magn_polling/README.rst b/samples/sensor/magn_polling/README.rst index 850b167a357635b..6299064ea5d0334 100644 --- a/samples/sensor/magn_polling/README.rst +++ b/samples/sensor/magn_polling/README.rst @@ -1,7 +1,7 @@ .. _magn-polling-sample: -BMC150 Magnetometer Sample -########################## +Magnetometer Sample +################### Overview ******** @@ -9,3 +9,27 @@ Overview Sample application that periodically reads magnetometer (X, Y, Z) data from the first available device that implements SENSOR_CHAN_MAGN_* (predefined array of device names). + +Board-specific overlays +*********************** + +TMAG5170 via Raspberry Pi Pico +============================== + +The Zephyr driver for the :dtcompatible:`ti,tmag5170`` requires an SPI driver +that supports 32-bit SPI_WORD_SIZE. On the :ref:`rpi_pico`, the +:dtcompatible:`raspberrypi,pico-spi-pio` SPI driver provides this support, +demonstrated with the +:zephyr_file:`samples/sensor/magn_polling/boards/rpi_pico.overlay`. + +The GPIO pin assignments in the overlay file are arbitrary. The PIO SPI +driver allows using any four GPIO pins for the SPI bus. Just keep in mind +that the pin assignments in the pinctrl block and the pio0_spi0 block +must match. + +With the sensor wired to the desired pins, build and flash with: + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/magn_polling + :goals: build flash + :board: rpi_pico diff --git a/samples/sensor/magn_polling/boards/rpi_pico.overlay b/samples/sensor/magn_polling/boards/rpi_pico.overlay new file mode 100644 index 000000000000000..e2cf9fe3770244f --- /dev/null +++ b/samples/sensor/magn_polling/boards/rpi_pico.overlay @@ -0,0 +1,44 @@ +/ { + aliases { + magn0 = &tmag5170; + }; +}; + +&pinctrl { + pio0_spi0_default: pio0_spi0_default { + /* gpio 9 is used for chip select, not assigned to the PIO */ + group1 { + pinmux = , ; + }; + group2 { + pinmux = ; + input-enable; + }; + }; + +}; + +&pio0 { + status = "okay"; + + pio0_spi0: pio0_spi0 { + pinctrl-0 = <&pio0_spi0_default>; + pinctrl-names = "default"; + + compatible = "raspberrypi,pico-spi-pio"; + status = "okay"; + #address-cells = <1>; + #size-cells = <0>; + clocks = <&clocks RPI_PICO_CLKID_CLK_SYS>; + miso-gpios = <&gpio0 8 0>; + cs-gpios = <&gpio0 9 GPIO_ACTIVE_LOW>; + clk-gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>; + mosi-gpios = <&gpio0 11 GPIO_ACTIVE_HIGH>; + tmag5170: tmag5170@0 { + compatible = "ti,tmag5170"; + reg = <0>; + operating-mode = <3>; + spi-max-frequency = <1000000>; /* conservatively set to 1MHz */ + }; + }; +};