diff --git a/app/hal.h b/app/hal.h index ead2af9..d21e365 100644 --- a/app/hal.h +++ b/app/hal.h @@ -63,7 +63,6 @@ typedef enum { hal_err_t hal_uart_send(hal_uart_port_t port, const uint8_t* data, size_t len); - // Crypto (only use in crypto library) hal_err_t hal_rng_next(uint8_t *buf, size_t len); @@ -79,4 +78,7 @@ hal_err_t hal_crc32_update(hal_crc32_ctx_t* ctx, const uint8_t* data, size_t len hal_err_t hal_crc32_finish(hal_crc32_ctx_t* ctx, uint32_t *out); #endif +// Timer +hal_err_t hal_delay_us(uint32_t usec); + #endif diff --git a/app/iso7816/smartcard.c b/app/iso7816/smartcard.c index eba13a5..06751ef 100644 --- a/app/iso7816/smartcard.c +++ b/app/iso7816/smartcard.c @@ -4,6 +4,7 @@ #include "iso7816/t1.h" #include "FreeRTOS.h" #include "task.h" +#include "hal.h" #define SC_RESET_DELAY 400 #define SC_DEFAULT_ETU10NS 9300 @@ -17,11 +18,10 @@ static inline void SmartCard_State_Reset(SmartCard* sc) { void SmartCard_Delay(SmartCard* sc, uint32_t etu) { uint32_t usec = (sc->etu_10ns * etu)/100; - if (usec & 0xffff0000) { + if (usec > 4000) { vTaskDelay(pdMS_TO_TICKS(usec/1000)); } else { - //sc->usec_timer->Instance->CNT = 0; - //while (sc->usec_timer->Instance->CNT < usec); + hal_delay_us(usec); } } @@ -138,38 +138,4 @@ uint8_t SmartCard_Send_APDU(SmartCard* sc, APDU* apdu) { return 0; } } -/* -void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc) { - uint32_t error = HAL_SMARTCARD_GetError(hsc); - if(error & HAL_SMARTCARD_ERROR_FE) { - __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); - } - - if(error & HAL_SMARTCARD_ERROR_PE) { - __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE); - __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); - } - - if(error & HAL_SMARTCARD_ERROR_NE) { - __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); - } - - if(error & HAL_SMARTCARD_ERROR_ORE) { - __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); - } - - if(error & HAL_SMARTCARD_ERROR_RTO) { - __HAL_SMARTCARD_FLUSH_DRREGISTER(hsc); - __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RTO); - } - -} - -void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc) { - -} - -void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc) { - __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RTO); -}*/ diff --git a/nxp/source/nxp.c b/nxp/source/nxp.c index f288aa5..8c3bfe7 100644 --- a/nxp/source/nxp.c +++ b/nxp/source/nxp.c @@ -42,6 +42,7 @@ #include "fsl_trng.h" #include "fsl_lpuart.h" #include "fsl_dcp.h" +#include "fsl_gpt.h" #include "hal.h" struct gpio_pin_spec { @@ -77,6 +78,14 @@ hal_err_t hal_init(void) { sha256_handle.channel = kDCP_Channel0; + gpt_config_t gptCfg; + GPT_GetDefaultConfig(&gptCfg); + gptCfg.clockSource = kGPT_ClockSource_Osc; + gptCfg.enableFreeRun = true; + gptCfg.enableMode = true; + gptCfg.divider = 24; + GPT_Init(GPT1, &gptCfg); + return HAL_OK; } @@ -112,3 +121,9 @@ hal_err_t hal_uart_send(hal_uart_port_t port, const uint8_t* data, size_t len) { return LPUART_WriteBlocking(BOARD_DEBUG_UART_BASEADDR, data, len) == kStatus_Success ? HAL_OK : HAL_ERROR; } +hal_err_t hal_delay_us(uint32_t usec) { + GPT_StartTimer(GPT1); + while(GPT_GetCurrentTimerCount(GPT1) < usec) {} + GPT_StopTimer(GPT1); + return HAL_OK; +}