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