]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3XX/stm32f30x_spi.c
Add a qwerty layer
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3XX / stm32f30x_spi.c
1 /**
2   ******************************************************************************
3   * @file    stm32f30x_spi.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    27-February-2014
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of the Serial peripheral interface (SPI):
9   *           + Initialization and Configuration
10   *           + Data transfers functions
11   *           + Hardware CRC Calculation
12   *           + DMA transfers management
13   *           + Interrupts and flags management
14   *
15   *  @verbatim
16   
17   
18  ===============================================================================
19                       ##### How to use this driver #####
20  ===============================================================================
21     [..]
22         (#) Enable peripheral clock using RCC_APBPeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE)
23             function for SPI1 or using RCC_APBPeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE)
24             function for SPI2.
25         (#) Enable SCK, MOSI, MISO and NSS GPIO clocks using RCC_AHBPeriphClockCmd()
26             function. 
27         (#) Peripherals alternate function: 
28             (++) Connect the pin to the desired peripherals' Alternate 
29                  Function (AF) using GPIO_PinAFConfig() function.
30             (++) Configure the desired pin in alternate function by:
31                  GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
32             (++) Select the type, pull-up/pull-down and output speed via 
33                  GPIO_PuPd, GPIO_OType and GPIO_Speed members.
34             (++) Call GPIO_Init() function.
35         (#) Program the Polarity, Phase, First Data, Baud Rate Prescaler, Slave 
36             Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()
37             function in SPI mode. In I2S mode, program the Mode, Standard, Data Format,
38             MCLK Output, Audio frequency and Polarity using I2S_Init() function.
39         (#) Configure the FIFO threshold using SPI_RxFIFOThresholdConfig() to select 
40             at which threshold the RXNE event is generated.     
41         (#) Enable the NVIC and the corresponding interrupt using the function 
42             SPI_I2S_ITConfig() if you need to use interrupt mode. 
43         (#) When using the DMA mode 
44             (++) Configure the DMA using DMA_Init() function.
45             (++) Active the needed channel Request using SPI_I2S_DMACmd() function.
46         (#) Enable the SPI using the SPI_Cmd() function or enable the I2S using
47             I2S_Cmd().
48         (#) Enable the DMA using the DMA_Cmd() function when using DMA mode. 
49         (#) Optionally you can enable/configure the following parameters without
50             re-initialization (i.e there is no need to call again SPI_Init() function):
51             (++) When bidirectional mode (SPI_Direction_1Line_Rx or SPI_Direction_1Line_Tx)
52                  is programmed as Data direction parameter using the SPI_Init() function
53                  it can be possible to switch between SPI_Direction_Tx or SPI_Direction_Rx
54                  using the SPI_BiDirectionalLineConfig() function.
55             (++) When SPI_NSS_Soft is selected as Slave Select Management parameter 
56                  using the SPI_Init() function it can be possible to manage the 
57                  NSS internal signal using the SPI_NSSInternalSoftwareConfig() function.
58             (++) Reconfigure the data size using the SPI_DataSizeConfig() function.  
59             (++) Enable or disable the SS output using the SPI_SSOutputCmd() function.
60         (#) To use the CRC Hardware calculation feature refer to the Peripheral 
61             CRC hardware Calculation subsection.
62     [..] It is possible to use SPI in I2S full duplex mode, in this case, each SPI 
63          peripheral is able to manage sending and receiving data simultaneously
64          using two data lines. Each SPI peripheral has an extended block called I2Sxext
65          (ie. I2S2ext for SPI2 and I2S3ext for SPI3).
66          The extension block is not a full SPI IP, it is used only as I2S slave to
67          implement full duplex mode. The extension block uses the same clock sources
68          as its master.          
69          To configure I2S full duplex you have to:
70         (#) Configure SPIx in I2S mode (I2S_Init() function) as described above. 
71         (#) Call the I2S_FullDuplexConfig() function using the same strucutre passed to  
72             I2S_Init() function.
73         (#) Call I2S_Cmd() for SPIx then for its extended block.
74         (#) Configure interrupts or DMA requests and to get/clear flag status, 
75             use I2Sxext instance for the extension block.
76         [..] Functions that can be called with I2Sxext instances are:
77              I2S_Cmd(), I2S_FullDuplexConfig(), SPI_I2S_ReceiveData16(), SPI_I2S_SendData16(), 
78              SPI_I2S_DMACmd(), SPI_I2S_ITConfig(), SPI_I2S_GetFlagStatus(), SPI_I2S_ClearFlag(),
79              SPI_I2S_GetITStatus() and SPI_I2S_ClearITPendingBit().
80         [..] Example: To use SPI3 in Full duplex mode (SPI3 is Master Tx, I2S3ext is Slave Rx):
81         [..] RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);   
82              I2S_StructInit(&I2SInitStruct);
83              I2SInitStruct.Mode = I2S_Mode_MasterTx;     
84              I2S_Init(SPI3, &I2SInitStruct);
85              I2S_FullDuplexConfig(SPI3ext, &I2SInitStruct)
86              I2S_Cmd(SPI3, ENABLE);
87              I2S_Cmd(SPI3ext, ENABLE);
88              ...
89              while (SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET)
90              {}
91              SPI_I2S_SendData16(SPI3, txdata[i]);
92              ...  
93              while (SPI_I2S_GetFlagStatus(I2S3ext, SPI_FLAG_RXNE) == RESET)
94              {}
95              rxdata[i] = SPI_I2S_ReceiveData16(I2S3ext);
96              ...          
97     [..]
98     (@) In SPI mode: To use the SPI TI mode, call the function SPI_TIModeCmd() 
99         just after calling the function SPI_Init().  
100               
101     @endverbatim
102   ******************************************************************************
103   * @attention
104   *
105   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
106   *
107   * Redistribution and use in source and binary forms, with or without modification,
108   * are permitted provided that the following conditions are met:
109   *   1. Redistributions of source code must retain the above copyright notice,
110   *      this list of conditions and the following disclaimer.
111   *   2. Redistributions in binary form must reproduce the above copyright notice,
112   *      this list of conditions and the following disclaimer in the documentation
113   *      and/or other materials provided with the distribution.
114   *   3. Neither the name of STMicroelectronics nor the names of its contributors
115   *      may be used to endorse or promote products derived from this software
116   *      without specific prior written permission.
117   *
118   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
119   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
120   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
121   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
122   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
123   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
124   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
125   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
126   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
127   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128   *
129   ******************************************************************************
130   */
131
132 /* Includes ------------------------------------------------------------------*/
133 #include "stm32f30x_spi.h"
134 #include "stm32f30x_rcc.h"
135
136 /** @addtogroup STM32F30x_StdPeriph_Driver
137   * @{
138   */
139
140 /** @defgroup SPI
141   * @brief SPI driver modules
142   * @{
143   */
144
145 /* Private typedef -----------------------------------------------------------*/
146 /* Private define ------------------------------------------------------------*/
147 /* SPI registers Masks */
148 #define CR1_CLEAR_MASK       ((uint16_t)0x3040)
149 #define CR2_LDMA_MASK        ((uint16_t)0x9FFF)
150
151 #define I2SCFGR_CLEAR_MASK   ((uint16_t)0xF040)
152
153 /* Private macro -------------------------------------------------------------*/
154 /* Private variables ---------------------------------------------------------*/
155 /* Private function prototypes -----------------------------------------------*/
156 /* Private functions ---------------------------------------------------------*/
157
158 /** @defgroup SPI_Private_Functions
159   * @{
160   */
161
162 /** @defgroup SPI_Group1 Initialization and Configuration functions
163  *  @brief   Initialization and Configuration functions 
164  *
165 @verbatim   
166  ===============================================================================
167            ##### Initialization and Configuration functions #####
168  ===============================================================================  
169     [..] This section provides a set of functions allowing to initialize the SPI Direction,
170          SPI Mode, SPI Data Size, SPI Polarity, SPI Phase, SPI NSS Management, SPI Baud
171          Rate Prescaler, SPI First Bit and SPI CRC Polynomial.
172     [..] The SPI_Init() function follows the SPI configuration procedures for Master mode
173          and Slave mode (details for these procedures are available in reference manual).
174     [..] When the Software NSS management (SPI_InitStruct->SPI_NSS = SPI_NSS_Soft) is selected,
175          use the following function to manage the NSS bit:
176          void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
177     [..] In Master mode, when the Hardware NSS management (SPI_InitStruct->SPI_NSS = SPI_NSS_Hard)
178          is selected, use the follwoing function to enable the NSS output feature.
179          void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
180     [..] The NSS pulse mode can be managed by the SPI TI mode when enabling it using the 
181          following function: void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
182          And it can be managed by software in the SPI Motorola mode using this function: 
183          void SPI_NSSPulseModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
184     [..] This section provides also functions to initialize the I2S Mode, Standard, 
185          Data Format, MCLK Output, Audio frequency and Polarity.
186     [..] The I2S_Init() function follows the I2S configuration procedures for Master mode
187          and Slave mode.
188   
189 @endverbatim
190   * @{
191   */
192
193 /**
194   * @brief  Deinitializes the SPIx peripheral registers to their default
195   *         reset values.
196   * @param  SPIx: To select the SPIx peripheral, where x can be: 1, 2 or 3 
197   *         in SPI mode.
198   * @retval None
199   */
200 void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
201 {
202   /* Check the parameters */
203   assert_param(IS_SPI_ALL_PERIPH(SPIx));
204
205   if (SPIx == SPI1)
206   {
207     /* Enable SPI1 reset state */
208     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
209     /* Release SPI1 from reset state */
210     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
211   }
212   else if (SPIx == SPI2)
213   {
214     /* Enable SPI2 reset state */
215     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
216     /* Release SPI2 from reset state */
217     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
218   }
219   else
220   {
221     if (SPIx == SPI3)
222     {
223       /* Enable SPI3 reset state */
224       RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
225       /* Release SPI3 from reset state */
226       RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
227     }
228   }
229 }
230
231 /**
232   * @brief  Fills each SPI_InitStruct member with its default value.
233   * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure which will be initialized.
234   * @retval None
235   */
236 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
237 {
238 /*--------------- Reset SPI init structure parameters values -----------------*/
239   /* Initialize the SPI_Direction member */
240   SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;
241   /* Initialize the SPI_Mode member */
242   SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;
243   /* Initialize the SPI_DataSize member */
244   SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
245   /* Initialize the SPI_CPOL member */
246   SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
247   /* Initialize the SPI_CPHA member */
248   SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
249   /* Initialize the SPI_NSS member */
250   SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;
251   /* Initialize the SPI_BaudRatePrescaler member */
252   SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
253   /* Initialize the SPI_FirstBit member */
254   SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
255   /* Initialize the SPI_CRCPolynomial member */
256   SPI_InitStruct->SPI_CRCPolynomial = 7;
257 }
258
259 /**
260   * @brief  Initializes the SPIx peripheral according to the specified 
261   *         parameters in the SPI_InitStruct.
262   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
263   * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
264   *         contains the configuration information for the specified SPI peripheral.
265   * @retval None
266   */
267 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
268 {
269   uint16_t tmpreg = 0;
270
271   /* check the parameters */
272   assert_param(IS_SPI_ALL_PERIPH(SPIx));
273
274   /* Check the SPI parameters */
275   assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
276   assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
277   assert_param(IS_SPI_DATA_SIZE(SPI_InitStruct->SPI_DataSize));
278   assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
279   assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
280   assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
281   assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
282   assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
283   assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
284
285   /* Configuring the SPI in master mode */
286   if(SPI_InitStruct->SPI_Mode == SPI_Mode_Master)
287   {
288 /*---------------------------- SPIx CR1 Configuration ------------------------*/
289     /* Get the SPIx CR1 value */
290     tmpreg = SPIx->CR1;
291     /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
292     tmpreg &= CR1_CLEAR_MASK;
293     /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
294        master/slave mode, CPOL and CPHA */
295     /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
296     /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
297     /* Set LSBFirst bit according to SPI_FirstBit value */
298     /* Set BR bits according to SPI_BaudRatePrescaler value */
299     /* Set CPOL bit according to SPI_CPOL value */
300     /* Set CPHA bit according to SPI_CPHA value */
301     tmpreg |= (uint16_t)((uint16_t)(SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode) |
302                          (uint16_t)((uint16_t)(SPI_InitStruct->SPI_CPOL | SPI_InitStruct->SPI_CPHA) |
303                          (uint16_t)((uint16_t)(SPI_InitStruct->SPI_NSS | SPI_InitStruct->SPI_BaudRatePrescaler) | 
304                          SPI_InitStruct->SPI_FirstBit)));
305     /* Write to SPIx CR1 */
306     SPIx->CR1 = tmpreg;
307     /*-------------------------Data Size Configuration -----------------------*/
308     /* Get the SPIx CR2 value */
309     tmpreg = SPIx->CR2;
310     /* Clear DS[3:0] bits */
311     tmpreg &= (uint16_t)~SPI_CR2_DS;
312     /* Configure SPIx: Data Size */
313     tmpreg |= (uint16_t)(SPI_InitStruct->SPI_DataSize);
314     /* Write to SPIx CR2 */
315     SPIx->CR2 = tmpreg;
316   }
317   /* Configuring the SPI in slave mode */
318   else
319   {
320 /*---------------------------- Data size Configuration -----------------------*/
321     /* Get the SPIx CR2 value */
322     tmpreg = SPIx->CR2;
323     /* Clear DS[3:0] bits */
324     tmpreg &= (uint16_t)~SPI_CR2_DS;
325     /* Configure SPIx: Data Size */
326     tmpreg |= (uint16_t)(SPI_InitStruct->SPI_DataSize);
327     /* Write to SPIx CR2 */
328     SPIx->CR2 = tmpreg;
329 /*---------------------------- SPIx CR1 Configuration ------------------------*/
330     /* Get the SPIx CR1 value */
331     tmpreg = SPIx->CR1;
332     /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
333     tmpreg &= CR1_CLEAR_MASK;
334     /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
335        master/salve mode, CPOL and CPHA */
336     /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
337     /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
338     /* Set LSBFirst bit according to SPI_FirstBit value */
339     /* Set BR bits according to SPI_BaudRatePrescaler value */
340     /* Set CPOL bit according to SPI_CPOL value */
341     /* Set CPHA bit according to SPI_CPHA value */
342     tmpreg |= (uint16_t)((uint16_t)(SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode) | 
343                          (uint16_t)((uint16_t)(SPI_InitStruct->SPI_CPOL | SPI_InitStruct->SPI_CPHA) | 
344                          (uint16_t)((uint16_t)(SPI_InitStruct->SPI_NSS | SPI_InitStruct->SPI_BaudRatePrescaler) | 
345                          SPI_InitStruct->SPI_FirstBit)));
346
347     /* Write to SPIx CR1 */
348     SPIx->CR1 = tmpreg;
349   }
350
351   /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
352   SPIx->I2SCFGR &= (uint16_t)~((uint16_t)SPI_I2SCFGR_I2SMOD);
353
354 /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
355   /* Write to SPIx CRCPOLY */
356   SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
357 }
358
359 /**
360   * @brief  Fills each I2S_InitStruct member with its default value.
361   * @param  I2S_InitStruct : pointer to a I2S_InitTypeDef structure which will be initialized.
362   * @retval None
363   */
364 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)
365 {
366 /*--------------- Reset I2S init structure parameters values -----------------*/
367   /* Initialize the I2S_Mode member */
368   I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;
369
370   /* Initialize the I2S_Standard member */
371   I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;
372
373   /* Initialize the I2S_DataFormat member */
374   I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;
375
376   /* Initialize the I2S_MCLKOutput member */
377   I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;
378
379   /* Initialize the I2S_AudioFreq member */
380   I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;
381
382   /* Initialize the I2S_CPOL member */
383   I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;
384 }
385
386 /**
387   * @brief  Initializes the SPIx peripheral according to the specified 
388   *   parameters in the I2S_InitStruct.
389   * @param  SPIx:To select the SPIx peripheral, where x can be: 2 or 3 
390   *         in I2S mode. 
391   * @param  I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
392   *   contains the configuration information for the specified SPI peripheral
393   *   configured in I2S mode.
394   * @note
395   *  The function calculates the optimal prescaler needed to obtain the most 
396   *  accurate audio frequency (depending on the I2S clock source, the PLL values 
397   *  and the product configuration). But in case the prescaler value is greater 
398   *  than 511, the default value (0x02) will be configured instead.     
399   * @retval None
400   */
401 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)
402 {
403   uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
404   uint32_t tmp = 0;
405   RCC_ClocksTypeDef RCC_Clocks;
406   uint32_t sourceclock = 0;
407
408   /* Check the I2S parameters */
409   assert_param(IS_SPI_23_PERIPH(SPIx));
410   assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
411   assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
412   assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
413   assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));
414   assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));
415   assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));  
416
417 /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
418   /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
419   SPIx->I2SCFGR &= I2SCFGR_CLEAR_MASK; 
420   SPIx->I2SPR = 0x0002;
421
422   /* Get the I2SCFGR register value */
423   tmpreg = SPIx->I2SCFGR;
424
425   /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
426   if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)
427   {
428     i2sodd = (uint16_t)0;
429     i2sdiv = (uint16_t)2;   
430   }
431   /* If the requested audio frequency is not the default, compute the prescaler */
432   else
433   {
434     /* Check the frame length (For the Prescaler computing) */
435     if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)
436     {
437       /* Packet length is 16 bits */
438       packetlength = 1;
439     }
440     else
441     {
442       /* Packet length is 32 bits */
443       packetlength = 2;
444     }
445
446     /* I2S Clock source is System clock: Get System Clock frequency */
447     RCC_GetClocksFreq(&RCC_Clocks);      
448
449     /* Get the source clock value: based on System Clock value */
450     sourceclock = RCC_Clocks.SYSCLK_Frequency;    
451
452     /* Compute the Real divider depending on the MCLK output state with a floating point */
453     if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable)
454     {
455       /* MCLK output is enabled */
456       tmp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitStruct->I2S_AudioFreq)) + 5);
457     }
458     else
459     {
460       /* MCLK output is disabled */
461       tmp = (uint16_t)(((((sourceclock / (32 * packetlength)) *10 ) / I2S_InitStruct->I2S_AudioFreq)) + 5);
462     }
463     
464     /* Remove the floating point */
465     tmp = tmp / 10;
466
467     /* Check the parity of the divider */
468     i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
469
470     /* Compute the i2sdiv prescaler */
471     i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
472
473     /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
474     i2sodd = (uint16_t) (i2sodd << 8);
475   }
476
477   /* Test if the divider is 1 or 0 or greater than 0xFF */
478   if ((i2sdiv < 2) || (i2sdiv > 0xFF))
479   {
480     /* Set the default values */
481     i2sdiv = 2;
482     i2sodd = 0;
483   }
484
485   /* Write to SPIx I2SPR register the computed value */
486   SPIx->I2SPR = (uint16_t)(i2sdiv | (uint16_t)(i2sodd | (uint16_t)I2S_InitStruct->I2S_MCLKOutput));
487
488   /* Configure the I2S with the SPI_InitStruct values */
489   tmpreg |= (uint16_t)((uint16_t)(SPI_I2SCFGR_I2SMOD | I2S_InitStruct->I2S_Mode) | \
490                        (uint16_t)((uint16_t)((uint16_t)(I2S_InitStruct->I2S_Standard |I2S_InitStruct->I2S_DataFormat) |\
491                        I2S_InitStruct->I2S_CPOL)));
492
493   /* Write to SPIx I2SCFGR */
494   SPIx->I2SCFGR = tmpreg;
495 }
496
497 /**
498   * @brief  Enables or disables the specified SPI peripheral.
499   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
500   * @param  NewState: new state of the SPIx peripheral. 
501   *         This parameter can be: ENABLE or DISABLE.
502   * @retval None
503   */
504 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
505 {
506   /* Check the parameters */
507   assert_param(IS_SPI_ALL_PERIPH(SPIx));
508   assert_param(IS_FUNCTIONAL_STATE(NewState));
509
510   if (NewState != DISABLE)
511   {
512     /* Enable the selected SPI peripheral */
513     SPIx->CR1 |= SPI_CR1_SPE;
514   }
515   else
516   {
517     /* Disable the selected SPI peripheral */
518     SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_SPE);
519   }
520 }
521
522 /**
523   * @brief  Enables or disables the TI Mode.
524   * @note    This function can be called only after the SPI_Init() function has 
525   *          been called. 
526   * @note    When TI mode is selected, the control bits SSM, SSI, CPOL and CPHA 
527   *          are not taken into consideration and are configured by hardware 
528   *          respectively to the TI mode requirements.  
529   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.  
530   * @param  NewState: new state of the selected SPI TI communication mode.
531   *         This parameter can be: ENABLE or DISABLE.
532   * @retval None
533   */
534 void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
535 {
536   /* Check the parameters */
537   assert_param(IS_SPI_ALL_PERIPH(SPIx));
538   assert_param(IS_FUNCTIONAL_STATE(NewState));
539
540   if (NewState != DISABLE)
541   {
542     /* Enable the TI mode for the selected SPI peripheral */
543     SPIx->CR2 |= SPI_CR2_FRF;
544   }
545   else
546   {
547     /* Disable the TI mode for the selected SPI peripheral */
548     SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRF);
549   }
550 }
551
552 /**
553   * @brief  Enables or disables the specified SPI peripheral (in I2S mode).
554   * @param  SPIx:To select the SPIx peripheral, where x can be: 2 or 3 in 
555   *         I2S mode or I2Sxext for I2S full duplex mode. 
556   * @param  NewState: new state of the SPIx peripheral. 
557   *   This parameter can be: ENABLE or DISABLE.
558   * @retval None
559   */
560 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
561 {
562   /* Check the parameters */
563   assert_param(IS_SPI_23_PERIPH_EXT(SPIx));
564   assert_param(IS_FUNCTIONAL_STATE(NewState));
565   if (NewState != DISABLE)
566   {
567     /* Enable the selected SPI peripheral in I2S mode */
568     SPIx->I2SCFGR |= SPI_I2SCFGR_I2SE;
569   }
570   else
571   {
572     /* Disable the selected SPI peripheral in I2S mode */
573     SPIx->I2SCFGR &= (uint16_t)~((uint16_t)SPI_I2SCFGR_I2SE);
574   }
575 }
576
577 /**
578   * @brief  Configures the data size for the selected SPI.
579   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. 
580   * @param  SPI_DataSize: specifies the SPI data size.
581   *   For the SPIx peripheral this parameter can be one of the following values:
582   *     @arg SPI_DataSize_4b: Set data size to 4 bits
583   *     @arg SPI_DataSize_5b: Set data size to 5 bits
584   *     @arg SPI_DataSize_6b: Set data size to 6 bits
585   *     @arg SPI_DataSize_7b: Set data size to 7 bits
586   *     @arg SPI_DataSize_8b: Set data size to 8 bits
587   *     @arg SPI_DataSize_9b: Set data size to 9 bits
588   *     @arg SPI_DataSize_10b: Set data size to 10 bits
589   *     @arg SPI_DataSize_11b: Set data size to 11 bits
590   *     @arg SPI_DataSize_12b: Set data size to 12 bits
591   *     @arg SPI_DataSize_13b: Set data size to 13 bits
592   *     @arg SPI_DataSize_14b: Set data size to 14 bits
593   *     @arg SPI_DataSize_15b: Set data size to 15 bits
594   *     @arg SPI_DataSize_16b: Set data size to 16 bits
595   * @retval None
596   */
597 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)
598 {
599   uint16_t tmpreg = 0;
600   
601   /* Check the parameters */
602   assert_param(IS_SPI_ALL_PERIPH(SPIx));
603   assert_param(IS_SPI_DATA_SIZE(SPI_DataSize));
604   /* Read the CR2 register */
605   tmpreg = SPIx->CR2;
606   /* Clear DS[3:0] bits */
607   tmpreg &= (uint16_t)~SPI_CR2_DS;
608   /* Set new DS[3:0] bits value */
609   tmpreg |= SPI_DataSize;
610   SPIx->CR2 = tmpreg;
611 }
612
613 /**
614   * @brief  Configures the FIFO reception threshold for the selected SPI.
615   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. 
616   * @param  SPI_RxFIFOThreshold: specifies the FIFO reception threshold.
617   *   This parameter can be one of the following values:
618   *     @arg SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO 
619   *          level is greater or equal to 1/2. 
620   *     @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO 
621   *          level is greater or equal to 1/4. 
622   * @retval None
623   */
624 void SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold)
625 {
626   /* Check the parameters */
627   assert_param(IS_SPI_ALL_PERIPH(SPIx));
628   assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold));
629
630   /* Clear FRXTH bit */
631   SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH);
632
633   /* Set new FRXTH bit value */
634   SPIx->CR2 |= SPI_RxFIFOThreshold;
635 }
636
637 /**
638   * @brief  Selects the data transfer direction in bidirectional mode for the specified SPI.
639   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. 
640   * @param  SPI_Direction: specifies the data transfer direction in bidirectional mode. 
641   *   This parameter can be one of the following values:
642   *     @arg SPI_Direction_Tx: Selects Tx transmission direction
643   *     @arg SPI_Direction_Rx: Selects Rx receive direction
644   * @retval None
645   */
646 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)
647 {
648   /* Check the parameters */
649   assert_param(IS_SPI_ALL_PERIPH(SPIx));
650   assert_param(IS_SPI_DIRECTION(SPI_Direction));
651   if (SPI_Direction == SPI_Direction_Tx)
652   {
653     /* Set the Tx only mode */
654     SPIx->CR1 |= SPI_Direction_Tx;
655   }
656   else
657   {
658     /* Set the Rx only mode */
659     SPIx->CR1 &= SPI_Direction_Rx;
660   }
661 }
662
663 /**
664   * @brief  Configures internally by software the NSS pin for the selected SPI.
665   * @note    This function can be called only after the SPI_Init() function has 
666   *          been called.  
667   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
668   * @param  SPI_NSSInternalSoft: specifies the SPI NSS internal state.
669   *   This parameter can be one of the following values:
670   *     @arg SPI_NSSInternalSoft_Set: Set NSS pin internally
671   *     @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally
672   * @retval None
673   */
674 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)
675 {
676   /* Check the parameters */
677   assert_param(IS_SPI_ALL_PERIPH(SPIx));
678   assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
679
680   if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)
681   {
682     /* Set NSS pin internally by software */
683     SPIx->CR1 |= SPI_NSSInternalSoft_Set;
684   }
685   else
686   {
687     /* Reset NSS pin internally by software */
688     SPIx->CR1 &= SPI_NSSInternalSoft_Reset;
689   }
690 }
691
692 /**
693   * @brief  Configures the full duplex mode for the I2Sx peripheral using its
694   *         extension I2Sxext according to the specified parameters in the 
695   *         I2S_InitStruct.
696   * @param  I2Sxext: where x can be  2 or 3 to select the I2S peripheral extension block.
697   * @param  I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
698   *         contains the configuration information for the specified I2S peripheral
699   *         extension.
700   * 
701   * @note   The structure pointed by I2S_InitStruct parameter should be the same
702   *         used for the master I2S peripheral. In this case, if the master is 
703   *         configured as transmitter, the slave will be receiver and vice versa.
704   *         Or you can force a different mode by modifying the field I2S_Mode to the
705   *         value I2S_SlaveRx or I2S_SlaveTx indepedently of the master configuration.    
706   *         
707   * @note   The I2S full duplex extension can be configured in slave mode only.    
708   *  
709   * @retval None
710   */
711 void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct)
712 {
713   uint16_t tmpreg = 0, tmp = 0;
714   
715   /* Check the I2S parameters */
716   assert_param(IS_I2S_EXT_PERIPH(I2Sxext));
717   assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
718   assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
719   assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
720   assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));  
721
722 /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
723   /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
724   I2Sxext->I2SCFGR &= I2SCFGR_CLEAR_MASK; 
725   I2Sxext->I2SPR = 0x0002;
726   
727   /* Get the I2SCFGR register value */
728   tmpreg = I2Sxext->I2SCFGR;
729   
730   /* Get the mode to be configured for the extended I2S */
731   if ((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterTx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveTx))
732   {
733     tmp = I2S_Mode_SlaveRx;
734   }
735   else
736   {
737     if ((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterRx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveRx))
738     {
739       tmp = I2S_Mode_SlaveTx;
740     }
741   }
742
743  
744   /* Configure the I2S with the SPI_InitStruct values */
745   tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(tmp | \
746                   (uint16_t)(I2S_InitStruct->I2S_Standard | (uint16_t)(I2S_InitStruct->I2S_DataFormat | \
747                   (uint16_t)I2S_InitStruct->I2S_CPOL))));
748  
749   /* Write to SPIx I2SCFGR */  
750   I2Sxext->I2SCFGR = tmpreg;
751 }
752
753 /**
754   * @brief  Enables or disables the SS output for the selected SPI.
755   * @note    This function can be called only after the SPI_Init() function has 
756   *          been called and the NSS hardware management mode is selected. 
757   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
758   * @param  NewState: new state of the SPIx SS output. 
759   *         This parameter can be: ENABLE or DISABLE.
760   * @retval None
761   */
762 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
763 {
764   /* Check the parameters */
765   assert_param(IS_SPI_ALL_PERIPH(SPIx));
766   assert_param(IS_FUNCTIONAL_STATE(NewState));
767   if (NewState != DISABLE)
768   {
769     /* Enable the selected SPI SS output */
770     SPIx->CR2 |= (uint16_t)SPI_CR2_SSOE;
771   }
772   else
773   {
774     /* Disable the selected SPI SS output */
775     SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_SSOE);
776   }
777 }
778
779 /**
780   * @brief  Enables or disables the NSS pulse management mode.
781   * @note    This function can be called only after the SPI_Init() function has 
782   *          been called. 
783   * @note    When TI mode is selected, the control bits NSSP is not taken into 
784   *          consideration and are configured by hardware respectively to the 
785   *          TI mode requirements. 
786   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. 
787   * @param  NewState: new state of the NSS pulse management mode.
788   *         This parameter can be: ENABLE or DISABLE.
789   * @retval None
790   */
791 void SPI_NSSPulseModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
792 {
793   /* Check the parameters */
794   assert_param(IS_SPI_ALL_PERIPH(SPIx));
795   assert_param(IS_FUNCTIONAL_STATE(NewState));
796
797   if (NewState != DISABLE)
798   {
799     /* Enable the NSS pulse management mode */
800     SPIx->CR2 |= SPI_CR2_NSSP;
801   }
802   else
803   {
804     /* Disable the NSS pulse management mode */
805     SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_NSSP);    
806   }
807 }
808
809 /**
810   * @}
811   */
812
813 /** @defgroup SPI_Group2 Data transfers functions
814  *  @brief   Data transfers functions
815  *
816 @verbatim
817  ===============================================================================
818                     ##### Data transfers functions #####
819  ===============================================================================  
820     [..] This section provides a set of functions allowing to manage the SPI or I2S 
821          data transfers.
822     [..] In reception, data are received and then stored into an internal Rx buffer while 
823          In transmission, data are first stored into an internal Tx buffer before being 
824          transmitted.
825     [..] The read access of the SPI_DR register can be done using the SPI_I2S_ReceiveData()
826          function and returns the Rx buffered value. Whereas a write access to the SPI_DR 
827          can be done using SPI_I2S_SendData() function and stores the written data into 
828          Tx buffer.
829
830 @endverbatim
831   * @{
832   */
833
834 /**
835   * @brief  Transmits a Data through the SPIx peripheral.
836   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
837   * @param  Data: Data to be transmitted.
838   * @retval None
839   */
840 void SPI_SendData8(SPI_TypeDef* SPIx, uint8_t Data)
841 {
842   uint32_t spixbase = 0x00;
843
844   /* Check the parameters */
845   assert_param(IS_SPI_ALL_PERIPH(SPIx));
846
847   spixbase = (uint32_t)SPIx; 
848   spixbase += 0x0C;
849   
850   *(__IO uint8_t *) spixbase = Data;
851 }
852
853 /**
854   * @brief  Transmits a Data through the SPIx/I2Sx peripheral.
855   * @param  SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
856   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.  
857   * @param  Data: Data to be transmitted.
858   * @retval None
859   */
860 void SPI_I2S_SendData16(SPI_TypeDef* SPIx, uint16_t Data)
861 {
862   /* Check the parameters */
863   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
864   
865   SPIx->DR = (uint16_t)Data;
866 }
867
868 /**
869   * @brief  Returns the most recent received data by the SPIx peripheral. 
870   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
871   * @retval The value of the received data.
872   */
873 uint8_t SPI_ReceiveData8(SPI_TypeDef* SPIx)
874 {
875   uint32_t spixbase = 0x00;
876   
877   /* Check the parameters */
878   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
879   
880   spixbase = (uint32_t)SPIx; 
881   spixbase += 0x0C;
882   
883   return *(__IO uint8_t *) spixbase;
884 }
885
886 /**
887   * @brief  Returns the most recent received data by the SPIx peripheral. 
888   * @param  SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
889   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
890   * @retval The value of the received data.
891   */
892 uint16_t SPI_I2S_ReceiveData16(SPI_TypeDef* SPIx)
893 {  
894   /* Check the parameters */
895   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
896   
897   return SPIx->DR;
898 }
899 /**
900   * @}
901   */
902
903 /** @defgroup SPI_Group3 Hardware CRC Calculation functions
904  *  @brief   Hardware CRC Calculation functions
905  *
906 @verbatim   
907  ===============================================================================
908                   ##### Hardware CRC Calculation functions #####
909  ===============================================================================  
910     [..] This section provides a set of functions allowing to manage the SPI CRC hardware 
911          calculation.
912     [..] SPI communication using CRC is possible through the following procedure:
913          (#) Program the Data direction, Polarity, Phase, First Data, Baud Rate Prescaler, 
914              Slave Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()
915              function.
916          (#) Enable the CRC calculation using the SPI_CalculateCRC() function.
917          (#) Enable the SPI using the SPI_Cmd() function
918          (#) Before writing the last data to the TX buffer, set the CRCNext bit using the 
919              SPI_TransmitCRC() function to indicate that after transmission of the last 
920              data, the CRC should be transmitted.
921          (#) After transmitting the last data, the SPI transmits the CRC. The SPI_CR1_CRCNEXT
922              bit is reset. The CRC is also received and compared against the SPI_RXCRCR 
923              value. 
924              If the value does not match, the SPI_FLAG_CRCERR flag is set and an interrupt
925              can be generated when the SPI_I2S_IT_ERR interrupt is enabled.
926     [..]
927     (@)
928          (+@) It is advised to don't read the calculate CRC values during the communication.
929          (+@) When the SPI is in slave mode, be careful to enable CRC calculation only 
930               when the clock is stable, that is, when the clock is in the steady state. 
931               If not, a wrong CRC calculation may be done. In fact, the CRC is sensitive 
932               to the SCK slave input clock as soon as CRCEN is set, and this, whatever 
933               the value of the SPE bit.
934          (+@) With high bitrate frequencies, be careful when transmitting the CRC.
935               As the number of used CPU cycles has to be as low as possible in the CRC 
936               transfer phase, it is forbidden to call software functions in the CRC 
937               transmission sequence to avoid errors in the last data and CRC reception. 
938               In fact, CRCNEXT bit has to be written before the end of the transmission/reception 
939               of the last data.
940          (+@) For high bit rate frequencies, it is advised to use the DMA mode to avoid the
941               degradation of the SPI speed performance due to CPU accesses impacting the 
942               SPI bandwidth.
943          (+@) When the STM32F30x are configured as slaves and the NSS hardware mode is 
944               used, the NSS pin needs to be kept low between the data phase and the CRC 
945               phase.
946          (+@) When the SPI is configured in slave mode with the CRC feature enabled, CRC
947               calculation takes place even if a high level is applied on the NSS pin. 
948               This may happen for example in case of a multislave environment where the 
949               communication master addresses slaves alternately.
950          (+@) Between a slave deselection (high level on NSS) and a new slave selection 
951               (low level on NSS), the CRC value should be cleared on both master and slave
952               sides in order to resynchronize the master and slave for their respective 
953               CRC calculation.
954     [..]          
955     (@) To clear the CRC, follow the procedure below:
956          (#@) Disable SPI using the SPI_Cmd() function.
957          (#@) Disable the CRC calculation using the SPI_CalculateCRC() function.
958          (#@) Enable the CRC calculation using the SPI_CalculateCRC() function.
959          (#@) Enable SPI using the SPI_Cmd() function.
960
961 @endverbatim
962   * @{
963   */
964
965 /**
966   * @brief  Configures the CRC calculation length for the selected SPI.
967   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
968   * @param  SPI_CRCLength: specifies the SPI CRC calculation length.
969   *   This parameter can be one of the following values:
970   *     @arg SPI_CRCLength_8b: Set CRC Calculation to 8 bits
971   *     @arg SPI_CRCLength_16b: Set CRC Calculation to 16 bits
972   * @retval None
973   */
974 void SPI_CRCLengthConfig(SPI_TypeDef* SPIx, uint16_t SPI_CRCLength)
975 {
976   /* Check the parameters */
977   assert_param(IS_SPI_ALL_PERIPH(SPIx));
978   assert_param(IS_SPI_CRC_LENGTH(SPI_CRCLength));
979
980   /* Clear CRCL bit */
981   SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_CRCL);
982
983   /* Set new CRCL bit value */
984   SPIx->CR1 |= SPI_CRCLength;
985 }
986
987 /**
988   * @brief  Enables or disables the CRC value calculation of the transferred bytes.
989   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
990   * @param  NewState: new state of the SPIx CRC value calculation.
991   *   This parameter can be: ENABLE or DISABLE.
992   * @retval None
993   */
994 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)
995 {
996   /* Check the parameters */
997   assert_param(IS_SPI_ALL_PERIPH(SPIx));
998   assert_param(IS_FUNCTIONAL_STATE(NewState));
999
1000   if (NewState != DISABLE)
1001   {
1002     /* Enable the selected SPI CRC calculation */
1003     SPIx->CR1 |= SPI_CR1_CRCEN;
1004   }
1005   else
1006   {
1007     /* Disable the selected SPI CRC calculation */
1008     SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_CRCEN);
1009   }
1010 }
1011
1012 /**
1013   * @brief  Transmits the SPIx CRC value.
1014   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
1015   * @retval None
1016   */
1017 void SPI_TransmitCRC(SPI_TypeDef* SPIx)
1018 {
1019   /* Check the parameters */
1020   assert_param(IS_SPI_ALL_PERIPH(SPIx));
1021
1022   /* Enable the selected SPI CRC transmission */
1023   SPIx->CR1 |= SPI_CR1_CRCNEXT;
1024 }
1025
1026 /**
1027   * @brief  Returns the transmit or the receive CRC register value for the specified SPI.
1028   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
1029   * @param  SPI_CRC: specifies the CRC register to be read.
1030   *   This parameter can be one of the following values:
1031   *     @arg SPI_CRC_Tx: Selects Tx CRC register
1032   *     @arg SPI_CRC_Rx: Selects Rx CRC register
1033   * @retval The selected CRC register value..
1034   */
1035 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)
1036 {
1037   uint16_t crcreg = 0;
1038   /* Check the parameters */
1039   assert_param(IS_SPI_ALL_PERIPH(SPIx));
1040   assert_param(IS_SPI_CRC(SPI_CRC));
1041
1042   if (SPI_CRC != SPI_CRC_Rx)
1043   {
1044     /* Get the Tx CRC register */
1045     crcreg = SPIx->TXCRCR;
1046   }
1047   else
1048   {
1049     /* Get the Rx CRC register */
1050     crcreg = SPIx->RXCRCR;
1051   }
1052   /* Return the selected CRC register */
1053   return crcreg;
1054 }
1055
1056 /**
1057   * @brief  Returns the CRC Polynomial register value for the specified SPI.
1058   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
1059   * @retval The CRC Polynomial register value.
1060   */
1061 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)
1062 {
1063   /* Check the parameters */
1064   assert_param(IS_SPI_ALL_PERIPH(SPIx));
1065
1066   /* Return the CRC polynomial register */
1067   return SPIx->CRCPR;
1068 }
1069
1070 /**
1071   * @}
1072   */
1073
1074 /** @defgroup SPI_Group4 DMA transfers management functions
1075  *  @brief   DMA transfers management functions
1076   *
1077 @verbatim   
1078  ===============================================================================
1079                   ##### DMA transfers management functions #####
1080  ===============================================================================
1081
1082 @endverbatim
1083   * @{
1084   */
1085
1086 /**
1087   * @brief  Enables or disables the SPIx/I2Sx DMA interface.
1088   * @param  SPIx:To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
1089   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode. 
1090   * @param  SPI_I2S_DMAReq: specifies the SPI DMA transfer request to be enabled or disabled. 
1091   *   This parameter can be any combination of the following values:
1092   *     @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request
1093   *     @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request
1094   * @param  NewState: new state of the selected SPI DMA transfer request.
1095   *         This parameter can be: ENABLE or DISABLE.
1096   * @retval None
1097   */
1098 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)
1099 {
1100   /* Check the parameters */
1101   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
1102   assert_param(IS_FUNCTIONAL_STATE(NewState));
1103   assert_param(IS_SPI_I2S_DMA_REQ(SPI_I2S_DMAReq));
1104
1105   if (NewState != DISABLE)
1106   {
1107     /* Enable the selected SPI DMA requests */
1108     SPIx->CR2 |= SPI_I2S_DMAReq;
1109   }
1110   else
1111   {
1112     /* Disable the selected SPI DMA requests */
1113     SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;
1114   }
1115 }
1116
1117 /**
1118   * @brief  Configures the number of data to transfer type(Even/Odd) for the DMA
1119   *         last transfers and for the selected SPI.
1120   * @note   This function have a meaning only if DMA mode is selected and if 
1121   *         the packing mode is used (data length <= 8 and DMA transfer size halfword)  
1122   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
1123   * @param  SPI_LastDMATransfer: specifies the SPI last DMA transfers state.
1124   *   This parameter can be one of the following values:
1125   *     @arg SPI_LastDMATransfer_TxEvenRxEven: Number of data for transmission Even
1126   *          and number of data for reception Even.
1127   *     @arg SPI_LastDMATransfer_TxOddRxEven: Number of data for transmission Odd
1128   *          and number of data for reception Even.
1129   *     @arg SPI_LastDMATransfer_TxEvenRxOdd: Number of data for transmission Even
1130   *          and number of data for reception Odd.
1131   *     @arg SPI_LastDMATransfer_TxOddRxOdd: RNumber of data for transmission Odd
1132   *          and number of data for reception Odd.
1133   * @retval None
1134   */
1135 void SPI_LastDMATransferCmd(SPI_TypeDef* SPIx, uint16_t SPI_LastDMATransfer)
1136 {
1137   /* Check the parameters */
1138   assert_param(IS_SPI_ALL_PERIPH(SPIx));
1139   assert_param(IS_SPI_LAST_DMA_TRANSFER(SPI_LastDMATransfer));
1140
1141   /* Clear LDMA_TX and LDMA_RX bits */
1142   SPIx->CR2 &= CR2_LDMA_MASK;
1143
1144   /* Set new LDMA_TX and LDMA_RX bits value */
1145   SPIx->CR2 |= SPI_LastDMATransfer; 
1146 }
1147
1148 /**
1149   * @}
1150   */
1151
1152 /** @defgroup SPI_Group5 Interrupts and flags management functions
1153  *  @brief   Interrupts and flags management functions
1154   *
1155 @verbatim   
1156  ===============================================================================
1157               ##### Interrupts and flags management functions #####
1158  ===============================================================================  
1159     [..] This section provides a set of functions allowing to configure the SPI/I2S 
1160          Interrupts sources and check or clear the flags or pending bits status.
1161          The user should identify which mode will be used in his application to manage 
1162          the communication: Polling mode, Interrupt mode or DMA mode. 
1163     
1164   *** Polling Mode ***
1165   ====================
1166     [..] In Polling Mode, the SPI/I2S communication can be managed by 9 flags:
1167          (#) SPI_I2S_FLAG_TXE : to indicate the status of the transmit buffer register.
1168          (#) SPI_I2S_FLAG_RXNE : to indicate the status of the receive buffer register.
1169          (#) SPI_I2S_FLAG_BSY : to indicate the state of the communication layer of the SPI.
1170          (#) SPI_FLAG_CRCERR : to indicate if a CRC Calculation error occur.              
1171          (#) SPI_FLAG_MODF : to indicate if a Mode Fault error occur.
1172          (#) SPI_I2S_FLAG_OVR : to indicate if an Overrun error occur.
1173          (#) SPI_I2S_FLAG_FRE: to indicate a Frame Format error occurs.
1174          (#) I2S_FLAG_UDR: to indicate an Underrun error occurs.
1175          (#) I2S_FLAG_CHSIDE: to indicate Channel Side.
1176     [..]
1177          (@) Do not use the BSY flag to handle each data transmission or reception.
1178              It is better to use the TXE and RXNE flags instead.
1179     [..] In this Mode it is advised to use the following functions:
1180          (+) FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
1181          (+) void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
1182
1183   *** Interrupt Mode ***
1184   ======================
1185     [..] In Interrupt Mode, the SPI/I2S communication can be managed by 3 interrupt sources
1186          and 5 pending bits: 
1187     [..] Pending Bits:
1188          (#) SPI_I2S_IT_TXE : to indicate the status of the transmit buffer register.
1189          (#) SPI_I2S_IT_RXNE : to indicate the status of the receive buffer register.
1190          (#) SPI_I2S_IT_OVR : to indicate if an Overrun error occur.
1191          (#) I2S_IT_UDR : to indicate an Underrun Error occurs.
1192          (#) SPI_I2S_FLAG_FRE : to indicate a Frame Format error occurs.
1193     [..] Interrupt Source:
1194          (#) SPI_I2S_IT_TXE: specifies the interrupt source for the Tx buffer empty 
1195              interrupt.  
1196          (#) SPI_I2S_IT_RXNE : specifies the interrupt source for the Rx buffer not 
1197              empty interrupt.
1198          (#) SPI_I2S_IT_ERR : specifies the interrupt source for the errors interrupt.
1199     [..] In this Mode it is advised to use the following functions:
1200          (+) void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
1201          (+) ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
1202
1203   *** FIFO Status ***
1204   ===================
1205     [..] It is possible to monitor the FIFO status when a transfer is ongoing using the
1206          following function:
1207          (+) uint32_t SPI_GetFIFOStatus(uint8_t SPI_FIFO_Direction); 
1208
1209   *** DMA Mode ***
1210   ================
1211     [..] In DMA Mode, the SPI communication can be managed by 2 DMA Channel requests:
1212          (#) SPI_I2S_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
1213          (#) SPI_I2S_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
1214     [..] In this Mode it is advised to use the following function:
1215          (+) void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
1216
1217 @endverbatim
1218   * @{
1219   */
1220
1221 /**
1222   * @brief  Enables or disables the specified SPI/I2S interrupts.
1223   * @param  SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
1224   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.  
1225   * @param  SPI_I2S_IT: specifies the SPI interrupt source to be enabled or disabled. 
1226   *   This parameter can be one of the following values:
1227   *     @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask
1228   *     @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask
1229   *     @arg SPI_I2S_IT_ERR: Error interrupt mask
1230   * @param  NewState: new state of the specified SPI interrupt.
1231   *         This parameter can be: ENABLE or DISABLE.
1232   * @retval None
1233   */
1234 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)
1235 {
1236   uint16_t itpos = 0, itmask = 0 ;
1237
1238   /* Check the parameters */
1239   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
1240   assert_param(IS_FUNCTIONAL_STATE(NewState));
1241   assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));
1242
1243   /* Get the SPI IT index */
1244   itpos = SPI_I2S_IT >> 4;
1245
1246   /* Set the IT mask */
1247   itmask = (uint16_t)1 << (uint16_t)itpos;
1248
1249   if (NewState != DISABLE)
1250   {
1251     /* Enable the selected SPI interrupt */
1252     SPIx->CR2 |= itmask;
1253   }
1254   else
1255   {
1256     /* Disable the selected SPI interrupt */
1257     SPIx->CR2 &= (uint16_t)~itmask;
1258   }
1259 }
1260
1261 /**
1262   * @brief  Returns the current SPIx Transmission FIFO filled level.
1263   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
1264   * @retval The Transmission FIFO filling state.
1265   *   - SPI_TransmissionFIFOStatus_Empty: when FIFO is empty
1266   *   - SPI_TransmissionFIFOStatus_1QuarterFull: if more than 1 quarter-full.
1267   *   - SPI_TransmissionFIFOStatus_HalfFull: if more than 1 half-full.
1268   *   - SPI_TransmissionFIFOStatus_Full: when FIFO is full.
1269   */
1270 uint16_t SPI_GetTransmissionFIFOStatus(SPI_TypeDef* SPIx)
1271 {
1272   /* Get the SPIx Transmission FIFO level bits */
1273   return (uint16_t)((SPIx->SR & SPI_SR_FTLVL));
1274 }
1275
1276 /**
1277   * @brief  Returns the current SPIx Reception FIFO filled level.
1278   * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
1279   * @retval The Reception FIFO filling state.
1280   *   - SPI_ReceptionFIFOStatus_Empty: when FIFO is empty
1281   *   - SPI_ReceptionFIFOStatus_1QuarterFull: if more than 1 quarter-full.
1282   *   - SPI_ReceptionFIFOStatus_HalfFull: if more than 1 half-full.
1283   *   - SPI_ReceptionFIFOStatus_Full: when FIFO is full.
1284   */
1285 uint16_t SPI_GetReceptionFIFOStatus(SPI_TypeDef* SPIx)
1286 {
1287   /* Get the SPIx Reception FIFO level bits */
1288   return (uint16_t)((SPIx->SR & SPI_SR_FRLVL));
1289 }
1290
1291 /**
1292   * @brief  Checks whether the specified SPI flag is set or not.
1293   * @param  SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
1294   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.  
1295   * @param  SPI_I2S_FLAG: specifies the SPI flag to check. 
1296   *   This parameter can be one of the following values:
1297   *     @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.
1298   *     @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.
1299   *     @arg SPI_I2S_FLAG_BSY: Busy flag.
1300   *     @arg SPI_I2S_FLAG_OVR: Overrun flag.
1301   *     @arg SPI_I2S_FLAG_MODF: Mode Fault flag.
1302   *     @arg SPI_I2S_FLAG_CRCERR: CRC Error flag.
1303   *     @arg SPI_I2S_FLAG_FRE: TI frame format error flag.
1304   *     @arg I2S_FLAG_UDR: Underrun Error flag.
1305   *     @arg I2S_FLAG_CHSIDE: Channel Side flag.   
1306   * @retval The new state of SPI_I2S_FLAG (SET or RESET).
1307   */
1308 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
1309 {
1310   FlagStatus bitstatus = RESET;
1311   /* Check the parameters */
1312   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
1313   assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
1314
1315   /* Check the status of the specified SPI flag */
1316   if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
1317   {
1318     /* SPI_I2S_FLAG is set */
1319     bitstatus = SET;
1320   }
1321   else
1322   {
1323     /* SPI_I2S_FLAG is reset */
1324     bitstatus = RESET;
1325   }
1326   /* Return the SPI_I2S_FLAG status */
1327   return  bitstatus;
1328 }
1329
1330 /**
1331   * @brief  Clears the SPIx CRC Error (CRCERR) flag.
1332   * @param  SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
1333   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode. 
1334   * @param  SPI_I2S_FLAG: specifies the SPI flag to clear. 
1335   *   This function clears only CRCERR flag.
1336   * @note OVR (OverRun error) flag is cleared by software sequence: a read 
1337   *       operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read 
1338   *       operation to SPI_SR register (SPI_I2S_GetFlagStatus()).
1339   * @note MODF (Mode Fault) flag is cleared by software sequence: a read/write 
1340   *       operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a 
1341   *       write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).
1342   * @retval None
1343   */
1344 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
1345 {
1346   /* Check the parameters */
1347   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
1348   assert_param(IS_SPI_CLEAR_FLAG(SPI_I2S_FLAG));
1349
1350   /* Clear the selected SPI CRC Error (CRCERR) flag */
1351   SPIx->SR = (uint16_t)~SPI_I2S_FLAG;
1352 }
1353
1354 /**
1355   * @brief  Checks whether the specified SPI/I2S interrupt has occurred or not.
1356   * @param  SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3 
1357   *         in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.  
1358   * @param  SPI_I2S_IT: specifies the SPI interrupt source to check. 
1359   *   This parameter can be one of the following values:
1360   *     @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.
1361   *     @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.
1362   *     @arg SPI_IT_MODF: Mode Fault interrupt.
1363   *     @arg SPI_I2S_IT_OVR: Overrun interrupt.
1364   *     @arg I2S_IT_UDR: Underrun interrupt.  
1365   *     @arg SPI_I2S_IT_FRE: Format Error interrupt.  
1366   * @retval The new state of SPI_I2S_IT (SET or RESET).
1367   */
1368 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
1369 {
1370   ITStatus bitstatus = RESET;
1371   uint16_t itpos = 0, itmask = 0, enablestatus = 0;
1372
1373   /* Check the parameters */
1374   assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
1375   assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));
1376
1377   /* Get the SPI_I2S_IT index */
1378   itpos = 0x01 << (SPI_I2S_IT & 0x0F);
1379
1380   /* Get the SPI_I2S_IT IT mask */
1381   itmask = SPI_I2S_IT >> 4;
1382
1383   /* Set the IT mask */
1384   itmask = 0x01 << itmask;
1385
1386   /* Get the SPI_I2S_IT enable bit status */
1387   enablestatus = (SPIx->CR2 & itmask) ;
1388
1389   /* Check the status of the specified SPI interrupt */
1390   if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)
1391   {
1392     /* SPI_I2S_IT is set */
1393     bitstatus = SET;
1394   }
1395   else
1396   {
1397     /* SPI_I2S_IT is reset */
1398     bitstatus = RESET;
1399   }
1400   /* Return the SPI_I2S_IT status */
1401   return bitstatus;
1402 }
1403
1404 /**
1405   * @}
1406   */
1407
1408 /**
1409   * @}
1410   */
1411
1412 /**
1413   * @}
1414   */ 
1415
1416 /**
1417   * @}
1418   */
1419
1420 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/