Merge pull request 'STM32 SPI Updates' (#518) from mueller/stm32-spi-updates into development

Reviewed-on: fsfw/fsfw#518
This commit is contained in:
Steffen Gaisser 2021-11-08 14:52:28 +01:00
commit 9a38106b57
11 changed files with 138 additions and 80 deletions

View File

@ -0,0 +1,25 @@
#ifndef FSFW_HAL_STM32H7_DEFINITIONS_H_
#define FSFW_HAL_STM32H7_DEFINITIONS_H_
#include <utility>
#include "stm32h7xx.h"
namespace stm32h7 {
/**
* Typedef for STM32 GPIO pair where the first entry is the port used (e.g. GPIOA)
* and the second entry is the pin number
*/
struct GpioCfg {
GpioCfg(): port(nullptr), pin(0), altFnc(0) {};
GpioCfg(GPIO_TypeDef* port, uint16_t pin, uint8_t altFnc = 0):
port(port), pin(pin), altFnc(altFnc) {};
GPIO_TypeDef* port;
uint16_t pin;
uint8_t altFnc;
};
}
#endif /* #ifndef FSFW_HAL_STM32H7_DEFINITIONS_H_ */

View File

@ -4,7 +4,7 @@
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "fsfw_hal/stm32h7/spi/stm32h743ziSpi.h"
#include "fsfw_hal/stm32h7/spi/stm32h743zi.h"
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
@ -33,20 +33,20 @@ GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transf
mspCfg = new spi::MspDmaConfigStruct();
auto typedCfg = dynamic_cast<spi::MspDmaConfigStruct*>(mspCfg);
spi::setDmaHandles(txDmaHandle, rxDmaHandle);
spi::h743zi::standardDmaCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS,
stm32h7::h743zi::standardDmaCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS,
IrqPriorities::HIGHEST_FREERTOS, IrqPriorities::HIGHEST_FREERTOS);
spi::setSpiDmaMspFunctions(typedCfg);
}
else if(transferMode == spi::TransferModes::INTERRUPT) {
mspCfg = new spi::MspIrqConfigStruct();
auto typedCfg = dynamic_cast<spi::MspIrqConfigStruct*>(mspCfg);
spi::h743zi::standardInterruptCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS);
stm32h7::h743zi::standardInterruptCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS);
spi::setSpiIrqMspFunctions(typedCfg);
}
else if(transferMode == spi::TransferModes::POLLING) {
mspCfg = new spi::MspPollingConfigStruct();
auto typedCfg = dynamic_cast<spi::MspPollingConfigStruct*>(mspCfg);
spi::h743zi::standardPollingCfg(*typedCfg);
stm32h7::h743zi::standardPollingCfg(*typedCfg);
spi::setSpiPollingMspFunctions(typedCfg);
}

View File

@ -5,5 +5,5 @@ target_sources(${LIB_FSFW_NAME} PRIVATE
mspInit.cpp
SpiCookie.cpp
SpiComIF.cpp
stm32h743ziSpi.cpp
stm32h743zi.cpp
)

View File

