2024-04-16 15:02:59 +00:00
|
|
|
|
/*
|
2024-03-06 11:34:22 +00:00
|
|
|
|
* File: main.c
|
2024-04-28 18:23:17 +00:00
|
|
|
|
* Author: Sebastian H. Gabrielli, Helle Augland Grasmo, Ina Min R<EFBFBD>rnes
|
2024-03-06 11:34:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* Created on March 6, 2024, 12:34 PM
|
|
|
|
|
|
*/
|
2024-04-30 09:37:21 +00:00
|
|
|
|
#define F_CPU 4E6
|
|
|
|
|
|
|
|
|
|
|
|
// Include AVR specific libs
|
2024-04-09 14:20:46 +00:00
|
|
|
|
#include <avr/interrupt.h>
|
2024-04-30 09:37:21 +00:00
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Include standard C libraries
|
|
|
|
|
|
#include <stdbool.h>
|
2024-03-06 11:34:22 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <stdlib.h>
|
2024-04-16 15:02:59 +00:00
|
|
|
|
#include <string.h>
|
2024-04-30 09:37:21 +00:00
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Include custom source files
|
|
|
|
|
|
#include "eeprom.h"
|
|
|
|
|
|
#include "voltage.h"
|
2024-04-16 13:30:38 +00:00
|
|
|
|
#include "command-handler.h"
|
2024-04-16 15:02:59 +00:00
|
|
|
|
#include "i2c.h"
|
2024-04-23 13:31:32 +00:00
|
|
|
|
#include "themistor-temp.h"
|
2024-04-30 09:37:21 +00:00
|
|
|
|
#include "fan-speed.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Only enable UART when required for debugging
|
|
|
|
|
|
#ifdef ENABLE_UART
|
2024-03-06 14:36:24 +00:00
|
|
|
|
#include "uart.h"
|
2024-04-30 09:37:21 +00:00
|
|
|
|
#endif
|
2024-04-23 13:31:32 +00:00
|
|
|
|
|
|
|
|
|
|
// Fan history variables
|
2024-04-30 08:59:47 +00:00
|
|
|
|
volatile uint16_t fan1_history[512] = {0};
|
|
|
|
|
|
volatile uint16_t fan2_history[512] = {0};
|
2024-04-30 09:26:08 +00:00
|
|
|
|
// Fan history index variable
|
|
|
|
|
|
volatile uint16_t fan1_history_index = 0;
|
|
|
|
|
|
volatile uint16_t fan2_history_index = 0;
|
2024-03-06 11:34:22 +00:00
|
|
|
|
|
2024-04-24 13:50:40 +00:00
|
|
|
|
// Default config is 500ms sample rate
|
2024-04-30 08:59:47 +00:00
|
|
|
|
volatile config_t config = {500};
|
2024-04-24 13:50:40 +00:00
|
|
|
|
volatile bool store_config = false;
|
|
|
|
|
|
|
2024-03-06 14:36:24 +00:00
|
|
|
|
int main() {
|
2024-04-24 13:50:40 +00:00
|
|
|
|
// Initialize functionality
|
2024-04-24 11:24:15 +00:00
|
|
|
|
ADC0_init();
|
2024-04-30 09:37:21 +00:00
|
|
|
|
init_alarm_gpio();
|
2024-04-16 15:02:59 +00:00
|
|
|
|
init_i2c();
|
2024-04-30 09:37:21 +00:00
|
|
|
|
|
|
|
|
|
|
// Fanspeed
|
|
|
|
|
|
init_AC0();
|
|
|
|
|
|
init_AC1();
|
|
|
|
|
|
init_TCA0();
|
2024-04-30 14:17:52 +00:00
|
|
|
|
TCA0_update_period(config.ms_fanspeed_sample_rate);
|
2024-04-30 09:37:21 +00:00
|
|
|
|
|
|
|
|
|
|
// Only enable UART when required for debugging
|
|
|
|
|
|
#ifdef ENABLE_UART
|
|
|
|
|
|
init_uart((uint16_t)9600);
|
2024-04-16 15:02:59 +00:00
|
|
|
|
stdout = &USART_stream;
|
2024-04-30 09:37:21 +00:00
|
|
|
|
#endif
|
2024-04-16 15:02:59 +00:00
|
|
|
|
|
2024-04-24 13:50:40 +00:00
|
|
|
|
// Read the stored config struct
|
2024-04-27 13:39:46 +00:00
|
|
|
|
config = read_struct_from_EEPROM();
|
2024-04-30 08:59:47 +00:00
|
|
|
|
|
2024-04-30 09:37:21 +00:00
|
|
|
|
// Enable interrupts
|
2024-04-16 15:02:59 +00:00
|
|
|
|
sei();
|
2024-03-06 11:34:22 +00:00
|
|
|
|
|
2024-04-16 15:02:59 +00:00
|
|
|
|
while (1) {
|
2024-04-30 10:00:29 +00:00
|
|
|
|
// If we have made a config change, store it and recalculate the period.
|
2024-04-30 08:59:47 +00:00
|
|
|
|
if (store_config) {
|
2024-04-30 14:17:52 +00:00
|
|
|
|
TCA0_update_period(config.ms_fanspeed_sample_rate);
|
2024-04-30 08:59:47 +00:00
|
|
|
|
write_struct_from_EEPROM(config);
|
|
|
|
|
|
store_config = false;
|
|
|
|
|
|
}
|
2024-04-16 15:02:59 +00:00
|
|
|
|
}
|
2024-04-30 14:43:07 +00:00
|
|
|
|
|
|
|
|
|
|
// 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);
|
2024-04-16 15:02:59 +00:00
|
|
|
|
}
|