1 /**
2 ******************************************************************************
3 * @file    HAL_spi.c
4 * @author  AE Team
5 * @version V1.1.0
6 * @date    28/08/2019
7 * @brief   This file provides all the SPI firmware functions.
8 ******************************************************************************
9 * @copy
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
19 */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "HAL_spi.h"
23 
24 
25 /** @addtogroup StdPeriph_Driver
26 * @{
27 */
28 
29 /** @defgroup SPI
30 * @brief SPI driver modules
31 * @{
32 */
33 
34 /** @defgroup SPI_Private_TypesDefinitions
35 * @{
36 */
37 
38 /**
39 * @}
40 */
41 
42 
43 /** @defgroup SPI_Private_Defines
44 * @{
45 */
46 
47 
48 
49 /* SPI SPIENE mask */
50 #define GCTL_SPIEN_Set          ((uint16_t)0x0001)
51 #define GCTL_SPIEN_Reset        ((uint16_t)0xFFFE)
52 /* SPI registers Masks */
53 #define GCTL_CLEAR_Mask       ((uint16_t)0xF000)
54 #define CCTL_CLEAR_Mask       ((uint16_t)0xFFC0)
55 #define SPBRG_CLEAR_Mask      ((uint16_t)0x0000)
56 #define SPI_DataSize_Mask     ((uint16_t)0xFCFF)
57 /**
58 * @}
59 */
60 
61 /** @defgroup SPI_Private_Macros
62 * @{
63 */
64 
65 /**
66 * @}
67 */
68 
69 /** @defgroup SPI_Private_Variables
70 * @{
71 */
72 
73 /**
74 * @}
75 */
76 
77 /** @defgroup SPI_Private_FunctionPrototypes
78 * @{
79 */
80 
81 /**
82 * @}
83 */
84 
85 /** @defgroup SPI_Private_Functions
86 * @{
87 */
88 
89 /**
90 * @brief  Deinitializes the SPIx peripheral registers to their default
91 *   reset values .
92 * @param SPIx: where x can be 0, 1 to select the SPI peripheral.
93 * @retval : None
94 */
SPI_DeInit(SPI_TypeDef * SPIx)95 void SPI_DeInit(SPI_TypeDef* SPIx)
96 {
97     /* Check the parameters */
98     assert_param(IS_SPI_ALL_PERIPH(SPIx));
99 
100     switch (*(uint32_t*)&SPIx)
101     {
102         case SPI1_BASE:
103             /* Enable SPI1 reset state */
104             RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
105             /* Release SPI1 from reset state */
106             RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
107             break;
108         case SPI2_BASE:
109             /* Enable SPI2 reset state */
110             RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
111             /* Release SPI1 from reset state */
112             RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
113             break;
114 
115 
116         default:
117             break;
118     }
119 }
120 
121 /**
122 * @brief  Initializes the SPIx peripheral according to the specified
123 *   parameters in the SPI_InitStruct.
124 * @param SPIx: where x can be 0, 1 to select the SPI peripheral.
125 * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
126 *   contains the configuration information for the specified
127 *   SPI peripheral.
128 * @retval : None
129 */
SPI_Init(SPI_TypeDef * SPIx,SPI_InitTypeDef * SPI_InitStruct)130 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
131 {
132     uint32_t tmpreg = 0;
133 
134     /* check the parameters */
135     assert_param(IS_SPI_ALL_PERIPH(SPIx));
136 
137     /* Check the SPI parameters */
138     assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
139     assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
140     assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
141     assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
142     assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
143     assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
144     assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
145     assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
146     assert_param(IS_SPI_DATAWIDRH(SPI_InitStruct->SPI_DataWidth));
147     assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
148     /*---------------------------- SPIx GCTL Configuration ------------------------*/
149     /* Get the SPIx GCTL value */
150     tmpreg = SPIx->GCTL;
151     /* Clear csn_sel, dmamode, txtlf, rxtlf,data_sel, rxen, txen, mm, int_en, spien bits */
152     tmpreg &= GCTL_CLEAR_Mask;
153     /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
154     master/salve mode, CPOL and CPHA */
155     /* Set dat_sel bits according to SPI_DataSize value */
156     /* Set csn and csn_sel bits according to SPI_NSS value */
157     /* Set mm bit according to SPI_Mode value */
158     tmpreg |= (uint32_t)((uint32_t) SPI_InitStruct->SPI_DataSize  |  SPI_InitStruct->SPI_NSS |
159                          SPI_InitStruct->SPI_Mode  );
160     /* Write to SPIx GCTL */
161     SPIx->GCTL = tmpreg;
162     /*---------------------------- SPIx CCTL Configuration ------------------------*/
163     tmpreg = SPIx->CCTL;
164     /* Clear spilen, lsbfe, CPOL, CPHA bits */
165     tmpreg &= CCTL_CLEAR_Mask;
166     /* Set Spilen bit according to SPI_DataWidth value */
167     /* Set LSBFirst bit according to SPI_FirstBit value */
168     /* Set CPOL bit according to SPI_CPOL value */
169     /* Set CPHA bit according to SPI_CPHA value */
170     tmpreg |= (uint16_t)( SPI_InitStruct->SPI_FirstBit   |  SPI_InitStruct->SPI_CPOL |
171                           SPI_InitStruct->SPI_CPHA) ;
172 
173     /* Write to SPIx CCTL */
174     SPIx->CCTL = tmpreg | 0x18;
175     /*---------------------------- SPIx SPBRG Configuration ------------------------*/
176     tmpreg = SPIx->SPBRG;
177     /* Clear spbrg bits */
178     tmpreg &= (uint16_t)SPBRG_CLEAR_Mask;
179     /* Set BR bits according to SPI_BaudRatePrescaler value */
180     tmpreg |= (uint16_t) SPI_InitStruct->SPI_BaudRatePrescaler;
181     /* Write to SPIx SPBRG */
182     SPIx->SPBRG = tmpreg;
183 
184     if((SPI_InitStruct->SPI_DataWidth) != SPI_DataWidth_8b)
185     {
186         SPIx->CCTL |= 1 << 2; //lsbfe
187         SPIx->CCTL |= 1 << 3; //spilen
188     }
189     SPIx->EXTCTL = SPI_InitStruct->SPI_DataWidth;
190 }
191 
192 /**
193 * @brief  Fills each SPI_InitStruct member with its default value.
194 * @param SPI_InitStruct : pointer to a SPI_InitTypeDef structure
195 *   which will be initialized.
196 * @retval : None
197 */
SPI_StructInit(SPI_InitTypeDef * SPI_InitStruct)198 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
199 {
200     /*--------------- Reset SPI init structure parameters values -----------------*/
201 
202     /* initialize the SPI_Mode member */
203     SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;
204     /* initialize the SPI_DataSize member */
205     SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
206     /* Initialize the SPILEN member */
207     SPI_InitStruct->SPI_DataWidth = SPI_DataWidth_8b;
208     /* Initialize the SPI_CPOL member */
209     SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
210     /* Initialize the SPI_CPHA member */
211     SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
212     /* Initialize the SPI_NSS member */
213     SPI_InitStruct->SPI_NSS = SPI_NSS_Soft;
214     /* Initialize the SPI_BaudRatePrescaler member */
215     SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
216     /* Initialize the SPI_FirstBit member */
217     SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
218 
219 }
220 
221 
222 /**
223 * @brief  Enables or disables the specified SPI peripheral.
224 * @param SPIx: where x can be 0, 1 to select the SPI peripheral.
225 * @param NewState: new state of the SPIx peripheral.
226 *   This parameter can be: ENABLE or DISABLE.
227 * @retval : None
228 */
SPI_Cmd(SPI_TypeDef * SPIx,FunctionalState NewState)229 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
230 {
231     /* Check the parameters */
232     assert_param(IS_SPI_ALL_PERIPH(SPIx));
233     assert_param(IS_FUNCTIONAL_STATE(NewState));
234     if (NewState != DISABLE)
235     {
236         /* Enable the selected SPI peripheral */
237         SPIx->GCTL |= GCTL_SPIEN_Set;
238     }
239     else
240     {
241         /* Disable the selected SPI peripheral */
242         SPIx->GCTL &= GCTL_SPIEN_Reset;
243     }
244 }
245 
246 
247 /**
248 * @brief  Enables or disables the specified SPIinterrupts.
249 * @param SPIx: where x can be :
250 *   0, 1  in SPI mode
251 * @param SPI_IT: specifies the SPI interrupt source to be
252 *   enabled or disabled.
253 *   This parameter can be one of the following values:
254 * @arg SPI_IT_TX: Tx buffer empty interrupt mask
255 * @arg SPI_IT_RX: Rx buffer  interrupt mask
256 * @arg SPI_IT_UNDERRUN: under Error interrupt mask in slave mode
257 * @arg SPI_IT_RXOVER: RX OVER Error interrupt mask
258 * @arg SPI_IT_RXMATCH: spectials rx data numbers  interrupt mask
259 * @arg SPI_IT_RXFULL: Rx buffer full interrupt mask
260 * @arg SPI_IT_TXEPT: Tx buffer empty interrupt mask
261 * @param NewState: new state of the specified SPI interrupt.
262 *   This parameter can be: ENABLE or DISABLE.
263 * @retval : None
264 */
SPI_ITConfig(SPI_TypeDef * SPIx,uint8_t SPI_IT,FunctionalState NewState)265 void SPI_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_IT, FunctionalState NewState)
266 {
267     /* Check the parameters */
268     assert_param(IS_SPI_ALL_PERIPH(SPIx));
269     assert_param(IS_FUNCTIONAL_STATE(NewState));
270     assert_param(IS_SPI_CONFIG_IT(SPI_IT));
271 
272     if (NewState != DISABLE)
273     {
274         /* Enable the selected SPI Global interrupt */
275         SPIx->GCTL |= SPI_INT_EN;
276         /* Enable the selected SPI interrupt */
277         SPIx->INTEN |= SPI_IT;
278     }
279     else
280     {
281         /* Disable the selected SPI interrupt */
282         SPIx->INTEN &= (uint16_t)~SPI_IT;
283         /* Disable the selected SPI Global interrupt */
284         SPIx->GCTL &= (uint16_t)~SPI_INT_EN;
285     }
286 
287 }
288 
289 /**
290 * @brief  Enables or disables the SPIx DMA interface.
291 * @param SPIx: where x can be :
292 *   0, 1 in SPI mode
293 * @param SPI_DMAReq: specifies the SPI DMA transfer request
294 *   to be enabled or disabled.
295 *   This parameter can be any combination of the following values:
296 * @arg SPI_DMAReq_EN: DMA transfer request enable
297 * @param NewState: new state of the selected SPI DMA transfer
298 *   request.
299 *   This parameter can be: ENABLE or DISABLE.
300 * @retval : None
301 */
SPI_DMACmd(SPI_TypeDef * SPIx,uint16_t SPI_DMAReq,FunctionalState NewState)302 void SPI_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_DMAReq, FunctionalState NewState)
303 {
304     /* Check the parameters */
305     assert_param(IS_SPI_ALL_PERIPH(SPIx));
306     assert_param(IS_FUNCTIONAL_STATE(NewState));
307     assert_param(IS_SPI_DMAREQ(SPI_DMAReq));
308     if (NewState != DISABLE)
309     {
310         /* Enable the selected SPI DMA requests */
311         SPIx->GCTL |= SPI_DMAReq;
312     }
313     else
314     {
315         /* Disable the selected SPI DMA requests */
316         SPIx->GCTL &= (uint32_t)~SPI_DMAReq;
317     }
318 }
319 
320 /**
321 * @brief  configure tn Fifo trigger level bit.
322 * @param SPIx: where x can be :
323 *   0, 1 in SPI mode
324 * @param SPI_FifoTriggerValue: specifies the Fifo trigger level
325 *   This parameter can be any combination of the following values:
326 * SPI_TXTLF : SPI TX FIFO Trigger value set
327 * SPI_RXTLF : SPI RX FIFO Trigger value set
328 * @param NewState: new state of the selected SPI DMA transfer
329 *   request.
330 *   This parameter can be: ENABLE or DISABLE.
331 * @retval : None
332 */
SPI_FifoTrigger(SPI_TypeDef * SPIx,uint16_t SPI_FifoTriggerValue,FunctionalState NewState)333 void SPI_FifoTrigger(SPI_TypeDef* SPIx, uint16_t SPI_FifoTriggerValue, FunctionalState NewState)
334 {
335     /* Check the parameters */
336     assert_param(IS_SPI_ALL_PERIPH(SPIx));
337     assert_param(IS_FUNCTIONAL_STATE(NewState));
338     assert_param(IS_SPI_FIFOTRIGGER(SPI_FifoTriggerValue));
339 
340     if (NewState != DISABLE)
341     {
342         /* Enable the selected SPI DMA requests */
343         SPIx->GCTL |= SPI_FifoTriggerValue;
344     }
345     else
346     {
347         /* Disable the selected SPI DMA requests */
348         SPIx->GCTL &= (uint32_t)~SPI_FifoTriggerValue;
349     }
350 }
351 
352 /**
353 * @brief  Transmits a Data through the SPIx peripheral.
354 * @param SPIx: where x can be :
355 *   0, 1 in SPI mode
356 * @param Data : Data to be transmitted..
357 * @retval : None
358 */
SPI_SendData(SPI_TypeDef * SPIx,uint32_t Data)359 void SPI_SendData(SPI_TypeDef* SPIx, uint32_t Data)
360 {
361     uint8_t temp;
362     /* Check the parameters */
363     assert_param(IS_SPI_ALL_PERIPH(SPIx));
364 
365     /* Write in the TXREG register the data to be sent */
366     temp = SPIx->EXTCTL;
367     SPIx->TXREG = Data;
368     if(temp > 0x8 || temp == 0) SPIx->TXREG  = Data >> 8;
369     if(temp > 0x10 || temp == 0) SPIx->TXREG = Data >> 16;
370     if(temp > 0x18 || temp == 0) SPIx->TXREG = Data >> 24;
371 }
372 
373 
374 /**
375 * @brief  Returns the most recent received data by the SPIx peripheral.
376 * @param SPIx: where x can be :
377 *   0, 1 in SPI mode
378 * @retval : The value of the received data.
379 */
SPI_ReceiveData(SPI_TypeDef * SPIx)380 uint32_t SPI_ReceiveData(SPI_TypeDef* SPIx)
381 {
382 
383     uint32_t temp = 0;
384     /* Check the parameters */
385     assert_param(IS_SPI_ALL_PERIPH(SPIx));
386     temp = temp;
387     temp |= (uint32_t)SPIx->RXREG;
388     if(SPIx->EXTCTL > 8 || SPIx->EXTCTL == 0)    temp |= (uint32_t) (SPIx->RXREG) << 8;
389     if(SPIx->EXTCTL > 16 || SPIx->EXTCTL == 0)  	temp |= (uint32_t)( SPIx->RXREG) << 16;
390     if(SPIx->EXTCTL > 24 || SPIx->EXTCTL == 0)	temp |= (uint32_t)( SPIx->RXREG) << 24;
391 
392     return temp;
393 }
394 
395 /**
396 * @brief Slave chip csn single by selected
397 * @param SPIx: where x can be 0, 1 to select the SPI peripheral.
398 * @param SPI_CSInternalSelected: specifies the SPI CS internal selected.
399 *   This parameter can be one of the following values:
400 * @arg SPI_CS_BIT0: cs bit 0 selected
401 * @arg SPI_CS_BIT1: cs bit 1 selected
402 * @arg SPI_CS_BIT2: cs bit 2 selected
403 * @arg SPI_CS_BIT3: cs bit 3 selected
404 * @arg SPI_CS_BIT4: cs bit 4 selected
405 * @arg SPI_CS_BIT5: cs bit 5 selected
406 * @arg SPI_CS_BIT6: cs bit 6 selected
407 * @arg SPI_CS_BIT7: cs bit 7 selected
408 * @param NewState: new state of the selected SPI CS pin
409 *   request.
410 *   This parameter can be: ENABLE or DISABLE.
411 * @retval : None
412 */
SPI_CSInternalSelected(SPI_TypeDef * SPIx,uint16_t SPI_CSInternalSelected,FunctionalState NewState)413 void SPI_CSInternalSelected(SPI_TypeDef* SPIx, uint16_t SPI_CSInternalSelected, FunctionalState NewState)
414 {
415     /* Check the parameters */
416     assert_param(IS_SPI_ALL_PERIPH(SPIx));
417     assert_param(IS_SPI_CS(SPI_CSInternalSelected));
418     assert_param(IS_FUNCTIONAL_STATE(NewState));
419 
420 
421     if (NewState != DISABLE)
422     {
423         /* selected cs pin according SCSR Value */
424         SPIx->SCSR &= SPI_CSInternalSelected;
425     }
426     else
427     {
428         /* release cs pin according SCSR Value*/
429         SPIx->SCSR |= ~SPI_CSInternalSelected;
430     }
431 }
432 
433 
434 /**
435 * @brief  Configures internally by software the NSS pin for the selected
436 *   SPI.
437 * @param SPIx: where x can be 1, 2 to select the SPI peripheral.
438 * @param SPI_NSSInternalSoft: specifies the SPI NSS internal state.
439 *   This parameter can be one of the following values:
440 * @arg SPI_NSSInternalSoft_Set: Set NSS pin internally
441 * @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally
442 * @retval : None
443 */
SPI_NSSInternalSoftwareConfig(SPI_TypeDef * SPIx,uint16_t SPI_NSSInternalSoft)444 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)
445 {
446     /* Check the parameters */
447     assert_param(IS_SPI_ALL_PERIPH(SPIx));
448     assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
449     if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)
450     {
451         /* Set NSS pin internally by software */
452         SPIx->GCTL |= SPI_NSSInternalSoft_Set;
453     }
454     else
455     {
456         /* Reset NSS pin internally by software */
457         SPIx->GCTL &= SPI_NSSInternalSoft_Reset;
458     }
459 }
460 
461 
462 
463 /**
464 * @brief  Configures the data size for the selected SPI.
465 * @param SPIx: where x can be 0, 1 to select the SPI peripheral.
466 * @param SPI_DataSize: specifies the SPI data size.
467 *   This parameter can be one of the following values:
468 * @arg SPI_DataSize_32b: Set data frame format to 32bit
469 * @arg SPI_DataSize_16b: Set data frame format to 16bit
470 * @arg SPI_DataSize_8b: Set data frame format to 8bit
471 * @retval : None
472 */
SPI_DataSizeConfig(SPI_TypeDef * SPIx,uint16_t SPI_DataSize)473 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)
474 {
475     /* Check the parameters */
476     assert_param(IS_SPI_ALL_PERIPH(SPIx));
477     assert_param(IS_SPI_DATASIZE(SPI_DataSize));
478     /* Clear data_sel bit */
479     SPIx->GCTL &= SPI_DataSize_Mask;
480     /* Set new data_sel bit value */
481     SPIx->GCTL |= SPI_DataSize;
482 }
483 
484 
485 
486 /**
487 * @brief  Selects the data transfer direction in bi-directional mode
488 *   for the specified SPI.
489 * @param SPIx: where x can be 0, 1 to select the SPI peripheral.
490 * @param SPI_Direction: specifies the data transfer direction in
491 *   bi-directional mode.
492 *   This parameter can be one of the following values:
493 * @arg SPI_Direction_Tx: Selects Tx transmission direction
494 * @arg SPI_Direction_Rx: Selects Rx receive direction
495 @arg SPI_Disable_Tx: Selects Rx receive direction
496 @arg SPI_Disable_Rx: Selects Rx receive direction
497 * @retval : None
498 */
SPI_BiDirectionalLineConfig(SPI_TypeDef * SPIx,uint16_t SPI_Direction)499 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)
500 {
501     /* Check the parameters */
502     assert_param(IS_SPI_ALL_PERIPH(SPIx));
503     assert_param(IS_SPI_DIRECTION(SPI_Direction));
504 
505     /* Set the Tx  only mode */
506     if(SPI_Direction == SPI_Direction_Tx)
507     {
508         SPIx->GCTL |= SPI_Direction_Tx;
509     }
510     /* Set the  Rx  only mode */
511     if(SPI_Direction == SPI_Direction_Rx)
512     {
513         SPIx->GCTL |= SPI_Direction_Rx;
514     }
515     /* Disable the  Tx  only mode */
516     if(SPI_Direction == SPI_Disable_Tx)
517     {
518         SPIx->GCTL &= SPI_Disable_Tx;
519     }
520     /* Disable the  Rx  only mode */
521     if(SPI_Direction == SPI_Disable_Rx)
522     {
523         SPIx->GCTL &= SPI_Disable_Rx;
524     }
525 }
526 
527 /**
528 * @brief  Checks whether the specified SPI flag is set or not.
529 * @param SPIx: where x can be :
530 *   0, 1 in SPI mode
531 * @param SPI_FLAG: specifies the SPI flag to check.
532 *   This parameter can be one of the following values:
533 * @arg SPI_FLAG_RXAVL: Rx buffer has bytes flag
534 * @arg SPI_FLAG_TXEPT:  Tx  buffer  and tx shifter empty flag
535 * @retval : The new state of SPI_FLAG (SET or RESET).
536 */
SPI_GetFlagStatus(SPI_TypeDef * SPIx,uint16_t SPI_FLAG)537 FlagStatus SPI_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_FLAG)
538 {
539     FlagStatus bitstatus = RESET;
540     /* Check the parameters */
541     assert_param(IS_SPI_ALL_PERIPH(SPIx));
542     assert_param(IS_SPI_GET_FLAG(SPI_FLAG));
543     if(SPIx->EXTCTL == 8)
544     {
545         /* Check the status of the specified SPI flag */
546         if ((SPIx->CSTAT & SPI_FLAG) != (uint16_t)RESET)
547         {
548             /* SPI_FLAG is set */
549             bitstatus = SET;
550         }
551         else
552         {
553             /* SPI_FLAG is reset */
554             bitstatus = RESET;
555         }
556         /* Return the SPI_FLAG status */
557         return  bitstatus;
558     }
559     else
560     {
561         uint8_t number;
562         if(SPIx->EXTCTL > 0 && SPIx->EXTCTL <= 8)
563             number = 1;
564         else if(SPIx->EXTCTL <= 16)
565             number = 2;
566         else if(SPIx->EXTCTL <= 24)
567             number = 3;
568         else if(SPIx->EXTCTL <= 31 || SPIx->EXTCTL == 0)
569             number = 4;
570         if(((SPIx->CSTAT & 0xf00) >> 8) >= number)
571         {
572             return SET;
573         }
574         else
575         {
576             return RESET;
577         }
578     }
579 }
580 
581 /**
582 * @brief  Checks whether the specified SPI interrupt has occurred or not.
583 * @param SPIx: where x can be :
584 *  0, 1 in SPI mode
585 * @param SPI_IT: specifies the SPI interrupt source to check.
586 *   This parameter can be one of the following values:
587 * @arg SPI_IT_TX: Tx buffer empty interrupt
588 * @arg SPI_IT_RX: Rx buffer  interrupt
589 * @arg SPI_IT_UNDERRUN: under Error interrupt in slave mode
590 * @arg SPI_IT_RXOVER: RX OVER Error interrupt
591 * @arg SPI_IT_RXMATCH: spectials rx data numbers  interrupt
592 * @arg SPI_IT_RXFULL: Rx buffer full interrupt
593 * @arg SPI_IT_TXEPT: Tx buffer  and tx shifter empty interrupt
594 * @retval : The new state of SPI_IT (SET or RESET).
595 */
SPI_GetITStatus(SPI_TypeDef * SPIx,uint8_t SPI_IT)596 ITStatus SPI_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_IT)
597 {
598     ITStatus bitstatus = RESET;
599 
600     /* Check the parameters */
601     assert_param(IS_SPI_ALL_PERIPH(SPIx));
602     assert_param(IS_SPI_GET_IT(SPI_IT));
603     /* Check the status of the specified SPI interrupt */
604     if ((SPIx->INTSTAT & SPI_IT) != (uint16_t)RESET)
605     {
606         /* SPI_IT is set */
607         bitstatus = SET;
608     }
609     else
610     {
611         /* SPI_IT is reset */
612         bitstatus = RESET;
613     }
614     /* Return the SPI_IT status */
615     return bitstatus;
616 }
617 
618 /**
619 * @brief  Clears the SPIx  Error  interrupt pending bit.
620 * @param SPIx: where x can be :
621 *   0, 1 in SPI mode
622 * @param SPI_IT: specifies the SPI interrupt pending bit to clear.
623 * @arg SPI_IT_TX: Tx buffer empty interrupt
624 * @arg SPI_IT_RX: Rx buffer  interrupt
625 * @arg SPI_IT_UNDERRUN: under Error interrupt in slave mode
626 * @arg SPI_IT_RXOVER: RX OVER Error interrupt
627 * @arg SPI_IT_RXMATCH: spectials rx data numbers  interrupt
628 * @arg SPI_IT_RXFULL: Rx buffer full interrupt
629 * @arg SPI_IT_TXEPT: Tx buffer  and tx shifter empty interrupt
630 *   This function clears only ERR intetrrupt pending bit.
631 * @retval : None
632 */
SPI_ClearITPendingBit(SPI_TypeDef * SPIx,uint8_t SPI_IT)633 void SPI_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_IT)
634 {
635 
636     /* Check the parameters */
637     assert_param(IS_SPI_ALL_PERIPH(SPIx));
638     assert_param(IS_SPI_CLEAR_IT(SPI_IT));
639 
640     /* Clear the selected SPI IT INTERRUPT */
641     SPIx->INTCLR |= (uint16_t)SPI_IT;
642 }
643 
644 
645 /**
646 * @brief  SPI Hole a count Received bytes in next receive process.
647 * @param SPIx: where x can be 0, 1 in SPI mode
648 * @param Number: specifies the SPI receive Number.
649 *   This parament can be 1-65535.
650 *   This function can use only in SPI master single receive mode.
651 * @retval : None
652 */
SPI_RxBytes(SPI_TypeDef * SPIx,uint16_t Number)653 void SPI_RxBytes(SPI_TypeDef* SPIx, uint16_t Number)
654 {
655     /* Check the parameters */
656     assert_param(IS_SPI_ALL_PERIPH(SPIx));
657     /*set the received bytes in next receive process */
658     SPIx->RXDNR = Number;
659 }
660 
661 /**
662 * @brief  slave mode tx data transmit phase adjust set.
663 * @param SPIx: where x can be 0, 1 in SPI mode
664 * @param AdjustValue: specifies the SPI receive Number.
665 *   This parament can be :
666 *   SPI_SlaveAdjust_FAST:  fast speed use
667 *   SPI_SlaveAdjust_LOW:   low speed use
668 *   This function can use only in SPI master single receive mode.
669 * @retval : None
670 */
SPI_SlaveAdjust(SPI_TypeDef * SPIx,uint16_t AdjustValue)671 void SPI_SlaveAdjust(SPI_TypeDef* SPIx, uint16_t AdjustValue)
672 {
673     /* Check the parameters */
674     assert_param(IS_SPI_ALL_PERIPH(SPIx));
675     assert_param(IS_SPI_SlaveAdjust(AdjustValue));
676     /*set the AdjustValue according to txedge bit of CCTL register*/
677     SPIx->CCTL |= AdjustValue;
678 }
679 
680 
681 /**
682 * @}
683 */
684 
685 /**
686 * @}
687 */
688 
689 /**
690 * @}
691 */
692 
693 /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/
694