Skip to content

Commit c56bb60

Browse files
[nrf fromtree] drivers: spi: nrfx_spim: patch flow so bus inits before cs is set
The current flow sets CS before the SPIM peripheral actually drives the bus. This can cause SCK to be in the incorrect initial state dictated by CPOL. New flow enables SPIM after it has been configured, before CS is set, and disabled is after CS is cleared, thus the bus is driven by SPIM during the entire transfer. Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no> (cherry picked from commit 407cedc) (cherry picked from commit 2297ceb)
1 parent 9a1cd0a commit c56bb60

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

drivers/spi/spi_nrfx_spim_common.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,41 @@ void spi_nrfx_spim_common_cs_set(const struct device *dev, const struct spi_conf
5050
struct spi_nrfx_common_data *dev_data = dev->data;
5151
NRF_SPIM_Type *spim_reg = dev_data->spim.p_reg;
5252

53-
if (spi_cfg->cs.cs_is_gpio == false && NRF_SPIM_IS_320MHZ_SPIM(spim_reg) == false) {
54-
return;
55-
}
53+
/*
54+
* Enable the SPIM peripheral so it drives the SPI bus, ensuring correct initial bus state
55+
* before transfer starts, and setting CS if its controlled by the SPIM peripheral.
56+
*/
57+
nrfy_spim_enable(spim_reg);
5658

5759
if (spi_cfg->cs.cs_is_gpio) {
5860
gpio_pin_set_dt(&spi_cfg->cs.gpio, 1);
59-
} else {
60-
nrfy_spim_enable(spim_reg);
6161
}
6262

63-
k_busy_wait(spi_cfg->cs.delay);
63+
/* Wait only if we control the CS pin */
64+
if (spi_cfg->cs.cs_is_gpio || NRF_SPIM_IS_320MHZ_SPIM(spim_reg)) {
65+
k_busy_wait(spi_cfg->cs.delay);
66+
}
6467
}
6568

6669
void spi_nrfx_spim_common_cs_clear(const struct device *dev, const struct spi_config *spi_cfg)
6770
{
6871
struct spi_nrfx_common_data *dev_data = dev->data;
6972
NRF_SPIM_Type *spim_reg = dev_data->spim.p_reg;
7073

71-
if (spi_cfg->cs.cs_is_gpio == false && NRF_SPIM_IS_320MHZ_SPIM(spim_reg) == false) {
72-
return;
74+
/* Wait only if we control the CS pin */
75+
if (spi_cfg->cs.cs_is_gpio || NRF_SPIM_IS_320MHZ_SPIM(spim_reg)) {
76+
k_busy_wait(spi_cfg->cs.delay);
7377
}
7478

75-
k_busy_wait(spi_cfg->cs.delay);
76-
7779
if (spi_cfg->cs.cs_is_gpio) {
7880
gpio_pin_set_dt(&spi_cfg->cs.gpio, 0);
79-
} else {
80-
nrfy_spim_disable(spim_reg);
8181
}
82+
83+
/*
84+
* Disable the SPIM peripheral so it no longer drives the SPI bus, clearing CS if its
85+
* controlled by the SPIM peripheral.
86+
*/
87+
nrfy_spim_disable(spim_reg);
8288
}
8389

8490
static void evt_handler(nrfx_spim_event_t const *evt, void *data)

0 commit comments

Comments
 (0)