mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-16 09:38:01 +00:00
new digital_signal
This commit is contained in:
parent
0121fa40a0
commit
f3c748eeac
2 changed files with 14 additions and 13 deletions
|
@ -8,16 +8,12 @@
|
||||||
#define F_TIM (64000000.0)
|
#define F_TIM (64000000.0)
|
||||||
#define T_TIM (1.0 / F_TIM)
|
#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* digital_signal_alloc(uint32_t max_edges_cnt) {
|
||||||
DigitalSignal* signal = malloc(sizeof(DigitalSignal));
|
DigitalSignal* signal = malloc(sizeof(DigitalSignal));
|
||||||
signal->start_level = true;
|
signal->start_level = true;
|
||||||
signal->edges_max_cnt = max_edges_cnt;
|
signal->edges_max_cnt = max_edges_cnt;
|
||||||
signal->edge_timings = malloc(max_edges_cnt * sizeof(float));
|
signal->edge_timings = malloc(max_edges_cnt * sizeof(float));
|
||||||
|
signal->reload_reg_buff = malloc(max_edges_cnt * sizeof(uint32_t));
|
||||||
signal->edge_cnt = 0;
|
signal->edge_cnt = 0;
|
||||||
|
|
||||||
return signal;
|
return signal;
|
||||||
|
@ -27,6 +23,7 @@ void digital_signal_free(DigitalSignal* signal) {
|
||||||
furi_assert(signal);
|
furi_assert(signal);
|
||||||
|
|
||||||
free(signal->edge_timings);
|
free(signal->edge_timings);
|
||||||
|
free(signal->reload_reg_buff);
|
||||||
free(signal);
|
free(signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,15 +35,18 @@ bool digital_signal_append(DigitalSignal* signal_a, DigitalSignal* signal_b) {
|
||||||
return false;
|
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;
|
uint8_t start_copy = 0;
|
||||||
if(end_level == signal_b->start_level) {
|
if(end_level == signal_b->start_level) {
|
||||||
if(signal_a->edge_cnt) {
|
if(signal_a->edge_cnt) {
|
||||||
signal_a->edge_timings[signal_a->edge_cnt - 1] += signal_b->edge_timings[0];
|
signal_a->edge_timings[signal_a->edge_cnt - 1] += signal_b->edge_timings[0];
|
||||||
|
start_copy += 1;
|
||||||
} else {
|
} else {
|
||||||
signal_a->edge_timings[signal_a->edge_cnt] += signal_b->edge_timings[0];
|
signal_a->edge_timings[signal_a->edge_cnt] += signal_b->edge_timings[0];
|
||||||
}
|
}
|
||||||
start_copy += 1;
|
|
||||||
}
|
}
|
||||||
memcpy(
|
memcpy(
|
||||||
&signal_a->edge_timings[signal_a->edge_cnt],
|
&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];
|
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_signal = 0;
|
||||||
float t_current = 0;
|
float t_current = 0;
|
||||||
float r = 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 = (t_signal - t_current) / T_TIM;
|
||||||
r_dec = modff(r, &r_int);
|
r_dec = modff(r, &r_int);
|
||||||
if(r_dec < 0.5f) {
|
if(r_dec < 0.5f) {
|
||||||
arr[i] = (uint32_t)r_int - 1;
|
signal->reload_reg_buff[i] = (uint32_t)r_int - 1;
|
||||||
} else {
|
} 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);
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
|
||||||
|
|
||||||
// Init timer arr register buffer and DMA channel
|
// Init timer arr register buffer and DMA channel
|
||||||
digital_signal_prepare_arr(signal, digital_signal_arr_buff);
|
digital_signal_prepare_arr(signal);
|
||||||
dma_config.MemoryOrM2MDstAddress = (uint32_t)digital_signal_arr_buff;
|
dma_config.MemoryOrM2MDstAddress = (uint32_t)signal->reload_reg_buff;
|
||||||
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR);
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR);
|
||||||
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
||||||
dma_config.Mode = LL_DMA_MODE_NORMAL;
|
dma_config.Mode = LL_DMA_MODE_NORMAL;
|
||||||
|
|
|
@ -11,6 +11,7 @@ typedef struct {
|
||||||
uint32_t edge_cnt;
|
uint32_t edge_cnt;
|
||||||
uint32_t edges_max_cnt;
|
uint32_t edges_max_cnt;
|
||||||
float* edge_timings;
|
float* edge_timings;
|
||||||
|
uint32_t* reload_reg_buff;
|
||||||
} DigitalSignal;
|
} DigitalSignal;
|
||||||
|
|
||||||
DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt);
|
DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt);
|
||||||
|
|
Loading…
Reference in a new issue