Merge pull request #93 from zyp/master
[fw/stlink] / example / libs_stm / src / stm32f10x / stm32f10x_spi.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_spi.c\r
4   * @author  MCD Application Team\r
5   * @version V3.3.0\r
6   * @date    04/16/2010\r
7   * @brief   This file provides all the SPI firmware functions.\r
8   ******************************************************************************\r
9   * @copy\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 2010 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32f10x_spi.h"\r
23 #include "stm32f10x_rcc.h"\r
24 \r
25 /** @addtogroup STM32F10x_StdPeriph_Driver\r
26   * @{\r
27   */\r
28 \r
29 /** @defgroup SPI \r
30   * @brief SPI driver modules\r
31   * @{\r
32   */ \r
33 \r
34 /** @defgroup SPI_Private_TypesDefinitions\r
35   * @{\r
36   */\r
37 \r
38 /**\r
39   * @}\r
40   */ \r
41 \r
42 \r
43 /** @defgroup SPI_Private_Defines\r
44   * @{\r
45   */\r
46 \r
47 /* SPI SPE mask */\r
48 #define CR1_SPE_Set          ((uint16_t)0x0040)\r
49 #define CR1_SPE_Reset        ((uint16_t)0xFFBF)\r
50 \r
51 /* I2S I2SE mask */\r
52 #define I2SCFGR_I2SE_Set     ((uint16_t)0x0400)\r
53 #define I2SCFGR_I2SE_Reset   ((uint16_t)0xFBFF)\r
54 \r
55 /* SPI CRCNext mask */\r
56 #define CR1_CRCNext_Set      ((uint16_t)0x1000)\r
57 \r
58 /* SPI CRCEN mask */\r
59 #define CR1_CRCEN_Set        ((uint16_t)0x2000)\r
60 #define CR1_CRCEN_Reset      ((uint16_t)0xDFFF)\r
61 \r
62 /* SPI SSOE mask */\r
63 #define CR2_SSOE_Set         ((uint16_t)0x0004)\r
64 #define CR2_SSOE_Reset       ((uint16_t)0xFFFB)\r
65 \r
66 /* SPI registers Masks */\r
67 #define CR1_CLEAR_Mask       ((uint16_t)0x3040)\r
68 #define I2SCFGR_CLEAR_Mask   ((uint16_t)0xF040)\r
69 \r
70 /* SPI or I2S mode selection masks */\r
71 #define SPI_Mode_Select      ((uint16_t)0xF7FF)\r
72 #define I2S_Mode_Select      ((uint16_t)0x0800) \r
73 \r
74 /* I2S clock source selection masks */\r
75 #define I2S2_CLOCK_SRC       ((uint32_t)(0x00020000))\r
76 #define I2S3_CLOCK_SRC       ((uint32_t)(0x00040000))\r
77 #define I2S_MUL_MASK         ((uint32_t)(0x0000F000))\r
78 #define I2S_DIV_MASK         ((uint32_t)(0x000000F0))\r
79 \r
80 /**\r
81   * @}\r
82   */\r
83 \r
84 /** @defgroup SPI_Private_Macros\r
85   * @{\r
86   */\r
87 \r
88 /**\r
89   * @}\r
90   */\r
91 \r
92 /** @defgroup SPI_Private_Variables\r
93   * @{\r
94   */\r
95 \r
96 /**\r
97   * @}\r
98   */\r
99 \r
100 /** @defgroup SPI_Private_FunctionPrototypes\r
101   * @{\r
102   */\r
103 \r
104 /**\r
105   * @}\r
106   */\r
107 \r
108 /** @defgroup SPI_Private_Functions\r
109   * @{\r
110   */\r
111 \r
112 /**\r
113   * @brief  Deinitializes the SPIx peripheral registers to their default\r
114   *   reset values (Affects also the I2Ss).\r
115   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
116   * @retval None\r
117   */\r
118 void SPI_I2S_DeInit(SPI_TypeDef* SPIx)\r
119 {\r
120   /* Check the parameters */\r
121   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
122 \r
123   if (SPIx == SPI1)\r
124   {\r
125     /* Enable SPI1 reset state */\r
126     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);\r
127     /* Release SPI1 from reset state */\r
128     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);\r
129   }\r
130   else if (SPIx == SPI2)\r
131   {\r
132     /* Enable SPI2 reset state */\r
133     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);\r
134     /* Release SPI2 from reset state */\r
135     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);\r
136   }\r
137   else\r
138   {\r
139     if (SPIx == SPI3)\r
140     {\r
141       /* Enable SPI3 reset state */\r
142       RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);\r
143       /* Release SPI3 from reset state */\r
144       RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);\r
145     }\r
146   }\r
147 }\r
148 \r
149 /**\r
150   * @brief  Initializes the SPIx peripheral according to the specified \r
151   *   parameters in the SPI_InitStruct.\r
152   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
153   * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure that\r
154   *   contains the configuration information for the specified SPI peripheral.\r
155   * @retval None\r
156   */\r
157 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)\r
158 {\r
159   uint16_t tmpreg = 0;\r
160   \r
161   /* check the parameters */\r
162   assert_param(IS_SPI_ALL_PERIPH(SPIx));   \r
163   \r
164   /* Check the SPI parameters */\r
165   assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));\r
166   assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));\r
167   assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));\r
168   assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));\r
169   assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));\r
170   assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));\r
171   assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));\r
172   assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));\r
173   assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));\r
174 \r
175 /*---------------------------- SPIx CR1 Configuration ------------------------*/\r
176   /* Get the SPIx CR1 value */\r
177   tmpreg = SPIx->CR1;\r
178   /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */\r
179   tmpreg &= CR1_CLEAR_Mask;\r
180   /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler\r
181      master/salve mode, CPOL and CPHA */\r
182   /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */\r
183   /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */\r
184   /* Set LSBFirst bit according to SPI_FirstBit value */\r
185   /* Set BR bits according to SPI_BaudRatePrescaler value */\r
186   /* Set CPOL bit according to SPI_CPOL value */\r
187   /* Set CPHA bit according to SPI_CPHA value */\r
188   tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |\r
189                   SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |  \r
190                   SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |  \r
191                   SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);\r
192   /* Write to SPIx CR1 */\r
193   SPIx->CR1 = tmpreg;\r
194   \r
195   /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */\r
196   SPIx->I2SCFGR &= SPI_Mode_Select;             \r
197 \r
198 /*---------------------------- SPIx CRCPOLY Configuration --------------------*/\r
199   /* Write to SPIx CRCPOLY */\r
200   SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;\r
201 }\r
202 \r
203 /**\r
204   * @brief  Initializes the SPIx peripheral according to the specified \r
205   *   parameters in the I2S_InitStruct.\r
206   * @param  SPIx: where x can be  2 or 3 to select the SPI peripheral\r
207   *   (configured in I2S mode).\r
208   * @param  I2S_InitStruct: pointer to an I2S_InitTypeDef structure that\r
209   *   contains the configuration information for the specified SPI peripheral\r
210   *   configured in I2S mode.\r
211   * @note\r
212   *  The function calculates the optimal prescaler needed to obtain the most \r
213   *  accurate audio frequency (depending on the I2S clock source, the PLL values \r
214   *  and the product configuration). But in case the prescaler value is greater \r
215   *  than 511, the default value (0x02) will be configured instead.  *   \r
216   * @retval None\r
217   */\r
218 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)\r
219 {\r
220   uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;\r
221   uint32_t tmp = 0;\r
222   RCC_ClocksTypeDef RCC_Clocks;\r
223   uint32_t sourceclock = 0;\r
224   \r
225   /* Check the I2S parameters */\r
226   assert_param(IS_SPI_23_PERIPH(SPIx));\r
227   assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));\r
228   assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));\r
229   assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));\r
230   assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));\r
231   assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));\r
232   assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));  \r
233 \r
234 /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/\r
235   /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */\r
236   SPIx->I2SCFGR &= I2SCFGR_CLEAR_Mask; \r
237   SPIx->I2SPR = 0x0002;\r
238   \r
239   /* Get the I2SCFGR register value */\r
240   tmpreg = SPIx->I2SCFGR;\r
241   \r
242   /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/\r
243   if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)\r
244   {\r
245     i2sodd = (uint16_t)0;\r
246     i2sdiv = (uint16_t)2;   \r
247   }\r
248   /* If the requested audio frequency is not the default, compute the prescaler */\r
249   else\r
250   {\r
251     /* Check the frame length (For the Prescaler computing) */\r
252     if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)\r
253     {\r
254       /* Packet length is 16 bits */\r
255       packetlength = 1;\r
256     }\r
257     else\r
258     {\r
259       /* Packet length is 32 bits */\r
260       packetlength = 2;\r
261     }\r
262 \r
263     /* Get the I2S clock source mask depending on the peripheral number */\r
264     if(((uint32_t)SPIx) == SPI2_BASE)\r
265     {\r
266       /* The mask is relative to I2S2 */\r
267       tmp = I2S2_CLOCK_SRC;\r
268     }\r
269     else \r
270     {\r
271       /* The mask is relative to I2S3 */      \r
272       tmp = I2S3_CLOCK_SRC;\r
273     }\r
274 \r
275     /* Check the I2S clock source configuration depending on the Device:\r
276        Only Connectivity line devices have the PLL3 VCO clock */\r
277 #ifdef STM32F10X_CL\r
278     if((RCC->CFGR2 & tmp) != 0)\r
279     {\r
280       /* Get the configuration bits of RCC PLL3 multiplier */\r
281       tmp = (uint32_t)((RCC->CFGR2 & I2S_MUL_MASK) >> 12);\r
282 \r
283       /* Get the value of the PLL3 multiplier */      \r
284       if((tmp > 5) && (tmp < 15))\r
285       {\r
286         /* Multplier is between 8 and 14 (value 15 is forbidden) */\r
287         tmp += 2;\r
288       }\r
289       else\r
290       {\r
291         if (tmp == 15)\r
292         {\r
293           /* Multiplier is 20 */\r
294           tmp = 20;\r
295         }\r
296       }      \r
297       /* Get the PREDIV2 value */\r
298       sourceclock = (uint32_t)(((RCC->CFGR2 & I2S_DIV_MASK) >> 4) + 1);\r
299       \r
300       /* Calculate the Source Clock frequency based on PLL3 and PREDIV2 values */\r
301       sourceclock = (uint32_t) ((HSE_Value / sourceclock) * tmp * 2); \r
302     }\r
303     else\r
304     {\r
305       /* I2S Clock source is System clock: Get System Clock frequency */\r
306       RCC_GetClocksFreq(&RCC_Clocks);      \r
307       \r
308       /* Get the source clock value: based on System Clock value */\r
309       sourceclock = RCC_Clocks.SYSCLK_Frequency;\r
310     }        \r
311 #else /* STM32F10X_HD */\r
312     /* I2S Clock source is System clock: Get System Clock frequency */\r
313     RCC_GetClocksFreq(&RCC_Clocks);      \r
314       \r
315     /* Get the source clock value: based on System Clock value */\r
316     sourceclock = RCC_Clocks.SYSCLK_Frequency;    \r
317 #endif /* STM32F10X_CL */    \r
318 \r
319     /* Compute the Real divider depending on the MCLK output state with a flaoting point */\r
320     if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable)\r
321     {\r
322       /* MCLK output is enabled */\r
323       tmp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitStruct->I2S_AudioFreq)) + 5);\r
324     }\r
325     else\r
326     {\r
327       /* MCLK output is disabled */\r
328       tmp = (uint16_t)(((((sourceclock / (32 * packetlength)) *10 ) / I2S_InitStruct->I2S_AudioFreq)) + 5);\r
329     }\r
330     \r
331     /* Remove the flaoting point */\r
332     tmp = tmp / 10;  \r
333       \r
334     /* Check the parity of the divider */\r
335     i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);\r
336    \r
337     /* Compute the i2sdiv prescaler */\r
338     i2sdiv = (uint16_t)((tmp - i2sodd) / 2);\r
339    \r
340     /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */\r
341     i2sodd = (uint16_t) (i2sodd << 8);\r
342   }\r
343   \r
344   /* Test if the divider is 1 or 0 or greater than 0xFF */\r
345   if ((i2sdiv < 2) || (i2sdiv > 0xFF))\r
346   {\r
347     /* Set the default values */\r
348     i2sdiv = 2;\r
349     i2sodd = 0;\r
350   }\r
351 \r
352   /* Write to SPIx I2SPR register the computed value */\r
353   SPIx->I2SPR = (uint16_t)(i2sdiv | (uint16_t)(i2sodd | (uint16_t)I2S_InitStruct->I2S_MCLKOutput));  \r
354  \r
355   /* Configure the I2S with the SPI_InitStruct values */\r
356   tmpreg |= (uint16_t)(I2S_Mode_Select | (uint16_t)(I2S_InitStruct->I2S_Mode | \\r
357                   (uint16_t)(I2S_InitStruct->I2S_Standard | (uint16_t)(I2S_InitStruct->I2S_DataFormat | \\r
358                   (uint16_t)I2S_InitStruct->I2S_CPOL))));\r
359  \r
360   /* Write to SPIx I2SCFGR */  \r
361   SPIx->I2SCFGR = tmpreg;   \r
362 }\r
363 \r
364 /**\r
365   * @brief  Fills each SPI_InitStruct member with its default value.\r
366   * @param  SPI_InitStruct : pointer to a SPI_InitTypeDef structure which will be initialized.\r
367   * @retval None\r
368   */\r
369 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)\r
370 {\r
371 /*--------------- Reset SPI init structure parameters values -----------------*/\r
372   /* Initialize the SPI_Direction member */\r
373   SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;\r
374   /* initialize the SPI_Mode member */\r
375   SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;\r
376   /* initialize the SPI_DataSize member */\r
377   SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;\r
378   /* Initialize the SPI_CPOL member */\r
379   SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;\r
380   /* Initialize the SPI_CPHA member */\r
381   SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;\r
382   /* Initialize the SPI_NSS member */\r
383   SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;\r
384   /* Initialize the SPI_BaudRatePrescaler member */\r
385   SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;\r
386   /* Initialize the SPI_FirstBit member */\r
387   SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;\r
388   /* Initialize the SPI_CRCPolynomial member */\r
389   SPI_InitStruct->SPI_CRCPolynomial = 7;\r
390 }\r
391 \r
392 /**\r
393   * @brief  Fills each I2S_InitStruct member with its default value.\r
394   * @param  I2S_InitStruct : pointer to a I2S_InitTypeDef structure which will be initialized.\r
395   * @retval None\r
396   */\r
397 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)\r
398 {\r
399 /*--------------- Reset I2S init structure parameters values -----------------*/\r
400   /* Initialize the I2S_Mode member */\r
401   I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;\r
402   \r
403   /* Initialize the I2S_Standard member */\r
404   I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;\r
405   \r
406   /* Initialize the I2S_DataFormat member */\r
407   I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;\r
408   \r
409   /* Initialize the I2S_MCLKOutput member */\r
410   I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;\r
411   \r
412   /* Initialize the I2S_AudioFreq member */\r
413   I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;\r
414   \r
415   /* Initialize the I2S_CPOL member */\r
416   I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;\r
417 }\r
418 \r
419 /**\r
420   * @brief  Enables or disables the specified SPI peripheral.\r
421   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
422   * @param  NewState: new state of the SPIx peripheral. \r
423   *   This parameter can be: ENABLE or DISABLE.\r
424   * @retval None\r
425   */\r
426 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
427 {\r
428   /* Check the parameters */\r
429   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
430   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
431   if (NewState != DISABLE)\r
432   {\r
433     /* Enable the selected SPI peripheral */\r
434     SPIx->CR1 |= CR1_SPE_Set;\r
435   }\r
436   else\r
437   {\r
438     /* Disable the selected SPI peripheral */\r
439     SPIx->CR1 &= CR1_SPE_Reset;\r
440   }\r
441 }\r
442 \r
443 /**\r
444   * @brief  Enables or disables the specified SPI peripheral (in I2S mode).\r
445   * @param  SPIx: where x can be 2 or 3 to select the SPI peripheral.\r
446   * @param  NewState: new state of the SPIx peripheral. \r
447   *   This parameter can be: ENABLE or DISABLE.\r
448   * @retval None\r
449   */\r
450 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
451 {\r
452   /* Check the parameters */\r
453   assert_param(IS_SPI_23_PERIPH(SPIx));\r
454   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
455   if (NewState != DISABLE)\r
456   {\r
457     /* Enable the selected SPI peripheral (in I2S mode) */\r
458     SPIx->I2SCFGR |= I2SCFGR_I2SE_Set;\r
459   }\r
460   else\r
461   {\r
462     /* Disable the selected SPI peripheral (in I2S mode) */\r
463     SPIx->I2SCFGR &= I2SCFGR_I2SE_Reset;\r
464   }\r
465 }\r
466 \r
467 /**\r
468   * @brief  Enables or disables the specified SPI/I2S interrupts.\r
469   * @param  SPIx: where x can be\r
470   *   - 1, 2 or 3 in SPI mode \r
471   *   - 2 or 3 in I2S mode\r
472   * @param  SPI_I2S_IT: specifies the SPI/I2S interrupt source to be enabled or disabled. \r
473   *   This parameter can be one of the following values:\r
474   *     @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask\r
475   *     @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask\r
476   *     @arg SPI_I2S_IT_ERR: Error interrupt mask\r
477   * @param  NewState: new state of the specified SPI/I2S interrupt.\r
478   *   This parameter can be: ENABLE or DISABLE.\r
479   * @retval None\r
480   */\r
481 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)\r
482 {\r
483   uint16_t itpos = 0, itmask = 0 ;\r
484   /* Check the parameters */\r
485   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
486   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
487   assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));\r
488 \r
489   /* Get the SPI/I2S IT index */\r
490   itpos = SPI_I2S_IT >> 4;\r
491 \r
492   /* Set the IT mask */\r
493   itmask = (uint16_t)1 << (uint16_t)itpos;\r
494 \r
495   if (NewState != DISABLE)\r
496   {\r
497     /* Enable the selected SPI/I2S interrupt */\r
498     SPIx->CR2 |= itmask;\r
499   }\r
500   else\r
501   {\r
502     /* Disable the selected SPI/I2S interrupt */\r
503     SPIx->CR2 &= (uint16_t)~itmask;\r
504   }\r
505 }\r
506 \r
507 /**\r
508   * @brief  Enables or disables the SPIx/I2Sx DMA interface.\r
509   * @param  SPIx: where x can be\r
510   *   - 1, 2 or 3 in SPI mode \r
511   *   - 2 or 3 in I2S mode\r
512   * @param  SPI_I2S_DMAReq: specifies the SPI/I2S DMA transfer request to be enabled or disabled. \r
513   *   This parameter can be any combination of the following values:\r
514   *     @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request\r
515   *     @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request\r
516   * @param  NewState: new state of the selected SPI/I2S DMA transfer request.\r
517   *   This parameter can be: ENABLE or DISABLE.\r
518   * @retval None\r
519   */\r
520 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)\r
521 {\r
522   /* Check the parameters */\r
523   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
524   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
525   assert_param(IS_SPI_I2S_DMAREQ(SPI_I2S_DMAReq));\r
526   if (NewState != DISABLE)\r
527   {\r
528     /* Enable the selected SPI/I2S DMA requests */\r
529     SPIx->CR2 |= SPI_I2S_DMAReq;\r
530   }\r
531   else\r
532   {\r
533     /* Disable the selected SPI/I2S DMA requests */\r
534     SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;\r
535   }\r
536 }\r
537 \r
538 /**\r
539   * @brief  Transmits a Data through the SPIx/I2Sx peripheral.\r
540   * @param  SPIx: where x can be\r
541   *   - 1, 2 or 3 in SPI mode \r
542   *   - 2 or 3 in I2S mode\r
543   * @param  Data : Data to be transmitted.\r
544   * @retval None\r
545   */\r
546 void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)\r
547 {\r
548   /* Check the parameters */\r
549   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
550   \r
551   /* Write in the DR register the data to be sent */\r
552   SPIx->DR = Data;\r
553 }\r
554 \r
555 /**\r
556   * @brief  Returns the most recent received data by the SPIx/I2Sx peripheral. \r
557   * @param  SPIx: where x can be\r
558   *   - 1, 2 or 3 in SPI mode \r
559   *   - 2 or 3 in I2S mode\r
560   * @retval The value of the received data.\r
561   */\r
562 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)\r
563 {\r
564   /* Check the parameters */\r
565   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
566   \r
567   /* Return the data in the DR register */\r
568   return SPIx->DR;\r
569 }\r
570 \r
571 /**\r
572   * @brief  Configures internally by software the NSS pin for the selected SPI.\r
573   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
574   * @param  SPI_NSSInternalSoft: specifies the SPI NSS internal state.\r
575   *   This parameter can be one of the following values:\r
576   *     @arg SPI_NSSInternalSoft_Set: Set NSS pin internally\r
577   *     @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally\r
578   * @retval None\r
579   */\r
580 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)\r
581 {\r
582   /* Check the parameters */\r
583   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
584   assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));\r
585   if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)\r
586   {\r
587     /* Set NSS pin internally by software */\r
588     SPIx->CR1 |= SPI_NSSInternalSoft_Set;\r
589   }\r
590   else\r
591   {\r
592     /* Reset NSS pin internally by software */\r
593     SPIx->CR1 &= SPI_NSSInternalSoft_Reset;\r
594   }\r
595 }\r
596 \r
597 /**\r
598   * @brief  Enables or disables the SS output for the selected SPI.\r
599   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
600   * @param  NewState: new state of the SPIx SS output. \r
601   *   This parameter can be: ENABLE or DISABLE.\r
602   * @retval None\r
603   */\r
604 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
605 {\r
606   /* Check the parameters */\r
607   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
608   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
609   if (NewState != DISABLE)\r
610   {\r
611     /* Enable the selected SPI SS output */\r
612     SPIx->CR2 |= CR2_SSOE_Set;\r
613   }\r
614   else\r
615   {\r
616     /* Disable the selected SPI SS output */\r
617     SPIx->CR2 &= CR2_SSOE_Reset;\r
618   }\r
619 }\r
620 \r
621 /**\r
622   * @brief  Configures the data size for the selected SPI.\r
623   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
624   * @param  SPI_DataSize: specifies the SPI data size.\r
625   *   This parameter can be one of the following values:\r
626   *     @arg SPI_DataSize_16b: Set data frame format to 16bit\r
627   *     @arg SPI_DataSize_8b: Set data frame format to 8bit\r
628   * @retval None\r
629   */\r
630 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)\r
631 {\r
632   /* Check the parameters */\r
633   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
634   assert_param(IS_SPI_DATASIZE(SPI_DataSize));\r
635   /* Clear DFF bit */\r
636   SPIx->CR1 &= (uint16_t)~SPI_DataSize_16b;\r
637   /* Set new DFF bit value */\r
638   SPIx->CR1 |= SPI_DataSize;\r
639 }\r
640 \r
641 /**\r
642   * @brief  Transmit the SPIx CRC value.\r
643   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
644   * @retval None\r
645   */\r
646 void SPI_TransmitCRC(SPI_TypeDef* SPIx)\r
647 {\r
648   /* Check the parameters */\r
649   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
650   \r
651   /* Enable the selected SPI CRC transmission */\r
652   SPIx->CR1 |= CR1_CRCNext_Set;\r
653 }\r
654 \r
655 /**\r
656   * @brief  Enables or disables the CRC value calculation of the transfered bytes.\r
657   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
658   * @param  NewState: new state of the SPIx CRC value calculation.\r
659   *   This parameter can be: ENABLE or DISABLE.\r
660   * @retval None\r
661   */\r
662 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)\r
663 {\r
664   /* Check the parameters */\r
665   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
666   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
667   if (NewState != DISABLE)\r
668   {\r
669     /* Enable the selected SPI CRC calculation */\r
670     SPIx->CR1 |= CR1_CRCEN_Set;\r
671   }\r
672   else\r
673   {\r
674     /* Disable the selected SPI CRC calculation */\r
675     SPIx->CR1 &= CR1_CRCEN_Reset;\r
676   }\r
677 }\r
678 \r
679 /**\r
680   * @brief  Returns the transmit or the receive CRC register value for the specified SPI.\r
681   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
682   * @param  SPI_CRC: specifies the CRC register to be read.\r
683   *   This parameter can be one of the following values:\r
684   *     @arg SPI_CRC_Tx: Selects Tx CRC register\r
685   *     @arg SPI_CRC_Rx: Selects Rx CRC register\r
686   * @retval The selected CRC register value..\r
687   */\r
688 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)\r
689 {\r
690   uint16_t crcreg = 0;\r
691   /* Check the parameters */\r
692   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
693   assert_param(IS_SPI_CRC(SPI_CRC));\r
694   if (SPI_CRC != SPI_CRC_Rx)\r
695   {\r
696     /* Get the Tx CRC register */\r
697     crcreg = SPIx->TXCRCR;\r
698   }\r
699   else\r
700   {\r
701     /* Get the Rx CRC register */\r
702     crcreg = SPIx->RXCRCR;\r
703   }\r
704   /* Return the selected CRC register */\r
705   return crcreg;\r
706 }\r
707 \r
708 /**\r
709   * @brief  Returns the CRC Polynomial register value for the specified SPI.\r
710   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
711   * @retval The CRC Polynomial register value.\r
712   */\r
713 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)\r
714 {\r
715   /* Check the parameters */\r
716   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
717   \r
718   /* Return the CRC polynomial register */\r
719   return SPIx->CRCPR;\r
720 }\r
721 \r
722 /**\r
723   * @brief  Selects the data transfer direction in bi-directional mode for the specified SPI.\r
724   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.\r
725   * @param  SPI_Direction: specifies the data transfer direction in bi-directional mode. \r
726   *   This parameter can be one of the following values:\r
727   *     @arg SPI_Direction_Tx: Selects Tx transmission direction\r
728   *     @arg SPI_Direction_Rx: Selects Rx receive direction\r
729   * @retval None\r
730   */\r
731 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)\r
732 {\r
733   /* Check the parameters */\r
734   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
735   assert_param(IS_SPI_DIRECTION(SPI_Direction));\r
736   if (SPI_Direction == SPI_Direction_Tx)\r
737   {\r
738     /* Set the Tx only mode */\r
739     SPIx->CR1 |= SPI_Direction_Tx;\r
740   }\r
741   else\r
742   {\r
743     /* Set the Rx only mode */\r
744     SPIx->CR1 &= SPI_Direction_Rx;\r
745   }\r
746 }\r
747 \r
748 /**\r
749   * @brief  Checks whether the specified SPI/I2S flag is set or not.\r
750   * @param  SPIx: where x can be\r
751   *   - 1, 2 or 3 in SPI mode \r
752   *   - 2 or 3 in I2S mode\r
753   * @param  SPI_I2S_FLAG: specifies the SPI/I2S flag to check. \r
754   *   This parameter can be one of the following values:\r
755   *     @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.\r
756   *     @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.\r
757   *     @arg SPI_I2S_FLAG_BSY: Busy flag.\r
758   *     @arg SPI_I2S_FLAG_OVR: Overrun flag.\r
759   *     @arg SPI_FLAG_MODF: Mode Fault flag.\r
760   *     @arg SPI_FLAG_CRCERR: CRC Error flag.\r
761   *     @arg I2S_FLAG_UDR: Underrun Error flag.\r
762   *     @arg I2S_FLAG_CHSIDE: Channel Side flag.\r
763   * @retval The new state of SPI_I2S_FLAG (SET or RESET).\r
764   */\r
765 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)\r
766 {\r
767   FlagStatus bitstatus = RESET;\r
768   /* Check the parameters */\r
769   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
770   assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));\r
771   /* Check the status of the specified SPI/I2S flag */\r
772   if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)\r
773   {\r
774     /* SPI_I2S_FLAG is set */\r
775     bitstatus = SET;\r
776   }\r
777   else\r
778   {\r
779     /* SPI_I2S_FLAG is reset */\r
780     bitstatus = RESET;\r
781   }\r
782   /* Return the SPI_I2S_FLAG status */\r
783   return  bitstatus;\r
784 }\r
785 \r
786 /**\r
787   * @brief  Clears the SPIx CRC Error (CRCERR) flag.\r
788   * @param  SPIx: where x can be\r
789   *   - 1, 2 or 3 in SPI mode \r
790   * @param  SPI_I2S_FLAG: specifies the SPI flag to clear. \r
791   *   This function clears only CRCERR flag.\r
792   * @note\r
793   *   - OVR (OverRun error) flag is cleared by software sequence: a read \r
794   *     operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read \r
795   *     operation to SPI_SR register (SPI_I2S_GetFlagStatus()).\r
796   *   - UDR (UnderRun error) flag is cleared by a read operation to \r
797   *     SPI_SR register (SPI_I2S_GetFlagStatus()).\r
798   *   - MODF (Mode Fault) flag is cleared by software sequence: a read/write \r
799   *     operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a \r
800   *     write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).\r
801   * @retval None\r
802   */\r
803 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)\r
804 {\r
805   /* Check the parameters */\r
806   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
807   assert_param(IS_SPI_I2S_CLEAR_FLAG(SPI_I2S_FLAG));\r
808     \r
809     /* Clear the selected SPI CRC Error (CRCERR) flag */\r
810     SPIx->SR = (uint16_t)~SPI_I2S_FLAG;\r
811 }\r
812 \r
813 /**\r
814   * @brief  Checks whether the specified SPI/I2S interrupt has occurred or not.\r
815   * @param  SPIx: where x can be\r
816   *   - 1, 2 or 3 in SPI mode \r
817   *   - 2 or 3 in I2S mode\r
818   * @param  SPI_I2S_IT: specifies the SPI/I2S interrupt source to check. \r
819   *   This parameter can be one of the following values:\r
820   *     @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.\r
821   *     @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.\r
822   *     @arg SPI_I2S_IT_OVR: Overrun interrupt.\r
823   *     @arg SPI_IT_MODF: Mode Fault interrupt.\r
824   *     @arg SPI_IT_CRCERR: CRC Error interrupt.\r
825   *     @arg I2S_IT_UDR: Underrun Error interrupt.\r
826   * @retval The new state of SPI_I2S_IT (SET or RESET).\r
827   */\r
828 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)\r
829 {\r
830   ITStatus bitstatus = RESET;\r
831   uint16_t itpos = 0, itmask = 0, enablestatus = 0;\r
832 \r
833   /* Check the parameters */\r
834   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
835   assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));\r
836 \r
837   /* Get the SPI/I2S IT index */\r
838   itpos = 0x01 << (SPI_I2S_IT & 0x0F);\r
839 \r
840   /* Get the SPI/I2S IT mask */\r
841   itmask = SPI_I2S_IT >> 4;\r
842 \r
843   /* Set the IT mask */\r
844   itmask = 0x01 << itmask;\r
845 \r
846   /* Get the SPI_I2S_IT enable bit status */\r
847   enablestatus = (SPIx->CR2 & itmask) ;\r
848 \r
849   /* Check the status of the specified SPI/I2S interrupt */\r
850   if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)\r
851   {\r
852     /* SPI_I2S_IT is set */\r
853     bitstatus = SET;\r
854   }\r
855   else\r
856   {\r
857     /* SPI_I2S_IT is reset */\r
858     bitstatus = RESET;\r
859   }\r
860   /* Return the SPI_I2S_IT status */\r
861   return bitstatus;\r
862 }\r
863 \r
864 /**\r
865   * @brief  Clears the SPIx CRC Error (CRCERR) interrupt pending bit.\r
866   * @param  SPIx: where x can be\r
867   *   - 1, 2 or 3 in SPI mode \r
868   * @param  SPI_I2S_IT: specifies the SPI interrupt pending bit to clear.\r
869   *   This function clears only CRCERR intetrrupt pending bit.   \r
870   * @note\r
871   *   - OVR (OverRun Error) interrupt pending bit is cleared by software \r
872   *     sequence: a read operation to SPI_DR register (SPI_I2S_ReceiveData()) \r
873   *     followed by a read operation to SPI_SR register (SPI_I2S_GetITStatus()).\r
874   *   - UDR (UnderRun Error) interrupt pending bit is cleared by a read \r
875   *     operation to SPI_SR register (SPI_I2S_GetITStatus()).\r
876   *   - MODF (Mode Fault) interrupt pending bit is cleared by software sequence:\r
877   *     a read/write operation to SPI_SR register (SPI_I2S_GetITStatus()) \r
878   *     followed by a write operation to SPI_CR1 register (SPI_Cmd() to enable \r
879   *     the SPI).\r
880   * @retval None\r
881   */\r
882 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)\r
883 {\r
884   uint16_t itpos = 0;\r
885   /* Check the parameters */\r
886   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
887   assert_param(IS_SPI_I2S_CLEAR_IT(SPI_I2S_IT));\r
888 \r
889   /* Get the SPI IT index */\r
890   itpos = 0x01 << (SPI_I2S_IT & 0x0F);\r
891 \r
892   /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */\r
893   SPIx->SR = (uint16_t)~itpos;\r
894 }\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 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r