[FL-2375] Migrate to LL part 3 (#1058)

* FuriHal: replace HAL with LL for ARR and CC
* Rfid, FuriHal: migrate emulation to LL
* RFID hal: disable arr preload during emulation
* Rfid, Furi, FuriHal: last piece of LL puzzle
* Rfid, Furi, FuriHal: filing the last piece of LL puzzle
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
あく 2022-03-26 01:56:18 +03:00 committed by GitHub
parent 7c4b0f534f
commit 413a03defb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 121 additions and 577 deletions

View file

@ -2,7 +2,6 @@
#include <furi.h>
#include <furi_hal.h>
#include <stm32wbxx_ll_cortex.h>
#include <tim.h>
/**
* @brief private violation assistant for RfidReader

View file

@ -1,7 +1,5 @@
#include "rfid_timer_emulator.h"
extern TIM_HandleTypeDef htim1;
RfidTimerEmulator::RfidTimerEmulator() {
}
@ -25,9 +23,7 @@ void RfidTimerEmulator::start(LfrfidKeyType type, const uint8_t* data, uint8_t d
furi_hal_rfid_tim_emulate(125000);
furi_hal_rfid_pins_emulate();
api_interrupt_add(timer_update_callback, InterruptTypeTimerUpdate, this);
furi_hal_rfid_tim_emulate_start();
furi_hal_rfid_tim_emulate_start(RfidTimerEmulator::timer_update_callback, this);
}
} else {
// not found
@ -36,30 +32,25 @@ void RfidTimerEmulator::start(LfrfidKeyType type, const uint8_t* data, uint8_t d
void RfidTimerEmulator::stop() {
furi_hal_rfid_tim_emulate_stop();
api_interrupt_remove(timer_update_callback, InterruptTypeTimerUpdate);
furi_hal_rfid_tim_reset();
furi_hal_rfid_pins_reset();
}
void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
void RfidTimerEmulator::timer_update_callback(void* ctx) {
RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx);
TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw);
if(furi_hal_rfid_is_tim_emulate(hw)) {
bool result;
bool polarity;
uint16_t period;
uint16_t pulse;
bool result;
bool polarity;
uint16_t period;
uint16_t pulse;
do {
_this->current_encoder->get_next(&polarity, &period, &pulse);
result = _this->pulse_joiner.push_pulse(polarity, period, pulse);
} while(result == false);
do {
_this->current_encoder->get_next(&polarity, &period, &pulse);
result = _this->pulse_joiner.push_pulse(polarity, period, pulse);
} while(result == false);
_this->pulse_joiner.pop_pulse(&period, &pulse);
_this->pulse_joiner.pop_pulse(&period, &pulse);
furi_hal_rfid_set_emulate_period(period - 1);
furi_hal_rfid_set_emulate_pulse(pulse);
}
furi_hal_rfid_set_emulate_period(period - 1);
furi_hal_rfid_set_emulate_pulse(pulse);
}

View file

@ -25,5 +25,5 @@ private:
};
PulseJoiner pulse_joiner;
static void timer_update_callback(void* _hw, void* ctx);
};
static void timer_update_callback(void* ctx);
};

View file

@ -1,7 +1,6 @@
#include "furi.h"
void furi_init() {
api_interrupt_init();
furi_log_init();
furi_record_init();
furi_stdglue_init();

View file

@ -18,7 +18,6 @@
#include <furi/log.h>
#include <furi_hal_gpio.h>
#include <furi_hal/api_interrupt_mgr.h>
#include <stdlib.h>

View file

@ -1,65 +0,0 @@
#include "api_interrupt_mgr.h"
#include <cmsis_os2.h>
#include <furi.h>
static volatile InterruptCallbackItem callback_list[InterruptTypeLast];
bool api_interrupt_init() {
for(uint8_t i = 0; i < InterruptTypeLast; i++) {
callback_list[i].callback = NULL;
callback_list[i].context = NULL;
callback_list[i].ready = false;
}
return true;
}
void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context) {
furi_assert(type < InterruptTypeLast);
furi_check(callback_list[type].callback == NULL);
callback_list[type].callback = callback;
callback_list[type].context = context;
__DMB();
callback_list[type].ready = true;
}
void api_interrupt_remove(InterruptCallback callback, InterruptType type) {
furi_assert(type < InterruptTypeLast);
if(callback_list[type].callback != NULL) {
furi_check(callback_list[type].callback == callback);
}
callback_list[type].ready = false;
__DMB();
callback_list[type].callback = NULL;
callback_list[type].context = NULL;
}
void api_interrupt_enable(InterruptCallback callback, InterruptType type) {
furi_assert(type < InterruptTypeLast);
furi_check(callback_list[type].callback == callback);
callback_list[type].ready = true;
__DMB();
}
void api_interrupt_disable(InterruptCallback callback, InterruptType type) {
furi_assert(type < InterruptTypeLast);
furi_check(callback_list[type].callback == callback);
callback_list[type].ready = false;
__DMB();
}
void api_interrupt_call(InterruptType type, void* hw) {
// that executed in interrupt ctx so mutex don't needed
// but we need to check ready flag
furi_assert(type < InterruptTypeLast);
if(callback_list[type].callback != NULL) {
if(callback_list[type].ready) {
callback_list[type].callback(hw, callback_list[type].context);
}
}
}

View file

@ -1,74 +0,0 @@
/**
* @file api_interrupt_mgr.h
* Furi: interrupt API
*/
#pragma once
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Interrupt callback prototype */
typedef void (*InterruptCallback)(void*, void*);
/** Interupt type */
typedef enum {
InterruptTypeTimerUpdate,
InterruptTypeLast,
} InterruptType;
/** Interrupt callback type */
typedef struct {
InterruptCallback callback;
void* context;
bool ready;
} InterruptCallbackItem;
/** Init interrupt
*
* @return true on succsessful initialization, false otherwise
*/
bool api_interrupt_init();
/** Add interrupt
*
* @param callback InterruptCallback
* @param type InterruptType
* @param context context for callback
*/
void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context);
/** Remove interrupt
*
* @param callback InterruptCallback
* @param type InterruptType
*/
void api_interrupt_remove(InterruptCallback callback, InterruptType type);
/** Enable interrupt
*
* @param callback InterruptCallback
* @param type InterruptType
*/
void api_interrupt_enable(InterruptCallback callback, InterruptType type);
/** Disable interrupt
*
* @param callback InterruptCallback
* @param type InterruptType
*/
void api_interrupt_disable(InterruptCallback callback, InterruptType type);
/** Call interrupt
*
* @param type InterruptType
* @param hw pointer to hardware peripheral
*/
void api_interrupt_call(InterruptType type, void* hw);
#ifdef __cplusplus
}
#endif

