Skip to content

Commit

Permalink
spi initialization: clean-up, rework, no logic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dron0gus committed Mar 3, 2024
1 parent 598a5a6 commit c284dea
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 76 deletions.
1 change: 0 additions & 1 deletion firmware/controllers/system/efi_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ const char *portname(ioportid_t GPIOx);

#endif /* EFI_GPIO_HARDWARE */

void printSpiConfig(const char *msg, spi_device_e device);
brain_pin_e parseBrainPin(const char *str);

extern EnginePins enginePins;
Expand Down
145 changes: 78 additions & 67 deletions firmware/hw_layer/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,47 @@ brain_pin_e getSckPin(spi_device_e device) {
return Gpio::Unassigned;
}

/**
* @return NULL if SPI device not specified
*/
SPIDriver * getSpiDevice(spi_device_e spiDevice) {
if (spiDevice == SPI_NONE) {
return NULL;
}
#if STM32_SPI_USE_SPI1
if (spiDevice == SPI_DEVICE_1) {
return &SPID1;
}
#endif
#if STM32_SPI_USE_SPI2
if (spiDevice == SPI_DEVICE_2) {
return &SPID2;
}
#endif
#if STM32_SPI_USE_SPI3
if (spiDevice == SPI_DEVICE_3) {
return &SPID3;
}
#endif
#if STM32_SPI_USE_SPI4
if (spiDevice == SPI_DEVICE_4) {
return &SPID4;
}
#endif
#if STM32_SPI_USE_SPI5
if (spiDevice == SPI_DEVICE_5) {
return &SPID5;
}
#endif
#if STM32_SPI_USE_SPI6
if (spiDevice == SPI_DEVICE_6) {
return &SPID6;
}
#endif
firmwareError(ObdCode::CUSTOM_ERR_UNEXPECTED_SPI, "Unexpected SPI device: %d", spiDevice);
return NULL;
}

/**
* Only one consumer can use SPI bus at a given time
*/
Expand Down Expand Up @@ -172,46 +213,48 @@ static void initSpiModules() {
}
}

/**
* @return NULL if SPI device not specified
*/
SPIDriver * getSpiDevice(spi_device_e spiDevice) {
if (spiDevice == SPI_NONE) {
return NULL;
void stopSpi(spi_device_e device) {
if (!isSpiInitialized[device]) {
return; // not turned on
}
#if STM32_SPI_USE_SPI1
if (spiDevice == SPI_DEVICE_1) {
return &SPID1;
isSpiInitialized[device] = false;
efiSetPadUnused(getSckPin(device));
efiSetPadUnused(getMisoPin(device));
efiSetPadUnused(getMosiPin(device));
}

static void stopSpiModules() {
if (isConfigurationChanged(is_enabled_spi_1)) {
stopSpi(SPI_DEVICE_1);
}
#endif
#if STM32_SPI_USE_SPI2
if (spiDevice == SPI_DEVICE_2) {
return &SPID2;

if (isConfigurationChanged(is_enabled_spi_2)) {
stopSpi(SPI_DEVICE_2);
}
#endif
#if STM32_SPI_USE_SPI3
if (spiDevice == SPI_DEVICE_3) {
return &SPID3;

if (isConfigurationChanged(is_enabled_spi_3)) {
stopSpi(SPI_DEVICE_3);
}
#endif
#if STM32_SPI_USE_SPI4
if (spiDevice == SPI_DEVICE_4) {
return &SPID4;

if (isConfigurationChanged(is_enabled_spi_4)) {
stopSpi(SPI_DEVICE_4);
}
#endif
#if STM32_SPI_USE_SPI5
if (spiDevice == SPI_DEVICE_5) {
return &SPID5;

if (isConfigurationChanged(is_enabled_spi_5)) {
stopSpi(SPI_DEVICE_5);
}
#endif
#if STM32_SPI_USE_SPI6
if (spiDevice == SPI_DEVICE_6) {
return &SPID6;

if (isConfigurationChanged(is_enabled_spi_6)) {
stopSpi(SPI_DEVICE_6);
}
#endif
firmwareError(ObdCode::CUSTOM_ERR_UNEXPECTED_SPI, "Unexpected SPI device: %d", spiDevice);
return NULL;
}

void printSpiConfig(const char *msg, spi_device_e device) {
efiPrintf("%s %s mosi=%s", msg, getSpi_device_e(device), hwPortname(getMosiPin(device)));
efiPrintf("%s %s miso=%s", msg, getSpi_device_e(device), hwPortname(getMisoPin(device)));
efiPrintf("%s %s sck=%s", msg, getSpi_device_e(device), hwPortname(getSckPin(device)));
}

#endif // HAL_USE_SPI

#if HAL_USE_ADC
Expand Down Expand Up @@ -291,18 +334,6 @@ static void adcConfigListener() {
calcFastAdcIndexes();
}

void stopSpi(spi_device_e device) {
#if HAL_USE_SPI
if (!isSpiInitialized[device]) {
return; // not turned on
}
isSpiInitialized[device] = false;
efiSetPadUnused(getSckPin(device));
efiSetPadUnused(getMisoPin(device));
efiSetPadUnused(getMosiPin(device));
#endif /* HAL_USE_SPI */
}

/**
* this method is NOT currently invoked on ECU start
* todo: reduce code duplication by moving more logic into startHardware method
Expand Down Expand Up @@ -347,29 +378,9 @@ void applyNewHardwareSettings() {

stopHardware();

if (isConfigurationChanged(is_enabled_spi_1)) {
stopSpi(SPI_DEVICE_1);
}

if (isConfigurationChanged(is_enabled_spi_2)) {
stopSpi(SPI_DEVICE_2);
}

if (isConfigurationChanged(is_enabled_spi_3)) {
stopSpi(SPI_DEVICE_3);
}

if (isConfigurationChanged(is_enabled_spi_4)) {
stopSpi(SPI_DEVICE_4);
}

if (isConfigurationChanged(is_enabled_spi_5)) {
stopSpi(SPI_DEVICE_5);
}

if (isConfigurationChanged(is_enabled_spi_6)) {
stopSpi(SPI_DEVICE_6);
}
#if HAL_USE_SPI
stopSpiModules();
#endif /* HAL_USE_SPI */

if (isPinOrModeChanged(clutchUpPin, clutchUpPinMode)) {
// bug? duplication with stopSwitchPins?
Expand Down
2 changes: 2 additions & 0 deletions firmware/hw_layer/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ brain_pin_e getMisoPin(spi_device_e device);
brain_pin_e getMosiPin(spi_device_e device);
brain_pin_e getSckPin(spi_device_e device);

void printSpiConfig(const char *msg, spi_device_e device);

#endif /* HAL_USE_SPI */

void applyNewHardwareSettings();
Expand Down
8 changes: 0 additions & 8 deletions firmware/hw_layer/pin_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,6 @@ static void reportPins() {
efiPrintf("Total pins used: %d", totalPinsUsed);
}

void printSpiConfig(const char *msg, spi_device_e device) {
#if HAL_USE_SPI
efiPrintf("%s %s mosi=%s", msg, getSpi_device_e(device), hwPortname(getMosiPin(device)));
efiPrintf("%s %s miso=%s", msg, getSpi_device_e(device), hwPortname(getMisoPin(device)));
efiPrintf("%s %s sck=%s", msg, getSpi_device_e(device), hwPortname(getSckPin(device)));
#endif // HAL_USE_SPI
}

__attribute__((weak)) const char * getBoardSpecificPinName(brain_pin_e /*brainPin*/) {
return nullptr;
}
Expand Down

0 comments on commit c284dea

Please sign in to comment.