@ -138,12 +138,14 @@ ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
spi::setSpiDmaMspFunctions(typedCfg);
}
gpio::initializeGpioClock(gpioPort);
GPIO_InitTypeDef chipSelect = {};
chipSelect.Pin = gpioPin;
chipSelect.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(gpioPort, &chipSelect);
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
if(gpioPort != nullptr) {
gpio::initializeGpioClock(gpioPort);
GPIO_InitTypeDef chipSelect = {};
chipSelect.Pin = gpioPin;
chipSelect.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(gpioPort, &chipSelect);
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
}
if(HAL_SPI_Init(&spiHandle) != HAL_OK) {
sif::printWarning("SpiComIF::initialize: Error initializing SPI\n");
@ -259,10 +261,15 @@ ReturnValue_t SpiComIF::handlePollingSendOperation(uint8_t* recvPtr, SPI_HandleT
return returnval;
}
spiCookie.setTransferState(spi::TransferStates::WAIT);
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
if(gpioPort != nullptr) {
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
}
auto result = HAL_SPI_TransmitReceive(&spiHandle, const_cast<uint8_t*>(sendData),
recvPtr, sendLen, defaultPollingTimeout);
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
if(gpioPort != nullptr) {
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
}
spiSemaphore->release();
switch(result) {
case(HAL_OK): {
@ -392,8 +399,10 @@ ReturnValue_t SpiComIF::genericIrqSendSetup(uint8_t *recvPtr, SPI_HandleTypeDef&
// The SPI handle is passed to the default SPI callback as a void argument. This callback
// is different from the user callbacks specified above!
spi::assignSpiUserArgs(spiCookie.getSpiIdx(), reinterpret_cast<void*>(&spiHandle));
HAL_GPIO_WritePin(spiCookie.getChipSelectGpioPort(), spiCookie.getChipSelectGpioPin(),
GPIO_PIN_RESET);
if(spiCookie.getChipSelectGpioPort() != nullptr) {
HAL_GPIO_WritePin(spiCookie.getChipSelectGpioPort(), spiCookie.getChipSelectGpioPin(),
GPIO_PIN_RESET);
}
return HasReturnvaluesIF::RETURN_OK;
}
@ -426,9 +435,12 @@ void SpiComIF::genericIrqHandler(void *irqArgsVoid, spi::TransferStates targetSt
spiCookie->setTransferState(targetState);
// Pull CS pin high again
HAL_GPIO_WritePin(spiCookie->getChipSelectGpioPort(), spiCookie->getChipSelectGpioPin(),
GPIO_PIN_SET);
if(spiCookie->getChipSelectGpioPort() != nullptr) {
// Pull CS pin high again
HAL_GPIO_WritePin(spiCookie->getChipSelectGpioPort(), spiCookie->getChipSelectGpioPin(),
GPIO_PIN_SET);
}
#if defined FSFW_OSAL_FREERTOS
// Release the task semaphore

View File

@ -60,7 +60,6 @@ public:
void addDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle);
ReturnValue_t initialize() override;
protected:
// DeviceCommunicationIF overrides
virtual ReturnValue_t initializeInterface(CookieIF * cookie) override;
@ -72,7 +71,7 @@ protected:
virtual ReturnValue_t readReceivedMessage(CookieIF *cookie,
uint8_t **buffer, size_t *size) override;
private:
protected:
struct SpiInstance {
SpiInstance(size_t maxRecvSize): replyBuffer(std::vector<uint8_t>(maxRecvSize)) {}

View File

@ -3,10 +3,10 @@
SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode,
uint16_t chipSelectGpioPin, GPIO_TypeDef* chipSelectGpioPort, size_t maxRecvSize):
size_t maxRecvSize, stm32h7::GpioCfg csGpio):
deviceAddress(deviceAddress), spiIdx(spiIdx), spiSpeed(spiSpeed), spiMode(spiMode),
transferMode(transferMode), chipSelectGpioPin(chipSelectGpioPin),
chipSelectGpioPort(chipSelectGpioPort), mspCfg(mspCfg), maxRecvSize(maxRecvSize) {
transferMode(transferMode), csGpio(csGpio),
mspCfg(mspCfg), maxRecvSize(maxRecvSize) {
spiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
spiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
spiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
@ -24,11 +24,11 @@ SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferM
}
uint16_t SpiCookie::getChipSelectGpioPin() const {
return chipSelectGpioPin;
return csGpio.pin;
}
GPIO_TypeDef* SpiCookie::getChipSelectGpioPort() {
return chipSelectGpioPort;
return csGpio.port;
}
address_t SpiCookie::getDeviceAddress() const {

View File

@ -3,11 +3,14 @@
#include "spiDefinitions.h"
#include "mspInit.h"
#include "../definitions.h"
#include "fsfw/devicehandlers/CookieIF.h"
#include "stm32h743xx.h"
#include <utility>
/**
* @brief SPI cookie implementation for the STM32H7 device family
* @details
@ -18,6 +21,7 @@
class SpiCookie: public CookieIF {
friend class SpiComIF;
public:
/**
* Allows construction of a SPI cookie for a connected SPI device
* @param deviceAddress
@ -32,10 +36,11 @@ public:
* definitions supplied in the MCU header file! (e.g. GPIO_PIN_X)
* @param chipSelectGpioPort GPIO port (e.g. GPIOA)
* @param maxRecvSize Maximum expected receive size. Chose as small as possible.
* @param csGpio Optional CS GPIO definition.
*/
SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode,
uint16_t chipSelectGpioPin, GPIO_TypeDef* chipSelectGpioPort, size_t maxRecvSize);
size_t maxRecvSize, stm32h7::GpioCfg csGpio = stm32h7::GpioCfg(nullptr, 0, 0));
uint16_t getChipSelectGpioPin() const;
GPIO_TypeDef* getChipSelectGpioPort();
@ -55,8 +60,8 @@ private:
spi::SpiModes spiMode;
spi::TransferModes transferMode;
volatile spi::TransferStates transferState = spi::TransferStates::IDLE;
uint16_t chipSelectGpioPin;
GPIO_TypeDef* chipSelectGpioPort;
stm32h7::GpioCfg csGpio;
// The MSP configuration is cached here. Be careful when using this, it is automatically
// deleted by the SPI communication interface if it is not required anymore!
spi::MspCfgBase* mspCfg = nullptr;

View File

@ -118,40 +118,40 @@ void spi::halMspInitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
GPIO_InitTypeDef GPIO_InitStruct = {};
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
cfg->setupMacroWrapper();
cfg->setupCb();
/*##-2- Configure peripheral GPIO ##########################################*/
/* SPI SCK GPIO pin configuration */
GPIO_InitStruct.Pin = cfg->sckPin;
GPIO_InitStruct.Pin = cfg->sck.pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = cfg->sckAlternateFunction;
HAL_GPIO_Init(cfg->sckPort, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = cfg->sck.altFnc;
HAL_GPIO_Init(cfg->sck.port, &GPIO_InitStruct);
/* SPI MISO GPIO pin configuration */
GPIO_InitStruct.Pin = cfg->misoPin;
GPIO_InitStruct.Alternate = cfg->misoAlternateFunction;
HAL_GPIO_Init(cfg->misoPort, &GPIO_InitStruct);
GPIO_InitStruct.Pin = cfg->miso.pin;
GPIO_InitStruct.Alternate = cfg->miso.altFnc;
HAL_GPIO_Init(cfg->miso.port, &GPIO_InitStruct);
/* SPI MOSI GPIO pin configuration */
GPIO_InitStruct.Pin = cfg->mosiPin;
GPIO_InitStruct.Alternate = cfg->mosiAlternateFunction;
HAL_GPIO_Init(cfg->mosiPort, &GPIO_InitStruct);
GPIO_InitStruct.Pin = cfg->mosi.pin;
GPIO_InitStruct.Alternate = cfg->mosi.altFnc;
HAL_GPIO_Init(cfg->mosi.port, &GPIO_InitStruct);
}
void spi::halMspDeinitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = reinterpret_cast<MspPollingConfigStruct*>(cfgBase);
// Reset peripherals
cfg->cleanUpMacroWrapper();
cfg->cleanupCb();
// Disable peripherals and GPIO Clocks
/* Configure SPI SCK as alternate function */
HAL_GPIO_DeInit(cfg->sckPort, cfg->sckPin);
HAL_GPIO_DeInit(cfg->sck.port, cfg->sck.pin);
/* Configure SPI MISO as alternate function */
HAL_GPIO_DeInit(cfg->misoPort, cfg->misoPin);
HAL_GPIO_DeInit(cfg->miso.port, cfg->miso.pin);
/* Configure SPI MOSI as alternate function */
HAL_GPIO_DeInit(cfg->mosiPort, cfg->mosiPin);
HAL_GPIO_DeInit(cfg->mosi.port, cfg->mosi.pin);
}
void spi::halMspInitInterrupt(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {

View File

@ -2,6 +2,7 @@
#define FSFW_HAL_STM32H7_SPI_MSPINIT_H_
#include "spiDefinitions.h"
#include "../definitions.h"
#include "../dma.h"
#include "stm32h7xx_hal_spi.h"
@ -12,6 +13,8 @@
extern "C" {
#endif
using mspCb = void (*) (void);
/**
* @brief This file provides MSP implementation for DMA, IRQ and Polling mode for the
* SPI peripheral. This configuration is required for the SPI communication to work.
@ -19,27 +22,37 @@ extern "C" {
namespace spi {
struct MspCfgBase {
MspCfgBase();
MspCfgBase(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr):
sck(sck), mosi(mosi), miso(miso), cleanupCb(cleanupCb),
setupCb(setupCb) {}
virtual ~MspCfgBase() = default;
void (* cleanUpMacroWrapper) (void) = nullptr;
void (* setupMacroWrapper) (void) = nullptr;
stm32h7::GpioCfg sck;
stm32h7::GpioCfg mosi;
stm32h7::GpioCfg miso;
GPIO_TypeDef* sckPort = nullptr;
uint32_t sckPin = 0;
uint8_t sckAlternateFunction = 0;
GPIO_TypeDef* mosiPort = nullptr;
uint32_t mosiPin = 0;
uint8_t mosiAlternateFunction = 0;
GPIO_TypeDef* misoPort = nullptr;
uint32_t misoPin = 0;
uint8_t misoAlternateFunction = 0;
mspCb cleanupCb = nullptr;
mspCb setupCb = nullptr;
};
struct MspPollingConfigStruct: public MspCfgBase {};
struct MspPollingConfigStruct: public MspCfgBase {
MspPollingConfigStruct(): MspCfgBase() {};
MspPollingConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr):
MspCfgBase(sck, mosi, miso, cleanupCb, setupCb) {}
};
/* A valid instance of this struct must be passed to the MSP initialization function as a void*
argument */
struct MspIrqConfigStruct: public MspPollingConfigStruct {
MspIrqConfigStruct(): MspPollingConfigStruct() {};
MspIrqConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr):
MspPollingConfigStruct(sck, mosi, miso, cleanupCb, setupCb) {}
SpiBus spiBus = SpiBus::SPI_1;
user_handler_t spiIrqHandler = nullptr;
user_args_t spiUserArgs = nullptr;
@ -53,11 +66,16 @@ struct MspIrqConfigStruct: public MspPollingConfigStruct {
/* A valid instance of this struct must be passed to the MSP initialization function as a void*
argument */
struct MspDmaConfigStruct: public MspIrqConfigStruct {
MspDmaConfigStruct(): MspIrqConfigStruct() {};
MspDmaConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr):
MspIrqConfigStruct(sck, mosi, miso, cleanupCb, setupCb) {}
void (* dmaClkEnableWrapper) (void) = nullptr;
dma::DMAIndexes txDmaIndex;
dma::DMAIndexes rxDmaIndex;
dma::DMAStreams txDmaStream;
dma::DMAStreams rxDmaStream;
dma::DMAIndexes txDmaIndex = dma::DMAIndexes::DMA_1;
dma::DMAIndexes rxDmaIndex = dma::DMAIndexes::DMA_1;
dma::DMAStreams txDmaStream = dma::DMAStreams::STREAM_0;
dma::DMAStreams rxDmaStream = dma::DMAStreams::STREAM_0;
IRQn_Type txDmaIrqNumber = DMA1_Stream0_IRQn;
IRQn_Type rxDmaIrqNumber = DMA1_Stream1_IRQn;
// Priorities for NVIC

View File

@ -1,4 +1,4 @@
#include "fsfw_hal/stm32h7/spi/stm32h743ziSpi.h"
#include "fsfw_hal/stm32h7/spi/stm32h743zi.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
@ -22,27 +22,27 @@ void spiDmaClockEnableWrapper() {
__HAL_RCC_DMA2_CLK_ENABLE();
}
void spi::h743zi::standardPollingCfg(MspPollingConfigStruct& cfg) {
cfg.setupMacroWrapper = &spiSetupWrapper;
cfg.cleanUpMacroWrapper = &spiCleanUpWrapper;
cfg.sckPort = GPIOA;
cfg.sckPin = GPIO_PIN_5;
cfg.misoPort = GPIOA;
cfg.misoPin = GPIO_PIN_6;
cfg.mosiPort = GPIOA;
cfg.mosiPin = GPIO_PIN_7;
cfg.sckAlternateFunction = GPIO_AF5_SPI1;
cfg.mosiAlternateFunction = GPIO_AF5_SPI1;
cfg.misoAlternateFunction = GPIO_AF5_SPI1;
void stm32h7::h743zi::standardPollingCfg(spi::MspPollingConfigStruct& cfg) {
cfg.setupCb = &spiSetupWrapper;
cfg.cleanupCb = &spiCleanUpWrapper;
cfg.sck.port = GPIOA;
cfg.sck.pin = GPIO_PIN_5;
cfg.miso.port = GPIOA;
cfg.miso.pin = GPIO_PIN_6;
cfg.mosi.port = GPIOA;
cfg.mosi.pin = GPIO_PIN_7;
cfg.sck.altFnc = GPIO_AF5_SPI1;
cfg.mosi.altFnc = GPIO_AF5_SPI1;
cfg.miso.altFnc = GPIO_AF5_SPI1;
}
void spi::h743zi::standardInterruptCfg(MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,
void stm32h7::h743zi::standardInterruptCfg(spi::MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities spiSubprio) {
// High, but works on FreeRTOS as well (priorities range from 0 to 15)
cfg.preEmptPriority = spiIrqPrio;
cfg.subpriority = spiSubprio;
cfg.spiIrqNumber = SPI1_IRQn;
cfg.spiBus = SpiBus::SPI_1;
cfg.spiBus = spi::SpiBus::SPI_1;
user_handler_t spiUserHandler = nullptr;
user_args_t spiUserArgs = nullptr;
getSpiUserHandler(spi::SpiBus::SPI_1, &spiUserHandler, &spiUserArgs);
@ -55,7 +55,7 @@ void spi::h743zi::standardInterruptCfg(MspIrqConfigStruct& cfg, IrqPriorities sp
standardPollingCfg(cfg);
}
void spi::h743zi::standardDmaCfg(MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio,
void stm32h7::h743zi::standardDmaCfg(spi::MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities txIrqPrio, IrqPriorities rxIrqPrio, IrqPriorities spiSubprio,
IrqPriorities txSubprio, IrqPriorities rxSubprio) {
cfg.dmaClkEnableWrapper = &spiDmaClockEnableWrapper;

View File

@ -3,21 +3,20 @@
#include "mspInit.h"
namespace spi {
namespace stm32h7 {
namespace h743zi {
void standardPollingCfg(MspPollingConfigStruct& cfg);
void standardInterruptCfg(MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,
void standardPollingCfg(spi::MspPollingConfigStruct& cfg);
void standardInterruptCfg(spi::MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities spiSubprio = HIGHEST);
void standardDmaCfg(MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio,
void standardDmaCfg(spi::MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities txIrqPrio, IrqPriorities rxIrqPrio,
IrqPriorities spiSubprio = HIGHEST, IrqPriorities txSubPrio = HIGHEST,
IrqPriorities rxSubprio = HIGHEST);
}
}
#endif /* FSFW_HAL_STM32H7_SPI_STM32H743ZISPI_H_ */