[FL-1311] F6, app LF-RFID: Updated timer api and config (#465)

* F6, app LF-RFID: Updated timer api and config
* Api hal rfid: updated comments

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG 2021-05-18 19:54:48 +10:00 committed by GitHub
parent 1ddd4acd80
commit ba0419276e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 156 additions and 69 deletions

View File

@ -64,7 +64,7 @@ void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx); RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx);
TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw); TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw);
if(hw == &LFRFID_TIM) { if(api_hal_rfid_is_tim_emulate(hw)) {
bool result; bool result;
bool polarity; bool polarity;
uint16_t period; uint16_t period;
@ -77,7 +77,7 @@ void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
_this->pulse_joiner.pop_pulse(&period, &pulse); _this->pulse_joiner.pop_pulse(&period, &pulse);
hw->Instance->ARR = period - 1; api_hal_rfid_set_emulate_period(period - 1);
hw->Instance->CCR1 = pulse; api_hal_rfid_set_emulate_pulse(pulse);
} }
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <main.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -68,6 +70,27 @@ void api_hal_rfid_tim_emulate_stop();
*/ */
void api_hal_rfid_tim_reset(); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -2,6 +2,11 @@
#include <api-hal-ibutton.h> #include <api-hal-ibutton.h>
#include <api-hal-resources.h> #include <api-hal-resources.h>
#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() { void api_hal_rfid_pins_reset() {
// ibutton bus disable // ibutton bus disable
api_hal_ibutton_stop(); api_hal_ibutton_stop();
@ -60,21 +65,20 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) {
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
// basic PWM setup with needed freq and internal clock // basic PWM setup with needed freq and internal clock
LFRFID_TIM.Instance = TIM1; LFRFID_READ_TIM.Init.Prescaler = 0;
LFRFID_TIM.Init.Prescaler = 0; LFRFID_READ_TIM.Init.CounterMode = TIM_COUNTERMODE_UP;
LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; LFRFID_READ_TIM.Init.Period = period;
LFRFID_TIM.Init.Period = period; LFRFID_READ_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; LFRFID_READ_TIM.Init.RepetitionCounter = 0;
LFRFID_TIM.Init.RepetitionCounter = 0; LFRFID_READ_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if(HAL_TIM_Base_Init(&LFRFID_READ_TIM) != HAL_OK) {
if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 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(); Error_Handler();
} }
if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { if(HAL_TIM_PWM_Init(&LFRFID_READ_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
@ -82,19 +86,19 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) {
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 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(); Error_Handler();
} }
// pwm config // pwm config
sConfigOC.OCMode = TIM_OCMODE_PWM1; 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.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_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(); Error_Handler();
} }
@ -112,17 +116,17 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) {
sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; 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(); Error_Handler();
} }
} }
void api_hal_rfid_tim_read_start() { 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() { 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) { void api_hal_rfid_tim_emulate(float freq) {
@ -135,21 +139,20 @@ void api_hal_rfid_tim_emulate(float freq) {
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
// basic PWM setup with needed freq and internal clock // basic PWM setup with needed freq and internal clock
LFRFID_TIM.Instance = TIM1; LFRFID_EMULATE_TIM.Init.Prescaler = prescaler;
LFRFID_TIM.Init.Prescaler = prescaler; LFRFID_EMULATE_TIM.Init.CounterMode = TIM_COUNTERMODE_UP;
LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; LFRFID_EMULATE_TIM.Init.Period = 1;
LFRFID_TIM.Init.Period = 1; LFRFID_EMULATE_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; LFRFID_EMULATE_TIM.Init.RepetitionCounter = 0;
LFRFID_TIM.Init.RepetitionCounter = 0; LFRFID_EMULATE_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if(HAL_TIM_Base_Init(&LFRFID_EMULATE_TIM) != HAL_OK) {
if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 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(); Error_Handler();
} }
if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { if(HAL_TIM_PWM_Init(&LFRFID_EMULATE_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
@ -157,7 +160,7 @@ void api_hal_rfid_tim_emulate(float freq) {
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 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(); Error_Handler();
} }
@ -169,7 +172,8 @@ void api_hal_rfid_tim_emulate(float freq) {
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_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(); Error_Handler();
} }
@ -187,20 +191,48 @@ void api_hal_rfid_tim_emulate(float freq) {
sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; 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(); Error_Handler();
} }
} }
void api_hal_rfid_tim_emulate_start() { void api_hal_rfid_tim_emulate_start() {
HAL_TIM_PWM_Start_IT(&LFRFID_TIM, LFRFID_CH); HAL_TIM_PWM_Start_IT(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL);
HAL_TIM_Base_Start_IT(&LFRFID_TIM); HAL_TIM_Base_Start_IT(&LFRFID_EMULATE_TIM);
} }
void api_hal_rfid_tim_emulate_stop() { void api_hal_rfid_tim_emulate_stop() {
HAL_TIM_Base_Stop(&LFRFID_TIM); HAL_TIM_Base_Stop(&LFRFID_EMULATE_TIM);
HAL_TIM_PWM_Stop(&LFRFID_TIM, LFRFID_CH); HAL_TIM_PWM_Stop(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL);
} }
void api_hal_rfid_tim_reset() { 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;
}
}

