fsfw/src/fsfw_hal/stm32h7/spi/SpiCookie.cpp

61 lines
2.1 KiB
C++
Raw Normal View History

2021-08-02 20:58:56 +02:00
#include "fsfw_hal/stm32h7/spi/SpiCookie.h"
2021-07-13 19:19:25 +02:00
SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
2022-02-02 10:29:30 +01:00
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode,
size_t maxRecvSize, stm32h7::GpioCfg csGpio)
: deviceAddress(deviceAddress),
spiIdx(spiIdx),
spiSpeed(spiSpeed),
spiMode(spiMode),
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;
spiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
spiHandle.Init.CRCPolynomial = 7;
spiHandle.Init.CRCLength = SPI_CRC_LENGTH_8BIT;
spiHandle.Init.NSS = SPI_NSS_SOFT;
spiHandle.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
spiHandle.Init.Direction = SPI_DIRECTION_2LINES;
// Recommended setting to avoid glitches
spiHandle.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
spiHandle.Init.Mode = SPI_MODE_MASTER;
spi::assignSpiMode(spiMode, spiHandle);
spiHandle.Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), spiSpeed);
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
uint16_t SpiCookie::getChipSelectGpioPin() const { return csGpio.pin; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
GPIO_TypeDef* SpiCookie::getChipSelectGpioPort() { return csGpio.port; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
address_t SpiCookie::getDeviceAddress() const { return deviceAddress; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
spi::SpiBus SpiCookie::getSpiIdx() const { return spiIdx; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
spi::SpiModes SpiCookie::getSpiMode() const { return spiMode; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
uint32_t SpiCookie::getSpiSpeed() const { return spiSpeed; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
size_t SpiCookie::getMaxRecvSize() const { return maxRecvSize; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
SPI_HandleTypeDef& SpiCookie::getSpiHandle() { return spiHandle; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
spi::MspCfgBase* SpiCookie::getMspCfg() { return mspCfg; }
2021-07-13 19:19:25 +02:00
void SpiCookie::deleteMspCfg() {
2022-02-02 10:29:30 +01:00
if (mspCfg != nullptr) {
delete mspCfg;
}
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
spi::TransferModes SpiCookie::getTransferMode() const { return transferMode; }
2021-07-13 19:19:25 +02:00
void SpiCookie::setTransferState(spi::TransferStates transferState) {
2022-02-02 10:29:30 +01:00
this->transferState = transferState;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
spi::TransferStates SpiCookie::getTransferState() const { return this->transferState; }