From f3c748eeac5adfb46c63f0c54b079af86fa25b2c Mon Sep 17 00:00:00 2001 From: r3df0xx Date: Tue, 24 May 2022 20:23:51 +0300 Subject: [PATCH] new digital_signal --- lib/digital_signal/digital_signal.c | 26 +++++++++++++------------- lib/digital_signal/digital_signal.h | 1 + 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/digital_signal/digital_signal.c b/lib/digital_signal/digital_signal.c index b9be3b602..23ddaf909 100644 --- a/lib/digital_signal/digital_signal.c +++ b/lib/digital_signal/digital_signal.c @@ -8,16 +8,12 @@ #define F_TIM (64000000.0) #define T_TIM (1.0 / F_TIM) -#define MAX_ARR_BUFF_SIZE (10000) - -// TODO rework with dynamic allocation -static uint32_t digital_signal_arr_buff[MAX_ARR_BUFF_SIZE]; - DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt) { DigitalSignal* signal = malloc(sizeof(DigitalSignal)); signal->start_level = true; signal->edges_max_cnt = max_edges_cnt; signal->edge_timings = malloc(max_edges_cnt * sizeof(float)); + signal->reload_reg_buff = malloc(max_edges_cnt * sizeof(uint32_t)); signal->edge_cnt = 0; return signal; @@ -27,6 +23,7 @@ void digital_signal_free(DigitalSignal* signal) { furi_assert(signal); free(signal->edge_timings); + free(signal->reload_reg_buff); free(signal); } @@ -38,15 +35,18 @@ bool digital_signal_append(DigitalSignal* signal_a, DigitalSignal* signal_b) { return false; } - bool end_level = signal_a->start_level ^ !(signal_a->edge_cnt % 2); + bool end_level = signal_a->start_level; + if(signal_a->edge_cnt) { + end_level = signal_a->start_level ^ !(signal_a->edge_cnt % 2); + } uint8_t start_copy = 0; if(end_level == signal_b->start_level) { if(signal_a->edge_cnt) { signal_a->edge_timings[signal_a->edge_cnt - 1] += signal_b->edge_timings[0]; + start_copy += 1; } else { signal_a->edge_timings[signal_a->edge_cnt] += signal_b->edge_timings[0]; } - start_copy += 1; } memcpy( &signal_a->edge_timings[signal_a->edge_cnt], @@ -76,7 +76,7 @@ float digital_signal_get_edge(DigitalSignal* signal, uint32_t edge_num) { return signal->edge_timings[edge_num]; } -static void digital_signal_prepare_arr(DigitalSignal* signal, uint32_t* arr) { +static void digital_signal_prepare_arr(DigitalSignal* signal) { float t_signal = 0; float t_current = 0; float r = 0; @@ -88,11 +88,11 @@ static void digital_signal_prepare_arr(DigitalSignal* signal, uint32_t* arr) { r = (t_signal - t_current) / T_TIM; r_dec = modff(r, &r_int); if(r_dec < 0.5f) { - arr[i] = (uint32_t)r_int - 1; + signal->reload_reg_buff[i] = (uint32_t)r_int - 1; } else { - arr[i] = (uint32_t)r_int; + signal->reload_reg_buff[i] = (uint32_t)r_int; } - t_current += (arr[i] + 1) * T_TIM; + t_current += (signal->reload_reg_buff[i] + 1) * T_TIM; } } @@ -130,8 +130,8 @@ bool digital_signal_send(DigitalSignal* signal, const GpioPin* gpio) { LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1); // Init timer arr register buffer and DMA channel - digital_signal_prepare_arr(signal, digital_signal_arr_buff); - dma_config.MemoryOrM2MDstAddress = (uint32_t)digital_signal_arr_buff; + digital_signal_prepare_arr(signal); + dma_config.MemoryOrM2MDstAddress = (uint32_t)signal->reload_reg_buff; dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR); dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH; dma_config.Mode = LL_DMA_MODE_NORMAL; diff --git a/lib/digital_signal/digital_signal.h b/lib/digital_signal/digital_signal.h index 3b3ad77f7..5e20e7332 100644 --- a/lib/digital_signal/digital_signal.h +++ b/lib/digital_signal/digital_signal.h @@ -11,6 +11,7 @@ typedef struct { uint32_t edge_cnt; uint32_t edges_max_cnt; float* edge_timings; + uint32_t* reload_reg_buff; } DigitalSignal; DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt);