STM32F4xx HAL Driver master
STM32CubeF4 HAL / LL Drivers API Reference
Loading...
Searching...
No Matches
stm32f4xx_hal_spi.c
Go to the documentation of this file.
1
200
201/* Includes ------------------------------------------------------------------*/
202#include "stm32f4xx_hal.h"
203
207
212#ifdef HAL_SPI_MODULE_ENABLED
213
214/* Private typedef -----------------------------------------------------------*/
215/* Private defines -----------------------------------------------------------*/
219#define SPI_DEFAULT_TIMEOUT 100U
220#define SPI_BSY_FLAG_WORKAROUND_TIMEOUT 1000U
224
225/* Private macros ------------------------------------------------------------*/
226/* Private variables ---------------------------------------------------------*/
227/* Private function prototypes -----------------------------------------------*/
231static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
232static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
233static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
234static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
235static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
236static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
237static void SPI_DMAError(DMA_HandleTypeDef *hdma);
238static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma);
239static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
240static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
241static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
242 uint32_t Timeout, uint32_t Tickstart);
243static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
244static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
245static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
246static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
247static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
248static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
249static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
250static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
251#if (USE_SPI_CRC != 0U)
252static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
253static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
254static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
255static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
256#endif /* USE_SPI_CRC */
257static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi);
258static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi);
259static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
260static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
261static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
262static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
263static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
267
268/* Exported functions --------------------------------------------------------*/
272
305
314{
315 /* Check the SPI handle allocation */
316 if (hspi == NULL)
317 {
318 return HAL_ERROR;
319 }
320
321 /* Check the parameters */
322 assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
330 if (hspi->Init.TIMode == SPI_TIMODE_DISABLE)
331 {
334
335 if (hspi->Init.Mode == SPI_MODE_MASTER)
336 {
338 }
339 else
340 {
341 /* Baudrate prescaler not use in Motoraola Slave mode. force to default value */
343 }
344 }
345 else
346 {
348
349 /* Force polarity and phase to TI protocaol requirements */
352 }
353#if (USE_SPI_CRC != 0U)
356 {
358 }
359#else
361#endif /* USE_SPI_CRC */
362
363 if (hspi->State == HAL_SPI_STATE_RESET)
364 {
365 /* Allocate lock resource and initialize it */
366 hspi->Lock = HAL_UNLOCKED;
367
368#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
369 /* Init the SPI Callback settings */
370 hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
371 hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
372 hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
373 hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
374 hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
375 hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
376 hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
377 hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
378
379 if (hspi->MspInitCallback == NULL)
380 {
381 hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
382 }
383
384 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
385 hspi->MspInitCallback(hspi);
386#else
387 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
388 HAL_SPI_MspInit(hspi);
389#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
390 }
391
393
394 /* Disable the selected SPI peripheral */
395 __HAL_SPI_DISABLE(hspi);
396
397 /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
398 /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
399 Communication speed, First bit and CRC calculation state */
400 WRITE_REG(hspi->Instance->CR1, ((hspi->Init.Mode & (SPI_CR1_MSTR | SPI_CR1_SSI)) |
401 (hspi->Init.Direction & (SPI_CR1_RXONLY | SPI_CR1_BIDIMODE)) |
402 (hspi->Init.DataSize & SPI_CR1_DFF) |
403 (hspi->Init.CLKPolarity & SPI_CR1_CPOL) |
404 (hspi->Init.CLKPhase & SPI_CR1_CPHA) |
405 (hspi->Init.NSS & SPI_CR1_SSM) |
406 (hspi->Init.BaudRatePrescaler & SPI_CR1_BR_Msk) |
407 (hspi->Init.FirstBit & SPI_CR1_LSBFIRST) |
408 (hspi->Init.CRCCalculation & SPI_CR1_CRCEN)));
409
410 /* Configure : NSS management, TI Mode */
411 WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) | (hspi->Init.TIMode & SPI_CR2_FRF)));
412
413#if (USE_SPI_CRC != 0U)
414 /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
415 /* Configure : CRC Polynomial */
417 {
418 WRITE_REG(hspi->Instance->CRCPR, (hspi->Init.CRCPolynomial & SPI_CRCPR_CRCPOLY_Msk));
419 }
420#endif /* USE_SPI_CRC */
421
422#if defined(SPI_I2SCFGR_I2SMOD)
423 /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
424 CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
425#endif /* SPI_I2SCFGR_I2SMOD */
426
429
430 return HAL_OK;
431}
432
440{
441 /* Check the SPI handle allocation */
442 if (hspi == NULL)
443 {
444 return HAL_ERROR;
445 }
446
447 /* Check SPI Instance parameter */
448 assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
449
451
452 /* Disable the SPI Peripheral Clock */
453 __HAL_SPI_DISABLE(hspi);
454
455#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
456 if (hspi->MspDeInitCallback == NULL)
457 {
458 hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
459 }
460
461 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
462 hspi->MspDeInitCallback(hspi);
463#else
464 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
465 HAL_SPI_MspDeInit(hspi);
466#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
467
470
471 /* Release Lock */
472 __HAL_UNLOCK(hspi);
473
474 return HAL_OK;
475}
476
483__weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
484{
485 /* Prevent unused argument(s) compilation warning */
486 UNUSED(hspi);
487
488 /* NOTE : This function should not be modified, when the callback is needed,
489 the HAL_SPI_MspInit should be implemented in the user file
490 */
491}
492
499__weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
500{
501 /* Prevent unused argument(s) compilation warning */
502 UNUSED(hspi);
503
504 /* NOTE : This function should not be modified, when the callback is needed,
505 the HAL_SPI_MspDeInit should be implemented in the user file
506 */
507}
508
509#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
519HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID,
520 pSPI_CallbackTypeDef pCallback)
521{
522 HAL_StatusTypeDef status = HAL_OK;
523
524 if (pCallback == NULL)
525 {
526 /* Update the error code */
527 hspi->ErrorCode |= HAL_SPI_ERROR_INVALID_CALLBACK;
528
529 return HAL_ERROR;
530 }
531 /* Process locked */
532 __HAL_LOCK(hspi);
533
534 if (HAL_SPI_STATE_READY == hspi->State)
535 {
536 switch (CallbackID)
537 {
538 case HAL_SPI_TX_COMPLETE_CB_ID :
539 hspi->TxCpltCallback = pCallback;
540 break;
541
542 case HAL_SPI_RX_COMPLETE_CB_ID :
543 hspi->RxCpltCallback = pCallback;
544 break;
545
546 case HAL_SPI_TX_RX_COMPLETE_CB_ID :
547 hspi->TxRxCpltCallback = pCallback;
548 break;
549
550 case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
551 hspi->TxHalfCpltCallback = pCallback;
552 break;
553
554 case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
555 hspi->RxHalfCpltCallback = pCallback;
556 break;
557
558 case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
559 hspi->TxRxHalfCpltCallback = pCallback;
560 break;
561
562 case HAL_SPI_ERROR_CB_ID :
563 hspi->ErrorCallback = pCallback;
564 break;
565
566 case HAL_SPI_ABORT_CB_ID :
567 hspi->AbortCpltCallback = pCallback;
568 break;
569
570 case HAL_SPI_MSPINIT_CB_ID :
571 hspi->MspInitCallback = pCallback;
572 break;
573
574 case HAL_SPI_MSPDEINIT_CB_ID :
575 hspi->MspDeInitCallback = pCallback;
576 break;
577
578 default :
579 /* Update the error code */
580 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
581
582 /* Return error status */
583 status = HAL_ERROR;
584 break;
585 }
586 }
587 else if (HAL_SPI_STATE_RESET == hspi->State)
588 {
589 switch (CallbackID)
590 {
591 case HAL_SPI_MSPINIT_CB_ID :
592 hspi->MspInitCallback = pCallback;
593 break;
594
595 case HAL_SPI_MSPDEINIT_CB_ID :
596 hspi->MspDeInitCallback = pCallback;
597 break;
598
599 default :
600 /* Update the error code */
601 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
602
603 /* Return error status */
604 status = HAL_ERROR;
605 break;
606 }
607 }
608 else
609 {
610 /* Update the error code */
611 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
612
613 /* Return error status */
614 status = HAL_ERROR;
615 }
616
617 /* Release Lock */
618 __HAL_UNLOCK(hspi);
619 return status;
620}
621
630HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID)
631{
632 HAL_StatusTypeDef status = HAL_OK;
633
634 /* Process locked */
635 __HAL_LOCK(hspi);
636
637 if (HAL_SPI_STATE_READY == hspi->State)
638 {
639 switch (CallbackID)
640 {
641 case HAL_SPI_TX_COMPLETE_CB_ID :
642 hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
643 break;
644
645 case HAL_SPI_RX_COMPLETE_CB_ID :
646 hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
647 break;
648
649 case HAL_SPI_TX_RX_COMPLETE_CB_ID :
650 hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
651 break;
652
653 case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
654 hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
655 break;
656
657 case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
658 hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
659 break;
660
661 case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
662 hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
663 break;
664
665 case HAL_SPI_ERROR_CB_ID :
666 hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
667 break;
668
669 case HAL_SPI_ABORT_CB_ID :
670 hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
671 break;
672
673 case HAL_SPI_MSPINIT_CB_ID :
674 hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
675 break;
676
677 case HAL_SPI_MSPDEINIT_CB_ID :
678 hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
679 break;
680
681 default :
682 /* Update the error code */
683 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
684
685 /* Return error status */
686 status = HAL_ERROR;
687 break;
688 }
689 }
690 else if (HAL_SPI_STATE_RESET == hspi->State)
691 {
692 switch (CallbackID)
693 {
694 case HAL_SPI_MSPINIT_CB_ID :
695 hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
696 break;
697
698 case HAL_SPI_MSPDEINIT_CB_ID :
699 hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
700 break;
701
702 default :
703 /* Update the error code */
704 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
705
706 /* Return error status */
707 status = HAL_ERROR;
708 break;
709 }
710 }
711 else
712 {
713 /* Update the error code */
714 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
715
716 /* Return error status */
717 status = HAL_ERROR;
718 }
719
720 /* Release Lock */
721 __HAL_UNLOCK(hspi);
722 return status;
723}
724#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
728
761
771HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
772{
773 uint32_t tickstart;
774 uint16_t initial_TxXferCount;
775
776 /* Check Direction parameter */
778
779 /* Init tickstart for timeout management*/
780 tickstart = HAL_GetTick();
781 initial_TxXferCount = Size;
782
783 if (hspi->State != HAL_SPI_STATE_READY)
784 {
785 return HAL_BUSY;
786 }
787
788 if ((pData == NULL) || (Size == 0U))
789 {
790 return HAL_ERROR;
791 }
792
793 /* Process Locked */
794 __HAL_LOCK(hspi);
795
796 /* Set the transaction information */
799 hspi->pTxBuffPtr = (const uint8_t *)pData;
800 hspi->TxXferSize = Size;
801 hspi->TxXferCount = Size;
802
803 /*Init field not used in handle to zero */
804 hspi->pRxBuffPtr = (uint8_t *)NULL;
805 hspi->RxXferSize = 0U;
806 hspi->RxXferCount = 0U;
807 hspi->TxISR = NULL;
808 hspi->RxISR = NULL;
809
810 /* Configure communication direction : 1Line */
812 {
813 /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
814 __HAL_SPI_DISABLE(hspi);
815 SPI_1LINE_TX(hspi);
816 }
817
818#if (USE_SPI_CRC != 0U)
819 /* Reset CRC Calculation */
821 {
822 SPI_RESET_CRC(hspi);
823 }
824#endif /* USE_SPI_CRC */
825
826 /* Check if the SPI is already enabled */
827 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
828 {
829 /* Enable SPI peripheral */
830 __HAL_SPI_ENABLE(hspi);
831 }
832
833 /* Transmit data in 16 Bit mode */
834 if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
835 {
836 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
837 {
838 hspi->Instance->DR = *((const uint16_t *)hspi->pTxBuffPtr);
839 hspi->pTxBuffPtr += sizeof(uint16_t);
840 hspi->TxXferCount--;
841 }
842 /* Transmit data in 16 Bit mode */
843 while (hspi->TxXferCount > 0U)
844 {
845 /* Wait until TXE flag is set to send data */
847 {
848 hspi->Instance->DR = *((const uint16_t *)hspi->pTxBuffPtr);
849 hspi->pTxBuffPtr += sizeof(uint16_t);
850 hspi->TxXferCount--;
851 }
852 else
853 {
854 /* Timeout management */
855 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
856 {
858 __HAL_UNLOCK(hspi);
859 return HAL_TIMEOUT;
860 }
861 }
862 }
863 }
864 /* Transmit data in 8 Bit mode */
865 else
866 {
867 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
868 {
869 *((__IO uint8_t *)&hspi->Instance->DR) = *((const uint8_t *)hspi->pTxBuffPtr);
870 hspi->pTxBuffPtr += sizeof(uint8_t);
871 hspi->TxXferCount--;
872 }
873 while (hspi->TxXferCount > 0U)
874 {
875 /* Wait until TXE flag is set to send data */
877 {
878 *((__IO uint8_t *)&hspi->Instance->DR) = *((const uint8_t *)hspi->pTxBuffPtr);
879 hspi->pTxBuffPtr += sizeof(uint8_t);
880 hspi->TxXferCount--;
881 }
882 else
883 {
884 /* Timeout management */
885 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
886 {
888 __HAL_UNLOCK(hspi);
889 return HAL_TIMEOUT;
890 }
891 }
892 }
893 }
894#if (USE_SPI_CRC != 0U)
895 /* Enable CRC Transmission */
897 {
898 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
899 }
900#endif /* USE_SPI_CRC */
901
902 /* Check the end of the transaction */
903 if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
904 {
906 }
907
908 /* Clear overrun flag in 2 Lines communication mode because received is not read */
910 {
912 }
913
915 /* Process Unlocked */
916 __HAL_UNLOCK(hspi);
917
918 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
919 {
920 return HAL_ERROR;
921 }
922 else
923 {
924 return HAL_OK;
925 }
926}
927
940HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
941{
942#if (USE_SPI_CRC != 0U)
943 __IO uint32_t tmpreg = 0U;
944#endif /* USE_SPI_CRC */
945 uint32_t tickstart;
946
947 if (hspi->State != HAL_SPI_STATE_READY)
948 {
949 return HAL_BUSY;
950 }
951
952 if ((pData == NULL) || (Size == 0U))
953 {
954 return HAL_ERROR;
955 }
956
957 if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
958 {
960 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
961 return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
962 }
963
964 /* Init tickstart for timeout management*/
965 tickstart = HAL_GetTick();
966
967 /* Process Locked */
968 __HAL_LOCK(hspi);
969
970 /* Set the transaction information */
973 hspi->pRxBuffPtr = (uint8_t *)pData;
974 hspi->RxXferSize = Size;
975 hspi->RxXferCount = Size;
976
977 /*Init field not used in handle to zero */
978 hspi->pTxBuffPtr = (uint8_t *)NULL;
979 hspi->TxXferSize = 0U;
980 hspi->TxXferCount = 0U;
981 hspi->RxISR = NULL;
982 hspi->TxISR = NULL;
983
984#if (USE_SPI_CRC != 0U)
985 /* Reset CRC Calculation */
987 {
988 SPI_RESET_CRC(hspi);
989 /* this is done to handle the CRCNEXT before the latest data */
990 hspi->RxXferCount--;
991 }
992#endif /* USE_SPI_CRC */
993
994 /* Configure communication direction: 1Line */
996 {
997 /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
998 __HAL_SPI_DISABLE(hspi);
999 SPI_1LINE_RX(hspi);
1000 }
1001
1002 /* Check if the SPI is already enabled */
1003 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1004 {
1005 /* Enable SPI peripheral */
1006 __HAL_SPI_ENABLE(hspi);
1007 }
1008
1009 /* Receive data in 8 Bit mode */
1010 if (hspi->Init.DataSize == SPI_DATASIZE_8BIT)
1011 {
1012 /* Transfer loop */
1013 while (hspi->RxXferCount > 0U)
1014 {
1015 /* Check the RXNE flag */
1017 {
1018 /* read the received data */
1019 (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
1020 hspi->pRxBuffPtr += sizeof(uint8_t);
1021 hspi->RxXferCount--;
1022 }
1023 else
1024 {
1025 /* Timeout management */
1026 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1027 {
1028 hspi->State = HAL_SPI_STATE_READY;
1029 __HAL_UNLOCK(hspi);
1030 return HAL_TIMEOUT;
1031 }
1032 }
1033 }
1034 }
1035 else
1036 {
1037 /* Transfer loop */
1038 while (hspi->RxXferCount > 0U)
1039 {
1040 /* Check the RXNE flag */
1042 {
1043 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1044 hspi->pRxBuffPtr += sizeof(uint16_t);
1045 hspi->RxXferCount--;
1046 }
1047 else
1048 {
1049 /* Timeout management */
1050 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1051 {
1052 hspi->State = HAL_SPI_STATE_READY;
1053 __HAL_UNLOCK(hspi);
1054 return HAL_TIMEOUT;
1055 }
1056 }
1057 }
1058 }
1059
1060#if (USE_SPI_CRC != 0U)
1061 /* Handle the CRC Transmission */
1063 {
1064 /* freeze the CRC before the latest data */
1065 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1066
1067 /* Read the latest data */
1068 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1069 {
1070 /* the latest data has not been received */
1071 __HAL_UNLOCK(hspi);
1072 return HAL_TIMEOUT;
1073 }
1074
1075 /* Receive last data in 16 Bit mode */
1076 if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
1077 {
1078 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1079 }
1080 /* Receive last data in 8 Bit mode */
1081 else
1082 {
1083 (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
1084 }
1085
1086 /* Wait the CRC data */
1087 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1088 {
1089 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1090 hspi->State = HAL_SPI_STATE_READY;
1091 __HAL_UNLOCK(hspi);
1092 return HAL_TIMEOUT;
1093 }
1094
1095 /* Read CRC to Flush DR and RXNE flag */
1096 tmpreg = READ_REG(hspi->Instance->DR);
1097 /* To avoid GCC warning */
1098 UNUSED(tmpreg);
1099 }
1100#endif /* USE_SPI_CRC */
1101
1102 /* Check the end of the transaction */
1103 if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK)
1104 {
1106 }
1107
1108#if (USE_SPI_CRC != 0U)
1109 /* Check if CRC error occurred */
1111 {
1112 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1114 }
1115#endif /* USE_SPI_CRC */
1116
1117 hspi->State = HAL_SPI_STATE_READY;
1118 /* Unlock the process */
1119 __HAL_UNLOCK(hspi);
1120 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1121 {
1122 return HAL_ERROR;
1123 }
1124 else
1125 {
1126 return HAL_OK;
1127 }
1128}
1129
1140HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,
1141 uint16_t Size, uint32_t Timeout)
1142{
1143 uint16_t initial_TxXferCount;
1144 uint32_t tmp_mode;
1145 HAL_SPI_StateTypeDef tmp_state;
1146 uint32_t tickstart;
1147#if (USE_SPI_CRC != 0U)
1148 __IO uint32_t tmpreg = 0U;
1149#endif /* USE_SPI_CRC */
1150
1151 /* Variable used to alternate Rx and Tx during transfer */
1152 uint32_t txallowed = 1U;
1153
1154 /* Check Direction parameter */
1156
1157 /* Init tickstart for timeout management*/
1158 tickstart = HAL_GetTick();
1159
1160 /* Init temporary variables */
1161 tmp_state = hspi->State;
1162 tmp_mode = hspi->Init.Mode;
1163 initial_TxXferCount = Size;
1164
1165 if (!((tmp_state == HAL_SPI_STATE_READY) || \
1166 ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) &&
1167 (tmp_state == HAL_SPI_STATE_BUSY_RX))))
1168 {
1169 return HAL_BUSY;
1170 }
1171
1172 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1173 {
1174 return HAL_ERROR;
1175 }
1176
1177 /* Process Locked */
1178 __HAL_LOCK(hspi);
1179
1180 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1181 if (hspi->State != HAL_SPI_STATE_BUSY_RX)
1182 {
1184 }
1185
1186 /* Set the transaction information */
1188 hspi->pRxBuffPtr = (uint8_t *)pRxData;
1189 hspi->RxXferCount = Size;
1190 hspi->RxXferSize = Size;
1191 hspi->pTxBuffPtr = (const uint8_t *)pTxData;
1192 hspi->TxXferCount = Size;
1193 hspi->TxXferSize = Size;
1194
1195 /*Init field not used in handle to zero */
1196 hspi->RxISR = NULL;
1197 hspi->TxISR = NULL;
1198
1199#if (USE_SPI_CRC != 0U)
1200 /* Reset CRC Calculation */
1202 {
1203 SPI_RESET_CRC(hspi);
1204 }
1205#endif /* USE_SPI_CRC */
1206
1207 /* Check if the SPI is already enabled */
1208 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1209 {
1210 /* Enable SPI peripheral */
1211 __HAL_SPI_ENABLE(hspi);
1212 }
1213
1214 /* Transmit and Receive data in 16 Bit mode */
1215 if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
1216 {
1217 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
1218 {
1219 hspi->Instance->DR = *((const uint16_t *)hspi->pTxBuffPtr);
1220 hspi->pTxBuffPtr += sizeof(uint16_t);
1221 hspi->TxXferCount--;
1222
1223#if (USE_SPI_CRC != 0U)
1224 /* Enable CRC Transmission */
1225 if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
1226 {
1227 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1228 }
1229#endif /* USE_SPI_CRC */
1230
1231 }
1232 while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
1233 {
1234 /* Check TXE flag */
1235 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
1236 {
1237 hspi->Instance->DR = *((const uint16_t *)hspi->pTxBuffPtr);
1238 hspi->pTxBuffPtr += sizeof(uint16_t);
1239 hspi->TxXferCount--;
1240 /* Next Data is a reception (Rx). Tx not allowed */
1241 txallowed = 0U;
1242
1243#if (USE_SPI_CRC != 0U)
1244 /* Enable CRC Transmission */
1245 if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
1246 {
1247 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1248 }
1249#endif /* USE_SPI_CRC */
1250 }
1251
1252 /* Check RXNE flag */
1253 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
1254 {
1255 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1256 hspi->pRxBuffPtr += sizeof(uint16_t);
1257 hspi->RxXferCount--;
1258 /* Next Data is a Transmission (Tx). Tx is allowed */
1259 txallowed = 1U;
1260 }
1261 if (((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY))
1262 {
1263 hspi->State = HAL_SPI_STATE_READY;
1264 __HAL_UNLOCK(hspi);
1265 return HAL_TIMEOUT;
1266 }
1267 }
1268 }
1269 /* Transmit and Receive data in 8 Bit mode */
1270 else
1271 {
1272 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
1273 {
1274 *((__IO uint8_t *)&hspi->Instance->DR) = *((const uint8_t *)hspi->pTxBuffPtr);
1275 hspi->pTxBuffPtr += sizeof(uint8_t);
1276 hspi->TxXferCount--;
1277
1278#if (USE_SPI_CRC != 0U)
1279 /* Enable CRC Transmission */
1280 if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
1281 {
1282 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1283 }
1284#endif /* USE_SPI_CRC */
1285 }
1286 while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
1287 {
1288 /* Check TXE flag */
1289 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
1290 {
1291 *(__IO uint8_t *)&hspi->Instance->DR = *((const uint8_t *)hspi->pTxBuffPtr);
1292 hspi->pTxBuffPtr++;
1293 hspi->TxXferCount--;
1294 /* Next Data is a reception (Rx). Tx not allowed */
1295 txallowed = 0U;
1296
1297#if (USE_SPI_CRC != 0U)
1298 /* Enable CRC Transmission */
1299 if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
1300 {
1301 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1302 }
1303#endif /* USE_SPI_CRC */
1304 }
1305
1306 /* Wait until RXNE flag is reset */
1307 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
1308 {
1309 (*(uint8_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
1310 hspi->pRxBuffPtr++;
1311 hspi->RxXferCount--;
1312 /* Next Data is a Transmission (Tx). Tx is allowed */
1313 txallowed = 1U;
1314 }
1315 if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U))
1316 {
1317 hspi->State = HAL_SPI_STATE_READY;
1318 __HAL_UNLOCK(hspi);
1319 return HAL_TIMEOUT;
1320 }
1321 }
1322 }
1323
1324#if (USE_SPI_CRC != 0U)
1325 /* Read CRC from DR to close CRC calculation process */
1327 {
1328 /* Wait until TXE flag */
1329 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1330 {
1331 /* Error on the CRC reception */
1332 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1333 hspi->State = HAL_SPI_STATE_READY;
1334 __HAL_UNLOCK(hspi);
1335 return HAL_TIMEOUT;
1336 }
1337 /* Read CRC */
1338 tmpreg = READ_REG(hspi->Instance->DR);
1339 /* To avoid GCC warning */
1340 UNUSED(tmpreg);
1341 }
1342
1343 /* Check if CRC error occurred */
1345 {
1346 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1347 /* Clear CRC Flag */
1349 __HAL_UNLOCK(hspi);
1350 return HAL_ERROR;
1351 }
1352#endif /* USE_SPI_CRC */
1353
1354 /* Check the end of the transaction */
1355 if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
1356 {
1358 __HAL_UNLOCK(hspi);
1359 return HAL_ERROR;
1360 }
1361
1362 /* Clear overrun flag in 2 Lines communication mode because received is not read */
1363 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
1364 {
1366 }
1367
1368
1369 hspi->State = HAL_SPI_STATE_READY;
1370 /* Unlock the process */
1371 __HAL_UNLOCK(hspi);
1372
1373 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1374 {
1375 return HAL_ERROR;
1376 }
1377 else
1378 {
1379 return HAL_OK;
1380 }
1381}
1382
1391HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
1392{
1393
1394 /* Check Direction parameter */
1396
1397
1398 if ((pData == NULL) || (Size == 0U))
1399 {
1400 return HAL_ERROR;
1401 }
1402
1403 if (hspi->State != HAL_SPI_STATE_READY)
1404 {
1405 return HAL_BUSY;
1406 }
1407
1408 /* Process Locked */
1409 __HAL_LOCK(hspi);
1410
1411 /* Set the transaction information */
1414 hspi->pTxBuffPtr = (const uint8_t *)pData;
1415 hspi->TxXferSize = Size;
1416 hspi->TxXferCount = Size;
1417
1418 /* Init field not used in handle to zero */
1419 hspi->pRxBuffPtr = (uint8_t *)NULL;
1420 hspi->RxXferSize = 0U;
1421 hspi->RxXferCount = 0U;
1422 hspi->RxISR = NULL;
1423
1424 /* Set the function for IT treatment */
1425 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1426 {
1427 hspi->TxISR = SPI_TxISR_16BIT;
1428 }
1429 else
1430 {
1431 hspi->TxISR = SPI_TxISR_8BIT;
1432 }
1433
1434 /* Configure communication direction : 1Line */
1435 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1436 {
1437 /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
1438 __HAL_SPI_DISABLE(hspi);
1439 SPI_1LINE_TX(hspi);
1440 }
1441
1442#if (USE_SPI_CRC != 0U)
1443 /* Reset CRC Calculation */
1445 {
1446 SPI_RESET_CRC(hspi);
1447 }
1448#endif /* USE_SPI_CRC */
1449
1450 /* Check if the SPI is already enabled */
1451 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1452 {
1453 /* Enable SPI peripheral */
1454 __HAL_SPI_ENABLE(hspi);
1455 }
1456
1457 /* Process Unlocked */
1458 __HAL_UNLOCK(hspi);
1459 /* Enable TXE and ERR interrupt */
1461
1462 return HAL_OK;
1463}
1464
1473HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1474{
1475
1476 if (hspi->State != HAL_SPI_STATE_READY)
1477 {
1478 return HAL_BUSY;
1479 }
1480
1481 if ((pData == NULL) || (Size == 0U))
1482 {
1483 return HAL_ERROR;
1484 }
1485
1486 if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
1487 {
1489 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1490 return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
1491 }
1492
1493
1494 /* Process Locked */
1495 __HAL_LOCK(hspi);
1496
1497 /* Set the transaction information */
1500 hspi->pRxBuffPtr = (uint8_t *)pData;
1501 hspi->RxXferSize = Size;
1502 hspi->RxXferCount = Size;
1503
1504 /* Init field not used in handle to zero */
1505 hspi->pTxBuffPtr = (uint8_t *)NULL;
1506 hspi->TxXferSize = 0U;
1507 hspi->TxXferCount = 0U;
1508 hspi->TxISR = NULL;
1509
1510 /* Set the function for IT treatment */
1511 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1512 {
1513 hspi->RxISR = SPI_RxISR_16BIT;
1514 }
1515 else
1516 {
1517 hspi->RxISR = SPI_RxISR_8BIT;
1518 }
1519
1520 /* Configure communication direction : 1Line */
1521 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1522 {
1523 /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
1524 __HAL_SPI_DISABLE(hspi);
1525 SPI_1LINE_RX(hspi);
1526 }
1527
1528#if (USE_SPI_CRC != 0U)
1529 /* Reset CRC Calculation */
1531 {
1532 SPI_RESET_CRC(hspi);
1533 }
1534#endif /* USE_SPI_CRC */
1535
1536 /* Note : The SPI must be enabled after unlocking current process
1537 to avoid the risk of SPI interrupt handle execution before current
1538 process unlock */
1539
1540 /* Check if the SPI is already enabled */
1541 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1542 {
1543 /* Enable SPI peripheral */
1544 __HAL_SPI_ENABLE(hspi);
1545 }
1546
1547 /* Process Unlocked */
1548 __HAL_UNLOCK(hspi);
1549 /* Enable RXNE and ERR interrupt */
1551
1552 return HAL_OK;
1553}
1554
1564HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,
1565 uint16_t Size)
1566{
1567 uint32_t tmp_mode;
1568 HAL_SPI_StateTypeDef tmp_state;
1569
1570 /* Check Direction parameter */
1572
1573 /* Init temporary variables */
1574 tmp_state = hspi->State;
1575 tmp_mode = hspi->Init.Mode;
1576
1577 if (!((tmp_state == HAL_SPI_STATE_READY) || \
1578 ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) &&
1579 (tmp_state == HAL_SPI_STATE_BUSY_RX))))
1580 {
1581 return HAL_BUSY;
1582 }
1583
1584 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1585 {
1586 return HAL_ERROR;
1587 }
1588
1589 /* Process locked */
1590 __HAL_LOCK(hspi);
1591
1592 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1593 if (hspi->State != HAL_SPI_STATE_BUSY_RX)
1594 {
1596 }
1597
1598 /* Set the transaction information */
1600 hspi->pTxBuffPtr = (const uint8_t *)pTxData;
1601 hspi->TxXferSize = Size;
1602 hspi->TxXferCount = Size;
1603 hspi->pRxBuffPtr = (uint8_t *)pRxData;
1604 hspi->RxXferSize = Size;
1605 hspi->RxXferCount = Size;
1606
1607 /* Set the function for IT treatment */
1608 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1609 {
1610 hspi->RxISR = SPI_2linesRxISR_16BIT;
1611 hspi->TxISR = SPI_2linesTxISR_16BIT;
1612 }
1613 else
1614 {
1615 hspi->RxISR = SPI_2linesRxISR_8BIT;
1616 hspi->TxISR = SPI_2linesTxISR_8BIT;
1617 }
1618
1619#if (USE_SPI_CRC != 0U)
1620 /* Reset CRC Calculation */
1622 {
1623 SPI_RESET_CRC(hspi);
1624 }
1625#endif /* USE_SPI_CRC */
1626
1627
1628 /* Check if the SPI is already enabled */
1629 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1630 {
1631 /* Enable SPI peripheral */
1632 __HAL_SPI_ENABLE(hspi);
1633 }
1634
1635 /* Process Unlocked */
1636 __HAL_UNLOCK(hspi);
1637 /* Enable TXE, RXNE and ERR interrupt */
1639
1640 return HAL_OK;
1641}
1642
1651HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
1652{
1653
1654 /* Check tx dma handle */
1656
1657 /* Check Direction parameter */
1659
1660 if (hspi->State != HAL_SPI_STATE_READY)
1661 {
1662 return HAL_BUSY;
1663 }
1664
1665 if ((pData == NULL) || (Size == 0U))
1666 {
1667 return HAL_ERROR;
1668 }
1669
1670 /* Process Locked */
1671 __HAL_LOCK(hspi);
1672
1673 /* Set the transaction information */
1676 hspi->pTxBuffPtr = (const uint8_t *)pData;
1677 hspi->TxXferSize = Size;
1678 hspi->TxXferCount = Size;
1679
1680 /* Init field not used in handle to zero */
1681 hspi->pRxBuffPtr = (uint8_t *)NULL;
1682 hspi->TxISR = NULL;
1683 hspi->RxISR = NULL;
1684 hspi->RxXferSize = 0U;
1685 hspi->RxXferCount = 0U;
1686
1687 /* Configure communication direction : 1Line */
1688 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1689 {
1690 /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
1691 __HAL_SPI_DISABLE(hspi);
1692 SPI_1LINE_TX(hspi);
1693 }
1694
1695#if (USE_SPI_CRC != 0U)
1696 /* Reset CRC Calculation */
1698 {
1699 SPI_RESET_CRC(hspi);
1700 }
1701#endif /* USE_SPI_CRC */
1702
1703 /* Set the SPI TxDMA Half transfer complete callback */
1704 hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
1705
1706 /* Set the SPI TxDMA transfer complete callback */
1707 hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
1708
1709 /* Set the DMA error callback */
1710 hspi->hdmatx->XferErrorCallback = SPI_DMAError;
1711
1712 /* Set the DMA AbortCpltCallback */
1713 hspi->hdmatx->XferAbortCallback = NULL;
1714
1715 /* Enable the Tx DMA Stream/Channel */
1716 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR,
1717 hspi->TxXferCount))
1718 {
1719 /* Update SPI error code */
1720 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
1721 /* Process Unlocked */
1722 __HAL_UNLOCK(hspi);
1723 return HAL_ERROR;
1724 }
1725
1726 /* Check if the SPI is already enabled */
1727 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1728 {
1729 /* Enable SPI peripheral */
1730 __HAL_SPI_ENABLE(hspi);
1731 }
1732
1733 /* Process Unlocked */
1734 __HAL_UNLOCK(hspi);
1735
1736 /* Enable the SPI Error Interrupt Bit */
1738
1739 /* Enable Tx DMA Request */
1740 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1741
1742 return HAL_OK;
1743}
1744
1755HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1756{
1757 /* Check rx dma handle */
1759
1760 if (hspi->State != HAL_SPI_STATE_READY)
1761 {
1762 return HAL_BUSY;
1763 }
1764
1765 if ((pData == NULL) || (Size == 0U))
1766 {
1767 return HAL_ERROR;
1768 }
1769
1770 if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
1771 {
1773
1774 /* Check tx dma handle */
1776
1777 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1778 return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
1779 }
1780
1781 /* Process Locked */
1782 __HAL_LOCK(hspi);
1783
1784 /* Set the transaction information */
1787 hspi->pRxBuffPtr = (uint8_t *)pData;
1788 hspi->RxXferSize = Size;
1789 hspi->RxXferCount = Size;
1790
1791 /*Init field not used in handle to zero */
1792 hspi->RxISR = NULL;
1793 hspi->TxISR = NULL;
1794 hspi->TxXferSize = 0U;
1795 hspi->TxXferCount = 0U;
1796
1797 /* Configure communication direction : 1Line */
1798 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1799 {
1800 /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
1801 __HAL_SPI_DISABLE(hspi);
1802 SPI_1LINE_RX(hspi);
1803 }
1804
1805#if (USE_SPI_CRC != 0U)
1806 /* Reset CRC Calculation */
1808 {
1809 SPI_RESET_CRC(hspi);
1810 }
1811#endif /* USE_SPI_CRC */
1812
1813 /* Set the SPI RxDMA Half transfer complete callback */
1814 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
1815
1816 /* Set the SPI Rx DMA transfer complete callback */
1817 hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
1818
1819 /* Set the DMA error callback */
1820 hspi->hdmarx->XferErrorCallback = SPI_DMAError;
1821
1822 /* Set the DMA AbortCpltCallback */
1823 hspi->hdmarx->XferAbortCallback = NULL;
1824
1825 /* Enable the Rx DMA Stream/Channel */
1826 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr,
1827 hspi->RxXferCount))
1828 {
1829 /* Update SPI error code */
1830 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
1831 /* Process Unlocked */
1832 __HAL_UNLOCK(hspi);
1833 return HAL_ERROR;
1834 }
1835
1836 /* Check if the SPI is already enabled */
1837 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1838 {
1839 /* Enable SPI peripheral */
1840 __HAL_SPI_ENABLE(hspi);
1841 }
1842
1843 /* Process Unlocked */
1844 __HAL_UNLOCK(hspi);
1845
1846 /* Enable the SPI Error Interrupt Bit */
1848
1849 /* Enable Rx DMA Request */
1850 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1851
1852 return HAL_OK;
1853}
1854
1865HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,
1866 uint16_t Size)
1867{
1868 uint32_t tmp_mode;
1869 HAL_SPI_StateTypeDef tmp_state;
1870
1871 /* Check rx & tx dma handles */
1874
1875 /* Check Direction parameter */
1877
1878 /* Init temporary variables */
1879 tmp_state = hspi->State;
1880 tmp_mode = hspi->Init.Mode;
1881
1882 if (!((tmp_state == HAL_SPI_STATE_READY) ||
1883 ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) &&
1884 (tmp_state == HAL_SPI_STATE_BUSY_RX))))
1885 {
1886 return HAL_BUSY;
1887 }
1888
1889 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1890 {
1891 return HAL_ERROR;
1892 }
1893
1894 /* Process locked */
1895 __HAL_LOCK(hspi);
1896
1897 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1898 if (hspi->State != HAL_SPI_STATE_BUSY_RX)
1899 {
1901 }
1902
1903 /* Set the transaction information */
1905 hspi->pTxBuffPtr = (const uint8_t *)pTxData;
1906 hspi->TxXferSize = Size;
1907 hspi->TxXferCount = Size;
1908 hspi->pRxBuffPtr = (uint8_t *)pRxData;
1909 hspi->RxXferSize = Size;
1910 hspi->RxXferCount = Size;
1911
1912 /* Init field not used in handle to zero */
1913 hspi->RxISR = NULL;
1914 hspi->TxISR = NULL;
1915
1916#if (USE_SPI_CRC != 0U)
1917 /* Reset CRC Calculation */
1919 {
1920 SPI_RESET_CRC(hspi);
1921 }
1922#endif /* USE_SPI_CRC */
1923
1924 /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
1925 if (hspi->State == HAL_SPI_STATE_BUSY_RX)
1926 {
1927 /* Set the SPI Rx DMA Half transfer complete callback */
1928 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
1929 hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
1930 }
1931 else
1932 {
1933 /* Set the SPI Tx/Rx DMA Half transfer complete callback */
1934 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
1935 hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
1936 }
1937
1938 /* Set the DMA error callback */
1939 hspi->hdmarx->XferErrorCallback = SPI_DMAError;
1940
1941 /* Set the DMA AbortCpltCallback */
1942 hspi->hdmarx->XferAbortCallback = NULL;
1943
1944 /* Enable the Rx DMA Stream/Channel */
1945 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr,
1946 hspi->RxXferCount))
1947 {
1948 /* Update SPI error code */
1949 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
1950 /* Process Unlocked */
1951 __HAL_UNLOCK(hspi);
1952 return HAL_ERROR;
1953 }
1954
1955 /* Enable Rx DMA Request */
1956 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1957
1958 /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
1959 is performed in DMA reception complete callback */
1960 hspi->hdmatx->XferHalfCpltCallback = NULL;
1961 hspi->hdmatx->XferCpltCallback = NULL;
1962 hspi->hdmatx->XferErrorCallback = NULL;
1963 hspi->hdmatx->XferAbortCallback = NULL;
1964
1965 /* Enable the Tx DMA Stream/Channel */
1966 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR,
1967 hspi->TxXferCount))
1968 {
1969 /* Update SPI error code */
1970 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
1971 /* Process Unlocked */
1972 __HAL_UNLOCK(hspi);
1973 return HAL_ERROR;
1974 }
1975
1976 /* Check if the SPI is already enabled */
1977 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1978 {
1979 /* Enable SPI peripheral */
1980 __HAL_SPI_ENABLE(hspi);
1981 }
1982
1983 /* Process Unlocked */
1984 __HAL_UNLOCK(hspi);
1985
1986 /* Enable the SPI Error Interrupt Bit */
1988
1989 /* Enable Tx DMA Request */
1990 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1991
1992 return HAL_OK;
1993}
1994
2009{
2010 HAL_StatusTypeDef errorcode;
2011 __IO uint32_t count;
2012 __IO uint32_t resetcount;
2013
2014 /* Initialized local variable */
2015 errorcode = HAL_OK;
2016 resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
2017 count = resetcount;
2018
2019 /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
2020 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
2021
2022 /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
2023 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
2024 {
2025 hspi->TxISR = SPI_AbortTx_ISR;
2026 /* Wait HAL_SPI_STATE_ABORT state */
2027 do
2028 {
2029 if (count == 0U)
2030 {
2031 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2032 break;
2033 }
2034 count--;
2035 } while (hspi->State != HAL_SPI_STATE_ABORT);
2036 /* Reset Timeout Counter */
2037 count = resetcount;
2038 }
2039
2040 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
2041 {
2042 hspi->RxISR = SPI_AbortRx_ISR;
2043 /* Wait HAL_SPI_STATE_ABORT state */
2044 do
2045 {
2046 if (count == 0U)
2047 {
2048 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2049 break;
2050 }
2051 count--;
2052 } while (hspi->State != HAL_SPI_STATE_ABORT);
2053 /* Reset Timeout Counter */
2054 count = resetcount;
2055 }
2056
2057 /* Disable the SPI DMA Tx request if enabled */
2058 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
2059 {
2060 /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
2061 if (hspi->hdmatx != NULL)
2062 {
2063 /* Set the SPI DMA Abort callback :
2064 will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
2065 hspi->hdmatx->XferAbortCallback = NULL;
2066
2067 /* Abort DMA Tx Handle linked to SPI Peripheral */
2068 if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK)
2069 {
2071 }
2072
2073 /* Disable Tx DMA Request */
2074 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN));
2075
2076 /* Wait until TXE flag is set */
2077 do
2078 {
2079 if (count == 0U)
2080 {
2081 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2082 break;
2083 }
2084 count--;
2085 } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
2086 }
2087 }
2088
2089 /* Disable the SPI DMA Rx request if enabled */
2090 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
2091 {
2092 /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
2093 if (hspi->hdmarx != NULL)
2094 {
2095 /* Set the SPI DMA Abort callback :
2096 will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
2097 hspi->hdmarx->XferAbortCallback = NULL;
2098
2099 /* Abort DMA Rx Handle linked to SPI Peripheral */
2100 if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK)
2101 {
2103 }
2104
2105 /* Disable peripheral */
2106 __HAL_SPI_DISABLE(hspi);
2107
2108 /* Disable Rx DMA Request */
2109 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN));
2110 }
2111 }
2112 /* Reset Tx and Rx transfer counters */
2113 hspi->RxXferCount = 0U;
2114 hspi->TxXferCount = 0U;
2115
2116 /* Check error during Abort procedure */
2117 if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
2118 {
2119 /* return HAL_Error in case of error during Abort procedure */
2120 errorcode = HAL_ERROR;
2121 }
2122 else
2123 {
2124 /* Reset errorCode */
2126 }
2127
2128 /* Clear the Error flags in the SR register */
2131
2132 /* Restore hspi->state to ready */
2133 hspi->State = HAL_SPI_STATE_READY;
2134
2135 return errorcode;
2136}
2137
2154{
2155 HAL_StatusTypeDef errorcode;
2156 uint32_t abortcplt ;
2157 __IO uint32_t count;
2158 __IO uint32_t resetcount;
2159
2160 /* Initialized local variable */
2161 errorcode = HAL_OK;
2162 abortcplt = 1U;
2163 resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
2164 count = resetcount;
2165
2166 /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
2167 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
2168
2169 /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */
2170 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
2171 {
2172 hspi->TxISR = SPI_AbortTx_ISR;
2173 /* Wait HAL_SPI_STATE_ABORT state */
2174 do
2175 {
2176 if (count == 0U)
2177 {
2178 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2179 break;
2180 }
2181 count--;
2182 } while (hspi->State != HAL_SPI_STATE_ABORT);
2183 /* Reset Timeout Counter */
2184 count = resetcount;
2185 }
2186
2187 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
2188 {
2189 hspi->RxISR = SPI_AbortRx_ISR;
2190 /* Wait HAL_SPI_STATE_ABORT state */
2191 do
2192 {
2193 if (count == 0U)
2194 {
2195 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2196 break;
2197 }
2198 count--;
2199 } while (hspi->State != HAL_SPI_STATE_ABORT);
2200 /* Reset Timeout Counter */
2201 count = resetcount;
2202 }
2203
2204 /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised
2205 before any call to DMA Abort functions */
2206 /* DMA Tx Handle is valid */
2207 if (hspi->hdmatx != NULL)
2208 {
2209 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
2210 Otherwise, set it to NULL */
2211 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
2212 {
2213 hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback;
2214 }
2215 else
2216 {
2217 hspi->hdmatx->XferAbortCallback = NULL;
2218 }
2219 }
2220 /* DMA Rx Handle is valid */
2221 if (hspi->hdmarx != NULL)
2222 {
2223 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
2224 Otherwise, set it to NULL */
2225 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
2226 {
2227 hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback;
2228 }
2229 else
2230 {
2231 hspi->hdmarx->XferAbortCallback = NULL;
2232 }
2233 }
2234
2235 /* Disable the SPI DMA Tx request if enabled */
2236 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
2237 {
2238 /* Abort the SPI DMA Tx Stream/Channel */
2239 if (hspi->hdmatx != NULL)
2240 {
2241 /* Abort DMA Tx Handle linked to SPI Peripheral */
2242 if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK)
2243 {
2244 hspi->hdmatx->XferAbortCallback = NULL;
2246 }
2247 else
2248 {
2249 abortcplt = 0U;
2250 }
2251 }
2252 }
2253 /* Disable the SPI DMA Rx request if enabled */
2254 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
2255 {
2256 /* Abort the SPI DMA Rx Stream/Channel */
2257 if (hspi->hdmarx != NULL)
2258 {
2259 /* Abort DMA Rx Handle linked to SPI Peripheral */
2260 if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK)
2261 {
2262 hspi->hdmarx->XferAbortCallback = NULL;
2264 }
2265 else
2266 {
2267 abortcplt = 0U;
2268 }
2269 }
2270 }
2271
2272 if (abortcplt == 1U)
2273 {
2274 /* Reset Tx and Rx transfer counters */
2275 hspi->RxXferCount = 0U;
2276 hspi->TxXferCount = 0U;
2277
2278 /* Check error during Abort procedure */
2279 if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
2280 {
2281 /* return HAL_Error in case of error during Abort procedure */
2282 errorcode = HAL_ERROR;
2283 }
2284 else
2285 {
2286 /* Reset errorCode */
2288 }
2289
2290 /* Clear the Error flags in the SR register */
2293
2294 /* Restore hspi->State to Ready */
2295 hspi->State = HAL_SPI_STATE_READY;
2296
2297 /* As no DMA to be aborted, call directly user Abort complete callback */
2298#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2299 hspi->AbortCpltCallback(hspi);
2300#else
2302#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2303 }
2304
2305 return errorcode;
2306}
2307
2315{
2316 /* Process Locked */
2317 __HAL_LOCK(hspi);
2318
2319 /* Disable the SPI DMA Tx & Rx requests */
2320 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2321
2322 /* Process Unlocked */
2323 __HAL_UNLOCK(hspi);
2324
2325 return HAL_OK;
2326}
2327
2335{
2336 /* Process Locked */
2337 __HAL_LOCK(hspi);
2338
2339 /* Enable the SPI DMA Tx & Rx requests */
2340 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2341
2342 /* Process Unlocked */
2343 __HAL_UNLOCK(hspi);
2344
2345 return HAL_OK;
2346}
2347
2355{
2356 HAL_StatusTypeDef errorcode = HAL_OK;
2357 /* The Lock is not implemented on this API to allow the user application
2358 to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or
2359 HAL_SPI_TxRxCpltCallback():
2360 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
2361 and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or
2362 HAL_SPI_TxRxCpltCallback()
2363 */
2364
2365 /* Abort the SPI DMA tx Stream/Channel */
2366 if (hspi->hdmatx != NULL)
2367 {
2368 if (HAL_OK != HAL_DMA_Abort(hspi->hdmatx))
2369 {
2370 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2371 errorcode = HAL_ERROR;
2372 }
2373 }
2374 /* Abort the SPI DMA rx Stream/Channel */
2375 if (hspi->hdmarx != NULL)
2376 {
2377 if (HAL_OK != HAL_DMA_Abort(hspi->hdmarx))
2378 {
2379 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2380 errorcode = HAL_ERROR;
2381 }
2382 }
2383
2384 /* Disable the SPI DMA Tx & Rx requests */
2385 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2386 hspi->State = HAL_SPI_STATE_READY;
2387 return errorcode;
2388}
2389
2397{
2398 uint32_t itsource = hspi->Instance->CR2;
2399 uint32_t itflag = hspi->Instance->SR;
2400
2401 /* SPI in mode Receiver ----------------------------------------------------*/
2402 if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) == RESET) &&
2403 (SPI_CHECK_FLAG(itflag, SPI_FLAG_RXNE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_RXNE) != RESET))
2404 {
2405 hspi->RxISR(hspi);
2406 return;
2407 }
2408
2409 /* SPI in mode Transmitter -------------------------------------------------*/
2410 if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_TXE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_TXE) != RESET))
2411 {
2412 hspi->TxISR(hspi);
2413 return;
2414 }
2415
2416 /* SPI in Error Treatment --------------------------------------------------*/
2417 if (((SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
2418 || (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_ERR) != RESET))
2419 {
2420 /* SPI Overrun error interrupt occurred ----------------------------------*/
2421 if (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
2422 {
2423 if (hspi->State != HAL_SPI_STATE_BUSY_TX)
2424 {
2425 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
2427 }
2428 else
2429 {
2431 return;
2432 }
2433 }
2434
2435 /* SPI Mode Fault error interrupt occurred -------------------------------*/
2436 if (SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET)
2437 {
2438 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
2440 }
2441
2442 /* SPI Frame error interrupt occurred ------------------------------------*/
2443 if (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)
2444 {
2445 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
2447 }
2448
2449 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2450 {
2451 /* Disable all interrupts */
2453
2454 hspi->State = HAL_SPI_STATE_READY;
2455 /* Disable the SPI DMA requests if enabled */
2456 if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN)))
2457 {
2458 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
2459
2460 /* Abort the SPI DMA Rx channel */
2461 if (hspi->hdmarx != NULL)
2462 {
2463 /* Set the SPI DMA Abort callback :
2464 will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
2465 hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError;
2466 if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx))
2467 {
2468 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2469 }
2470 }
2471 /* Abort the SPI DMA Tx channel */
2472 if (hspi->hdmatx != NULL)
2473 {
2474 /* Set the SPI DMA Abort callback :
2475 will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
2476 hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError;
2477 if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx))
2478 {
2479 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2480 }
2481 }
2482 }
2483 else
2484 {
2485 /* Call user error callback */
2486#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2487 hspi->ErrorCallback(hspi);
2488#else
2490#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2491 }
2492 }
2493 return;
2494 }
2495}
2496
2504{
2505 /* Prevent unused argument(s) compilation warning */
2506 UNUSED(hspi);
2507
2508 /* NOTE : This function should not be modified, when the callback is needed,
2509 the HAL_SPI_TxCpltCallback should be implemented in the user file
2510 */
2511}
2512
2520{
2521 /* Prevent unused argument(s) compilation warning */
2522 UNUSED(hspi);
2523
2524 /* NOTE : This function should not be modified, when the callback is needed,
2525 the HAL_SPI_RxCpltCallback should be implemented in the user file
2526 */
2527}
2528
2536{
2537 /* Prevent unused argument(s) compilation warning */
2538 UNUSED(hspi);
2539
2540 /* NOTE : This function should not be modified, when the callback is needed,
2541 the HAL_SPI_TxRxCpltCallback should be implemented in the user file
2542 */
2543}
2544
2552{
2553 /* Prevent unused argument(s) compilation warning */
2554 UNUSED(hspi);
2555
2556 /* NOTE : This function should not be modified, when the callback is needed,
2557 the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
2558 */
2559}
2560
2568{
2569 /* Prevent unused argument(s) compilation warning */
2570 UNUSED(hspi);
2571
2572 /* NOTE : This function should not be modified, when the callback is needed,
2573 the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
2574 */
2575}
2576
2584{
2585 /* Prevent unused argument(s) compilation warning */
2586 UNUSED(hspi);
2587
2588 /* NOTE : This function should not be modified, when the callback is needed,
2589 the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
2590 */
2591}
2592
2600{
2601 /* Prevent unused argument(s) compilation warning */
2602 UNUSED(hspi);
2603
2604 /* NOTE : This function should not be modified, when the callback is needed,
2605 the HAL_SPI_ErrorCallback should be implemented in the user file
2606 */
2607 /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
2608 and user can use HAL_SPI_GetError() API to check the latest error occurred
2609 */
2610}
2611
2618{
2619 /* Prevent unused argument(s) compilation warning */
2620 UNUSED(hspi);
2621
2622 /* NOTE : This function should not be modified, when the callback is needed,
2623 the HAL_SPI_AbortCpltCallback can be implemented in the user file.
2624 */
2625}
2626
2630
2645
2653{
2654 /* Return SPI handle state */
2655 return hspi->State;
2656}
2657
2664uint32_t HAL_SPI_GetError(const SPI_HandleTypeDef *hspi)
2665{
2666 /* Return SPI ErrorCode */
2667 return hspi->ErrorCode;
2668}
2669
2673
2677
2682
2689static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2690{
2691 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2692 uint32_t tickstart;
2693
2694 /* Init tickstart for timeout management*/
2695 tickstart = HAL_GetTick();
2696
2697 /* DMA Normal Mode */
2698 if ((hdma->Instance->CR & DMA_SxCR_CIRC) != DMA_SxCR_CIRC)
2699 {
2700 /* Disable ERR interrupt */
2702
2703 /* Disable Tx DMA Request */
2704 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
2705
2706 /* Check the end of the transaction */
2707 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
2708 {
2709 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
2710 }
2711
2712 /* Clear overrun flag in 2 Lines communication mode because received data is not read */
2713 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
2714 {
2716 }
2717
2718 hspi->TxXferCount = 0U;
2719 hspi->State = HAL_SPI_STATE_READY;
2720
2721 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2722 {
2723 /* Call user error callback */
2724#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2725 hspi->ErrorCallback(hspi);
2726#else
2728#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2729 return;
2730 }
2731 }
2732 /* Call user Tx complete callback */
2733#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2734 hspi->TxCpltCallback(hspi);
2735#else
2737#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2738}
2739
2746static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2747{
2748 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2749 uint32_t tickstart;
2750#if (USE_SPI_CRC != 0U)
2751 __IO uint32_t tmpreg = 0U;
2752#endif /* USE_SPI_CRC */
2753
2754 /* Init tickstart for timeout management*/
2755 tickstart = HAL_GetTick();
2756
2757 /* DMA Normal Mode */
2758 if ((hdma->Instance->CR & DMA_SxCR_CIRC) != DMA_SxCR_CIRC)
2759 {
2760 /* Disable ERR interrupt */
2762
2763#if (USE_SPI_CRC != 0U)
2764 /* CRC handling */
2766 {
2767 /* Wait until RXNE flag */
2768 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
2769 {
2770 /* Error on the CRC reception */
2771 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
2772 }
2773 /* Read CRC */
2774 tmpreg = READ_REG(hspi->Instance->DR);
2775 /* To avoid GCC warning */
2776 UNUSED(tmpreg);
2777 }
2778#endif /* USE_SPI_CRC */
2779
2780 /* Check if we are in Master RX 2 line mode */
2781 if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
2782 {
2783 /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
2784 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2785 }
2786 else
2787 {
2788 /* Normal case */
2789 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
2790 }
2791
2792 /* Check the end of the transaction */
2793 if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
2794 {
2796 }
2797
2798 hspi->RxXferCount = 0U;
2799 hspi->State = HAL_SPI_STATE_READY;
2800
2801#if (USE_SPI_CRC != 0U)
2802 /* Check if CRC error occurred */
2804 {
2805 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
2807 }
2808#endif /* USE_SPI_CRC */
2809
2810 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2811 {
2812 /* Call user error callback */
2813#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2814 hspi->ErrorCallback(hspi);
2815#else
2817#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2818 return;
2819 }
2820 }
2821 /* Call user Rx complete callback */
2822#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2823 hspi->RxCpltCallback(hspi);
2824#else
2826#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2827}
2828
2835static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
2836{
2837 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2838 uint32_t tickstart;
2839#if (USE_SPI_CRC != 0U)
2840 __IO uint32_t tmpreg = 0U;
2841#endif /* USE_SPI_CRC */
2842
2843 /* Init tickstart for timeout management*/
2844 tickstart = HAL_GetTick();
2845
2846 /* DMA Normal Mode */
2847 if ((hdma->Instance->CR & DMA_SxCR_CIRC) != DMA_SxCR_CIRC)
2848 {
2849 /* Disable ERR interrupt */
2851
2852#if (USE_SPI_CRC != 0U)
2853 /* CRC handling */
2855 {
2856 /* Wait the CRC data */
2857 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
2858 {
2859 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
2860 }
2861 /* Read CRC to Flush DR and RXNE flag */
2862 tmpreg = READ_REG(hspi->Instance->DR);
2863 /* To avoid GCC warning */
2864 UNUSED(tmpreg);
2865 }
2866#endif /* USE_SPI_CRC */
2867
2868 /* Check the end of the transaction */
2869 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
2870 {
2871 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
2872 }
2873
2874 /* Disable Rx/Tx DMA Request */
2875 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2876
2877 hspi->TxXferCount = 0U;
2878 hspi->RxXferCount = 0U;
2879 hspi->State = HAL_SPI_STATE_READY;
2880
2881#if (USE_SPI_CRC != 0U)
2882 /* Check if CRC error occurred */
2884 {
2885 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
2887 }
2888#endif /* USE_SPI_CRC */
2889
2890 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2891 {
2892 /* Call user error callback */
2893#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2894 hspi->ErrorCallback(hspi);
2895#else
2897#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2898 return;
2899 }
2900 }
2901 /* Call user TxRx complete callback */
2902#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2903 hspi->TxRxCpltCallback(hspi);
2904#else
2906#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2907}
2908
2915static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
2916{
2917 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2918
2919 /* Call user Tx half complete callback */
2920#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2921 hspi->TxHalfCpltCallback(hspi);
2922#else
2924#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2925}
2926
2933static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
2934{
2935 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2936
2937 /* Call user Rx half complete callback */
2938#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2939 hspi->RxHalfCpltCallback(hspi);
2940#else
2942#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2943}
2944
2951static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
2952{
2953 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2954
2955 /* Call user TxRx half complete callback */
2956#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2957 hspi->TxRxHalfCpltCallback(hspi);
2958#else
2960#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2961}
2962
2969static void SPI_DMAError(DMA_HandleTypeDef *hdma)
2970{
2971 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2972
2973 /* Stop the disable DMA transfer on SPI side */
2974 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2975
2976 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2977 hspi->State = HAL_SPI_STATE_READY;
2978 /* Call user error callback */
2979#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2980 hspi->ErrorCallback(hspi);
2981#else
2983#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2984}
2985
2992static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2993{
2994 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
2995 hspi->RxXferCount = 0U;
2996 hspi->TxXferCount = 0U;
2997
2998 /* Call user error callback */
2999#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3000 hspi->ErrorCallback(hspi);
3001#else
3003#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3004}
3005
3014static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3015{
3016 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
3017 __IO uint32_t count;
3018
3019 hspi->hdmatx->XferAbortCallback = NULL;
3020 count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
3021
3022 /* Disable Tx DMA Request */
3023 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
3024
3025 /* Wait until TXE flag is set */
3026 do
3027 {
3028 if (count == 0U)
3029 {
3030 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
3031 break;
3032 }
3033 count--;
3034 } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
3035
3036 /* Check if an Abort process is still ongoing */
3037 if (hspi->hdmarx != NULL)
3038 {
3039 if (hspi->hdmarx->XferAbortCallback != NULL)
3040 {
3041 return;
3042 }
3043 }
3044
3045 /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
3046 hspi->RxXferCount = 0U;
3047 hspi->TxXferCount = 0U;
3048
3049 /* Check no error during Abort procedure */
3050 if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
3051 {
3052 /* Reset errorCode */
3054 }
3055
3056 /* Clear the Error flags in the SR register */
3059
3060 /* Restore hspi->State to Ready */
3061 hspi->State = HAL_SPI_STATE_READY;
3062
3063 /* Call user Abort complete callback */
3064#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3065 hspi->AbortCpltCallback(hspi);
3066#else
3068#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3069}
3070
3079static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
3080{
3081 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
3082
3083 /* Disable SPI Peripheral */
3084 __HAL_SPI_DISABLE(hspi);
3085
3086 hspi->hdmarx->XferAbortCallback = NULL;
3087
3088 /* Disable Rx DMA Request */
3089 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
3090
3091 /* Check Busy flag */
3092 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
3093 {
3094 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
3095 }
3096
3097 /* Check if an Abort process is still ongoing */
3098 if (hspi->hdmatx != NULL)
3099 {
3100 if (hspi->hdmatx->XferAbortCallback != NULL)
3101 {
3102 return;
3103 }
3104 }
3105
3106 /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
3107 hspi->RxXferCount = 0U;
3108 hspi->TxXferCount = 0U;
3109
3110 /* Check no error during Abort procedure */
3111 if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
3112 {
3113 /* Reset errorCode */
3115 }
3116
3117 /* Clear the Error flags in the SR register */
3120
3121 /* Restore hspi->State to Ready */
3122 hspi->State = HAL_SPI_STATE_READY;
3123
3124 /* Call user Abort complete callback */
3125#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3126 hspi->AbortCpltCallback(hspi);
3127#else
3129#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3130}
3131
3138static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
3139{
3140 /* Receive data in 8bit mode */
3141 *hspi->pRxBuffPtr = *((__IO uint8_t *)&hspi->Instance->DR);
3142 hspi->pRxBuffPtr++;
3143 hspi->RxXferCount--;
3144
3145 /* Check end of the reception */
3146 if (hspi->RxXferCount == 0U)
3147 {
3148#if (USE_SPI_CRC != 0U)
3150 {
3151 hspi->RxISR = SPI_2linesRxISR_8BITCRC;
3152 return;
3153 }
3154#endif /* USE_SPI_CRC */
3155
3156 /* Disable RXNE and ERR interrupt */
3158
3159 if (hspi->TxXferCount == 0U)
3160 {
3161 SPI_CloseRxTx_ISR(hspi);
3162 }
3163 }
3164}
3165
3166#if (USE_SPI_CRC != 0U)
3173static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
3174{
3175 __IO uint8_t *ptmpreg8;
3176 __IO uint8_t tmpreg8 = 0;
3177
3178 /* Initialize the 8bit temporary pointer */
3179 ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
3180 /* Read 8bit CRC to flush Data Register */
3181 tmpreg8 = *ptmpreg8;
3182 /* To avoid GCC warning */
3183 UNUSED(tmpreg8);
3184
3185 /* Disable RXNE and ERR interrupt */
3187
3188 if (hspi->TxXferCount == 0U)
3189 {
3190 SPI_CloseRxTx_ISR(hspi);
3191 }
3192}
3193#endif /* USE_SPI_CRC */
3194
3201static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
3202{
3203 *(__IO uint8_t *)&hspi->Instance->DR = *((const uint8_t *)hspi->pTxBuffPtr);
3204 hspi->pTxBuffPtr++;
3205 hspi->TxXferCount--;
3206
3207 /* Check the end of the transmission */
3208 if (hspi->TxXferCount == 0U)
3209 {
3210#if (USE_SPI_CRC != 0U)
3212 {
3213 /* Set CRC Next Bit to send CRC */
3214 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
3215 /* Disable TXE interrupt */
3217 return;
3218 }
3219#endif /* USE_SPI_CRC */
3220
3221 /* Disable TXE interrupt */
3223
3224 if (hspi->RxXferCount == 0U)
3225 {
3226 SPI_CloseRxTx_ISR(hspi);
3227 }
3228 }
3229}
3230
3237static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
3238{
3239 /* Receive data in 16 Bit mode */
3240 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
3241 hspi->pRxBuffPtr += sizeof(uint16_t);
3242 hspi->RxXferCount--;
3243
3244 if (hspi->RxXferCount == 0U)
3245 {
3246#if (USE_SPI_CRC != 0U)
3248 {
3249 hspi->RxISR = SPI_2linesRxISR_16BITCRC;
3250 return;
3251 }
3252#endif /* USE_SPI_CRC */
3253
3254 /* Disable RXNE interrupt */
3256
3257 if (hspi->TxXferCount == 0U)
3258 {
3259 SPI_CloseRxTx_ISR(hspi);
3260 }
3261 }
3262}
3263
3264#if (USE_SPI_CRC != 0U)
3271static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
3272{
3273 __IO uint32_t tmpreg = 0U;
3274
3275 /* Read 16bit CRC to flush Data Register */
3276 tmpreg = READ_REG(hspi->Instance->DR);
3277 /* To avoid GCC warning */
3278 UNUSED(tmpreg);
3279
3280 /* Disable RXNE interrupt */
3282
3283 SPI_CloseRxTx_ISR(hspi);
3284}
3285#endif /* USE_SPI_CRC */
3286
3293static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
3294{
3295 /* Transmit data in 16 Bit mode */
3296 hspi->Instance->DR = *((const uint16_t *)hspi->pTxBuffPtr);
3297 hspi->pTxBuffPtr += sizeof(uint16_t);
3298 hspi->TxXferCount--;
3299
3300 /* Enable CRC Transmission */
3301 if (hspi->TxXferCount == 0U)
3302 {
3303#if (USE_SPI_CRC != 0U)
3305 {
3306 /* Set CRC Next Bit to send CRC */
3307 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
3308 /* Disable TXE interrupt */
3310 return;
3311 }
3312#endif /* USE_SPI_CRC */
3313
3314 /* Disable TXE interrupt */
3316
3317 if (hspi->RxXferCount == 0U)
3318 {
3319 SPI_CloseRxTx_ISR(hspi);
3320 }
3321 }
3322}
3323
3324#if (USE_SPI_CRC != 0U)
3331static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
3332{
3333 __IO uint8_t *ptmpreg8;
3334 __IO uint8_t tmpreg8 = 0;
3335
3336 /* Initialize the 8bit temporary pointer */
3337 ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
3338 /* Read 8bit CRC to flush Data Register */
3339 tmpreg8 = *ptmpreg8;
3340 /* To avoid GCC warning */
3341 UNUSED(tmpreg8);
3342
3343 SPI_CloseRx_ISR(hspi);
3344}
3345#endif /* USE_SPI_CRC */
3346
3353static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
3354{
3355 *hspi->pRxBuffPtr = (*(__IO uint8_t *)&hspi->Instance->DR);
3356 hspi->pRxBuffPtr++;
3357 hspi->RxXferCount--;
3358
3359#if (USE_SPI_CRC != 0U)
3360 /* Enable CRC Transmission */
3361 if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
3362 {
3363 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
3364 }
3365#endif /* USE_SPI_CRC */
3366
3367 if (hspi->RxXferCount == 0U)
3368 {
3369#if (USE_SPI_CRC != 0U)
3371 {
3372 hspi->RxISR = SPI_RxISR_8BITCRC;
3373 return;
3374 }
3375#endif /* USE_SPI_CRC */
3376 SPI_CloseRx_ISR(hspi);
3377 }
3378}
3379
3380#if (USE_SPI_CRC != 0U)
3387static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
3388{
3389 __IO uint32_t tmpreg = 0U;
3390
3391 /* Read 16bit CRC to flush Data Register */
3392 tmpreg = READ_REG(hspi->Instance->DR);
3393 /* To avoid GCC warning */
3394 UNUSED(tmpreg);
3395
3396 /* Disable RXNE and ERR interrupt */
3398
3399 SPI_CloseRx_ISR(hspi);
3400}
3401#endif /* USE_SPI_CRC */
3402
3409static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
3410{
3411 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
3412 hspi->pRxBuffPtr += sizeof(uint16_t);
3413 hspi->RxXferCount--;
3414
3415#if (USE_SPI_CRC != 0U)
3416 /* Enable CRC Transmission */
3417 if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
3418 {
3419 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
3420 }
3421#endif /* USE_SPI_CRC */
3422
3423 if (hspi->RxXferCount == 0U)
3424 {
3425#if (USE_SPI_CRC != 0U)
3427 {
3428 hspi->RxISR = SPI_RxISR_16BITCRC;
3429 return;
3430 }
3431#endif /* USE_SPI_CRC */
3432 SPI_CloseRx_ISR(hspi);
3433 }
3434}
3435
3442static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
3443{
3444 *(__IO uint8_t *)&hspi->Instance->DR = *((const uint8_t *)hspi->pTxBuffPtr);
3445 hspi->pTxBuffPtr++;
3446 hspi->TxXferCount--;
3447
3448 if (hspi->TxXferCount == 0U)
3449 {
3450#if (USE_SPI_CRC != 0U)
3452 {
3453 /* Enable CRC Transmission */
3454 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
3455 }
3456#endif /* USE_SPI_CRC */
3457 SPI_CloseTx_ISR(hspi);
3458 }
3459}
3460
3467static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
3468{
3469 /* Transmit data in 16 Bit mode */
3470 hspi->Instance->DR = *((const uint16_t *)hspi->pTxBuffPtr);
3471 hspi->pTxBuffPtr += sizeof(uint16_t);
3472 hspi->TxXferCount--;
3473
3474 if (hspi->TxXferCount == 0U)
3475 {
3476#if (USE_SPI_CRC != 0U)
3478 {
3479 /* Enable CRC Transmission */
3480 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
3481 }
3482#endif /* USE_SPI_CRC */
3483 SPI_CloseTx_ISR(hspi);
3484 }
3485}
3486
3497static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
3498 uint32_t Timeout, uint32_t Tickstart)
3499{
3500 __IO uint32_t count;
3501 uint32_t tmp_timeout;
3502 uint32_t tmp_tickstart;
3503
3504 /* Adjust Timeout value in case of end of transfer */
3505 tmp_timeout = Timeout - (HAL_GetTick() - Tickstart);
3506 tmp_tickstart = HAL_GetTick();
3507
3508 /* Calculate Timeout based on a software loop to avoid blocking issue if Systick is disabled */
3509 count = tmp_timeout * ((SystemCoreClock * 32U) >> 20U);
3510
3511 while ((__HAL_SPI_GET_FLAG(hspi, Flag) ? SET : RESET) != State)
3512 {
3513 if (Timeout != HAL_MAX_DELAY)
3514 {
3515 if (((HAL_GetTick() - tmp_tickstart) >= tmp_timeout) || (tmp_timeout == 0U))
3516 {
3517 /* Disable the SPI and reset the CRC: the CRC value should be cleared
3518 on both master and slave sides in order to resynchronize the master
3519 and slave for their respective CRC calculation */
3520
3521 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
3523
3524 if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
3526 {
3527 /* Disable SPI peripheral */
3528 __HAL_SPI_DISABLE(hspi);
3529 }
3530
3531 /* Reset CRC Calculation */
3533 {
3534 SPI_RESET_CRC(hspi);
3535 }
3536
3537 hspi->State = HAL_SPI_STATE_READY;
3538
3539 /* Process Unlocked */
3540 __HAL_UNLOCK(hspi);
3541
3542 return HAL_TIMEOUT;
3543 }
3544 /* If Systick is disabled or not incremented, deactivate timeout to go in disable loop procedure */
3545 if (count == 0U)
3546 {
3547 tmp_timeout = 0U;
3548 }
3549 else
3550 {
3551 count--;
3552 }
3553 }
3554 }
3555
3556 return HAL_OK;
3557}
3558
3567static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
3568{
3569 if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
3571 {
3572 /* Disable SPI peripheral */
3573 __HAL_SPI_DISABLE(hspi);
3574 }
3575
3576 /* Erratasheet: BSY bit may stay high at the end of a data transfer in Slave mode */
3577 if (hspi->Init.Mode == SPI_MODE_MASTER)
3578 {
3580 {
3581 /* Control the BSY flag */
3582 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
3583 {
3584 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3585 return HAL_TIMEOUT;
3586 }
3587 }
3588 else
3589 {
3590 /* Wait the RXNE reset */
3591 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout, Tickstart) != HAL_OK)
3592 {
3593 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3594 return HAL_TIMEOUT;
3595 }
3596 }
3597 }
3598 else
3599 {
3600 /* Wait the RXNE reset */
3601 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout, Tickstart) != HAL_OK)
3602 {
3603 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3604 return HAL_TIMEOUT;
3605 }
3606 }
3607 return HAL_OK;
3608}
3609
3617static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
3618{
3619 __IO uint32_t count;
3620
3621 /* Wait until TXE flag */
3622 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_TXE, SET, Timeout, Tickstart) != HAL_OK)
3623 {
3624 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3625 return HAL_TIMEOUT;
3626 }
3627
3628 /* Timeout in us */
3629 count = SPI_BSY_FLAG_WORKAROUND_TIMEOUT * (SystemCoreClock / 24U / 1000000U);
3630 /* Erratasheet: BSY bit may stay high at the end of a data transfer in Slave mode */
3631 if (hspi->Init.Mode == SPI_MODE_MASTER)
3632 {
3633 /* Control the BSY flag */
3634 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
3635 {
3636 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3637 return HAL_TIMEOUT;
3638 }
3639 }
3640 else
3641 {
3642 /* Wait BSY flag during 1 Byte time transfer in case of Full-Duplex and Tx transfer
3643 * If Timeout is reached, the transfer is considered as finish.
3644 * User have to calculate the timeout value to fit with the time of 1 byte transfer.
3645 * This time is directly link with the SPI clock from Master device.
3646 */
3647 do
3648 {
3649 if (count == 0U)
3650 {
3651 break;
3652 }
3653 count--;
3654 } while (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_BSY) != RESET);
3655 }
3656
3657 return HAL_OK;
3658}
3659
3666static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
3667{
3668 uint32_t tickstart;
3669 __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
3670
3671 /* Init tickstart for timeout management */
3672 tickstart = HAL_GetTick();
3673
3674 /* Disable ERR interrupt */
3676
3677 /* Wait until TXE flag is set */
3678 do
3679 {
3680 if (count == 0U)
3681 {
3682 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3683 break;
3684 }
3685 count--;
3686 } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
3687
3688 /* Check the end of the transaction */
3689 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
3690 {
3691 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3692 }
3693
3694 /* Clear overrun flag in 2 Lines communication mode because received is not read */
3695 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
3696 {
3698 }
3699
3700#if (USE_SPI_CRC != 0U)
3701 /* Check if CRC error occurred */
3702 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
3703 {
3704 hspi->State = HAL_SPI_STATE_READY;
3705 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
3707 /* Call user error callback */
3708#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3709 hspi->ErrorCallback(hspi);
3710#else
3712#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3713 }
3714 else
3715 {
3716#endif /* USE_SPI_CRC */
3717 if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
3718 {
3719 if (hspi->State == HAL_SPI_STATE_BUSY_RX)
3720 {
3721 hspi->State = HAL_SPI_STATE_READY;
3722 /* Call user Rx complete callback */
3723#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3724 hspi->RxCpltCallback(hspi);
3725#else
3727#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3728 }
3729 else
3730 {
3731 hspi->State = HAL_SPI_STATE_READY;
3732 /* Call user TxRx complete callback */
3733#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3734 hspi->TxRxCpltCallback(hspi);
3735#else
3737#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3738 }
3739 }
3740 else
3741 {
3742 hspi->State = HAL_SPI_STATE_READY;
3743 /* Call user error callback */
3744#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3745 hspi->ErrorCallback(hspi);
3746#else
3748#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3749 }
3750#if (USE_SPI_CRC != 0U)
3751 }
3752#endif /* USE_SPI_CRC */
3753}
3754
3761static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
3762{
3763 /* Disable RXNE and ERR interrupt */
3765
3766 /* Check the end of the transaction */
3767 if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
3768 {
3769 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3770 }
3771
3772 /* Clear overrun flag in 2 Lines communication mode because received is not read */
3773 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
3774 {
3776 }
3777 hspi->State = HAL_SPI_STATE_READY;
3778
3779#if (USE_SPI_CRC != 0U)
3780 /* Check if CRC error occurred */
3781 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
3782 {
3783 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
3785 /* Call user error callback */
3786#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3787 hspi->ErrorCallback(hspi);
3788#else
3790#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3791 }
3792 else
3793 {
3794#endif /* USE_SPI_CRC */
3795 if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
3796 {
3797 /* Call user Rx complete callback */
3798#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3799 hspi->RxCpltCallback(hspi);
3800#else
3802#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3803 }
3804 else
3805 {
3806 /* Call user error callback */
3807#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3808 hspi->ErrorCallback(hspi);
3809#else
3811#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3812 }
3813#if (USE_SPI_CRC != 0U)
3814 }
3815#endif /* USE_SPI_CRC */
3816}
3817
3824static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
3825{
3826 uint32_t tickstart;
3827 __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
3828
3829 /* Init tickstart for timeout management*/
3830 tickstart = HAL_GetTick();
3831
3832 /* Wait until TXE flag is set */
3833 do
3834 {
3835 if (count == 0U)
3836 {
3837 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3838 break;
3839 }
3840 count--;
3841 } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
3842
3843 /* Disable TXE and ERR interrupt */
3845
3846 /* Check the end of the transaction */
3847 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
3848 {
3849 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
3850 }
3851
3852 /* Clear overrun flag in 2 Lines communication mode because received is not read */
3853 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
3854 {
3856 }
3857
3858 hspi->State = HAL_SPI_STATE_READY;
3859 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
3860 {
3861 /* Call user error callback */
3862#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3863 hspi->ErrorCallback(hspi);
3864#else
3866#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3867 }
3868 else
3869 {
3870 /* Call user Rx complete callback */
3871#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3872 hspi->TxCpltCallback(hspi);
3873#else
3875#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3876 }
3877}
3878
3885static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi)
3886{
3887 __IO uint32_t tmpreg = 0U;
3888 __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
3889
3890 /* Wait until TXE flag is set */
3891 do
3892 {
3893 if (count == 0U)
3894 {
3895 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
3896 break;
3897 }
3898 count--;
3899 } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
3900
3901 /* Disable SPI Peripheral */
3902 __HAL_SPI_DISABLE(hspi);
3903
3904 /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
3905 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE | SPI_CR2_RXNEIE | SPI_CR2_ERRIE));
3906
3907 /* Flush Data Register by a blank read */
3908 tmpreg = READ_REG(hspi->Instance->DR);
3909 /* To avoid GCC warning */
3910 UNUSED(tmpreg);
3911
3912 hspi->State = HAL_SPI_STATE_ABORT;
3913}
3914
3921static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi)
3922{
3923 /* Disable TXEIE interrupt */
3924 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE));
3925
3926 /* Disable SPI Peripheral */
3927 __HAL_SPI_DISABLE(hspi);
3928
3929 hspi->State = HAL_SPI_STATE_ABORT;
3930}
3931
3935
3936#endif /* HAL_SPI_MODULE_ENABLED */
3937
3941
3945
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
struct __DMA_HandleTypeDef DMA_HandleTypeDef
DMA handle Structure definition.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define SPI_BAUDRATEPRESCALER_2
#define SPI_CRCCALCULATION_ENABLE
#define SPI_CRCCALCULATION_DISABLE
#define SPI_PHASE_1EDGE
#define SPI_POLARITY_LOW
#define SPI_DATASIZE_8BIT
#define SPI_DATASIZE_16BIT
#define SPI_DIRECTION_2LINES_RXONLY
#define SPI_DIRECTION_2LINES
#define SPI_DIRECTION_1LINE
#define HAL_SPI_ERROR_MODF
#define HAL_SPI_ERROR_FLAG
#define HAL_SPI_ERROR_OVR
#define HAL_SPI_ERROR_NONE
#define HAL_SPI_ERROR_DMA
#define HAL_SPI_ERROR_ABORT
#define HAL_SPI_ERROR_CRC
#define HAL_SPI_ERROR_FRE
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_SPI_StateTypeDef HAL_SPI_GetState(const SPI_HandleTypeDef *hspi)
uint32_t HAL_SPI_GetError(const SPI_HandleTypeDef *hspi)
#define __HAL_SPI_ENABLE(__HANDLE__)
Enable the SPI peripheral.
#define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the specified SPI interrupts.
#define __HAL_SPI_CLEAR_MODFFLAG(__HANDLE__)
Clear the SPI MODF pending flag.
#define __HAL_SPI_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the specified SPI interrupts.
#define __HAL_SPI_CLEAR_FREFLAG(__HANDLE__)
Clear the SPI FRE pending flag.
#define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__)
Check whether the specified SPI flag is set or not.
#define __HAL_SPI_DISABLE(__HANDLE__)
Disable the SPI peripheral.
#define __HAL_SPI_CLEAR_CRCERRFLAG(__HANDLE__)
Clear the SPI CRCERR pending flag.
#define __HAL_SPI_CLEAR_OVRFLAG(__HANDLE__)
Clear the SPI OVR pending flag.
HAL_SPI_StateTypeDef
HAL SPI State structure definition.
struct __SPI_HandleTypeDef SPI_HandleTypeDef
SPI handle Structure definition.
@ HAL_SPI_STATE_BUSY
@ HAL_SPI_STATE_ABORT
@ HAL_SPI_STATE_BUSY_TX
@ HAL_SPI_STATE_BUSY_TX_RX
@ HAL_SPI_STATE_READY
@ HAL_SPI_STATE_RESET
@ HAL_SPI_STATE_BUSY_RX
#define SPI_FLAG_FRE
#define SPI_FLAG_CRCERR
#define SPI_FLAG_RXNE
#define SPI_FLAG_OVR
#define SPI_FLAG_MODF
#define SPI_FLAG_BSY
#define SPI_FLAG_TXE
#define SPI_IT_RXNE
#define SPI_IT_TXE
#define SPI_IT_ERR
#define SPI_MODE_SLAVE
#define SPI_MODE_MASTER
#define IS_SPI_CRC_POLYNOMIAL(__POLYNOMIAL__)
Checks if SPI polynomial value to be used for the CRC calculation, is in allowed range.
#define IS_SPI_MODE(__MODE__)
Checks if SPI Mode parameter is in allowed range.
#define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__)
Checks if SPI Direction Mode parameter is 1 or 2 lines.
#define IS_SPI_CRC_CALCULATION(__CALCULATION__)
Checks if SPI CRC calculation enabled state is in allowed range.
#define IS_SPI_DIRECTION(__MODE__)
Checks if SPI Direction Mode parameter is in allowed range.
#define IS_SPI_FIRST_BIT(__BIT__)
Checks if SPI MSB LSB transmission parameter is in allowed range.
#define IS_SPI_NSS(__NSS__)
Checks if SPI Slave Select parameter is in allowed range.
#define SPI_CHECK_IT_SOURCE(__CR2__, __INTERRUPT__)
Check whether the specified SPI Interrupt is set or not.
#define IS_SPI_TIMODE(__MODE__)
Checks if SPI TI mode parameter is in allowed range.
#define SPI_1LINE_RX(__HANDLE__)
Set the SPI receive-only mode.
#define SPI_RESET_CRC(__HANDLE__)
Reset the CRC calculation of the SPI.
#define IS_SPI_DATASIZE(__DATASIZE__)
Checks if SPI Data Size parameter is in allowed range.
#define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__)
Checks if SPI Baudrate prescaler parameter is in allowed range.
#define IS_SPI_CPOL(__CPOL__)
Checks if SPI Serial clock steady state parameter is in allowed range.
#define SPI_1LINE_TX(__HANDLE__)
Set the SPI transmit-only mode.
#define IS_SPI_DMA_HANDLE(__HANDLE__)
Checks if DMA handle is valid.
#define SPI_CHECK_FLAG(__SR__, __FLAG__)
Check whether the specified SPI flag is set or not.
#define IS_SPI_CPHA(__CPHA__)
Checks if SPI Clock Phase parameter is in allowed range.
#define IS_SPI_DIRECTION_2LINES(__MODE__)
Checks if SPI Direction Mode parameter is 2 lines.
#define SPI_TIMODE_DISABLE
#define assert_param(expr)
This file contains all the functions prototypes for the HAL module driver.
#define HAL_IS_BIT_SET(REG, BIT)
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_TIMEOUT
@ HAL_ERROR
@ HAL_OK
@ HAL_BUSY
#define __HAL_UNLOCK(__HANDLE__)
#define HAL_MAX_DELAY
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
DMA_Stream_TypeDef * Instance
SPI handle Structure definition.
DMA_HandleTypeDef * hdmarx
volatile HAL_SPI_StateTypeDef State
volatile uint32_t ErrorCode
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
volatile uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
volatile uint16_t RxXferCount
DMA_HandleTypeDef * hdmatx
const uint8_t * pTxBuffPtr