Skip to content

Commit

Permalink
AP_HAL_ChibiOS: add option to set HAL_SPI_SCK_SAVE_RESTORE
Browse files Browse the repository at this point in the history
  • Loading branch information
bugobliterator committed Oct 18, 2023
1 parent 10dfcf1 commit aaec330
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libraries/AP_HAL_ChibiOS/SPIDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/*
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions libraries/AP_HAL_ChibiOS/SPIDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit aaec330

Please sign in to comment.