1 /**
2 ******************************************************************************
3 * @file stm32l1xx_hal_spi_ex.c
4 * @author MCD Application Team
5 * @brief Extended SPI HAL module driver.
6 * This file provides firmware functions to manage the following
7 * SPI peripheral extended functionalities :
8 * + IO operation functions
9 *
10 ******************************************************************************
11 * @attention
12 *
13 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
14 *
15 * Redistribution and use in source and binary forms, with or without modification,
16 * are permitted provided that the following conditions are met:
17 * 1. Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. Neither the name of STMicroelectronics nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************
38 */
39
40 /* Includes ------------------------------------------------------------------*/
41 #include "stm32l1xx_hal.h"
42
43 /** @addtogroup STM32L1xx_HAL_Driver
44 * @{
45 */
46
47 /** @addtogroup SPI
48 * @{
49 */
50 #ifdef HAL_SPI_MODULE_ENABLED
51
52 /* Private typedef -----------------------------------------------------------*/
53 /* Private defines -----------------------------------------------------------*/
54 /* Private macros ------------------------------------------------------------*/
55 /* Private variables ---------------------------------------------------------*/
56 /* Private function prototypes -----------------------------------------------*/
57 /* Exported functions --------------------------------------------------------*/
58
59 /** @addtogroup SPI_Exported_Functions
60 * @{
61 */
62
63 /** @addtogroup SPI_Exported_Functions_Group1
64 *
65 * @{
66 */
67
68 /**
69 * @brief Initializes the SPI according to the specified parameters
70 * in the SPI_InitTypeDef and create the associated handle.
71 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
72 * the configuration information for SPI module.
73 * @retval HAL status
74 */
HAL_SPI_Init(SPI_HandleTypeDef * hspi)75 HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
76 {
77 /* Check the SPI handle allocation */
78 if (hspi == NULL)
79 {
80 return HAL_ERROR;
81 }
82
83 /* Check the parameters */
84 assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
85 assert_param(IS_SPI_MODE(hspi->Init.Mode));
86 assert_param(IS_SPI_DIRECTION_MODE(hspi->Init.Direction));
87 assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
88 assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
89 assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
90 assert_param(IS_SPI_NSS(hspi->Init.NSS));
91 assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
92 assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
93 assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
94 assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
95 assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
96
97 if (hspi->State == HAL_SPI_STATE_RESET)
98 {
99 /* Allocate lock resource and initialize it */
100 hspi->Lock = HAL_UNLOCKED;
101
102 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
103 HAL_SPI_MspInit(hspi);
104 }
105
106 hspi->State = HAL_SPI_STATE_BUSY;
107
108 /* Disble the selected SPI peripheral */
109 __HAL_SPI_DISABLE(hspi);
110
111 /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
112 /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
113 Communication speed, First bit and CRC calculation state */
114 hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize |
115 hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
116 hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation);
117
118 /* Configure : NSS management */
119 hspi->Instance->CR2 = (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) | hspi->Init.TIMode);
120
121 /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
122 /* Configure : CRC Polynomial */
123 hspi->Instance->CRCPR = hspi->Init.CRCPolynomial;
124
125 #if defined (STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined (STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L151xE) || defined (STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
126 /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
127 CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
128 #endif
129
130 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
131 hspi->State = HAL_SPI_STATE_READY;
132
133 return HAL_OK;
134 }
135
136 /**
137 * @}
138 */
139
140 /**
141 * @}
142 */
143
144 #endif /* HAL_SPI_MODULE_ENABLED */
145
146 /**
147 * @}
148 */
149
150 /**
151 * @}
152 */
153
154 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
155