polling transfer worked
This commit is contained in:
parent
ff7421e1dd
commit
20815c1d6b
@ -17,9 +17,15 @@ DMA_HandleTypeDef txDmaHandle;
|
|||||||
DMA_HandleTypeDef rxDmaHandle;
|
DMA_HandleTypeDef rxDmaHandle;
|
||||||
|
|
||||||
GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transferMode):
|
GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transferMode):
|
||||||
spiHandle(spiHandle), transferMode(transferMode) {
|
spiHandle(spiHandle), transferMode(transferMode) {
|
||||||
set_dma_handles(&txDmaHandle, &rxDmaHandle);
|
if(transferMode == spi::TransferModes::DMA) {
|
||||||
set_spi_msp_functions(&hal_spi_msp_init_dma, spiHandle, &hal_spi_msp_deinit_dma, spiHandle);
|
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 = {};
|
GPIO_InitTypeDef chipSelect = {};
|
||||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||||
chipSelect.Pin = GPIO_PIN_14;
|
chipSelect.Pin = GPIO_PIN_14;
|
||||||
@ -53,13 +59,8 @@ ReturnValue_t GyroL3GD20H::initialize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t GyroL3GD20H::performOperation() {
|
ReturnValue_t GyroL3GD20H::performOperation() {
|
||||||
|
|
||||||
transferState = TransferStates::WAIT;
|
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");
|
sif::printInfo("GyroL3GD20H::performOperation: Reading WHO AM I register\n");
|
||||||
|
|
||||||
txBuffer[0] = WHO_AM_I_REG | STM_READ_MASK;
|
txBuffer[0] = WHO_AM_I_REG | STM_READ_MASK;
|
||||||
@ -67,6 +68,21 @@ ReturnValue_t GyroL3GD20H::performOperation() {
|
|||||||
|
|
||||||
// Start SPI transfer via DMA
|
// Start SPI transfer via DMA
|
||||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
|
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) {
|
if(HAL_SPI_TransmitReceive_DMA(spiHandle, txBuffer.data(), rxBuffer.data(), 2) != HAL_OK) {
|
||||||
// Transfer error in transmission process
|
// Transfer error in transmission process
|
||||||
sif::printWarning("Error transmitting SPI with DMA\n");
|
sif::printWarning("Error transmitting SPI with DMA\n");
|
||||||
@ -82,7 +98,7 @@ ReturnValue_t GyroL3GD20H::performOperation() {
|
|||||||
|
|
||||||
switch(transferState) {
|
switch(transferState) {
|
||||||
case(TransferStates::SUCCESS): {
|
case(TransferStates::SUCCESS): {
|
||||||
sif::printInfo("Transfer success\n");
|
sif::printInfo("DMA transfer success\n");
|
||||||
uint8_t whoAmIVal = rxBuffer[1];
|
uint8_t whoAmIVal = rxBuffer[1];
|
||||||
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
|
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
|
||||||
sif::printDebug("GyroL3GD20H::performOperation: "
|
sif::printDebug("GyroL3GD20H::performOperation: "
|
||||||
@ -97,7 +113,7 @@ ReturnValue_t GyroL3GD20H::performOperation() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
break;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
@ -125,3 +141,29 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
|
|||||||
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
|
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
|
||||||
transferState = TransferStates::FAILURE;
|
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;
|
||||||
|
}
|
||||||
|
@ -26,11 +26,18 @@ public:
|
|||||||
|
|
||||||
private:
|
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_HandleTypeDef* spiHandle;
|
||||||
spi::TransferModes transferMode;
|
spi::TransferModes transferMode;
|
||||||
static constexpr size_t recvBufferSize = 32 * 10;
|
static constexpr size_t recvBufferSize = 32 * 10;
|
||||||
static std::array<uint8_t, recvBufferSize> rxBuffer;
|
static std::array<uint8_t, recvBufferSize> rxBuffer;
|
||||||
std::array<uint8_t, 32> txBuffer;
|
std::array<uint8_t, 32> txBuffer;
|
||||||
|
|
||||||
|
ReturnValue_t handleDmaTransfer();
|
||||||
|
ReturnValue_t handlePollingTransfer();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ */
|
#endif /* FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user