]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_spi.c
Merge pull request #24 from marknsikora/master
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_spi.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_spi.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   SPI HAL module driver.
8   *    
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the SPI peripheral:
11   *           + Initialization/de-initialization functions
12   *           + I/O operation functions
13   *           + Peripheral Control functions 
14   *           + Peripheral State functions
15   *         
16   @verbatim
17 ===============================================================================
18             ##### How to use this driver #####
19  ===============================================================================
20            [..]
21     The SPI HAL driver can be used as follows:
22   
23     (#) Declare a SPI_HandleTypeDef handle structure, for example:
24         SPI_HandleTypeDef  hspi; 
25   
26     (#)Initialize the SPI low level resources by implement the HAL_SPI_MspInit ()API:
27         (##) Enable the SPIx interface clock 
28         (##) SPI pins configuration
29             (+) Enable the clock for the SPI GPIOs 
30             (+) Configure these SPI pins as alternate function push-pull
31         (##) NVIC configuration if you need to use interrupt process
32             (+) Configure the SPIx interrupt priority
33             (+) Enable the NVIC SPI IRQ handle
34         (##) DMA Configuration if you need to use DMA process
35             (+) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
36             (+) Enable the DMAx interface clock using 
37             (+) Configure the DMA handle parameters 
38             (+) Configure the DMA Tx or Rx channel
39             (+) Associate the initilalized hdma_tx handle to the hspi DMA Tx or Rx handle
40             (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx channel
41   
42     (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS 
43         management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
44   
45     (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
46         (+) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
47             by calling the customed HAL_SPI_MspInit(&hspi) API.
48   
49     [..]
50     Using the HAL it is not possible to reach all supported SPI frequency with the differents SPI Modes,
51     the following table resume the max SPI frequency reached with data size 8bits/16bits:
52    +-----------------------------------------------------------------------------------------+
53    |         |                | 2Lines Fullduplex  |     2Lines RxOnly  |        1Line       |
54    | Process | Tranfert mode  |--------------------|--------------------|--------------------|
55    |         |                |  Master  |  Slave  |  Master  |  Slave  |  Master  |  Slave  |
56    |=========================================================================================|
57    |    T    |     Polling    |  Fcpu/4  |  Fcpu/8 |    NA    |    NA   |    NA    |   NA    |
58    |    X    |----------------|----------|---------|----------|---------|----------|---------|
59    |    /    |     Interrupt  |  Fcpu/4  | Fcpu/16 |    NA    |    NA   |    NA    |   NA    |
60    |    R    |----------------|----------|---------|----------|---------|----------|---------|
61    |    X    |       DMA      |  Fcpu/2  |  Fcpu/2 |    NA    |    NA   |    NA    |   NA    |
62    |=========|================|==========|=========|==========|=========|==========|=========|
63    |         |     Polling    |  Fcpu/4  |  Fcpu/8 |  Fcpu/16 |  Fcpu/8 |   Fcpu/8 |  Fcpu/8 |
64    |         |----------------|----------|---------|----------|---------|----------|---------|
65    |    R    |     Interrupt  |  Fcpu/8  | Fcpu/16 |   Fcpu/8 |  Fcpu/8 |   Fcpu/8 |  Fcpu/4 |
66    |    X    |----------------|----------|---------|----------|---------|----------|---------|
67    |         |       DMA      |  Fcpu/4  |  Fcpu/2 |   Fcpu/2 | Fcpu/16 |   Fcpu/2 | Fcpu/16 |
68    |=========|================|==========|=========|==========|=========|==========|=========|
69    |         |     Polling    |  Fcpu/8  |  Fcpu/2 |    NA    |    NA   |   Fcpu/8 |  Fcpu/8 |
70    |         |----------------|----------|---------|----------|---------|----------|---------|
71    |    T    |     Interrupt  |  Fcpu/2  |  Fcpu/4 |    NA    |    NA   |  Fcpu/16 |  Fcpu/8 |
72    |    X    |----------------|----------|---------|----------|---------|----------|---------|
73    |         |       DMA      |  Fcpu/2  |  Fcpu/2 |    NA    |    NA   |   Fcpu/8 | Fcpu/16 |
74    +-----------------------------------------------------------------------------------------+
75   @note The max SPI frequency depend on SPI data size (4bits, 5bits,..., 8bits,...15bits, 16bits),
76         SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA).
77   @note
78    (#) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA()
79    (#) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA()
80    (#) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA()
81   
82   @endverbatim
83   ******************************************************************************
84   * @attention
85   *
86   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
87   *
88   * Redistribution and use in source and binary forms, with or without modification,
89   * are permitted provided that the following conditions are met:
90   *   1. Redistributions of source code must retain the above copyright notice,
91   *      this list of conditions and the following disclaimer.
92   *   2. Redistributions in binary form must reproduce the above copyright notice,
93   *      this list of conditions and the following disclaimer in the documentation
94   *      and/or other materials provided with the distribution.
95   *   3. Neither the name of STMicroelectronics nor the names of its contributors
96   *      may be used to endorse or promote products derived from this software
97   *      without specific prior written permission.
98   *
99   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
100   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
102   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
103   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
105   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
106   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
107   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
108   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109   *
110   ******************************************************************************  
111   */
112     
113 /* Includes ------------------------------------------------------------------*/
114 #include "stm32f3xx_hal.h"
115    
116 /** @addtogroup STM32F3xx_HAL_Driver
117   * @{
118   */
119
120 /** @defgroup SPI SPI HAL module driver
121   * @brief SPI HAL module driver
122   * @{
123   */
124 #ifdef HAL_SPI_MODULE_ENABLED
125
126 /* Private typedef -----------------------------------------------------------*/
127 /* Private define ------------------------------------------------------------*/
128 /** @defgroup SPI_Private_Define SPI Private Define
129  * @{
130  */
131 #define SPI_DEFAULT_TIMEOUT 50
132 /**
133   * @}
134   */
135
136 /* Private macro -------------------------------------------------------------*/
137 /* Private variables ---------------------------------------------------------*/
138 /* Private function prototypes -----------------------------------------------*/
139 /** @defgroup SPI_Private_Functions SPI Private Functions
140   * @{
141   */
142
143 static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
144 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
145 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
146 static void SPI_DMAError(DMA_HandleTypeDef *hdma);
147 static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout);
148 static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout);
149 static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
150 static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
151 static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
152 static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
153 static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
154 static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
155 static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
156 static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
157 static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
158 static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
159 static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
160 static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
161 static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
162 static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
163 static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
164 static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout);
165 static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout);
166 /**
167   * @}
168   */
169
170 /* Exported functions ---------------------------------------------------------*/
171
172 /** @defgroup SPI_Exported_Functions SPI Exported Functions
173   * @{
174   */
175
176 /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
177  *  @brief    Initialization and Configuration functions 
178  *
179 @verbatim    
180  ===============================================================================
181               ##### Initialization/de-initialization functions #####
182  ===============================================================================
183     [..]  This subsection provides a set of functions allowing to initialize and 
184           de-initialiaze the SPIx peripheral:
185
186       (+) User must Implement HAL_SPI_MspInit() function in which he configures 
187           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
188
189       (+) Call the function HAL_SPI_Init() to configure the selected device with 
190           the selected configuration:
191         (++) Mode
192         (++) Direction 
193         (++) Data Size
194         (++) Clock Polarity and Phase
195         (++) NSS Management
196         (++) BaudRate Prescaler
197         (++) FirstBit
198         (++) TIMode
199         (++) CRC Calculation
200         (++) CRC Polynomial if CRC enabled
201         (++) CRC Length, used only with Data8 and Data16  
202         (++) FIFO reception threshold
203
204       (+) Call the function HAL_SPI_DeInit() to restore the default configuration 
205           of the selected SPIx periperal.       
206  
207 @endverbatim
208   * @{
209   */
210
211 /**
212   * @brief  Initializes the SPI according to the specified parameters 
213   *         in the SPI_InitTypeDef and create the associated handle.
214   * @param  hspi: SPI handle
215   * @retval HAL status
216   */
217 HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
218 {
219   uint32_t frxth;
220   
221   /* Check the SPI handle allocation */
222   if(hspi == HAL_NULL)
223   { 
224     return HAL_ERROR;
225   }
226   
227   /* Check the parameters */
228   assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
229   assert_param(IS_SPI_MODE(hspi->Init.Mode));
230   assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
231   assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
232   assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
233   assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
234   assert_param(IS_SPI_NSS(hspi->Init.NSS));
235   assert_param(IS_SPI_NSSP(hspi->Init.NSSPMode));
236   assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
237   assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
238   assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
239   assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
240   assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
241   assert_param(IS_SPI_CRC_LENGTH(hspi->Init.CRCLength));
242   
243   hspi->State = HAL_SPI_STATE_BUSY;
244   
245   /* Init the low level hardware : GPIO, CLOCK, NVIC... */
246   HAL_SPI_MspInit(hspi);
247   
248   /* Disable the selected SPI peripheral */
249   __HAL_SPI_DISABLE(hspi);
250   
251   /* Align by default the rs fifo threshold on the data size */
252   if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
253   {
254     frxth = SPI_RXFIFO_THRESHOLD_HF;
255   }
256   else
257   {
258     frxth = SPI_RXFIFO_THRESHOLD_QF;
259   }
260   
261   /* CRC calculation is valid only for 16Bit and 8 Bit */
262   if(( hspi->Init.DataSize != SPI_DATASIZE_16BIT ) && ( hspi->Init.DataSize != SPI_DATASIZE_8BIT ))
263   {
264     /* CRC must be disabled */
265     hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
266   }
267   
268   /* Align the CRC Length on the data size */
269   if( hspi->Init.CRCLength == SPI_CRC_LENGTH_DATASIZE)
270   {
271     /* CRC Lengtht aligned on the data size : value set by default */
272     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
273     {
274       hspi->Init.CRCLength = SPI_CRC_LENGTH_16BIT;
275     }
276     else
277     {
278       hspi->Init.CRCLength = SPI_CRC_LENGTH_8BIT;
279     }
280   }
281   
282   /*---------------------------- SPIx CR1 & CR2 Configuration ------------------------*/
283   /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
284   Communication speed, First bit, CRC calculation state, CRC Length */
285   hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction | 
286                          hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
287                          hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit  | hspi->Init.CRCCalculation);
288   
289   if( hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
290   {
291     hspi->Instance->CR1|= SPI_CR1_CRCL;
292   }
293   
294   /* Configure : NSS management */
295   /* Configure : Rx Fifo Threshold */
296   hspi->Instance->CR2 = (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode | hspi->Init.NSSPMode |
297                          hspi->Init.DataSize ) | frxth;
298   
299   /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
300   /* Configure : CRC Polynomial */
301   hspi->Instance->CRCPR = hspi->Init.CRCPolynomial;
302   
303   hspi->ErrorCode = HAL_SPI_ERROR_NONE;
304   hspi->State= HAL_SPI_STATE_READY;
305   
306   return HAL_OK;
307 }
308
309 /**
310   * @brief  DeInitializes the SPI peripheral 
311   * @param  hspi: SPI handle
312   * @retval HAL status
313   */
314 HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
315 {
316   /* Check the SPI handle allocation */
317   if(hspi == HAL_NULL)
318   {
319      return HAL_ERROR;
320   }
321
322   /* Check the parameters */
323   assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
324
325   hspi->State = HAL_SPI_STATE_BUSY;
326   
327   /* Disable the SPI Peripheral Clock */
328   __HAL_SPI_DISABLE(hspi);
329   
330   /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
331   HAL_SPI_MspDeInit(hspi);
332   
333   hspi->ErrorCode = HAL_SPI_ERROR_NONE;
334   hspi->State = HAL_SPI_STATE_RESET;
335   
336   __HAL_UNLOCK(hspi);
337     
338   return HAL_OK;
339 }
340
341 /**
342   * @brief SPI MSP Init
343   * @param hspi: SPI handle
344   * @retval None
345   */
346  __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
347 {
348   /* NOTE : This function Should not be modified, when the callback is needed,
349             the HAL_SPI_MspInit could be implenetd in the user file
350    */
351 }
352
353 /**
354   * @brief SPI MSP DeInit
355   * @param hspi: SPI handle
356   * @retval None
357   */
358  __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
359 {
360   /* NOTE : This function Should not be modified, when the callback is needed,
361             the HAL_SPI_MspDeInit could be implenetd in the user file
362    */
363 }
364
365 /**
366   * @}
367   */
368
369 /** @defgroup SPI_Exported_Functions_Group2 Input and Output operation functions 
370  *  @brief   Data transfers functions 
371  *
372 @verbatim   
373  ===============================================================================
374                       ##### I/O operation functions #####
375  ===============================================================================  
376     This subsection provides a set of functions allowing to manage the SPI
377     data transfers.
378       
379     [..] The SPI supports master and slave mode : 
380
381     (#) There are two mode of transfer:
382        (+) Blocking mode: The communication is performed in polling mode. 
383             The HAL status of all data processing is returned by the same function 
384             after finishing transfer.  
385        (+) No-Blocking mode: The communication is performed using Interrupts 
386            or DMA, These API's return the HAL status.
387            The end of the data processing will be indicated through the 
388            dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when 
389            using DMA mode.
390            The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks 
391            will be executed respectivelly at the end of the transmit or Receive process
392            The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
393
394     (#) Blocking mode API's are :
395         (+) HAL_SPI_Transmit()in 1Line (simplex) and 2Lines (full duplex) mode
396         (+) HAL_SPI_Receive() in 1Line (simplex) and 2Lines (full duplex) mode
397         (+) HAL_SPI_TransmitReceive() in full duplex mode         
398         
399     (#) Non-Blocking mode API's with Interrupt are :
400         (+) HAL_SPI_Transmit_IT()in 1Line (simplex) and 2Lines (full duplex) mode
401         (+) HAL_SPI_Receive_IT() in 1Line (simplex) and 2Lines (full duplex) mode
402         (+) HAL_SPI_TransmitReceive_IT()in full duplex mode
403         (+) HAL_SPI_IRQHandler()
404
405     (#) No-Blocking mode functions with DMA are :
406         (+) HAL_SPI_Transmit_DMA()in 1Line (simplex) and 2Lines (full duplex) mode
407         (+) HAL_SPI_Receive_DMA() in 1Line (simplex) and 2Lines (full duplex) mode
408         (+) HAL_SPI_TransmitReceie_DMA() in full duplex mode
409           
410     (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
411         (+) HAL_SPI_TxCpltCallback()
412         (+) HAL_SPI_RxCpltCallback()
413         (+) HAL_SPI_ErrorCallback()
414         (+) HAL_SPI_TxRxCpltCallback()
415
416 @endverbatim
417   * @{
418   */
419
420 /**
421   * @brief  Transmit an amount of data in blocking mode
422   * @param  hspi: SPI handle
423   * @param  pData: pointer to data buffer
424   * @param  Size: amount of data to be sent 
425   * @param  Timeout: Timeout duration
426   * @retval HAL status
427   */
428 HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
429 {
430   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
431   
432   if(hspi->State != HAL_SPI_STATE_READY)
433   {
434     return HAL_BUSY;
435   }
436   
437   if((pData == HAL_NULL ) || (Size == 0))
438   {
439     return HAL_ERROR;
440   }
441   
442   /* Process Locked */
443   __HAL_LOCK(hspi);
444   
445   /* Set the transaction information */  
446   hspi->State       = HAL_SPI_STATE_BUSY_TX;
447   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
448   hspi->pTxBuffPtr  = pData;
449   hspi->TxXferSize  = Size;
450   hspi->TxXferCount = Size;
451   hspi->pRxBuffPtr  = HAL_NULL;
452   hspi->RxXferSize  = 0;
453   hspi->RxXferCount = 0;
454
455   /* Reset CRC Calculation */
456   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
457   {
458     __HAL_SPI_RESET_CRC(hspi);
459   }
460   
461   /* Configure communication direction : 1Line */
462   if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
463   {
464     __HAL_SPI_1LINE_TX(hspi);
465   }
466
467   /* Check if the SPI is already enabled */ 
468   if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
469   {
470     /* Enable SPI peripheral */
471     __HAL_SPI_ENABLE(hspi);
472   }
473   
474   /* Transmit data in 16 Bit mode */
475   if(hspi->Init.DataSize > SPI_DATASIZE_8BIT) 
476   {
477     while (hspi->TxXferCount > 0)
478     {
479       /* Wait until TXE flag is set to send data */
480       if(SPI_WaitFlagStateUntilTimeout(hspi,SPI_FLAG_TXE,SPI_FLAG_TXE,Timeout) != HAL_OK)
481       {
482         return HAL_TIMEOUT;
483       }
484       hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
485       hspi->pTxBuffPtr += sizeof(uint16_t);
486       hspi->TxXferCount--;
487     }
488   }
489   /* Transmit data in 8 Bit mode */
490   else
491   {
492     while (hspi->TxXferCount > 0)
493     {
494       if(hspi->TxXferCount != 0x1)
495       {
496         /* Wait until TXE flag is set to send data */
497         if(SPI_WaitFlagStateUntilTimeout(hspi,SPI_FLAG_TXE,SPI_FLAG_TXE,Timeout) != HAL_OK)
498         {
499           return HAL_TIMEOUT;
500         }
501         hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
502         hspi->pTxBuffPtr += sizeof(uint16_t);
503         hspi->TxXferCount -= 2;
504       }
505       else
506       {
507         /* Wait until TXE flag is set to send data */
508         if(SPI_WaitFlagStateUntilTimeout(hspi,SPI_FLAG_TXE,SPI_FLAG_TXE,Timeout) != HAL_OK)  
509         {
510           return HAL_TIMEOUT;
511         }
512         *((__IO uint8_t*)&hspi->Instance->DR) = (*hspi->pTxBuffPtr++);
513         hspi->TxXferCount--;    
514       }
515     }
516   }
517
518   /* Enable CRC Transmission */
519   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED) 
520   {
521      hspi->Instance->CR1|= SPI_CR1_CRCNEXT;
522   }
523
524   /* Check the end of the transaction */
525   if(SPI_EndRxTxTransaction(hspi,Timeout) != HAL_OK)
526   {
527     return HAL_TIMEOUT;
528   }
529   
530   /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
531   if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
532   {
533     __HAL_SPI_CLEAR_OVRFLAG(hspi);
534   }
535     
536   hspi->State = HAL_SPI_STATE_READY; 
537
538   /* Process Unlocked */
539   __HAL_UNLOCK(hspi);
540   
541   if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
542   {   
543     return HAL_ERROR;
544   }
545   else
546   {
547     return HAL_OK;
548   }
549 }
550
551 /**
552   * @brief  Receive an amount of data in blocking mode 
553   * @param  hspi: SPI handle
554   * @param  pData: pointer to data buffer
555   * @param  Size: amount of data to be sent
556   * @param  Timeout: Timeout duration
557   * @retval HAL status
558   */
559 HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
560 {
561   __IO uint16_t tmpreg;
562   
563   if(hspi->State != HAL_SPI_STATE_READY)
564   {
565     return HAL_BUSY;
566   }
567   
568   if((pData == HAL_NULL ) || (Size == 0))
569   {
570     return HAL_ERROR;
571   }
572
573   if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
574   {
575     /* the receive process is not supported in 2Lines direction master mode */
576     /* in this case we call the transmitReceive process                     */
577     return HAL_SPI_TransmitReceive(hspi,pData,pData,Size,Timeout);
578   }
579   
580   /* Process Locked */
581   __HAL_LOCK(hspi);
582     
583   hspi->State       = HAL_SPI_STATE_BUSY_RX;
584   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
585   hspi->pRxBuffPtr  = pData;
586   hspi->RxXferSize  = Size;
587   hspi->RxXferCount = Size;
588   hspi->pTxBuffPtr  = HAL_NULL;
589   hspi->TxXferSize  = 0;
590   hspi->TxXferCount = 0;
591     
592   /* Reset CRC Calculation */
593   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
594   {
595     __HAL_SPI_RESET_CRC(hspi);
596   }
597
598   /* Set the Rx Fido thresold */
599   if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
600   {
601     /* set fiforxthresold according the reception data lenght: 16bit */
602     CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
603   }
604   else
605   {
606     /* set fiforxthresold according the reception data lenght: 8bit */
607     SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
608   }
609
610   /* Configure communication direction 1Line and enabled SPI if needed */
611   if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
612   {
613     __HAL_SPI_1LINE_RX(hspi);
614   }
615
616   /* Check if the SPI is already enabled */ 
617   if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
618   {
619     /* Enable SPI peripheral */    
620     __HAL_SPI_ENABLE(hspi);
621   }
622
623   /* Receive data in 8 Bit mode */
624   if(hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
625   {
626     while(hspi->RxXferCount > 1)
627     {
628       /* Wait until the RXNE flag */
629       if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
630       {
631         return HAL_TIMEOUT;
632       }
633       (*hspi->pRxBuffPtr++)= *(__IO uint8_t *)&hspi->Instance->DR;
634       hspi->RxXferCount--;  
635     }
636   }
637   else /* Receive data in 16 Bit mode */
638   {   
639     while(hspi->RxXferCount > 1 )
640     {
641       /* Wait until RXNE flag is reset to read data */
642       if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
643       {
644         return HAL_TIMEOUT;
645       }
646       *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
647       hspi->pRxBuffPtr += sizeof(uint16_t);
648       hspi->RxXferCount--;
649     } 
650   }
651   
652   /* Enable CRC Transmission */
653   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED) 
654   {
655     hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
656   }  
657
658   /* Wait until RXNE flag is set */
659   if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
660   {
661     return HAL_TIMEOUT;
662   }
663   
664   /* Receive last data in 16 Bit mode */
665   if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
666   {        
667     *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
668     hspi->pRxBuffPtr += sizeof(uint16_t);
669   }
670   /* Receive last data in 8 Bit mode */
671   else 
672   {
673     (*hspi->pRxBuffPtr++) = *(__IO uint8_t *)&hspi->Instance->DR;
674   }
675   hspi->RxXferCount--;
676   
677   /* Read CRC from DR to close CRC calculation process */
678   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
679   {
680     /* Wait until TXE flag */
681     if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK) 
682     {
683       /* Erreur on the CRC reception */
684       hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
685     }
686     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
687     {        
688       tmpreg = hspi->Instance->DR;
689     }
690     else
691     {
692       tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
693       if((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
694       {
695         if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
696         {
697           /* Erreur on the CRC reception */
698           hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
699         }
700         tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
701       }
702     }
703   }
704   
705   /* Check the end of the transaction */
706   if(SPI_EndRxTransaction(hspi,Timeout) != HAL_OK)
707   {
708     return HAL_TIMEOUT;
709   }
710
711   hspi->State = HAL_SPI_STATE_READY; 
712     
713   /* Check if CRC error occurred */
714   if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
715   {
716     hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
717     __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
718                   
719     /* Process Unlocked */
720     __HAL_UNLOCK(hspi);
721     return HAL_ERROR;
722   }
723     
724   /* Process Unlocked */
725   __HAL_UNLOCK(hspi);
726   
727   if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
728   {   
729     return HAL_ERROR;
730   }
731   else
732   {
733     return HAL_OK;
734   }
735 }
736
737 /**
738   * @brief  Transmit and Receive an amount of data in blocking mode 
739   * @param  hspi: SPI handle
740   * @param  pTxData: pointer to transmission data buffer
741   * @param  pRxData: pointer to reception data buffer to be
742   * @param  Size: amount of data to be sent
743   * @param  Timeout: Timeout duration
744   * @retval HAL status
745   */
746 HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
747 {
748   __IO uint16_t tmpreg = 0;
749   uint32_t tickstart = 0;
750   
751   assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
752   
753   if(hspi->State != HAL_SPI_STATE_READY) 
754   {
755     return HAL_BUSY;
756   }
757   
758   if((pTxData == HAL_NULL) || (pRxData == HAL_NULL) || (Size == 0))
759   {
760     return HAL_ERROR;
761   }
762
763   tickstart = HAL_GetTick();
764   
765   /* Process Locked */
766   __HAL_LOCK(hspi); 
767   
768   hspi->State       = HAL_SPI_STATE_BUSY_TX_RX;
769   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
770   hspi->pRxBuffPtr  = pRxData;
771   hspi->RxXferCount = Size;
772   hspi->RxXferSize  = Size;
773   hspi->pTxBuffPtr  = pTxData;
774   hspi->TxXferCount = Size;
775   hspi->TxXferSize  = Size; 
776   
777   /* Reset CRC Calculation */
778   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
779   {
780     __HAL_SPI_RESET_CRC(hspi);
781   }
782   
783   /* Set the Rx Fido threshold */
784   if((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (hspi->RxXferCount > 1))
785   {
786     /* set fiforxthreshold according the reception data lenght: 16bit */
787     CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
788   }
789   else
790   {
791     /* set fiforxthreshold according the reception data lenght: 8bit */
792     SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
793   }
794   
795   /* Check if the SPI is already enabled */ 
796   if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
797   {
798     /* Enable SPI peripheral */    
799     __HAL_SPI_ENABLE(hspi);
800   }
801   
802   /* Transmit and Receive data in 16 Bit mode */
803   if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
804   {  
805     while ((hspi->TxXferCount > 0 ) || (hspi->RxXferCount > 0))
806     {
807       /* Wait until TXE flag */
808       if((hspi->TxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE))
809       {
810         hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
811         hspi->pTxBuffPtr += sizeof(uint16_t);
812         hspi->TxXferCount--;
813         
814         /* Enable CRC Transmission */
815         if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED))
816         {
817           SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
818         } 
819       }
820       
821       /* Wait until RXNE flag */
822       if((hspi->RxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE))
823       {
824         *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
825         hspi->pRxBuffPtr += sizeof(uint16_t);
826         hspi->RxXferCount--;
827       }
828       if(Timeout != HAL_MAX_DELAY)
829       {
830         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout)) 
831         {
832           hspi->State = HAL_SPI_STATE_READY;
833           __HAL_UNLOCK(hspi);
834           return HAL_TIMEOUT;
835         }
836       }
837     }  
838   }
839   /* Transmit and Receive data in 8 Bit mode */
840   else
841   { 
842     while((hspi->TxXferCount > 0) || (hspi->RxXferCount > 0))
843     {
844       /* check if TXE flag is set to send data */
845       if((hspi->TxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE))
846       {
847         if(hspi->TxXferCount > 2)
848         {
849           hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
850           hspi->pTxBuffPtr += sizeof(uint16_t);
851           hspi->TxXferCount -= 2;
852         } 
853         else
854         {
855           *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
856           hspi->TxXferCount--;
857         }
858         
859         /* Enable CRC Transmission */
860         if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED))
861         {
862           SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
863         }
864       }
865             
866       /* Wait until RXNE flag is reset */
867       if((hspi->RxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE))
868       {
869         if(hspi->RxXferCount > 1)
870         {
871           *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
872           hspi->pRxBuffPtr += sizeof(uint16_t);
873           hspi->RxXferCount -= 2;
874           if(hspi->RxXferCount <= 1)
875           {
876             /* set fiforxthresold before to switch on 8 bit data size */
877             SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
878           }
879         }
880         else
881         {
882           (*hspi->pRxBuffPtr++) =  *(__IO uint8_t *)&hspi->Instance->DR;
883           hspi->RxXferCount--;
884         }
885       }
886       if(Timeout != HAL_MAX_DELAY)
887       {
888         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
889         {
890           hspi->State = HAL_SPI_STATE_READY;
891           __HAL_UNLOCK(hspi);
892           return HAL_TIMEOUT;
893         }
894       }
895     }
896   }
897   
898   /* Read CRC from DR to close CRC calculation process */
899   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
900   {
901     /* Wait until TXE flag */
902     if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
903     {  
904       /* Erreur on the CRC reception */
905       hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
906     }
907     
908     if(hspi->Init.DataSize == SPI_DATASIZE_16BIT)
909     {
910       tmpreg = hspi->Instance->DR;
911     }
912     else
913     {
914       tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
915       if(hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
916       {
917         if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK) 
918         {  
919           /* Erreur on the CRC reception */
920           hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
921         }    
922         tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
923       }
924     }
925   }
926
927   /* Check the end of the transaction */
928   if(SPI_EndRxTxTransaction(hspi,Timeout) != HAL_OK)
929   {
930     return HAL_TIMEOUT;
931   }
932
933   hspi->State = HAL_SPI_STATE_READY;
934   
935   /* Check if CRC error occurred */
936   if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
937   {
938     hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
939     /* Clear CRC Flag */
940     __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
941     
942     /* Process Unlocked */
943     __HAL_UNLOCK(hspi);
944     
945     return HAL_ERROR;
946   }
947   
948   /* Process Unlocked */
949   __HAL_UNLOCK(hspi);
950   
951   if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
952   {   
953     return HAL_ERROR;
954   }
955   else
956   {
957     return HAL_OK;
958   }
959 }
960
961 /**
962   * @brief  Transmit an amount of data in no-blocking mode with Interrupt
963   * @param  hspi: SPI handle
964   * @param  pData: pointer to data buffer
965   * @param  Size: amount of data to be sent
966   * @retval HAL status
967   */
968 HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
969 {
970   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
971   
972   if(hspi->State == HAL_SPI_STATE_READY)
973   {
974     if((pData == HAL_NULL) || (Size == 0)) 
975     {
976       return  HAL_ERROR;                                    
977     }
978     
979     /* Process Locked */
980     __HAL_LOCK(hspi);
981     
982     hspi->State       = HAL_SPI_STATE_BUSY_TX;
983     hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
984     hspi->pTxBuffPtr  = pData;
985     hspi->TxXferSize  = Size;
986     hspi->TxXferCount = Size;
987     hspi->pRxBuffPtr  = HAL_NULL;
988     hspi->RxXferSize  = 0;
989     hspi->RxXferCount = 0;
990
991     /* Set the function for IT treatement */
992     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
993     {
994       hspi->RxISR = HAL_NULL;
995       hspi->TxISR = SPI_TxISR_16BIT;
996     }
997     else
998     {
999       hspi->RxISR = HAL_NULL;
1000       hspi->TxISR = SPI_TxISR_8BIT;
1001     }
1002     
1003     /* Configure communication direction : 1Line */
1004     if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
1005     {
1006       __HAL_SPI_1LINE_TX(hspi);
1007     }
1008     
1009     /* Reset CRC Calculation */
1010     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1011     {
1012       __HAL_SPI_RESET_CRC(hspi);    
1013     }
1014     
1015     /* Enable TXE and ERR interrupt */
1016     __HAL_SPI_ENABLE_IT(hspi,(SPI_IT_TXE));
1017
1018     /* Process Unlocked */
1019     __HAL_UNLOCK(hspi);
1020
1021     /* Note : The SPI must be enabled after unlocking current process 
1022               to avoid the risk of SPI interrupt handle execution before current
1023               process unlock */
1024         
1025     /* Check if the SPI is already enabled */ 
1026     if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1027     {
1028       /* Enable SPI peripheral */    
1029       __HAL_SPI_ENABLE(hspi);
1030     }
1031         
1032     return HAL_OK;
1033   }
1034   else
1035   {
1036     return HAL_BUSY;
1037   }
1038 }
1039
1040 /**
1041   * @brief  Receive an amount of data in no-blocking mode with Interrupt
1042   * @param  hspi: SPI handle
1043   * @param  pData: pointer to data buffer
1044   * @param  Size: amount of data to be sent
1045   * @retval HAL status
1046   */
1047 HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1048 {
1049   if(hspi->State == HAL_SPI_STATE_READY)
1050   {
1051     if((pData == HAL_NULL) || (Size == 0))
1052     { 
1053       return  HAL_ERROR;                      
1054     }
1055
1056     /* Process Locked */
1057     __HAL_LOCK(hspi);
1058     
1059     /* Configure communication */
1060     hspi->State       = HAL_SPI_STATE_BUSY_RX;
1061     hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1062     hspi->pRxBuffPtr  = pData;
1063     hspi->RxXferSize  = Size;
1064     hspi->RxXferCount = Size;
1065     hspi->pTxBuffPtr  = HAL_NULL;
1066     hspi->TxXferSize  = 0;
1067     hspi->TxXferCount = 0;
1068
1069     if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
1070     {
1071       /* Process Unlocked */
1072       __HAL_UNLOCK(hspi);
1073       /* the receive process is not supported in 2Lines direction master mode */
1074       /* in this we call the transmitReceive process          */
1075       return HAL_SPI_TransmitReceive_IT(hspi,pData,pData,Size);
1076     }
1077         
1078     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1079     {
1080       hspi->CRCSize = 1;
1081       if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
1082       {
1083         hspi->CRCSize = 2;
1084       }
1085     }
1086     else
1087     {
1088       hspi->CRCSize = 0;
1089     }
1090         
1091     /* check the data size to adapt Rx threshold and the set the function for IT treatement */
1092     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
1093     {
1094       /* set fiforxthresold according the reception data lenght: 16 bit */
1095       CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1096       hspi->RxISR = SPI_RxISR_16BIT;
1097       hspi->TxISR = HAL_NULL;
1098     }
1099     else
1100     {
1101       /* set fiforxthresold according the reception data lenght: 8 bit */
1102       SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1103       hspi->RxISR = SPI_RxISR_8BIT;
1104       hspi->TxISR = HAL_NULL;
1105     }
1106     
1107     /* Configure communication direction : 1Line */
1108     if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
1109     {
1110       __HAL_SPI_1LINE_RX(hspi);
1111     }
1112     
1113     /* Reset CRC Calculation */
1114     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1115     {
1116       __HAL_SPI_RESET_CRC(hspi);
1117     }
1118     
1119     /* Enable TXE and ERR interrupt */
1120     __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
1121     
1122     /* Process Unlocked */
1123     __HAL_UNLOCK(hspi);
1124     
1125     /* Note : The SPI must be enabled after unlocking current process 
1126     to avoid the risk of SPI interrupt handle execution before current
1127     process unlock */
1128     
1129     /* Check if the SPI is already enabled */ 
1130     if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1131     {
1132       /* Enable SPI peripheral */    
1133       __HAL_SPI_ENABLE(hspi);
1134     }
1135     
1136     return HAL_OK;
1137   }
1138   else
1139   {
1140     return HAL_BUSY; 
1141   }
1142 }
1143
1144 /**
1145   * @brief  Transmit and Receive an amount of data in no-blocking mode with Interrupt 
1146   * @param  hspi: SPI handle
1147   * @param  pTxData: pointer to transmission data buffer
1148   * @param  pRxData: pointer to reception data buffer to be
1149   * @param  Size: amount of data to be sent
1150   * @retval HAL status
1151   */
1152 HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1153 {
1154   assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1155   
1156   if((hspi->State == HAL_SPI_STATE_READY) || \
1157      ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX)))
1158   {
1159     if((pTxData == HAL_NULL ) || (pRxData == HAL_NULL ) || (Size == 0)) 
1160     {
1161       return  HAL_ERROR;                                    
1162     }
1163     
1164     /* Process locked */
1165     __HAL_LOCK(hspi);
1166     
1167     hspi->CRCSize = 0;
1168     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1169     {
1170       hspi->CRCSize = 1;
1171       if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
1172       {
1173         hspi->CRCSize = 2;
1174       }
1175     }
1176     
1177     if(hspi->State != HAL_SPI_STATE_BUSY_RX)
1178     {
1179       hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
1180     }
1181     
1182     hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1183     hspi->pTxBuffPtr  = pTxData;
1184     hspi->TxXferSize  = Size;
1185     hspi->TxXferCount = Size;
1186     hspi->pRxBuffPtr  = pRxData;
1187     hspi->RxXferSize  = Size;
1188     hspi->RxXferCount = Size;
1189     
1190     /* Set the function for IT treatement */
1191     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
1192     {
1193       hspi->RxISR = SPI_2linesRxISR_16BIT;
1194       hspi->TxISR = SPI_2linesTxISR_16BIT;       
1195     }
1196     else
1197     {
1198       hspi->RxISR = SPI_2linesRxISR_8BIT;
1199       hspi->TxISR = SPI_2linesTxISR_8BIT;
1200     }
1201     
1202     /* Reset CRC Calculation */
1203     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1204     {
1205       __HAL_SPI_RESET_CRC(hspi);
1206     }
1207     
1208     /* check if packing mode is enabled and if there is more than 2 data to receive */
1209     if((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (hspi->RxXferCount >= 2))
1210     {
1211       /* set fiforxthresold according the reception data lenght: 16 bit */
1212       CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1213     }
1214     else
1215     {
1216       /* set fiforxthresold according the reception data lenght: 8 bit */
1217       SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1218     }
1219     
1220     /* Enable TXE, RXNE and ERR interrupt */
1221     __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
1222     
1223     /* Process Unlocked */
1224     __HAL_UNLOCK(hspi);
1225     
1226     /* Check if the SPI is already enabled */ 
1227     if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1228     {
1229       /* Enable SPI peripheral */    
1230       __HAL_SPI_ENABLE(hspi);
1231     }
1232     
1233     return HAL_OK;
1234   }
1235   else
1236   {
1237     return HAL_BUSY;
1238   }
1239 }
1240
1241 /**
1242   * @brief  Transmit an amount of data in no-blocking mode with DMA
1243   * @param  hspi: SPI handle
1244   * @param  pData: pointer to data buffer
1245   * @param  Size: amount of data to be sent
1246   * @retval HAL status
1247   */
1248 HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1249 {    
1250   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
1251
1252   if(hspi->State != HAL_SPI_STATE_READY) 
1253   {
1254     return HAL_BUSY;
1255   }
1256   
1257   if((pData == HAL_NULL) || (Size == 0))
1258   {
1259     return HAL_ERROR;
1260   }
1261   
1262   /* Process Locked */
1263   __HAL_LOCK(hspi);
1264   
1265   hspi->State       = HAL_SPI_STATE_BUSY_TX;
1266   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1267   hspi->pTxBuffPtr  = pData;
1268   hspi->TxXferSize  = Size;
1269   hspi->TxXferCount = Size;
1270   hspi->pRxBuffPtr  = HAL_NULL;
1271   hspi->RxXferSize  = 0;
1272   hspi->RxXferCount = 0;
1273   
1274   /* Configure communication direction : 1Line */
1275   if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
1276   {
1277     __HAL_SPI_1LINE_TX(hspi);
1278   }
1279   
1280   /* Reset CRC Calculation */
1281   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1282   {
1283     __HAL_SPI_RESET_CRC(hspi);
1284   }
1285   
1286   /* Set the SPI TxDMA transfer complete callback */
1287   hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
1288   
1289   /* Set the DMA error callback */
1290   hspi->hdmatx->XferErrorCallback = SPI_DMAError;
1291   
1292   CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1293   /* packing mode is enabled only if the DMA setting is HALWORD */
1294   if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
1295   {
1296     /* Check the even/odd of the data size + crc if enabled */
1297     if((hspi->TxXferCount & 0x1) == 0)
1298     {
1299       CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1300       hspi->TxXferCount = (hspi->TxXferCount >> 1);
1301     }
1302     else
1303     {
1304       SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1305       hspi->TxXferCount = (hspi->TxXferCount >> 1) + 1;
1306     }
1307   }
1308   
1309   /* Enable the Tx DMA channel */
1310   HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
1311   
1312   /* Check if the SPI is already enabled */ 
1313   if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1314   {
1315     /* Enable SPI peripheral */    
1316     __HAL_SPI_ENABLE(hspi);
1317   }
1318
1319   /* Enable Tx DMA Request */
1320   hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
1321   
1322   /* Process Unlocked */
1323   __HAL_UNLOCK(hspi);
1324   
1325   return HAL_OK;
1326 }
1327
1328 /**
1329 * @brief  Receive an amount of data in no-blocking mode with DMA 
1330 * @param  hspi: SPI handle
1331 * @param  pData: pointer to data buffer
1332 * @param  Size: amount of data to be sent
1333 * @retval HAL status
1334 */
1335 HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1336 {
1337   if(hspi->State != HAL_SPI_STATE_READY)
1338   {
1339     return HAL_BUSY;
1340   }
1341   
1342   if((pData == HAL_NULL) || (Size == 0))
1343   {
1344     return HAL_ERROR;
1345   }
1346   
1347   /* Process Locked */
1348   __HAL_LOCK(hspi);
1349
1350   hspi->State       = HAL_SPI_STATE_BUSY_RX;
1351   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1352   hspi->pRxBuffPtr  = pData;
1353   hspi->RxXferSize  = Size;
1354   hspi->RxXferCount = Size;
1355   hspi->pTxBuffPtr  = HAL_NULL;
1356   hspi->TxXferSize  = 0;
1357   hspi->TxXferCount = 0;
1358
1359   if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
1360   {
1361     /* Process Unlocked */
1362     __HAL_UNLOCK(hspi); 
1363     /* the receive process is not supported in 2Lines direction master mode */
1364     /* in this case we call the transmitReceive process                     */
1365     return HAL_SPI_TransmitReceive_DMA(hspi,pData,pData,Size);
1366   }
1367   
1368   /* Configure communication direction : 1Line */
1369   if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
1370   {
1371     __HAL_SPI_1LINE_RX(hspi);
1372   }
1373   
1374   /* Reset CRC Calculation */
1375   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1376   {
1377     __HAL_SPI_RESET_CRC(hspi);
1378   }
1379   
1380   /* packing mode management is enabled by the DMA settings */
1381   if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
1382   {
1383     /* Process Locked */
1384     __HAL_UNLOCK(hspi);
1385     /* Restriction the DMA data received is not allowed in this mode */
1386     return HAL_ERROR;
1387   }
1388   
1389   CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1390   if( hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1391   {
1392     /* set fiforxthresold according the reception data lenght: 16bit */
1393     CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1394   }
1395   else
1396   {
1397     /* set fiforxthresold according the reception data lenght: 8bit */
1398     SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1399   }
1400   
1401   /* Set the SPI Rx DMA transfer complete callback */
1402   hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
1403   
1404   /* Set the DMA error callback */
1405   hspi->hdmarx->XferErrorCallback = SPI_DMAError;
1406   
1407   /* Enable Rx DMA Request */  
1408   hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
1409   
1410   /* Enable the Rx DMA channel */
1411   HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
1412   
1413   /* Process Unlocked */
1414   __HAL_UNLOCK(hspi);
1415   
1416   /* Check if the SPI is already enabled */ 
1417   if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1418   {
1419     /* Enable SPI peripheral */    
1420     __HAL_SPI_ENABLE(hspi);
1421   }
1422   
1423   return HAL_OK;
1424 }
1425
1426 /**
1427   * @brief  Transmit and Receive an amount of data in no-blocking mode with DMA 
1428   * @param  hspi: SPI handle
1429   * @param  pTxData: pointer to transmission data buffer
1430   * @param  pRxData: pointer to reception data buffer to be
1431   * @param  Size: amount of data to be sent
1432   * @retval HAL status
1433   */
1434 HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1435 {
1436   assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1437   
1438   if((hspi->State == HAL_SPI_STATE_READY) ||
1439      ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX)))
1440   {
1441     if((pTxData == HAL_NULL ) || (pRxData == HAL_NULL ) || (Size == 0)) 
1442     {
1443       return  HAL_ERROR;                                    
1444     }
1445     
1446     /* Process locked */
1447     __HAL_LOCK(hspi);
1448     
1449     /* check if the transmit Receive function is not called by a receive master */
1450     if(hspi->State != HAL_SPI_STATE_BUSY_RX)
1451     {  
1452       hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
1453     }
1454     
1455     hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1456     hspi->pTxBuffPtr  = (uint8_t *)pTxData;
1457     hspi->TxXferSize  = Size;
1458     hspi->TxXferCount = Size;
1459     hspi->pRxBuffPtr  = (uint8_t *)pRxData;
1460     hspi->RxXferSize  = Size;
1461     hspi->RxXferCount = Size;
1462     
1463     /* Reset CRC Calculation + increase the rxsize */
1464     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1465     {
1466       __HAL_SPI_RESET_CRC(hspi);
1467     }
1468     
1469     /* Reset the threshold bit */
1470     CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1471     CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1472     
1473     /* the packing mode management is enabled by the DMA settings according the spi data size */
1474     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1475     {
1476       /* set fiforxthreshold according the reception data lenght: 16bit */
1477       CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1478     }
1479     else
1480     {
1481       /* set fiforxthresold according the reception data lenght: 8bit */
1482       SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1483       
1484       if(hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
1485       {
1486         if((hspi->TxXferSize & 0x1) == 0x0 )
1487         {
1488           CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1489           hspi->TxXferCount = hspi->TxXferCount >> 1;
1490         }
1491         else
1492         {
1493           SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1494           hspi->TxXferCount = (hspi->TxXferCount >> 1) + 1;
1495         }      
1496       }
1497       
1498       if(hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
1499       {
1500         /* set fiforxthresold according the reception data lenght: 16bit */
1501         CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1502         
1503         /* Size must include the CRC lenght */
1504         if((hspi->RxXferCount & 0x1) == 0x0 )
1505         {
1506           CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1507           hspi->RxXferCount = hspi->RxXferCount >> 1;
1508         }
1509         else
1510         {
1511           SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1512           hspi->RxXferCount = (hspi->RxXferCount >> 1) + 1; 
1513         } 
1514       }
1515     }   
1516     
1517     /* Set the SPI Rx DMA transfer complete callback because the last generated transfer request is 
1518     the reception request (RXNE) */
1519     if(hspi->State == HAL_SPI_STATE_BUSY_RX)
1520     {
1521       hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
1522     }
1523     else
1524     {
1525       hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
1526     }
1527     /* Set the DMA error callback */
1528     hspi->hdmarx->XferErrorCallback = SPI_DMAError;
1529     
1530     /* Enable Rx DMA Request */  
1531     hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
1532     
1533     /* Enable the Rx DMA channel */
1534     HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t) hspi->pRxBuffPtr, hspi->RxXferCount);
1535     
1536     /* Set the SPI Tx DMA transfer complete callback as HAL_NULL because the communication closing
1537     is performed in DMA reception complete callback  */
1538     hspi->hdmatx->XferCpltCallback = HAL_NULL;
1539     
1540     /* Set the DMA error callback */
1541     hspi->hdmatx->XferErrorCallback = SPI_DMAError;
1542     
1543     /* Enable the Tx DMA channel */
1544     HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
1545     
1546     /* Check if the SPI is already enabled */ 
1547     if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1548     {
1549       /* Enable SPI peripheral */    
1550       __HAL_SPI_ENABLE(hspi);
1551     }
1552     
1553     /* Enable Tx DMA Request */  
1554     hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
1555     
1556     /* Process Unlocked */
1557     __HAL_UNLOCK(hspi);
1558     
1559     return HAL_OK;
1560   }
1561   else
1562   {
1563     return HAL_BUSY;
1564   }
1565 }
1566
1567 /**
1568   * @brief  This function handles SPI interrupt request.
1569   * @param  hspi: SPI handle
1570   * @retval HAL status
1571   */
1572 void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
1573 {
1574   /* SPI in mode Receiver ----------------------------------------------------*/
1575   if((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) == RESET) &&
1576      (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE) != RESET) && (__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_RXNE) != RESET))
1577   {
1578     hspi->RxISR(hspi);
1579     return;
1580   }
1581   
1582   /* SPI in mode Tramitter ---------------------------------------------------*/
1583   if((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE) != RESET) && (__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_TXE) != RESET))
1584   {   
1585     hspi->TxISR(hspi);
1586     return;
1587   }
1588   
1589   /* SPI in Erreur Treatment ---------------------------------------------------*/
1590   if((hspi->Instance->SR & (SPI_FLAG_MODF | SPI_FLAG_OVR | SPI_FLAG_FRE)) != RESET)  
1591   {
1592     /* SPI Overrun error interrupt occured -------------------------------------*/
1593     if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) != RESET) 
1594     {
1595       if(hspi->State != HAL_SPI_STATE_BUSY_TX)
1596       {
1597         hspi->ErrorCode |= HAL_SPI_ERROR_OVR;
1598         __HAL_SPI_CLEAR_OVRFLAG(hspi);
1599       }
1600       else
1601       {
1602         return;
1603       }
1604     }
1605     
1606     /* SPI Mode Fault error interrupt occured -------------------------------------*/
1607     if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_MODF) != RESET)
1608     { 
1609       hspi->ErrorCode |= HAL_SPI_ERROR_MODF;
1610       __HAL_SPI_CLEAR_MODFFLAG(hspi);
1611     }
1612     
1613     /* SPI Frame error interrupt occured ----------------------------------------*/
1614     if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_FRE) != RESET)
1615     { 
1616       hspi->ErrorCode |= HAL_SPI_ERROR_FRE;
1617       __HAL_SPI_CLEAR_FREFLAG(hspi);
1618     }
1619     
1620     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
1621     hspi->State = HAL_SPI_STATE_READY;
1622     HAL_SPI_ErrorCallback(hspi);
1623     
1624     return;
1625   }
1626 }
1627
1628 /**
1629   * @brief DMA SPI transmit process complete callback 
1630   * @param hdma : DMA handle
1631   * @retval None
1632   */
1633 static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)   
1634 {
1635   SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1636
1637   /* Disable Tx DMA Request */
1638   hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
1639
1640   /* Check the end of the transaction */
1641   SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
1642   
1643   /* Clear OVERUN flag in 2 Lines communication mode because received data is not read */
1644   if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
1645   {
1646     __HAL_SPI_CLEAR_OVRFLAG(hspi);
1647   }
1648     
1649   hspi->TxXferCount = 0;
1650   hspi->State = HAL_SPI_STATE_READY;
1651   
1652   /* Check if CRC error occurred or Error code */
1653   if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
1654   {
1655     hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
1656     __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
1657     HAL_SPI_ErrorCallback(hspi); 
1658   }
1659   else 
1660   {
1661     if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
1662     {
1663       HAL_SPI_TxCpltCallback(hspi);
1664     }
1665     else
1666     {
1667       HAL_SPI_ErrorCallback(hspi);
1668     }     
1669   }  
1670 }
1671
1672 /**
1673   * @brief DMA SPI receive process complete callback 
1674   * @param hdma : DMA handle
1675   * @retval None
1676   */
1677 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)   
1678 {
1679   __IO uint16_t tmpreg;
1680   SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1681   
1682   /* CRC handling */
1683   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1684   {
1685     /* Wait until TXE flag */
1686     if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT) != HAL_OK)
1687     {
1688       /* Erreur on the CRC reception */
1689       hspi->ErrorCode|= HAL_SPI_ERROR_CRC;      
1690     }
1691     if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1692     {        
1693       tmpreg = hspi->Instance->DR;
1694     }
1695     else
1696     {
1697       tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
1698       if(hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
1699       {
1700         if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT) != HAL_OK)
1701         {
1702           /* Erreur on the CRC reception */
1703           hspi->ErrorCode|= HAL_SPI_ERROR_CRC;      
1704         }
1705         tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
1706       }
1707     }  
1708   }
1709
1710   /* Disable Rx DMA Request */
1711   hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
1712   /* Disable Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
1713   hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
1714
1715   /* Check the end of the transaction */
1716   SPI_EndRxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
1717   
1718   hspi->RxXferCount = 0;
1719   hspi->State = HAL_SPI_STATE_READY;
1720   
1721   /* Check if CRC error occurred */
1722   if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
1723   {
1724     hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
1725     __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
1726     HAL_SPI_RxCpltCallback(hspi);
1727   }
1728   else
1729   {
1730     if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
1731     {
1732       HAL_SPI_RxCpltCallback(hspi);
1733     }
1734     else
1735     {
1736       HAL_SPI_ErrorCallback(hspi); 
1737     }
1738   }
1739 }
1740
1741 /**
1742   * @brief DMA SPI transmit receive process complete callback 
1743   * @param hdma : DMA handle
1744   * @retval None
1745   */
1746
1747 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)   
1748 {
1749   __IO int16_t tmpreg;
1750   SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1751   
1752   /* CRC handling */
1753   if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1754   {
1755     if((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_8BIT))
1756     {        
1757       if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_QUARTER_FULL, SPI_DEFAULT_TIMEOUT) != HAL_OK)
1758       {
1759         /* Erreur on the CRC reception */
1760         hspi->ErrorCode|= HAL_SPI_ERROR_CRC;      
1761       }
1762       tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
1763     }
1764     else
1765     {
1766       if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_HALF_FULL, SPI_DEFAULT_TIMEOUT) != HAL_OK)
1767       {
1768         /* Erreur on the CRC reception */
1769         hspi->ErrorCode|= HAL_SPI_ERROR_CRC;      
1770       }
1771       tmpreg = hspi->Instance->DR;
1772     }
1773   }  
1774   
1775   /* Check the end of the transaction */
1776   SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
1777   
1778   /* Disable Tx DMA Request */
1779   CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1780   
1781   /* Disable Rx DMA Request */
1782   CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1783    
1784   hspi->TxXferCount = 0;
1785   hspi->RxXferCount = 0;
1786   hspi->State = HAL_SPI_STATE_READY;
1787   
1788   /* Check if CRC error occurred */
1789   if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
1790   {
1791     hspi->ErrorCode = HAL_SPI_ERROR_CRC;
1792     __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
1793     HAL_SPI_ErrorCallback(hspi);
1794   }
1795   else
1796   {     
1797     if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
1798     {
1799       HAL_SPI_TxRxCpltCallback(hspi);
1800     }
1801     else
1802     {
1803       HAL_SPI_ErrorCallback(hspi);
1804     }
1805   }
1806 }
1807       
1808 /**
1809   * @brief DMA SPI communication error callback 
1810   * @param hdma : DMA handle
1811   * @retval None
1812   */
1813 static void SPI_DMAError(DMA_HandleTypeDef *hdma)   
1814 {
1815   SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1816   hspi->TxXferCount = 0;
1817   hspi->RxXferCount = 0;  
1818   hspi->ErrorCode|= HAL_SPI_ERROR_DMA;
1819   hspi->State = HAL_SPI_STATE_READY;
1820   HAL_SPI_ErrorCallback(hspi);
1821 }
1822
1823 /**
1824   * @brief Tx Transfer completed callbacks
1825   * @param hspi: SPI handle
1826   * @retval None
1827   */
1828 __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
1829 {
1830   /* NOTE : This function Should not be modified, when the callback is needed,
1831             the HAL_SPI_TxCpltCallback could be implenetd in the user file
1832    */ 
1833 }
1834
1835 /**
1836   * @brief Rx Transfer completed callbacks
1837   * @param hspi: SPI handle
1838   * @retval None
1839   */
1840 __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
1841 {
1842   /* NOTE : This function Should not be modified, when the callback is needed,
1843             the HAL_SPI_RxCpltCallback could be implenetd in the user file
1844    */
1845 }
1846   
1847 /**
1848   * @brief Tx and Rx Transfer completed callbacks
1849   * @param hspi: SPI handle
1850   * @retval None
1851   */
1852 __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
1853 {
1854   /* NOTE : This function Should not be modified, when the callback is needed,
1855             the HAL_SPI_TxRxCpltCallback could be implenetd in the user file
1856    */
1857 }
1858   
1859 /**
1860   * @brief SPI error callbacks
1861   * @param hspi: SPI handle
1862   * @retval None
1863   */
1864  __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
1865 {
1866   /* NOTE : This function Should not be modified, when the callback is needed,
1867             the HAL_SPI_ErrorCallback could be implenetd in the user file
1868    */ 
1869 }
1870
1871 /**
1872   * @}
1873   */
1874
1875 /** @defgroup SPI_Exported_Functions_Group3 Peripheral Control functions 
1876   *  @brief   SPI control functions 
1877  *
1878 @verbatim   
1879  ===============================================================================
1880                       ##### Peripheral Control functions #####
1881  ===============================================================================  
1882     [..]
1883     This subsection provides a set of functions allowing to control the SPI.
1884      (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral. 
1885      (+) HAL_SPI_Ctl() API can be used to update the spi configuration (only one parameter)
1886          without calling the HAL_SPI_Init() API
1887 @endverbatim
1888   * @{
1889   */
1890
1891 /**
1892   * @brief  Return the SPI state
1893   * @param  hspi : SPI handle
1894   * @retval HAL state
1895   */
1896 HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
1897 {
1898   return hspi->State;
1899 }
1900 /**
1901   * @}
1902   */
1903
1904 /**
1905   * @}
1906   */
1907
1908 /** @addtogroup SPI_Private_Functions SPI Private Functions
1909  *  @brief   Data transfers Private functions 
1910   * @{
1911   */
1912
1913 /**
1914   * @brief  Rx Handler for Transmit and Receive in Interrupt mode
1915   * @param  hspi: SPI handle
1916   */
1917 static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
1918 {
1919   /* Receive data in packing mode */
1920   if(hspi->RxXferCount > 1)
1921   {
1922     *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
1923     hspi->pRxBuffPtr += sizeof(uint16_t);
1924     hspi->RxXferCount -= 2;
1925     if(hspi->RxXferCount == 1)
1926     {
1927       /* set fiforxthresold according the reception data lenght: 8bit */
1928       SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);      
1929     }    
1930   }
1931   /* Receive data in 8 Bit mode */
1932   else
1933   {
1934     *hspi->pRxBuffPtr++ = *((__IO uint8_t *)&hspi->Instance->DR);
1935     hspi->RxXferCount--;
1936   }
1937   
1938   /* check end of the reception */
1939   if(hspi->RxXferCount == 0)
1940   {
1941     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1942     {
1943       hspi->RxISR =  SPI_2linesRxISR_8BITCRC; 
1944       return;
1945     }
1946         
1947     /* Disable RXNE interrupt */
1948     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); 
1949     
1950     if(hspi->TxXferCount == 0)
1951     {
1952       SPI_CloseRxTx_ISR(hspi);
1953     }
1954   }
1955 }
1956
1957 /**
1958   * @brief  Rx Handler for Transmit and Receive in Interrupt mode
1959   * @param  hspi: SPI handle
1960   */
1961 static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
1962 {
1963   __IO uint8_t tmpreg;
1964   
1965   tmpreg = *((__IO uint8_t *)&hspi->Instance->DR);
1966   hspi->CRCSize--;
1967   
1968   /* check end of the reception */
1969   if(hspi->CRCSize == 0)
1970   {
1971     /* Disable RXNE interrupt */
1972     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); 
1973     
1974     if(hspi->TxXferCount == 0)
1975     {
1976       SPI_CloseRxTx_ISR(hspi);
1977     }
1978   }
1979 }
1980
1981 /**
1982   * @brief  Tx Handler for Transmit and Receive in Interrupt mode
1983   * @param  hspi: SPI handle
1984   */
1985 static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
1986 {
1987   /* Transmit data in packing Bit mode */
1988   if(hspi->TxXferCount >= 2)
1989   {
1990     hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
1991     hspi->pTxBuffPtr += sizeof(uint16_t);
1992     hspi->TxXferCount -= 2;
1993   }
1994   /* Transmit data in 8 Bit mode */
1995   else
1996   {        
1997     *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
1998     hspi->TxXferCount--;
1999   }
2000   
2001   /* check the end of the transmission */
2002   if(hspi->TxXferCount == 0)
2003   {
2004     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2005     {
2006       hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
2007     }
2008     /* Disable TXE interrupt */
2009     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
2010     
2011     if(hspi->RxXferCount == 0)
2012     { 
2013       SPI_CloseRxTx_ISR(hspi);
2014     }
2015   }
2016 }
2017
2018 /**
2019   * @brief  Rx 16Bit Handler for Transmit and Receive in Interrupt mode
2020   * @param  hspi: SPI handle
2021   */
2022 static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
2023 {
2024   /* Receive data in 16 Bit mode */
2025   *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
2026   hspi->pRxBuffPtr += sizeof(uint16_t);
2027   hspi->RxXferCount--;  
2028   
2029   if(hspi->RxXferCount == 0)
2030   {
2031     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2032     {
2033       hspi->RxISR =  SPI_2linesRxISR_16BITCRC; 
2034       return;
2035     }
2036     
2037     /* Disable RXNE interrupt */
2038     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); 
2039     
2040     if(hspi->TxXferCount == 0)
2041     {
2042       SPI_CloseRxTx_ISR(hspi);
2043     }
2044   }
2045 }
2046
2047 /**
2048   * @brief  Manage the CRC 16bit receive for Transmit and Receive in Interrupt mode
2049   * @param  hspi: SPI handle
2050   */
2051 static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
2052 {
2053   __IO uint16_t tmpreg;
2054   /* Receive data in 16 Bit mode */
2055   tmpreg = hspi->Instance->DR;
2056   
2057   /* Disable RXNE interrupt */
2058   __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE); 
2059   
2060   SPI_CloseRxTx_ISR(hspi);
2061 }
2062
2063 /**
2064   * @brief  Tx Handler for Transmit and Receive in Interrupt mode
2065   * @param  hspi: SPI handle
2066   */
2067 static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
2068 {
2069   /* Transmit data in 16 Bit mode */
2070   hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
2071   hspi->pTxBuffPtr += sizeof(uint16_t);
2072   hspi->TxXferCount--;
2073   
2074   /* Enable CRC Transmission */
2075   if(hspi->TxXferCount == 0)
2076   {
2077     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2078     {
2079       hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
2080     }
2081     /* Disable TXE interrupt */
2082     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
2083     
2084     if(hspi->RxXferCount == 0)
2085     { 
2086       SPI_CloseRxTx_ISR(hspi);
2087     }
2088   }
2089 }
2090
2091 /**
2092   * @brief  Manage the CRC receive in Interrupt context
2093   * @param  hspi: SPI handle
2094   */
2095 static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
2096 {
2097   __IO uint8_t tmpreg;
2098   tmpreg = *((__IO uint8_t*)&hspi->Instance->DR);
2099   hspi->CRCSize--;
2100   
2101   if(hspi->CRCSize == 0)
2102   { 
2103     SPI_CloseRx_ISR(hspi);
2104   }
2105 }
2106
2107 /**
2108   * @brief  Manage the recieve in Interrupt context
2109   * @param  hspi: SPI handle
2110   */
2111 static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
2112 {
2113   *hspi->pRxBuffPtr++ = (*(__IO uint8_t *)&hspi->Instance->DR);
2114   hspi->RxXferCount--;
2115   
2116   /* Enable CRC Transmission */
2117   if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)) 
2118   {
2119     hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
2120   }
2121   
2122   if(hspi->RxXferCount == 0)
2123   {
2124     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2125     {
2126       hspi->RxISR =  SPI_RxISR_8BITCRC; 
2127       return;
2128     }
2129     SPI_CloseRx_ISR(hspi);
2130   }
2131 }
2132
2133 /**
2134   * @brief  Manage the CRC 16bit recieve in Interrupt context
2135   * @param  hspi: SPI handle
2136   */
2137 static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
2138 {
2139   __IO uint16_t tmpreg;
2140   
2141   tmpreg = hspi->Instance->DR;
2142   
2143   /* Disable RXNE and ERR interrupt */
2144   __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
2145   
2146   SPI_CloseRx_ISR(hspi);
2147 }
2148
2149 /**
2150   * @brief  Manage the 16Bit recieve in Interrupt context
2151   * @param  hspi: SPI handle
2152   */
2153 static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
2154 {
2155   *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
2156   hspi->pRxBuffPtr += sizeof(uint16_t);
2157   hspi->RxXferCount--;
2158   
2159   /* Enable CRC Transmission */
2160   if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)) 
2161   {
2162     hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
2163   }
2164   
2165   if(hspi->RxXferCount == 0)
2166   {    
2167     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2168     {
2169       hspi->RxISR = SPI_RxISR_16BITCRC;
2170       return;
2171     }
2172     SPI_CloseRx_ISR(hspi);
2173   }
2174 }
2175
2176 /**
2177   * @brief  Handle the data 8Bit transmit in Interrupt mode
2178   * @param  hspi: SPI handle
2179   */
2180 static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
2181 {
2182   *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
2183   hspi->TxXferCount--;
2184   
2185   if(hspi->TxXferCount == 0)
2186   {
2187     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2188     {
2189       /* Enable CRC Transmission */
2190       hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
2191     }
2192     SPI_CloseTx_ISR(hspi);
2193   }
2194 }
2195
2196 /**
2197   * @brief  Handle the data 16Bit transmit in Interrupt mode
2198   * @param  hspi: SPI handle
2199   */
2200 static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
2201
2202   /* Transmit data in 16 Bit mode */
2203   hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
2204   hspi->pTxBuffPtr += sizeof(uint16_t);
2205   hspi->TxXferCount--;
2206   
2207   if(hspi->TxXferCount == 0)
2208   {
2209     if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2210     {
2211       /* Enable CRC Transmission */
2212       hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
2213     }
2214     SPI_CloseTx_ISR(hspi);
2215   }
2216 }
2217
2218 /**
2219   * @brief This function handles SPI Communication Timeout.
2220   * @param hspi: SPI handle
2221   * @param Flag : SPI flag to check
2222   * @param State : flag state to check
2223   * @param Timeout : Timeout duration
2224   * @retval HAL status
2225   */
2226 static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout)
2227 {
2228   uint32_t tickstart = HAL_GetTick();
2229      
2230   while((hspi->Instance->SR & Flag) != State)
2231   {
2232     if(Timeout != HAL_MAX_DELAY)
2233     {
2234       if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
2235       {
2236         /* Disable the SPI and reset the CRC: the CRC value should be cleared
2237         on both master and slave sides in order to resynchronize the master
2238         and slave for their respective CRC calculation */
2239         
2240         /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
2241         __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
2242         
2243         if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
2244         {
2245           /* Disable SPI peripheral */
2246           __HAL_SPI_DISABLE(hspi);
2247         }
2248         
2249         /* Reset CRC Calculation */
2250         if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2251         {
2252           __HAL_SPI_RESET_CRC(hspi);
2253         }
2254         
2255         hspi->State= HAL_SPI_STATE_READY;
2256         
2257         /* Process Unlocked */
2258         __HAL_UNLOCK(hspi);
2259         
2260         return HAL_TIMEOUT;
2261       }
2262     }
2263   }
2264   
2265   return HAL_OK;      
2266 }
2267
2268 /**
2269   * @brief This function handles SPI Communication Timeout.
2270   * @param hspi: SPI handle
2271   * @param Flag: Fifo flag to check
2272   * @param State: Fifo state to check
2273   * @param Timeout : Timeout duration
2274   * @retval HAL status
2275   */
2276 static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout)
2277 {
2278   __IO uint8_t tmpreg;
2279   uint32_t tickstart = HAL_GetTick();
2280
2281   while((hspi->Instance->SR & Flag) != State)
2282   {
2283     if((Flag == SPI_SR_FRLVL) && (State == SPI_FRLVL_EMPTY))
2284     {
2285       tmpreg = *((__IO uint8_t*)&hspi->Instance->DR);
2286     }
2287     if(Timeout != HAL_MAX_DELAY)
2288     {
2289       if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
2290       {
2291         /* Disable the SPI and reset the CRC: the CRC value should be cleared
2292         on both master and slave sides in order to resynchronize the master
2293         and slave for their respective CRC calculation */
2294         
2295         /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
2296         __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
2297         
2298         if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
2299         {
2300           /* Disable SPI peripheral */
2301           __HAL_SPI_DISABLE(hspi);
2302         }
2303         
2304         /* Reset CRC Calculation */
2305         if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2306         {
2307           __HAL_SPI_RESET_CRC(hspi);
2308         }
2309         
2310         hspi->State = HAL_SPI_STATE_READY;
2311         
2312         /* Process Unlocked */
2313         __HAL_UNLOCK(hspi);
2314         
2315         return HAL_TIMEOUT;
2316       }
2317     }
2318   }
2319   
2320   return HAL_OK;      
2321 }
2322
2323 /**
2324   * @brief This function handles the check of the RX transaction complete.
2325   * @param hspi: SPI handle
2326   * @param Timeout : Timeout duration
2327   */
2328 static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi,  uint32_t Timeout)
2329 {
2330   if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
2331   {
2332     /* Disable SPI peripheral */
2333     __HAL_SPI_DISABLE(hspi);
2334   }
2335   if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout) != HAL_OK)
2336   {  
2337     hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
2338     return HAL_TIMEOUT;
2339   }
2340   if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout) != HAL_OK) 
2341   {
2342     hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
2343     return HAL_TIMEOUT;
2344   }
2345   
2346   return HAL_OK;
2347 }
2348   
2349 /**
2350   * @brief This function handles the check of the RXTX or TX transaction complete.
2351   * @param hspi: SPI handle
2352   * @param Timeout : Timeout duration
2353   */
2354 static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout)
2355 {
2356   /* Procedure to check the transaction complete */
2357   if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FTLVL, SPI_FTLVL_EMPTY, Timeout) != HAL_OK)
2358   {
2359     hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
2360     return HAL_TIMEOUT;
2361   }
2362   if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout) != HAL_OK)
2363   {
2364     hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
2365     return HAL_TIMEOUT;
2366   }
2367   if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout) != HAL_OK)
2368   {
2369     hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
2370     return HAL_TIMEOUT;
2371   }
2372   return HAL_OK;
2373 }
2374
2375 /**
2376   * @brief This function handles the close of the RXTX transaction.
2377   * @param hspi: SPI handle
2378   */
2379 static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
2380 {
2381   /* Disable ERR interrupt */
2382   __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
2383   
2384   /* Check the end of the transaction */
2385   SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
2386   
2387   /* Check if CRC error occurred */
2388   if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
2389   {
2390     hspi->State = HAL_SPI_STATE_READY;
2391     hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
2392     __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
2393     HAL_SPI_ErrorCallback(hspi);
2394   }
2395   else
2396   {
2397     if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
2398     {
2399       if(hspi->State == HAL_SPI_STATE_BUSY_RX)
2400       {
2401         hspi->State = HAL_SPI_STATE_READY;
2402         HAL_SPI_RxCpltCallback(hspi);
2403       }
2404       else
2405       {
2406         hspi->State = HAL_SPI_STATE_READY;
2407         HAL_SPI_TxRxCpltCallback(hspi);
2408       }      
2409     }
2410     else
2411     {
2412       hspi->State = HAL_SPI_STATE_READY;
2413       HAL_SPI_ErrorCallback(hspi);
2414     }
2415   }
2416 }
2417
2418 /**
2419   * @brief This function handles the close of the RX transaction.
2420   * @param hspi: SPI handle
2421   */
2422 static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
2423 {
2424     /* Disable RXNE and ERR interrupt */
2425     __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
2426     
2427     /* Check the end of the transaction */
2428     SPI_EndRxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
2429     
2430     hspi->State = HAL_SPI_STATE_READY; 
2431     
2432     /* Check if CRC error occurred */
2433     if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
2434     {
2435       hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
2436       __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
2437       HAL_SPI_ErrorCallback(hspi);         
2438     }
2439     else
2440     {
2441       if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
2442       {
2443         HAL_SPI_RxCpltCallback(hspi);         
2444       }
2445       else
2446       {
2447         HAL_SPI_ErrorCallback(hspi);
2448       }
2449     }
2450 }
2451
2452 /**
2453   * @brief This function handles the close of the TX transaction.
2454   * @param hspi: SPI handle
2455   */
2456 static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
2457 {
2458   /* Disable TXE and ERR interrupt */
2459   __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
2460   
2461   /* Check the end of the transaction */
2462   SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
2463   
2464   /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
2465   if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
2466   {
2467     __HAL_SPI_CLEAR_OVRFLAG(hspi);
2468   }
2469   
2470   hspi->State = HAL_SPI_STATE_READY;
2471   if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2472   {
2473     HAL_SPI_ErrorCallback(hspi);
2474   }
2475   else
2476   {
2477     HAL_SPI_TxCpltCallback(hspi);
2478   }
2479 }
2480
2481 /**
2482   * @}
2483   */
2484
2485 #endif /* HAL_SPI_MODULE_ENABLED */
2486 /**
2487   * @}
2488   */
2489
2490 /**
2491   * @}
2492   */
2493
2494 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/