From dd53b4dbf228450154ec702741a6c5ad7547b5b1 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:08:03 +0200 Subject: [PATCH 1/9] Fan data is now saved in the array Closes #9 --- prosjekt.X/command-handler.h | 3 +++ prosjekt.X/fan_speeeed.c | 21 +++++++++++++++++++-- prosjekt.X/fan_speeeed.h | 6 ++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/prosjekt.X/command-handler.h b/prosjekt.X/command-handler.h index cf0f428..23ca9f8 100644 --- a/prosjekt.X/command-handler.h +++ b/prosjekt.X/command-handler.h @@ -66,6 +66,9 @@ typedef struct { // Fan history variables extern volatile uint16_t fan1_history[512]; extern volatile uint16_t fan2_history[512]; +// Fan history index variable +volatile uint16_t fan1_history_index = 0; +volatile uint16_t fan2_history_index = 0; // Config extern volatile config_t config; diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 38540af..21d0850 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -98,8 +98,25 @@ ISR(AC1_AC_vect){ // AC1 vec flag // TIMER INTERUPT ISR (TCA0_OVF_vect) { cli(); - RPM_calculation(fan1_edge_counter,timer_period_ms); - RPM_calculation(fan2_edge_counter,timer_period_ms); + + // 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); + + // Increment the index, or reset if it is at the top + if (fan1_history_index < 512) { + fan1_history_index++; + } else { + fan1_history_index = 0; + } + + if (fan2_history_index < 512) { + fan2_history_index++; + } else { + fan2_history_index = 0; + } + + // Reset the edge counter fan1_edge_counter = 0; fan2_edge_counter = 0; TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 160071d..dfe47a8 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -29,6 +29,12 @@ extern "C" { * and inspiration form practice 6 for TCA0 setup */ +// Fan history variables +extern volatile uint16_t fan1_history[512]; +extern volatile uint16_t fan2_history[512]; +// Fan history index variable +volatile uint16_t fan1_history_index = 0; +volatile uint16_t fan2_history_index = 0; // INITALICE TIMER COUNTER void init_TCA0(); From bcd631001be117394478d40f68f22d7272821df6 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:10:26 +0200 Subject: [PATCH 2/9] Current fan speed now returns the last fan speed measured --- prosjekt.X/command-handler.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 347043c..96bc956 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -1,4 +1,5 @@ #include "command-handler.h" +#include "fan_speeeed.h" // Initialize empty, global command context volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE, @@ -235,9 +236,9 @@ uint8_t route_command(int pos) { break; case READ_FAN_SPEED: if (context.fan == FAN1) { - return fan1_history[0]; + return fan1_history[fan1_history_index]; } else if (context.fan == FAN2) { - return fan2_history[0]; + return fan2_history[fan2_history_index]; } else { return 0; } From 1671476f4191dab5c9fbcbb3b0d3276aa7d717e7 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:11:32 +0200 Subject: [PATCH 3/9] Move index incrementation over storage This avoids the problem where the current index has outdated data. It does however mean the first piece of stored data is empty, but it is a worthwile tradeoff. --- prosjekt.X/fan_speeeed.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 21d0850..db41f07 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -99,10 +99,6 @@ ISR(AC1_AC_vect){ // AC1 vec flag ISR (TCA0_OVF_vect) { cli(); - // 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); - // Increment the index, or reset if it is at the top if (fan1_history_index < 512) { fan1_history_index++; @@ -116,6 +112,10 @@ ISR (TCA0_OVF_vect) { fan2_history_index = 0; } + // 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); + // Reset the edge counter fan1_edge_counter = 0; fan2_edge_counter = 0; From b858d29ce8e0b90edc451d5d22c81feadc055c1f Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:21:26 +0200 Subject: [PATCH 4/9] Make read bulk fan speed read correctly from the array --- prosjekt.X/command-handler.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 96bc956..7ae96f5 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -245,9 +245,23 @@ uint8_t route_command(int pos) { break; case READ_BULK_FAN_SPEED: if (context.fan == FAN1) { - return fan1_history[pos]; + // Validate that pos is valid, if not return 0 + if (pos >= 512) { return 0; } + + // Calculate the index position + int16_t index = fan1_history_index - pos; + if (index < 0) { index = 511-pos; } + + return fan1_history[index]; } else if (context.fan == FAN2) { - return fan2_history[pos]; + // Validate that pos is valid, if not return 0 + if (pos >= 512) { return 0; } + + // Calculate the index position + int16_t index = fan2_history_index - pos; + if (index < 0) { index = 511-pos; } + + return fan2_history[index]; } else { return 0; } From 458ae00f24c6d840dd0a9763dad514dab8f67fb0 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:26:08 +0200 Subject: [PATCH 5/9] Rename fanspeeeed to fanspeed --- prosjekt.X/command-handler.c | 2 +- prosjekt.X/{fan_speeeed.c => fan_speed.c} | 2 +- prosjekt.X/{fan_speeeed.h => fan_speed.h} | 0 prosjekt.X/main.c | 3 +++ prosjekt.X/nbproject/configurations.xml | 4 ++-- 5 files changed, 7 insertions(+), 4 deletions(-) rename prosjekt.X/{fan_speeeed.c => fan_speed.c} (95%) rename prosjekt.X/{fan_speeeed.h => fan_speed.h} (100%) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 7ae96f5..eb97922 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -1,5 +1,5 @@ #include "command-handler.h" -#include "fan_speeeed.h" +#include "fan_speed.h" // Initialize empty, global command context volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE, diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speed.c similarity index 95% rename from prosjekt.X/fan_speeeed.c rename to prosjekt.X/fan_speed.c index db41f07..7b84c59 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speed.c @@ -1,4 +1,4 @@ -#include "fan_speeeed.h" +#include "fan_speed.h" #include "uart.h" uint16_t timer_period_ms = 1; diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speed.h similarity index 100% rename from prosjekt.X/fan_speeeed.h rename to prosjekt.X/fan_speed.h diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 7dfb0f0..25da254 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -26,6 +26,9 @@ // Fan history variables volatile uint16_t fan1_history[512] = {0}; volatile uint16_t fan2_history[512] = {0}; +// Fan history index variable +volatile uint16_t fan1_history_index = 0; +volatile uint16_t fan2_history_index = 0; // Default config is 500ms sample rate volatile config_t config = {500}; diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 46b7e18..7d01886 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -6,11 +6,11 @@ projectFiles="true"> uart.h voltage.h - fan_speeeed.h themistor-temp.h command-handler.h eeprom.h i2c.h + fan_speed.h main.c uart.c voltage.c - fan_speeeed.c thermistor-temp.c command-handler.c i2c.c eeprom.c + fan_speed.c Makefile From d215e204d4563b37ba33ceb437505751708119cc Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:37:21 +0200 Subject: [PATCH 6/9] Rename files & re-factor to pretty up project --- prosjekt.X/command-handler.c | 7 +++- prosjekt.X/command-handler.h | 4 +-- prosjekt.X/{fan_speed.c => fan-speed.c} | 2 +- prosjekt.X/{fan_speed.h => fan-speed.h} | 4 +-- prosjekt.X/i2c.h | 1 - prosjekt.X/main.c | 44 +++++++++++++++++-------- prosjekt.X/nbproject/configurations.xml | 4 +-- prosjekt.X/themistor-temp.h | 7 ++-- prosjekt.X/thermistor-temp.c | 8 ++--- 9 files changed, 51 insertions(+), 30 deletions(-) rename prosjekt.X/{fan_speed.c => fan-speed.c} (95%) rename prosjekt.X/{fan_speed.h => fan-speed.h} (90%) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index eb97922..56df078 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -1,5 +1,5 @@ #include "command-handler.h" -#include "fan_speed.h" +#include "fan-speed.h" // Initialize empty, global command context volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE, @@ -169,6 +169,10 @@ uint8_t route_command(int pos) { // Set the flag to store it in the EEPROM store_config = true; + return 0; + break; + case CNF_NONE: + return 0; break; } break; @@ -273,6 +277,7 @@ uint8_t route_command(int pos) { } else if (context.fan == FAN2) { memset(fan2_history, 0, sizeof(fan2_history)); } + return 0; break; case UNKNOWN_COMMAND: default: diff --git a/prosjekt.X/command-handler.h b/prosjekt.X/command-handler.h index 23ca9f8..8fbdd74 100644 --- a/prosjekt.X/command-handler.h +++ b/prosjekt.X/command-handler.h @@ -67,8 +67,8 @@ typedef struct { extern volatile uint16_t fan1_history[512]; extern volatile uint16_t fan2_history[512]; // Fan history index variable -volatile uint16_t fan1_history_index = 0; -volatile uint16_t fan2_history_index = 0; +extern volatile uint16_t fan1_history_index; +extern volatile uint16_t fan2_history_index; // Config extern volatile config_t config; diff --git a/prosjekt.X/fan_speed.c b/prosjekt.X/fan-speed.c similarity index 95% rename from prosjekt.X/fan_speed.c rename to prosjekt.X/fan-speed.c index 7b84c59..1cc0ed9 100644 --- a/prosjekt.X/fan_speed.c +++ b/prosjekt.X/fan-speed.c @@ -1,4 +1,4 @@ -#include "fan_speed.h" +#include "fan-speed.h" #include "uart.h" uint16_t timer_period_ms = 1; diff --git a/prosjekt.X/fan_speed.h b/prosjekt.X/fan-speed.h similarity index 90% rename from prosjekt.X/fan_speed.h rename to prosjekt.X/fan-speed.h index dfe47a8..e56e8a2 100644 --- a/prosjekt.X/fan_speed.h +++ b/prosjekt.X/fan-speed.h @@ -33,8 +33,8 @@ extern "C" { extern volatile uint16_t fan1_history[512]; extern volatile uint16_t fan2_history[512]; // Fan history index variable -volatile uint16_t fan1_history_index = 0; -volatile uint16_t fan2_history_index = 0; +extern volatile uint16_t fan1_history_index; +extern volatile uint16_t fan2_history_index; // INITALICE TIMER COUNTER void init_TCA0(); diff --git a/prosjekt.X/i2c.h b/prosjekt.X/i2c.h index 1272b11..859df15 100644 --- a/prosjekt.X/i2c.h +++ b/prosjekt.X/i2c.h @@ -14,7 +14,6 @@ extern "C" { // Include the IO for I2C #include "command-handler.h" -#include "uart.h" #include #include #include diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 25da254..75337b2 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,24 +4,32 @@ * * Created on March 6, 2024, 12:34 PM */ -#include "uart.h" -#include "voltage.h" -#include -#define RTC_PERIOD (511) -#define DELAY_TIME 1000 -#include "eeprom.h" +#define F_CPU 4E6 + +// Include AVR specific libs #include +#include +#include + +// Include standard C libraries +#include #include #include #include -#define F_CPU 4E6 +#include + +// Include custom source files +#include "eeprom.h" +#include "voltage.h" #include "command-handler.h" #include "i2c.h" #include "themistor-temp.h" +#include "fan-speed.h" + +// Only enable UART when required for debugging +#ifdef ENABLE_UART #include "uart.h" -#include -#include -#include +#endif // Fan history variables volatile uint16_t fan1_history[512] = {0}; @@ -36,17 +44,25 @@ volatile bool store_config = false; int main() { // Initialize functionality - init_uart((uint16_t)9600); ADC0_init(); - init_led(); + init_alarm_gpio(); init_i2c(); + + // Fanspeed + init_AC0(); + init_AC1(); + init_TCA0(); + + // Only enable UART when required for debugging +#ifdef ENABLE_UART + init_uart((uint16_t)9600); stdout = &USART_stream; +#endif // Read the stored config struct config = read_struct_from_EEPROM(); - PORTB.DIRSET = PIN3_bm; - + // Enable interrupts sei(); while (1) { diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 7d01886..aaaca26 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -10,7 +10,7 @@ command-handler.h eeprom.h i2c.h - fan_speed.h + fan-speed.h command-handler.c i2c.c eeprom.c - fan_speed.c + fan-speed.c Makefile diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index 4d85568..fe671b9 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -23,7 +23,8 @@ extern "C" { #define T_0 298.15 #define B 3950 #define R_1 1000 -#define ledpin PIN3_bm + +#define ALERT_PIN PIN3_bm // Takes inn messured value // Calculates the temperature in celcius @@ -36,8 +37,8 @@ bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp); void alert_voltage_threshold_exceeded(bool voltage_threshold_bool); -// Initialise led -void init_led(); +// Initialise alarm GPIO +void init_alarm_gpio(); #ifdef __cplusplus } diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 71933a5..1b3bd2f 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1,8 +1,8 @@ #include "themistor-temp.h" -void init_led(){ - PORTB.DIRSET = ledpin; +void init_alarm_gpio(){ + PORTB.DIRSET = ALERT_PIN; } float calculate_thermistor_temp(float thermistor_voltage){ @@ -42,8 +42,8 @@ bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){ void alert_voltage_threshold_exceeded(bool voltage_threshold_bool){ if (voltage_threshold_bool){ printf("Error: maximum temperature exceeded"); - PORTB.OUTSET = ledpin; + PORTB.OUTSET = ALERT_PIN; } else{ - PORTB.OUTCLR = ledpin; + PORTB.OUTCLR = ALERT_PIN; } } \ No newline at end of file From bc9e3f6885dfb57482346aed8bf651b9dc8a9bc4 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 12:00:29 +0200 Subject: [PATCH 7/9] 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; } From cd65b8fd1f9c5ac7003f483e6341747b1ad292b0 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 16:17:52 +0200 Subject: [PATCH 8/9] Lots of bug fixes, everything works --- prosjekt.X/command-handler.c | 37 ++++++++++++++++++++++++++++-------- prosjekt.X/eeprom.h | 2 +- prosjekt.X/fan-speed.c | 16 ++++++++++------ prosjekt.X/main.c | 4 ++-- prosjekt.X/themistor-temp.h | 2 +- prosjekt.X/thermistor-temp.c | 2 +- prosjekt.X/voltage.c | 10 +++++----- 7 files changed, 49 insertions(+), 24 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 50b3378..25ab39e 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -127,7 +127,7 @@ void parse_command(uint8_t command[], uint8_t command_len) { } // Store the parameter - param = command[2]; + param = command[1]; context.conf = param; @@ -149,8 +149,8 @@ void parse_command(uint8_t command[], uint8_t command_len) { uint8_t bytes[2]; } config_value; - config_value.bytes[0] = command[3]; - config_value.bytes[1] = command[4]; + config_value.bytes[0] = command[2]; + config_value.bytes[1] = command[3]; // Store the value context.conf_val = config_value.value; @@ -165,7 +165,7 @@ uint8_t route_command(int pos) { switch (context.conf) { case SAMPLE_TIME: // Overwrite the config value - config.fanspeed_sample_rate = context.conf_val; + config.ms_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.fanspeed_sample_rate; + config_value.value = config.ms_fanspeed_sample_rate; // Return the corresponding data byte return config_value.bytes[1-pos]; @@ -202,6 +202,9 @@ uint8_t route_command(int pos) { case READ_VOLTAGE: { + // Validate that pos is within range + if (pos >= 2) { return 0; } + // Create a union to store the data union { int16_t v; @@ -225,7 +228,7 @@ uint8_t route_command(int pos) { } // Send the data - return voltage.bytes[pos]; + return voltage.bytes[1-pos]; } case READ_TERMPERATURE: { @@ -239,10 +242,28 @@ uint8_t route_command(int pos) { } break; case READ_FAN_SPEED: + // Validate that pos is within range + if (pos >= 2) { return 0; } + + // Union to hold the u16 if (context.fan == FAN1) { - return fan1_history[fan1_history_index]; + union { + uint16_t val; + uint8_t bytes[2]; + } speed; + + speed.val = fan1_history[fan1_history_index]; + + return speed.bytes[1-pos]; } else if (context.fan == FAN2) { - return fan2_history[fan2_history_index]; + union { + uint16_t val; + uint8_t bytes[2]; + } speed; + + speed.val = fan2_history[fan2_history_index]; + + return speed.bytes[1-pos]; } else { return 0; } diff --git a/prosjekt.X/eeprom.h b/prosjekt.X/eeprom.h index b4fe331..e6d9cae 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 fanspeed_sample_rate; + uint16_t ms_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 9ba3b89..0fa2085 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 = 1; +uint16_t timer_period_ms = 1000; uint16_t fan_speed = 0; volatile uint16_t fan1_edge_counter = 0; volatile uint16_t fan2_edge_counter = 0; @@ -12,15 +12,19 @@ void init_TCA0() { TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ } -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 */ +void TCA0_update_period(uint16_t period_ms) { + float period_s = period_ms / 1E3; // Convert the ms to s + float frequency = 1/ period_s; // convert the period to a frequency + float perbuf_val = frequency * F_CPU / 1024; // F_IQR * F_CPU / TCA_prescaler + TCA0.SINGLE.PERBUF = (uint16_t)perbuf_val; + timer_period_ms = period_ms; } // COUNTINGS / TIME = FREQUENCY // FREQ / 2 = GIVES ACTUAL FREQ // FREQ * SEC-IN-A-MINUTE(60) / FAN-BLADES uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms) { - fan_speed = (edge_counter / time_ms); + fan_speed = (edge_counter / (float)((float)time_ms/1000.0) ); fan_speed = fan_speed/2; fan_speed = fan_speed * 60/5; edge_counter = 0; @@ -112,8 +116,8 @@ ISR (TCA0_OVF_vect) { } // Calculate the fanspeed - 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); + 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); // Reset the edge counter fan1_edge_counter = 0; diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index ea0d4a1..b0612b7 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -52,7 +52,7 @@ int main() { init_AC0(); init_AC1(); init_TCA0(); - TCA0_update_period(config.fanspeed_sample_rate); + TCA0_update_period(config.ms_fanspeed_sample_rate); // Only enable UART when required for debugging #ifdef ENABLE_UART @@ -69,7 +69,7 @@ int main() { while (1) { // If we have made a config change, store it and recalculate the period. if (store_config) { - TCA0_update_period(config.fanspeed_sample_rate); + TCA0_update_period(config.ms_fanspeed_sample_rate); write_struct_from_EEPROM(config); store_config = false; } diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index fe671b9..46ec2aa 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -19,7 +19,7 @@ extern "C" { #include #include #include -#define R_T0 10000 +#define R_T0 100E3 #define T_0 298.15 #define B 3950 #define R_1 1000 diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 1b3bd2f..dccc95f 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -11,7 +11,7 @@ float calculate_thermistor_temp(float thermistor_voltage){ float ln; float T_thermistor; float V_thermistor; - #define V_TOT 5 + #define V_TOT 3.3 // Calculate Voltage over thermistor V_thermistor = (V_TOT/1024)*thermistor_voltage; diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index a3eb335..6e1162b 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -3,10 +3,10 @@ void ADC0_init(void) { /* Initializing ADC0 pin*/ - /*Voltage reading on pin pd6*/ - PORTD.PIN6CTRL &= ~PORT_ISC_gm; - PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ - PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; + /*Voltage reading on pin pd2*/ + PORTD.PIN2CTRL &= ~PORT_ISC_gm; + PORTD.PIN2CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN2CTRL &= PORT_PULLUPEN_bm; /* Thermistor */ PORTD.PIN3CTRL &= ~PORT_ISC_gm; @@ -42,7 +42,7 @@ uint16_t thermistor_voltage_read() { } // Gets the value over thermistor uint16_t external_voltage_read() { - ADC0.MUXPOS = 0x06; // Read PD6 + ADC0.MUXPOS = 0x02; // Read PD6 uint16_t adc_val = ADC0_read(); return adc_val; From 25d27c1e1be2b0cb6914583a1a657925f10bf402 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 16:43:07 +0200 Subject: [PATCH 9/9] Add alarm code --- prosjekt.X/main.c | 6 ++++++ prosjekt.X/thermistor-temp.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index b0612b7..5690960 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -74,4 +74,10 @@ int main() { store_config = false; } } + + // Check the temperature, and start the alarm if required + uint16_t thermistor_voltage = thermistor_voltage_read(); + float temperature = calculate_thermistor_temp(thermistor_voltage); + bool start_alarm = voltage_threshold_bool(temperature, 40); + alert_voltage_threshold_exceeded(start_alarm); } diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index dccc95f..b5754a5 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -41,7 +41,7 @@ bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){ //print if the maximum threshold is exceeded. void alert_voltage_threshold_exceeded(bool voltage_threshold_bool){ if (voltage_threshold_bool){ - printf("Error: maximum temperature exceeded"); + //printf("Error: maximum temperature exceeded"); PORTB.OUTSET = ALERT_PIN; } else{ PORTB.OUTCLR = ALERT_PIN;