stm32f1xx_hal_spi_ex.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_spi_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended SPI HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities SPI extension peripheral:
  9. * + Extended Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ******************************************************************************
  39. */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32f1xx_hal.h"
  42. /** @addtogroup STM32F1xx_HAL_Driver
  43. * @{
  44. */
  45. /** @addtogroup SPI
  46. * @{
  47. */
  48. #ifdef HAL_SPI_MODULE_ENABLED
  49. /** @defgroup SPI_Private_Variables SPI Private Variables
  50. * @{
  51. */
  52. #if (USE_SPI_CRC != 0U)
  53. /* Variable used to determine if device is impacted by implementation of workaround
  54. related to wrong CRC errors detection on SPI2. Conditions in which this workaround has to be applied, are:
  55. - STM32F101CDE/STM32F103CDE
  56. - Revision ID : Z
  57. - SPI2
  58. - In receive only mode, with CRC calculation enabled, at the end of the CRC reception,
  59. the software needs to check the CRCERR flag. If it is found set, read back the SPI_RXCRC:
  60. + If the value is 0, the complete data transfer is successful.
  61. + Otherwise, one or more errors have been detected during the data transfer by CPU or DMA.
  62. If CRCERR is found reset, the complete data transfer is considered successful.
  63. */
  64. uint8_t uCRCErrorWorkaroundCheck = 0U;
  65. #endif /* USE_SPI_CRC */
  66. /**
  67. * @}
  68. */
  69. /* Private typedef -----------------------------------------------------------*/
  70. /* Private define ------------------------------------------------------------*/
  71. /* Private macro -------------------------------------------------------------*/
  72. /* Private variables ---------------------------------------------------------*/
  73. /* Private function prototypes -----------------------------------------------*/
  74. /* Private functions ---------------------------------------------------------*/
  75. /** @addtogroup SPI_Exported_Functions
  76. * @{
  77. */
  78. /** @addtogroup SPI_Exported_Functions_Group1
  79. *
  80. * @{
  81. */
  82. /**
  83. * @brief Initializes the SPI according to the specified parameters
  84. * in the SPI_InitTypeDef and create the associated handle.
  85. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  86. * the configuration information for SPI module.
  87. * @retval HAL status
  88. */
  89. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  90. {
  91. /* Check the SPI handle allocation */
  92. if(hspi == NULL)
  93. {
  94. return HAL_ERROR;
  95. }
  96. /* Check the parameters */
  97. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  98. assert_param(IS_SPI_MODE(hspi->Init.Mode));
  99. assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
  100. assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
  101. assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
  102. assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
  103. assert_param(IS_SPI_NSS(hspi->Init.NSS));
  104. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  105. assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
  106. #if (USE_SPI_CRC != 0U)
  107. assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
  108. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  109. {
  110. assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
  111. }
  112. #else
  113. hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  114. #endif /* USE_SPI_CRC */
  115. if(hspi->State == HAL_SPI_STATE_RESET)
  116. {
  117. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  118. HAL_SPI_MspInit(hspi);
  119. }
  120. hspi->State = HAL_SPI_STATE_BUSY;
  121. /* Disble the selected SPI peripheral */
  122. __HAL_SPI_DISABLE(hspi);
  123. /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
  124. /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
  125. Communication speed, First bit and CRC calculation state */
  126. WRITE_REG(hspi->Instance->CR1, (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize |
  127. hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
  128. hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation) );
  129. /* Configure : NSS management */
  130. WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) | hspi->Init.TIMode));
  131. /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
  132. /* Configure : CRC Polynomial */
  133. WRITE_REG(hspi->Instance->CRCPR, hspi->Init.CRCPolynomial);
  134. #if defined(SPI_I2SCFGR_I2SMOD)
  135. /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
  136. CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  137. #endif /* SPI_I2SCFGR_I2SMOD */
  138. #if (USE_SPI_CRC != 0U)
  139. #if defined (STM32F101xE) || defined (STM32F103xE)
  140. /* Check RevisionID value for identifying if Device is Rev Z (0x0001) in order to enable workaround for
  141. CRC errors wrongly detected */
  142. /* Pb is that ES_STM32F10xxCDE also identify an issue in Debug registers access while not in Debug mode.
  143. Revision ID information is only available in Debug mode, so Workaround could not be implemented
  144. to distinguish Rev Z devices (issue present) from more recent version (issue fixed).
  145. So, in case of Revison Z F101 or F103 devices, below variable should be assigned to 1 */
  146. uCRCErrorWorkaroundCheck = 0U;
  147. #else
  148. uCRCErrorWorkaroundCheck = 0U;
  149. #endif /* STM32F101xE || STM32F103xE */
  150. #endif /* USE_SPI_CRC */
  151. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  152. hspi->State = HAL_SPI_STATE_READY;
  153. return HAL_OK;
  154. }
  155. /**
  156. * @}
  157. */
  158. /**
  159. * @}
  160. */
  161. /** @addtogroup SPI_Private_Functions
  162. * @{
  163. */
  164. #if (USE_SPI_CRC != 0U)
  165. /**
  166. * @brief Checks if encountered CRC error could be corresponding to wrongly detected errors
  167. * according to SPI instance, Device type, and revision ID.
  168. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  169. * the configuration information for SPI module.
  170. * @retval CRC error validity (SPI_INVALID_CRC_ERROR or SPI_VALID_CRC_ERROR).
  171. */
  172. uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi)
  173. {
  174. #if defined(STM32F101xE) || defined(STM32F103xE)
  175. /* Check how to handle this CRC error (workaround to be applied or not) */
  176. /* If CRC errors could be wrongly detected (issue 2.15.2 in STM32F10xxC/D/E silicon limitations ES (DocID14732 Rev 13) */
  177. if((uCRCErrorWorkaroundCheck != 0U) && (hspi->Instance == SPI2))
  178. {
  179. if(hspi->Instance->RXCRCR == 0U)
  180. {
  181. return (SPI_INVALID_CRC_ERROR);
  182. }
  183. }
  184. return (SPI_VALID_CRC_ERROR);
  185. #else
  186. /* Prevent unused argument(s) compilation warning */
  187. UNUSED(hspi);
  188. return (SPI_VALID_CRC_ERROR);
  189. #endif
  190. }
  191. #endif /* USE_SPI_CRC */
  192. /**
  193. * @}
  194. */
  195. #endif /* HAL_SPI_MODULE_ENABLED */
  196. /**
  197. * @}
  198. */
  199. /**
  200. * @}
  201. */
  202. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/