Merge pull request #93 from zyp/master
[fw/stlink] / example / libs_stm / src / stm32l1xx / stm32l1xx_spi.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_spi.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Serial peripheral interface (SPI):           \r
9   *           - Initialization and Configuration\r
10   *           - Data transfers functions\r
11   *           - Hardware CRC Calculation\r
12   *           - DMA transfers management\r
13   *           - Interrupts and flags management \r
14   *           \r
15   *  @verbatim\r
16   *          \r
17   *          The I2S feature is not implemented in STM32L1xx Ultra Low Power\r
18   *          Medium-density devices and will be supported in future products.\r
19   *                    \r
20   *          ===================================================================\r
21   *                                 How to use this driver\r
22   *          ===================================================================\r
23   *          1. Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE)\r
24   *             function for SPI1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE)\r
25   *             function for SPI2.\r
26   *\r
27   *          2. Enable SCK, MOSI, MISO and NSS GPIO clocks using RCC_AHBPeriphClockCmd()\r
28   *             function. \r
29   *\r
30   *          3. Peripherals alternate function: \r
31   *                 - Connect the pin to the desired peripherals' Alternate \r
32   *                   Function (AF) using GPIO_PinAFConfig() function\r
33   *                 - Configure the desired pin in alternate function by:\r
34   *                   GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF\r
35   *                 - Select the type, pull-up/pull-down and output speed via \r
36   *                   GPIO_PuPd, GPIO_OType and GPIO_Speed members\r
37   *                 - Call GPIO_Init() function\r
38   *        \r
39   *          4. Program the Polarity, Phase, First Data, Baud Rate Prescaler, Slave \r
40   *             Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()\r
41   *             function.\r
42   *\r
43   *          5. Enable the NVIC and the corresponding interrupt using the function \r
44   *             SPI_ITConfig() if you need to use interrupt mode. \r
45   *\r
46   *          6. When using the DMA mode \r
47   *                   - Configure the DMA using DMA_Init() function\r
48   *                   - Active the needed channel Request using SPI_I2S_DMACmd() function\r
49   * \r
50   *          7. Enable the SPI using the SPI_Cmd() function.\r
51   * \r
52   *          8. Enable the DMA using the DMA_Cmd() function when using DMA mode. \r
53   *\r
54   *          9. Optionally you can enable/configure the following parameters without\r
55   *             re-initialization (i.e there is no need to call again SPI_Init() function):\r
56   *              - When bidirectional mode (SPI_Direction_1Line_Rx or SPI_Direction_1Line_Tx)\r
57   *                is programmed as Data direction parameter using the SPI_Init() function\r
58   *                it can be possible to switch between SPI_Direction_Tx or SPI_Direction_Rx\r
59   *                using the SPI_BiDirectionalLineConfig() function.\r
60   *              - When SPI_NSS_Soft is selected as Slave Select Management parameter \r
61   *                using the SPI_Init() function it can be possible to manage the \r
62   *                NSS internal signal using the SPI_NSSInternalSoftwareConfig() function.\r
63   *              -  Reconfigure the data size using the SPI_DataSizeConfig() function  \r
64   *              -  Enable or disable the SS output using the SPI_SSOutputCmd() function  \r
65   *          \r
66   *          10. To use the CRC Hardware calculation feature refer to the Peripheral \r
67   *              CRC hardware Calculation subsection.\r
68   *\r
69   *  @endverbatim  \r
70   *                                  \r
71   ******************************************************************************\r
72   * @attention\r
73   *\r
74   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
75   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
76   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
77   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
78   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
79   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
80   *\r
81   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
82   ******************************************************************************  \r
83   */ \r
84 \r
85 /* Includes ------------------------------------------------------------------*/\r
86 #include "stm32l1xx_spi.h"\r
87 #include "stm32l1xx_rcc.h"\r
88 \r
89 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
90   * @{\r
91   */\r
92 \r
93 /** @defgroup SPI \r
94   * @brief SPI driver modules\r
95   * @{\r
96   */ \r
97 \r
98 /* Private typedef -----------------------------------------------------------*/\r
99 /* Private define ------------------------------------------------------------*/\r
100 /* SPI registers Masks */\r
101 #define CR1_CLEAR_MASK       ((uint16_t)0x3040)\r
102 \r
103 /* Private macro -------------------------------------------------------------*/\r
104 /* Private variables ---------------------------------------------------------*/\r
105 /* Private function prototypes -----------------------------------------------*/\r
106 /* Private functions ---------------------------------------------------------*/\r
107 \r
108 /** @defgroup SPI_Private_Functions\r
109   * @{\r
110   */\r
111 \r
112 /** @defgroup SPI_Group1 Initialization and Configuration functions\r
113  *  @brief   Initialization and Configuration functions \r
114  *\r
115 @verbatim   \r
116  ===============================================================================\r
117                   Initialization and Configuration functions\r
118  ===============================================================================  \r
119 \r
120   This section provides a set of functions allowing to initialize the SPI Direction,\r
121   SPI Mode, SPI Data Size, SPI Polarity, SPI Phase, SPI NSS Management, SPI Baud\r
122   Rate Prescaler, SPI First Bit and SPI CRC Polynomial.\r
123   \r
124   The SPI_Init() function follows the SPI configuration procedures for Master mode\r
125   and Slave mode (details for these procedures are available in reference manual\r
126   (RM0038)).\r
127   \r
128 @endverbatim\r
129   * @{\r
130   */\r
131 \r
132 /**\r
133   * @brief  Deinitializes the SPIx peripheral registers to their default\r
134   *         reset values.\r
135   * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.\r
136   * @retval None\r
137   */\r
138 void SPI_I2S_DeInit(SPI_TypeDef* SPIx)\r
139 {\r
140   /* Check the parameters */\r
141   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
142 \r
143   if (SPIx == SPI1)\r
144   {\r
145     /* Enable SPI1 reset state */\r
146     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);\r
147     /* Release SPI1 from reset state */\r
148     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);\r
149   }\r
150   else\r
151   {\r
152     if (SPIx == SPI2)\r
153     {\r
154       /* Enable SPI2 reset state */\r
155       RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);\r
156       /* Release SPI2 from reset state */\r
157       RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);\r
158     }\r
159   }\r
160 }\r
161 \r
162 /**\r
163   * @brief  Initializes the SPIx peripheral according to the specified \r
164   *   parameters in the SPI_InitStruct.\r
165   * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.\r
166   * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure that\r
167   *   contains the configuration information for the specified SPI peripheral.\r
168   * @retval None\r
169   */\r
170 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)\r
171 {\r
172   uint16_t tmpreg = 0;\r
173   \r
174   /* check the parameters */\r
175   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
176   \r
177   /* Check the SPI parameters */\r
178   assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));\r
179   assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));\r
180   assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));\r
181   assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));\r
182   assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));\r
183   assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));\r
184   assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));\r
185   assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));\r
186   assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));\r
187 \r
188 /*---------------------------- SPIx CR1 Configuration ------------------------*/\r
189   /* Get the SPIx CR1 value */\r
190   tmpreg = SPIx->CR1;\r
191   /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */\r
192   tmpreg &= CR1_CLEAR_MASK;\r
193   /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler\r
194      master/salve mode, CPOL and CPHA */\r
195   /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */\r
196   /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */\r
197   /* Set LSBFirst bit according to SPI_FirstBit value */\r
198   /* Set BR bits according to SPI_BaudRatePrescaler value */\r
199   /* Set CPOL bit according to SPI_CPOL value */\r
200   /* Set CPHA bit according to SPI_CPHA value */\r
201   tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |\r
202                   SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |  \r
203                   SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |  \r
204                   SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);\r
205   /* Write to SPIx CR1 */\r
206   SPIx->CR1 = tmpreg;\r
207   \r
208 /*---------------------------- SPIx CRCPOLY Configuration --------------------*/\r
209   /* Write to SPIx CRCPOLY */\r
210   SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;\r
211 }\r
212 \r
213 /**\r
214   * @brief  Fills each SPI_InitStruct member with its default value.\r
215   * @param  SPI_InitStruct : pointer to a SPI_InitTypeDef structure which will be initialized.\r
216   * @retval None\r
217   */\r
218 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)\r
219 {\r
220 /*--------------- Reset SPI init structure parameters values -----------------*/\r
221   /* Initialize the SPI_Direction member */\r
222   SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;\r
223   /* initialize the SPI_Mode member */\r
224   SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;\r
225   /* initialize the SPI_DataSize member */\r
226   SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;\r
227   /* Initialize the SPI_CPOL member */\r
228   SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;\r
229   /* Initialize the SPI_CPHA member */\r
230   SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;\r
231   /* Initialize the SPI_NSS member */\r
232   SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;\r
233   /* Initialize the SPI_BaudRatePrescaler member */\r
234   SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;\r
235   /* Initialize the SPI_FirstBit member */\r
236   SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;\r
237   /* Initialize the SPI_CRCPolynomial member */\r
238   SPI_InitStruct->SPI_CRCPolynomial = 7;\r
239 }\r
240 \r
241 /**\r
242   * @brief  Enables or disables the specified SPI peripheral.\r
243   * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.\r
244   * @param  NewState: new state of the SPIx peripheral. \r
245   *   This parameter can be: ENABLE or DISABLE.\r
246   * @retval None\r
247   */\r
248 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
249 {\r
250   /* Check the parameters */\r
251   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
252   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
253   if (NewState != DISABLE)\r
254   {\r
255     /* Enable the selected SPI peripheral */\r
256     SPIx->CR1 |= SPI_CR1_SPE;\r
257   }\r
258   else\r
259   {\r
260     /* Disable the selected SPI peripheral */\r
261     SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_SPE);\r
262   }\r
263 }\r
264 \r
265 /**\r
266   * @brief  Configures the data size for the selected SPI.\r
267   * @param  SPIx: where x can be 1 or 2  to select the SPI peripheral.\r
268   * @param  SPI_DataSize: specifies the SPI data size.\r
269   *   This parameter can be one of the following values:\r
270   *     @arg SPI_DataSize_16b: Set data frame format to 16bit\r
271   *     @arg SPI_DataSize_8b: Set data frame format to 8bit\r
272   * @retval None\r
273   */\r
274 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)\r
275 {\r
276   /* Check the parameters */\r
277   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
278   assert_param(IS_SPI_DATASIZE(SPI_DataSize));\r
279   /* Clear DFF bit */\r
280   SPIx->CR1 &= (uint16_t)~SPI_DataSize_16b;\r
281   /* Set new DFF bit value */\r
282   SPIx->CR1 |= SPI_DataSize;\r
283 }\r
284 \r
285 /**\r
286   * @brief  Selects the data transfer direction in bidirectional mode for the specified SPI.\r
287   * @param  SPIx: where x can be 1 or 2  to select the SPI peripheral.\r
288   * @param  SPI_Direction: specifies the data transfer direction in bidirectional mode. \r
289   *   This parameter can be one of the following values:\r
290   *     @arg SPI_Direction_Tx: Selects Tx transmission direction\r
291   *     @arg SPI_Direction_Rx: Selects Rx receive direction\r
292   * @retval None\r
293   */\r
294 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)\r
295 {\r
296   /* Check the parameters */\r
297   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
298   assert_param(IS_SPI_DIRECTION(SPI_Direction));\r
299   if (SPI_Direction == SPI_Direction_Tx)\r
300   {\r
301     /* Set the Tx only mode */\r
302     SPIx->CR1 |= SPI_Direction_Tx;\r
303   }\r
304   else\r
305   {\r
306     /* Set the Rx only mode */\r
307     SPIx->CR1 &= SPI_Direction_Rx;\r
308   }\r
309 }\r
310 \r
311 /**\r
312   * @brief  Configures internally by software the NSS pin for the selected SPI.\r
313   * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.\r
314   * @param  SPI_NSSInternalSoft: specifies the SPI NSS internal state.\r
315   *   This parameter can be one of the following values:\r
316   *     @arg SPI_NSSInternalSoft_Set: Set NSS pin internally\r
317   *     @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally\r
318   * @retval None\r
319   */\r
320 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)\r
321 {\r
322   /* Check the parameters */\r
323   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
324   assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));\r
325   if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)\r
326   {\r
327     /* Set NSS pin internally by software */\r
328     SPIx->CR1 |= SPI_NSSInternalSoft_Set;\r
329   }\r
330   else\r
331   {\r
332     /* Reset NSS pin internally by software */\r
333     SPIx->CR1 &= SPI_NSSInternalSoft_Reset;\r
334   }\r
335 }\r
336 \r
337 /**\r
338   * @brief  Enables or disables the SS output for the selected SPI.\r
339   * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.\r
340   * @param  NewState: new state of the SPIx SS output. \r
341   *   This parameter can be: ENABLE or DISABLE.\r
342   * @retval None\r
343   */\r
344 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)\r
345 {\r
346   /* Check the parameters */\r
347   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
348   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
349   if (NewState != DISABLE)\r
350   {\r
351     /* Enable the selected SPI SS output */\r
352     SPIx->CR2 |= (uint16_t)SPI_CR2_SSOE;\r
353   }\r
354   else\r
355   {\r
356     /* Disable the selected SPI SS output */\r
357     SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_SSOE);\r
358   }\r
359 }\r
360 \r
361 /**\r
362   * @}\r
363   */\r
364 \r
365 /** @defgroup SPI_Group2 Data transfers functions\r
366  *  @brief   Data transfers functions\r
367  *\r
368 @verbatim   \r
369  ===============================================================================\r
370                          Data transfers functions\r
371  ===============================================================================  \r
372 \r
373   This section provides a set of functions allowing to manage the SPI data transfers\r
374   \r
375   In reception, data are received and then stored into an internal Rx buffer while \r
376   In transmission, data are first stored into an internal Tx buffer before being \r
377   transmitted.\r
378 \r
379   The read access of the SPI_DR register can be done using the SPI_I2S_ReceiveData()\r
380   function and returns the Rx buffered value. Whereas a write access to the SPI_DR \r
381   can be done using SPI_I2S_SendData() function and stores the written data into \r
382   Tx buffer.\r
383 \r
384 @endverbatim\r
385   * @{\r
386   */\r
387 \r
388 /**\r
389   * @brief  Returns the most recent received data by the SPIx peripheral. \r
390   * @param  SPIx: where x can be 1 or 2 in SPI mode.\r
391   * @retval The value of the received data.\r
392   */\r
393 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)\r
394 {\r
395   /* Check the parameters */\r
396   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
397   \r
398   /* Return the data in the DR register */\r
399   return SPIx->DR;\r
400 }\r
401 \r
402 /**\r
403   * @brief  Transmits a Data through the SPIx peripheral.\r
404   * @param  SPIx: where x can be 1 or 2 in SPI mode. \r
405   * @param  Data: Data to be transmitted.\r
406   * @retval None\r
407   */\r
408 void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)\r
409 {\r
410   /* Check the parameters */\r
411   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
412   \r
413   /* Write in the DR register the data to be sent */\r
414   SPIx->DR = Data;\r
415 }\r
416 \r
417 /**\r
418   * @}\r
419   */\r
420 \r
421 /** @defgroup SPI_Group3 Hardware CRC Calculation functions\r
422  *  @brief   Hardware CRC Calculation functions\r
423  *\r
424 @verbatim   \r
425  ===============================================================================\r
426                          Hardware CRC Calculation functions\r
427  ===============================================================================  \r
428 \r
429   This section provides a set of functions allowing to manage the SPI CRC hardware \r
430   calculation\r
431 \r
432   SPI communication using CRC is possible through the following procedure:\r
433      1. Program the Data direction, Polarity, Phase, First Data, Baud Rate Prescaler, \r
434         Slave Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()\r
435         function.\r
436      2. Enable the CRC calculation using the SPI_CalculateCRC() function.\r
437      3. Enable the SPI using the SPI_Cmd() function\r
438      4. Before writing the last data to the TX buffer, set the CRCNext bit using the \r
439       SPI_TransmitCRC() function to indicate that after transmission of the last \r
440       data, the CRC should be transmitted.\r
441      5. After transmitting the last data, the SPI transmits the CRC. The SPI_CR1_CRCNEXT\r
442         bit is reset. The CRC is also received and compared against the SPI_RXCRCR \r
443         value. \r
444         If the value does not match, the SPI_FLAG_CRCERR flag is set and an interrupt\r
445         can be generated when the SPI_I2S_IT_ERR interrupt is enabled.\r
446 \r
447 Note: \r
448 -----\r
449     - It is advised to don't read the calculate CRC values during the communication.\r
450 \r
451     - When the SPI is in slave mode, be careful to enable CRC calculation only \r
452       when the clock is stable, that is, when the clock is in the steady state. \r
453       If not, a wrong CRC calculation may be done. In fact, the CRC is sensitive \r
454       to the SCK slave input clock as soon as CRCEN is set, and this, whatever \r
455       the value of the SPE bit.\r
456 \r
457     - With high bitrate frequencies, be careful when transmitting the CRC.\r
458       As the number of used CPU cycles has to be as low as possible in the CRC \r
459       transfer phase, it is forbidden to call software functions in the CRC \r
460       transmission sequence to avoid errors in the last data and CRC reception. \r
461       In fact, CRCNEXT bit has to be written before the end of the transmission/reception \r
462       of the last data.\r
463 \r
464     - For high bit rate frequencies, it is advised to use the DMA mode to avoid the\r
465       degradation of the SPI speed performance due to CPU accesses impacting the \r
466       SPI bandwidth.\r
467 \r
468     - When the STM32L15xxx are configured as slaves and the NSS hardware mode is \r
469       used, the NSS pin needs to be kept low between the data phase and the CRC \r
470       phase.\r
471 \r
472     - When the SPI is configured in slave mode with the CRC feature enabled, CRC\r
473       calculation takes place even if a high level is applied on the NSS pin. \r
474       This may happen for example in case of a multislave environment where the \r
475       communication master addresses slaves alternately.\r
476 \r
477     - Between a slave deselection (high level on NSS) and a new slave selection \r
478       (low level on NSS), the CRC value should be cleared on both master and slave\r
479       sides in order to resynchronize the master and slave for their respective \r
480       CRC calculation.\r
481 \r
482     To clear the CRC, follow the procedure below:\r
483       1. Disable SPI using the SPI_Cmd() function\r
484       2. Disable the CRC calculation using the SPI_CalculateCRC() function.\r
485       3. Enable the CRC calculation using the SPI_CalculateCRC() function.\r
486       4. Enable SPI using the SPI_Cmd() function.\r
487 \r
488 @endverbatim\r
489   * @{\r
490   */\r
491 \r
492 /**\r
493   * @brief  Enables or disables the CRC value calculation of the transferred bytes.\r
494   * @param  SPIx: where x can be 1 or 2  to select the SPI peripheral.\r
495   * @param  NewState: new state of the SPIx CRC value calculation.\r
496   *   This parameter can be: ENABLE or DISABLE.\r
497   * @retval None\r
498   */\r
499 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)\r
500 {\r
501   /* Check the parameters */\r
502   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
503   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
504   if (NewState != DISABLE)\r
505   {\r
506     /* Enable the selected SPI CRC calculation */\r
507     SPIx->CR1 |= SPI_CR1_CRCEN;\r
508   }\r
509   else\r
510   {\r
511     /* Disable the selected SPI CRC calculation */\r
512     SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_CRCEN);\r
513   }\r
514 }\r
515 \r
516 /**\r
517   * @brief  Transmit the SPIx CRC value.\r
518   * @param  SPIx: where x can be 1 or 2  to select the SPI peripheral.\r
519   * @retval None\r
520   */\r
521 void SPI_TransmitCRC(SPI_TypeDef* SPIx)\r
522 {\r
523   /* Check the parameters */\r
524   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
525   \r
526   /* Enable the selected SPI CRC transmission */\r
527   SPIx->CR1 |= SPI_CR1_CRCNEXT;\r
528 }\r
529 \r
530 /**\r
531   * @brief  Returns the transmit or the receive CRC register value for the specified SPI.\r
532   * @param  SPIx: where x can be 1 or 2  to select the SPI peripheral.\r
533   * @param  SPI_CRC: specifies the CRC register to be read.\r
534   *   This parameter can be one of the following values:\r
535   *     @arg SPI_CRC_Tx: Selects Tx CRC register\r
536   *     @arg SPI_CRC_Rx: Selects Rx CRC register\r
537   * @retval The selected CRC register value..\r
538   */\r
539 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)\r
540 {\r
541   uint16_t crcreg = 0;\r
542   /* Check the parameters */\r
543   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
544   assert_param(IS_SPI_CRC(SPI_CRC));\r
545   if (SPI_CRC != SPI_CRC_Rx)\r
546   {\r
547     /* Get the Tx CRC register */\r
548     crcreg = SPIx->TXCRCR;\r
549   }\r
550   else\r
551   {\r
552     /* Get the Rx CRC register */\r
553     crcreg = SPIx->RXCRCR;\r
554   }\r
555   /* Return the selected CRC register */\r
556   return crcreg;\r
557 }\r
558 \r
559 /**\r
560   * @brief  Returns the CRC Polynomial register value for the specified SPI.\r
561   * @param  SPIx: where x can be 1 or 2  to select the SPI peripheral.\r
562   * @retval The CRC Polynomial register value.\r
563   */\r
564 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)\r
565 {\r
566   /* Check the parameters */\r
567   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
568   \r
569   /* Return the CRC polynomial register */\r
570   return SPIx->CRCPR;\r
571 }\r
572 \r
573 /**\r
574   * @}\r
575   */\r
576 \r
577 /** @defgroup SPI_Group4 DMA transfers management functions\r
578  *  @brief   DMA transfers management functions\r
579   *\r
580 @verbatim   \r
581  ===============================================================================\r
582                          DMA transfers management functions\r
583  ===============================================================================  \r
584 \r
585 @endverbatim\r
586   * @{\r
587   */\r
588 \r
589 /**\r
590   * @brief  Enables or disables the SPIx DMA interface.\r
591   * @param  SPIx: where x can be 1 or 2 in SPI mode \r
592   * @param  SPI_I2S_DMAReq: specifies the SPI DMA transfer request to be enabled or disabled. \r
593   *   This parameter can be any combination of the following values:\r
594   *     @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request\r
595   *     @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request\r
596   * @param  NewState: new state of the selected SPI DMA transfer request.\r
597   *   This parameter can be: ENABLE or DISABLE.\r
598   * @retval None\r
599   */\r
600 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)\r
601 {\r
602   /* Check the parameters */\r
603   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
604   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
605   assert_param(IS_SPI_I2S_DMAREQ(SPI_I2S_DMAReq));\r
606 \r
607   if (NewState != DISABLE)\r
608   {\r
609     /* Enable the selected SPI DMA requests */\r
610     SPIx->CR2 |= SPI_I2S_DMAReq;\r
611   }\r
612   else\r
613   {\r
614     /* Disable the selected SPI DMA requests */\r
615     SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;\r
616   }\r
617 }\r
618 \r
619 /**\r
620   * @}\r
621   */\r
622 \r
623 /** @defgroup SPI_Group5 Interrupts and flags management functions\r
624  *  @brief   Interrupts and flags management functions\r
625   *\r
626 @verbatim   \r
627  ===============================================================================\r
628                          Interrupts and flags management functions\r
629  ===============================================================================  \r
630 \r
631   This section provides a set of functions allowing to configure the SPI Interrupts \r
632   sources and check or clear the flags or pending bits status.\r
633   The user should identify which mode will be used in his application to manage \r
634   the communication: Polling mode, Interrupt mode or DMA mode. \r
635     \r
636   Polling Mode\r
637   =============\r
638   In Polling Mode, the SPI communication can be managed by 6 flags:\r
639      1. SPI_I2S_FLAG_TXE : to indicate the status of the transmit buffer register\r
640      2. SPI_I2S_FLAG_RXNE : to indicate the status of the receive buffer register\r
641      3. SPI_I2S_FLAG_BSY : to indicate the state of the communication layer of the SPI.\r
642      4. SPI_FLAG_CRCERR : to indicate if a CRC Calculation error occur              \r
643      5. SPI_FLAG_MODF : to indicate if a Mode Fault error occur\r
644      6. SPI_I2S_FLAG_OVR : to indicate if an Overrun error occur\r
645 \r
646 Note: Do not use the BSY flag to handle each data transmission or reception.\r
647 ----- It is better to use the TXE and RXNE flags instead.\r
648 \r
649   In this Mode it is advised to use the following functions:\r
650      - FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);\r
651      - void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);\r
652 \r
653   Interrupt Mode\r
654   ===============\r
655   In Interrupt Mode, the SPI communication can be managed by 3 interrupt sources\r
656   and 5 pending bits: \r
657   Pending Bits:\r
658   ------------- \r
659      1. SPI_I2S_IT_TXE : to indicate the status of the transmit buffer register\r
660      2. SPI_I2S_IT_RXNE : to indicate the status of the receive buffer register\r
661      3. SPI_IT_CRCERR : to indicate if a CRC Calculation error occur              \r
662      4. SPI_IT_MODF : to indicate if a Mode Fault error occur\r
663      5. SPI_I2S_IT_OVR : to indicate if an Overrun error occur\r
664 \r
665   Interrupt Source:\r
666   -----------------\r
667      1. SPI_I2S_IT_TXE: specifies the interrupt source for the Tx buffer empty \r
668                         interrupt.  \r
669      2. SPI_I2S_IT_RXNE : specifies the interrupt source for the Rx buffer not \r
670                           empty interrupt.\r
671      3. SPI_I2S_IT_ERR : specifies the interrupt source for the errors interrupt.\r
672 \r
673   In this Mode it is advised to use the following functions:\r
674      - void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);\r
675      - ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);\r
676      - void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);\r
677 \r
678   DMA Mode\r
679   ========\r
680   In DMA Mode, the SPI communication can be managed by 2 DMA Channel requests:\r
681      1. SPI_I2S_DMAReq_Tx: specifies the Tx buffer DMA transfer request\r
682      2. SPI_I2S_DMAReq_Rx: specifies the Rx buffer DMA transfer request\r
683 \r
684   In this Mode it is advised to use the following function:\r
685     - void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);\r
686 \r
687 @endverbatim\r
688   * @{\r
689   */\r
690 \r
691 /**\r
692   * @brief  Enables or disables the specified SPI interrupts.\r
693   * @param  SPIx: where x can be 1 or 2 in SPI mode \r
694   * @param  SPI_I2S_IT: specifies the SPI interrupt source to be enabled or disabled. \r
695   *   This parameter can be one of the following values:\r
696   *     @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask\r
697   *     @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask\r
698   *     @arg SPI_I2S_IT_ERR: Error interrupt mask\r
699   * @param  NewState: new state of the specified SPI interrupt.\r
700   *   This parameter can be: ENABLE or DISABLE.\r
701   * @retval None\r
702   */\r
703 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)\r
704 {\r
705   uint16_t itpos = 0, itmask = 0 ;\r
706   \r
707   /* Check the parameters */\r
708   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
709   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
710   assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));\r
711 \r
712   /* Get the SPI IT index */\r
713   itpos = SPI_I2S_IT >> 4;\r
714 \r
715   /* Set the IT mask */\r
716   itmask = (uint16_t)1 << (uint16_t)itpos;\r
717 \r
718   if (NewState != DISABLE)\r
719   {\r
720     /* Enable the selected SPI interrupt */\r
721     SPIx->CR2 |= itmask;\r
722   }\r
723   else\r
724   {\r
725     /* Disable the selected SPI interrupt */\r
726     SPIx->CR2 &= (uint16_t)~itmask;\r
727   }\r
728 }\r
729 \r
730 /**\r
731   * @brief  Checks whether the specified SPI flag is set or not.\r
732   * @param  SPIx: where x can be 1 or 2 in SPI mode \r
733   * @param  SPI_I2S_FLAG: specifies the SPI flag to check. \r
734   *   This parameter can be one of the following values:\r
735   *     @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.\r
736   *     @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.\r
737   *     @arg SPI_I2S_FLAG_BSY: Busy flag.\r
738   *     @arg SPI_I2S_FLAG_OVR: Overrun flag.\r
739   *     @arg SPI_I2S_FLAG_MODF: Mode Fault flag.\r
740   *     @arg SPI_I2S_FLAG_CRCERR: CRC Error flag.\r
741   * @retval The new state of SPI_I2S_FLAG (SET or RESET).\r
742   */\r
743 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)\r
744 {\r
745   FlagStatus bitstatus = RESET;\r
746   /* Check the parameters */\r
747   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
748   assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));\r
749   \r
750   /* Check the status of the specified SPI flag */\r
751   if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)\r
752   {\r
753     /* SPI_I2S_FLAG is set */\r
754     bitstatus = SET;\r
755   }\r
756   else\r
757   {\r
758     /* SPI_I2S_FLAG is reset */\r
759     bitstatus = RESET;\r
760   }\r
761   /* Return the SPI_I2S_FLAG status */\r
762   return  bitstatus;\r
763 }\r
764 \r
765 /**\r
766   * @brief  Clears the SPIx CRC Error (CRCERR) flag.\r
767   * @param  SPIx: where x can be 1 or 2 in SPI mode \r
768   * @param  SPI_I2S_FLAG: specifies the SPI flag to clear. \r
769   *   This function clears only CRCERR flag.\r
770   * @note\r
771   *   - OVR (OverRun error) flag is cleared by software sequence: a read \r
772   *     operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read \r
773   *     operation to SPI_SR register (SPI_I2S_GetFlagStatus()).\r
774   *   - MODF (Mode Fault) flag is cleared by software sequence: a read/write \r
775   *     operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a \r
776   *     write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).\r
777   * @retval None\r
778   */\r
779 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)\r
780 {\r
781   /* Check the parameters */\r
782   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
783   assert_param(IS_SPI_I2S_CLEAR_FLAG(SPI_I2S_FLAG));\r
784     \r
785   /* Clear the selected SPI CRC Error (CRCERR) flag */\r
786   SPIx->SR = (uint16_t)~SPI_I2S_FLAG;\r
787 }\r
788 \r
789 /**\r
790   * @brief  Checks whether the specified SPI interrupt has occurred or not.\r
791   * @param  SPIx: where x can be\r
792   *   - 1 or 2 in SPI mode \r
793   * @param  SPI_I2S_IT: specifies the SPI interrupt source to check. \r
794   *   This parameter can be one of the following values:\r
795   *     @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.\r
796   *     @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.\r
797   *     @arg SPI_I2S_IT_OVR: Overrun interrupt.\r
798   *     @arg SPI_I2S_IT_MODF: Mode Fault interrupt.\r
799   *     @arg SPI_I2S_IT_CRCERR: CRC Error interrupt.\r
800   * @retval The new state of SPI_I2S_IT (SET or RESET).\r
801   */\r
802 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)\r
803 {\r
804   ITStatus bitstatus = RESET;\r
805   uint16_t itpos = 0, itmask = 0, enablestatus = 0;\r
806 \r
807   /* Check the parameters */\r
808   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
809   assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));\r
810 \r
811   /* Get the SPI_I2S_IT index */\r
812   itpos = 0x01 << (SPI_I2S_IT & 0x0F);\r
813 \r
814   /* Get the SPI_I2S_IT IT mask */\r
815   itmask = SPI_I2S_IT >> 4;\r
816 \r
817   /* Set the IT mask */\r
818   itmask = 0x01 << itmask;\r
819 \r
820   /* Get the SPI_I2S_IT enable bit status */\r
821   enablestatus = (SPIx->CR2 & itmask) ;\r
822 \r
823   /* Check the status of the specified SPI interrupt */\r
824   if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)\r
825   {\r
826     /* SPI_I2S_IT is set */\r
827     bitstatus = SET;\r
828   }\r
829   else\r
830   {\r
831     /* SPI_I2S_IT is reset */\r
832     bitstatus = RESET;\r
833   }\r
834   /* Return the SPI_I2S_IT status */\r
835   return bitstatus;\r
836 }\r
837 \r
838 /**\r
839   * @brief  Clears the SPIx CRC Error (CRCERR) interrupt pending bit.\r
840   * @param  SPIx: where x can be\r
841   *   - 1 or 2 in SPI mode \r
842   * @param  SPI_I2S_IT: specifies the SPI interrupt pending bit to clear.\r
843   *   This function clears only CRCERR interrupt pending bit.   \r
844   * @note\r
845   *   - OVR (OverRun Error) interrupt pending bit is cleared by software \r
846   *     sequence: a read operation to SPI_DR register (SPI_I2S_ReceiveData()) \r
847   *     followed by a read operation to SPI_SR register (SPI_I2S_GetITStatus()).\r
848   *   - MODF (Mode Fault) interrupt pending bit is cleared by software sequence:\r
849   *     a read/write operation to SPI_SR register (SPI_I2S_GetITStatus()) \r
850   *     followed by a write operation to SPI_CR1 register (SPI_Cmd() to enable \r
851   *     the SPI).\r
852   * @retval None\r
853   */\r
854 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)\r
855 {\r
856   uint16_t itpos = 0;\r
857   /* Check the parameters */\r
858   assert_param(IS_SPI_ALL_PERIPH(SPIx));\r
859   assert_param(IS_SPI_I2S_CLEAR_IT(SPI_I2S_IT));\r
860 \r
861   /* Get the SPI_I2S IT index */\r
862   itpos = 0x01 << (SPI_I2S_IT & 0x0F);\r
863 \r
864   /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */\r
865   SPIx->SR = (uint16_t)~itpos;\r
866 }\r
867 \r
868 /**\r
869   * @}\r
870   */\r
871 \r
872 /**\r
873   * @}\r
874   */ \r
875 \r
876 /**\r
877   * @}\r
878   */ \r
879 \r
880 /**\r
881   * @}\r
882   */ \r
883 \r
884 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r