SPI ComIF updates

1. Make setting a chip select pin optional
2. Make ComIF member functions public
This commit is contained in:
Robin Müller 2021-10-27 17:23:14 +02:00
parent da42edcc0c
commit 3448a8c01b
2 changed files with 26 additions and 15 deletions

View File

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

View File

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