AP_HAL_ChibiOS: add option to set HAL_SPI_SCK_SAVE_RESTORE

This commit is contained in:
bugobliterator 2023-10-18 14:58:00 +11:00 committed by Andrew Tridgell
parent 1f1c0714df
commit 14093ab2a0
2 changed files with 12 additions and 2 deletions

View File

@ -88,7 +88,9 @@ SPIBus::SPIBus(uint8_t _bus) :
FUNCTOR_BIND_MEMBER(&SPIBus::dma_deallocate, void, Shared_DMA *)); FUNCTOR_BIND_MEMBER(&SPIBus::dma_deallocate, void, Shared_DMA *));
// remember the SCK line for stop_peripheral()/start_peripheral() // remember the SCK line for stop_peripheral()/start_peripheral()
#if HAL_SPI_SCK_SAVE_RESTORE
sck_mode = palReadLineMode(spi_devices[bus].sck_line); sck_mode = palReadLineMode(spi_devices[bus].sck_line);
#endif
} }
/* /*
@ -349,6 +351,7 @@ void SPIBus::stop_peripheral(void)
return; return;
} }
const auto &sbus = spi_devices[bus]; const auto &sbus = spi_devices[bus];
#if HAL_SPI_SCK_SAVE_RESTORE
if (spi_mode == SPIDEV_MODE0 || spi_mode == SPIDEV_MODE1) { if (spi_mode == SPIDEV_MODE0 || spi_mode == SPIDEV_MODE1) {
// Clock polarity is 0, so we need to set the clock line low before spi reset // Clock polarity is 0, so we need to set the clock line low before spi reset
palClearLine(sbus.sck_line); palClearLine(sbus.sck_line);
@ -357,6 +360,7 @@ void SPIBus::stop_peripheral(void)
palSetLine(sbus.sck_line); palSetLine(sbus.sck_line);
} }
palSetLineMode(sbus.sck_line, PAL_MODE_OUTPUT_PUSHPULL); palSetLineMode(sbus.sck_line, PAL_MODE_OUTPUT_PUSHPULL);
#endif
spiStop(sbus.driver); spiStop(sbus.driver);
spi_started = false; spi_started = false;
} }
@ -372,10 +376,10 @@ void SPIBus::start_peripheral(void)
/* start driver and setup transfer parameters */ /* start driver and setup transfer parameters */
spiStart(spi_devices[bus].driver, &spicfg); spiStart(spi_devices[bus].driver, &spicfg);
#if HAL_SPI_SCK_SAVE_RESTORE
// restore sck pin mode from stop_peripheral() // restore sck pin mode from stop_peripheral()
palSetLineMode(spi_devices[bus].sck_line, sck_mode); palSetLineMode(spi_devices[bus].sck_line, sck_mode);
#endif
spi_started = true; spi_started = true;
} }

View File

@ -25,6 +25,10 @@
#include "Scheduler.h" #include "Scheduler.h"
#include "Device.h" #include "Device.h"
#ifndef HAL_SPI_SCK_SAVE_RESTORE
#define HAL_SPI_SCK_SAVE_RESTORE FALSE
#endif
namespace ChibiOS { namespace ChibiOS {
class SPIBus : public DeviceBus { class SPIBus : public DeviceBus {
@ -54,7 +58,9 @@ private:
bool spi_started; bool spi_started;
// mode line for SCK pin // mode line for SCK pin
#if HAL_SPI_SCK_SAVE_RESTORE
iomode_t sck_mode; iomode_t sck_mode;
#endif
}; };
struct SPIDesc { struct SPIDesc {