fsfw-hal/stm32h7/devicetest/GyroL3GD20H.cpp

127 lines
4.4 KiB
C++
Raw Normal View History

2021-05-31 20:40:16 +02:00
#include "GyroL3GD20H.h"
2021-06-03 14:00:50 +02:00
#include "spiConf.h"
2021-06-03 21:42:52 +02:00
#include "../spi/mspInit.h"
2021-06-03 14:00:50 +02:00
#include "../spi/spiDefinitions.h"
2021-06-03 14:38:34 +02:00
#include "../spi/spiCore.h"
2021-05-31 20:40:16 +02:00
2021-06-03 14:00:50 +02:00
#include "fsfw/tasks/TaskFactory.h"
2021-05-31 20:40:16 +02:00
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-06-03 14:00:50 +02:00
2021-05-31 20:40:16 +02:00
#include "stm32h7xx_nucleo.h"
#include "stm32h7xx_hal_spi.h"
2021-06-03 21:42:52 +02:00
#include "stm32h7xx_hal_rcc.h"
2021-05-31 20:40:16 +02:00
2021-06-03 14:00:50 +02:00
std::array<uint8_t, GyroL3GD20H::recvBufferSize> GyroL3GD20H::rxBuffer;
TransferStates transferState = TransferStates::IDLE;
DMA_HandleTypeDef txDmaHandle;
DMA_HandleTypeDef rxDmaHandle;
2021-05-31 20:40:16 +02:00
GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle): spiHandle(spiHandle) {
2021-06-03 21:42:52 +02:00
set_dma_handles(&txDmaHandle, &rxDmaHandle);
set_spi_msp_functions(&hal_spi_msp_init_dma, spiHandle, &hal_spi_msp_deinit_dma, spiHandle);
2021-06-03 16:44:02 +02:00
GPIO_InitTypeDef chipSelect = {};
2021-06-03 21:42:52 +02:00
__HAL_RCC_GPIOD_CLK_ENABLE();
2021-06-03 16:44:02 +02:00
chipSelect.Pin = GPIO_PIN_14;
chipSelect.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOD, &chipSelect);
2021-06-03 21:42:52 +02:00
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
2021-05-31 20:40:16 +02:00
}
2021-06-03 14:00:50 +02:00
ReturnValue_t GyroL3GD20H::initialize() {
// Configure the SPI peripheral
2021-05-31 20:40:16 +02:00
spiHandle->Instance = SPI1;
2021-06-03 14:03:38 +02:00
spiHandle->Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), 3900000);
2021-05-31 20:40:16 +02:00
spiHandle->Init.Direction = SPI_DIRECTION_2LINES;
2021-06-03 14:00:50 +02:00
spi::assignSpiMode(spi::SpiModes::MODE_3, spiHandle);
2021-05-31 20:40:16 +02:00
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;
2021-06-03 14:00:50 +02:00
// Recommended setting to avoid glitches
2021-05-31 20:40:16 +02:00
spiHandle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
spiHandle->Init.Mode = SPI_MODE_MASTER;
if(HAL_SPI_Init(spiHandle) != HAL_OK) {
sif::printWarning("Error initializing SPI\n");
2021-06-03 14:00:50 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t GyroL3GD20H::performOperation() {
transferState = TransferStates::WAIT;
2021-06-03 16:44:02 +02:00
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;
txBuffer[1] = 0;
2021-06-03 14:00:50 +02:00
// Start SPI transfer via DMA
2021-06-03 21:42:52 +02:00
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
2021-06-03 14:00:50 +02:00
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");
}
// Wait for the transfer to complete
while (transferState == TransferStates::WAIT) {
2021-06-03 21:42:52 +02:00
//TaskFactory::delayTask(1);
2021-06-03 14:00:50 +02:00
}
// Invalidate cache prior to access by CPU
SCB_InvalidateDCache_by_Addr ((uint32_t *)rxBuffer.data(), recvBufferSize);
switch(transferState) {
case(TransferStates::SUCCESS): {
sif::printInfo("Transfer success\n");
2021-06-03 16:44:02 +02:00
uint8_t whoAmIVal = rxBuffer[1];
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
sif::printDebug("GyroL3GD20H::performOperation: "
2021-06-03 21:42:52 +02:00
"Read WHO AM I value %d not equal to expected value!\n", whoAmIVal);
2021-06-03 16:44:02 +02:00
}
2021-06-03 14:00:50 +02:00
transferState = TransferStates::IDLE;
break;
}
case(TransferStates::FAILURE): {
sif::printWarning("Transfer failure\n");
transferState = TransferStates::FAILURE;
break;
}
default: {
break;
}
2021-05-31 20:40:16 +02:00
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-06-03 14:00:50 +02:00
/**
* @brief TxRx Transfer completed callback.
* @param hspi: SPI handle
* @note This example shows a simple way to report end of DMA TxRx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
transferState = TransferStates::SUCCESS;
2021-06-03 21:42:52 +02:00
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
2021-06-03 14:00:50 +02:00
}
/**
* @brief SPI error callbacks.
* @param hspi: SPI handle
* @note This example shows a simple way to report transfer error, and you can
* add your own implementation.
* @retval None
*/
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
transferState = TransferStates::FAILURE;
}