From 66a7a4dbbe9b6913ddab3369e2076ca9105f1336 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 3 Jun 2021 21:49:16 +0200 Subject: [PATCH] added polling msp init --- stm32h7/spi/mspInit.c | 95 +++++++++++++++++++++++-------------------- stm32h7/spi/mspInit.h | 3 ++ 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/stm32h7/spi/mspInit.c b/stm32h7/spi/mspInit.c index 864f3da..1964391 100644 --- a/stm32h7/spi/mspInit.c +++ b/stm32h7/spi/mspInit.c @@ -44,39 +44,12 @@ void hal_spi_msp_init_dma(void *spi_handle) { return; } - GPIO_InitTypeDef GPIO_InitStruct; - if (hspi->Instance == SPI1) - { - /*##-1- Enable peripherals and GPIO Clocks #################################*/ - /* Enable GPIO TX/RX clock */ - SPIx_SCK_GPIO_CLK_ENABLE(); - SPIx_MISO_GPIO_CLK_ENABLE(); - SPIx_MOSI_GPIO_CLK_ENABLE(); - /* Enable SPI1 clock */ - SPIx_CLK_ENABLE(); - /* Enable DMA clock */ + hal_spi_msp_init_polling(spi_handle); + if (hspi->Instance == SPI1) { + // DMA setup DMAx_CLK_ENABLE(); - /*##-2- Configure peripheral GPIO ##########################################*/ - /* SPI SCK GPIO pin configuration */ - GPIO_InitStruct.Pin = SPIx_SCK_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.Alternate = SPIx_SCK_AF; - HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); - - /* SPI MISO GPIO pin configuration */ - GPIO_InitStruct.Pin = SPIx_MISO_PIN; - GPIO_InitStruct.Alternate = SPIx_MISO_AF; - HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); - - /* SPI MOSI GPIO pin configuration */ - GPIO_InitStruct.Pin = SPIx_MOSI_PIN; - GPIO_InitStruct.Alternate = SPIx_MOSI_AF; - HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); - - /*##-3- Configure the DMA ##################################################*/ + // Configure the DMA /* Configure the DMA handler for Transmission process */ hdma_tx->Instance = SPIx_TX_DMA_STREAM; hdma_tx->Init.FIFOMode = DMA_FIFOMODE_DISABLE; @@ -154,20 +127,8 @@ void hal_spi_msp_deinit_dma(void *spi_handle) if(hspi == NULL) { return; } - if(hspi->Instance == SPIx) - { - /*##-1- Reset peripherals ##################################################*/ - SPIx_FORCE_RESET(); - SPIx_RELEASE_RESET(); - - /*##-2- Disable peripherals and GPIO Clocks ################################*/ - /* Deconfigure SPI SCK */ - HAL_GPIO_DeInit(SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN); - /* Deconfigure SPI MISO */ - HAL_GPIO_DeInit(SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN); - /* Deconfigure SPI MOSI */ - HAL_GPIO_DeInit(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN); - + hal_spi_msp_deinit_polling(spi_handle); + if(hspi->Instance == SPIx) { DMA_HandleTypeDef* hdma_tx = NULL; DMA_HandleTypeDef* hdma_rx = NULL; get_dma_handles(&hdma_tx, &hdma_rx); @@ -190,3 +151,47 @@ void hal_spi_msp_deinit_dma(void *spi_handle) HAL_NVIC_EnableIRQ(SPIx_IRQn); } } + +void hal_spi_msp_init_polling(void *hspi) { + GPIO_InitTypeDef GPIO_InitStruct = {}; + /*##-1- Enable peripherals and GPIO Clocks #################################*/ + /* Enable GPIO TX/RX clock */ + SPIx_SCK_GPIO_CLK_ENABLE(); + SPIx_MISO_GPIO_CLK_ENABLE(); + SPIx_MOSI_GPIO_CLK_ENABLE(); + /* Enable SPI clock */ + SPIx_CLK_ENABLE(); + + /*##-2- Configure peripheral GPIO ##########################################*/ + /* SPI SCK GPIO pin configuration */ + GPIO_InitStruct.Pin = SPIx_SCK_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Alternate = SPIx_SCK_AF; + HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); + + /* SPI MISO GPIO pin configuration */ + GPIO_InitStruct.Pin = SPIx_MISO_PIN; + GPIO_InitStruct.Alternate = SPIx_MISO_AF; + HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); + + /* SPI MOSI GPIO pin configuration */ + GPIO_InitStruct.Pin = SPIx_MOSI_PIN; + GPIO_InitStruct.Alternate = SPIx_MOSI_AF; + HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); +} + +void hal_spi_msp_deinit_polling(void *hspi) { + /*##-1- Reset peripherals ##################################################*/ + SPIx_FORCE_RESET(); + SPIx_RELEASE_RESET(); + + /*##-2- Disable peripherals and GPIO Clocks ################################*/ + /* Configure SPI SCK as alternate function */ + HAL_GPIO_DeInit(SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN); + /* Configure SPI MISO as alternate function */ + HAL_GPIO_DeInit(SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN); + /* Configure SPI MOSI as alternate function */ + HAL_GPIO_DeInit(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN); +} diff --git a/stm32h7/spi/mspInit.h b/stm32h7/spi/mspInit.h index 9ac8562..48ac7b1 100644 --- a/stm32h7/spi/mspInit.h +++ b/stm32h7/spi/mspInit.h @@ -8,6 +8,9 @@ extern "C" { void hal_spi_msp_init_dma(void *hspi); void hal_spi_msp_deinit_dma(void *hspi); +void hal_spi_msp_init_polling(void *hspi); +void hal_spi_msp_deinit_polling(void *hspi); + #ifdef __cplusplus } #endif