You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

246 lines
7.8 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_spi_ex.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 29-December-2017
  7. * @brief Extended SPI HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * SPI peripheral extended functionalities :
  10. * + IO operation functions
  11. * + Peripheral Control functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. * 1. Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32h7xx_hal.h"
  44. /** @addtogroup STM32H7xx_HAL_Driver
  45. * @{
  46. */
  47. /** @defgroup SPIEx SPIEx
  48. * @brief SPI Extended HAL module driver
  49. * @{
  50. */
  51. #ifdef HAL_SPI_MODULE_ENABLED
  52. /* Private typedef -----------------------------------------------------------*/
  53. /* Private defines -----------------------------------------------------------*/
  54. /* Private macros ------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private function prototypes -----------------------------------------------*/
  57. /* Exported functions --------------------------------------------------------*/
  58. /** @defgroup SPIEx_Exported_Functions SPIEx Exported Functions
  59. * @{
  60. */
  61. /** @defgroup SPIEx_Exported_Functions_Group1 IO operation functions
  62. * @brief Data transfers functions
  63. *
  64. @verbatim
  65. ==============================================================================
  66. ##### IO operation functions #####
  67. ===============================================================================
  68. [..]
  69. This subsection provides a set of extended functions to manage the SPI
  70. data transfers.
  71. (#) SPIEx function:
  72. (++) HAL_SPIEx_FlushRxFifo()
  73. (++) HAL_SPIEx_FlushRxFifo()
  74. (++) HAL_SPIEx_EnableLockConfiguration()
  75. (++) HAL_SPIEx_ConfigureUnderrun()
  76. @endverbatim
  77. * @{
  78. */
  79. /**
  80. * @brief Flush the RX fifo.
  81. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  82. * the configuration information for the specified SPI module.
  83. * @retval HAL status
  84. */
  85. HAL_StatusTypeDef HAL_SPIEx_FlushRxFifo(SPI_HandleTypeDef *hspi)
  86. {
  87. __IO uint32_t tmpreg;
  88. uint8_t count = 0;
  89. while ( ((hspi->Instance->SR & SPI_FLAG_FRLVL) != SPI_FRLVL_EMPTY) || ((hspi->Instance->SR & SPI_FLAG_RXWNE) == SPI_FLAG_RXWNE))
  90. {
  91. count+=4;
  92. tmpreg = hspi->Instance->RXDR;
  93. UNUSED(tmpreg); /* To avoid GCC warning */
  94. if (IS_SPI_HIGHEND_INSTANCE(hspi->Instance))
  95. {
  96. if(count > SPI_HIGHEND_FIFO_SIZE)
  97. {
  98. return HAL_TIMEOUT;
  99. }
  100. }
  101. else
  102. {
  103. if(count > SPI_LOWEND_FIFO_SIZE)
  104. {
  105. return HAL_TIMEOUT;
  106. }
  107. }
  108. }
  109. return HAL_OK;
  110. }
  111. /**
  112. * @brief Enable the Lock for the AF configuration of associated IOs
  113. * and write protect the Content of Configuartion register 2
  114. * when SPI is enabled
  115. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  116. * the configuration information for SPI module.
  117. * @retval None
  118. */
  119. HAL_StatusTypeDef HAL_SPIEx_EnableLockConfiguration(SPI_HandleTypeDef *hspi)
  120. {
  121. HAL_StatusTypeDef errorcode = HAL_OK;
  122. /* Process Locked */
  123. __HAL_LOCK(hspi);
  124. if (hspi->State != HAL_SPI_STATE_READY)
  125. {
  126. errorcode = HAL_BUSY;
  127. hspi->State = HAL_SPI_STATE_READY;
  128. /* Process Unlocked */
  129. __HAL_UNLOCK(hspi);
  130. return errorcode;
  131. }
  132. /* Check if the SPI is disabled to edit IOLOCK bit */
  133. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  134. {
  135. SET_BIT(hspi->Instance->CR1 , SPI_CR1_IOLOCK);
  136. }
  137. else
  138. {
  139. /* Disable SPI peripheral */
  140. __HAL_SPI_DISABLE(hspi);
  141. SET_BIT(hspi->Instance->CR1 , SPI_CR1_IOLOCK);
  142. /* Enable SPI peripheral */
  143. __HAL_SPI_ENABLE(hspi);
  144. }
  145. hspi->State = HAL_SPI_STATE_READY;
  146. /* Process Unlocked */
  147. __HAL_UNLOCK(hspi);
  148. return errorcode;
  149. }
  150. /**
  151. * @brief Configure the UNDERRUN condition and behavior of slave transmitter.
  152. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  153. * the configuration information for SPI module.
  154. * @param UnderrunDetection : Detection of underrun condition at slave transmitter
  155. * This parameter can be a value of @ref SPI_Underrun_Detection.
  156. * @param UnderrunBehaviour : Behavior of slave transmitter at underrun condition
  157. * This parameter can be a value of @ref SPI_Underrun_Behaviour.
  158. * @retval None
  159. */
  160. HAL_StatusTypeDef HAL_SPIEx_ConfigureUnderrun(SPI_HandleTypeDef *hspi, uint32_t UnderrunDetection, uint32_t UnderrunBehaviour)
  161. {
  162. HAL_StatusTypeDef errorcode = HAL_OK;
  163. /* Process Locked */
  164. __HAL_LOCK(hspi);
  165. /* Check State and Insure that Underrun configuration is managed only by Salve */
  166. if ((hspi->State != HAL_SPI_STATE_READY) || (hspi->Init.Mode != SPI_MODE_SLAVE))
  167. {
  168. errorcode = HAL_BUSY;
  169. hspi->State = HAL_SPI_STATE_READY;
  170. /* Process Unlocked */
  171. __HAL_UNLOCK(hspi);
  172. return errorcode;
  173. }
  174. /* Check the parameters */
  175. assert_param(IS_SPI_UNDERRUN_DETECTION(UnderrunDetection));
  176. assert_param(IS_SPI_UNDERRUN_BEHAVIOUR(UnderrunBehaviour));
  177. /* Check if the SPI is disabled to edit CFG1 register */
  178. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  179. {
  180. /* Configure Underrun fields */
  181. MODIFY_REG(hspi->Instance->CFG1, SPI_CFG1_UDRDET, UnderrunDetection);
  182. MODIFY_REG(hspi->Instance->CFG1, SPI_CFG1_UDRCFG, UnderrunBehaviour);
  183. }
  184. else
  185. {
  186. /* Disable SPI peripheral */
  187. __HAL_SPI_DISABLE(hspi);
  188. /* Configure Underrun fields */
  189. MODIFY_REG(hspi->Instance->CFG1, SPI_CFG1_UDRDET, UnderrunDetection);
  190. MODIFY_REG(hspi->Instance->CFG1, SPI_CFG1_UDRCFG, UnderrunBehaviour);
  191. /* Enable SPI peripheral */
  192. __HAL_SPI_ENABLE(hspi);
  193. }
  194. hspi->State = HAL_SPI_STATE_READY;
  195. /* Process Unlocked */
  196. __HAL_UNLOCK(hspi);
  197. return errorcode;
  198. }
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @}
  204. */
  205. #endif /* HAL_SPI_MODULE_ENABLED */
  206. /**
  207. * @}
  208. */
  209. /**
  210. * @}
  211. */
  212. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/