diff --git a/adc.c b/adc.c index 4ee4f8f..a3486eb 100644 --- a/adc.c +++ b/adc.c @@ -32,6 +32,8 @@ struct pin_t reset = { .port = GPIO5, .pin = GPIOPIN2 }; struct pin_t range = { .port = GPIO5, .pin = GPIOPIN6 }; struct pin_t standby = { .port = GPIO5, .pin = GPIOPIN0 }; // inverted +unsigned int adc_sample_counter = 0; + #ifdef USE_DMA static void configure_rx_dma() { @@ -245,5 +247,7 @@ void pin_int0_isr(void) setup_buffer(NULL); } } + + increment_event_counter(adc_sample_counter); } } diff --git a/clock.c b/clock.c index 5d02afb..08346af 100644 --- a/clock.c +++ b/clock.c @@ -1,10 +1,20 @@ +#include + #include "clock.h" #include "config.h" - -#include +#include "utils.h" volatile uint32_t msTicks; /* counts 1ms timeTicks */ +volatile struct event_counters event_counters[N_EVENT_COUNTERS]; +unsigned int active_event_counter_index = 0; +#define active_event_counters (&event_counters[active_event_counter_index]) + +struct event_counters get_last_event_counters(void) +{ + return event_counters[(active_event_counter_index-1) % N_EVENT_COUNTERS]; +} + void init_systick() { systick_set_reload(CLK_BASE_M4 / 8 / 1000); @@ -20,4 +30,10 @@ void delay_ms(unsigned int ms) { void sys_tick_handler() { msTicks++; + + if (msTicks % 1024 == 0) { + unsigned int next = (active_event_counter_index + 1) % N_EVENT_COUNTERS; + memzero((void*) &event_counters[next], sizeof(struct event_counters)); + active_event_counter_index = next; + } } diff --git a/clock.h b/clock.h index 1b69619..1943732 100644 --- a/clock.h +++ b/clock.h @@ -6,3 +6,20 @@ extern volatile uint32_t msTicks; /* counts 1ms timeTicks */ void init_systick(); void delay_ms(unsigned int ms); +struct event_counters { + unsigned int feedback_counter; + unsigned int adc_sample_counter; +}; + +// public +struct event_counters get_last_event_counters(void); + +// private +#define N_EVENT_COUNTERS 4 +extern volatile struct event_counters event_counters[N_EVENT_COUNTERS]; +extern unsigned int active_event_counter_index; + +// public +#define increment_event_counter(counter) \ + event_counters[active_event_counter_index].counter++; + diff --git a/feedback.c b/feedback.c index 97a46dd..23bf114 100644 --- a/feedback.c +++ b/feedback.c @@ -130,6 +130,7 @@ void do_feedback() } feedback_update(); + increment_event_counter(feedback_counter); } void timer2_isr(void) diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..99a868a --- /dev/null +++ b/utils.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +static inline void memzero(void* ptr, unsigned int size) +{ + memset(ptr, 0, size); +}