]> jspc29.x-matter.uni-frankfurt.de Git - mvd_firmware.git/blob
51a9cce7736fa1243cc433ac8364f74493f489cd
[mvd_firmware.git] /
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_spi.c\r
4   * @author  MCD Application Team\r
5   * @version V3.5.0\r
6   * @date    11-March-2011\r
7   * @brief   This file provides all the SPI firmware functions.\r
8   ******************************************************************************\r
9   * @attention\r
10   *\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
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>\r
19   ******************************************************************************\r
20   */\r
21 \r
22 /* Includes ------------------------------------------------------------------*/\r
23 #include "stm32f10x_spi.h"\r
24 #include "stm32f10x_rcc.h"\r
25 \r
26 /** @addtogroup STM32F10x_StdPeriph_Driver\r
27   * @{\r
28   */\r
29 \r
30 /** @defgroup SPI \r
31   * @brief SPI driver modules\r
32   * @{\r
33   */ \r
34 \r
35 /** @defgroup SPI_Private_TypesDefinitions\r
36   * @{\r
37   */\r
38 \r
39 /**\r
40   * @}\r
41   */ \r
42 \r
43 \r
44 /** @defgroup SPI_Private_Defines\r
45   * @{\r
46   */\r
47 \r
48 /* SPI SPE mask */\r
49 #define CR1_SPE_Set          ((uint16_t)0x0040)\r
50 #define CR1_SPE_Reset        ((uint16_t)0xFFBF)\r
51 \r
52 /* I2S I2SE mask */\r
53 #define I2SCFGR_I2SE_Set     ((uint16_t)0x0400)\r
54 #define I2SCFGR_I2SE_Reset   ((uint16_t)0xFBFF)\r
55 \r
56 /* SPI CRCNext mask */\r
57 #define CR1_CRCNext_Set      ((uint16_t)0x1000)\r
58 \r
59 /* SPI CRCEN mask */\r
60 #define CR1_CRCEN_Set        ((uint16_t)0x2000)\r
61 #define CR1_CRCEN_Reset      ((uint16_t)0xDFFF)\r
62 \r
63 /* SPI SSOE mask */\r
64 #define CR2_SSOE_Set         ((uint16_t)0x0004)\r
65 #define CR2_SSOE_Reset       ((uint16_t)0xFFFB)\r
66 \r
67 /* SPI registers Masks */\r
68 #define CR1_CLEAR_Mask       ((uint16_t)0x3040)\r
69 #define I2SCFGR_CLEAR_Mask   ((uint16_t)0xF040)\r
70 \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
74 \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
80 \r
81 /**\r
82   * @}\r
83   */\r
84 \r
85 /** @defgroup SPI_Private_Macros\r
86   * @{\r
87   */\r
88 \r
89 /**\r
90   * @}\r
91   */\r
92 \r
93 /** @defgroup SPI_Private_Variables\r
94   * @{\r
95   */\r
96 \r
97 /**\r
98   * @}\r
99   */\r
100 \r
101 /** @defgroup SPI_Private_FunctionPrototypes\r
102   * @{\r
103   */\r
104 \r
105 /**\r
106   * @}\r
107   */\r
108 \r
109 /** @defgroup SPI_Private_Functions\r
110   * @{\r
111   */\r
112 \r
113 /**\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
117   * @retval None\r
118   */\r
119 void SPI_I2S_DeInit(SPI_TypeDef* SPIx)\r
120 {\r
121   /* Check the parameters */\r
122   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
123 \r
124   if (SPIx == SPI1)\r
125   {\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
130   }\r
131   else if (SPIx == SPI2)\r
132   {\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
137   }\r
138   else\r
139   {\r
140     if (SPIx == SPI3)\r
141     {\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
146     }\r
147   }\r
148 }\r
149 \r
150 /**\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
156   * @retval None\r
157   */\r
158 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)\r
159 {\r
160   uint16_t tmpreg = 0;\r
161   \r
162   /* check the parameters */\r
163   assert_param(IS_SPI_ALL_PERIPH(SPIx));   \r
164   \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
175 \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
195   \r
196   /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */\r
197   SPIx->I2SCFGR &= SPI_Mode_Select;             \r
198 \r
199 /*---------------------------- SPIx CRCPOLY Configuration --------------------*/\r
200   /* Write to SPIx CRCPOLY */\r
201   SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;\r
202 }\r
203 \r
204 /**\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
212   * @note\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
217   * @retval None\r
218   */\r
219 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)\r
220 {\r
221   uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;\r
222   uint32_t tmp = 0;\r
223   RCC_ClocksTypeDef RCC_Clocks;\r
224   uint32_t sourceclock = 0;\r
225   \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
234 \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
239   \r
240   /* Get the I2SCFGR register value */\r
241   tmpreg = SPIx->I2SCFGR;\r
242   \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
245   {\r
246     i2sodd = (uint16_t)0;\r
247     i2sdiv = (uint16_t)2;   \r
248   }\r
249   /* If the requested audio frequency is not the default, compute the prescaler */\r
250   else\r
251   {\r
252     /* Check the frame length (For the Prescaler computing) */\r
253     if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)\r
254     {\r
255       /* Packet length is 16 bits */\r
256       packetlength = 1;\r
257     }\r
258     else\r
259     {\r
260       /* Packet length is 32 bits */\r
261       packetlength = 2;\r
262     }\r
263 \r
264     /* Get the I2S clock source mask depending on the peripheral number */\r
265     if(((uint32_t)SPIx) == SPI2_BASE)\r
266     {\r
267       /* The mask is relative to I2S2 */\r
268       tmp = I2S2_CLOCK_SRC;\r
269     }\r
270     else \r
271     {\r
272       /* The mask is relative to I2S3 */      \r
273       tmp = I2S3_CLOCK_SRC;\r
274     }\r
275 \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
280     {\r
281       /* Get the configuration bits of RCC PLL3 multiplier */\r
282       tmp = (uint32_t)((RCC->CFGR2 & I2S_MUL_MASK) >> 12);\r
283 \r
284       /* Get the value of the PLL3 multiplier */      \r
285       if((tmp > 5) && (tmp < 15))\r
286       {\r
287         /* Multiplier is between 8 and 14 (value 15 is forbidden) */\r
288         tmp += 2;\r
289       }\r
290       else\r
291       {\r
292         if (tmp == 15)\r
293         {\r
294           /* Multiplier is 20 */\r
295           tmp = 20;\r
296         }\r
297       }      \r
298       /* Get the PREDIV2 value */\r
299       sourceclock = (uint32_t)(((RCC->CFGR2 & I2S_DIV_MASK) >> 4) + 1);\r
300       \r
301       /* Calculate the Source Clock frequency based on PLL3 and PREDIV2 values */\r
302       sourceclock = (uint32_t) ((HSE_Value / sourceclock) * tmp * 2); \r
303     }\r
304     else\r
305     {\r
306       /* I2S Clock source is System clock: Get System Clock frequency */\r
307       RCC_GetClocksFreq(&RCC_Clocks);      \r
308       \r
309       /* Get the source clock value: based on System Clock value */\r
310       sourceclock = RCC_Clocks.SYSCLK_Frequency;\r
311     }        \r
312 #else /* STM32F10X_HD */\r
313     /* I2S Clock source is System clock: Get System Clock frequency */\r
314     RCC_GetClocksFreq(&RCC_Clocks);      \r
315       \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
319 \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
322     {\r
323       /* MCLK output is enabled */\r
324       tmp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitStruct->I2S_AudioFreq)) + 5);\r
325     }\r
326     else\r
327     {\r
328       /* MCLK output is disabled */\r
329       tmp = (uint16_t)(((((sourceclock / (32 * packetlength)) *10 ) / I2S_InitStruct->I2S_AudioFreq)) + 5);\r
330     }\r
331     \r
332     /* Remove the floating point */\r
333     tmp = tmp / 10;  \r
334       \r
335     /* Check the parity of the divider */\r
336     i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);\r
337    \r
338     /* Compute the i2sdiv prescaler */\r
339     i2sdiv = (uint16_t)((tmp - i2sodd) / 2);\r
340    \r
341     /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */\r
342     i2sodd = (uint16_t) (i2sodd << 8);\r
343   }\r
344   \r
345   /* Test if the divider is 1 or 0 or greater than 0xFF */\r
346   if ((i2sdiv < 2) || (i2sdiv > 0xFF))\r
347   {\r
348     /* Set the default values */\r
349     i2sdiv = 2;\r
350     i2sodd = 0;\r
351   }\r
352 \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
355  \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
360  \r
361   /* Write to SPIx I2SCFGR */  \r
362   SPIx->I2SCFGR = tmpreg;   \r
363 }\r
364 \r
365 /**\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
368   * @retval None\r
369   */\r
370 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)\r
371 {\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
391 }\r
392 \r
393 /**\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
396   * @retval None\r
397   */\r
398 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)\r
399 {\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
403   \r
404   /* Initialize the I2S_Standard member */\r
405   I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;\r
406   \r
407   /* Initialize the I2S_DataFormat member */\r
408   I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;\r
409   \r
410   /* Initialize the I2S_MCLKOutput member */\r
411   I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;\r
412   \r
413   /* Initialize the I2S_AudioFreq member */\r
414   I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;\r
415   \r
416   /* Initialize the I2S_CPOL member */\r
417   I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;\r
418 }\r
419 \r
420 /**\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
425   * @retval None\r
426   */\r
427 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
428 {\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
433   {\r
434     /* Enable the selected SPI peripheral */\r
435     SPIx->CR1 |= CR1_SPE_Set;\r
436   }\r
437   else\r
438   {\r
439     /* Disable the selected SPI peripheral */\r
440     SPIx->CR1 &= CR1_SPE_Reset;\r
441   }\r
442 }\r
443 \r
444 /**\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
449   * @retval None\r
450   */\r
451 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
452 {\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
457   {\r
458     /* Enable the selected SPI peripheral (in I2S mode) */\r
459     SPIx->I2SCFGR |= I2SCFGR_I2SE_Set;\r
460   }\r
461   else\r
462   {\r
463     /* Disable the selected SPI peripheral (in I2S mode) */\r
464     SPIx->I2SCFGR &= I2SCFGR_I2SE_Reset;\r
465   }\r
466 }\r
467 \r
468 /**\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
480   * @retval None\r
481   */\r
482 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)\r
483 {\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
489 \r
490   /* Get the SPI/I2S IT index */\r
491   itpos = SPI_I2S_IT >> 4;\r
492 \r
493   /* Set the IT mask */\r
494   itmask = (uint16_t)1 << (uint16_t)itpos;\r
495 \r
496   if (NewState != DISABLE)\r
497   {\r
498     /* Enable the selected SPI/I2S interrupt */\r
499     SPIx->CR2 |= itmask;\r
500   }\r
501   else\r
502   {\r
503     /* Disable the selected SPI/I2S interrupt */\r
504     SPIx->CR2 &= (uint16_t)~itmask;\r
505   }\r
506 }\r
507 \r
508 /**\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
519   * @retval None\r
520   */\r
521 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)\r
522 {\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
528   {\r
529     /* Enable the selected SPI/I2S DMA requests */\r
530     SPIx->CR2 |= SPI_I2S_DMAReq;\r
531   }\r
532   else\r
533   {\r
534     /* Disable the selected SPI/I2S DMA requests */\r
535     SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;\r
536   }\r
537 }\r
538 \r
539 /**\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
545   * @retval None\r
546   */\r
547 void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)\r
548 {\r
549   /* Check the parameters */\r
550   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
551   \r
552   /* Write in the DR register the data to be sent */\r
553   SPIx->DR = Data;\r
554 }\r
555 \r
556 /**\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
562   */\r
563 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)\r
564 {\r
565   /* Check the parameters */\r
566   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
567   \r
568   /* Return the data in the DR register */\r
569   return SPIx->DR;\r
570 }\r
571 \r
572 /**\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
579   * @retval None\r
580   */\r
581 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)\r
582 {\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
587   {\r
588     /* Set NSS pin internally by software */\r
589     SPIx->CR1 |= SPI_NSSInternalSoft_Set;\r
590   }\r
591   else\r
592   {\r
593     /* Reset NSS pin internally by software */\r
594     SPIx->CR1 &= SPI_NSSInternalSoft_Reset;\r
595   }\r
596 }\r
597 \r
598 /**\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
603   * @retval None\r
604   */\r
605 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
606 {\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
611   {\r
612     /* Enable the selected SPI SS output */\r
613     SPIx->CR2 |= CR2_SSOE_Set;\r
614   }\r
615   else\r
616   {\r
617     /* Disable the selected SPI SS output */\r
618     SPIx->CR2 &= CR2_SSOE_Reset;\r
619   }\r
620 }\r
621 \r
622 /**\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
629   * @retval None\r
630   */\r
631 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)\r
632 {\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
640 }\r
641 \r
642 /**\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
645   * @retval None\r
646   */\r
647 void SPI_TransmitCRC(SPI_TypeDef* SPIx)\r
648 {\r
649   /* Check the parameters */\r
650   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
651   \r
652   /* Enable the selected SPI CRC transmission */\r
653   SPIx->CR1 |= CR1_CRCNext_Set;\r
654 }\r
655 \r
656 /**\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
661   * @retval None\r
662   */\r
663 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)\r
664 {\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
669   {\r
670     /* Enable the selected SPI CRC calculation */\r
671     SPIx->CR1 |= CR1_CRCEN_Set;\r
672   }\r
673   else\r
674   {\r
675     /* Disable the selected SPI CRC calculation */\r
676     SPIx->CR1 &= CR1_CRCEN_Reset;\r
677   }\r
678 }\r
679 \r
680 /**\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
688   */\r
689 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)\r
690 {\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
696   {\r
697     /* Get the Tx CRC register */\r
698     crcreg = SPIx->TXCRCR;\r
699   }\r
700   else\r
701   {\r
702     /* Get the Rx CRC register */\r
703     crcreg = SPIx->RXCRCR;\r
704   }\r
705   /* Return the selected CRC register */\r
706   return crcreg;\r
707 }\r
708 \r
709 /**\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
713   */\r
714 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)\r
715 {\r
716   /* Check the parameters */\r
717   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
718   \r
719   /* Return the CRC polynomial register */\r
720   return SPIx->CRCPR;\r
721 }\r
722 \r
723 /**\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
730   * @retval None\r
731   */\r
732 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)\r
733 {\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
738   {\r
739     /* Set the Tx only mode */\r
740     SPIx->CR1 |= SPI_Direction_Tx;\r
741   }\r
742   else\r
743   {\r
744     /* Set the Rx only mode */\r
745     SPIx->CR1 &= SPI_Direction_Rx;\r
746   }\r
747 }\r
748 \r
749 /**\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
765   */\r
766 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)\r
767 {\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
774   {\r
775     /* SPI_I2S_FLAG is set */\r
776     bitstatus = SET;\r
777   }\r
778   else\r
779   {\r
780     /* SPI_I2S_FLAG is reset */\r
781     bitstatus = RESET;\r
782   }\r
783   /* Return the SPI_I2S_FLAG status */\r
784   return  bitstatus;\r
785 }\r
786 \r
787 /**\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
793   * @note\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
802   * @retval None\r
803   */\r
804 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)\r
805 {\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
809     \r
810     /* Clear the selected SPI CRC Error (CRCERR) flag */\r
811     SPIx->SR = (uint16_t)~SPI_I2S_FLAG;\r
812 }\r
813 \r
814 /**\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
828   */\r
829 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)\r
830 {\r
831   ITStatus bitstatus = RESET;\r
832   uint16_t itpos = 0, itmask = 0, enablestatus = 0;\r
833 \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
837 \r
838   /* Get the SPI/I2S IT index */\r
839   itpos = 0x01 << (SPI_I2S_IT & 0x0F);\r
840 \r
841   /* Get the SPI/I2S IT mask */\r
842   itmask = SPI_I2S_IT >> 4;\r
843 \r
844   /* Set the IT mask */\r
845   itmask = 0x01 << itmask;\r
846 \r
847   /* Get the SPI_I2S_IT enable bit status */\r
848   enablestatus = (SPIx->CR2 & itmask) ;\r
849 \r
850   /* Check the status of the specified SPI/I2S interrupt */\r
851   if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)\r
852   {\r
853     /* SPI_I2S_IT is set */\r
854     bitstatus = SET;\r
855   }\r
856   else\r
857   {\r
858     /* SPI_I2S_IT is reset */\r
859     bitstatus = RESET;\r
860   }\r
861   /* Return the SPI_I2S_IT status */\r
862   return bitstatus;\r
863 }\r
864 \r
865 /**\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
871   * @note\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
880   *     the SPI).\r
881   * @retval None\r
882   */\r
883 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)\r
884 {\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
889 \r
890   /* Get the SPI IT index */\r
891   itpos = 0x01 << (SPI_I2S_IT & 0x0F);\r
892 \r
893   /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */\r
894   SPIx->SR = (uint16_t)~itpos;\r
895 }\r
896 /**\r
897   * @}\r
898   */ \r
899 \r
900 /**\r
901   * @}\r
902   */ \r
903 \r
904 /**\r
905   * @}\r
906   */ \r
907 \r
908 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/\r