From bc9e3f6885dfb57482346aed8bf651b9dc8a9bc4 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 12:00:29 +0200 Subject: [PATCH] Update the sample rate when the config is changed --- prosjekt.X/command-handler.c | 4 ++-- prosjekt.X/eeprom.h | 2 +- prosjekt.X/fan-speed.c | 11 +++++------ prosjekt.X/main.c | 4 +++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 56df078..50b3378 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -165,7 +165,7 @@ uint8_t route_command(int pos) { switch (context.conf) { case SAMPLE_TIME: // Overwrite the config value - config.ms_fanspeed_sample_rate = context.conf_val; + config.fanspeed_sample_rate = context.conf_val; // Set the flag to store it in the EEPROM store_config = true; @@ -190,7 +190,7 @@ uint8_t route_command(int pos) { uint16_t value; uint8_t bytes[2]; } config_value; - config_value.value = config.ms_fanspeed_sample_rate; + config_value.value = config.fanspeed_sample_rate; // Return the corresponding data byte return config_value.bytes[1-pos]; diff --git a/prosjekt.X/eeprom.h b/prosjekt.X/eeprom.h index e6d9cae..b4fe331 100644 --- a/prosjekt.X/eeprom.h +++ b/prosjekt.X/eeprom.h @@ -21,7 +21,7 @@ extern "C" { // Struct for information on the controller. typedef struct { - uint16_t ms_fanspeed_sample_rate; + uint16_t fanspeed_sample_rate; } config_t; // Check if EEPROM is ready to be written to diff --git a/prosjekt.X/fan-speed.c b/prosjekt.X/fan-speed.c index 1cc0ed9..9ba3b89 100644 --- a/prosjekt.X/fan-speed.c +++ b/prosjekt.X/fan-speed.c @@ -1,7 +1,7 @@ #include "fan-speed.h" #include "uart.h" -uint16_t timer_period_ms = 1; +uint16_t timer_period = 1; uint16_t fan_speed = 0; volatile uint16_t fan1_edge_counter = 0; volatile uint16_t fan2_edge_counter = 0; @@ -10,11 +10,10 @@ volatile uint16_t fan2_edge_counter = 0; void init_TCA0() { TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ - TCA0_update_period(timer_period_ms); } -void TCA0_update_period(uint16_t timer_period) { - TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ +void TCA0_update_period(uint16_t timer_period_s) { + TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_s) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ } // COUNTINGS / TIME = FREQUENCY @@ -113,8 +112,8 @@ ISR (TCA0_OVF_vect) { } // Calculate the fanspeed - fan1_history[fan1_history_index] = RPM_calculation(fan1_edge_counter,timer_period_ms); - fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter,timer_period_ms); + fan1_history[fan1_history_index] = RPM_calculation(fan1_edge_counter,timer_period/1000); + fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter,timer_period/1000); // Reset the edge counter fan1_edge_counter = 0; diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 75337b2..ea0d4a1 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -52,6 +52,7 @@ int main() { init_AC0(); init_AC1(); init_TCA0(); + TCA0_update_period(config.fanspeed_sample_rate); // Only enable UART when required for debugging #ifdef ENABLE_UART @@ -66,8 +67,9 @@ int main() { sei(); while (1) { - // If we have made a config change, store it. + // If we have made a config change, store it and recalculate the period. if (store_config) { + TCA0_update_period(config.fanspeed_sample_rate); write_struct_from_EEPROM(config); store_config = false; }