2023-02-07 16:33:05 +00:00
|
|
|
#include <furi_hal_infrared.h>
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <core/check.h>
|
2021-08-05 21:11:35 +00:00
|
|
|
#include "stm32wbxx_ll_dma.h"
|
|
|
|
#include "sys/_stdint.h"
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_interrupt.h>
|
|
|
|
#include <furi_hal_resources.h>
|
2021-06-29 21:19:20 +00:00
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
#include <stdint.h>
|
2021-05-18 10:51:00 +00:00
|
|
|
#include <stm32wbxx_ll_tim.h>
|
|
|
|
#include <stm32wbxx_ll_gpio.h>
|
2021-06-29 21:19:20 +00:00
|
|
|
|
2021-05-18 10:51:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <furi.h>
|
2021-08-05 21:11:35 +00:00
|
|
|
#include <math.h>
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
#define INFRARED_TX_DEBUG 0
|
2021-08-19 00:18:42 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
#if INFRARED_TX_DEBUG == 1
|
|
|
|
#define gpio_infrared_tx gpio_infrared_tx_debug
|
2023-04-06 09:06:19 +00:00
|
|
|
const GpioPin gpio_infrared_tx_debug = {.port = GPIOA, .pin = GpioModeAnalog};
|
2021-08-19 00:18:42 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
#define INFRARED_TIM_TX_DMA_BUFFER_SIZE 200
|
|
|
|
#define INFRARED_POLARITY_SHIFT 1
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
#define INFRARED_TX_CCMR_HIGH \
|
|
|
|
(TIM_CCMR2_OC3PE | LL_TIM_OCMODE_PWM2) /* Mark time - enable PWM2 mode */
|
|
|
|
#define INFRARED_TX_CCMR_LOW \
|
2022-01-05 16:10:18 +00:00
|
|
|
(TIM_CCMR2_OC3PE | LL_TIM_OCMODE_FORCED_INACTIVE) /* Space time - force low */
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
/* DMA Channels definition */
|
|
|
|
#define IR_DMA DMA2
|
|
|
|
#define IR_DMA_CH1_CHANNEL LL_DMA_CHANNEL_1
|
|
|
|
#define IR_DMA_CH2_CHANNEL LL_DMA_CHANNEL_2
|
|
|
|
#define IR_DMA_CH1_IRQ FuriHalInterruptIdDma2Ch1
|
|
|
|
#define IR_DMA_CH2_IRQ FuriHalInterruptIdDma2Ch2
|
|
|
|
#define IR_DMA_CH1_DEF IR_DMA, IR_DMA_CH1_CHANNEL
|
|
|
|
#define IR_DMA_CH2_DEF IR_DMA, IR_DMA_CH2_CHANNEL
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
typedef struct {
|
2022-02-25 15:22:58 +00:00
|
|
|
FuriHalInfraredRxCaptureCallback capture_callback;
|
2022-01-05 16:10:18 +00:00
|
|
|
void* capture_context;
|
2022-02-25 15:22:58 +00:00
|
|
|
FuriHalInfraredRxTimeoutCallback timeout_callback;
|
2022-01-05 16:10:18 +00:00
|
|
|
void* timeout_context;
|
2022-02-25 15:22:58 +00:00
|
|
|
} InfraredTimRx;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
typedef struct {
|
2021-08-05 21:11:35 +00:00
|
|
|
uint8_t* polarity;
|
|
|
|
uint16_t* data;
|
|
|
|
size_t size;
|
|
|
|
bool packet_end;
|
|
|
|
bool last_packet_end;
|
2022-02-25 15:22:58 +00:00
|
|
|
} InfraredTxBuf;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2021-08-05 21:11:35 +00:00
|
|
|
typedef struct {
|
|
|
|
float cycle_duration;
|
2022-02-25 15:22:58 +00:00
|
|
|
FuriHalInfraredTxGetDataISRCallback data_callback;
|
|
|
|
FuriHalInfraredTxSignalSentISRCallback signal_sent_callback;
|
2021-08-05 21:11:35 +00:00
|
|
|
void* data_context;
|
2021-08-11 17:51:06 +00:00
|
|
|
void* signal_sent_context;
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredTxBuf buffer[2];
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriSemaphore* stop_semaphore;
|
2022-01-05 16:10:18 +00:00
|
|
|
uint32_t
|
|
|
|
tx_timing_rest_duration; /** if timing is too long (> 0xFFFF), send it in few iterations */
|
2021-08-19 00:18:42 +00:00
|
|
|
bool tx_timing_rest_level;
|
2022-02-25 15:22:58 +00:00
|
|
|
FuriHalInfraredTxGetDataState tx_timing_rest_status;
|
|
|
|
} InfraredTimTx;
|
2021-07-16 16:43:54 +00:00
|
|
|
|
2021-08-05 21:11:35 +00:00
|
|
|
typedef enum {
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStateIdle, /** Furi Hal Infrared is ready to start RX or TX */
|
|
|
|
InfraredStateAsyncRx, /** Async RX started */
|
|
|
|
InfraredStateAsyncTx, /** Async TX started, DMA and timer is on */
|
|
|
|
InfraredStateAsyncTxStopReq, /** Async TX started, async stop request received */
|
|
|
|
InfraredStateAsyncTxStopInProgress, /** Async TX started, stop request is processed and we wait for last data to be sent */
|
|
|
|
InfraredStateAsyncTxStopped, /** Async TX complete, cleanup needed */
|
|
|
|
InfraredStateMAX,
|
|
|
|
} InfraredState;
|
|
|
|
|
|
|
|
static volatile InfraredState furi_hal_infrared_state = InfraredStateIdle;
|
|
|
|
static InfraredTimTx infrared_tim_tx;
|
|
|
|
static InfraredTimRx infrared_tim_rx;
|
|
|
|
|
|
|
|
static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
|
|
|
|
static void furi_hal_infrared_async_tx_free_resources(void);
|
|
|
|
static void furi_hal_infrared_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift);
|
|
|
|
static void furi_hal_infrared_tx_dma_set_buffer(uint8_t buf_num);
|
|
|
|
static void furi_hal_infrared_tx_fill_buffer_last(uint8_t buf_num);
|
|
|
|
static uint8_t furi_hal_infrared_get_current_dma_tx_buffer(void);
|
|
|
|
static void furi_hal_infrared_tx_dma_polarity_isr();
|
|
|
|
static void furi_hal_infrared_tx_dma_isr();
|
|
|
|
|
|
|
|
static void furi_hal_infrared_tim_rx_isr() {
|
2021-08-19 00:18:42 +00:00
|
|
|
static uint32_t previous_captured_ch2 = 0;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2021-08-05 21:11:35 +00:00
|
|
|
/* Timeout */
|
2021-07-16 16:43:54 +00:00
|
|
|
if(LL_TIM_IsActiveFlag_CC3(TIM2)) {
|
|
|
|
LL_TIM_ClearFlag_CC3(TIM2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
|
|
|
/* Timers CNT register starts to counting from 0 to ARR, but it is
|
|
|
|
* reseted when Channel 1 catches interrupt. It is not reseted by
|
|
|
|
* channel 2, though, so we have to distract it's values (see TimerIRQSourceCCI1 ISR).
|
|
|
|
* This can cause false timeout: when time is over, but we started
|
|
|
|
* receiving new signal few microseconds ago, because CNT register
|
|
|
|
* is reseted once per period, not per sample. */
|
2022-02-25 15:22:58 +00:00
|
|
|
if(LL_GPIO_IsInputPinSet(gpio_infrared_rx.port, gpio_infrared_rx.pin) != 0) {
|
|
|
|
if(infrared_tim_rx.timeout_callback)
|
|
|
|
infrared_tim_rx.timeout_callback(infrared_tim_rx.timeout_context);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
2021-08-05 21:11:35 +00:00
|
|
|
|
|
|
|
/* Rising Edge */
|
2021-07-16 16:43:54 +00:00
|
|
|
if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
|
2021-06-29 21:19:20 +00:00
|
|
|
LL_TIM_ClearFlag_CC1(TIM2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
2021-06-29 21:19:20 +00:00
|
|
|
|
|
|
|
if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
/* Low pin level is a Mark state of INFRARED signal. Invert level for further processing. */
|
2021-08-19 00:18:42 +00:00
|
|
|
uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - previous_captured_ch2;
|
2022-02-25 15:22:58 +00:00
|
|
|
if(infrared_tim_rx.capture_callback)
|
|
|
|
infrared_tim_rx.capture_callback(infrared_tim_rx.capture_context, 1, duration);
|
2021-08-05 21:11:35 +00:00
|
|
|
} else {
|
|
|
|
furi_assert(0);
|
2021-06-29 21:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-05 21:11:35 +00:00
|
|
|
|
|
|
|
/* Falling Edge */
|
2021-07-16 16:43:54 +00:00
|
|
|
if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
|
2021-06-29 21:19:20 +00:00
|
|
|
LL_TIM_ClearFlag_CC2(TIM2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
2021-06-29 21:19:20 +00:00
|
|
|
|
|
|
|
if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
/* High pin level is a Space state of INFRARED signal. Invert level for further processing. */
|
2021-08-05 21:11:35 +00:00
|
|
|
uint32_t duration = LL_TIM_IC_GetCaptureCH2(TIM2);
|
2021-08-19 00:18:42 +00:00
|
|
|
previous_captured_ch2 = duration;
|
2022-02-25 15:22:58 +00:00
|
|
|
if(infrared_tim_rx.capture_callback)
|
|
|
|
infrared_tim_rx.capture_callback(infrared_tim_rx.capture_context, 0, duration);
|
2021-08-05 21:11:35 +00:00
|
|
|
} else {
|
|
|
|
furi_assert(0);
|
2021-06-29 21:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_rx_start(void) {
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateIdle);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
2022-02-25 15:22:58 +00:00
|
|
|
&gpio_infrared_rx, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
|
2021-07-16 16:43:54 +00:00
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
2021-05-18 10:51:00 +00:00
|
|
|
TIM_InitStruct.Prescaler = 64 - 1;
|
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
2021-07-16 16:43:54 +00:00
|
|
|
TIM_InitStruct.Autoreload = 0x7FFFFFFE;
|
2021-05-18 10:51:00 +00:00
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
|
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
2021-07-16 16:43:54 +00:00
|
|
|
|
2021-05-18 10:51:00 +00:00
|
|
|
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
2021-07-16 16:43:54 +00:00
|
|
|
LL_TIM_DisableARRPreload(TIM2);
|
|
|
|
LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI1FP1);
|
|
|
|
LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
|
|
|
|
LL_TIM_CC_DisableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
|
|
|
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_FALLING);
|
|
|
|
LL_TIM_DisableIT_TRIG(TIM2);
|
|
|
|
LL_TIM_DisableDMAReq_TRIG(TIM2);
|
2021-05-18 10:51:00 +00:00
|
|
|
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
|
2021-07-16 16:43:54 +00:00
|
|
|
LL_TIM_EnableMasterSlaveMode(TIM2);
|
2021-05-18 10:51:00 +00:00
|
|
|
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);
|
2021-07-16 16:43:54 +00:00
|
|
|
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
|
2021-05-18 10:51:00 +00:00
|
|
|
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);
|
|
|
|
|
2022-03-29 17:37:23 +00:00
|
|
|
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, furi_hal_infrared_tim_rx_isr, NULL);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_state = InfraredStateAsyncRx;
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2021-05-18 10:51:00 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_rx_stop(void) {
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
2022-03-25 10:33:01 +00:00
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
|
2021-06-29 21:19:20 +00:00
|
|
|
LL_TIM_DeInit(TIM2);
|
2022-03-29 17:37:23 +00:00
|
|
|
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_state = InfraredStateIdle;
|
2022-03-25 10:33:01 +00:00
|
|
|
|
|
|
|
FURI_CRITICAL_EXIT();
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_rx_set_timeout(uint32_t timeout_us) {
|
2021-08-11 17:51:06 +00:00
|
|
|
LL_TIM_OC_SetCompareCH3(TIM2, timeout_us);
|
2021-07-16 16:43:54 +00:00
|
|
|
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_ACTIVE);
|
|
|
|
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH3);
|
|
|
|
LL_TIM_EnableIT_CC3(TIM2);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool furi_hal_infrared_is_busy(void) {
|
|
|
|
return furi_hal_infrared_state != InfraredStateIdle;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_rx_set_capture_isr_callback(
|
|
|
|
FuriHalInfraredRxCaptureCallback callback,
|
2022-01-05 16:10:18 +00:00
|
|
|
void* ctx) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_rx.capture_callback = callback;
|
|
|
|
infrared_tim_rx.capture_context = ctx;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_rx_set_timeout_isr_callback(
|
|
|
|
FuriHalInfraredRxTimeoutCallback callback,
|
2022-01-05 16:10:18 +00:00
|
|
|
void* ctx) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_rx.timeout_callback = callback;
|
|
|
|
infrared_tim_rx.timeout_context = ctx;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_dma_terminate(void) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_DisableIT_TC(IR_DMA_CH1_DEF);
|
|
|
|
LL_DMA_DisableIT_HT(IR_DMA_CH2_DEF);
|
|
|
|
LL_DMA_DisableIT_TC(IR_DMA_CH2_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_DisableIT_TC(IR_DMA_CH1_DEF);
|
|
|
|
LL_DMA_DisableChannel(IR_DMA_CH2_DEF);
|
|
|
|
LL_DMA_DisableChannel(IR_DMA_CH1_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
LL_TIM_DisableCounter(TIM1);
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriStatus status = furi_semaphore_release(infrared_tim_tx.stop_semaphore);
|
|
|
|
furi_check(status == FuriStatusOk);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTxStopped;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static uint8_t furi_hal_infrared_get_current_dma_tx_buffer(void) {
|
2021-08-05 21:11:35 +00:00
|
|
|
uint8_t buf_num = 0;
|
2023-02-08 04:41:22 +00:00
|
|
|
uint32_t buffer_adr = LL_DMA_GetMemoryAddress(IR_DMA_CH2_DEF);
|
2022-02-25 15:22:58 +00:00
|
|
|
if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[0].data) {
|
2021-08-05 21:11:35 +00:00
|
|
|
buf_num = 0;
|
2022-02-25 15:22:58 +00:00
|
|
|
} else if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[1].data) {
|
2021-08-05 21:11:35 +00:00
|
|
|
buf_num = 1;
|
|
|
|
} else {
|
|
|
|
furi_assert(0);
|
|
|
|
}
|
|
|
|
return buf_num;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_dma_polarity_isr() {
|
2023-02-08 04:41:22 +00:00
|
|
|
#if IR_DMA_CH1_CHANNEL == LL_DMA_CHANNEL_1
|
|
|
|
if(LL_DMA_IsActiveFlag_TE1(IR_DMA)) {
|
|
|
|
LL_DMA_ClearFlag_TE1(IR_DMA);
|
2021-09-15 09:59:49 +00:00
|
|
|
furi_crash(NULL);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
if(LL_DMA_IsActiveFlag_TC1(IR_DMA) && LL_DMA_IsEnabledIT_TC(IR_DMA_CH1_DEF)) {
|
|
|
|
LL_DMA_ClearFlag_TC1(IR_DMA);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
furi_check(
|
2022-02-25 15:22:58 +00:00
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTx) ||
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress));
|
2021-08-05 21:11:35 +00:00
|
|
|
/* actually TC2 is processed and buffer is next buffer */
|
2022-02-25 15:22:58 +00:00
|
|
|
uint8_t next_buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
|
|
|
|
furi_hal_infrared_tx_dma_set_polarity(next_buf_num, 0);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
#else
|
|
|
|
#error Update this code. Would you kindly?
|
|
|
|
#endif
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_dma_isr() {
|
2023-02-08 04:41:22 +00:00
|
|
|
#if IR_DMA_CH2_CHANNEL == LL_DMA_CHANNEL_2
|
|
|
|
if(LL_DMA_IsActiveFlag_TE2(IR_DMA)) {
|
|
|
|
LL_DMA_ClearFlag_TE2(IR_DMA);
|
2021-09-15 09:59:49 +00:00
|
|
|
furi_crash(NULL);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
if(LL_DMA_IsActiveFlag_HT2(IR_DMA) && LL_DMA_IsEnabledIT_HT(IR_DMA_CH2_DEF)) {
|
|
|
|
LL_DMA_ClearFlag_HT2(IR_DMA);
|
2022-02-25 15:22:58 +00:00
|
|
|
uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
|
2021-08-05 21:11:35 +00:00
|
|
|
uint8_t next_buf_num = !buf_num;
|
2022-02-25 15:22:58 +00:00
|
|
|
if(infrared_tim_tx.buffer[buf_num].last_packet_end) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_DisableIT_HT(IR_DMA_CH2_DEF);
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(
|
2022-02-25 15:22:58 +00:00
|
|
|
!infrared_tim_tx.buffer[buf_num].packet_end ||
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTx)) {
|
|
|
|
furi_hal_infrared_tx_fill_buffer(next_buf_num, 0);
|
|
|
|
if(infrared_tim_tx.buffer[next_buf_num].last_packet_end) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_DisableIT_HT(IR_DMA_CH2_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2022-02-25 15:22:58 +00:00
|
|
|
} else if(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) {
|
2021-08-05 21:11:35 +00:00
|
|
|
/* fallthrough */
|
|
|
|
} else {
|
2021-09-15 09:59:49 +00:00
|
|
|
furi_crash(NULL);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
if(LL_DMA_IsActiveFlag_TC2(IR_DMA) && LL_DMA_IsEnabledIT_TC(IR_DMA_CH2_DEF)) {
|
|
|
|
LL_DMA_ClearFlag_TC2(IR_DMA);
|
2022-01-05 16:10:18 +00:00
|
|
|
furi_check(
|
2022-02-25 15:22:58 +00:00
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) ||
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTx));
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
|
2021-08-05 21:11:35 +00:00
|
|
|
uint8_t next_buf_num = !buf_num;
|
2022-02-25 15:22:58 +00:00
|
|
|
if(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) {
|
|
|
|
furi_hal_infrared_tx_dma_terminate();
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_tx.buffer[buf_num].last_packet_end ||
|
|
|
|
(infrared_tim_tx.buffer[buf_num].packet_end &&
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopReq))) {
|
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTxStopInProgress;
|
|
|
|
furi_hal_infrared_tx_fill_buffer_last(next_buf_num);
|
|
|
|
furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
|
2021-08-05 21:11:35 +00:00
|
|
|
} else {
|
|
|
|
/* if it's not end of the packet - continue receiving */
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2022-02-25 15:22:58 +00:00
|
|
|
if(infrared_tim_tx.signal_sent_callback && infrared_tim_tx.buffer[buf_num].packet_end &&
|
|
|
|
(furi_hal_infrared_state != InfraredStateAsyncTxStopped)) {
|
|
|
|
infrared_tim_tx.signal_sent_callback(infrared_tim_tx.signal_sent_context);
|
2021-08-11 17:51:06 +00:00
|
|
|
}
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
#else
|
|
|
|
#error Update this code. Would you kindly?
|
|
|
|
#endif
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_configure_tim_pwm_tx(uint32_t freq, float duty_cycle) {
|
2022-01-05 16:10:18 +00:00
|
|
|
/* LL_DBGMCU_APB2_GRP1_FreezePeriph(LL_DBGMCU_APB2_GRP1_TIM1_STOP); */
|
2021-08-05 21:11:35 +00:00
|
|
|
|
|
|
|
LL_TIM_DisableCounter(TIM1);
|
|
|
|
LL_TIM_SetRepetitionCounter(TIM1, 0);
|
|
|
|
LL_TIM_SetCounter(TIM1, 0);
|
|
|
|
LL_TIM_SetPrescaler(TIM1, 0);
|
|
|
|
LL_TIM_SetCounterMode(TIM1, LL_TIM_COUNTERMODE_UP);
|
|
|
|
LL_TIM_EnableARRPreload(TIM1);
|
2022-01-05 16:10:18 +00:00
|
|
|
LL_TIM_SetAutoReload(
|
|
|
|
TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
|
2022-02-25 15:22:58 +00:00
|
|
|
#if INFRARED_TX_DEBUG == 1
|
2022-01-05 16:10:18 +00:00
|
|
|
LL_TIM_OC_SetCompareCH1(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
|
2021-08-19 00:18:42 +00:00
|
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
|
|
|
|
/* LL_TIM_OCMODE_PWM2 set by DMA */
|
|
|
|
LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
|
|
|
|
LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH1N, LL_TIM_OCPOLARITY_HIGH);
|
|
|
|
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
|
|
|
|
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N);
|
|
|
|
LL_TIM_DisableIT_CC1(TIM1);
|
|
|
|
#else
|
2022-01-05 16:10:18 +00:00
|
|
|
LL_TIM_OC_SetCompareCH3(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
|
2021-08-05 21:11:35 +00:00
|
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
|
|
|
|
/* LL_TIM_OCMODE_PWM2 set by DMA */
|
|
|
|
LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_FORCED_INACTIVE);
|
|
|
|
LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH3N, LL_TIM_OCPOLARITY_HIGH);
|
|
|
|
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
|
|
|
|
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH3N);
|
|
|
|
LL_TIM_DisableIT_CC3(TIM1);
|
2021-08-19 00:18:42 +00:00
|
|
|
#endif
|
2021-08-05 21:11:35 +00:00
|
|
|
LL_TIM_DisableMasterSlaveMode(TIM1);
|
|
|
|
LL_TIM_EnableAllOutputs(TIM1);
|
|
|
|
LL_TIM_DisableIT_UPDATE(TIM1);
|
|
|
|
LL_TIM_EnableDMAReq_UPDATE(TIM1);
|
|
|
|
|
|
|
|
NVIC_SetPriority(TIM1_UP_TIM16_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
|
|
|
|
NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_configure_tim_cmgr2_dma_tx(void) {
|
2021-08-05 21:11:35 +00:00
|
|
|
LL_DMA_InitTypeDef dma_config = {0};
|
2022-02-25 15:22:58 +00:00
|
|
|
#if INFRARED_TX_DEBUG == 1
|
2022-01-05 16:10:18 +00:00
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR1);
|
2021-08-19 00:18:42 +00:00
|
|
|
#else
|
2022-01-05 16:10:18 +00:00
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR2);
|
2021-08-19 00:18:42 +00:00
|
|
|
#endif
|
2022-01-05 16:10:18 +00:00
|
|
|
dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
|
2021-08-05 21:11:35 +00:00
|
|
|
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
|
|
|
dma_config.Mode = LL_DMA_MODE_NORMAL;
|
|
|
|
dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
|
|
|
|
dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
|
|
|
|
/* fill word to have other bits set to 0 */
|
|
|
|
dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
|
|
|
|
dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
|
|
|
|
dma_config.NbData = 0;
|
|
|
|
dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
|
|
|
|
dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_Init(IR_DMA_CH1_DEF, &dma_config);
|
2022-03-29 17:37:23 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
#if IR_DMA_CH1_CHANNEL == LL_DMA_CHANNEL_1
|
|
|
|
LL_DMA_ClearFlag_TE1(IR_DMA);
|
|
|
|
LL_DMA_ClearFlag_TC1(IR_DMA);
|
|
|
|
#else
|
|
|
|
#error Update this code. Would you kindly?
|
|
|
|
#endif
|
2022-03-29 17:37:23 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_EnableIT_TE(IR_DMA_CH1_DEF);
|
|
|
|
LL_DMA_EnableIT_TC(IR_DMA_CH1_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
furi_hal_interrupt_set_isr_ex(IR_DMA_CH1_IRQ, 4, furi_hal_infrared_tx_dma_polarity_isr, NULL);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_configure_tim_rcr_dma_tx(void) {
|
2021-08-05 21:11:35 +00:00
|
|
|
LL_DMA_InitTypeDef dma_config = {0};
|
2022-01-05 16:10:18 +00:00
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->RCR);
|
|
|
|
dma_config.MemoryOrM2MDstAddress = (uint32_t)NULL;
|
2021-08-05 21:11:35 +00:00
|
|
|
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
|
|
|
dma_config.Mode = LL_DMA_MODE_NORMAL;
|
|
|
|
dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
|
|
|
|
dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
|
|
|
|
dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_HALFWORD;
|
|
|
|
dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD;
|
|
|
|
dma_config.NbData = 0;
|
|
|
|
dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
|
|
|
|
dma_config.Priority = LL_DMA_PRIORITY_MEDIUM;
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_Init(IR_DMA_CH2_DEF, &dma_config);
|
2022-03-29 17:37:23 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
#if IR_DMA_CH2_CHANNEL == LL_DMA_CHANNEL_2
|
|
|
|
LL_DMA_ClearFlag_TC2(IR_DMA);
|
|
|
|
LL_DMA_ClearFlag_HT2(IR_DMA);
|
|
|
|
LL_DMA_ClearFlag_TE2(IR_DMA);
|
|
|
|
#else
|
|
|
|
#error Update this code. Would you kindly?
|
|
|
|
#endif
|
2022-03-29 17:37:23 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_EnableIT_TC(IR_DMA_CH2_DEF);
|
|
|
|
LL_DMA_EnableIT_HT(IR_DMA_CH2_DEF);
|
|
|
|
LL_DMA_EnableIT_TE(IR_DMA_CH2_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
furi_hal_interrupt_set_isr_ex(IR_DMA_CH2_IRQ, 5, furi_hal_infrared_tx_dma_isr, NULL);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_fill_buffer_last(uint8_t buf_num) {
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buf_num < 2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state != InfraredStateAsyncRx);
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
furi_assert(infrared_tim_tx.data_callback);
|
|
|
|
InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buffer->data != NULL);
|
Skorp subghz signal archive (#667)
* SubGhz: Add millis() furi, add subghz history struct
* SubGhz: Fix subghz history
* Gubghz: Fix code repeat history, add clean history
* SubGhz: reading and adding keys to history
* Gui: Renaming Sub 1-Ghz -> SubGhz
* Archive: Renaming Sub 1-Ghz -> SubGhz
* SubGhz: Add menu history, modified button for sending a signal, changed output of data about accepted protocol
* Archive: Fix name subghz
* SubGhz: Menu navigation
* Assets: Add assets/SubGHz/icon.png
* Assets: add new icons for subghz
* SubGhz: Fix name Add manually scene
* SubGhz: Fix load icon Read scene. rename encoder struct, rename protocol function load from file, add load raw data protocol, add info pleasant signals all protocol
* SubGhz: fix memory leak
* SubGhz: change of receiving frequency for read scene
* SubGhz: Add save/load frequency and preset, add automatic configuration of transmit/receive to the desired frequency and modulation, add button "save" config scene
* SubGhz: Fix frequency and preset, fix frequency add manualli scene, fix re-executing the parser
* Furi-hal-subghz: add 2-FSK config, fix ook config 650KHz BW Tx filter
* Fix formatting and release build
* SubGhz: Delete read scene
* SubGhz: Fix frequency add manualli scene, refactoring code
* SubGhz: 2 profiles for OOK, fix broken build.
* SubGhz: Add passing static codes from read scene, add notification read scene, refactoring code
* SubGhz: fix assert on worker double stop.
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-08-28 13:51:48 +00:00
|
|
|
(void)buffer->data;
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buffer->polarity != NULL);
|
Skorp subghz signal archive (#667)
* SubGhz: Add millis() furi, add subghz history struct
* SubGhz: Fix subghz history
* Gubghz: Fix code repeat history, add clean history
* SubGhz: reading and adding keys to history
* Gui: Renaming Sub 1-Ghz -> SubGhz
* Archive: Renaming Sub 1-Ghz -> SubGhz
* SubGhz: Add menu history, modified button for sending a signal, changed output of data about accepted protocol
* Archive: Fix name subghz
* SubGhz: Menu navigation
* Assets: Add assets/SubGHz/icon.png
* Assets: add new icons for subghz
* SubGhz: Fix name Add manually scene
* SubGhz: Fix load icon Read scene. rename encoder struct, rename protocol function load from file, add load raw data protocol, add info pleasant signals all protocol
* SubGhz: fix memory leak
* SubGhz: change of receiving frequency for read scene
* SubGhz: Add save/load frequency and preset, add automatic configuration of transmit/receive to the desired frequency and modulation, add button "save" config scene
* SubGhz: Fix frequency and preset, fix frequency add manualli scene, fix re-executing the parser
* Furi-hal-subghz: add 2-FSK config, fix ook config 650KHz BW Tx filter
* Fix formatting and release build
* SubGhz: Delete read scene
* SubGhz: Fix frequency add manualli scene, refactoring code
* SubGhz: 2 profiles for OOK, fix broken build.
* SubGhz: Add passing static codes from read scene, add notification read scene, refactoring code
* SubGhz: fix assert on worker double stop.
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-08-28 13:51:48 +00:00
|
|
|
(void)buffer->polarity;
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_tx.buffer[buf_num].data[0] = 0; // 1 pulse
|
|
|
|
infrared_tim_tx.buffer[buf_num].polarity[0] = INFRARED_TX_CCMR_LOW;
|
|
|
|
infrared_tim_tx.buffer[buf_num].data[1] = 0; // 1 pulse
|
|
|
|
infrared_tim_tx.buffer[buf_num].polarity[1] = INFRARED_TX_CCMR_LOW;
|
|
|
|
infrared_tim_tx.buffer[buf_num].size = 2;
|
|
|
|
infrared_tim_tx.buffer[buf_num].last_packet_end = true;
|
|
|
|
infrared_tim_tx.buffer[buf_num].packet_end = true;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buf_num < 2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state != InfraredStateAsyncRx);
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
furi_assert(infrared_tim_tx.data_callback);
|
|
|
|
InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buffer->data != NULL);
|
|
|
|
furi_assert(buffer->polarity != NULL);
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
FuriHalInfraredTxGetDataState status = FuriHalInfraredTxGetDataStateOk;
|
2021-08-05 21:11:35 +00:00
|
|
|
uint32_t duration = 0;
|
|
|
|
bool level = 0;
|
2022-01-05 16:10:18 +00:00
|
|
|
size_t* size = &buffer->size;
|
2021-08-05 21:11:35 +00:00
|
|
|
size_t polarity_counter = 0;
|
2022-01-05 16:10:18 +00:00
|
|
|
while(polarity_shift--) {
|
2022-02-25 15:22:58 +00:00
|
|
|
buffer->polarity[polarity_counter++] = INFRARED_TX_CCMR_LOW;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
for(*size = 0; (*size < INFRARED_TIM_TX_DMA_BUFFER_SIZE) &&
|
|
|
|
(status == FuriHalInfraredTxGetDataStateOk);) {
|
|
|
|
if(infrared_tim_tx.tx_timing_rest_duration > 0) {
|
|
|
|
if(infrared_tim_tx.tx_timing_rest_duration > 0xFFFF) {
|
2021-08-19 00:18:42 +00:00
|
|
|
buffer->data[*size] = 0xFFFF;
|
2022-02-25 15:22:58 +00:00
|
|
|
status = FuriHalInfraredTxGetDataStateOk;
|
2021-08-19 00:18:42 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
buffer->data[*size] = infrared_tim_tx.tx_timing_rest_duration;
|
|
|
|
status = infrared_tim_tx.tx_timing_rest_status;
|
2021-08-19 00:18:42 +00:00
|
|
|
}
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_tx.tx_timing_rest_duration -= buffer->data[*size];
|
|
|
|
buffer->polarity[polarity_counter] = infrared_tim_tx.tx_timing_rest_level ?
|
|
|
|
INFRARED_TX_CCMR_HIGH :
|
|
|
|
INFRARED_TX_CCMR_LOW;
|
2021-09-09 21:37:32 +00:00
|
|
|
++(*size);
|
|
|
|
++polarity_counter;
|
2021-08-19 00:18:42 +00:00
|
|
|
continue;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
status = infrared_tim_tx.data_callback(infrared_tim_tx.data_context, &duration, &level);
|
2021-08-19 00:18:42 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
uint32_t num_of_impulses = roundf(duration / infrared_tim_tx.cycle_duration);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(num_of_impulses == 0) {
|
2022-02-25 15:22:58 +00:00
|
|
|
if((*size == 0) && (status == FuriHalInfraredTxGetDataStateDone)) {
|
2021-08-19 00:18:42 +00:00
|
|
|
/* if this is one sample in current buffer, but we
|
|
|
|
* have more to send - continue
|
|
|
|
*/
|
2022-02-25 15:22:58 +00:00
|
|
|
status = FuriHalInfraredTxGetDataStateOk;
|
2021-08-19 00:18:42 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if((num_of_impulses - 1) > 0xFFFF) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_tx.tx_timing_rest_duration = num_of_impulses - 1;
|
|
|
|
infrared_tim_tx.tx_timing_rest_status = status;
|
|
|
|
infrared_tim_tx.tx_timing_rest_level = level;
|
|
|
|
status = FuriHalInfraredTxGetDataStateOk;
|
2021-08-19 00:18:42 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
buffer->polarity[polarity_counter] = level ? INFRARED_TX_CCMR_HIGH :
|
|
|
|
INFRARED_TX_CCMR_LOW;
|
2021-08-19 00:18:42 +00:00
|
|
|
buffer->data[*size] = num_of_impulses - 1;
|
2021-09-09 21:37:32 +00:00
|
|
|
++(*size);
|
|
|
|
++polarity_counter;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
buffer->last_packet_end = (status == FuriHalInfraredTxGetDataStateLastDone);
|
|
|
|
buffer->packet_end = buffer->last_packet_end || (status == FuriHalInfraredTxGetDataStateDone);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(*size == 0) {
|
|
|
|
buffer->data[0] = 0; // 1 pulse
|
2022-02-25 15:22:58 +00:00
|
|
|
buffer->polarity[0] = INFRARED_TX_CCMR_LOW;
|
2021-08-19 00:18:42 +00:00
|
|
|
buffer->size = 1;
|
|
|
|
}
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buf_num < 2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buffer->polarity != NULL);
|
|
|
|
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_ENTER();
|
2023-02-08 04:41:22 +00:00
|
|
|
bool channel_enabled = LL_DMA_IsEnabledChannel(IR_DMA_CH1_DEF);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(channel_enabled) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_DisableChannel(IR_DMA_CH1_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_SetMemoryAddress(IR_DMA_CH1_DEF, (uint32_t)buffer->polarity);
|
|
|
|
LL_DMA_SetDataLength(IR_DMA_CH1_DEF, buffer->size + polarity_shift);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(channel_enabled) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_EnableChannel(IR_DMA_CH1_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_tx_dma_set_buffer(uint8_t buf_num) {
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buf_num < 2);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
|
2021-08-05 21:11:35 +00:00
|
|
|
furi_assert(buffer->data != NULL);
|
|
|
|
|
|
|
|
/* non-circular mode requires disabled channel before setup */
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_ENTER();
|
2023-02-08 04:41:22 +00:00
|
|
|
bool channel_enabled = LL_DMA_IsEnabledChannel(IR_DMA_CH2_DEF);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(channel_enabled) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_DisableChannel(IR_DMA_CH2_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_SetMemoryAddress(IR_DMA_CH2_DEF, (uint32_t)buffer->data);
|
|
|
|
LL_DMA_SetDataLength(IR_DMA_CH2_DEF, buffer->size);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(channel_enabled) {
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_EnableChannel(IR_DMA_CH2_DEF);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void furi_hal_infrared_async_tx_free_resources(void) {
|
2022-01-05 16:10:18 +00:00
|
|
|
furi_assert(
|
2022-02-25 15:22:58 +00:00
|
|
|
(furi_hal_infrared_state == InfraredStateIdle) ||
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopped));
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2023-04-06 09:06:19 +00:00
|
|
|
furi_hal_gpio_init(&gpio_infrared_tx, GpioModeAnalog, GpioPullDown, GpioSpeedLow);
|
2023-02-08 04:41:22 +00:00
|
|
|
furi_hal_interrupt_set_isr(IR_DMA_CH1_IRQ, NULL, NULL);
|
|
|
|
furi_hal_interrupt_set_isr(IR_DMA_CH2_IRQ, NULL, NULL);
|
2021-08-05 21:11:35 +00:00
|
|
|
LL_TIM_DeInit(TIM1);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_semaphore_free(infrared_tim_tx.stop_semaphore);
|
2022-02-25 15:22:58 +00:00
|
|
|
free(infrared_tim_tx.buffer[0].data);
|
|
|
|
free(infrared_tim_tx.buffer[1].data);
|
|
|
|
free(infrared_tim_tx.buffer[0].polarity);
|
|
|
|
free(infrared_tim_tx.buffer[1].polarity);
|
|
|
|
|
|
|
|
infrared_tim_tx.buffer[0].data = NULL;
|
|
|
|
infrared_tim_tx.buffer[1].data = NULL;
|
|
|
|
infrared_tim_tx.buffer[0].polarity = NULL;
|
|
|
|
infrared_tim_tx.buffer[1].polarity = NULL;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_tx_start(uint32_t freq, float duty_cycle) {
|
|
|
|
if((duty_cycle > 1) || (duty_cycle <= 0) || (freq > INFRARED_MAX_FREQUENCY) ||
|
|
|
|
(freq < INFRARED_MIN_FREQUENCY) || (infrared_tim_tx.data_callback == NULL)) {
|
2021-09-15 09:59:49 +00:00
|
|
|
furi_crash(NULL);
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateIdle);
|
|
|
|
furi_assert(infrared_tim_tx.buffer[0].data == NULL);
|
|
|
|
furi_assert(infrared_tim_tx.buffer[1].data == NULL);
|
|
|
|
furi_assert(infrared_tim_tx.buffer[0].polarity == NULL);
|
|
|
|
furi_assert(infrared_tim_tx.buffer[1].polarity == NULL);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
size_t alloc_size_data = INFRARED_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
|
|
|
|
infrared_tim_tx.buffer[0].data = malloc(alloc_size_data);
|
|
|
|
infrared_tim_tx.buffer[1].data = malloc(alloc_size_data);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
size_t alloc_size_polarity =
|
2022-02-25 15:22:58 +00:00
|
|
|
(INFRARED_TIM_TX_DMA_BUFFER_SIZE + INFRARED_POLARITY_SHIFT) * sizeof(uint8_t);
|
|
|
|
infrared_tim_tx.buffer[0].polarity = malloc(alloc_size_polarity);
|
|
|
|
infrared_tim_tx.buffer[1].polarity = malloc(alloc_size_polarity);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
infrared_tim_tx.stop_semaphore = furi_semaphore_alloc(1, 0);
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_tx.cycle_duration = 1000000.0 / freq;
|
|
|
|
infrared_tim_tx.tx_timing_rest_duration = 0;
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_tx_fill_buffer(0, INFRARED_POLARITY_SHIFT);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_configure_tim_pwm_tx(freq, duty_cycle);
|
|
|
|
furi_hal_infrared_configure_tim_cmgr2_dma_tx();
|
|
|
|
furi_hal_infrared_configure_tim_rcr_dma_tx();
|
|
|
|
furi_hal_infrared_tx_dma_set_polarity(0, INFRARED_POLARITY_SHIFT);
|
|
|
|
furi_hal_infrared_tx_dma_set_buffer(0);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTx;
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2021-08-19 00:18:42 +00:00
|
|
|
LL_TIM_ClearFlag_UPDATE(TIM1);
|
2023-02-08 04:41:22 +00:00
|
|
|
LL_DMA_EnableChannel(IR_DMA_CH1_DEF);
|
|
|
|
LL_DMA_EnableChannel(IR_DMA_CH2_DEF);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_us(5);
|
2022-01-05 16:10:18 +00:00
|
|
|
LL_TIM_GenerateEvent_UPDATE(TIM1); /* DMA -> TIMx_RCR */
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_us(5);
|
2022-01-05 16:10:18 +00:00
|
|
|
LL_GPIO_ResetOutputPin(
|
2022-02-25 15:22:58 +00:00
|
|
|
gpio_infrared_tx.port, gpio_infrared_tx.pin); /* when disable it prevents false pulse */
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_ex(
|
2022-02-25 15:22:58 +00:00
|
|
|
&gpio_infrared_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_ENTER();
|
2022-01-05 16:10:18 +00:00
|
|
|
LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
|
2021-08-19 00:18:42 +00:00
|
|
|
LL_TIM_EnableCounter(TIM1);
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_tx_wait_termination(void) {
|
|
|
|
furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriStatus status;
|
|
|
|
status = furi_semaphore_acquire(infrared_tim_tx.stop_semaphore, FuriWaitForever);
|
|
|
|
furi_check(status == FuriStatusOk);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_async_tx_free_resources();
|
|
|
|
furi_hal_infrared_state = InfraredStateIdle;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_tx_stop(void) {
|
|
|
|
furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_ENTER();
|
2022-02-25 15:22:58 +00:00
|
|
|
if(furi_hal_infrared_state == InfraredStateAsyncTx)
|
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTxStopReq;
|
2022-02-10 11:20:50 +00:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-08-05 21:11:35 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_hal_infrared_async_tx_wait_termination();
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_tx_set_data_isr_callback(
|
|
|
|
FuriHalInfraredTxGetDataISRCallback callback,
|
2022-01-05 16:10:18 +00:00
|
|
|
void* context) {
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateIdle);
|
|
|
|
infrared_tim_tx.data_callback = callback;
|
|
|
|
infrared_tim_tx.data_context = context;
|
2021-08-05 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void furi_hal_infrared_async_tx_set_signal_sent_isr_callback(
|
|
|
|
FuriHalInfraredTxSignalSentISRCallback callback,
|
2022-01-05 16:10:18 +00:00
|
|
|
void* context) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_tim_tx.signal_sent_callback = callback;
|
|
|
|
infrared_tim_tx.signal_sent_context = context;
|
2021-08-11 17:51:06 +00:00
|
|
|
}
|