[FL-1156, FL-1249] Add IRDA encoder/decoder library (#451)
* Add cscope db generation * Add api-hal-irda, TIM2: HAL->LL * Add libirda: pwm decoding * Universal state machine * Add irda decoder library * Move IRDA capture to standalone tool * Add encoder/decoder samsung32, NEC, fix bugs * Port current App to new Irda lib * Fix clang format for test data * Port IRDA api-hal to f6 Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
55
firmware/targets/api-hal-include/api-hal-irda.h
Normal file
55
firmware/targets/api-hal-include/api-hal-irda.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Signature of callback function for receiving continuous IRDA rx signal.
|
||||
*
|
||||
* @param level - level of input IRDA rx signal
|
||||
* @param duration - duration of continuous rx signal level in us
|
||||
*/
|
||||
typedef void (*TimerISRCallback)(void* ctx, bool level, uint32_t duration);
|
||||
|
||||
|
||||
/**
|
||||
* Initialize IRDA RX timer to receive interrupts.
|
||||
* It provides interrupts for every RX-signal edge changing
|
||||
* with its duration.
|
||||
*/
|
||||
void api_hal_irda_rx_irq_init(void);
|
||||
|
||||
/**
|
||||
* Deinitialize IRDA RX interrupt.
|
||||
*/
|
||||
void api_hal_irda_rx_irq_deinit(void);
|
||||
|
||||
/**
|
||||
* Setup callback for previously initialized IRDA RX interrupt.
|
||||
*
|
||||
* @param callback - callback to call when RX signal edge changing occurs
|
||||
* @param ctx - context for callback
|
||||
*/
|
||||
void api_hal_irda_rx_irq_set_callback(TimerISRCallback callback, void *ctx);
|
||||
|
||||
/**
|
||||
* Start generating IRDA TX PWM. Provides PWM initialization on
|
||||
* defined frequency.
|
||||
*
|
||||
* @param duty_cycle - duty cycle
|
||||
* @param freq - PWM frequency to generate
|
||||
*/
|
||||
void api_hal_irda_pwm_set(float duty_cycle, float freq);
|
||||
|
||||
/**
|
||||
* Stop generating IRDA PWM signal.
|
||||
*/
|
||||
void api_hal_irda_pwm_stop();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -86,10 +86,6 @@ void TIM1_CC_IRQHandler(void) {
|
||||
HAL_TIM_IRQHandler(&htim1);
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler(void) {
|
||||
HAL_TIM_IRQHandler(&htim2);
|
||||
}
|
||||
|
||||
void HSEM_IRQHandler(void) {
|
||||
HAL_HSEM_IRQHandler();
|
||||
}
|
||||
|
114
firmware/targets/f5/api-hal/api-hal-irda.c
Normal file
114
firmware/targets/f5/api-hal/api-hal-irda.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "api-hal-tim_i.h"
|
||||
#include "api-hal-irda.h"
|
||||
#include <stm32wbxx_ll_tim.h>
|
||||
#include <stm32wbxx_ll_gpio.h>
|
||||
#include <stdio.h>
|
||||
#include <furi.h>
|
||||
#include "main.h"
|
||||
#include "api-hal-pwm.h"
|
||||
|
||||
|
||||
static struct{
|
||||
TimerISRCallback callback;
|
||||
void *ctx;
|
||||
} timer_irda;
|
||||
|
||||
|
||||
void api_hal_irda_tim_isr(TimerIRQSource source)
|
||||
{
|
||||
uint32_t duration = 0;
|
||||
bool level = 0;
|
||||
|
||||
switch (source) {
|
||||
case TimerIRQSourceCCI1:
|
||||
duration = LL_TIM_OC_GetCompareCH1(TIM2);
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
level = 1;
|
||||
break;
|
||||
case TimerIRQSourceCCI2:
|
||||
duration = LL_TIM_OC_GetCompareCH2(TIM2);
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
level = 0;
|
||||
break;
|
||||
default:
|
||||
furi_check(0);
|
||||
}
|
||||
|
||||
if (timer_irda.callback)
|
||||
timer_irda.callback(timer_irda.ctx, level, duration);
|
||||
}
|
||||
|
||||
void api_hal_irda_rx_irq_init(void)
|
||||
{
|
||||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
||||
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
|
||||
/**TIM2 GPIO Configuration
|
||||
PA0 ------> TIM2_CH1
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
TIM_InitStruct.Prescaler = 64 - 1;
|
||||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||||
TIM_InitStruct.Autoreload = 0xFFFFFFFF;
|
||||
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
||||
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
||||
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
||||
LL_TIM_EnableARRPreload(TIM2);
|
||||
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
|
||||
LL_TIM_DisableMasterSlaveMode(TIM2);
|
||||
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
||||
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
|
||||
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
|
||||
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
|
||||
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_INDIRECTTI);
|
||||
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
|
||||
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
|
||||
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
|
||||
|
||||
LL_TIM_EnableIT_CC1(TIM2);
|
||||
LL_TIM_EnableIT_CC2(TIM2);
|
||||
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
|
||||
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
||||
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
|
||||
NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
}
|
||||
|
||||
void api_hal_irda_rx_irq_deinit(void) {
|
||||
LL_TIM_DisableIT_CC1(TIM2);
|
||||
LL_TIM_DisableIT_CC2(TIM2);
|
||||
LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH1);
|
||||
LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
||||
}
|
||||
|
||||
void api_hal_irda_rx_irq_set_callback(TimerISRCallback callback, void *ctx) {
|
||||
furi_check(callback);
|
||||
|
||||
timer_irda.callback = callback;
|
||||
timer_irda.ctx = ctx;
|
||||
}
|
||||
|
||||
void api_hal_irda_pwm_set(float value, float freq) {
|
||||
hal_pwmn_set(value, freq, &IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
||||
|
||||
void api_hal_irda_pwm_stop() {
|
||||
hal_pwmn_stop(&IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
||||
|
10
firmware/targets/f5/api-hal/api-hal-irda_i.h
Normal file
10
firmware/targets/f5/api-hal/api-hal-irda_i.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "api-hal-tim_i.h"
|
||||
|
||||
/**
|
||||
* Function to handle IRDA timer ISR.
|
||||
*
|
||||
* @param source - reason for interrupt request.
|
||||
*/
|
||||
void api_hal_irda_tim_isr(TimerIRQSource source);
|
||||
|
@@ -48,10 +48,3 @@ void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
|
||||
HAL_TIMEx_PWMN_Stop(tim, channel);
|
||||
}
|
||||
|
||||
void irda_pwm_set(float value, float freq) {
|
||||
hal_pwmn_set(value, freq, &IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
||||
|
||||
void irda_pwm_stop() {
|
||||
hal_pwmn_stop(&IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
@@ -11,9 +11,6 @@ void hal_pwmn_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t chan
|
||||
void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel);
|
||||
void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel);
|
||||
|
||||
void irda_pwm_set(float value, float freq);
|
||||
void irda_pwm_stop();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
@@ -1,47 +1,46 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "api-hal-tim.h"
|
||||
#include "api-hal-tim_i.h"
|
||||
#include "api-hal-irda_i.h"
|
||||
#include <stm32wbxx_ll_tim.h>
|
||||
#include <furi.h>
|
||||
|
||||
/* setup TIM2 CH1 and CH2 to capture rising and falling events */
|
||||
void tim_irda_rx_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();
|
||||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
bool consumed = false;
|
||||
|
||||
if(LL_TIM_IsActiveFlag_CC1(TIM2) == 1) {
|
||||
if (LL_TIM_IsEnabledIT_CC1(TIM2)) {
|
||||
LL_TIM_ClearFlag_CC1(TIM2);
|
||||
|
||||
if (READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
|
||||
// input capture
|
||||
api_hal_irda_tim_isr(TimerIRQSourceCCI1);
|
||||
consumed = true;
|
||||
}
|
||||
else {
|
||||
// output compare
|
||||
// HAL_TIM_OC_DelayElapsedCallback(htim);
|
||||
// HAL_TIM_PWM_PulseFinishedCallback(htim);
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
if(LL_TIM_IsActiveFlag_CC2(TIM2) == 1) {
|
||||
if (LL_TIM_IsEnabledIT_CC2(TIM2)) {
|
||||
LL_TIM_ClearFlag_CC2(TIM2);
|
||||
|
||||
if (READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
|
||||
// input capture
|
||||
api_hal_irda_tim_isr(TimerIRQSourceCCI2);
|
||||
consumed = true;
|
||||
}
|
||||
else {
|
||||
// output compare
|
||||
// HAL_TIM_OC_DelayElapsedCallback(htim);
|
||||
// HAL_TIM_PWM_PulseFinishedCallback(htim);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0);
|
||||
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
|
||||
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
|
||||
}
|
||||
furi_check(consumed);
|
||||
}
|
||||
|
||||
|
@@ -1,4 +0,0 @@
|
||||
#pragma once
|
||||
#include "main.h"
|
||||
|
||||
void tim_irda_rx_init(void);
|
||||
|
7
firmware/targets/f5/api-hal/api-hal-tim_i.h
Normal file
7
firmware/targets/f5/api-hal/api-hal-tim_i.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum{
|
||||
TimerIRQSourceCCI1,
|
||||
TimerIRQSourceCCI2,
|
||||
} TimerIRQSource;
|
||||
|
@@ -10,11 +10,6 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef* hcomp) {
|
||||
api_interrupt_call(InterruptTypeComparatorTrigger, hcomp);
|
||||
}
|
||||
|
||||
/* Timer input capture event */
|
||||
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) {
|
||||
api_interrupt_call(InterruptTypeTimerCapture, htim);
|
||||
}
|
||||
|
||||
/* Output compare event */
|
||||
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
api_interrupt_call(InterruptTypeTimerOutputCompare, htim);
|
||||
@@ -24,3 +19,8 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
api_interrupt_call(InterruptTypeTimerUpdate, htim);
|
||||
}
|
||||
|
||||
/* External interrupt event */
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
||||
api_interrupt_call(InterruptTypeExternalInterrupt, (void*)(uint32_t)GPIO_Pin);
|
||||
}
|
||||
|
@@ -75,6 +75,7 @@ C_SOURCES += \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_gpio.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_i2c.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_rcc.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_tim.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_lptim.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_usb.c \
|
||||
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/croutine.c \
|
||||
|
@@ -86,10 +86,6 @@ void TIM1_CC_IRQHandler(void) {
|
||||
HAL_TIM_IRQHandler(&htim1);
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler(void) {
|
||||
HAL_TIM_IRQHandler(&htim2);
|
||||
}
|
||||
|
||||
void HSEM_IRQHandler(void) {
|
||||
HAL_HSEM_IRQHandler();
|
||||
}
|
||||
|
114
firmware/targets/f6/api-hal/api-hal-irda.c
Normal file
114
firmware/targets/f6/api-hal/api-hal-irda.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "api-hal-tim_i.h"
|
||||
#include "api-hal-irda.h"
|
||||
#include <stm32wbxx_ll_tim.h>
|
||||
#include <stm32wbxx_ll_gpio.h>
|
||||
#include <stdio.h>
|
||||
#include <furi.h>
|
||||
#include "main.h"
|
||||
#include "api-hal-pwm.h"
|
||||
|
||||
|
||||
static struct{
|
||||
TimerISRCallback callback;
|
||||
void *ctx;
|
||||
} timer_irda;
|
||||
|
||||
|
||||
void api_hal_irda_tim_isr(TimerIRQSource source)
|
||||
{
|
||||
uint32_t duration = 0;
|
||||
bool level = 0;
|
||||
|
||||
switch (source) {
|
||||
case TimerIRQSourceCCI1:
|
||||
duration = LL_TIM_OC_GetCompareCH1(TIM2);
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
level = 1;
|
||||
break;
|
||||
case TimerIRQSourceCCI2:
|
||||
duration = LL_TIM_OC_GetCompareCH2(TIM2);
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
level = 0;
|
||||
break;
|
||||
default:
|
||||
furi_check(0);
|
||||
}
|
||||
|
||||
if (timer_irda.callback)
|
||||
timer_irda.callback(timer_irda.ctx, level, duration);
|
||||
}
|
||||
|
||||
void api_hal_irda_rx_irq_init(void)
|
||||
{
|
||||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
||||
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
|
||||
/**TIM2 GPIO Configuration
|
||||
PA0 ------> TIM2_CH1
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
TIM_InitStruct.Prescaler = 64 - 1;
|
||||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||||
TIM_InitStruct.Autoreload = 0xFFFFFFFF;
|
||||
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
||||
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
||||
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
||||
LL_TIM_EnableARRPreload(TIM2);
|
||||
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
|
||||
LL_TIM_DisableMasterSlaveMode(TIM2);
|
||||
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
||||
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
|
||||
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
|
||||
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
|
||||
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_INDIRECTTI);
|
||||
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
|
||||
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
|
||||
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
|
||||
|
||||
LL_TIM_EnableIT_CC1(TIM2);
|
||||
LL_TIM_EnableIT_CC2(TIM2);
|
||||
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
|
||||
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
||||
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
|
||||
NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
}
|
||||
|
||||
void api_hal_irda_rx_irq_deinit(void) {
|
||||
LL_TIM_DisableIT_CC1(TIM2);
|
||||
LL_TIM_DisableIT_CC2(TIM2);
|
||||
LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH1);
|
||||
LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
||||
}
|
||||
|
||||
void api_hal_irda_rx_irq_set_callback(TimerISRCallback callback, void *ctx) {
|
||||
furi_check(callback);
|
||||
|
||||
timer_irda.callback = callback;
|
||||
timer_irda.ctx = ctx;
|
||||
}
|
||||
|
||||
void api_hal_irda_pwm_set(float value, float freq) {
|
||||
hal_pwmn_set(value, freq, &IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
||||
|
||||
void api_hal_irda_pwm_stop() {
|
||||
hal_pwmn_stop(&IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
||||
|
10
firmware/targets/f6/api-hal/api-hal-irda_i.h
Normal file
10
firmware/targets/f6/api-hal/api-hal-irda_i.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "api-hal-tim_i.h"
|
||||
|
||||
/**
|
||||
* Function to handle IRDA timer ISR.
|
||||
*
|
||||
* @param source - reason for interrupt request.
|
||||
*/
|
||||
void api_hal_irda_tim_isr(TimerIRQSource source);
|
||||
|
@@ -48,10 +48,3 @@ void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
|
||||
HAL_TIMEx_PWMN_Stop(tim, channel);
|
||||
}
|
||||
|
||||
void irda_pwm_set(float value, float freq) {
|
||||
hal_pwmn_set(value, freq, &IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
||||
|
||||
void irda_pwm_stop() {
|
||||
hal_pwmn_stop(&IRDA_TX_TIM, IRDA_TX_CH);
|
||||
}
|
@@ -11,9 +11,6 @@ void hal_pwmn_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t chan
|
||||
void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel);
|
||||
void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel);
|
||||
|
||||
void irda_pwm_set(float value, float freq);
|
||||
void irda_pwm_stop();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
@@ -1,47 +1,46 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "api-hal-tim.h"
|
||||
#include "api-hal-tim_i.h"
|
||||
#include "api-hal-irda_i.h"
|
||||
#include <stm32wbxx_ll_tim.h>
|
||||
#include <furi.h>
|
||||
|
||||
/* setup TIM2 CH1 and CH2 to capture rising and falling events */
|
||||
void tim_irda_rx_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();
|
||||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
bool consumed = false;
|
||||
|
||||
if(LL_TIM_IsActiveFlag_CC1(TIM2) == 1) {
|
||||
if (LL_TIM_IsEnabledIT_CC1(TIM2)) {
|
||||
LL_TIM_ClearFlag_CC1(TIM2);
|
||||
|
||||
if (READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
|
||||
// input capture
|
||||
api_hal_irda_tim_isr(TimerIRQSourceCCI1);
|
||||
consumed = true;
|
||||
}
|
||||
else {
|
||||
// output compare
|
||||
// HAL_TIM_OC_DelayElapsedCallback(htim);
|
||||
// HAL_TIM_PWM_PulseFinishedCallback(htim);
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
if(LL_TIM_IsActiveFlag_CC2(TIM2) == 1) {
|
||||
if (LL_TIM_IsEnabledIT_CC2(TIM2)) {
|
||||
LL_TIM_ClearFlag_CC2(TIM2);
|
||||
|
||||
if (READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
|
||||
// input capture
|
||||
api_hal_irda_tim_isr(TimerIRQSourceCCI2);
|
||||
consumed = true;
|
||||
}
|
||||
else {
|
||||
// output compare
|
||||
// HAL_TIM_OC_DelayElapsedCallback(htim);
|
||||
// HAL_TIM_PWM_PulseFinishedCallback(htim);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0);
|
||||
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
|
||||
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
|
||||
}
|
||||
furi_check(consumed);
|
||||
}
|
||||
|
||||
|
@@ -1,4 +0,0 @@
|
||||
#pragma once
|
||||
#include "main.h"
|
||||
|
||||
void tim_irda_rx_init(void);
|
||||
|
7
firmware/targets/f6/api-hal/api-hal-tim_i.h
Normal file
7
firmware/targets/f6/api-hal/api-hal-tim_i.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum{
|
||||
TimerIRQSourceCCI1,
|
||||
TimerIRQSourceCCI2,
|
||||
} TimerIRQSource;
|
||||
|
@@ -10,11 +10,6 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef* hcomp) {
|
||||
api_interrupt_call(InterruptTypeComparatorTrigger, hcomp);
|
||||
}
|
||||
|
||||
/* Timer input capture event */
|
||||
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) {
|
||||
api_interrupt_call(InterruptTypeTimerCapture, htim);
|
||||
}
|
||||
|
||||
/* Output compare event */
|
||||
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
api_interrupt_call(InterruptTypeTimerOutputCompare, htim);
|
||||
@@ -24,3 +19,8 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
api_interrupt_call(InterruptTypeTimerUpdate, htim);
|
||||
}
|
||||
|
||||
/* External interrupt event */
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
||||
api_interrupt_call(InterruptTypeExternalInterrupt, (void*)(uint32_t)GPIO_Pin);
|
||||
}
|
||||
|
@@ -75,6 +75,7 @@ C_SOURCES += \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_gpio.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_i2c.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_rcc.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_tim.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_lptim.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_usb.c \
|
||||
$(CUBE_DIR)/Middlewares/Third_Party/FreeRTOS/Source/croutine.c \
|
||||
|
Reference in New Issue
Block a user