From 1c58de24f52833e2725f2c5b9b81dfdacfd46936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=8F?= Date: Wed, 28 Jul 2021 11:45:42 +0300 Subject: [PATCH] Nfc: switch to HAL ticks. ApiHal: small cleanup and refactoring. (#609) --- applications/nfc/nfc_cli.c | 2 -- applications/nfc/nfc_worker.c | 8 +------- applications/nfc/nfc_worker.h | 2 -- applications/nfc/nfc_worker_i.h | 1 - firmware/targets/api-hal-include/api-hal-nfc.h | 2 +- firmware/targets/f6/api-hal/api-hal-boot.c | 2 ++ firmware/targets/f6/api-hal/api-hal-console.c | 4 ++++ firmware/targets/f6/api-hal/api-hal-delay.c | 1 + firmware/targets/f6/api-hal/api-hal-i2c.c | 1 + firmware/targets/f6/api-hal/api-hal-interrupt.c | 1 + firmware/targets/f6/api-hal/api-hal-light.c | 1 + firmware/targets/f6/api-hal/api-hal-nfc.c | 16 +++++++--------- firmware/targets/f6/api-hal/api-hal-os.c | 5 +++-- firmware/targets/f6/api-hal/api-hal-power.c | 3 +++ firmware/targets/f6/api-hal/api-hal-spi.c | 1 + firmware/targets/f6/api-hal/api-hal-subghz.c | 1 + firmware/targets/f6/api-hal/api-hal-vcp.c | 1 + firmware/targets/f6/api-hal/api-hal-version.c | 1 + firmware/targets/f6/api-hal/api-hal-vibro.c | 2 ++ firmware/targets/f6/api-hal/api-hal.c | 14 +------------- lib/ST25RFAL002/timer.c | 8 ++++---- 21 files changed, 36 insertions(+), 41 deletions(-) diff --git a/applications/nfc/nfc_cli.c b/applications/nfc/nfc_cli.c index 7656bd317..6349cde4c 100755 --- a/applications/nfc/nfc_cli.c +++ b/applications/nfc/nfc_cli.c @@ -19,7 +19,6 @@ void nfc_cli_detect(Cli* cli, string_t args, void* context) { rfalNfcDevice* dev_list; uint8_t dev_cnt = 0; bool cmd_exit = false; - api_hal_nfc_init(); api_hal_nfc_exit_sleep(); printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n"); while(!cmd_exit) { @@ -51,7 +50,6 @@ void nfc_cli_emulate(Cli* cli, string_t args, void* context) { return; } - api_hal_nfc_init(); api_hal_nfc_exit_sleep(); printf("Emulating NFC-A Type: T2T UID: CF72D440 SAK: 20 ATQA: 00/04\r\n"); printf("Press Ctrl+C to abort\r\n"); diff --git a/applications/nfc/nfc_worker.c b/applications/nfc/nfc_worker.c index 0e0003d6f..e536620ef 100755 --- a/applications/nfc/nfc_worker.c +++ b/applications/nfc/nfc_worker.c @@ -15,9 +15,7 @@ NfcWorker* nfc_worker_alloc() { nfc_worker->callback = NULL; nfc_worker->context = NULL; // Initialize rfal - nfc_worker->error = api_hal_nfc_init(); - if(nfc_worker->error == ERR_NONE) { - api_hal_nfc_start_sleep(); + if(!api_hal_nfc_is_busy()) { nfc_worker_change_state(nfc_worker, NfcWorkerStateReady); } else { nfc_worker_change_state(nfc_worker, NfcWorkerStateBroken); @@ -35,10 +33,6 @@ NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker) { return nfc_worker->state; } -ReturnCode nfc_worker_get_error(NfcWorker* nfc_worker) { - return nfc_worker->error; -} - void nfc_worker_start( NfcWorker* nfc_worker, NfcWorkerState state, diff --git a/applications/nfc/nfc_worker.h b/applications/nfc/nfc_worker.h index 7109e5f88..4719cb21b 100755 --- a/applications/nfc/nfc_worker.h +++ b/applications/nfc/nfc_worker.h @@ -28,8 +28,6 @@ NfcWorker* nfc_worker_alloc(); NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker); -ReturnCode nfc_worker_get_error(NfcWorker* nfc_worker); - void nfc_worker_free(NfcWorker* nfc_worker); void nfc_worker_start( diff --git a/applications/nfc/nfc_worker_i.h b/applications/nfc/nfc_worker_i.h index 00d5e4549..9df5707d2 100755 --- a/applications/nfc/nfc_worker_i.h +++ b/applications/nfc/nfc_worker_i.h @@ -26,7 +26,6 @@ struct NfcWorker { void* context; NfcWorkerState state; - ReturnCode error; }; void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state); diff --git a/firmware/targets/api-hal-include/api-hal-nfc.h b/firmware/targets/api-hal-include/api-hal-nfc.h index 75d8c98ae..dda13e3bf 100644 --- a/firmware/targets/api-hal-include/api-hal-nfc.h +++ b/firmware/targets/api-hal-include/api-hal-nfc.h @@ -14,7 +14,7 @@ extern "C" { /** * Init nfc */ -ReturnCode api_hal_nfc_init(); +void api_hal_nfc_init(); /** * Check if nfc worker is busy diff --git a/firmware/targets/f6/api-hal/api-hal-boot.c b/firmware/targets/f6/api-hal/api-hal-boot.c index 320e2b6b4..5a868a930 100644 --- a/firmware/targets/f6/api-hal/api-hal-boot.c +++ b/firmware/targets/f6/api-hal/api-hal-boot.c @@ -1,5 +1,6 @@ #include #include +#include // Boot request enum #define BOOT_REQUEST_TAINTED 0x00000000 @@ -10,6 +11,7 @@ void api_hal_boot_init() { #ifndef DEBUG LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED); #endif + FURI_LOG_I("FuriHalBoot", "Init OK"); } void api_hal_boot_set_mode(ApiHalBootMode mode) { diff --git a/firmware/targets/f6/api-hal/api-hal-console.c b/firmware/targets/f6/api-hal/api-hal-console.c index 6438e6730..7fbfa17a0 100644 --- a/firmware/targets/f6/api-hal/api-hal-console.c +++ b/firmware/targets/f6/api-hal/api-hal-console.c @@ -4,6 +4,8 @@ #include #include +#include + volatile bool api_hal_console_alive = false; void api_hal_console_init() { @@ -34,6 +36,8 @@ void api_hal_console_init() { while(!LL_USART_IsActiveFlag_TEACK(USART1)) ; api_hal_console_alive = true; + + FURI_LOG_I("FuriHalConsole", "Init OK"); } void api_hal_console_tx(const uint8_t* buffer, size_t buffer_size) { diff --git a/firmware/targets/f6/api-hal/api-hal-delay.c b/firmware/targets/f6/api-hal/api-hal-delay.c index f4cd80f7e..bb33fb0e3 100644 --- a/firmware/targets/f6/api-hal/api-hal-delay.c +++ b/firmware/targets/f6/api-hal/api-hal-delay.c @@ -10,6 +10,7 @@ void api_hal_delay_init(void) { DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; DWT->CYCCNT = 0U; clk_per_microsecond = SystemCoreClock / 1000000.0f; + FURI_LOG_I("FuriHalDelay", "Init OK"); } void delay_us(float microseconds) { diff --git a/firmware/targets/f6/api-hal/api-hal-i2c.c b/firmware/targets/f6/api-hal/api-hal-i2c.c index dc9dd87f6..110a5d26a 100644 --- a/firmware/targets/f6/api-hal/api-hal-i2c.c +++ b/firmware/targets/f6/api-hal/api-hal-i2c.c @@ -42,6 +42,7 @@ void api_hal_i2c_init() { LL_I2C_DisableOwnAddress2(POWER_I2C); LL_I2C_DisableGeneralCall(POWER_I2C); LL_I2C_EnableClockStretching(POWER_I2C); + FURI_LOG_I("FuriHalI2C", "Init OK"); } bool api_hal_i2c_tx( diff --git a/firmware/targets/f6/api-hal/api-hal-interrupt.c b/firmware/targets/f6/api-hal/api-hal-interrupt.c index cbaf67fd8..ce8bd5798 100644 --- a/firmware/targets/f6/api-hal/api-hal-interrupt.c +++ b/firmware/targets/f6/api-hal/api-hal-interrupt.c @@ -14,6 +14,7 @@ volatile ApiHalInterruptISR api_hal_dma_channel_isr[API_HAL_INTERRUPT_DMA_COUNT] void api_hal_interrupt_init() { NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0)); NVIC_EnableIRQ(DMA1_Channel1_IRQn); + FURI_LOG_I("FuriHalInterrupt", "Init OK"); } void api_hal_interrupt_set_timer_isr(TIM_TypeDef* timer, ApiHalInterruptISR isr) { diff --git a/firmware/targets/f6/api-hal/api-hal-light.c b/firmware/targets/f6/api-hal/api-hal-light.c index b284d4df6..b01a9ab9d 100644 --- a/firmware/targets/f6/api-hal/api-hal-light.c +++ b/firmware/targets/f6/api-hal/api-hal-light.c @@ -21,6 +21,7 @@ void api_hal_light_init() { lp5562_enable(); lp5562_configure(); + FURI_LOG_I("FuriHalLight", "Init OK"); } void api_hal_light_set(Light light, uint8_t value) { diff --git a/firmware/targets/f6/api-hal/api-hal-nfc.c b/firmware/targets/f6/api-hal/api-hal-nfc.c index 813b6db26..56f675f60 100644 --- a/firmware/targets/f6/api-hal/api-hal-nfc.c +++ b/firmware/targets/f6/api-hal/api-hal-nfc.c @@ -3,20 +3,18 @@ static const uint32_t clocks_in_ms = 64 * 1000; -ReturnCode api_hal_nfc_init() { - // Check if Nfc worker was started - rfalNfcState state = rfalNfcGetState(); - if(state == RFAL_NFC_STATE_NOTINIT) { - return rfalNfcInitialize(); - } else if(state == RFAL_NFC_STATE_IDLE) { - return ERR_NONE; +void api_hal_nfc_init() { + ReturnCode ret = rfalNfcInitialize(); + if(ret == ERR_NONE) { + api_hal_nfc_start_sleep(); + FURI_LOG_I("FuriHalNfc", "Init OK"); } else { - return ERR_BUSY; + FURI_LOG_W("FuriHalNfc", "Initialization failed, RFAL returned: %d", ret); } } bool api_hal_nfc_is_busy() { - return rfalNfcGetState() > RFAL_NFC_STATE_IDLE; + return rfalNfcGetState() != RFAL_NFC_STATE_IDLE; } void api_hal_nfc_field_on() { diff --git a/firmware/targets/f6/api-hal/api-hal-os.c b/firmware/targets/f6/api-hal/api-hal-os.c index 3016cc519..2f4d83634 100644 --- a/firmware/targets/f6/api-hal/api-hal-os.c +++ b/firmware/targets/f6/api-hal/api-hal-os.c @@ -3,8 +3,7 @@ #include #include -#include -#include +#include #define API_HAL_OS_CLK_FREQUENCY 32768 #define API_HAL_OS_TICK_PER_SECOND 1024 @@ -42,6 +41,8 @@ void api_hal_os_init() { osTimerId_t second_timer = osTimerNew(api_hal_os_timer_callback, osTimerPeriodic, NULL, NULL); osTimerStart(second_timer, 1024); #endif + + FURI_LOG_I("FuriHalOs", "Init OK"); } void LPTIM2_IRQHandler(void) { diff --git a/firmware/targets/f6/api-hal/api-hal-power.c b/firmware/targets/f6/api-hal/api-hal-power.c index 90e5af8a6..24533e735 100644 --- a/firmware/targets/f6/api-hal/api-hal-power.c +++ b/firmware/targets/f6/api-hal/api-hal-power.c @@ -13,6 +13,8 @@ #include #include +#include + typedef struct { volatile uint32_t insomnia; volatile uint32_t deep_insomnia; @@ -70,6 +72,7 @@ void api_hal_power_init() { LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN); bq27220_init(&cedv); bq25896_init(); + FURI_LOG_I("FuriHalPower", "Init OK"); } uint16_t api_hal_power_insomnia_level() { diff --git a/firmware/targets/f6/api-hal/api-hal-spi.c b/firmware/targets/f6/api-hal/api-hal-spi.c index 6991e3a61..cffdc5911 100644 --- a/firmware/targets/f6/api-hal/api-hal-spi.c +++ b/firmware/targets/f6/api-hal/api-hal-spi.c @@ -22,6 +22,7 @@ void api_hal_spi_init() { GpioSpeedVeryHigh ); } + FURI_LOG_I("FuriHalSpi", "Init OK"); } void api_hal_spi_bus_lock(const ApiHalSpiBus* bus) { diff --git a/firmware/targets/f6/api-hal/api-hal-subghz.c b/firmware/targets/f6/api-hal/api-hal-subghz.c index 3b11c7f8b..5ccde68fb 100644 --- a/firmware/targets/f6/api-hal/api-hal-subghz.c +++ b/firmware/targets/f6/api-hal/api-hal-subghz.c @@ -114,6 +114,7 @@ void api_hal_subghz_init() { cc1101_shutdown(device); api_hal_spi_device_return(device); + FURI_LOG_I("FuriHalSubGhz", "Init OK"); } void api_hal_subghz_sleep() { diff --git a/firmware/targets/f6/api-hal/api-hal-vcp.c b/firmware/targets/f6/api-hal/api-hal-vcp.c index 1f77460e2..296ee1e8a 100644 --- a/firmware/targets/f6/api-hal/api-hal-vcp.c +++ b/firmware/targets/f6/api-hal/api-hal-vcp.c @@ -29,6 +29,7 @@ void api_hal_vcp_init() { api_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL); api_hal_vcp->alive = false; api_hal_vcp->underrun = false; + FURI_LOG_I("FuriHalVcp", "Init OK"); } void _api_hal_vcp_init() { diff --git a/firmware/targets/f6/api-hal/api-hal-version.c b/firmware/targets/f6/api-hal/api-hal-version.c index 2b965be59..761f57e16 100644 --- a/firmware/targets/f6/api-hal/api-hal-version.c +++ b/firmware/targets/f6/api-hal/api-hal-version.c @@ -165,6 +165,7 @@ void api_hal_version_init() { break; default: furi_check(0); } + FURI_LOG_I("FuriHalVersion", "Init OK"); } bool api_hal_version_do_i_belong_here() { diff --git a/firmware/targets/f6/api-hal/api-hal-vibro.c b/firmware/targets/f6/api-hal/api-hal-vibro.c index d418aaccf..71102f7fe 100644 --- a/firmware/targets/f6/api-hal/api-hal-vibro.c +++ b/firmware/targets/f6/api-hal/api-hal-vibro.c @@ -4,6 +4,8 @@ void api_hal_vibro_init() { hal_gpio_init(&vibro_gpio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow); hal_gpio_write(&vibro_gpio, false); + FURI_LOG_I("FuriHalVibro", "Init OK"); + } void api_hal_vibro_on(bool value) { diff --git a/firmware/targets/f6/api-hal/api-hal.c b/firmware/targets/f6/api-hal/api-hal.c index 98836881e..d5edd11f8 100644 --- a/firmware/targets/f6/api-hal/api-hal.c +++ b/firmware/targets/f6/api-hal/api-hal.c @@ -16,11 +16,8 @@ void api_hal_init() { api_hal_clock_init(); api_hal_console_init(); - FURI_LOG_I("FURI_HAL", "CLOCK and CONSOLE OK"); api_hal_interrupt_init(); - FURI_LOG_I("FURI_HAL", "INTERRUPT OK"); api_hal_delay_init(); - FURI_LOG_I("FURI_HAL", "DELAY OK"); MX_GPIO_Init(); FURI_LOG_I("HAL", "GPIO OK"); @@ -28,9 +25,7 @@ void api_hal_init() { MX_RTC_Init(); FURI_LOG_I("HAL", "RTC OK"); api_hal_boot_init(); - FURI_LOG_I("FURI_HAL", "BOOT OK"); api_hal_version_init(); - FURI_LOG_I("FURI_HAL", "VERSION OK"); MX_ADC1_Init(); FURI_LOG_I("HAL", "ADC1 OK"); @@ -40,7 +35,6 @@ void api_hal_init() { MX_SPI2_Init(); FURI_LOG_I("HAL", "SPI2 OK"); api_hal_spi_init(); - FURI_LOG_I("FURI_HAL", "SPI OK"); MX_TIM1_Init(); FURI_LOG_I("HAL", "TIM1 OK"); @@ -65,24 +59,18 @@ void api_hal_init() { // VCP + USB api_hal_vcp_init(); - FURI_LOG_I("FURI_HAL", "VCP OK"); MX_USB_Device_Init(); FURI_LOG_I("HAL", "USB OK"); api_hal_i2c_init(); - FURI_LOG_I("FURI_HAL", "I2C OK"); // High Level api_hal_power_init(); - FURI_LOG_I("FURI_HAL", "POWER OK"); api_hal_light_init(); - FURI_LOG_I("FURI_HAL", "LIGHT OK"); api_hal_vibro_init(); - FURI_LOG_I("FURI_HAL", "VIBRO OK"); api_hal_subghz_init(); - FURI_LOG_I("FURI_HAL", "SUBGHZ OK"); + api_hal_nfc_init(); // FreeRTOS glue api_hal_os_init(); - FURI_LOG_I("FURI_HAL", "OS OK"); } diff --git a/lib/ST25RFAL002/timer.c b/lib/ST25RFAL002/timer.c index 29ecab90d..ed230a423 100644 --- a/lib/ST25RFAL002/timer.c +++ b/lib/ST25RFAL002/timer.c @@ -67,7 +67,7 @@ static uint32_t timerStopwatchTick; /*******************************************************************************/ uint32_t timerCalculateTimer( uint16_t time ) { - return (osKernelGetTickCount() + time); + return (HAL_GetTick() + time); } @@ -77,7 +77,7 @@ bool timerIsExpired( uint32_t timer ) uint32_t uDiff; int32_t sDiff; - uDiff = (timer - osKernelGetTickCount()); /* Calculate the diff between the timers */ + uDiff = (timer - HAL_GetTick()); /* Calculate the diff between the timers */ sDiff = uDiff; /* Convert the diff to a signed var */ /* Check if the given timer has expired already */ @@ -104,13 +104,13 @@ void timerDelay( uint16_t tOut ) /*******************************************************************************/ void timerStopwatchStart( void ) { - timerStopwatchTick = osKernelGetTickCount(); + timerStopwatchTick = HAL_GetTick(); } /*******************************************************************************/ uint32_t timerStopwatchMeasure( void ) { - return (uint32_t)(osKernelGetTickCount() - timerStopwatchTick); + return (uint32_t)(HAL_GetTick() - timerStopwatchTick); }