View File

@ -2,6 +2,11 @@
#include <api-hal-ibutton.h> #include <api-hal-ibutton.h>
#include <api-hal-resources.h> #include <api-hal-resources.h>
#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() { void api_hal_rfid_pins_reset() {
// ibutton bus disable // ibutton bus disable
api_hal_ibutton_stop(); api_hal_ibutton_stop();
@ -56,21 +61,20 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) {
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
// basic PWM setup with needed freq and internal clock // basic PWM setup with needed freq and internal clock
LFRFID_TIM.Instance = TIM1; LFRFID_READ_TIM.Init.Prescaler = 0;
LFRFID_TIM.Init.Prescaler = 0; LFRFID_READ_TIM.Init.CounterMode = TIM_COUNTERMODE_UP;
LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; LFRFID_READ_TIM.Init.Period = period;
LFRFID_TIM.Init.Period = period; LFRFID_READ_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; LFRFID_READ_TIM.Init.RepetitionCounter = 0;
LFRFID_TIM.Init.RepetitionCounter = 0; LFRFID_READ_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if(HAL_TIM_Base_Init(&LFRFID_READ_TIM) != HAL_OK) {
if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 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(); Error_Handler();
} }
if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { if(HAL_TIM_PWM_Init(&LFRFID_READ_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
@ -78,19 +82,19 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) {
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 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(); Error_Handler();
} }
// pwm config // pwm config
sConfigOC.OCMode = TIM_OCMODE_PWM1; 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.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_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(); Error_Handler();
} }
@ -108,17 +112,17 @@ void api_hal_rfid_tim_read(float freq, float duty_cycle) {
sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; 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(); Error_Handler();
} }
} }
void api_hal_rfid_tim_read_start() { 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() { 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) { void api_hal_rfid_tim_emulate(float freq) {
@ -131,21 +135,20 @@ void api_hal_rfid_tim_emulate(float freq) {
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
// basic PWM setup with needed freq and internal clock // basic PWM setup with needed freq and internal clock
LFRFID_TIM.Instance = TIM1; LFRFID_EMULATE_TIM.Init.Prescaler = prescaler;
LFRFID_TIM.Init.Prescaler = prescaler; LFRFID_EMULATE_TIM.Init.CounterMode = TIM_COUNTERMODE_UP;
LFRFID_TIM.Init.CounterMode = TIM_COUNTERMODE_UP; LFRFID_EMULATE_TIM.Init.Period = 1;
LFRFID_TIM.Init.Period = 1; LFRFID_EMULATE_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
LFRFID_TIM.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; LFRFID_EMULATE_TIM.Init.RepetitionCounter = 0;
LFRFID_TIM.Init.RepetitionCounter = 0; LFRFID_EMULATE_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
LFRFID_TIM.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if(HAL_TIM_Base_Init(&LFRFID_EMULATE_TIM) != HAL_OK) {
if(HAL_TIM_Base_Init(&LFRFID_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 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(); Error_Handler();
} }
if(HAL_TIM_PWM_Init(&LFRFID_TIM) != HAL_OK) { if(HAL_TIM_PWM_Init(&LFRFID_EMULATE_TIM) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
@ -153,7 +156,7 @@ void api_hal_rfid_tim_emulate(float freq) {
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 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(); Error_Handler();
} }
@ -165,7 +168,8 @@ void api_hal_rfid_tim_emulate(float freq) {
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_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(); Error_Handler();
} }
@ -183,20 +187,48 @@ void api_hal_rfid_tim_emulate(float freq) {
sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; 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(); Error_Handler();
} }
} }
void api_hal_rfid_tim_emulate_start() { void api_hal_rfid_tim_emulate_start() {
HAL_TIM_PWM_Start_IT(&LFRFID_TIM, LFRFID_CH); HAL_TIM_PWM_Start_IT(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL);
HAL_TIM_Base_Start_IT(&LFRFID_TIM); HAL_TIM_Base_Start_IT(&LFRFID_EMULATE_TIM);
} }
void api_hal_rfid_tim_emulate_stop() { void api_hal_rfid_tim_emulate_stop() {
HAL_TIM_Base_Stop(&LFRFID_TIM); HAL_TIM_Base_Stop(&LFRFID_EMULATE_TIM);
HAL_TIM_PWM_Stop(&LFRFID_TIM, LFRFID_CH); HAL_TIM_PWM_Stop(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL);
} }
void api_hal_rfid_tim_reset() { 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;
}
}