Nfc: switch to HAL ticks. ApiHal: small cleanup and refactoring. (#609)

This commit is contained in:
あく 2021-07-28 11:45:42 +03:00 committed by GitHub
parent 4c1ac2a13d
commit 1c58de24f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 36 additions and 41 deletions

View file

@ -19,7 +19,6 @@ void nfc_cli_detect(Cli* cli, string_t args, void* context) {
rfalNfcDevice* dev_list; rfalNfcDevice* dev_list;
uint8_t dev_cnt = 0; uint8_t dev_cnt = 0;
bool cmd_exit = false; bool cmd_exit = false;
api_hal_nfc_init();
api_hal_nfc_exit_sleep(); api_hal_nfc_exit_sleep();
printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n"); printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
while(!cmd_exit) { while(!cmd_exit) {
@ -51,7 +50,6 @@ void nfc_cli_emulate(Cli* cli, string_t args, void* context) {
return; return;
} }
api_hal_nfc_init();
api_hal_nfc_exit_sleep(); api_hal_nfc_exit_sleep();
printf("Emulating NFC-A Type: T2T UID: CF72D440 SAK: 20 ATQA: 00/04\r\n"); printf("Emulating NFC-A Type: T2T UID: CF72D440 SAK: 20 ATQA: 00/04\r\n");
printf("Press Ctrl+C to abort\r\n"); printf("Press Ctrl+C to abort\r\n");

View file

@ -15,9 +15,7 @@ NfcWorker* nfc_worker_alloc() {
nfc_worker->callback = NULL; nfc_worker->callback = NULL;
nfc_worker->context = NULL; nfc_worker->context = NULL;
// Initialize rfal // Initialize rfal
nfc_worker->error = api_hal_nfc_init(); if(!api_hal_nfc_is_busy()) {
if(nfc_worker->error == ERR_NONE) {
api_hal_nfc_start_sleep();
nfc_worker_change_state(nfc_worker, NfcWorkerStateReady); nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
} else { } else {
nfc_worker_change_state(nfc_worker, NfcWorkerStateBroken); nfc_worker_change_state(nfc_worker, NfcWorkerStateBroken);
@ -35,10 +33,6 @@ NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker) {
return nfc_worker->state; return nfc_worker->state;
} }
ReturnCode nfc_worker_get_error(NfcWorker* nfc_worker) {
return nfc_worker->error;
}
void nfc_worker_start( void nfc_worker_start(
NfcWorker* nfc_worker, NfcWorker* nfc_worker,
NfcWorkerState state, NfcWorkerState state,

View file

@ -28,8 +28,6 @@ NfcWorker* nfc_worker_alloc();
NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker); 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_free(NfcWorker* nfc_worker);
void nfc_worker_start( void nfc_worker_start(

View file

@ -26,7 +26,6 @@ struct NfcWorker {
void* context; void* context;
NfcWorkerState state; NfcWorkerState state;
ReturnCode error;
}; };
void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state); void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state);

View file

@ -14,7 +14,7 @@ extern "C" {
/** /**
* Init nfc * Init nfc
*/ */
ReturnCode api_hal_nfc_init(); void api_hal_nfc_init();
/** /**
* Check if nfc worker is busy * Check if nfc worker is busy

View file

@ -1,5 +1,6 @@
#include <api-hal-boot.h> #include <api-hal-boot.h>
#include <stm32wbxx_ll_rtc.h> #include <stm32wbxx_ll_rtc.h>
#include <furi.h>
// Boot request enum // Boot request enum
#define BOOT_REQUEST_TAINTED 0x00000000 #define BOOT_REQUEST_TAINTED 0x00000000
@ -10,6 +11,7 @@ void api_hal_boot_init() {
#ifndef DEBUG #ifndef DEBUG
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED); LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
#endif #endif
FURI_LOG_I("FuriHalBoot", "Init OK");
} }
void api_hal_boot_set_mode(ApiHalBootMode mode) { void api_hal_boot_set_mode(ApiHalBootMode mode) {

View file

@ -4,6 +4,8 @@
#include <stm32wbxx_ll_gpio.h> #include <stm32wbxx_ll_gpio.h>
#include <stm32wbxx_ll_usart.h> #include <stm32wbxx_ll_usart.h>
#include <furi.h>
volatile bool api_hal_console_alive = false; volatile bool api_hal_console_alive = false;
void api_hal_console_init() { void api_hal_console_init() {
@ -34,6 +36,8 @@ void api_hal_console_init() {
while(!LL_USART_IsActiveFlag_TEACK(USART1)) ; while(!LL_USART_IsActiveFlag_TEACK(USART1)) ;
api_hal_console_alive = true; api_hal_console_alive = true;
FURI_LOG_I("FuriHalConsole", "Init OK");
} }
void api_hal_console_tx(const uint8_t* buffer, size_t buffer_size) { void api_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {

View file

@ -10,6 +10,7 @@ void api_hal_delay_init(void) {
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
DWT->CYCCNT = 0U; DWT->CYCCNT = 0U;
clk_per_microsecond = SystemCoreClock / 1000000.0f; clk_per_microsecond = SystemCoreClock / 1000000.0f;
FURI_LOG_I("FuriHalDelay", "Init OK");
} }
void delay_us(float microseconds) { void delay_us(float microseconds) {

View file

@ -42,6 +42,7 @@ void api_hal_i2c_init() {
LL_I2C_DisableOwnAddress2(POWER_I2C); LL_I2C_DisableOwnAddress2(POWER_I2C);
LL_I2C_DisableGeneralCall(POWER_I2C); LL_I2C_DisableGeneralCall(POWER_I2C);
LL_I2C_EnableClockStretching(POWER_I2C); LL_I2C_EnableClockStretching(POWER_I2C);
FURI_LOG_I("FuriHalI2C", "Init OK");
} }
bool api_hal_i2c_tx( bool api_hal_i2c_tx(

View file

@ -14,6 +14,7 @@ volatile ApiHalInterruptISR api_hal_dma_channel_isr[API_HAL_INTERRUPT_DMA_COUNT]
void api_hal_interrupt_init() { void api_hal_interrupt_init() {
NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0)); NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
NVIC_EnableIRQ(DMA1_Channel1_IRQn); NVIC_EnableIRQ(DMA1_Channel1_IRQn);
FURI_LOG_I("FuriHalInterrupt", "Init OK");
} }
void api_hal_interrupt_set_timer_isr(TIM_TypeDef* timer, ApiHalInterruptISR isr) { void api_hal_interrupt_set_timer_isr(TIM_TypeDef* timer, ApiHalInterruptISR isr) {

View file

@ -21,6 +21,7 @@ void api_hal_light_init() {
lp5562_enable(); lp5562_enable();
lp5562_configure(); lp5562_configure();
FURI_LOG_I("FuriHalLight", "Init OK");
} }
void api_hal_light_set(Light light, uint8_t value) { void api_hal_light_set(Light light, uint8_t value) {

View file

@ -3,20 +3,18 @@
static const uint32_t clocks_in_ms = 64 * 1000; static const uint32_t clocks_in_ms = 64 * 1000;
ReturnCode api_hal_nfc_init() { void api_hal_nfc_init() {
// Check if Nfc worker was started ReturnCode ret = rfalNfcInitialize();
rfalNfcState state = rfalNfcGetState(); if(ret == ERR_NONE) {
if(state == RFAL_NFC_STATE_NOTINIT) { api_hal_nfc_start_sleep();
return rfalNfcInitialize(); FURI_LOG_I("FuriHalNfc", "Init OK");
} else if(state == RFAL_NFC_STATE_IDLE) {
return ERR_NONE;
} else { } else {
return ERR_BUSY; FURI_LOG_W("FuriHalNfc", "Initialization failed, RFAL returned: %d", ret);
} }
} }
bool api_hal_nfc_is_busy() { bool api_hal_nfc_is_busy() {
return rfalNfcGetState() > RFAL_NFC_STATE_IDLE; return rfalNfcGetState() != RFAL_NFC_STATE_IDLE;
} }
void api_hal_nfc_field_on() { void api_hal_nfc_field_on() {

View file

@ -3,8 +3,7 @@
#include <api-hal-power.h> #include <api-hal-power.h>
#include <stm32wbxx_ll_cortex.h> #include <stm32wbxx_ll_cortex.h>
#include <FreeRTOS.h> #include <furi.h>
#include <cmsis_os.h>
#define API_HAL_OS_CLK_FREQUENCY 32768 #define API_HAL_OS_CLK_FREQUENCY 32768
#define API_HAL_OS_TICK_PER_SECOND 1024 #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); osTimerId_t second_timer = osTimerNew(api_hal_os_timer_callback, osTimerPeriodic, NULL, NULL);
osTimerStart(second_timer, 1024); osTimerStart(second_timer, 1024);
#endif #endif
FURI_LOG_I("FuriHalOs", "Init OK");
} }
void LPTIM2_IRQHandler(void) { void LPTIM2_IRQHandler(void) {

View file

@ -13,6 +13,8 @@
#include <bq27220.h> #include <bq27220.h>
#include <bq25896.h> #include <bq25896.h>
#include <furi.h>
typedef struct { typedef struct {
volatile uint32_t insomnia; volatile uint32_t insomnia;
volatile uint32_t deep_insomnia; volatile uint32_t deep_insomnia;
@ -70,6 +72,7 @@ void api_hal_power_init() {
LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN); LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
bq27220_init(&cedv); bq27220_init(&cedv);
bq25896_init(); bq25896_init();
FURI_LOG_I("FuriHalPower", "Init OK");
} }
uint16_t api_hal_power_insomnia_level() { uint16_t api_hal_power_insomnia_level() {

View file

@ -22,6 +22,7 @@ void api_hal_spi_init() {
GpioSpeedVeryHigh GpioSpeedVeryHigh
); );
} }
FURI_LOG_I("FuriHalSpi", "Init OK");
} }
void api_hal_spi_bus_lock(const ApiHalSpiBus* bus) { void api_hal_spi_bus_lock(const ApiHalSpiBus* bus) {

View file

@ -114,6 +114,7 @@ void api_hal_subghz_init() {
cc1101_shutdown(device); cc1101_shutdown(device);
api_hal_spi_device_return(device); api_hal_spi_device_return(device);
FURI_LOG_I("FuriHalSubGhz", "Init OK");
} }
void api_hal_subghz_sleep() { void api_hal_subghz_sleep() {

View file

@ -29,6 +29,7 @@ void api_hal_vcp_init() {
api_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL); api_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
api_hal_vcp->alive = false; api_hal_vcp->alive = false;
api_hal_vcp->underrun = false; api_hal_vcp->underrun = false;
FURI_LOG_I("FuriHalVcp", "Init OK");
} }
void _api_hal_vcp_init() { void _api_hal_vcp_init() {

View file

@ -165,6 +165,7 @@ void api_hal_version_init() {
break; break;
default: furi_check(0); default: furi_check(0);
} }
FURI_LOG_I("FuriHalVersion", "Init OK");
} }
bool api_hal_version_do_i_belong_here() { bool api_hal_version_do_i_belong_here() {

View file

@ -4,6 +4,8 @@
void api_hal_vibro_init() { void api_hal_vibro_init() {
hal_gpio_init(&vibro_gpio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow); hal_gpio_init(&vibro_gpio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
hal_gpio_write(&vibro_gpio, false); hal_gpio_write(&vibro_gpio, false);
FURI_LOG_I("FuriHalVibro", "Init OK");
} }
void api_hal_vibro_on(bool value) { void api_hal_vibro_on(bool value) {

View file

@ -16,11 +16,8 @@
void api_hal_init() { void api_hal_init() {
api_hal_clock_init(); api_hal_clock_init();
api_hal_console_init(); api_hal_console_init();
FURI_LOG_I("FURI_HAL", "CLOCK and CONSOLE OK");
api_hal_interrupt_init(); api_hal_interrupt_init();
FURI_LOG_I("FURI_HAL", "INTERRUPT OK");
api_hal_delay_init(); api_hal_delay_init();
FURI_LOG_I("FURI_HAL", "DELAY OK");
MX_GPIO_Init(); MX_GPIO_Init();
FURI_LOG_I("HAL", "GPIO OK"); FURI_LOG_I("HAL", "GPIO OK");
@ -28,9 +25,7 @@ void api_hal_init() {
MX_RTC_Init(); MX_RTC_Init();
FURI_LOG_I("HAL", "RTC OK"); FURI_LOG_I("HAL", "RTC OK");
api_hal_boot_init(); api_hal_boot_init();
FURI_LOG_I("FURI_HAL", "BOOT OK");
api_hal_version_init(); api_hal_version_init();
FURI_LOG_I("FURI_HAL", "VERSION OK");
MX_ADC1_Init(); MX_ADC1_Init();
FURI_LOG_I("HAL", "ADC1 OK"); FURI_LOG_I("HAL", "ADC1 OK");
@ -40,7 +35,6 @@ void api_hal_init() {
MX_SPI2_Init(); MX_SPI2_Init();
FURI_LOG_I("HAL", "SPI2 OK"); FURI_LOG_I("HAL", "SPI2 OK");
api_hal_spi_init(); api_hal_spi_init();
FURI_LOG_I("FURI_HAL", "SPI OK");
MX_TIM1_Init(); MX_TIM1_Init();
FURI_LOG_I("HAL", "TIM1 OK"); FURI_LOG_I("HAL", "TIM1 OK");
@ -65,24 +59,18 @@ void api_hal_init() {
// VCP + USB // VCP + USB
api_hal_vcp_init(); api_hal_vcp_init();
FURI_LOG_I("FURI_HAL", "VCP OK");
MX_USB_Device_Init(); MX_USB_Device_Init();
FURI_LOG_I("HAL", "USB OK"); FURI_LOG_I("HAL", "USB OK");
api_hal_i2c_init(); api_hal_i2c_init();
FURI_LOG_I("FURI_HAL", "I2C OK");
// High Level // High Level
api_hal_power_init(); api_hal_power_init();
FURI_LOG_I("FURI_HAL", "POWER OK");
api_hal_light_init(); api_hal_light_init();
FURI_LOG_I("FURI_HAL", "LIGHT OK");
api_hal_vibro_init(); api_hal_vibro_init();
FURI_LOG_I("FURI_HAL", "VIBRO OK");
api_hal_subghz_init(); api_hal_subghz_init();
FURI_LOG_I("FURI_HAL", "SUBGHZ OK"); api_hal_nfc_init();
// FreeRTOS glue // FreeRTOS glue
api_hal_os_init(); api_hal_os_init();
FURI_LOG_I("FURI_HAL", "OS OK");
} }

View file

@ -67,7 +67,7 @@ static uint32_t timerStopwatchTick;
/*******************************************************************************/ /*******************************************************************************/
uint32_t timerCalculateTimer( uint16_t time ) 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; uint32_t uDiff;
int32_t sDiff; 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 */ sDiff = uDiff; /* Convert the diff to a signed var */
/* Check if the given timer has expired already */ /* Check if the given timer has expired already */
@ -104,13 +104,13 @@ void timerDelay( uint16_t tOut )
/*******************************************************************************/ /*******************************************************************************/
void timerStopwatchStart( void ) void timerStopwatchStart( void )
{ {
timerStopwatchTick = osKernelGetTickCount(); timerStopwatchTick = HAL_GetTick();
} }
/*******************************************************************************/ /*******************************************************************************/
uint32_t timerStopwatchMeasure( void ) uint32_t timerStopwatchMeasure( void )
{ {
return (uint32_t)(osKernelGetTickCount() - timerStopwatchTick); return (uint32_t)(HAL_GetTick() - timerStopwatchTick);
} }