/** * @brief Initializes the SmartCard mode according to the specified * parameters in the SMARTCARD_InitTypeDef and create the associated handle . * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains * the configuration information for SMARTCARD module. * @retval HAL status */ HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc) { /* Check the SMARTCARD handle allocation */ if(hsc == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance)); assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState)); if(hsc->State == HAL_SMARTCARD_STATE_RESET) { /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ HAL_SMARTCARD_MspInit(hsc); } hsc->State = HAL_SMARTCARD_STATE_BUSY; /* Set the Prescaler */ MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler); /* Set the Guard Time */ MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8)); /* Set the Smartcard Communication parameters */ SMARTCARD_SetConfig(hsc); /* In SmartCard mode, the following bits must be kept cleared: - LINEN bit in the USART_CR2 register - HDSEL and IREN bits in the USART_CR3 register.*/ hsc->Instance->CR2 &= ~USART_CR2_LINEN; hsc->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_HDSEL); /* Enable the SMARTCARD Parity Error Interrupt */ __SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE); /* Enable the SMARTCARD Framing Error Interrupt */ __SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR); /* Enable the Peripharal */ __SMARTCARD_ENABLE(hsc); /* Configure the Smartcard NACK state */ MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState); /* Enable the SC mode by setting the SCEN bit in the CR3 register */ hsc->Instance->CR3 |= (USART_CR3_SCEN); /* Initialize the SMARTCARD state*/ hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; hsc->State= HAL_SMARTCARD_STATE_READY; return HAL_OK; }
/** * @brief Receive an amount of data in non blocking mode * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains * the configuration information for SMARTCARD module. * @param pData: pointer to data buffer * @param Size: amount of data to be received * @retval HAL status */ HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size) { uint32_t tmp1 = 0; tmp1 = hsc->State; if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX)) { if((pData == NULL) || (Size == 0)) { return HAL_ERROR; } /* Process Locked */ __HAL_LOCK(hsc); hsc->pRxBuffPtr = pData; hsc->RxXferSize = Size; hsc->RxXferCount = Size; hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; /* Check if a non-blocking transmit process is ongoing or not */ if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) { hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; } else { hsc->State = HAL_SMARTCARD_STATE_BUSY_RX; } /* Enable the SMARTCARD Data Register not empty Interrupt */ __SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE); /* Enable the SMARTCARD Parity Error Interrupt */ __SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE); /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ __SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR); /* Process Unlocked */ __HAL_UNLOCK(hsc); return HAL_OK; } else { return HAL_BUSY; } }
/** * @brief SMARTCARD error callbacks * @param hsc: usart handle * @retval None */ void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc) { if(HAL_SMARTCARD_GetError(hsc) & HAL_SMARTCARD_ERROR_FE) { __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); /* Resend the byte that failed to be received (by the Smartcard) correctly */ SC_ParityErrorHandler(); } if(HAL_SMARTCARD_GetError(hsc) & HAL_SMARTCARD_ERROR_PE) { /* Enable SC_USART RXNE Interrupt (until receiving the corrupted byte) */ __SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE); /* Flush the SC_USART DR register */ __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); } if(HAL_SMARTCARD_GetError(hsc) & HAL_SMARTCARD_ERROR_NE) { __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); } if(HAL_SMARTCARD_GetError(hsc) & HAL_SMARTCARD_ERROR_ORE) { __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); } }