diff --git a/libraries/AP_HAL_ChibiOS/SPIDevice.cpp b/libraries/AP_HAL_ChibiOS/SPIDevice.cpp index eb7e99df3b80f5..b0bad4d05420ac 100644 --- a/libraries/AP_HAL_ChibiOS/SPIDevice.cpp +++ b/libraries/AP_HAL_ChibiOS/SPIDevice.cpp @@ -88,7 +88,9 @@ SPIBus::SPIBus(uint8_t _bus) : FUNCTOR_BIND_MEMBER(&SPIBus::dma_deallocate, void, Shared_DMA *)); // remember the SCK line for stop_peripheral()/start_peripheral() +#if HAL_SPI_SCK_SAVE_RESTORE sck_mode = palReadLineMode(spi_devices[bus].sck_line); +#endif } /* @@ -348,6 +350,7 @@ void SPIBus::stop_peripheral(void) if (!spi_started) { return; } +#if HAL_SPI_SCK_SAVE_RESTORE const auto &sbus = spi_devices[bus]; 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 @@ -357,6 +360,7 @@ void SPIBus::stop_peripheral(void) palSetLine(sbus.sck_line); } palSetLineMode(sbus.sck_line, PAL_MODE_OUTPUT_PUSHPULL); +#endif spiStop(sbus.driver); spi_started = false; } @@ -372,10 +376,10 @@ void SPIBus::start_peripheral(void) /* start driver and setup transfer parameters */ spiStart(spi_devices[bus].driver, &spicfg); - +#if HAL_SPI_SCK_SAVE_RESTORE // restore sck pin mode from stop_peripheral() palSetLineMode(spi_devices[bus].sck_line, sck_mode); - +#endif spi_started = true; } diff --git a/libraries/AP_HAL_ChibiOS/SPIDevice.h b/libraries/AP_HAL_ChibiOS/SPIDevice.h index 55a9c4d674742f..c082b4f7283a2a 100644 --- a/libraries/AP_HAL_ChibiOS/SPIDevice.h +++ b/libraries/AP_HAL_ChibiOS/SPIDevice.h @@ -25,6 +25,10 @@ #include "Scheduler.h" #include "Device.h" +#ifndef HAL_SPI_SCK_SAVE_RESTORE +#define HAL_SPI_SCK_SAVE_RESTORE TRUE +#endif + namespace ChibiOS { class SPIBus : public DeviceBus { @@ -54,7 +58,9 @@ class SPIBus : public DeviceBus { bool spi_started; // mode line for SCK pin +#if HAL_SPI_SCK_SAVE_RESTORE iomode_t sck_mode; +#endif }; struct SPIDesc {