Skip to content

Commit 7a1014a

Browse files
committed
cpu/stm32/periph_spi: Fix /CS handling
Previously, the /CS signal was performed by enabling / disabling the SPI peripheral. This had the disadvantage that clock polarity settings where not applied starting with `spi_acquire()`, as assumed by e.g. the SPI SD card driver, but only just before transmitting data. Now the SPI peripheral is enabled on `spi_acquire()` and only disabled when calling `spi_release()`, and the `SPI_CR2_SSOE` bit in the `CR2` register is used for hardware /CS handling (as supposed to).
1 parent 56323a7 commit 7a1014a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

cpu/stm32/periph/spi.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <assert.h>
3030

3131
#include "bitarithm.h"
32-
#include "cpu.h"
3332
#include "mutex.h"
3433
#include "periph/gpio.h"
3534
#include "periph/spi.h"
@@ -246,7 +245,7 @@ void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
246245
periph_apb_clk(spi_config[bus].apbbus)/(1 << (br + 1)),
247246
br);
248247

249-
uint16_t cr1_settings = ((br << BR_SHIFT) | mode | SPI_CR1_MSTR);
248+
uint16_t cr1_settings = ((br << BR_SHIFT) | mode | SPI_CR1_MSTR | SPI_CR1_SPE);
250249
/* Settings to add to CR2 in addition to SPI_CR2_SETTINGS */
251250
uint16_t cr2_extra_settings = 0;
252251
if (cs != SPI_HWCS_MASK) {
@@ -393,10 +392,12 @@ void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
393392
assert(out || in);
394393

395394
/* active the given chip select line */
396-
dev(bus)->CR1 |= (SPI_CR1_SPE); /* this pulls the HW CS line low */
397395
if ((cs != SPI_HWCS_MASK) && gpio_is_valid(cs)) {
398396
gpio_clear((gpio_t)cs);
399397
}
398+
else {
399+
dev(bus)->CR2 |= SPI_CR2_SSOE;
400+
}
400401

401402
#ifdef MODULE_PERIPH_DMA
402403
if (_use_dma(&spi_config[bus])) {
@@ -411,9 +412,11 @@ void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
411412

412413
/* release the chip select if not specified differently */
413414
if ((!cont) && gpio_is_valid(cs)) {
414-
dev(bus)->CR1 &= ~(SPI_CR1_SPE); /* pull HW CS line high */
415415
if (cs != SPI_HWCS_MASK) {
416416
gpio_set((gpio_t)cs);
417417
}
418+
else {
419+
dev(bus)->CR2 &= ~(SPI_CR2_SSOE);
420+
}
418421
}
419422
}

0 commit comments

Comments
 (0)