stm32l1xx_hal_spi_ex.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_spi_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief Extended SPI HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities SPI extension peripheral:
  11. * + Extended Peripheral Control functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT(c) 2014 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 "stm32l1xx_hal.h"
  44. /** @addtogroup STM32L1xx_HAL_Driver
  45. * @{
  46. */
  47. /** @addtogroup SPI
  48. * @{
  49. */
  50. #ifdef HAL_SPI_MODULE_ENABLED
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /** @addtogroup SPI_Exported_Functions
  58. * @{
  59. */
  60. /** @addtogroup SPI_Exported_Functions_Group1
  61. *
  62. * @{
  63. */
  64. /**
  65. * @brief Initializes the SPI according to the specified parameters
  66. * in the SPI_InitTypeDef and create the associated handle.
  67. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  68. * the configuration information for SPI module.
  69. * @retval HAL status
  70. */
  71. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  72. {
  73. /* Check the SPI handle allocation */
  74. if(hspi == NULL)
  75. {
  76. return HAL_ERROR;
  77. }
  78. /* Check the parameters */
  79. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  80. assert_param(IS_SPI_MODE(hspi->Init.Mode));
  81. assert_param(IS_SPI_DIRECTION_MODE(hspi->Init.Direction));
  82. assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
  83. assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
  84. assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
  85. assert_param(IS_SPI_NSS(hspi->Init.NSS));
  86. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  87. assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
  88. assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
  89. assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
  90. assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
  91. if(hspi->State == HAL_SPI_STATE_RESET)
  92. {
  93. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  94. HAL_SPI_MspInit(hspi);
  95. }
  96. hspi->State = HAL_SPI_STATE_BUSY;
  97. /* Disble the selected SPI peripheral */
  98. __HAL_SPI_DISABLE(hspi);
  99. /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
  100. /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
  101. Communication speed, First bit and CRC calculation state */
  102. hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize |
  103. hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
  104. hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation);
  105. /* Configure : NSS management */
  106. hspi->Instance->CR2 = (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode);
  107. /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
  108. /* Configure : CRC Polynomial */
  109. hspi->Instance->CRCPR = hspi->Init.CRCPolynomial;
  110. #if defined (STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined (STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
  111. /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
  112. CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  113. #endif
  114. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  115. hspi->State = HAL_SPI_STATE_READY;
  116. return HAL_OK;
  117. }
  118. /**
  119. * @}
  120. */
  121. /**
  122. * @}
  123. */
  124. #endif /* HAL_SPI_MODULE_ENABLED */
  125. /**
  126. * @}
  127. */
  128. /**
  129. * @}
  130. */
  131. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/