From d1e785444d7c3ac31bf6ad0e475ad3701e9dac3c Mon Sep 17 00:00:00 2001 From: chuck todd Date: Sun, 26 Jan 2020 10:55:15 -0700 Subject: [PATCH 1/2] fix removeApbChangeCallback() error in spiStopBus() spiStartBus() was using spiStopBus() to init the hardware, one of spiStopBus() functions is to unregister the runtime CPU clock speed change callback. But, spiStartBus() only wanted to init the hardware. This patch separates the hardware init into a standalone function spiInitBus() that both spiStartBus() and spiStopBus() call. --- cores/esp32/esp32-hal-spi.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index cf302cac88f..cda7f28e8d2 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -384,12 +384,8 @@ static void _on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb } } -void spiStopBus(spi_t * spi) +static void spiInitBus(spi_t * spi) { - if(!spi) { - return; - } - SPI_MUTEX_LOCK(); spi->dev->slave.trans_done = 0; spi->dev->slave.slave_mode = 0; spi->dev->pin.val = 0; @@ -399,8 +395,19 @@ void spiStopBus(spi_t * spi) spi->dev->ctrl1.val = 0; spi->dev->ctrl2.val = 0; spi->dev->clock.val = 0; - SPI_MUTEX_UNLOCK(); +} + +void spiStopBus(spi_t * spi) +{ + if(!spi) { + return; + } + removeApbChangeCallback(spi, _on_apb_change); + + SPI_MUTEX_LOCK(); + spiInitbus(spi); + SPI_MUTEX_UNLOCK(); } spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_t bitOrder) @@ -431,12 +438,8 @@ spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_ DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI_RST_1); } - spiStopBus(spi); - spiSetDataMode(spi, dataMode); - spiSetBitOrder(spi, bitOrder); - spiSetClockDiv(spi, clockDiv); - SPI_MUTEX_LOCK(); + spiInitBus(spi); spi->dev->user.usr_mosi = 1; spi->dev->user.usr_miso = 1; spi->dev->user.doutdin = 1; @@ -447,6 +450,10 @@ spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_ } SPI_MUTEX_UNLOCK(); + spiSetDataMode(spi, dataMode); + spiSetBitOrder(spi, bitOrder); + spiSetClockDiv(spi, clockDiv); + addApbChangeCallback(spi, _on_apb_change); return spi; } From b73a44a77f04f47de4c6afb23478b2a0a622b084 Mon Sep 17 00:00:00 2001 From: chuck todd Date: Sun, 26 Jan 2020 12:53:42 -0700 Subject: [PATCH 2/2] Update esp32-hal-spi.c capitalization problem --- cores/esp32/esp32-hal-spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index cda7f28e8d2..b3b703ccc2a 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -406,7 +406,7 @@ void spiStopBus(spi_t * spi) removeApbChangeCallback(spi, _on_apb_change); SPI_MUTEX_LOCK(); - spiInitbus(spi); + spiInitBus(spi); SPI_MUTEX_UNLOCK(); }