Update the sample rate when the config is changed

This commit is contained in:
Sebastian H. Gabrielli 2024-04-30 12:00:29 +02:00
parent d215e204d4
commit bc9e3f6885
4 changed files with 11 additions and 10 deletions

View File

@ -165,7 +165,7 @@ uint8_t route_command(int pos) {
switch (context.conf) { switch (context.conf) {
case SAMPLE_TIME: case SAMPLE_TIME:
// Overwrite the config value // 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 // Set the flag to store it in the EEPROM
store_config = true; store_config = true;
@ -190,7 +190,7 @@ uint8_t route_command(int pos) {
uint16_t value; uint16_t value;
uint8_t bytes[2]; uint8_t bytes[2];
} config_value; } config_value;
config_value.value = config.ms_fanspeed_sample_rate; config_value.value = config.fanspeed_sample_rate;
// Return the corresponding data byte // Return the corresponding data byte
return config_value.bytes[1-pos]; return config_value.bytes[1-pos];

View File

@ -21,7 +21,7 @@ extern "C" {
// Struct for information on the controller. // Struct for information on the controller.
typedef struct { typedef struct {
uint16_t ms_fanspeed_sample_rate; uint16_t fanspeed_sample_rate;
} config_t; } config_t;
// Check if EEPROM is ready to be written to // Check if EEPROM is ready to be written to

View File

@ -1,7 +1,7 @@
#include "fan-speed.h" #include "fan-speed.h"
#include "uart.h" #include "uart.h"
uint16_t timer_period_ms = 1; uint16_t timer_period = 1;
uint16_t fan_speed = 0; uint16_t fan_speed = 0;
volatile uint16_t fan1_edge_counter = 0; volatile uint16_t fan1_edge_counter = 0;
volatile uint16_t fan2_edge_counter = 0; volatile uint16_t fan2_edge_counter = 0;
@ -10,11 +10,10 @@ volatile uint16_t fan2_edge_counter = 0;
void init_TCA0() { void init_TCA0() {
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ 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) { void TCA0_update_period(uint16_t timer_period_s) {
TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_s) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
} }
// COUNTINGS / TIME = FREQUENCY // COUNTINGS / TIME = FREQUENCY
@ -113,8 +112,8 @@ ISR (TCA0_OVF_vect) {
} }
// Calculate the fanspeed // Calculate the fanspeed
fan1_history[fan1_history_index] = RPM_calculation(fan1_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_ms); fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter,timer_period/1000);
// Reset the edge counter // Reset the edge counter
fan1_edge_counter = 0; fan1_edge_counter = 0;

View File

@ -52,6 +52,7 @@ int main() {
init_AC0(); init_AC0();
init_AC1(); init_AC1();
init_TCA0(); init_TCA0();
TCA0_update_period(config.fanspeed_sample_rate);
// Only enable UART when required for debugging // Only enable UART when required for debugging
#ifdef ENABLE_UART #ifdef ENABLE_UART
@ -66,8 +67,9 @@ int main() {
sei(); sei();
while (1) { 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) { if (store_config) {
TCA0_update_period(config.fanspeed_sample_rate);
write_struct_from_EEPROM(config); write_struct_from_EEPROM(config);
store_config = false; store_config = false;
} }