From 21414c3594e1e36f0c177fc6f69678c83a894206 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 3 Jun 2021 14:38:34 +0200 Subject: [PATCH] extended spi core --- stm32h7/devicetest/GyroL3GD20H.cpp | 2 ++ stm32h7/spi/CMakeLists.txt | 1 + stm32h7/spi/spiCore.c | 56 ++++++++++++++++++++++++++++++ stm32h7/spi/spiCore.h | 36 +++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 stm32h7/spi/spiCore.c create mode 100644 stm32h7/spi/spiCore.h diff --git a/stm32h7/devicetest/GyroL3GD20H.cpp b/stm32h7/devicetest/GyroL3GD20H.cpp index a56ea4c..fad9923 100644 --- a/stm32h7/devicetest/GyroL3GD20H.cpp +++ b/stm32h7/devicetest/GyroL3GD20H.cpp @@ -2,6 +2,7 @@ #include "stm32h7xx_spi_dma_msp.h" #include "spiConf.h" #include "../spi/spiDefinitions.h" +#include "../spi/spiCore.h" #include "fsfw/tasks/TaskFactory.h" #include "fsfw/serviceinterface/ServiceInterface.h" @@ -21,6 +22,7 @@ GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle): spiHandle(spiHandle) { ReturnValue_t GyroL3GD20H::initialize() { // Configure the SPI peripheral spiHandle->Instance = SPI1; + uint32_t test = HAL_RCC_GetHCLKFreq(); spiHandle->Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), 3900000); spiHandle->Init.Direction = SPI_DIRECTION_2LINES; spi::assignSpiMode(spi::SpiModes::MODE_3, spiHandle); diff --git a/stm32h7/spi/CMakeLists.txt b/stm32h7/spi/CMakeLists.txt index 3b5fd0d..99d39dd 100644 --- a/stm32h7/spi/CMakeLists.txt +++ b/stm32h7/spi/CMakeLists.txt @@ -1,3 +1,4 @@ target_sources(${TARGET_NAME} PRIVATE + spiCore.c spiDefinitions.cpp ) diff --git a/stm32h7/spi/spiCore.c b/stm32h7/spi/spiCore.c new file mode 100644 index 0000000..ebb956d --- /dev/null +++ b/stm32h7/spi/spiCore.c @@ -0,0 +1,56 @@ +#include "spiCore.h" + +SPI_HandleTypeDef* spiHandle = NULL; +DMA_HandleTypeDef* hdma_tx = NULL; +DMA_HandleTypeDef* hdma_rx = NULL; + +void setDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle) { + hdma_tx = txHandle; + hdma_rx = rxHandle; +} + +void getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle) { + *txHandle = hdma_tx; + *rxHandle = hdma_rx; +} + +void assignSpiHandle(SPI_HandleTypeDef *spiHandle_) { + if(spiHandle_ == NULL) { + return; + } + spiHandle = spiHandle_; +} + +SPI_HandleTypeDef* getSpiHandle() { + return spiHandle; +} + +/** + * @brief This function handles DMA Rx interrupt request. + * @param None + * @retval None + */ +void SPIx_DMA_RX_IRQHandler(void) +{ + HAL_DMA_IRQHandler(spiHandle->hdmarx); +} + +/** + * @brief This function handles DMA Tx interrupt request. + * @param None + * @retval None + */ +void SPIx_DMA_TX_IRQHandler(void) +{ + HAL_DMA_IRQHandler(spiHandle->hdmatx); +} + +/** + * @brief This function handles SPIx interrupt request. + * @param None + * @retval None + */ +void SPIx_IRQHandler(void) +{ + HAL_SPI_IRQHandler(spiHandle); +} diff --git a/stm32h7/spi/spiCore.h b/stm32h7/spi/spiCore.h new file mode 100644 index 0000000..9af0de0 --- /dev/null +++ b/stm32h7/spi/spiCore.h @@ -0,0 +1,36 @@ +#ifndef FSFW_HAL_STM32H7_SPI_SPICORE_H_ +#define FSFW_HAL_STM32H7_SPI_SPICORE_H_ + +#include "stm32h7xx_hal.h" +#include "stm32h7xx_hal_dma.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Assign DMA handles. Required to use DMA for SPI transfers. + * @param txHandle + * @param rxHandle + */ +void setDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle); + +void getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle); + +/** + * Assign SPI handle. Needs to be done before using the SPI + * @param spiHandle + */ +void assignSpiHandle(SPI_HandleTypeDef *spiHandle); + +/** + * Get the assigned SPI handle. + * @return + */ +SPI_HandleTypeDef* getSpiHandle(); + +#ifdef __cplusplus +} +#endif + +#endif /* FSFW_HAL_STM32H7_SPI_SPICORE_H_ */