STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_smartcard.c
Go to the documentation of this file.
1
175/* Includes ------------------------------------------------------------------*/
176#include "stm32f4xx_hal.h"
177
186#ifdef HAL_SMARTCARD_MODULE_ENABLED
187/* Private typedef -----------------------------------------------------------*/
188/* Private define ------------------------------------------------------------*/
196/* Private macro -------------------------------------------------------------*/
197/* Private variables ---------------------------------------------------------*/
198/* Private function prototypes -----------------------------------------------*/
202#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
203void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc);
204#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
213static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
219static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
224/* Exported functions --------------------------------------------------------*/
300{
301 /* Check the SMARTCARD handle allocation */
302 if(hsc == NULL)
303 {
304 return HAL_ERROR;
305 }
306
307 /* Check the parameters */
308 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
310
312 {
313 /* Allocate lock resource and initialize it */
314 hsc->Lock = HAL_UNLOCKED;
315
316#if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
317 SMARTCARD_InitCallbacksToDefault(hsc);
318
319 if (hsc->MspInitCallback == NULL)
320 {
321 hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
322 }
323
324 /* Init the low level hardware */
325 hsc->MspInitCallback(hsc);
326#else
327 /* Init the low level hardware : GPIO, CLOCK */
329#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
330 }
331
333
334 /* Set the Prescaler */
335 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
336
337 /* Set the Guard Time */
338 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
339
340 /* Set the Smartcard Communication parameters */
342
343 /* In SmartCard mode, the following bits must be kept cleared:
344 - LINEN bit in the USART_CR2 register
345 - HDSEL and IREN bits in the USART_CR3 register.*/
346 CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
347 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
348
349 /* Enable the SMARTCARD Parity Error Interrupt */
350 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
351
352 /* Enable the SMARTCARD Framing Error Interrupt */
353 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
354
355 /* Enable the Peripheral */
357
358 /* Configure the Smartcard NACK state */
359 MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
360
361 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
362 hsc->Instance->CR3 |= (USART_CR3_SCEN);
363
364 /* Initialize the SMARTCARD state*/
368
369 return HAL_OK;
370}
371
379{
380 /* Check the SMARTCARD handle allocation */
381 if(hsc == NULL)
382 {
383 return HAL_ERROR;
384 }
385
386 /* Check the parameters */
387 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
388
390
391 /* Disable the Peripheral */
393
394 /* DeInit the low level hardware */
395#if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
396 if (hsc->MspDeInitCallback == NULL)
397 {
398 hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
399 }
400 /* DeInit the low level hardware */
401 hsc->MspDeInitCallback(hsc);
402#else
404#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
405
409
410 /* Release Lock */
411 __HAL_UNLOCK(hsc);
412
413 return HAL_OK;
414}
415
423{
424 /* Prevent unused argument(s) compilation warning */
425 UNUSED(hsc);
426
427 /* NOTE : This function should not be modified, when the callback is needed,
428 the HAL_SMARTCARD_MspInit can be implemented in the user file
429 */
430}
431
439{
440 /* Prevent unused argument(s) compilation warning */
441 UNUSED(hsc);
442
443 /* NOTE : This function should not be modified, when the callback is needed,
444 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
445 */
446}
447
448#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
469HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
470{
471 HAL_StatusTypeDef status = HAL_OK;
472
473 if (pCallback == NULL)
474 {
475 /* Update the error code */
476 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
477
478 return HAL_ERROR;
479 }
480
482 {
483 switch (CallbackID)
484 {
485
486 case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
487 hsc->TxCpltCallback = pCallback;
488 break;
489
490 case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
491 hsc->RxCpltCallback = pCallback;
492 break;
493
494 case HAL_SMARTCARD_ERROR_CB_ID :
495 hsc->ErrorCallback = pCallback;
496 break;
497
498 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
499 hsc->AbortCpltCallback = pCallback;
500 break;
501
502 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
503 hsc->AbortTransmitCpltCallback = pCallback;
504 break;
505
506 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
507 hsc->AbortReceiveCpltCallback = pCallback;
508 break;
509
510
511 case HAL_SMARTCARD_MSPINIT_CB_ID :
512 hsc->MspInitCallback = pCallback;
513 break;
514
515 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
516 hsc->MspDeInitCallback = pCallback;
517 break;
518
519 default :
520 /* Update the error code */
521 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
522
523 /* Return error status */
524 status = HAL_ERROR;
525 break;
526 }
527 }
528 else if (hsc->gState == HAL_SMARTCARD_STATE_RESET)
529 {
530 switch (CallbackID)
531 {
532 case HAL_SMARTCARD_MSPINIT_CB_ID :
533 hsc->MspInitCallback = pCallback;
534 break;
535
536 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
537 hsc->MspDeInitCallback = pCallback;
538 break;
539
540 default :
541 /* Update the error code */
542 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
543
544 /* Return error status */
545 status = HAL_ERROR;
546 break;
547 }
548 }
549 else
550 {
551 /* Update the error code */
552 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
553
554 /* Return error status */
555 status = HAL_ERROR;
556 }
557
558 return status;
559}
560
580HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
581{
582 HAL_StatusTypeDef status = HAL_OK;
583
585 {
586 switch (CallbackID)
587 {
588 case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
589 hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
590 break;
591
592 case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
593 hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
594 break;
595
596 case HAL_SMARTCARD_ERROR_CB_ID :
597 hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
598 break;
599
600 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
601 hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
602 break;
603
604 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
605 hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
606 break;
607
608 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
609 hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
610 break;
611
612
613 case HAL_SMARTCARD_MSPINIT_CB_ID :
614 hsc->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */
615 break;
616
617 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
618 hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */
619 break;
620
621 default :
622 /* Update the error code */
623 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
624
625 /* Return error status */
626 status = HAL_ERROR;
627 break;
628 }
629 }
630 else if (HAL_SMARTCARD_STATE_RESET == hsc->gState)
631 {
632 switch (CallbackID)
633 {
634 case HAL_SMARTCARD_MSPINIT_CB_ID :
635 hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
636 break;
637
638 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
639 hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
640 break;
641
642 default :
643 /* Update the error code */
644 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
645
646 /* Return error status */
647 status = HAL_ERROR;
648 break;
649 }
650 }
651 else
652 {
653 /* Update the error code */
654 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
655
656 /* Return error status */
657 status = HAL_ERROR;
658 }
659
660 return status;
661}
662#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
663
754HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
755{
756 const uint8_t *tmp = pData;
757 uint32_t tickstart = 0U;
758
760 {
761 if((pData == NULL) || (Size == 0U))
762 {
763 return HAL_ERROR;
764 }
765
766 /* Process Locked */
767 __HAL_LOCK(hsc);
768
771
772 /* Init tickstart for timeout management */
773 tickstart = HAL_GetTick();
774
775 hsc->TxXferSize = Size;
776 hsc->TxXferCount = Size;
777 while(hsc->TxXferCount > 0U)
778 {
779 hsc->TxXferCount--;
780 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
781 {
782 return HAL_TIMEOUT;
783 }
784 hsc->Instance->DR = (uint8_t)(*tmp & 0xFFU);
785 tmp++;
786 }
787
788 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
789 {
790 return HAL_TIMEOUT;
791 }
792
793 /* At end of Tx process, restore hsc->gState to Ready */
795
796 /* Process Unlocked */
797 __HAL_UNLOCK(hsc);
798
799 return HAL_OK;
800 }
801 else
802 {
803 return HAL_BUSY;
804 }
805}
806
816HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
817{
818 uint8_t *tmp = pData;
819 uint32_t tickstart = 0U;
820
822 {
823 if((pData == NULL) || (Size == 0U))
824 {
825 return HAL_ERROR;
826 }
827
828 /* Process Locked */
829 __HAL_LOCK(hsc);
830
833
834 /* Init tickstart for timeout management */
835 tickstart = HAL_GetTick();
836
837 hsc->RxXferSize = Size;
838 hsc->RxXferCount = Size;
839
840 /* Check the remain data to be received */
841 while(hsc->RxXferCount > 0U)
842 {
843 hsc->RxXferCount--;
844 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
845 {
846 return HAL_TIMEOUT;
847 }
848 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
849 tmp++;
850 }
851
852 /* At end of Rx process, restore hsc->RxState to Ready */
854
855 /* Process Unlocked */
856 __HAL_UNLOCK(hsc);
857
858 return HAL_OK;
859 }
860 else
861 {
862 return HAL_BUSY;
863 }
864}
865
875{
876 /* Check that a Tx process is not already ongoing */
878 {
879 if((pData == NULL) || (Size == 0U))
880 {
881 return HAL_ERROR;
882 }
883
884 /* Process Locked */
885 __HAL_LOCK(hsc);
886
887 hsc->pTxBuffPtr = pData;
888 hsc->TxXferSize = Size;
889 hsc->TxXferCount = Size;
890
893
894 /* Process Unlocked */
895 __HAL_UNLOCK(hsc);
896
897 /* Enable the SMARTCARD Parity Error Interrupt */
898 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
899
900 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
901 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
902
903 /* Enable the SMARTCARD Transmit data register empty Interrupt */
904 SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
905
906 return HAL_OK;
907 }
908 else
909 {
910 return HAL_BUSY;
911 }
912}
913
923{
924 /* Check that a Rx process is not already ongoing */
926 {
927 if((pData == NULL) || (Size == 0U))
928 {
929 return HAL_ERROR;
930 }
931
932 /* Process Locked */
933 __HAL_LOCK(hsc);
934
935 hsc->pRxBuffPtr = pData;
936 hsc->RxXferSize = Size;
937 hsc->RxXferCount = Size;
938
941
942 /* Process Unlocked */
943 __HAL_UNLOCK(hsc);
944
945 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
946 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
947
948 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
949 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
950
951 return HAL_OK;
952 }
953 else
954 {
955 return HAL_BUSY;
956 }
957}
958
968{
969 const uint32_t *tmp;
970
971 /* Check that a Tx process is not already ongoing */
973 {
974 if((pData == NULL) || (Size == 0U))
975 {
976 return HAL_ERROR;
977 }
978
979 /* Process Locked */
980 __HAL_LOCK(hsc);
981
982 hsc->pTxBuffPtr = pData;
983 hsc->TxXferSize = Size;
984 hsc->TxXferCount = Size;
985
988
989 /* Set the SMARTCARD DMA transfer complete callback */
991
992 /* Set the DMA error callback */
994
995 /* Set the DMA abort callback */
996 hsc->hdmatx->XferAbortCallback = NULL;
997
998 /* Enable the SMARTCARD transmit DMA stream */
999 tmp = (const uint32_t*)&pData;
1000 HAL_DMA_Start_IT(hsc->hdmatx, *(const uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
1001
1002 /* Clear the TC flag in the SR register by writing 0 to it */
1004
1005 /* Process Unlocked */
1006 __HAL_UNLOCK(hsc);
1007
1008 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1009 in the SMARTCARD CR3 register */
1010 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1011
1012 return HAL_OK;
1013 }
1014 else
1015 {
1016 return HAL_BUSY;
1017 }
1018}
1019
1030{
1031 uint32_t *tmp;
1032
1033 /* Check that a Rx process is not already ongoing */
1035 {
1036 if((pData == NULL) || (Size == 0U))
1037 {
1038 return HAL_ERROR;
1039 }
1040
1041 /* Process Locked */
1042 __HAL_LOCK(hsc);
1043
1044 hsc->pRxBuffPtr = pData;
1045 hsc->RxXferSize = Size;
1046
1049
1050 /* Set the SMARTCARD DMA transfer complete callback */
1052
1053 /* Set the DMA error callback */
1055
1056 /* Set the DMA abort callback */
1057 hsc->hdmatx->XferAbortCallback = NULL;
1058
1059 /* Enable the DMA stream */
1060 tmp = (uint32_t*)&pData;
1061 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
1062
1063 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
1065
1066 /* Process Unlocked */
1067 __HAL_UNLOCK(hsc);
1068
1069 /* Enable the SMARTCARD Parity Error Interrupt */
1070 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
1071
1072 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1073 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1074
1075 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1076 in the SMARTCARD CR3 register */
1077 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1078
1079 return HAL_OK;
1080 }
1081 else
1082 {
1083 return HAL_BUSY;
1084 }
1085}
1086
1100{
1101 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1102 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1103 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1104
1105 /* Disable the SMARTCARD DMA Tx request if enabled */
1106 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1107 {
1108 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1109
1110 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1111 if(hsc->hdmatx != NULL)
1112 {
1113 /* Set the SMARTCARD DMA Abort callback to Null.
1114 No call back execution at end of DMA abort procedure */
1115 hsc->hdmatx->XferAbortCallback = NULL;
1116
1117 HAL_DMA_Abort(hsc->hdmatx);
1118 }
1119 }
1120
1121 /* Disable the SMARTCARD DMA Rx request if enabled */
1122 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1123 {
1124 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1125
1126 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1127 if(hsc->hdmarx != NULL)
1128 {
1129 /* Set the SMARTCARD DMA Abort callback to Null.
1130 No call back execution at end of DMA abort procedure */
1131 hsc->hdmarx->XferAbortCallback = NULL;
1132
1133 HAL_DMA_Abort(hsc->hdmarx);
1134 }
1135 }
1136
1137 /* Reset Tx and Rx transfer counters */
1138 hsc->TxXferCount = 0x00U;
1139 hsc->RxXferCount = 0x00U;
1140
1141 /* Reset ErrorCode */
1143
1144 /* Restore hsc->RxState and hsc->gState to Ready */
1147
1148 return HAL_OK;
1149}
1150
1164{
1165 /* Disable TXEIE and TCIE interrupts */
1166 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1167
1168 /* Disable the SMARTCARD DMA Tx request if enabled */
1169 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1170 {
1171 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1172
1173 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1174 if(hsc->hdmatx != NULL)
1175 {
1176 /* Set the SMARTCARD DMA Abort callback to Null.
1177 No call back execution at end of DMA abort procedure */
1178 hsc->hdmatx->XferAbortCallback = NULL;
1179
1180 HAL_DMA_Abort(hsc->hdmatx);
1181 }
1182 }
1183
1184 /* Reset Tx transfer counter */
1185 hsc->TxXferCount = 0x00U;
1186
1187 /* Restore hsc->gState to Ready */
1189
1190 return HAL_OK;
1191}
1192
1206{
1207 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1208 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1209 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1210
1211 /* Disable the SMARTCARD DMA Rx request if enabled */
1212 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1213 {
1214 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1215
1216 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1217 if(hsc->hdmarx != NULL)
1218 {
1219 /* Set the SMARTCARD DMA Abort callback to Null.
1220 No call back execution at end of DMA abort procedure */
1221 hsc->hdmarx->XferAbortCallback = NULL;
1222
1223 HAL_DMA_Abort(hsc->hdmarx);
1224 }
1225 }
1226
1227 /* Reset Rx transfer counter */
1228 hsc->RxXferCount = 0x00U;
1229
1230 /* Restore hsc->RxState to Ready */
1232
1233 return HAL_OK;
1234}
1235
1251{
1252 uint32_t AbortCplt = 0x01U;
1253
1254 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1255 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1256 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1257
1258 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1259 before any call to DMA Abort functions */
1260 /* DMA Tx Handle is valid */
1261 if(hsc->hdmatx != NULL)
1262 {
1263 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1264 Otherwise, set it to NULL */
1265 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1266 {
1268 }
1269 else
1270 {
1271 hsc->hdmatx->XferAbortCallback = NULL;
1272 }
1273 }
1274 /* DMA Rx Handle is valid */
1275 if(hsc->hdmarx != NULL)
1276 {
1277 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1278 Otherwise, set it to NULL */
1279 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1280 {
1282 }
1283 else
1284 {
1285 hsc->hdmarx->XferAbortCallback = NULL;
1286 }
1287 }
1288
1289 /* Disable the SMARTCARD DMA Tx request if enabled */
1290 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1291 {
1292 /* Disable DMA Tx at SMARTCARD level */
1293 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1294
1295 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1296 if(hsc->hdmatx != NULL)
1297 {
1298 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1299 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1300
1301 /* Abort DMA TX */
1302 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
1303 {
1304 hsc->hdmatx->XferAbortCallback = NULL;
1305 }
1306 else
1307 {
1308 AbortCplt = 0x00U;
1309 }
1310 }
1311 }
1312
1313 /* Disable the SMARTCARD DMA Rx request if enabled */
1314 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1315 {
1316 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1317
1318 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1319 if(hsc->hdmarx != NULL)
1320 {
1321 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1322 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1323
1324 /* Abort DMA RX */
1325 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1326 {
1327 hsc->hdmarx->XferAbortCallback = NULL;
1328 AbortCplt = 0x01U;
1329 }
1330 else
1331 {
1332 AbortCplt = 0x00U;
1333 }
1334 }
1335 }
1336
1337 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1338 if(AbortCplt == 0x01U)
1339 {
1340 /* Reset Tx and Rx transfer counters */
1341 hsc->TxXferCount = 0x00U;
1342 hsc->RxXferCount = 0x00U;
1343
1344 /* Reset ErrorCode */
1346
1347 /* Restore hsc->gState and hsc->RxState to Ready */
1350
1351 /* As no DMA to be aborted, call directly user Abort complete callback */
1352#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1353 /* Call registered Abort complete callback */
1354 hsc->AbortCpltCallback(hsc);
1355#else
1356 /* Call legacy weak Abort complete callback */
1358#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1359 }
1360 return HAL_OK;
1361}
1362
1378{
1379 /* Disable TXEIE and TCIE interrupts */
1380 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1381
1382 /* Disable the SMARTCARD DMA Tx request if enabled */
1383 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1384 {
1385 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1386
1387 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1388 if(hsc->hdmatx != NULL)
1389 {
1390 /* Set the SMARTCARD DMA Abort callback :
1391 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1393
1394 /* Abort DMA TX */
1395 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
1396 {
1397 /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
1398 hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
1399 }
1400 }
1401 else
1402 {
1403 /* Reset Tx transfer counter */
1404 hsc->TxXferCount = 0x00U;
1405
1406 /* Restore hsc->gState to Ready */
1408
1409 /* As no DMA to be aborted, call directly user Abort complete callback */
1410#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1411 /* Call registered Abort Transmit Complete Callback */
1412 hsc->AbortTransmitCpltCallback(hsc);
1413#else
1414 /* Call legacy weak Abort Transmit Complete Callback */
1416#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1417 }
1418 }
1419 else
1420 {
1421 /* Reset Tx transfer counter */
1422 hsc->TxXferCount = 0x00U;
1423
1424 /* Restore hsc->gState to Ready */
1426
1427 /* As no DMA to be aborted, call directly user Abort complete callback */
1428#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1429 /* Call registered Abort Transmit Complete Callback */
1430 hsc->AbortTransmitCpltCallback(hsc);
1431#else
1432 /* Call legacy weak Abort Transmit Complete Callback */
1434#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1435 }
1436
1437 return HAL_OK;
1438}
1439
1455{
1456 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1457 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1458 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1459
1460 /* Disable the SMARTCARD DMA Rx request if enabled */
1461 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1462 {
1463 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1464
1465 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1466 if(hsc->hdmarx != NULL)
1467 {
1468 /* Set the SMARTCARD DMA Abort callback :
1469 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1471
1472 /* Abort DMA RX */
1473 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1474 {
1475 /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
1476 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
1477 }
1478 }
1479 else
1480 {
1481 /* Reset Rx transfer counter */
1482 hsc->RxXferCount = 0x00U;
1483
1484 /* Restore hsc->RxState to Ready */
1486
1487 /* As no DMA to be aborted, call directly user Abort complete callback */
1488#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1489 /* Call registered Abort Receive Complete Callback */
1490 hsc->AbortReceiveCpltCallback(hsc);
1491#else
1492 /* Call legacy weak Abort Receive Complete Callback */
1494#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1495 }
1496 }
1497 else
1498 {
1499 /* Reset Rx transfer counter */
1500 hsc->RxXferCount = 0x00U;
1501
1502 /* Restore hsc->RxState to Ready */
1504
1505 /* As no DMA to be aborted, call directly user Abort complete callback */
1506#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1507 /* Call registered Abort Receive Complete Callback */
1508 hsc->AbortReceiveCpltCallback(hsc);
1509#else
1510 /* Call legacy weak Abort Receive Complete Callback */
1512#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1513 }
1514
1515 return HAL_OK;
1516}
1517
1525{
1526 uint32_t isrflags = READ_REG(hsc->Instance->SR);
1527 uint32_t cr1its = READ_REG(hsc->Instance->CR1);
1528 uint32_t cr3its = READ_REG(hsc->Instance->CR3);
1529 uint32_t dmarequest = 0x00U;
1530 uint32_t errorflags = 0x00U;
1531
1532 /* If no error occurs */
1533 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1534 if(errorflags == RESET)
1535 {
1536 /* SMARTCARD in mode Receiver -------------------------------------------------*/
1537 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1538 {
1540 return;
1541 }
1542 }
1543
1544 /* If some errors occur */
1545 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1546 {
1547 /* SMARTCARD parity error interrupt occurred ---------------------------*/
1548 if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1549 {
1551 }
1552
1553 /* SMARTCARD frame error interrupt occurred ----------------------------*/
1554 if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1555 {
1557 }
1558
1559 /* SMARTCARD noise error interrupt occurred ----------------------------*/
1560 if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1561 {
1563 }
1564
1565 /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
1566 if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
1567 {
1569 }
1570 /* Call the Error call Back in case of Errors --------------------------*/
1572 {
1573 /* SMARTCARD in mode Receiver ----------------------------------------*/
1574 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1575 {
1577 }
1578
1579 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1580 consider error as blocking */
1581 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
1582 if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
1583 {
1584 /* Blocking error : transfer is aborted
1585 Set the SMARTCARD state ready to be able to start again the process,
1586 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1588 /* Disable the SMARTCARD DMA Rx request if enabled */
1589 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1590 {
1591 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1592
1593 /* Abort the SMARTCARD DMA Rx channel */
1594 if(hsc->hdmarx != NULL)
1595 {
1596 /* Set the SMARTCARD DMA Abort callback :
1597 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1599
1600 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1601 {
1602 /* Call Directly XferAbortCallback function in case of error */
1603 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
1604 }
1605 }
1606 else
1607 {
1608#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1609 /* Call registered user error callback */
1610 hsc->ErrorCallback(hsc);
1611#else
1612 /* Call legacy weak user error callback */
1614#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1615 }
1616 }
1617 else
1618 {
1619#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1620 /* Call registered user error callback */
1621 hsc->ErrorCallback(hsc);
1622#else
1623 /* Call legacy weak user error callback */
1625#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1626 }
1627 }
1628 else
1629 {
1630 /* Non Blocking error : transfer could go on.
1631 Error is notified to user through user error callback */
1632#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1633 /* Call registered user error callback */
1634 hsc->ErrorCallback(hsc);
1635#else
1636 /* Call legacy weak user error callback */
1638#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1640 }
1641 }
1642 return;
1643 } /* End if some error occurs */
1644
1645 /* SMARTCARD in mode Transmitter ------------------------------------------*/
1646 if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1647 {
1649 return;
1650 }
1651
1652 /* SMARTCARD in mode Transmitter (transmission end) -----------------------*/
1653 if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1654 {
1656 return;
1657 }
1658}
1659
1667{
1668 /* Prevent unused argument(s) compilation warning */
1669 UNUSED(hsc);
1670
1671 /* NOTE : This function should not be modified, when the callback is needed,
1672 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
1673 */
1674}
1675
1683{
1684 /* Prevent unused argument(s) compilation warning */
1685 UNUSED(hsc);
1686
1687 /* NOTE : This function should not be modified, when the callback is needed,
1688 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
1689 */
1690}
1691
1699{
1700 /* Prevent unused argument(s) compilation warning */
1701 UNUSED(hsc);
1702
1703 /* NOTE : This function should not be modified, when the callback is needed,
1704 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
1705 */
1706}
1707
1714{
1715 /* Prevent unused argument(s) compilation warning */
1716 UNUSED(hsc);
1717
1718 /* NOTE : This function should not be modified, when the callback is needed,
1719 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
1720 */
1721}
1722
1729{
1730 /* Prevent unused argument(s) compilation warning */
1731 UNUSED(hsc);
1732
1733 /* NOTE : This function should not be modified, when the callback is needed,
1734 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
1735 */
1736}
1737
1744{
1745 /* Prevent unused argument(s) compilation warning */
1746 UNUSED(hsc);
1747
1748 /* NOTE : This function should not be modified, when the callback is needed,
1749 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
1750 */
1751}
1752
1779{
1780 uint32_t temp1= 0x00U, temp2 = 0x00U;
1781 temp1 = hsc->gState;
1782 temp2 = hsc->RxState;
1783
1784 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
1785}
1786
1794{
1795 return hsc->ErrorCode;
1796}
1797
1810#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1816void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc)
1817{
1818 /* Init the SMARTCARD Callback settings */
1819 hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
1820 hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
1821 hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
1822 hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
1823 hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
1824 hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
1825
1826}
1827#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
1828
1836{
1838
1839 hsc->TxXferCount = 0U;
1840
1841 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1842 in the USART CR3 register */
1843 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1844
1845 /* Enable the SMARTCARD Transmit Complete Interrupt */
1846 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
1847}
1848
1856{
1858
1859 hsc->RxXferCount = 0U;
1860
1861 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1862 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1863 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1864
1865 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1866 in the USART CR3 register */
1867 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1868
1869 /* At end of Rx process, restore hsc->RxState to Ready */
1871
1872#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1873 /* Call registered Rx complete callback */
1874 hsc->RxCpltCallback(hsc);
1875#else
1876 /* Call legacy weak Rx complete callback */
1878#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1879}
1880
1888{
1889 uint32_t dmarequest = 0x00U;
1891 hsc->RxXferCount = 0U;
1892 hsc->TxXferCount = 0U;
1894
1895 /* Stop SMARTCARD DMA Tx request if ongoing */
1896 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
1897 if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
1898 {
1900 }
1901
1902 /* Stop SMARTCARD DMA Rx request if ongoing */
1903 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
1904 if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
1905 {
1907 }
1908
1909#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1910 /* Call registered user error callback */
1911 hsc->ErrorCallback(hsc);
1912#else
1913 /* Call legacy weak user error callback */
1915#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1916}
1917
1929static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1930{
1931 /* Wait until flag is set */
1932 while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
1933 {
1934 /* Check for the Timeout */
1935 if(Timeout != HAL_MAX_DELAY)
1936 {
1937 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
1938 {
1939 /* Disable TXE and RXNE interrupts for the interrupt process */
1940 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
1941 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
1942
1945
1946 /* Process Unlocked */
1947 __HAL_UNLOCK(hsc);
1948
1949 return HAL_TIMEOUT;
1950 }
1951 }
1952 }
1953 return HAL_OK;
1954}
1955
1963{
1964 /* At end of Tx process, restore hsc->gState to Ready */
1966
1967 /* Disable TXEIE and TCIE interrupts */
1968 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1969}
1970
1971
1979{
1980 /* At end of Rx process, restore hsc->RxState to Ready */
1982
1983 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1984 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1985 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1986}
1987
1995{
1996
1997 /* Check that a Tx process is ongoing */
1999 {
2000 hsc->Instance->DR = (uint8_t)(*hsc->pTxBuffPtr & 0xFFU);
2001 hsc->pTxBuffPtr++;
2002
2003 if(--hsc->TxXferCount == 0U)
2004 {
2005 /* Disable the SMARTCARD Transmit data register empty Interrupt */
2006 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
2007
2008 /* Enable the SMARTCARD Transmit Complete Interrupt */
2009 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
2010 }
2011
2012 return HAL_OK;
2013 }
2014 else
2015 {
2016 return HAL_BUSY;
2017 }
2018}
2019
2027{
2028 /* Disable the SMARTCARD Transmit Complete Interrupt */
2029 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
2030
2031 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2032 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
2033
2034 /* Tx process is ended, restore hsc->gState to Ready */
2036
2037#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2038 /* Call registered Tx complete callback */
2039 hsc->TxCpltCallback(hsc);
2040#else
2041 /* Call legacy weak Tx complete callback */
2043#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2044
2045 return HAL_OK;
2046}
2047
2055{
2056
2057 /* Check that a Rx process is ongoing */
2059 {
2060 *hsc->pRxBuffPtr = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
2061 hsc->pRxBuffPtr++;
2062
2063 if(--hsc->RxXferCount == 0U)
2064 {
2065 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
2066
2067 /* Disable the SMARTCARD Parity Error Interrupt */
2068 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
2069
2070 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2071 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
2072
2073 /* Rx process is completed, restore hsc->RxState to Ready */
2075
2076#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2077 /* Call registered Rx complete callback */
2078 hsc->RxCpltCallback(hsc);
2079#else
2080 /* Call legacy weak Rx complete callback */
2082#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2083
2084 return HAL_OK;
2085 }
2086 return HAL_OK;
2087 }
2088 else
2089 {
2090 return HAL_BUSY;
2091 }
2092}
2093
2101{
2103 hsc->RxXferCount = 0x00U;
2104 hsc->TxXferCount = 0x00U;
2105
2106#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2107 /* Call registered user error callback */
2108 hsc->ErrorCallback(hsc);
2109#else
2110 /* Call legacy weak user error callback */
2112#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2113}
2114
2124{
2126
2127 hsc->hdmatx->XferAbortCallback = NULL;
2128
2129 /* Check if an Abort process is still ongoing */
2130 if(hsc->hdmarx != NULL)
2131 {
2132 if(hsc->hdmarx->XferAbortCallback != NULL)
2133 {
2134 return;
2135 }
2136 }
2137
2138 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2139 hsc->TxXferCount = 0x00U;
2140 hsc->RxXferCount = 0x00U;
2141
2142 /* Reset ErrorCode */
2144
2145 /* Restore hsc->gState and hsc->RxState to Ready */
2148
2149#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2150 /* Call registered Abort complete callback */
2151 hsc->AbortCpltCallback(hsc);
2152#else
2153 /* Call legacy weak Abort complete callback */
2155#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2156}
2157
2167{
2169
2170 hsc->hdmarx->XferAbortCallback = NULL;
2171
2172 /* Check if an Abort process is still ongoing */
2173 if(hsc->hdmatx != NULL)
2174 {
2175 if(hsc->hdmatx->XferAbortCallback != NULL)
2176 {
2177 return;
2178 }
2179 }
2180
2181 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2182 hsc->TxXferCount = 0x00U;
2183 hsc->RxXferCount = 0x00U;
2184
2185 /* Reset ErrorCode */
2187
2188 /* Restore hsc->gState and hsc->RxState to Ready */
2191
2192#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2193 /* Call registered Abort complete callback */
2194 hsc->AbortCpltCallback(hsc);
2195#else
2196 /* Call legacy weak Abort complete callback */
2198#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2199}
2200
2210{
2212
2213 hsc->TxXferCount = 0x00U;
2214
2215 /* Restore hsc->gState to Ready */
2217
2218#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2219 /* Call registered Abort Transmit Complete Callback */
2220 hsc->AbortTransmitCpltCallback(hsc);
2221#else
2222 /* Call legacy weak Abort Transmit Complete Callback */
2224#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2225}
2226
2236{
2238
2239 hsc->RxXferCount = 0x00U;
2240
2241 /* Restore hsc->RxState to Ready */
2243
2244#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2245 /* Call registered Abort Receive Complete Callback */
2246 hsc->AbortReceiveCpltCallback(hsc);
2247#else
2248 /* Call legacy weak Abort Receive Complete Callback */
2250#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2251}
2252
2260{
2261 uint32_t tmpreg = 0x00U;
2262 uint32_t pclk;
2263
2264 /* Check the parameters */
2265 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
2275
2276 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
2277 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
2278 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2279
2280 /*---------------------------- USART CR2 Configuration ---------------------*/
2281 tmpreg = hsc->Instance->CR2;
2282 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
2283 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
2284 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
2285 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
2286 /* Set CPHA bit according to hsc->Init.CLKPhase value */
2287 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
2288 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
2289 tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
2290 hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
2291 /* Write to USART CR2 */
2292 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
2293
2294 tmpreg = hsc->Instance->CR2;
2295
2296 /* Clear STOP[13:12] bits */
2297 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
2298
2299 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
2300 tmpreg |= (uint32_t)(hsc->Init.StopBits);
2301
2302 /* Write to USART CR2 */
2303 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
2304
2305 /*-------------------------- USART CR1 Configuration -----------------------*/
2306 tmpreg = hsc->Instance->CR1;
2307
2308 /* Clear M, PCE, PS, TE and RE bits */
2309 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
2310 USART_CR1_RE));
2311
2312 /* Configure the SMARTCARD Word Length, Parity and mode:
2313 Set the M bits according to hsc->Init.WordLength value
2314 Set PCE and PS bits according to hsc->Init.Parity value
2315 Set TE and RE bits according to hsc->Init.Mode value */
2316 tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
2317
2318 /* Write to USART CR1 */
2319 WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
2320
2321 /*-------------------------- USART CR3 Configuration -----------------------*/
2322 /* Clear CTSE and RTSE bits */
2323 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
2324
2325 /*-------------------------- USART BRR Configuration -----------------------*/
2326#if defined(USART6)
2327 if((hsc->Instance == USART1) || (hsc->Instance == USART6))
2328 {
2329 pclk = HAL_RCC_GetPCLK2Freq();
2330 hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
2331 }
2332#else
2333 if(hsc->Instance == USART1)
2334 {
2335 pclk = HAL_RCC_GetPCLK2Freq();
2336 hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
2337 }
2338#endif /* USART6 */
2339 else
2340 {
2341 pclk = HAL_RCC_GetPCLK1Freq();
2342 hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
2343 }
2344}
2345
2350#endif /* HAL_SMARTCARD_MODULE_ENABLED */
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer.
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer in Interrupt mode.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
uint32_t HAL_RCC_GetPCLK1Freq(void)
Returns the PCLK1 frequency.
uint32_t HAL_RCC_GetPCLK2Freq(void)
Returns the PCLK2 frequency.
#define HAL_SMARTCARD_ERROR_NONE
#define HAL_SMARTCARD_ERROR_NE
#define HAL_SMARTCARD_ERROR_DMA
#define HAL_SMARTCARD_ERROR_ORE
#define HAL_SMARTCARD_ERROR_FE
#define HAL_SMARTCARD_ERROR_PE
void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
SMARTCARD MSP Init.
HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
DeInitializes the USART SmartCard peripheral.
HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
Initializes the SmartCard mode according to the specified parameters in the SMARTCARD_InitTypeDef and...
void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
SMARTCARD MSP DeInit.
HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
Abort ongoing Transmit transfer (Interrupt mode).
HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
Abort ongoing Receive transfer (Interrupt mode).
void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsc)
SMARTCARD Abort Complete callback.
HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size)
Send an amount of data in non blocking mode.
void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
SMARTCARD error callback.
HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size)
Send an amount of data in non blocking mode.
HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
Abort ongoing Transmit transfer (blocking mode).
void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
Tx Transfer completed callbacks.
HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
Receive an amount of data in non blocking mode.
void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
This function handles SMARTCARD interrupt request.
HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
Receive an amount of data in blocking mode.
void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsc)
SMARTCARD Abort Receive Complete callback.
void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsc)
SMARTCARD Abort Transmit Complete callback.
HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
Receive an amount of data in non blocking mode.
void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
Rx Transfer completed callback.
HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
Abort ongoing transfers (Interrupt mode).
HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
Send an amount of data in blocking mode.
HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
Abort ongoing transfers (blocking mode).
HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
Abort ongoing Receive transfer (blocking mode).
uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsc)
Return the SMARTCARD error code.
HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsc)
Return the SMARTCARD handle state.
#define __HAL_SMARTCARD_GET_FLAG(__HANDLE__, __FLAG__)
Check whether the specified Smartcard flag is set or not.
#define __HAL_SMARTCARD_CLEAR_OREFLAG(__HANDLE__)
Clear the SMARTCARD ORE pending flag.
#define __HAL_SMARTCARD_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the specified Smartcard pending flags.
#define __HAL_SMARTCARD_ENABLE(__HANDLE__)
Enable the USART associated to the SMARTCARD Handle.
#define __HAL_SMARTCARD_DISABLE(__HANDLE__)
Disable the USART associated to the SMARTCARD Handle.
HAL_SMARTCARD_StateTypeDef
HAL SMARTCARD State structures definition.
@ HAL_SMARTCARD_STATE_BUSY
@ HAL_SMARTCARD_STATE_RESET
@ HAL_SMARTCARD_STATE_READY
@ HAL_SMARTCARD_STATE_BUSY_TX
@ HAL_SMARTCARD_STATE_BUSY_RX
static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
DMA SMARTCARD communication abort callback, when initiated by HAL services on Error (To be called at ...
static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
This function handles SMARTCARD Communication Timeout. It waits until a flag is no longer in the spec...
static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
DMA SMARTCARD Tx communication abort callback, when initiated by user (To be called at end of DMA Tx ...
static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
Configure the SMARTCARD peripheral.
static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
Send an amount of data in non blocking mode.
static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
DMA SMARTCARD communication error callback.
static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to HAL_SMARTCARD_Abor...
static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
Receive an amount of data in non blocking mode.
static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SMARTCARD receive process complete callback.
static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to HAL_SMARTCARD_Abor...
static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
Wraps up transmission in non blocking mode.
static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
DMA SMARTCARD transmit process complete callback.
static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
DMA SMARTCARD Rx communication abort callback, when initiated by user (To be called at end of DMA Rx ...
#define IS_SMARTCARD_MODE(MODE)
#define IS_SMARTCARD_NACK_STATE(NACK)
#define IS_SMARTCARD_STOPBITS(STOPBITS)
#define SMARTCARD_BRR(__PCLK__, __BAUD__)
#define IS_SMARTCARD_LASTBIT(LASTBIT)
#define IS_SMARTCARD_BAUDRATE(BAUDRATE)
#define IS_SMARTCARD_PARITY(PARITY)
#define IS_SMARTCARD_WORD_LENGTH(LENGTH)
#define IS_SMARTCARD_POLARITY(CPOL)
#define IS_SMARTCARD_PHASE(CPHA)
#define SMARTCARD_FLAG_NE
#define SMARTCARD_FLAG_PE
#define SMARTCARD_FLAG_RXNE
#define SMARTCARD_FLAG_ORE
#define SMARTCARD_FLAG_TXE
#define SMARTCARD_FLAG_TC
#define SMARTCARD_FLAG_FE
#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 UNUSED(X)
#define __HAL_UNLOCK(__HANDLE__)
#define HAL_MAX_DELAY
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
DMA handle Structure definition.
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
SMARTCARD handle Structure definition.
__IO HAL_SMARTCARD_StateTypeDef gState
__IO HAL_SMARTCARD_StateTypeDef RxState