1 /**
2 ******************************************************************************
3 * @file stm32wbxx_ll_spi.c
4 * @author MCD Application Team
5 * @brief SPI LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2019 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 #if defined(USE_FULL_LL_DRIVER)
19
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32wbxx_ll_spi.h"
22 #include "stm32wbxx_ll_bus.h"
23
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif /* USE_FULL_ASSERT */
29
30 /** @addtogroup STM32WBxx_LL_Driver
31 * @{
32 */
33
34 #if defined (SPI1) || defined (SPI2)
35
36 /** @addtogroup SPI_LL
37 * @{
38 */
39
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42
43 /* Private constants ---------------------------------------------------------*/
44 /** @defgroup SPI_LL_Private_Constants SPI Private Constants
45 * @{
46 */
47 /* SPI registers Masks */
48 #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
49 SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
50 SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_CRCL | \
51 SPI_CR1_CRCNEXT | SPI_CR1_CRCEN | SPI_CR1_BIDIOE | \
52 SPI_CR1_BIDIMODE)
53 /**
54 * @}
55 */
56
57 /* Private macros ------------------------------------------------------------*/
58 /** @defgroup SPI_LL_Private_Macros SPI Private Macros
59 * @{
60 */
61 #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
62 || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
63 || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
64 || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
65
66 #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
67 || ((__VALUE__) == LL_SPI_MODE_SLAVE))
68
69 #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_4BIT) \
70 || ((__VALUE__) == LL_SPI_DATAWIDTH_5BIT) \
71 || ((__VALUE__) == LL_SPI_DATAWIDTH_6BIT) \
72 || ((__VALUE__) == LL_SPI_DATAWIDTH_7BIT) \
73 || ((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
74 || ((__VALUE__) == LL_SPI_DATAWIDTH_9BIT) \
75 || ((__VALUE__) == LL_SPI_DATAWIDTH_10BIT) \
76 || ((__VALUE__) == LL_SPI_DATAWIDTH_11BIT) \
77 || ((__VALUE__) == LL_SPI_DATAWIDTH_12BIT) \
78 || ((__VALUE__) == LL_SPI_DATAWIDTH_13BIT) \
79 || ((__VALUE__) == LL_SPI_DATAWIDTH_14BIT) \
80 || ((__VALUE__) == LL_SPI_DATAWIDTH_15BIT) \
81 || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
82
83 #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
84 || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
85
86 #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
87 || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
88
89 #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
90 || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
91 || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
92
93 #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
94 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
95 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
96 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
97 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
98 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
99 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
100 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
101
102 #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
103 || ((__VALUE__) == LL_SPI_MSB_FIRST))
104
105 #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
106 || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
107
108 #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
109
110 /**
111 * @}
112 */
113
114 /* Private function prototypes -----------------------------------------------*/
115
116 /* Exported functions --------------------------------------------------------*/
117 /** @addtogroup SPI_LL_Exported_Functions
118 * @{
119 */
120
121 /** @addtogroup SPI_LL_EF_Init
122 * @{
123 */
124
125 /**
126 * @brief De-initialize the SPI registers to their default reset values.
127 * @param SPIx SPI Instance
128 * @retval An ErrorStatus enumeration value:
129 * - SUCCESS: SPI registers are de-initialized
130 * - ERROR: SPI registers are not de-initialized
131 */
LL_SPI_DeInit(SPI_TypeDef * SPIx)132 ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
133 {
134 ErrorStatus status = ERROR;
135
136 /* Check the parameters */
137 assert_param(IS_SPI_ALL_INSTANCE(SPIx));
138
139 #if defined(SPI1)
140 if (SPIx == SPI1)
141 {
142 /* Force reset of SPI clock */
143 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
144
145 /* Release reset of SPI clock */
146 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
147
148 status = SUCCESS;
149 }
150 #endif /* SPI1 */
151 #if defined(SPI2)
152 if (SPIx == SPI2)
153 {
154 /* Force reset of SPI clock */
155 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
156
157 /* Release reset of SPI clock */
158 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
159
160 status = SUCCESS;
161 }
162 #endif /* SPI2 */
163
164 return status;
165 }
166
167 /**
168 * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
169 * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
170 * SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
171 * @param SPIx SPI Instance
172 * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
173 * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
174 */
LL_SPI_Init(SPI_TypeDef * SPIx,LL_SPI_InitTypeDef * SPI_InitStruct)175 ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
176 {
177 ErrorStatus status = ERROR;
178
179 /* Check the SPI Instance SPIx*/
180 assert_param(IS_SPI_ALL_INSTANCE(SPIx));
181
182 /* Check the SPI parameters from SPI_InitStruct*/
183 assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
184 assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
185 assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
186 assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
187 assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
188 assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
189 assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
190 assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
191 assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
192
193 if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
194 {
195 /*---------------------------- SPIx CR1 Configuration ------------------------
196 * Configure SPIx CR1 with parameters:
197 * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
198 * - Master/Slave Mode: SPI_CR1_MSTR bit
199 * - ClockPolarity: SPI_CR1_CPOL bit
200 * - ClockPhase: SPI_CR1_CPHA bit
201 * - NSS management: SPI_CR1_SSM bit
202 * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
203 * - BitOrder: SPI_CR1_LSBFIRST bit
204 * - CRCCalculation: SPI_CR1_CRCEN bit
205 */
206 MODIFY_REG(SPIx->CR1,
207 SPI_CR1_CLEAR_MASK,
208 SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
209 SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
210 SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
211 SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
212
213 /*---------------------------- SPIx CR2 Configuration ------------------------
214 * Configure SPIx CR2 with parameters:
215 * - DataWidth: DS[3:0] bits
216 * - NSS management: SSOE bit
217 */
218 MODIFY_REG(SPIx->CR2,
219 SPI_CR2_DS | SPI_CR2_SSOE,
220 SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
221
222 /* Set Rx FIFO to Quarter (1 Byte) in case of 8 Bits mode. No DataPacking by default */
223 if (SPI_InitStruct->DataWidth < LL_SPI_DATAWIDTH_9BIT)
224 {
225 LL_SPI_SetRxFIFOThreshold(SPIx, LL_SPI_RX_FIFO_TH_QUARTER);
226 }
227
228 /*---------------------------- SPIx CRCPR Configuration ----------------------
229 * Configure SPIx CRCPR with parameters:
230 * - CRCPoly: CRCPOLY[15:0] bits
231 */
232 if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
233 {
234 assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
235 LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
236 }
237 status = SUCCESS;
238 }
239
240 return status;
241 }
242
243 /**
244 * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
245 * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
246 * whose fields will be set to default values.
247 * @retval None
248 */
LL_SPI_StructInit(LL_SPI_InitTypeDef * SPI_InitStruct)249 void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
250 {
251 /* Set SPI_InitStruct fields to default values */
252 SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
253 SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
254 SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
255 SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
256 SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
257 SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
258 SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
259 SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
260 SPI_InitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
261 SPI_InitStruct->CRCPoly = 7U;
262 }
263
264 /**
265 * @}
266 */
267
268 /**
269 * @}
270 */
271
272 /**
273 * @}
274 */
275
276 #endif /* defined (SPI1) || defined (SPI2) */
277
278 /**
279 * @}
280 */
281
282 #endif /* USE_FULL_LL_DRIVER */
283
284