Hi,
We are currently migrating our nrf5 SDK code to NCS 2.2.0 on a custom nrf52833 board (using a .overlay file on the DK).
Our power consumption for the same peripheral is ~6.1 uA average, as opposed to our expected ~3.5 uA average on nrf5SDK - but we are able to achieve around ~4 uA average by not calling the SPI module at all.
Some points to note before the code:
- According to the data sheet of the peripheral - we should be working in NRF_SPI_MODE_3 on nrf5 SDK, which seems to translate to | CPOL | CPHA on zephyr. However - if we try to use this mode, SPI does not work at all, and we see a very high baseline on PPK.
- This peripheral and another peripheral on our board share 2 pins: SCK, and another pin which is MISO for this peripheral, and MISO_MOSI for the other peripheral. This does not cause problems on nrf5 SDK.
Our SPI usage is as follows:
const struct gpio_dt_spec spec = { .dt_flags = GPIO_ACTIVE_LOW, .pin = 5, .port = DEVICE_DT_GET(DT_NODELABEL(gpio0)) }; struct spi_cs_control spi_cs = { .gpio = spec, /* delay in microseconds to wait before starting the transmission and before releasing the CS line */ .delay = 10, }; #define SPI_CS (&spi_cs) struct spi_config spi_cfg = { .frequency = 0x4000000, .operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8), // | SPI_MODE_CPOL | SPI_MODE_CPHA, .cs = SPI_CS, }; const struct device *spi1_dev = DEVICE_DT_GET(DT_NODELABEL(spi1)); void acc_spi_write(uint8_t cmd, uint8_t val) { uint8_t com[2] = { cmd, val }; struct spi_buf tx_buf[1] = { { .buf = com, .len = 2 } }; struct spi_buf_set tx_bufs = { .buffers = tx_buf, .count = 1 }; int ans = spi_transceive(spi1_dev,&spi_cfg,&tx_bufs,NULL); if(ans) { printk("SPI write failed - %d\n",ans); } }
And in the overlay file:
&pinctrl { // acc status = "okay"; spi1_default: spi1_default { group1 { psels = <NRF_PSEL(SPIM_SCK, 0, 4)>, <NRF_PSEL(SPIM_MOSI, 0, 11)>, <NRF_PSEL(SPIM_MISO, 1, 9)>; }; }; spi1_sleep: spi1_sleep { group1 { psels = <NRF_PSEL(SPIM_SCK, 0, 4)>, <NRF_PSEL(SPIM_MOSI, 0, 11)>, <NRF_PSEL(SPIM_MISO, 1, 9)>; low-power-enable; }; }; spi0_default: spi0_default { group1 { psels = <NRF_PSEL(SPIM_SCK, 0, 4)>, <NRF_PSEL(SPIM_MOSI, 0, 11)>; }; }; spi0_sleep: spi0_sleep { group1 { psels = <NRF_PSEL(SPIM_SCK, 0, 4)>, <NRF_PSEL(SPIM_MOSI, 0, 11)>; low-power-enable; }; }; }; &spi1 { compatible = "nordic,nrf-spi"; status = "okay"; clock-frequency = <0x4000000>; overrun-character = < 255 >; cs-gpios = < &gpio0 5 GPIO_ACTIVE_LOW >; }; &spi0 { compatible = "nordic,nrf-spi"; status = "okay"; clock-frequency = <0x4000000>; overrun-character = < 255 >; cs-gpios = < &gpio0 15 GPIO_ACTIVE_LOW >; };