diff --git a/STM32/CM7/Core/Inc/global_state.h b/STM32/CM7/Core/Inc/global_state.h index fcdcef3..66e70dd 100644 --- a/STM32/CM7/Core/Inc/global_state.h +++ b/STM32/CM7/Core/Inc/global_state.h @@ -6,6 +6,7 @@ #include #define MAX_CAM_MISS 2 +#define SPARK_ADVANCE 20 typedef enum { SYNC_OK = 0, SYNC_PENDING = 1, SYNC_NOT_OK = 2 } sync_state_t; diff --git a/STM32/CM7/Core/Inc/ring_buffer.h b/STM32/CM7/Core/Inc/ring_buffer.h index de92307..a33c228 100644 --- a/STM32/CM7/Core/Inc/ring_buffer.h +++ b/STM32/CM7/Core/Inc/ring_buffer.h @@ -8,8 +8,8 @@ typedef struct ring_buffer_t { uint8_t w_head; } ring_buffer_t; -inline void ringBufferPush(ring_buffer_t *rb, uint32_t value); +inline void ringBufferPush(volatile ring_buffer_t *rb, uint32_t value); -inline uint32_t ringBufferRead(ring_buffer_t *rb, uint8_t idx); +inline uint32_t ringBufferRead(volatile ring_buffer_t *rb, uint8_t neg_idx); -inline void ringBufferRevert(ring_buffer_t *rb, uint8_t val); +inline void ringBufferRevert(volatile ring_buffer_t *rb, uint8_t val); diff --git a/STM32/CM7/Core/Src/ring_buffer.c b/STM32/CM7/Core/Src/ring_buffer.c index c5d5a09..f42d79a 100644 --- a/STM32/CM7/Core/Src/ring_buffer.c +++ b/STM32/CM7/Core/Src/ring_buffer.c @@ -6,17 +6,17 @@ #include #include -void ringBufferPush(ring_buffer_t *rb, uint32_t value) { +void ringBufferPush(volatile ring_buffer_t *rb, uint32_t value) { rb->buffer[rb->w_head] = value; rb->w_head++; return; } -uint32_t ringBufferRead(ring_buffer_t *rb, uint8_t idx) { - return rb->buffer[rb->w_head - 1 - idx]; +uint32_t ringBufferRead(volatile ring_buffer_t *rb, uint8_t neg_idx) { + return rb->buffer[rb->w_head - 1 - neg_idx]; } -void ringBufferRevert(ring_buffer_t *rb, uint8_t val) { +void ringBufferRevert(volatile ring_buffer_t *rb, uint8_t val) { rb->w_head -= val; return; } diff --git a/STM32/CM7/Core/Src/tasks/crank.c b/STM32/CM7/Core/Src/tasks/crank.c index 6e14295..36345b8 100644 --- a/STM32/CM7/Core/Src/tasks/crank.c +++ b/STM32/CM7/Core/Src/tasks/crank.c @@ -8,6 +8,7 @@ #include "main.h" #include "ring_buffer.h" #include "tasks.h" +#include void crankHandler(void *argument) { for (;;) { @@ -59,6 +60,13 @@ void crankHandler(void *argument) { if (state_g.crank_state == CYCLE_COMPRESSION && state_g.sync_state == SYNC_OK) { DEBUG_LOG("Spark schedule reached, congrats\n\r"); + uint32_t t_now = ringBufferRead(&state_g.crank_RB, 0); + uint32_t t_prev = ringBufferRead(&state_g.crank_RB, 1); + + uint32_t d1a = t_now - t_prev; + // TODO: Map interpolation rather than SPARK_ADVANCE CONSTANT + uint32_t d_spark = d1a * (45 - SPARK_ADVANCE) / 180; + uint32_t t_spark = t_now + d_spark; // TODO: schedule spark } }