From ba0419276e26ba6bf32688e0bf0af4b3c544dd1a Mon Sep 17 00:00:00 2001 From: SG Date: Tue, 18 May 2021 19:54:48 +1000 Subject: [PATCH] [FL-1311] F6, app LF-RFID: Updated timer api and config (#465) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * F6, app LF-RFID: Updated timer api and config * Api hal rfid: updated comments Co-authored-by: あく --- .../lf-rfid/helpers/rfid-timer-emulator.cpp | 6 +- .../targets/api-hal-include/api-hal-rfid.h | 23 +++++ firmware/targets/f5/api-hal/api-hal-rfid.c | 98 ++++++++++++------- firmware/targets/f6/api-hal/api-hal-rfid.c | 98 ++++++++++++------- 4 files changed, 156 insertions(+), 69 deletions(-) diff --git a/applications/lf-rfid/helpers/rfid-timer-emulator.cpp b/applications/lf-rfid/helpers/rfid-timer-emulator.cpp index 988af65c..cbe6a511 100644 --- a/applications/lf-rfid/helpers/rfid-timer-emulator.cpp +++ b/applications/lf-rfid/helpers/rfid-timer-emulator.cpp @@ -64,7 +64,7 @@ void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) { RfidTimerEmulator* _this = static_cast(ctx); TIM_HandleTypeDef* hw = static_cast(_hw); - if(hw == &LFRFID_TIM) { + if(api_hal_rfid_is_tim_emulate(hw)) { bool result; bool polarity; uint16_t period; @@ -77,7 +77,7 @@ void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) { _this->pulse_joiner.pop_pulse(&period, &pulse); - hw->Instance->ARR = period - 1; - hw->Instance->CCR1 = pulse; + api_hal_rfid_set_emulate_period(period - 1); + api_hal_rfid_set_emulate_pulse(pulse); } } diff --git a/firmware/targets/api-hal-include/api-hal-rfid.h b/firmware/targets/api-hal-include/api-hal-rfid.h index 16de695b..a95a153d 100644 --- a/firmware/targets/api-hal-include/api-hal-rfid.h +++ b/firmware/targets/api-hal-include/api-hal-rfid.h @@ -1,5 +1,7 @@ #pragma once #include +#include +#include #ifdef __cplusplus extern "C" { @@ -68,6 +70,27 @@ void api_hal_rfid_tim_emulate_stop(); */ void api_hal_rfid_tim_reset(); +/** + * @brief check that timer instance is emulation timer + * + * @param hw timer instance + */ +bool api_hal_rfid_is_tim_emulate(TIM_HandleTypeDef* hw); + +/** + * @brief set emulation timer period + * + * @param period overall duration + */ +void api_hal_rfid_set_emulate_period(uint32_t period); + +/** + * @brief set emulation timer pulse + * + * @param pulse duration of high level + */ +void api_hal_rfid_set_emulate_pulse(uint32_t pulse); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/firmware/targets/f5/api-hal/api-hal-rfid.c b/firmware/targets/f5/api-hal/api-hal-rfid.c index 84d7619e..e2520992 100644 --- a/firmware/targets/f5/api-hal/api-hal-rfid.c +++ b/firmware/targets/f5/api-hal/api-hal-rfid.c @@ -2,6 +2,11 @@ #include #include +#define LFRFID_READ_TIM htim1 +#define LFRFID_READ_CHANNEL TIM_CHANNEL_1 +#define LFRFID_EMULATE_TIM htim1 +#define LFRFID_EMULATE_CHANNEL TIM_CHANNEL_1 + void api_hal_rfid_pins_reset() { // ibutton bus disable api_hal_ibutton_stop(); @@ -60,21 +65,20 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) { TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; // basic PWM setup with needed freq and internal clock - LFRFID_TIM.Instance = TIM1; - LFRFID_TIM.Init.Prescaler = 0; - LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; - LFRFID_TIM.Init.Period = period; - LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - LFRFID_TIM.Init.RepetitionCounter = 0; - LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) { + LFRFID_READ_TIM.Init.Prescaler = 0; + LFRFID_READ_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; + LFRFID_READ_TIM.Init.Period = period; + LFRFID_READ_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + LFRFID_READ_TIM.Init.RepetitionCounter = 0; + LFRFID_READ_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if(HAL_TIM_Base_Init(&LFRFID_READ_TIM) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; - if(HAL_TIM_ConfigClockSource(&LFRFID_TIM, &sClockSourceConfig) != HAL_OK) { + if(HAL_TIM_ConfigClockSource(&LFRFID_READ_TIM, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } - if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { + if(HAL_TIM_PWM_Init(&LFRFID_READ_TIM) != HAL_OK) { Error_Handler(); } @@ -82,19 +86,19 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) { sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_TIM, &sMasterConfig) != HAL_OK) { + if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_READ_TIM, &sMasterConfig) != HAL_OK) { Error_Handler(); } // pwm config sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = (uint32_t)(LFRFID_TIM.Init.Period * duty_cycle); + sConfigOC.Pulse = (uint32_t)(LFRFID_READ_TIM.Init.Period * duty_cycle); sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; - if(HAL_TIM_OC_ConfigChannel(&LFRFID_TIM, &sConfigOC, LFRFID_CH) != HAL_OK) { + if(HAL_TIM_OC_ConfigChannel(&LFRFID_READ_TIM, &sConfigOC, LFRFID_READ_CHANNEL) != HAL_OK) { Error_Handler(); } @@ -112,17 +116,17 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) { sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; - if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_TIM, &sBreakDeadTimeConfig) != HAL_OK) { + if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_READ_TIM, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } } void api_hal_rfid_tim_read_start() { - HAL_TIMEx_PWMN_Start(&LFRFID_TIM, LFRFID_CH); + HAL_TIMEx_PWMN_Start(&LFRFID_READ_TIM, LFRFID_READ_CHANNEL); } void api_hal_rfid_tim_read_stop() { - HAL_TIMEx_PWMN_Stop(&LFRFID_TIM, LFRFID_CH); + HAL_TIMEx_PWMN_Stop(&LFRFID_READ_TIM, LFRFID_READ_CHANNEL); } void api_hal_rfid_tim_emulate(float freq) { @@ -135,21 +139,20 @@ void api_hal_rfid_tim_emulate(float freq) { TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; // basic PWM setup with needed freq and internal clock - LFRFID_TIM.Instance = TIM1; - LFRFID_TIM.Init.Prescaler = prescaler; - LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; - LFRFID_TIM.Init.Period = 1; - LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - LFRFID_TIM.Init.RepetitionCounter = 0; - LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; - if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) { + LFRFID_EMULATE_TIM.Init.Prescaler = prescaler; + LFRFID_EMULATE_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; + LFRFID_EMULATE_TIM.Init.Period = 1; + LFRFID_EMULATE_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + LFRFID_EMULATE_TIM.Init.RepetitionCounter = 0; + LFRFID_EMULATE_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if(HAL_TIM_Base_Init(&LFRFID_EMULATE_TIM) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; - if(HAL_TIM_ConfigClockSource(&LFRFID_TIM, &sClockSourceConfig) != HAL_OK) { + if(HAL_TIM_ConfigClockSource(&LFRFID_EMULATE_TIM, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } - if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { + if(HAL_TIM_PWM_Init(&LFRFID_EMULATE_TIM) != HAL_OK) { Error_Handler(); } @@ -157,7 +160,7 @@ void api_hal_rfid_tim_emulate(float freq) { sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_TIM, &sMasterConfig) != HAL_OK) { + if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_EMULATE_TIM, &sMasterConfig) != HAL_OK) { Error_Handler(); } @@ -169,7 +172,8 @@ void api_hal_rfid_tim_emulate(float freq) { sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; - if(HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LFRFID_CH) != HAL_OK) { + if(HAL_TIM_PWM_ConfigChannel(&LFRFID_EMULATE_TIM, &sConfigOC, LFRFID_EMULATE_CHANNEL) != + HAL_OK) { Error_Handler(); } @@ -187,20 +191,48 @@ void api_hal_rfid_tim_emulate(float freq) { sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; - if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_TIM, &sBreakDeadTimeConfig) != HAL_OK) { + if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_EMULATE_TIM, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } } void api_hal_rfid_tim_emulate_start() { - HAL_TIM_PWM_Start_IT(&LFRFID_TIM, LFRFID_CH); - HAL_TIM_Base_Start_IT(&LFRFID_TIM); + HAL_TIM_PWM_Start_IT(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL); + HAL_TIM_Base_Start_IT(&LFRFID_EMULATE_TIM); } void api_hal_rfid_tim_emulate_stop() { - HAL_TIM_Base_Stop(&LFRFID_TIM); - HAL_TIM_PWM_Stop(&LFRFID_TIM, LFRFID_CH); + HAL_TIM_Base_Stop(&LFRFID_EMULATE_TIM); + HAL_TIM_PWM_Stop(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL); } void api_hal_rfid_tim_reset() { +} + +bool api_hal_rfid_is_tim_emulate(TIM_HandleTypeDef* hw) { + return (hw == &LFRFID_EMULATE_TIM); +} + +void api_hal_rfid_set_emulate_period(uint32_t period) { + LFRFID_EMULATE_TIM.Instance->ARR = period; +} + +void api_hal_rfid_set_emulate_pulse(uint32_t pulse) { + switch(LFRFID_EMULATE_CHANNEL) { + case TIM_CHANNEL_1: + LFRFID_EMULATE_TIM.Instance->CCR1 = pulse; + break; + case TIM_CHANNEL_2: + LFRFID_EMULATE_TIM.Instance->CCR2 = pulse; + break; + case TIM_CHANNEL_3: + LFRFID_EMULATE_TIM.Instance->CCR3 = pulse; + break; + case TIM_CHANNEL_4: + LFRFID_EMULATE_TIM.Instance->CCR4 = pulse; + break; + default: + furi_check(0); + break; + } } \ No newline at end of file diff --git a/firmware/targets/f6/api-hal/api-hal-rfid.c b/firmware/targets/f6/api-hal/api-hal-rfid.c index 95e2fdc9..0fdec2d9 100644 --- a/firmware/targets/f6/api-hal/api-hal-rfid.c +++ b/firmware/targets/f6/api-hal/api-hal-rfid.c @@ -2,6 +2,11 @@ #include #include +#define LFRFID_READ_TIM htim1 +#define LFRFID_READ_CHANNEL TIM_CHANNEL_1 +#define LFRFID_EMULATE_TIM htim2 +#define LFRFID_EMULATE_CHANNEL TIM_CHANNEL_3 + void api_hal_rfid_pins_reset() { // ibutton bus disable api_hal_ibutton_stop(); @@ -56,21 +61,20 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) { TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; // basic PWM setup with needed freq and internal clock - LFRFID_TIM.Instance = TIM1; - LFRFID_TIM.Init.Prescaler = 0; - LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; - LFRFID_TIM.Init.Period = period; - LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - LFRFID_TIM.Init.RepetitionCounter = 0; - LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) { + LFRFID_READ_TIM.Init.Prescaler = 0; + LFRFID_READ_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; + LFRFID_READ_TIM.Init.Period = period; + LFRFID_READ_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + LFRFID_READ_TIM.Init.RepetitionCounter = 0; + LFRFID_READ_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if(HAL_TIM_Base_Init(&LFRFID_READ_TIM) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; - if(HAL_TIM_ConfigClockSource(&LFRFID_TIM, &sClockSourceConfig) != HAL_OK) { + if(HAL_TIM_ConfigClockSource(&LFRFID_READ_TIM, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } - if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { + if(HAL_TIM_PWM_Init(&LFRFID_READ_TIM) != HAL_OK) { Error_Handler(); } @@ -78,19 +82,19 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) { sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_TIM, &sMasterConfig) != HAL_OK) { + if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_READ_TIM, &sMasterConfig) != HAL_OK) { Error_Handler(); } // pwm config sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = (uint32_t)(LFRFID_TIM.Init.Period * duty_cycle); + sConfigOC.Pulse = (uint32_t)(LFRFID_READ_TIM.Init.Period * duty_cycle); sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; - if(HAL_TIM_OC_ConfigChannel(&LFRFID_TIM, &sConfigOC, LFRFID_CH) != HAL_OK) { + if(HAL_TIM_OC_ConfigChannel(&LFRFID_READ_TIM, &sConfigOC, LFRFID_READ_CHANNEL) != HAL_OK) { Error_Handler(); } @@ -108,17 +112,17 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) { sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; - if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_TIM, &sBreakDeadTimeConfig) != HAL_OK) { + if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_READ_TIM, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } } void api_hal_rfid_tim_read_start() { - HAL_TIMEx_PWMN_Start(&LFRFID_TIM, LFRFID_CH); + HAL_TIMEx_PWMN_Start(&LFRFID_READ_TIM, LFRFID_READ_CHANNEL); } void api_hal_rfid_tim_read_stop() { - HAL_TIMEx_PWMN_Stop(&LFRFID_TIM, LFRFID_CH); + HAL_TIMEx_PWMN_Stop(&LFRFID_READ_TIM, LFRFID_READ_CHANNEL); } void api_hal_rfid_tim_emulate(float freq) { @@ -131,21 +135,20 @@ void api_hal_rfid_tim_emulate(float freq) { TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; // basic PWM setup with needed freq and internal clock - LFRFID_TIM.Instance = TIM1; - LFRFID_TIM.Init.Prescaler = prescaler; - LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; - LFRFID_TIM.Init.Period = 1; - LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - LFRFID_TIM.Init.RepetitionCounter = 0; - LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; - if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) { + LFRFID_EMULATE_TIM.Init.Prescaler = prescaler; + LFRFID_EMULATE_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; + LFRFID_EMULATE_TIM.Init.Period = 1; + LFRFID_EMULATE_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + LFRFID_EMULATE_TIM.Init.RepetitionCounter = 0; + LFRFID_EMULATE_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if(HAL_TIM_Base_Init(&LFRFID_EMULATE_TIM) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; - if(HAL_TIM_ConfigClockSource(&LFRFID_TIM, &sClockSourceConfig) != HAL_OK) { + if(HAL_TIM_ConfigClockSource(&LFRFID_EMULATE_TIM, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } - if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { + if(HAL_TIM_PWM_Init(&LFRFID_EMULATE_TIM) != HAL_OK) { Error_Handler(); } @@ -153,7 +156,7 @@ void api_hal_rfid_tim_emulate(float freq) { sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_TIM, &sMasterConfig) != HAL_OK) { + if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_EMULATE_TIM, &sMasterConfig) != HAL_OK) { Error_Handler(); } @@ -165,7 +168,8 @@ void api_hal_rfid_tim_emulate(float freq) { sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; - if(HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LFRFID_CH) != HAL_OK) { + if(HAL_TIM_PWM_ConfigChannel(&LFRFID_EMULATE_TIM, &sConfigOC, LFRFID_EMULATE_CHANNEL) != + HAL_OK) { Error_Handler(); } @@ -183,20 +187,48 @@ void api_hal_rfid_tim_emulate(float freq) { sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; - if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_TIM, &sBreakDeadTimeConfig) != HAL_OK) { + if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_EMULATE_TIM, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } } void api_hal_rfid_tim_emulate_start() { - HAL_TIM_PWM_Start_IT(&LFRFID_TIM, LFRFID_CH); - HAL_TIM_Base_Start_IT(&LFRFID_TIM); + HAL_TIM_PWM_Start_IT(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL); + HAL_TIM_Base_Start_IT(&LFRFID_EMULATE_TIM); } void api_hal_rfid_tim_emulate_stop() { - HAL_TIM_Base_Stop(&LFRFID_TIM); - HAL_TIM_PWM_Stop(&LFRFID_TIM, LFRFID_CH); + HAL_TIM_Base_Stop(&LFRFID_EMULATE_TIM); + HAL_TIM_PWM_Stop(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL); } void api_hal_rfid_tim_reset() { +} + +bool api_hal_rfid_is_tim_emulate(TIM_HandleTypeDef* hw) { + return (hw == &LFRFID_EMULATE_TIM); +} + +void api_hal_rfid_set_emulate_period(uint32_t period) { + LFRFID_EMULATE_TIM.Instance->ARR = period; +} + +void api_hal_rfid_set_emulate_pulse(uint32_t pulse) { + switch(LFRFID_EMULATE_CHANNEL) { + case TIM_CHANNEL_1: + LFRFID_EMULATE_TIM.Instance->CCR1 = pulse; + break; + case TIM_CHANNEL_2: + LFRFID_EMULATE_TIM.Instance->CCR2 = pulse; + break; + case TIM_CHANNEL_3: + LFRFID_EMULATE_TIM.Instance->CCR3 = pulse; + break; + case TIM_CHANNEL_4: + LFRFID_EMULATE_TIM.Instance->CCR4 = pulse; + break; + default: + furi_check(0); + break; + } } \ No newline at end of file