polling transfer worked

This commit is contained in:
Robin Müller 2021-06-03 22:07:59 +02:00
parent ff7421e1dd
commit 20815c1d6b
No known key found for this signature in database
GPG Key ID: BE6480244DFE612C
2 changed files with 59 additions and 10 deletions

View File

@ -17,9 +17,15 @@ DMA_HandleTypeDef txDmaHandle;
DMA_HandleTypeDef rxDmaHandle;
GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transferMode):
spiHandle(spiHandle), transferMode(transferMode) {
set_dma_handles(&txDmaHandle, &rxDmaHandle);
set_spi_msp_functions(&hal_spi_msp_init_dma, spiHandle, &hal_spi_msp_deinit_dma, spiHandle);
spiHandle(spiHandle), transferMode(transferMode) {
if(transferMode == spi::TransferModes::DMA) {
set_dma_handles(&txDmaHandle, &rxDmaHandle);
set_spi_msp_functions(&hal_spi_msp_init_dma, spiHandle, &hal_spi_msp_deinit_dma, spiHandle);
}
else if(transferMode == spi::TransferModes::POLLING) {
set_spi_msp_functions(&hal_spi_msp_init_polling, spiHandle,
&hal_spi_msp_deinit_polling, spiHandle);
}
GPIO_InitTypeDef chipSelect = {};
__HAL_RCC_GPIOD_CLK_ENABLE();
chipSelect.Pin = GPIO_PIN_14;
@ -53,13 +59,8 @@ ReturnValue_t GyroL3GD20H::initialize() {
}
ReturnValue_t GyroL3GD20H::performOperation() {
transferState = TransferStates::WAIT;
const uint8_t WHO_AM_I_REG = 0b00001111;
const uint8_t STM_READ_MASK = 0b10000000;
const uint8_t EXPECTED_WHO_AM_I_VAL = 0b11010111;
sif::printInfo("GyroL3GD20H::performOperation: Reading WHO AM I register\n");
txBuffer[0] = WHO_AM_I_REG | STM_READ_MASK;
@ -67,6 +68,21 @@ ReturnValue_t GyroL3GD20H::performOperation() {
// Start SPI transfer via DMA
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
switch(transferMode) {
case(spi::TransferModes::DMA): {
return handleDmaTransfer();
}
case(spi::TransferModes::POLLING): {
return handlePollingTransfer();
}
default: {
}
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t GyroL3GD20H::handleDmaTransfer() {
if(HAL_SPI_TransmitReceive_DMA(spiHandle, txBuffer.data(), rxBuffer.data(), 2) != HAL_OK) {
// Transfer error in transmission process
sif::printWarning("Error transmitting SPI with DMA\n");
@ -82,7 +98,7 @@ ReturnValue_t GyroL3GD20H::performOperation() {
switch(transferState) {
case(TransferStates::SUCCESS): {
sif::printInfo("Transfer success\n");
sif::printInfo("DMA transfer success\n");
uint8_t whoAmIVal = rxBuffer[1];
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
sif::printDebug("GyroL3GD20H::performOperation: "
@ -97,7 +113,7 @@ ReturnValue_t GyroL3GD20H::performOperation() {
break;
}
default: {
break;
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
@ -125,3 +141,29 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
transferState = TransferStates::FAILURE;
}
ReturnValue_t GyroL3GD20H::handlePollingTransfer() {
switch(HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 2, 1000)) {
case(HAL_OK): {
sif::printInfo("Polling transfer success\n");
uint8_t whoAmIVal = rxBuffer[1];
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
sif::printDebug("GyroL3GD20H::performOperation: "
"Read WHO AM I value %d not equal to expected value!\n", whoAmIVal);
}
break;
}
case(HAL_TIMEOUT): {
sif::printDebug("Polling transfer timeout\n");
return HasReturnvaluesIF::RETURN_FAILED;
}
case(HAL_ERROR): {
sif::printDebug("Polling transfer failure\n");
return HasReturnvaluesIF::RETURN_FAILED;
}
default: {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -26,11 +26,18 @@ public:
private:
const uint8_t WHO_AM_I_REG = 0b00001111;
const uint8_t STM_READ_MASK = 0b10000000;
const uint8_t EXPECTED_WHO_AM_I_VAL = 0b11010111;
SPI_HandleTypeDef* spiHandle;
spi::TransferModes transferMode;
static constexpr size_t recvBufferSize = 32 * 10;
static std::array<uint8_t, recvBufferSize> rxBuffer;
std::array<uint8_t, 32> txBuffer;
ReturnValue_t handleDmaTransfer();
ReturnValue_t handlePollingTransfer();
};
#endif /* FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ */