2 ******************************************************************************
\r
3 * @file stm32f10x_spi.c
\r
4 * @author MCD Application Team
\r
6 * @date 11-March-2011
\r
7 * @brief This file provides all the SPI firmware functions.
\r
8 ******************************************************************************
\r
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
\r
19 ******************************************************************************
\r
22 /* Includes ------------------------------------------------------------------*/
\r
23 #include "stm32f10x_spi.h"
\r
24 #include "stm32f10x_rcc.h"
\r
26 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
31 * @brief SPI driver modules
\r
35 /** @defgroup SPI_Private_TypesDefinitions
\r
44 /** @defgroup SPI_Private_Defines
\r
49 #define CR1_SPE_Set ((uint16_t)0x0040)
\r
50 #define CR1_SPE_Reset ((uint16_t)0xFFBF)
\r
53 #define I2SCFGR_I2SE_Set ((uint16_t)0x0400)
\r
54 #define I2SCFGR_I2SE_Reset ((uint16_t)0xFBFF)
\r
56 /* SPI CRCNext mask */
\r
57 #define CR1_CRCNext_Set ((uint16_t)0x1000)
\r
59 /* SPI CRCEN mask */
\r
60 #define CR1_CRCEN_Set ((uint16_t)0x2000)
\r
61 #define CR1_CRCEN_Reset ((uint16_t)0xDFFF)
\r
64 #define CR2_SSOE_Set ((uint16_t)0x0004)
\r
65 #define CR2_SSOE_Reset ((uint16_t)0xFFFB)
\r
67 /* SPI registers Masks */
\r
68 #define CR1_CLEAR_Mask ((uint16_t)0x3040)
\r
69 #define I2SCFGR_CLEAR_Mask ((uint16_t)0xF040)
\r
71 /* SPI or I2S mode selection masks */
\r
72 #define SPI_Mode_Select ((uint16_t)0xF7FF)
\r
73 #define I2S_Mode_Select ((uint16_t)0x0800)
\r
75 /* I2S clock source selection masks */
\r
76 #define I2S2_CLOCK_SRC ((uint32_t)(0x00020000))
\r
77 #define I2S3_CLOCK_SRC ((uint32_t)(0x00040000))
\r
78 #define I2S_MUL_MASK ((uint32_t)(0x0000F000))
\r
79 #define I2S_DIV_MASK ((uint32_t)(0x000000F0))
\r
85 /** @defgroup SPI_Private_Macros
\r
93 /** @defgroup SPI_Private_Variables
\r
101 /** @defgroup SPI_Private_FunctionPrototypes
\r
109 /** @defgroup SPI_Private_Functions
\r
114 * @brief Deinitializes the SPIx peripheral registers to their default
\r
115 * reset values (Affects also the I2Ss).
\r
116 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
119 void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
\r
121 /* Check the parameters */
\r
122 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
126 /* Enable SPI1 reset state */
\r
127 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
\r
128 /* Release SPI1 from reset state */
\r
129 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
\r
131 else if (SPIx == SPI2)
\r
133 /* Enable SPI2 reset state */
\r
134 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
\r
135 /* Release SPI2 from reset state */
\r
136 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
\r
142 /* Enable SPI3 reset state */
\r
143 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
\r
144 /* Release SPI3 from reset state */
\r
145 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
\r
151 * @brief Initializes the SPIx peripheral according to the specified
\r
152 * parameters in the SPI_InitStruct.
\r
153 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
154 * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
\r
155 * contains the configuration information for the specified SPI peripheral.
\r
158 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
\r
160 uint16_t tmpreg = 0;
\r
162 /* check the parameters */
\r
163 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
165 /* Check the SPI parameters */
\r
166 assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
\r
167 assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
\r
168 assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
\r
169 assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
\r
170 assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
\r
171 assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
\r
172 assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
\r
173 assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
\r
174 assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
\r
176 /*---------------------------- SPIx CR1 Configuration ------------------------*/
\r
177 /* Get the SPIx CR1 value */
\r
178 tmpreg = SPIx->CR1;
\r
179 /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
\r
180 tmpreg &= CR1_CLEAR_Mask;
\r
181 /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
\r
182 master/salve mode, CPOL and CPHA */
\r
183 /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
\r
184 /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
\r
185 /* Set LSBFirst bit according to SPI_FirstBit value */
\r
186 /* Set BR bits according to SPI_BaudRatePrescaler value */
\r
187 /* Set CPOL bit according to SPI_CPOL value */
\r
188 /* Set CPHA bit according to SPI_CPHA value */
\r
189 tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
\r
190 SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |
\r
191 SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |
\r
192 SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
\r
193 /* Write to SPIx CR1 */
\r
194 SPIx->CR1 = tmpreg;
\r
196 /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
\r
197 SPIx->I2SCFGR &= SPI_Mode_Select;
\r
199 /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
\r
200 /* Write to SPIx CRCPOLY */
\r
201 SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
\r
205 * @brief Initializes the SPIx peripheral according to the specified
\r
206 * parameters in the I2S_InitStruct.
\r
207 * @param SPIx: where x can be 2 or 3 to select the SPI peripheral
\r
208 * (configured in I2S mode).
\r
209 * @param I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
\r
210 * contains the configuration information for the specified SPI peripheral
\r
211 * configured in I2S mode.
\r
213 * The function calculates the optimal prescaler needed to obtain the most
\r
214 * accurate audio frequency (depending on the I2S clock source, the PLL values
\r
215 * and the product configuration). But in case the prescaler value is greater
\r
216 * than 511, the default value (0x02) will be configured instead. *
\r
219 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)
\r
221 uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
\r
223 RCC_ClocksTypeDef RCC_Clocks;
\r
224 uint32_t sourceclock = 0;
\r
226 /* Check the I2S parameters */
\r
227 assert_param(IS_SPI_23_PERIPH(SPIx));
\r
228 assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
\r
229 assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
\r
230 assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
\r
231 assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));
\r
232 assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));
\r
233 assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));
\r
235 /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
\r
236 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
\r
237 SPIx->I2SCFGR &= I2SCFGR_CLEAR_Mask;
\r
238 SPIx->I2SPR = 0x0002;
\r
240 /* Get the I2SCFGR register value */
\r
241 tmpreg = SPIx->I2SCFGR;
\r
243 /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
\r
244 if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)
\r
246 i2sodd = (uint16_t)0;
\r
247 i2sdiv = (uint16_t)2;
\r
249 /* If the requested audio frequency is not the default, compute the prescaler */
\r
252 /* Check the frame length (For the Prescaler computing) */
\r
253 if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)
\r
255 /* Packet length is 16 bits */
\r
260 /* Packet length is 32 bits */
\r
264 /* Get the I2S clock source mask depending on the peripheral number */
\r
265 if(((uint32_t)SPIx) == SPI2_BASE)
\r
267 /* The mask is relative to I2S2 */
\r
268 tmp = I2S2_CLOCK_SRC;
\r
272 /* The mask is relative to I2S3 */
\r
273 tmp = I2S3_CLOCK_SRC;
\r
276 /* Check the I2S clock source configuration depending on the Device:
\r
277 Only Connectivity line devices have the PLL3 VCO clock */
\r
278 #ifdef STM32F10X_CL
\r
279 if((RCC->CFGR2 & tmp) != 0)
\r
281 /* Get the configuration bits of RCC PLL3 multiplier */
\r
282 tmp = (uint32_t)((RCC->CFGR2 & I2S_MUL_MASK) >> 12);
\r
284 /* Get the value of the PLL3 multiplier */
\r
285 if((tmp > 5) && (tmp < 15))
\r
287 /* Multiplier is between 8 and 14 (value 15 is forbidden) */
\r
294 /* Multiplier is 20 */
\r
298 /* Get the PREDIV2 value */
\r
299 sourceclock = (uint32_t)(((RCC->CFGR2 & I2S_DIV_MASK) >> 4) + 1);
\r
301 /* Calculate the Source Clock frequency based on PLL3 and PREDIV2 values */
\r
302 sourceclock = (uint32_t) ((HSE_Value / sourceclock) * tmp * 2);
\r
306 /* I2S Clock source is System clock: Get System Clock frequency */
\r
307 RCC_GetClocksFreq(&RCC_Clocks);
\r
309 /* Get the source clock value: based on System Clock value */
\r
310 sourceclock = RCC_Clocks.SYSCLK_Frequency;
\r
312 #else /* STM32F10X_HD */
\r
313 /* I2S Clock source is System clock: Get System Clock frequency */
\r
314 RCC_GetClocksFreq(&RCC_Clocks);
\r
316 /* Get the source clock value: based on System Clock value */
\r
317 sourceclock = RCC_Clocks.SYSCLK_Frequency;
\r
318 #endif /* STM32F10X_CL */
\r
320 /* Compute the Real divider depending on the MCLK output state with a floating point */
\r
321 if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable)
\r
323 /* MCLK output is enabled */
\r
324 tmp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitStruct->I2S_AudioFreq)) + 5);
\r
328 /* MCLK output is disabled */
\r
329 tmp = (uint16_t)(((((sourceclock / (32 * packetlength)) *10 ) / I2S_InitStruct->I2S_AudioFreq)) + 5);
\r
332 /* Remove the floating point */
\r
335 /* Check the parity of the divider */
\r
336 i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
\r
338 /* Compute the i2sdiv prescaler */
\r
339 i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
\r
341 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
\r
342 i2sodd = (uint16_t) (i2sodd << 8);
\r
345 /* Test if the divider is 1 or 0 or greater than 0xFF */
\r
346 if ((i2sdiv < 2) || (i2sdiv > 0xFF))
\r
348 /* Set the default values */
\r
353 /* Write to SPIx I2SPR register the computed value */
\r
354 SPIx->I2SPR = (uint16_t)(i2sdiv | (uint16_t)(i2sodd | (uint16_t)I2S_InitStruct->I2S_MCLKOutput));
\r
356 /* Configure the I2S with the SPI_InitStruct values */
\r
357 tmpreg |= (uint16_t)(I2S_Mode_Select | (uint16_t)(I2S_InitStruct->I2S_Mode | \
\r
358 (uint16_t)(I2S_InitStruct->I2S_Standard | (uint16_t)(I2S_InitStruct->I2S_DataFormat | \
\r
359 (uint16_t)I2S_InitStruct->I2S_CPOL))));
\r
361 /* Write to SPIx I2SCFGR */
\r
362 SPIx->I2SCFGR = tmpreg;
\r
366 * @brief Fills each SPI_InitStruct member with its default value.
\r
367 * @param SPI_InitStruct : pointer to a SPI_InitTypeDef structure which will be initialized.
\r
370 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
\r
372 /*--------------- Reset SPI init structure parameters values -----------------*/
\r
373 /* Initialize the SPI_Direction member */
\r
374 SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;
\r
375 /* initialize the SPI_Mode member */
\r
376 SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;
\r
377 /* initialize the SPI_DataSize member */
\r
378 SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
\r
379 /* Initialize the SPI_CPOL member */
\r
380 SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
\r
381 /* Initialize the SPI_CPHA member */
\r
382 SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
\r
383 /* Initialize the SPI_NSS member */
\r
384 SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;
\r
385 /* Initialize the SPI_BaudRatePrescaler member */
\r
386 SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
\r
387 /* Initialize the SPI_FirstBit member */
\r
388 SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
\r
389 /* Initialize the SPI_CRCPolynomial member */
\r
390 SPI_InitStruct->SPI_CRCPolynomial = 7;
\r
394 * @brief Fills each I2S_InitStruct member with its default value.
\r
395 * @param I2S_InitStruct : pointer to a I2S_InitTypeDef structure which will be initialized.
\r
398 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)
\r
400 /*--------------- Reset I2S init structure parameters values -----------------*/
\r
401 /* Initialize the I2S_Mode member */
\r
402 I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;
\r
404 /* Initialize the I2S_Standard member */
\r
405 I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;
\r
407 /* Initialize the I2S_DataFormat member */
\r
408 I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;
\r
410 /* Initialize the I2S_MCLKOutput member */
\r
411 I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;
\r
413 /* Initialize the I2S_AudioFreq member */
\r
414 I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;
\r
416 /* Initialize the I2S_CPOL member */
\r
417 I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;
\r
421 * @brief Enables or disables the specified SPI peripheral.
\r
422 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
423 * @param NewState: new state of the SPIx peripheral.
\r
424 * This parameter can be: ENABLE or DISABLE.
\r
427 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
\r
429 /* Check the parameters */
\r
430 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
431 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
432 if (NewState != DISABLE)
\r
434 /* Enable the selected SPI peripheral */
\r
435 SPIx->CR1 |= CR1_SPE_Set;
\r
439 /* Disable the selected SPI peripheral */
\r
440 SPIx->CR1 &= CR1_SPE_Reset;
\r
445 * @brief Enables or disables the specified SPI peripheral (in I2S mode).
\r
446 * @param SPIx: where x can be 2 or 3 to select the SPI peripheral.
\r
447 * @param NewState: new state of the SPIx peripheral.
\r
448 * This parameter can be: ENABLE or DISABLE.
\r
451 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
\r
453 /* Check the parameters */
\r
454 assert_param(IS_SPI_23_PERIPH(SPIx));
\r
455 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
456 if (NewState != DISABLE)
\r
458 /* Enable the selected SPI peripheral (in I2S mode) */
\r
459 SPIx->I2SCFGR |= I2SCFGR_I2SE_Set;
\r
463 /* Disable the selected SPI peripheral (in I2S mode) */
\r
464 SPIx->I2SCFGR &= I2SCFGR_I2SE_Reset;
\r
469 * @brief Enables or disables the specified SPI/I2S interrupts.
\r
470 * @param SPIx: where x can be
\r
471 * - 1, 2 or 3 in SPI mode
\r
472 * - 2 or 3 in I2S mode
\r
473 * @param SPI_I2S_IT: specifies the SPI/I2S interrupt source to be enabled or disabled.
\r
474 * This parameter can be one of the following values:
\r
475 * @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask
\r
476 * @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask
\r
477 * @arg SPI_I2S_IT_ERR: Error interrupt mask
\r
478 * @param NewState: new state of the specified SPI/I2S interrupt.
\r
479 * This parameter can be: ENABLE or DISABLE.
\r
482 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)
\r
484 uint16_t itpos = 0, itmask = 0 ;
\r
485 /* Check the parameters */
\r
486 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
487 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
488 assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));
\r
490 /* Get the SPI/I2S IT index */
\r
491 itpos = SPI_I2S_IT >> 4;
\r
493 /* Set the IT mask */
\r
494 itmask = (uint16_t)1 << (uint16_t)itpos;
\r
496 if (NewState != DISABLE)
\r
498 /* Enable the selected SPI/I2S interrupt */
\r
499 SPIx->CR2 |= itmask;
\r
503 /* Disable the selected SPI/I2S interrupt */
\r
504 SPIx->CR2 &= (uint16_t)~itmask;
\r
509 * @brief Enables or disables the SPIx/I2Sx DMA interface.
\r
510 * @param SPIx: where x can be
\r
511 * - 1, 2 or 3 in SPI mode
\r
512 * - 2 or 3 in I2S mode
\r
513 * @param SPI_I2S_DMAReq: specifies the SPI/I2S DMA transfer request to be enabled or disabled.
\r
514 * This parameter can be any combination of the following values:
\r
515 * @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request
\r
516 * @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request
\r
517 * @param NewState: new state of the selected SPI/I2S DMA transfer request.
\r
518 * This parameter can be: ENABLE or DISABLE.
\r
521 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)
\r
523 /* Check the parameters */
\r
524 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
525 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
526 assert_param(IS_SPI_I2S_DMAREQ(SPI_I2S_DMAReq));
\r
527 if (NewState != DISABLE)
\r
529 /* Enable the selected SPI/I2S DMA requests */
\r
530 SPIx->CR2 |= SPI_I2S_DMAReq;
\r
534 /* Disable the selected SPI/I2S DMA requests */
\r
535 SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;
\r
540 * @brief Transmits a Data through the SPIx/I2Sx peripheral.
\r
541 * @param SPIx: where x can be
\r
542 * - 1, 2 or 3 in SPI mode
\r
543 * - 2 or 3 in I2S mode
\r
544 * @param Data : Data to be transmitted.
\r
547 void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
\r
549 /* Check the parameters */
\r
550 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
552 /* Write in the DR register the data to be sent */
\r
557 * @brief Returns the most recent received data by the SPIx/I2Sx peripheral.
\r
558 * @param SPIx: where x can be
\r
559 * - 1, 2 or 3 in SPI mode
\r
560 * - 2 or 3 in I2S mode
\r
561 * @retval The value of the received data.
\r
563 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
\r
565 /* Check the parameters */
\r
566 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
568 /* Return the data in the DR register */
\r
573 * @brief Configures internally by software the NSS pin for the selected SPI.
\r
574 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
575 * @param SPI_NSSInternalSoft: specifies the SPI NSS internal state.
\r
576 * This parameter can be one of the following values:
\r
577 * @arg SPI_NSSInternalSoft_Set: Set NSS pin internally
\r
578 * @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally
\r
581 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)
\r
583 /* Check the parameters */
\r
584 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
585 assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
\r
586 if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)
\r
588 /* Set NSS pin internally by software */
\r
589 SPIx->CR1 |= SPI_NSSInternalSoft_Set;
\r
593 /* Reset NSS pin internally by software */
\r
594 SPIx->CR1 &= SPI_NSSInternalSoft_Reset;
\r
599 * @brief Enables or disables the SS output for the selected SPI.
\r
600 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
601 * @param NewState: new state of the SPIx SS output.
\r
602 * This parameter can be: ENABLE or DISABLE.
\r
605 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
\r
607 /* Check the parameters */
\r
608 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
609 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
610 if (NewState != DISABLE)
\r
612 /* Enable the selected SPI SS output */
\r
613 SPIx->CR2 |= CR2_SSOE_Set;
\r
617 /* Disable the selected SPI SS output */
\r
618 SPIx->CR2 &= CR2_SSOE_Reset;
\r
623 * @brief Configures the data size for the selected SPI.
\r
624 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
625 * @param SPI_DataSize: specifies the SPI data size.
\r
626 * This parameter can be one of the following values:
\r
627 * @arg SPI_DataSize_16b: Set data frame format to 16bit
\r
628 * @arg SPI_DataSize_8b: Set data frame format to 8bit
\r
631 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)
\r
633 /* Check the parameters */
\r
634 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
635 assert_param(IS_SPI_DATASIZE(SPI_DataSize));
\r
636 /* Clear DFF bit */
\r
637 SPIx->CR1 &= (uint16_t)~SPI_DataSize_16b;
\r
638 /* Set new DFF bit value */
\r
639 SPIx->CR1 |= SPI_DataSize;
\r
643 * @brief Transmit the SPIx CRC value.
\r
644 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
647 void SPI_TransmitCRC(SPI_TypeDef* SPIx)
\r
649 /* Check the parameters */
\r
650 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
652 /* Enable the selected SPI CRC transmission */
\r
653 SPIx->CR1 |= CR1_CRCNext_Set;
\r
657 * @brief Enables or disables the CRC value calculation of the transferred bytes.
\r
658 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
659 * @param NewState: new state of the SPIx CRC value calculation.
\r
660 * This parameter can be: ENABLE or DISABLE.
\r
663 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)
\r
665 /* Check the parameters */
\r
666 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
667 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
668 if (NewState != DISABLE)
\r
670 /* Enable the selected SPI CRC calculation */
\r
671 SPIx->CR1 |= CR1_CRCEN_Set;
\r
675 /* Disable the selected SPI CRC calculation */
\r
676 SPIx->CR1 &= CR1_CRCEN_Reset;
\r
681 * @brief Returns the transmit or the receive CRC register value for the specified SPI.
\r
682 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
683 * @param SPI_CRC: specifies the CRC register to be read.
\r
684 * This parameter can be one of the following values:
\r
685 * @arg SPI_CRC_Tx: Selects Tx CRC register
\r
686 * @arg SPI_CRC_Rx: Selects Rx CRC register
\r
687 * @retval The selected CRC register value..
\r
689 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)
\r
691 uint16_t crcreg = 0;
\r
692 /* Check the parameters */
\r
693 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
694 assert_param(IS_SPI_CRC(SPI_CRC));
\r
695 if (SPI_CRC != SPI_CRC_Rx)
\r
697 /* Get the Tx CRC register */
\r
698 crcreg = SPIx->TXCRCR;
\r
702 /* Get the Rx CRC register */
\r
703 crcreg = SPIx->RXCRCR;
\r
705 /* Return the selected CRC register */
\r
710 * @brief Returns the CRC Polynomial register value for the specified SPI.
\r
711 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
712 * @retval The CRC Polynomial register value.
\r
714 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)
\r
716 /* Check the parameters */
\r
717 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
719 /* Return the CRC polynomial register */
\r
720 return SPIx->CRCPR;
\r
724 * @brief Selects the data transfer direction in bi-directional mode for the specified SPI.
\r
725 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
\r
726 * @param SPI_Direction: specifies the data transfer direction in bi-directional mode.
\r
727 * This parameter can be one of the following values:
\r
728 * @arg SPI_Direction_Tx: Selects Tx transmission direction
\r
729 * @arg SPI_Direction_Rx: Selects Rx receive direction
\r
732 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)
\r
734 /* Check the parameters */
\r
735 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
736 assert_param(IS_SPI_DIRECTION(SPI_Direction));
\r
737 if (SPI_Direction == SPI_Direction_Tx)
\r
739 /* Set the Tx only mode */
\r
740 SPIx->CR1 |= SPI_Direction_Tx;
\r
744 /* Set the Rx only mode */
\r
745 SPIx->CR1 &= SPI_Direction_Rx;
\r
750 * @brief Checks whether the specified SPI/I2S flag is set or not.
\r
751 * @param SPIx: where x can be
\r
752 * - 1, 2 or 3 in SPI mode
\r
753 * - 2 or 3 in I2S mode
\r
754 * @param SPI_I2S_FLAG: specifies the SPI/I2S flag to check.
\r
755 * This parameter can be one of the following values:
\r
756 * @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.
\r
757 * @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.
\r
758 * @arg SPI_I2S_FLAG_BSY: Busy flag.
\r
759 * @arg SPI_I2S_FLAG_OVR: Overrun flag.
\r
760 * @arg SPI_FLAG_MODF: Mode Fault flag.
\r
761 * @arg SPI_FLAG_CRCERR: CRC Error flag.
\r
762 * @arg I2S_FLAG_UDR: Underrun Error flag.
\r
763 * @arg I2S_FLAG_CHSIDE: Channel Side flag.
\r
764 * @retval The new state of SPI_I2S_FLAG (SET or RESET).
\r
766 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
\r
768 FlagStatus bitstatus = RESET;
\r
769 /* Check the parameters */
\r
770 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
771 assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
\r
772 /* Check the status of the specified SPI/I2S flag */
\r
773 if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
\r
775 /* SPI_I2S_FLAG is set */
\r
780 /* SPI_I2S_FLAG is reset */
\r
783 /* Return the SPI_I2S_FLAG status */
\r
788 * @brief Clears the SPIx CRC Error (CRCERR) flag.
\r
789 * @param SPIx: where x can be
\r
790 * - 1, 2 or 3 in SPI mode
\r
791 * @param SPI_I2S_FLAG: specifies the SPI flag to clear.
\r
792 * This function clears only CRCERR flag.
\r
794 * - OVR (OverRun error) flag is cleared by software sequence: a read
\r
795 * operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read
\r
796 * operation to SPI_SR register (SPI_I2S_GetFlagStatus()).
\r
797 * - UDR (UnderRun error) flag is cleared by a read operation to
\r
798 * SPI_SR register (SPI_I2S_GetFlagStatus()).
\r
799 * - MODF (Mode Fault) flag is cleared by software sequence: a read/write
\r
800 * operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a
\r
801 * write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).
\r
804 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
\r
806 /* Check the parameters */
\r
807 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
808 assert_param(IS_SPI_I2S_CLEAR_FLAG(SPI_I2S_FLAG));
\r
810 /* Clear the selected SPI CRC Error (CRCERR) flag */
\r
811 SPIx->SR = (uint16_t)~SPI_I2S_FLAG;
\r
815 * @brief Checks whether the specified SPI/I2S interrupt has occurred or not.
\r
816 * @param SPIx: where x can be
\r
817 * - 1, 2 or 3 in SPI mode
\r
818 * - 2 or 3 in I2S mode
\r
819 * @param SPI_I2S_IT: specifies the SPI/I2S interrupt source to check.
\r
820 * This parameter can be one of the following values:
\r
821 * @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.
\r
822 * @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.
\r
823 * @arg SPI_I2S_IT_OVR: Overrun interrupt.
\r
824 * @arg SPI_IT_MODF: Mode Fault interrupt.
\r
825 * @arg SPI_IT_CRCERR: CRC Error interrupt.
\r
826 * @arg I2S_IT_UDR: Underrun Error interrupt.
\r
827 * @retval The new state of SPI_I2S_IT (SET or RESET).
\r
829 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
\r
831 ITStatus bitstatus = RESET;
\r
832 uint16_t itpos = 0, itmask = 0, enablestatus = 0;
\r
834 /* Check the parameters */
\r
835 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
836 assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));
\r
838 /* Get the SPI/I2S IT index */
\r
839 itpos = 0x01 << (SPI_I2S_IT & 0x0F);
\r
841 /* Get the SPI/I2S IT mask */
\r
842 itmask = SPI_I2S_IT >> 4;
\r
844 /* Set the IT mask */
\r
845 itmask = 0x01 << itmask;
\r
847 /* Get the SPI_I2S_IT enable bit status */
\r
848 enablestatus = (SPIx->CR2 & itmask) ;
\r
850 /* Check the status of the specified SPI/I2S interrupt */
\r
851 if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)
\r
853 /* SPI_I2S_IT is set */
\r
858 /* SPI_I2S_IT is reset */
\r
861 /* Return the SPI_I2S_IT status */
\r
866 * @brief Clears the SPIx CRC Error (CRCERR) interrupt pending bit.
\r
867 * @param SPIx: where x can be
\r
868 * - 1, 2 or 3 in SPI mode
\r
869 * @param SPI_I2S_IT: specifies the SPI interrupt pending bit to clear.
\r
870 * This function clears only CRCERR interrupt pending bit.
\r
872 * - OVR (OverRun Error) interrupt pending bit is cleared by software
\r
873 * sequence: a read operation to SPI_DR register (SPI_I2S_ReceiveData())
\r
874 * followed by a read operation to SPI_SR register (SPI_I2S_GetITStatus()).
\r
875 * - UDR (UnderRun Error) interrupt pending bit is cleared by a read
\r
876 * operation to SPI_SR register (SPI_I2S_GetITStatus()).
\r
877 * - MODF (Mode Fault) interrupt pending bit is cleared by software sequence:
\r
878 * a read/write operation to SPI_SR register (SPI_I2S_GetITStatus())
\r
879 * followed by a write operation to SPI_CR1 register (SPI_Cmd() to enable
\r
883 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
\r
885 uint16_t itpos = 0;
\r
886 /* Check the parameters */
\r
887 assert_param(IS_SPI_ALL_PERIPH(SPIx));
\r
888 assert_param(IS_SPI_I2S_CLEAR_IT(SPI_I2S_IT));
\r
890 /* Get the SPI IT index */
\r
891 itpos = 0x01 << (SPI_I2S_IT & 0x0F);
\r
893 /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */
\r
894 SPIx->SR = (uint16_t)~itpos;
\r
908 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
\r