View file

@ -53,7 +53,7 @@ extern "C" {
/*#define HAL_SMBUS_MODULE_ENABLED */
/*#define HAL_SMARTCARD_MODULE_ENABLED */
/*#define HAL_SPI_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
/*#define HAL_TIM_MODULE_ENABLED */
/*#define HAL_TSC_MODULE_ENABLED */
/*#define HAL_UART_MODULE_ENABLED */
/*#define HAL_USART_MODULE_ENABLED */

View file

@ -1,56 +0,0 @@
/**
******************************************************************************
* @file tim.h
* @brief This file contains all the function prototypes for
* the tim.c file
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TIM_H__
#define __TIM_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_TIM1_Init(void);
void MX_TIM2_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __TIM_H__ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -5,7 +5,6 @@
#include "usbd_core.h"
extern usbd_device udev;
extern TIM_HandleTypeDef htim1;
extern void HW_TS_RTC_Wakeup_Handler();
extern void HW_IPCC_Tx_Handler();
@ -19,14 +18,6 @@ void USB_LP_IRQHandler(void) {
usbd_poll(&udev);
}
void TIM1_TRG_COM_TIM17_IRQHandler(void) {
HAL_TIM_IRQHandler(&htim1);
}
void TIM1_CC_IRQHandler(void) {
HAL_TIM_IRQHandler(&htim1);
}
void HSEM_IRQHandler(void) {
HAL_HSEM_IRQHandler();
}

View file

@ -1,150 +0,0 @@
#include "tim.h"
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
/* TIM1 init function */
void MX_TIM1_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if(HAL_TIM_Base_Init(&htim1) != HAL_OK) {
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if(HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_OC_Init(&htim1) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_PWM_Init(&htim1) != HAL_OK) {
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if(HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TIMING;
sConfigOC.Pulse = 0;
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(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) {
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
if(HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) {
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if(HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK) {
Error_Handler();
}
HAL_TIM_MspPostInit(&htim1);
}
/* TIM2 init function */
void MX_TIM2_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_IC_InitTypeDef sConfigIC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 64 - 1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 4294967295;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if(HAL_TIM_Base_Init(&htim2) != HAL_OK) {
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if(HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_IC_Init(&htim2) != HAL_OK) {
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if(HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if(HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) {
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if(HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) {
Error_Handler();
}
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(tim_baseHandle->Instance == TIM1) {
HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn);
} else if(tim_baseHandle->Instance == TIM2) {
GPIO_InitStruct.Pin = IR_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(IR_RX_GPIO_Port, &GPIO_InitStruct);
/* TIM2 interrupt Init */
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance == TIM1) {
GPIO_InitStruct.Pin = IR_TX_Pin | RFID_OUT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) {
if(tim_baseHandle->Instance == TIM1) {
HAL_NVIC_DisableIRQ(TIM1_TRG_COM_TIM17_IRQn);
} else if(tim_baseHandle->Instance == TIM2) {
HAL_GPIO_DeInit(IR_RX_GPIO_Port, IR_RX_Pin);
HAL_NVIC_DisableIRQ(TIM2_IRQn);
}
}

View file

@ -1,6 +1,5 @@
#include <furi_hal.h>
#include <tim.h>
#include <gpio.h>
#include <stm32wbxx_ll_cortex.h>
@ -22,11 +21,6 @@ void furi_hal_init() {
furi_hal_spi_init();
MX_TIM1_Init();
FURI_LOG_I(TAG, "TIM1 OK");
MX_TIM2_Init();
FURI_LOG_I(TAG, "TIM2 OK");
furi_hal_ibutton_init();
FURI_LOG_I(TAG, "iButton OK");
furi_hal_speaker_init();

View file

@ -3,7 +3,6 @@
#include <furi.h>
#include <main.h>
#include <tim.h>
#include <stm32wbxx_ll_tim.h>
#define TAG "FuriHalInterrupt"
@ -67,20 +66,10 @@ void furi_hal_interrupt_set_dma_channel_isr(
}
}
extern void api_interrupt_call(InterruptType type, void* hw);
/* ST HAL symbols */
/* Timer update event */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
api_interrupt_call(InterruptTypeTimerUpdate, htim);
}
/* Timer 2 */
void TIM2_IRQHandler(void) {
if(furi_hal_tim_tim2_isr) {
furi_hal_tim_tim2_isr();
} else {
HAL_TIM_IRQHandler(&htim2);
}
}
@ -88,11 +77,15 @@ void TIM2_IRQHandler(void) {
void TIM1_UP_TIM16_IRQHandler(void) {
if(furi_hal_tim_tim1_isr) {
furi_hal_tim_tim1_isr();
} else {
HAL_TIM_IRQHandler(&htim1);
}
}
void TIM1_TRG_COM_TIM17_IRQHandler(void) {
}
void TIM1_CC_IRQHandler(void) {
}
/* DMA 1 */
void DMA1_Channel1_IRQHandler(void) {
if(furi_hal_dma_channel_isr[0][0]) furi_hal_dma_channel_isr[0][0]();

View file

@ -1,20 +1,32 @@
#include <furi_hal_rfid.h>
#include <furi_hal_ibutton.h>
#include <furi_hal_interrupt.h>
#include <furi_hal_resources.h>
#include <furi_hal_version.h>
#include <furi.h>
#include <tim.h>
#include <stm32wbxx_ll_tim.h>
#include <stm32wbxx_ll_comp.h>
#define LFRFID_TIM htim1
#define LFRFID_CH TIM_CHANNEL_1
#define LFRFID_READ_TIM htim1
#define LFRFID_READ_CHANNEL TIM_CHANNEL_1
#define LFRFID_EMULATE_TIM htim2
#define LFRFID_EMULATE_CHANNEL TIM_CHANNEL_3
#define FURI_HAL_RFID_READ_TIMER TIM1
#define FURI_HAL_RFID_READ_TIMER_CHANNEL LL_TIM_CHANNEL_CH1N
// We can't use N channel for LL_TIM_OC_Init, so...
#define FURI_HAL_RFID_READ_TIMER_CHANNEL_CONFIG LL_TIM_CHANNEL_CH1
#define FURI_HAL_RFID_EMULATE_TIMER TIM2
#define FURI_HAL_RFID_EMULATE_TIMER_IRQ TIM2_IRQn
#define FURI_HAL_RFID_EMULATE_TIMER_CHANNEL LL_TIM_CHANNEL_CH3
typedef struct {
FuriHalRfidEmulateCallback callback;
void* context;
} FuriHalRfid;
FuriHalRfid* furi_hal_rfid = NULL;
void furi_hal_rfid_init() {
furi_assert(furi_hal_rfid == NULL);
furi_hal_rfid = malloc(sizeof(FuriHalRfid));
furi_hal_rfid_pins_reset();
LL_COMP_InitTypeDef COMP_InitStruct = {0};
@ -105,210 +117,129 @@ void furi_hal_rfid_pin_pull_pulldown() {
}
void furi_hal_rfid_tim_read(float freq, float duty_cycle) {
// TODO LL init
uint32_t period = (uint32_t)((SystemCoreClock) / freq) - 1;
FURI_CRITICAL_ENTER();
LL_TIM_DeInit(FURI_HAL_RFID_READ_TIMER);
FURI_CRITICAL_EXIT();
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
LL_TIM_InitTypeDef TIM_InitStruct = {0};
TIM_InitStruct.Autoreload = (SystemCoreClock / freq) - 1;
LL_TIM_Init(FURI_HAL_RFID_READ_TIMER, &TIM_InitStruct);
LL_TIM_DisableARRPreload(FURI_HAL_RFID_READ_TIMER);
// basic PWM setup with needed freq and internal clock
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_READ_TIM, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_PWM_Init(&LFRFID_READ_TIM) != HAL_OK) {
Error_Handler();
}
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStruct.CompareValue = TIM_InitStruct.Autoreload * duty_cycle;
LL_TIM_OC_Init(
FURI_HAL_RFID_READ_TIMER, FURI_HAL_RFID_READ_TIMER_CHANNEL_CONFIG, &TIM_OC_InitStruct);
// no master-slave mode
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_READ_TIM, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
// pwm config
sConfigOC.OCMode = TIM_OCMODE_PWM1;
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_PWM_ConfigChannel(&LFRFID_READ_TIM, &sConfigOC, LFRFID_READ_CHANNEL) != HAL_OK) {
Error_Handler();
}
// no deadtime
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if(HAL_TIMEx_ConfigBreakDeadTime(&LFRFID_READ_TIM, &sBreakDeadTimeConfig) != HAL_OK) {
Error_Handler();
}
LL_TIM_EnableCounter(FURI_HAL_RFID_READ_TIMER);
}
void furi_hal_rfid_tim_read_start() {
HAL_TIMEx_PWMN_Start(&LFRFID_READ_TIM, LFRFID_READ_CHANNEL);
LL_TIM_EnableAllOutputs(FURI_HAL_RFID_READ_TIMER);
}
void furi_hal_rfid_tim_read_stop() {
HAL_TIMEx_PWMN_Stop(&LFRFID_READ_TIM, LFRFID_READ_CHANNEL);
LL_TIM_DisableAllOutputs(FURI_HAL_RFID_READ_TIMER);
}
void furi_hal_rfid_tim_emulate(float freq) {
// TODO LL init
// uint32_t prescaler = (uint32_t)((SystemCoreClock) / freq) - 1;
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
// basic PWM setup with needed freq and internal clock
LFRFID_EMULATE_TIM.Init.Prescaler = 0;
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();
}
FURI_CRITICAL_ENTER();
LL_TIM_DeInit(FURI_HAL_RFID_EMULATE_TIMER);
FURI_CRITICAL_EXIT();
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
sClockSourceConfig.ClockPolarity = TIM_ETRPOLARITY_INVERTED;
sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
sClockSourceConfig.ClockFilter = 0;
if(HAL_TIM_ConfigClockSource(&LFRFID_EMULATE_TIM, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_PWM_Init(&LFRFID_EMULATE_TIM) != HAL_OK) {
Error_Handler();
}
LL_TIM_SetPrescaler(FURI_HAL_RFID_EMULATE_TIMER, 0);
LL_TIM_SetCounterMode(FURI_HAL_RFID_EMULATE_TIMER, LL_TIM_COUNTERMODE_UP);
LL_TIM_SetAutoReload(FURI_HAL_RFID_EMULATE_TIMER, 1);
LL_TIM_DisableARRPreload(FURI_HAL_RFID_EMULATE_TIMER);
LL_TIM_SetRepetitionCounter(FURI_HAL_RFID_EMULATE_TIMER, 0);
// no master-slave mode
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if(HAL_TIMEx_MasterConfigSynchronization(&LFRFID_EMULATE_TIM, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
LL_TIM_SetClockDivision(FURI_HAL_RFID_EMULATE_TIMER, LL_TIM_CLOCKDIVISION_DIV1);
LL_TIM_SetClockSource(FURI_HAL_RFID_EMULATE_TIMER, LL_TIM_CLOCKSOURCE_EXT_MODE2);
LL_TIM_ConfigETR(
FURI_HAL_RFID_EMULATE_TIMER,
LL_TIM_ETR_POLARITY_INVERTED,
LL_TIM_ETR_PRESCALER_DIV1,
LL_TIM_ETR_FILTER_FDIV1);
// pwm config
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1;
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_PWM_ConfigChannel(&LFRFID_EMULATE_TIM, &sConfigOC, LFRFID_EMULATE_CHANNEL) !=
HAL_OK) {
Error_Handler();
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStruct.CompareValue = 1;
LL_TIM_OC_Init(
FURI_HAL_RFID_EMULATE_TIMER, FURI_HAL_RFID_EMULATE_TIMER_CHANNEL, &TIM_OC_InitStruct);
LL_TIM_GenerateEvent_UPDATE(FURI_HAL_RFID_EMULATE_TIMER);
}
static void furi_hal_rfid_emulate_isr() {
if(LL_TIM_IsActiveFlag_UPDATE(FURI_HAL_RFID_EMULATE_TIMER)) {
LL_TIM_ClearFlag_UPDATE(FURI_HAL_RFID_EMULATE_TIMER);
furi_hal_rfid->callback(furi_hal_rfid->context);
}
}
void furi_hal_rfid_tim_emulate_start() {
void furi_hal_rfid_tim_emulate_start(FuriHalRfidEmulateCallback callback, void* context) {
furi_assert(furi_hal_rfid);
furi_hal_rfid->callback = callback;
furi_hal_rfid->context = context;
// TODO make api for interrupts priority
for(size_t i = WWDG_IRQn; i <= DMAMUX1_OVR_IRQn; i++) {
HAL_NVIC_SetPriority(i, 15, 0);
}
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
furi_hal_interrupt_set_timer_isr(FURI_HAL_RFID_EMULATE_TIMER, furi_hal_rfid_emulate_isr);
HAL_TIM_PWM_Start_IT(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL);
HAL_TIM_Base_Start_IT(&LFRFID_EMULATE_TIM);
NVIC_SetPriority(
FURI_HAL_RFID_EMULATE_TIMER_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
NVIC_EnableIRQ(FURI_HAL_RFID_EMULATE_TIMER_IRQ);
LL_TIM_EnableIT_UPDATE(FURI_HAL_RFID_EMULATE_TIMER);
LL_TIM_EnableAllOutputs(FURI_HAL_RFID_EMULATE_TIMER);
LL_TIM_EnableCounter(FURI_HAL_RFID_EMULATE_TIMER);
}
void furi_hal_rfid_tim_emulate_stop() {
HAL_TIM_Base_Stop(&LFRFID_EMULATE_TIM);
HAL_TIM_PWM_Stop(&LFRFID_EMULATE_TIM, LFRFID_EMULATE_CHANNEL);
furi_hal_interrupt_set_timer_isr(FURI_HAL_RFID_EMULATE_TIMER, NULL);
LL_TIM_DisableCounter(FURI_HAL_RFID_EMULATE_TIMER);
LL_TIM_DisableAllOutputs(FURI_HAL_RFID_EMULATE_TIMER);
}
void furi_hal_rfid_tim_reset() {
FURI_CRITICAL_ENTER();
HAL_TIM_Base_DeInit(&LFRFID_READ_TIM);
LL_TIM_DeInit(TIM1);
HAL_TIM_Base_DeInit(&LFRFID_EMULATE_TIM);
LL_TIM_DeInit(TIM2);
LL_TIM_DeInit(FURI_HAL_RFID_READ_TIMER);
LL_TIM_DeInit(FURI_HAL_RFID_EMULATE_TIMER);
FURI_CRITICAL_EXIT();
}
bool furi_hal_rfid_is_tim_emulate(TIM_HandleTypeDef* hw) {
return (hw == &LFRFID_EMULATE_TIM);
}
void furi_hal_rfid_set_emulate_period(uint32_t period) {
LFRFID_EMULATE_TIM.Instance->ARR = period;
LL_TIM_SetAutoReload(FURI_HAL_RFID_EMULATE_TIMER, period);
}
void furi_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_crash(NULL);
break;
}
#if FURI_HAL_RFID_EMULATE_TIMER_CHANNEL == LL_TIM_CHANNEL_CH3
LL_TIM_OC_SetCompareCH3(FURI_HAL_RFID_EMULATE_TIMER, pulse);
#else
#error Update this code. Would you kindly?
#endif
}
void furi_hal_rfid_set_read_period(uint32_t period) {
LFRFID_TIM.Instance->ARR = period;
LL_TIM_SetAutoReload(FURI_HAL_RFID_READ_TIMER, period);
}
void furi_hal_rfid_set_read_pulse(uint32_t pulse) {
switch(LFRFID_READ_CHANNEL) {
case TIM_CHANNEL_1:
LFRFID_TIM.Instance->CCR1 = pulse;
break;
case TIM_CHANNEL_2:
LFRFID_TIM.Instance->CCR2 = pulse;
break;
case TIM_CHANNEL_3:
LFRFID_TIM.Instance->CCR3 = pulse;
break;
case TIM_CHANNEL_4:
LFRFID_TIM.Instance->CCR4 = pulse;
break;
default:
furi_crash(NULL);
break;
}
#if FURI_HAL_RFID_READ_TIMER_CHANNEL == LL_TIM_CHANNEL_CH1N
LL_TIM_OC_SetCompareCH1(FURI_HAL_RFID_READ_TIMER, pulse);
#else
#error Update this code. Would you kindly?
#endif
}
void furi_hal_rfid_change_read_config(float freq, float duty_cycle) {

View file

@ -62,8 +62,6 @@ C_SOURCES += \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_pwr.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_pwr_ex.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_rcc.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_tim.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_tim_ex.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_adc.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_comp.c \
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_dma.c \

View file

@ -58,9 +58,11 @@ void furi_hal_rfid_tim_read_stop();
*/
void furi_hal_rfid_tim_emulate(float freq);
typedef void (*FuriHalRfidEmulateCallback)(void* context);
/** Start emulation timer
*/
void furi_hal_rfid_tim_emulate_start();
void furi_hal_rfid_tim_emulate_start(FuriHalRfidEmulateCallback callback, void* context);
/** Stop emulation timer
*/
@ -70,14 +72,6 @@ void furi_hal_rfid_tim_emulate_stop();
*/
void furi_hal_rfid_tim_reset();
/** Check that timer instance is emulation timer
*
* @param hw timer instance
*
* @return true if instance is emulation timer
*/
bool furi_hal_rfid_is_tim_emulate(TIM_HandleTypeDef* hw);
/** Set emulation timer period
*
* @param period overall duration