diff --git a/application.c b/application.c index d888ff7..d5ec347 100644 --- a/application.c +++ b/application.c @@ -26,6 +26,7 @@ #include // design utilities #include // flash utilities #include // ADC utilities +#include // timer library /* own libraries */ #include "global.h" // board definitions @@ -73,6 +74,9 @@ static const uint32_t channel_pins[] = {GPIO_PIN(PB12), GPIO_PIN(PB13), GPIO_PIN static uint8_t channel_start = 0; /**< first signal of range to probe */ static uint8_t channel_stop = CHANNEL_NUMBERS - 1; /**< last signal of range to probe */ +/** timer ID for timer to measure activity timing */ +#define MONITOR_TIMER 2 + size_t putc(char c) { size_t length = 0; // number of characters printed @@ -428,6 +432,18 @@ static void command_monitor(void* argument) } puts("\n"); + // setup timer to measure milliseconds + rcc_periph_clock_enable(RCC_TIM(MONITOR_TIMER)); // enable clock for timer peripheral + rcc_periph_reset_pulse(RST_TIM(MONITOR_TIMER)); // reset timer state + timer_disable_counter(TIM(MONITOR_TIMER)); // disable timer to configure it + timer_set_mode(TIM(MONITOR_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up + timer_set_prescaler(TIM(MONITOR_TIMER), (rcc_ahb_frequency / 2000) - 1); // generate half millisecond ticks (prescaler is not large enough for milliseconds) + timer_set_period(TIM(MONITOR_TIMER), 2000 - 1); // set period to seconds + timer_clear_flag(TIM(MONITOR_TIMER), TIM_SR_UIF); // clear update (overflow) flag + timer_update_on_overflow(TIM(MONITOR_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout) + uint32_t seconds = 0; // count the seconds using the overflow + timer_enable_counter(TIM(MONITOR_TIMER)); // enable timer + // start monitoring uint16_t gpioa_data = UINT16_MAX; uint16_t gpiob_data = UINT16_MAX; @@ -438,12 +454,17 @@ static void command_monitor(void* argument) wakeup_flag = false; // clear flag second_flag = false; // clear flag } + // one second has passed + if (timer_get_flag(TIM(MONITOR_TIMER), TIM_SR_UIF)) { + timer_clear_flag(TIM(MONITOR_TIMER), TIM_SR_UIF); // clear flag + seconds++; // count the second + } // check is there is a change on a channel const uint16_t gpioa_new = gpio_get(GPIOA, gpioa_mask); const uint16_t gpiob_new = gpio_get(GPIOB, gpioa_mask); if (gpioa_new != gpioa_data || gpiob_new != gpiob_data) { // print data - puts("0000.000"); + printf("%04u.%03u", seconds, timer_get_counter(TIM(MONITOR_TIMER)) / 2); for (uint8_t i = channel_start; i <= channel_stop; i++) { const uint16_t port = (GPIOA == channel_ports[i] ? gpioa_new : gpiob_new); const uint8_t high = ((port & channel_pins[i]) ? 1 : 0); @@ -462,10 +483,13 @@ static void command_monitor(void* argument) } user_input_get(); // clean input - // set all back to input - for (uint8_t i = channel_start; i <= channel_stop; i++) { + // clean up + for (uint8_t i = channel_start; i <= channel_stop; i++) { // set all back to input gpio_mode_setup(channel_ports[i], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[i]); // ensure pin is floating input } + timer_disable_counter(TIM(MONITOR_TIMER)); // disable timer + rcc_periph_reset_pulse(RST_TIM(MONITOR_TIMER)); // reset timer state + rcc_periph_clock_disable(RCC_TIM(MONITOR_TIMER)); // disable clock for timer peripheral } /** set first channel of range to scan