msp init improvements

This commit is contained in:
Robin Müller 2021-10-27 18:05:18 +02:00
parent d675621b73
commit cb7399b999
6 changed files with 90 additions and 51 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

@ -3,7 +3,7 @@
SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode, SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode, spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode,
size_t maxRecvSize, GpioPair csGpio): size_t maxRecvSize, stm32h7::GpioCfg csGpio):
deviceAddress(deviceAddress), spiIdx(spiIdx), spiSpeed(spiSpeed), spiMode(spiMode), deviceAddress(deviceAddress), spiIdx(spiIdx), spiSpeed(spiSpeed), spiMode(spiMode),
transferMode(transferMode), csGpio(csGpio), transferMode(transferMode), csGpio(csGpio),
mspCfg(mspCfg), maxRecvSize(maxRecvSize) { mspCfg(mspCfg), maxRecvSize(maxRecvSize) {
@ -24,11 +24,11 @@ SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferM
} }
uint16_t SpiCookie::getChipSelectGpioPin() const { uint16_t SpiCookie::getChipSelectGpioPin() const {
return csGpio.second; return csGpio.pin;
} }
GPIO_TypeDef* SpiCookie::getChipSelectGpioPort() { GPIO_TypeDef* SpiCookie::getChipSelectGpioPort() {
return csGpio.first; return csGpio.port;
} }
address_t SpiCookie::getDeviceAddress() const { address_t SpiCookie::getDeviceAddress() const {

View File

@ -3,6 +3,7 @@
#include "spiDefinitions.h" #include "spiDefinitions.h"
#include "mspInit.h" #include "mspInit.h"
#include "../definitions.h"
#include "fsfw/devicehandlers/CookieIF.h" #include "fsfw/devicehandlers/CookieIF.h"
@ -20,11 +21,6 @@
class SpiCookie: public CookieIF { class SpiCookie: public CookieIF {
friend class SpiComIF; friend class SpiComIF;
public: public:
/**
* Typedef for STM32 GPIO pair where the first entry is the port used (e.g. GPIOA)
* and the second entry is the pin number
*/
using GpioPair = std::pair<GPIO_TypeDef*, uint16_t>;
/** /**
* Allows construction of a SPI cookie for a connected SPI device * Allows construction of a SPI cookie for a connected SPI device
@ -44,7 +40,7 @@ public:
*/ */
SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode, SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode, spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode,
size_t maxRecvSize, GpioPair csGpio = GpioPair(nullptr, 0)); size_t maxRecvSize, stm32h7::GpioCfg csGpio = stm32h7::GpioCfg(nullptr, 0, 0));
uint16_t getChipSelectGpioPin() const; uint16_t getChipSelectGpioPin() const;
GPIO_TypeDef* getChipSelectGpioPort(); GPIO_TypeDef* getChipSelectGpioPort();
@ -64,7 +60,7 @@ private:
spi::SpiModes spiMode; spi::SpiModes spiMode;
spi::TransferModes transferMode; spi::TransferModes transferMode;
volatile spi::TransferStates transferState = spi::TransferStates::IDLE; volatile spi::TransferStates transferState = spi::TransferStates::IDLE;
GpioPair csGpio; stm32h7::GpioCfg csGpio;
// The MSP configuration is cached here. Be careful when using this, it is automatically // 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! // deleted by the SPI communication interface if it is not required anymore!

View File

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

View File

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

View File

@ -23,17 +23,17 @@ void spiDmaClockEnableWrapper() {
} }
void spi::h743zi::standardPollingCfg(MspPollingConfigStruct& cfg) { void spi::h743zi::standardPollingCfg(MspPollingConfigStruct& cfg) {
cfg.setupMacroWrapper = &spiSetupWrapper; cfg.setupCb = &spiSetupWrapper;
cfg.cleanUpMacroWrapper = &spiCleanUpWrapper; cfg.cleanupCb = &spiCleanUpWrapper;
cfg.sckPort = GPIOA; cfg.sck.port = GPIOA;
cfg.sckPin = GPIO_PIN_5; cfg.sck.pin = GPIO_PIN_5;
cfg.misoPort = GPIOA; cfg.miso.port = GPIOA;
cfg.misoPin = GPIO_PIN_6; cfg.miso.pin = GPIO_PIN_6;
cfg.mosiPort = GPIOA; cfg.mosi.port = GPIOA;
cfg.mosiPin = GPIO_PIN_7; cfg.mosi.pin = GPIO_PIN_7;
cfg.sckAlternateFunction = GPIO_AF5_SPI1; cfg.sck.altFnc = GPIO_AF5_SPI1;
cfg.mosiAlternateFunction = GPIO_AF5_SPI1; cfg.mosi.altFnc = GPIO_AF5_SPI1;
cfg.misoAlternateFunction = GPIO_AF5_SPI1; cfg.miso.altFnc = GPIO_AF5_SPI1;
} }
void spi::h743zi::standardInterruptCfg(MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio, void spi::h743zi::standardInterruptCfg(MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,