|
|
|
@@ -1,4 +1,4 @@
|
|
|
|
|
#include "furi_hal_irda.h"
|
|
|
|
|
#include "furi_hal_infrared.h"
|
|
|
|
|
#include "furi_hal_delay.h"
|
|
|
|
|
#include "furi/check.h"
|
|
|
|
|
#include "stm32wbxx_ll_dma.h"
|
|
|
|
@@ -17,26 +17,27 @@
|
|
|
|
|
#include <main.h>
|
|
|
|
|
#include <furi_hal_pwm.h>
|
|
|
|
|
|
|
|
|
|
#define IRDA_TX_DEBUG 0
|
|
|
|
|
#define INFRARED_TX_DEBUG 0
|
|
|
|
|
|
|
|
|
|
#if IRDA_TX_DEBUG == 1
|
|
|
|
|
#define gpio_irda_tx gpio_irda_tx_debug
|
|
|
|
|
const GpioPin gpio_irda_tx_debug = {.port = GPIOA, .pin = GPIO_PIN_7};
|
|
|
|
|
#if INFRARED_TX_DEBUG == 1
|
|
|
|
|
#define gpio_infrared_tx gpio_infrared_tx_debug
|
|
|
|
|
const GpioPin gpio_infrared_tx_debug = {.port = GPIOA, .pin = GPIO_PIN_7};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define IRDA_TIM_TX_DMA_BUFFER_SIZE 200
|
|
|
|
|
#define IRDA_POLARITY_SHIFT 1
|
|
|
|
|
#define INFRARED_TIM_TX_DMA_BUFFER_SIZE 200
|
|
|
|
|
#define INFRARED_POLARITY_SHIFT 1
|
|
|
|
|
|
|
|
|
|
#define IRDA_TX_CCMR_HIGH (TIM_CCMR2_OC3PE | LL_TIM_OCMODE_PWM2) /* Mark time - enable PWM2 mode */
|
|
|
|
|
#define IRDA_TX_CCMR_LOW \
|
|
|
|
|
#define INFRARED_TX_CCMR_HIGH \
|
|
|
|
|
(TIM_CCMR2_OC3PE | LL_TIM_OCMODE_PWM2) /* Mark time - enable PWM2 mode */
|
|
|
|
|
#define INFRARED_TX_CCMR_LOW \
|
|
|
|
|
(TIM_CCMR2_OC3PE | LL_TIM_OCMODE_FORCED_INACTIVE) /* Space time - force low */
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
FuriHalIrdaRxCaptureCallback capture_callback;
|
|
|
|
|
FuriHalInfraredRxCaptureCallback capture_callback;
|
|
|
|
|
void* capture_context;
|
|
|
|
|
FuriHalIrdaRxTimeoutCallback timeout_callback;
|
|
|
|
|
FuriHalInfraredRxTimeoutCallback timeout_callback;
|
|
|
|
|
void* timeout_context;
|
|
|
|
|
} IrdaTimRx;
|
|
|
|
|
} InfraredTimRx;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
uint8_t* polarity;
|
|
|
|
@@ -44,52 +45,52 @@ typedef struct {
|
|
|
|
|
size_t size;
|
|
|
|
|
bool packet_end;
|
|
|
|
|
bool last_packet_end;
|
|
|
|
|
} IrdaTxBuf;
|
|
|
|
|
} InfraredTxBuf;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
float cycle_duration;
|
|
|
|
|
FuriHalIrdaTxGetDataISRCallback data_callback;
|
|
|
|
|
FuriHalIrdaTxSignalSentISRCallback signal_sent_callback;
|
|
|
|
|
FuriHalInfraredTxGetDataISRCallback data_callback;
|
|
|
|
|
FuriHalInfraredTxSignalSentISRCallback signal_sent_callback;
|
|
|
|
|
void* data_context;
|
|
|
|
|
void* signal_sent_context;
|
|
|
|
|
IrdaTxBuf buffer[2];
|
|
|
|
|
InfraredTxBuf buffer[2];
|
|
|
|
|
osSemaphoreId_t stop_semaphore;
|
|
|
|
|
uint32_t
|
|
|
|
|
tx_timing_rest_duration; /** if timing is too long (> 0xFFFF), send it in few iterations */
|
|
|
|
|
bool tx_timing_rest_level;
|
|
|
|
|
FuriHalIrdaTxGetDataState tx_timing_rest_status;
|
|
|
|
|
} IrdaTimTx;
|
|
|
|
|
FuriHalInfraredTxGetDataState tx_timing_rest_status;
|
|
|
|
|
} InfraredTimTx;
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
IrdaStateIdle, /** Furi Hal Irda is ready to start RX or TX */
|
|
|
|
|
IrdaStateAsyncRx, /** Async RX started */
|
|
|
|
|
IrdaStateAsyncTx, /** Async TX started, DMA and timer is on */
|
|
|
|
|
IrdaStateAsyncTxStopReq, /** Async TX started, async stop request received */
|
|
|
|
|
IrdaStateAsyncTxStopInProgress, /** Async TX started, stop request is processed and we wait for last data to be sent */
|
|
|
|
|
IrdaStateAsyncTxStopped, /** Async TX complete, cleanup needed */
|
|
|
|
|
IrdaStateMAX,
|
|
|
|
|
} IrdaState;
|
|
|
|
|
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 IrdaState furi_hal_irda_state = IrdaStateIdle;
|
|
|
|
|
static IrdaTimTx irda_tim_tx;
|
|
|
|
|
static IrdaTimRx irda_tim_rx;
|
|
|
|
|
static volatile InfraredState furi_hal_infrared_state = InfraredStateIdle;
|
|
|
|
|
static InfraredTimTx infrared_tim_tx;
|
|
|
|
|
static InfraredTimRx infrared_tim_rx;
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
|
|
|
|
|
static void furi_hal_irda_async_tx_free_resources(void);
|
|
|
|
|
static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift);
|
|
|
|
|
static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num);
|
|
|
|
|
static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num);
|
|
|
|
|
static uint8_t furi_hal_irda_get_current_dma_tx_buffer(void);
|
|
|
|
|
static void furi_hal_irda_tx_dma_polarity_isr();
|
|
|
|
|
static void furi_hal_irda_tx_dma_isr();
|
|
|
|
|
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_irda_tim_rx_isr() {
|
|
|
|
|
static void furi_hal_infrared_tim_rx_isr() {
|
|
|
|
|
static uint32_t previous_captured_ch2 = 0;
|
|
|
|
|
|
|
|
|
|
/* Timeout */
|
|
|
|
|
if(LL_TIM_IsActiveFlag_CC3(TIM2)) {
|
|
|
|
|
LL_TIM_ClearFlag_CC3(TIM2);
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
|
|
|
|
|
|
|
|
|
/* Timers CNT register starts to counting from 0 to ARR, but it is
|
|
|
|
|
* reseted when Channel 1 catches interrupt. It is not reseted by
|
|
|
|
@@ -97,22 +98,22 @@ static void furi_hal_irda_tim_rx_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. */
|
|
|
|
|
if(LL_GPIO_IsInputPinSet(gpio_irda_rx.port, gpio_irda_rx.pin) != 0) {
|
|
|
|
|
if(irda_tim_rx.timeout_callback)
|
|
|
|
|
irda_tim_rx.timeout_callback(irda_tim_rx.timeout_context);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Rising Edge */
|
|
|
|
|
if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
|
|
|
|
|
LL_TIM_ClearFlag_CC1(TIM2);
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
|
|
|
|
|
|
|
|
|
if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
|
|
|
|
|
/* Low pin level is a Mark state of IRDA signal. Invert level for further processing. */
|
|
|
|
|
/* Low pin level is a Mark state of INFRARED signal. Invert level for further processing. */
|
|
|
|
|
uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - previous_captured_ch2;
|
|
|
|
|
if(irda_tim_rx.capture_callback)
|
|
|
|
|
irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 1, duration);
|
|
|
|
|
if(infrared_tim_rx.capture_callback)
|
|
|
|
|
infrared_tim_rx.capture_callback(infrared_tim_rx.capture_context, 1, duration);
|
|
|
|
|
} else {
|
|
|
|
|
furi_assert(0);
|
|
|
|
|
}
|
|
|
|
@@ -121,22 +122,22 @@ static void furi_hal_irda_tim_rx_isr() {
|
|
|
|
|
/* Falling Edge */
|
|
|
|
|
if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
|
|
|
|
|
LL_TIM_ClearFlag_CC2(TIM2);
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
|
|
|
|
|
|
|
|
|
if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
|
|
|
|
|
/* High pin level is a Space state of IRDA signal. Invert level for further processing. */
|
|
|
|
|
/* High pin level is a Space state of INFRARED signal. Invert level for further processing. */
|
|
|
|
|
uint32_t duration = LL_TIM_IC_GetCaptureCH2(TIM2);
|
|
|
|
|
previous_captured_ch2 = duration;
|
|
|
|
|
if(irda_tim_rx.capture_callback)
|
|
|
|
|
irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 0, duration);
|
|
|
|
|
if(infrared_tim_rx.capture_callback)
|
|
|
|
|
infrared_tim_rx.capture_callback(infrared_tim_rx.capture_context, 0, duration);
|
|
|
|
|
} else {
|
|
|
|
|
furi_assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_rx_start(void) {
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateIdle);
|
|
|
|
|
void furi_hal_infrared_async_rx_start(void) {
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateIdle);
|
|
|
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
|
|
|
@@ -144,7 +145,7 @@ void furi_hal_irda_async_rx_start(void) {
|
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
|
|
|
|
|
hal_gpio_init_ex(
|
|
|
|
|
&gpio_irda_rx, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
|
|
|
|
|
&gpio_infrared_rx, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
|
|
|
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
|
|
|
TIM_InitStruct.Prescaler = 64 - 1;
|
|
|
|
@@ -171,8 +172,8 @@ void furi_hal_irda_async_rx_start(void) {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_irda_tim_rx_isr);
|
|
|
|
|
furi_hal_irda_state = IrdaStateAsyncRx;
|
|
|
|
|
furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_infrared_tim_rx_isr);
|
|
|
|
|
furi_hal_infrared_state = InfraredStateAsyncRx;
|
|
|
|
|
|
|
|
|
|
LL_TIM_EnableIT_CC1(TIM2);
|
|
|
|
|
LL_TIM_EnableIT_CC2(TIM2);
|
|
|
|
@@ -186,15 +187,15 @@ void furi_hal_irda_async_rx_start(void) {
|
|
|
|
|
NVIC_EnableIRQ(TIM2_IRQn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_rx_stop(void) {
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateAsyncRx);
|
|
|
|
|
void furi_hal_infrared_async_rx_stop(void) {
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncRx);
|
|
|
|
|
LL_TIM_DeInit(TIM2);
|
|
|
|
|
furi_hal_interrupt_set_timer_isr(TIM2, NULL);
|
|
|
|
|
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
|
|
|
|
furi_hal_irda_state = IrdaStateIdle;
|
|
|
|
|
furi_hal_infrared_state = InfraredStateIdle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_rx_set_timeout(uint32_t timeout_us) {
|
|
|
|
|
void furi_hal_infrared_async_rx_set_timeout(uint32_t timeout_us) {
|
|
|
|
|
furi_assert(LL_APB1_GRP1_IsEnabledClock(LL_APB1_GRP1_PERIPH_TIM2));
|
|
|
|
|
|
|
|
|
|
LL_TIM_OC_SetCompareCH3(TIM2, timeout_us);
|
|
|
|
@@ -203,46 +204,46 @@ void furi_hal_irda_async_rx_set_timeout(uint32_t timeout_us) {
|
|
|
|
|
LL_TIM_EnableIT_CC3(TIM2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool furi_hal_irda_is_busy(void) {
|
|
|
|
|
return furi_hal_irda_state != IrdaStateIdle;
|
|
|
|
|
bool furi_hal_infrared_is_busy(void) {
|
|
|
|
|
return furi_hal_infrared_state != InfraredStateIdle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_rx_set_capture_isr_callback(
|
|
|
|
|
FuriHalIrdaRxCaptureCallback callback,
|
|
|
|
|
void furi_hal_infrared_async_rx_set_capture_isr_callback(
|
|
|
|
|
FuriHalInfraredRxCaptureCallback callback,
|
|
|
|
|
void* ctx) {
|
|
|
|
|
irda_tim_rx.capture_callback = callback;
|
|
|
|
|
irda_tim_rx.capture_context = ctx;
|
|
|
|
|
infrared_tim_rx.capture_callback = callback;
|
|
|
|
|
infrared_tim_rx.capture_context = ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_rx_set_timeout_isr_callback(
|
|
|
|
|
FuriHalIrdaRxTimeoutCallback callback,
|
|
|
|
|
void furi_hal_infrared_async_rx_set_timeout_isr_callback(
|
|
|
|
|
FuriHalInfraredRxTimeoutCallback callback,
|
|
|
|
|
void* ctx) {
|
|
|
|
|
irda_tim_rx.timeout_callback = callback;
|
|
|
|
|
irda_tim_rx.timeout_context = ctx;
|
|
|
|
|
infrared_tim_rx.timeout_callback = callback;
|
|
|
|
|
infrared_tim_rx.timeout_context = ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_dma_terminate(void) {
|
|
|
|
|
static void furi_hal_infrared_tx_dma_terminate(void) {
|
|
|
|
|
LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_1);
|
|
|
|
|
LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
|
|
|
|
|
LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_2);
|
|
|
|
|
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateAsyncTxStopInProgress);
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress);
|
|
|
|
|
|
|
|
|
|
LL_DMA_DisableIT_TC(DMA1, LL_DMA_CHANNEL_1);
|
|
|
|
|
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
|
|
|
|
|
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
|
|
|
|
|
LL_TIM_DisableCounter(TIM1);
|
|
|
|
|
osStatus_t status = osSemaphoreRelease(irda_tim_tx.stop_semaphore);
|
|
|
|
|
osStatus_t status = osSemaphoreRelease(infrared_tim_tx.stop_semaphore);
|
|
|
|
|
furi_check(status == osOK);
|
|
|
|
|
furi_hal_irda_state = IrdaStateAsyncTxStopped;
|
|
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTxStopped;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t furi_hal_irda_get_current_dma_tx_buffer(void) {
|
|
|
|
|
static uint8_t furi_hal_infrared_get_current_dma_tx_buffer(void) {
|
|
|
|
|
uint8_t buf_num = 0;
|
|
|
|
|
uint32_t buffer_adr = LL_DMA_GetMemoryAddress(DMA1, LL_DMA_CHANNEL_2);
|
|
|
|
|
if(buffer_adr == (uint32_t)irda_tim_tx.buffer[0].data) {
|
|
|
|
|
if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[0].data) {
|
|
|
|
|
buf_num = 0;
|
|
|
|
|
} else if(buffer_adr == (uint32_t)irda_tim_tx.buffer[1].data) {
|
|
|
|
|
} else if(buffer_adr == (uint32_t)infrared_tim_tx.buffer[1].data) {
|
|
|
|
|
buf_num = 1;
|
|
|
|
|
} else {
|
|
|
|
|
furi_assert(0);
|
|
|
|
@@ -250,7 +251,7 @@ static uint8_t furi_hal_irda_get_current_dma_tx_buffer(void) {
|
|
|
|
|
return buf_num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_dma_polarity_isr() {
|
|
|
|
|
static void furi_hal_infrared_tx_dma_polarity_isr() {
|
|
|
|
|
if(LL_DMA_IsActiveFlag_TE1(DMA1)) {
|
|
|
|
|
LL_DMA_ClearFlag_TE1(DMA1);
|
|
|
|
|
furi_crash(NULL);
|
|
|
|
@@ -259,33 +260,34 @@ static void furi_hal_irda_tx_dma_polarity_isr() {
|
|
|
|
|
LL_DMA_ClearFlag_TC1(DMA1);
|
|
|
|
|
|
|
|
|
|
furi_check(
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTx) ||
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTxStopReq) ||
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTxStopInProgress));
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTx) ||
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress));
|
|
|
|
|
/* actually TC2 is processed and buffer is next buffer */
|
|
|
|
|
uint8_t next_buf_num = furi_hal_irda_get_current_dma_tx_buffer();
|
|
|
|
|
furi_hal_irda_tx_dma_set_polarity(next_buf_num, 0);
|
|
|
|
|
uint8_t next_buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
|
|
|
|
|
furi_hal_infrared_tx_dma_set_polarity(next_buf_num, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_dma_isr() {
|
|
|
|
|
static void furi_hal_infrared_tx_dma_isr() {
|
|
|
|
|
if(LL_DMA_IsActiveFlag_TE2(DMA1)) {
|
|
|
|
|
LL_DMA_ClearFlag_TE2(DMA1);
|
|
|
|
|
furi_crash(NULL);
|
|
|
|
|
}
|
|
|
|
|
if(LL_DMA_IsActiveFlag_HT2(DMA1) && LL_DMA_IsEnabledIT_HT(DMA1, LL_DMA_CHANNEL_2)) {
|
|
|
|
|
LL_DMA_ClearFlag_HT2(DMA1);
|
|
|
|
|
uint8_t buf_num = furi_hal_irda_get_current_dma_tx_buffer();
|
|
|
|
|
uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
|
|
|
|
|
uint8_t next_buf_num = !buf_num;
|
|
|
|
|
if(irda_tim_tx.buffer[buf_num].last_packet_end) {
|
|
|
|
|
if(infrared_tim_tx.buffer[buf_num].last_packet_end) {
|
|
|
|
|
LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
|
|
|
|
|
} else if(
|
|
|
|
|
!irda_tim_tx.buffer[buf_num].packet_end || (furi_hal_irda_state == IrdaStateAsyncTx)) {
|
|
|
|
|
furi_hal_irda_tx_fill_buffer(next_buf_num, 0);
|
|
|
|
|
if(irda_tim_tx.buffer[next_buf_num].last_packet_end) {
|
|
|
|
|
!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) {
|
|
|
|
|
LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
|
|
|
|
|
}
|
|
|
|
|
} else if(furi_hal_irda_state == IrdaStateAsyncTxStopReq) {
|
|
|
|
|
} else if(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) {
|
|
|
|
|
/* fallthrough */
|
|
|
|
|
} else {
|
|
|
|
|
furi_crash(NULL);
|
|
|
|
@@ -294,33 +296,33 @@ static void furi_hal_irda_tx_dma_isr() {
|
|
|
|
|
if(LL_DMA_IsActiveFlag_TC2(DMA1) && LL_DMA_IsEnabledIT_TC(DMA1, LL_DMA_CHANNEL_2)) {
|
|
|
|
|
LL_DMA_ClearFlag_TC2(DMA1);
|
|
|
|
|
furi_check(
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTxStopInProgress) ||
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTxStopReq) ||
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTx));
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) ||
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopReq) ||
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTx));
|
|
|
|
|
|
|
|
|
|
uint8_t buf_num = furi_hal_irda_get_current_dma_tx_buffer();
|
|
|
|
|
uint8_t buf_num = furi_hal_infrared_get_current_dma_tx_buffer();
|
|
|
|
|
uint8_t next_buf_num = !buf_num;
|
|
|
|
|
if(furi_hal_irda_state == IrdaStateAsyncTxStopInProgress) {
|
|
|
|
|
furi_hal_irda_tx_dma_terminate();
|
|
|
|
|
if(furi_hal_infrared_state == InfraredStateAsyncTxStopInProgress) {
|
|
|
|
|
furi_hal_infrared_tx_dma_terminate();
|
|
|
|
|
} else if(
|
|
|
|
|
irda_tim_tx.buffer[buf_num].last_packet_end ||
|
|
|
|
|
(irda_tim_tx.buffer[buf_num].packet_end &&
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTxStopReq))) {
|
|
|
|
|
furi_hal_irda_state = IrdaStateAsyncTxStopInProgress;
|
|
|
|
|
furi_hal_irda_tx_fill_buffer_last(next_buf_num);
|
|
|
|
|
furi_hal_irda_tx_dma_set_buffer(next_buf_num);
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
/* if it's not end of the packet - continue receiving */
|
|
|
|
|
furi_hal_irda_tx_dma_set_buffer(next_buf_num);
|
|
|
|
|
furi_hal_infrared_tx_dma_set_buffer(next_buf_num);
|
|
|
|
|
}
|
|
|
|
|
if(irda_tim_tx.signal_sent_callback && irda_tim_tx.buffer[buf_num].packet_end &&
|
|
|
|
|
(furi_hal_irda_state != IrdaStateAsyncTxStopped)) {
|
|
|
|
|
irda_tim_tx.signal_sent_callback(irda_tim_tx.signal_sent_context);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle) {
|
|
|
|
|
static void furi_hal_infrared_configure_tim_pwm_tx(uint32_t freq, float duty_cycle) {
|
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
|
|
|
|
/* LL_DBGMCU_APB2_GRP1_FreezePeriph(LL_DBGMCU_APB2_GRP1_TIM1_STOP); */
|
|
|
|
|
|
|
|
|
@@ -332,7 +334,7 @@ static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle)
|
|
|
|
|
LL_TIM_EnableARRPreload(TIM1);
|
|
|
|
|
LL_TIM_SetAutoReload(
|
|
|
|
|
TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
|
|
|
|
|
#if IRDA_TX_DEBUG == 1
|
|
|
|
|
#if INFRARED_TX_DEBUG == 1
|
|
|
|
|
LL_TIM_OC_SetCompareCH1(TIM1, ((LL_TIM_GetAutoReload(TIM1) + 1) * (1 - duty_cycle)));
|
|
|
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
|
|
|
|
|
/* LL_TIM_OCMODE_PWM2 set by DMA */
|
|
|
|
@@ -360,11 +362,11 @@ static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle)
|
|
|
|
|
NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_configure_tim_cmgr2_dma_tx(void) {
|
|
|
|
|
static void furi_hal_infrared_configure_tim_cmgr2_dma_tx(void) {
|
|
|
|
|
LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
|
|
|
|
|
|
|
|
|
|
LL_DMA_InitTypeDef dma_config = {0};
|
|
|
|
|
#if IRDA_TX_DEBUG == 1
|
|
|
|
|
#if INFRARED_TX_DEBUG == 1
|
|
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR1);
|
|
|
|
|
#else
|
|
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM1->CCMR2);
|
|
|
|
@@ -382,7 +384,7 @@ static void furi_hal_irda_configure_tim_cmgr2_dma_tx(void) {
|
|
|
|
|
dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
|
|
|
|
|
LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &dma_config);
|
|
|
|
|
furi_hal_interrupt_set_dma_channel_isr(
|
|
|
|
|
DMA1, LL_DMA_CHANNEL_1, furi_hal_irda_tx_dma_polarity_isr);
|
|
|
|
|
DMA1, LL_DMA_CHANNEL_1, furi_hal_infrared_tx_dma_polarity_isr);
|
|
|
|
|
LL_DMA_ClearFlag_TE1(DMA1);
|
|
|
|
|
LL_DMA_ClearFlag_TC1(DMA1);
|
|
|
|
|
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
|
|
|
|
@@ -392,7 +394,7 @@ static void furi_hal_irda_configure_tim_cmgr2_dma_tx(void) {
|
|
|
|
|
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_configure_tim_rcr_dma_tx(void) {
|
|
|
|
|
static void furi_hal_infrared_configure_tim_rcr_dma_tx(void) {
|
|
|
|
|
LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
|
|
|
|
|
|
|
|
|
|
LL_DMA_InitTypeDef dma_config = {0};
|
|
|
|
@@ -408,7 +410,7 @@ static void furi_hal_irda_configure_tim_rcr_dma_tx(void) {
|
|
|
|
|
dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM1_UP;
|
|
|
|
|
dma_config.Priority = LL_DMA_PRIORITY_MEDIUM;
|
|
|
|
|
LL_DMA_Init(DMA1, LL_DMA_CHANNEL_2, &dma_config);
|
|
|
|
|
furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, furi_hal_irda_tx_dma_isr);
|
|
|
|
|
furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, furi_hal_infrared_tx_dma_isr);
|
|
|
|
|
LL_DMA_ClearFlag_TC2(DMA1);
|
|
|
|
|
LL_DMA_ClearFlag_HT2(DMA1);
|
|
|
|
|
LL_DMA_ClearFlag_TE2(DMA1);
|
|
|
|
@@ -420,100 +422,102 @@ static void furi_hal_irda_configure_tim_rcr_dma_tx(void) {
|
|
|
|
|
NVIC_EnableIRQ(DMA1_Channel2_IRQn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num) {
|
|
|
|
|
static void furi_hal_infrared_tx_fill_buffer_last(uint8_t buf_num) {
|
|
|
|
|
furi_assert(buf_num < 2);
|
|
|
|
|
furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
|
|
|
|
|
furi_assert(furi_hal_irda_state < IrdaStateMAX);
|
|
|
|
|
furi_assert(irda_tim_tx.data_callback);
|
|
|
|
|
IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
|
|
|
|
|
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];
|
|
|
|
|
furi_assert(buffer->data != NULL);
|
|
|
|
|
(void)buffer->data;
|
|
|
|
|
furi_assert(buffer->polarity != NULL);
|
|
|
|
|
(void)buffer->polarity;
|
|
|
|
|
|
|
|
|
|
irda_tim_tx.buffer[buf_num].data[0] = 0; // 1 pulse
|
|
|
|
|
irda_tim_tx.buffer[buf_num].polarity[0] = IRDA_TX_CCMR_LOW;
|
|
|
|
|
irda_tim_tx.buffer[buf_num].data[1] = 0; // 1 pulse
|
|
|
|
|
irda_tim_tx.buffer[buf_num].polarity[1] = IRDA_TX_CCMR_LOW;
|
|
|
|
|
irda_tim_tx.buffer[buf_num].size = 2;
|
|
|
|
|
irda_tim_tx.buffer[buf_num].last_packet_end = true;
|
|
|
|
|
irda_tim_tx.buffer[buf_num].packet_end = true;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
|
|
|
|
|
static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
|
|
|
|
|
furi_assert(buf_num < 2);
|
|
|
|
|
furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
|
|
|
|
|
furi_assert(furi_hal_irda_state < IrdaStateMAX);
|
|
|
|
|
furi_assert(irda_tim_tx.data_callback);
|
|
|
|
|
IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
|
|
|
|
|
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];
|
|
|
|
|
furi_assert(buffer->data != NULL);
|
|
|
|
|
furi_assert(buffer->polarity != NULL);
|
|
|
|
|
|
|
|
|
|
FuriHalIrdaTxGetDataState status = FuriHalIrdaTxGetDataStateOk;
|
|
|
|
|
FuriHalInfraredTxGetDataState status = FuriHalInfraredTxGetDataStateOk;
|
|
|
|
|
uint32_t duration = 0;
|
|
|
|
|
bool level = 0;
|
|
|
|
|
size_t* size = &buffer->size;
|
|
|
|
|
size_t polarity_counter = 0;
|
|
|
|
|
while(polarity_shift--) {
|
|
|
|
|
buffer->polarity[polarity_counter++] = IRDA_TX_CCMR_LOW;
|
|
|
|
|
buffer->polarity[polarity_counter++] = INFRARED_TX_CCMR_LOW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(*size = 0;
|
|
|
|
|
(*size < IRDA_TIM_TX_DMA_BUFFER_SIZE) && (status == FuriHalIrdaTxGetDataStateOk);) {
|
|
|
|
|
if(irda_tim_tx.tx_timing_rest_duration > 0) {
|
|
|
|
|
if(irda_tim_tx.tx_timing_rest_duration > 0xFFFF) {
|
|
|
|
|
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) {
|
|
|
|
|
buffer->data[*size] = 0xFFFF;
|
|
|
|
|
status = FuriHalIrdaTxGetDataStateOk;
|
|
|
|
|
status = FuriHalInfraredTxGetDataStateOk;
|
|
|
|
|
} else {
|
|
|
|
|
buffer->data[*size] = irda_tim_tx.tx_timing_rest_duration;
|
|
|
|
|
status = irda_tim_tx.tx_timing_rest_status;
|
|
|
|
|
buffer->data[*size] = infrared_tim_tx.tx_timing_rest_duration;
|
|
|
|
|
status = infrared_tim_tx.tx_timing_rest_status;
|
|
|
|
|
}
|
|
|
|
|
irda_tim_tx.tx_timing_rest_duration -= buffer->data[*size];
|
|
|
|
|
buffer->polarity[polarity_counter] =
|
|
|
|
|
irda_tim_tx.tx_timing_rest_level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
|
|
|
|
|
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;
|
|
|
|
|
++(*size);
|
|
|
|
|
++polarity_counter;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = irda_tim_tx.data_callback(irda_tim_tx.data_context, &duration, &level);
|
|
|
|
|
status = infrared_tim_tx.data_callback(infrared_tim_tx.data_context, &duration, &level);
|
|
|
|
|
|
|
|
|
|
uint32_t num_of_impulses = roundf(duration / irda_tim_tx.cycle_duration);
|
|
|
|
|
uint32_t num_of_impulses = roundf(duration / infrared_tim_tx.cycle_duration);
|
|
|
|
|
|
|
|
|
|
if(num_of_impulses == 0) {
|
|
|
|
|
if((*size == 0) && (status == FuriHalIrdaTxGetDataStateDone)) {
|
|
|
|
|
if((*size == 0) && (status == FuriHalInfraredTxGetDataStateDone)) {
|
|
|
|
|
/* if this is one sample in current buffer, but we
|
|
|
|
|
* have more to send - continue
|
|
|
|
|
*/
|
|
|
|
|
status = FuriHalIrdaTxGetDataStateOk;
|
|
|
|
|
status = FuriHalInfraredTxGetDataStateOk;
|
|
|
|
|
}
|
|
|
|
|
} else if((num_of_impulses - 1) > 0xFFFF) {
|
|
|
|
|
irda_tim_tx.tx_timing_rest_duration = num_of_impulses - 1;
|
|
|
|
|
irda_tim_tx.tx_timing_rest_status = status;
|
|
|
|
|
irda_tim_tx.tx_timing_rest_level = level;
|
|
|
|
|
status = FuriHalIrdaTxGetDataStateOk;
|
|
|
|
|
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;
|
|
|
|
|
} else {
|
|
|
|
|
buffer->polarity[polarity_counter] = level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
|
|
|
|
|
buffer->polarity[polarity_counter] = level ? INFRARED_TX_CCMR_HIGH :
|
|
|
|
|
INFRARED_TX_CCMR_LOW;
|
|
|
|
|
buffer->data[*size] = num_of_impulses - 1;
|
|
|
|
|
++(*size);
|
|
|
|
|
++polarity_counter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer->last_packet_end = (status == FuriHalIrdaTxGetDataStateLastDone);
|
|
|
|
|
buffer->packet_end = buffer->last_packet_end || (status == FuriHalIrdaTxGetDataStateDone);
|
|
|
|
|
buffer->last_packet_end = (status == FuriHalInfraredTxGetDataStateLastDone);
|
|
|
|
|
buffer->packet_end = buffer->last_packet_end || (status == FuriHalInfraredTxGetDataStateDone);
|
|
|
|
|
|
|
|
|
|
if(*size == 0) {
|
|
|
|
|
buffer->data[0] = 0; // 1 pulse
|
|
|
|
|
buffer->polarity[0] = IRDA_TX_CCMR_LOW;
|
|
|
|
|
buffer->polarity[0] = INFRARED_TX_CCMR_LOW;
|
|
|
|
|
buffer->size = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
|
|
|
|
|
static void furi_hal_infrared_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
|
|
|
|
|
furi_assert(buf_num < 2);
|
|
|
|
|
furi_assert(furi_hal_irda_state < IrdaStateMAX);
|
|
|
|
|
IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
|
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
|
InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
|
|
|
|
|
furi_assert(buffer->polarity != NULL);
|
|
|
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
@@ -529,10 +533,10 @@ static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_
|
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num) {
|
|
|
|
|
static void furi_hal_infrared_tx_dma_set_buffer(uint8_t buf_num) {
|
|
|
|
|
furi_assert(buf_num < 2);
|
|
|
|
|
furi_assert(furi_hal_irda_state < IrdaStateMAX);
|
|
|
|
|
IrdaTxBuf* buffer = &irda_tim_tx.buffer[buf_num];
|
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
|
InfraredTxBuf* buffer = &infrared_tim_tx.buffer[buf_num];
|
|
|
|
|
furi_assert(buffer->data != NULL);
|
|
|
|
|
|
|
|
|
|
/* non-circular mode requires disabled channel before setup */
|
|
|
|
@@ -549,66 +553,66 @@ static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num) {
|
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void furi_hal_irda_async_tx_free_resources(void) {
|
|
|
|
|
static void furi_hal_infrared_async_tx_free_resources(void) {
|
|
|
|
|
furi_assert(
|
|
|
|
|
(furi_hal_irda_state == IrdaStateIdle) ||
|
|
|
|
|
(furi_hal_irda_state == IrdaStateAsyncTxStopped));
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateIdle) ||
|
|
|
|
|
(furi_hal_infrared_state == InfraredStateAsyncTxStopped));
|
|
|
|
|
osStatus_t status;
|
|
|
|
|
|
|
|
|
|
hal_gpio_init(&gpio_irda_tx, GpioModeOutputOpenDrain, GpioPullDown, GpioSpeedLow);
|
|
|
|
|
hal_gpio_init(&gpio_infrared_tx, GpioModeOutputOpenDrain, GpioPullDown, GpioSpeedLow);
|
|
|
|
|
furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, NULL);
|
|
|
|
|
furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_2, NULL);
|
|
|
|
|
LL_TIM_DeInit(TIM1);
|
|
|
|
|
LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
|
|
|
|
LL_C2_AHB1_GRP1_DisableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
|
|
|
|
|
|
|
|
|
|
status = osSemaphoreDelete(irda_tim_tx.stop_semaphore);
|
|
|
|
|
status = osSemaphoreDelete(infrared_tim_tx.stop_semaphore);
|
|
|
|
|
furi_check(status == osOK);
|
|
|
|
|
free(irda_tim_tx.buffer[0].data);
|
|
|
|
|
free(irda_tim_tx.buffer[1].data);
|
|
|
|
|
free(irda_tim_tx.buffer[0].polarity);
|
|
|
|
|
free(irda_tim_tx.buffer[1].polarity);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
irda_tim_tx.buffer[0].data = NULL;
|
|
|
|
|
irda_tim_tx.buffer[1].data = NULL;
|
|
|
|
|
irda_tim_tx.buffer[0].polarity = NULL;
|
|
|
|
|
irda_tim_tx.buffer[1].polarity = NULL;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
|
|
|
|
|
if((duty_cycle > 1) || (duty_cycle <= 0) || (freq > IRDA_MAX_FREQUENCY) ||
|
|
|
|
|
(freq < IRDA_MIN_FREQUENCY) || (irda_tim_tx.data_callback == NULL)) {
|
|
|
|
|
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)) {
|
|
|
|
|
furi_crash(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateIdle);
|
|
|
|
|
furi_assert(irda_tim_tx.buffer[0].data == NULL);
|
|
|
|
|
furi_assert(irda_tim_tx.buffer[1].data == NULL);
|
|
|
|
|
furi_assert(irda_tim_tx.buffer[0].polarity == NULL);
|
|
|
|
|
furi_assert(irda_tim_tx.buffer[1].polarity == NULL);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
size_t alloc_size_data = IRDA_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
|
|
|
|
|
irda_tim_tx.buffer[0].data = malloc(alloc_size_data);
|
|
|
|
|
irda_tim_tx.buffer[1].data = malloc(alloc_size_data);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
size_t alloc_size_polarity =
|
|
|
|
|
(IRDA_TIM_TX_DMA_BUFFER_SIZE + IRDA_POLARITY_SHIFT) * sizeof(uint8_t);
|
|
|
|
|
irda_tim_tx.buffer[0].polarity = malloc(alloc_size_polarity);
|
|
|
|
|
irda_tim_tx.buffer[1].polarity = malloc(alloc_size_polarity);
|
|
|
|
|
(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);
|
|
|
|
|
|
|
|
|
|
irda_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
|
|
|
|
|
irda_tim_tx.cycle_duration = 1000000.0 / freq;
|
|
|
|
|
irda_tim_tx.tx_timing_rest_duration = 0;
|
|
|
|
|
infrared_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
|
|
|
|
|
infrared_tim_tx.cycle_duration = 1000000.0 / freq;
|
|
|
|
|
infrared_tim_tx.tx_timing_rest_duration = 0;
|
|
|
|
|
|
|
|
|
|
furi_hal_irda_tx_fill_buffer(0, IRDA_POLARITY_SHIFT);
|
|
|
|
|
furi_hal_infrared_tx_fill_buffer(0, INFRARED_POLARITY_SHIFT);
|
|
|
|
|
|
|
|
|
|
furi_hal_irda_configure_tim_pwm_tx(freq, duty_cycle);
|
|
|
|
|
furi_hal_irda_configure_tim_cmgr2_dma_tx();
|
|
|
|
|
furi_hal_irda_configure_tim_rcr_dma_tx();
|
|
|
|
|
furi_hal_irda_tx_dma_set_polarity(0, IRDA_POLARITY_SHIFT);
|
|
|
|
|
furi_hal_irda_tx_dma_set_buffer(0);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
furi_hal_irda_state = IrdaStateAsyncTx;
|
|
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTx;
|
|
|
|
|
|
|
|
|
|
LL_TIM_ClearFlag_UPDATE(TIM1);
|
|
|
|
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
|
|
|
|
@@ -617,9 +621,9 @@ void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
|
|
|
|
|
LL_TIM_GenerateEvent_UPDATE(TIM1); /* DMA -> TIMx_RCR */
|
|
|
|
|
delay_us(5);
|
|
|
|
|
LL_GPIO_ResetOutputPin(
|
|
|
|
|
gpio_irda_tx.port, gpio_irda_tx.pin); /* when disable it prevents false pulse */
|
|
|
|
|
gpio_infrared_tx.port, gpio_infrared_tx.pin); /* when disable it prevents false pulse */
|
|
|
|
|
hal_gpio_init_ex(
|
|
|
|
|
&gpio_irda_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
|
|
|
|
|
&gpio_infrared_tx, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1);
|
|
|
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
|
LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
|
|
|
|
@@ -627,39 +631,40 @@ void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
|
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_tx_wait_termination(void) {
|
|
|
|
|
furi_assert(furi_hal_irda_state >= IrdaStateAsyncTx);
|
|
|
|
|
furi_assert(furi_hal_irda_state < IrdaStateMAX);
|
|
|
|
|
void furi_hal_infrared_async_tx_wait_termination(void) {
|
|
|
|
|
furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
|
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
|
|
|
|
|
|
osStatus_t status;
|
|
|
|
|
status = osSemaphoreAcquire(irda_tim_tx.stop_semaphore, osWaitForever);
|
|
|
|
|
status = osSemaphoreAcquire(infrared_tim_tx.stop_semaphore, osWaitForever);
|
|
|
|
|
furi_check(status == osOK);
|
|
|
|
|
furi_hal_irda_async_tx_free_resources();
|
|
|
|
|
furi_hal_irda_state = IrdaStateIdle;
|
|
|
|
|
furi_hal_infrared_async_tx_free_resources();
|
|
|
|
|
furi_hal_infrared_state = InfraredStateIdle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_tx_stop(void) {
|
|
|
|
|
furi_assert(furi_hal_irda_state >= IrdaStateAsyncTx);
|
|
|
|
|
furi_assert(furi_hal_irda_state < IrdaStateMAX);
|
|
|
|
|
void furi_hal_infrared_async_tx_stop(void) {
|
|
|
|
|
furi_assert(furi_hal_infrared_state >= InfraredStateAsyncTx);
|
|
|
|
|
furi_assert(furi_hal_infrared_state < InfraredStateMAX);
|
|
|
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
|
if(furi_hal_irda_state == IrdaStateAsyncTx) furi_hal_irda_state = IrdaStateAsyncTxStopReq;
|
|
|
|
|
if(furi_hal_infrared_state == InfraredStateAsyncTx)
|
|
|
|
|
furi_hal_infrared_state = InfraredStateAsyncTxStopReq;
|
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
|
|
|
|
|
furi_hal_irda_async_tx_wait_termination();
|
|
|
|
|
furi_hal_infrared_async_tx_wait_termination();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_tx_set_data_isr_callback(
|
|
|
|
|
FuriHalIrdaTxGetDataISRCallback callback,
|
|
|
|
|
void furi_hal_infrared_async_tx_set_data_isr_callback(
|
|
|
|
|
FuriHalInfraredTxGetDataISRCallback callback,
|
|
|
|
|
void* context) {
|
|
|
|
|
furi_assert(furi_hal_irda_state == IrdaStateIdle);
|
|
|
|
|
irda_tim_tx.data_callback = callback;
|
|
|
|
|
irda_tim_tx.data_context = context;
|
|
|
|
|
furi_assert(furi_hal_infrared_state == InfraredStateIdle);
|
|
|
|
|
infrared_tim_tx.data_callback = callback;
|
|
|
|
|
infrared_tim_tx.data_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_hal_irda_async_tx_set_signal_sent_isr_callback(
|
|
|
|
|
FuriHalIrdaTxSignalSentISRCallback callback,
|
|
|
|
|
void furi_hal_infrared_async_tx_set_signal_sent_isr_callback(
|
|
|
|
|
FuriHalInfraredTxSignalSentISRCallback callback,
|
|
|
|
|
void* context) {
|
|
|
|
|
irda_tim_tx.signal_sent_callback = callback;
|
|
|
|
|
irda_tim_tx.signal_sent_context = context;
|
|
|
|
|
infrared_tim_tx.signal_sent_callback = callback;
|
|
|
|
|
infrared_tim_tx.signal_sent_context = context;
|
|
|
|
|
}
|