84 lines
1.9 KiB
C
84 lines
1.9 KiB
C
/*
|
|
* File: main.c
|
|
* Author: Sebastian H. Gabrielli, Helle Augland Grasmo, Ina Min Rørnes
|
|
*
|
|
* Created on March 6, 2024, 12:34 PM
|
|
*/
|
|
#define F_CPU 4E6
|
|
|
|
// Include AVR specific libs
|
|
#include <avr/interrupt.h>
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
|
|
// Include standard C libraries
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
// 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"
|
|
#endif
|
|
|
|
// 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};
|
|
volatile bool store_config = false;
|
|
|
|
int main() {
|
|
// Initialize functionality
|
|
ADC0_init();
|
|
init_alarm_gpio();
|
|
init_i2c();
|
|
|
|
// Fanspeed
|
|
init_AC0();
|
|
init_AC1();
|
|
init_TCA0();
|
|
TCA0_update_period(config.ms_fanspeed_sample_rate);
|
|
|
|
// 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();
|
|
|
|
// Enable interrupts
|
|
sei();
|
|
|
|
while (1) {
|
|
// If we have made a config change, store it and recalculate the period.
|
|
if (store_config) {
|
|
TCA0_update_period(config.ms_fanspeed_sample_rate);
|
|
write_struct_from_EEPROM(config);
|
|
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);
|
|
}
|