Compare commits
No commits in common. "main" and "eeprom" have entirely different histories.
@ -1,307 +0,0 @@
|
||||
#include "command-handler.h"
|
||||
#include "fan-speed.h"
|
||||
|
||||
// Initialize empty, global command context
|
||||
volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE,
|
||||
CNF_NONE};
|
||||
|
||||
void parse_command(uint8_t command[], uint8_t command_len) {
|
||||
///////////////////////
|
||||
// Command selection //
|
||||
///////////////////////
|
||||
|
||||
// Validate that we have a command
|
||||
if (command_len < 1) {
|
||||
context.command = UNKNOWN_COMMAND;
|
||||
}
|
||||
|
||||
// Extract the first byte, which contains the command
|
||||
uint8_t command_byte = command[0];
|
||||
|
||||
// Figure out which command to run
|
||||
switch (command_byte) {
|
||||
case 0x11: // Read config
|
||||
context.command = READ_CONFIG;
|
||||
break;
|
||||
case 0x12: // Read voltage
|
||||
context.command = READ_VOLTAGE;
|
||||
break;
|
||||
case 0x13: // Read temperature
|
||||
context.command = READ_TERMPERATURE;
|
||||
break;
|
||||
case 0x14: // Read current fan speed
|
||||
context.command = READ_FAN_SPEED;
|
||||
break;
|
||||
case 0x15: // Read bulk fan speed
|
||||
context.command = READ_BULK_FAN_SPEED;
|
||||
break;
|
||||
case 0x21: // Write config
|
||||
context.command = WRITE_CONFIG;
|
||||
break;
|
||||
case 0x22: // Clear stored fan speed data
|
||||
context.command = CLEAR_BULK_FAN_SPEED;
|
||||
break;
|
||||
default: // Unrecognized command
|
||||
context.command = UNKNOWN_COMMAND;
|
||||
break;
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
// First parameter selection //
|
||||
///////////////////////////////
|
||||
|
||||
// Check if the command does not require a parameter. If it does not, return.
|
||||
if (context.command == READ_TERMPERATURE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate that we have a second parameter, else requirements for command are
|
||||
// not fulfilled. return unknown command.
|
||||
if (command_len < 2) {
|
||||
context.command = UNKNOWN_COMMAND;
|
||||
return;
|
||||
}
|
||||
|
||||
// Store the parameter
|
||||
uint8_t param = command[1];
|
||||
|
||||
// Extract the parameter. Parameter is dependent on command
|
||||
switch (command[0]) {
|
||||
// Configuration parameters
|
||||
case 0x11: // Read config
|
||||
case 0x21: // Write config
|
||||
if (param == 0x01) {
|
||||
context.conf = SAMPLE_TIME;
|
||||
} else {
|
||||
context.conf = CNF_NONE;
|
||||
}
|
||||
break;
|
||||
|
||||
// Voltage parameters
|
||||
case 0x12: // Read voltage
|
||||
if (param == 0x01) {
|
||||
context.src_voltage = SRC_INTERNAL;
|
||||
} else if (param == 0x02) {
|
||||
context.src_voltage = SRC_EXTRNAL;
|
||||
} else if (param == 0x03) {
|
||||
context.src_voltage = SRC_THERMISTOR;
|
||||
} else {
|
||||
context.src_voltage = SRC_NONE;
|
||||
}
|
||||
break;
|
||||
|
||||
// Fan parameters
|
||||
case 0x14: // Read current fan speed
|
||||
case 0x15: // Read bulk fan speed
|
||||
case 0x22: // Clear stored fan speed data
|
||||
if (param == 0x01) {
|
||||
context.fan = FAN1;
|
||||
} else if (param == 0x02) {
|
||||
context.fan = FAN2;
|
||||
} else {
|
||||
context.fan = FAN_NONE;
|
||||
}
|
||||
break;
|
||||
|
||||
// This should never be reached
|
||||
default: // Unrecognized command
|
||||
context.command = UNKNOWN_COMMAND;
|
||||
break;
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
// Third parameter selection //
|
||||
/////////////////////////////////
|
||||
|
||||
// Check if the command does not require a second parameter. If it does not,
|
||||
// return. Only config write requires a second parameter.
|
||||
if (context.command != WRITE_CONFIG) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate that we have a third parameter, else requirements for command are
|
||||
// not fulfilled. return unknown command.
|
||||
if (command_len < 3) {
|
||||
context.command = UNKNOWN_COMMAND;
|
||||
return;
|
||||
}
|
||||
|
||||
// Store the parameter
|
||||
param = command[1];
|
||||
|
||||
context.conf = param;
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// Fourth parameter selection //
|
||||
////////////////////////////////
|
||||
|
||||
// Validate that we have a fourth parameter, else requirements for command are
|
||||
// not fulfilled. return unknown command. Second parameter is u16, thus two bytes.
|
||||
if (command_len < 4) {
|
||||
context.command = UNKNOWN_COMMAND;
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract the value
|
||||
union {
|
||||
uint16_t value;
|
||||
uint8_t bytes[2];
|
||||
} config_value;
|
||||
|
||||
config_value.bytes[0] = command[2];
|
||||
config_value.bytes[1] = command[3];
|
||||
|
||||
// Store the value
|
||||
context.conf_val = config_value.value;
|
||||
|
||||
// exit
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t route_command(int pos) {
|
||||
switch (context.command) {
|
||||
case WRITE_CONFIG:
|
||||
switch (context.conf) {
|
||||
case SAMPLE_TIME:
|
||||
// Overwrite the config value
|
||||
config.ms_fanspeed_sample_rate = context.conf_val;
|
||||
|
||||
// Set the flag to store it in the EEPROM
|
||||
store_config = true;
|
||||
return 0;
|
||||
break;
|
||||
case CNF_NONE:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case READ_CONFIG:
|
||||
{
|
||||
switch (context.conf) {
|
||||
case SAMPLE_TIME:
|
||||
{
|
||||
// Validate that pos is within the valid range
|
||||
if (pos >= 2) { return 0x00; }
|
||||
|
||||
// Config only has one parameter so we sent that parameter
|
||||
// Create a union to store the data
|
||||
union {
|
||||
uint16_t value;
|
||||
uint8_t bytes[2];
|
||||
} config_value;
|
||||
config_value.value = config.ms_fanspeed_sample_rate;
|
||||
|
||||
// Return the corresponding data byte
|
||||
return config_value.bytes[1-pos];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
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;
|
||||
uint8_t bytes[2];
|
||||
} voltage;
|
||||
|
||||
// Figure out which voltage source to read
|
||||
switch (context.src_voltage) {
|
||||
case SRC_INTERNAL:
|
||||
voltage.v = internal_voltage_read();
|
||||
break;
|
||||
case SRC_EXTRNAL:
|
||||
voltage.v = external_voltage_read();
|
||||
break;
|
||||
case SRC_THERMISTOR:
|
||||
voltage.v = thermistor_voltage_read();
|
||||
break;
|
||||
default:
|
||||
return 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
// Send the data
|
||||
return voltage.bytes[1-pos];
|
||||
}
|
||||
case READ_TERMPERATURE:
|
||||
{
|
||||
uint16_t v_therm = thermistor_voltage_read();
|
||||
union {
|
||||
int16_t temp;
|
||||
uint8_t bytes[2];
|
||||
} temperature;
|
||||
temperature.temp = (int16_t) ( calculate_thermistor_temp(v_therm) * 1000 );
|
||||
return temperature.bytes[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) {
|
||||
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) {
|
||||
union {
|
||||
uint16_t val;
|
||||
uint8_t bytes[2];
|
||||
} speed;
|
||||
|
||||
speed.val = fan2_history[fan2_history_index];
|
||||
|
||||
return speed.bytes[1-pos];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case READ_BULK_FAN_SPEED:
|
||||
if (context.fan == FAN1) {
|
||||
// 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) {
|
||||
// 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;
|
||||
}
|
||||
break;
|
||||
case CLEAR_BULK_FAN_SPEED:
|
||||
// Overwrite the content of the desired fan array with zeroes
|
||||
if (context.fan == FAN1) {
|
||||
memset(fan1_history, 0, sizeof(fan1_history));
|
||||
} else if (context.fan == FAN2) {
|
||||
memset(fan2_history, 0, sizeof(fan2_history));
|
||||
}
|
||||
return 0;
|
||||
break;
|
||||
case UNKNOWN_COMMAND:
|
||||
default:
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
@ -1,89 +0,0 @@
|
||||
/*
|
||||
* File: command-handler.h
|
||||
* Author: Sebastian H. Gabrielli
|
||||
*
|
||||
* Created on March 6, 2024, 12:43 PM
|
||||
*/
|
||||
|
||||
#ifndef COMMAND_HANDLER_H
|
||||
#define COMMAND_HANDLER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "eeprom.h"
|
||||
#include <avr/io.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "voltage.h"
|
||||
#include "themistor-temp.h"
|
||||
|
||||
// Enum of all valid command types
|
||||
typedef enum {
|
||||
WRITE_CONFIG = 0x21, // Change the configuration
|
||||
READ_CONFIG = 0x11, // Read, and print the current configuration
|
||||
READ_VOLTAGE = 0x12, // Read, and print a voltage
|
||||
READ_TERMPERATURE = 0x13, // Read, and print the temperature
|
||||
READ_FAN_SPEED = 0x14, // Read, and print the current fan speed
|
||||
READ_BULK_FAN_SPEED = 0x15, // Read, and print the stored back fan speed data
|
||||
CLEAR_BULK_FAN_SPEED = 0x22, // Clear the buffer of stored fan speed data
|
||||
UNKNOWN_COMMAND // An unrecognized command has been sent
|
||||
} command_t;
|
||||
|
||||
// Enum of all valid voltage sources
|
||||
typedef enum {
|
||||
SRC_INTERNAL, // Internal volage
|
||||
SRC_EXTRNAL, // External voltage
|
||||
SRC_THERMISTOR, // Thermistor voltage
|
||||
SRC_NONE // No voltage source selected
|
||||
} src_voltage_t;
|
||||
|
||||
// Enum of all valid config options
|
||||
// TODO: Move into config header file
|
||||
typedef enum {
|
||||
SAMPLE_TIME = 0x01, // Time between each fan speed sample
|
||||
CNF_NONE, // No config option
|
||||
} config_option_t;
|
||||
|
||||
// Enum of all valid fans
|
||||
// TODO: Consider moving into it's own fan page
|
||||
typedef enum {
|
||||
FAN1 = 1, // The first fan
|
||||
FAN2 = 2, // The second fan
|
||||
FAN_NONE // No fan
|
||||
} fans_t;
|
||||
|
||||
// Struct with command context
|
||||
typedef struct {
|
||||
command_t command; // The command to execute
|
||||
src_voltage_t src_voltage; // The selected voltage source
|
||||
fans_t fan; // The selected fan
|
||||
config_option_t conf; // The configuration option to cange
|
||||
uint16_t conf_val; // The value of the config option to change
|
||||
} command_context_t;
|
||||
|
||||
// Fan history variables
|
||||
extern volatile uint16_t fan1_history[512];
|
||||
extern volatile uint16_t fan2_history[512];
|
||||
// Fan history index variable
|
||||
extern volatile uint16_t fan1_history_index;
|
||||
extern volatile uint16_t fan2_history_index;
|
||||
|
||||
// Config
|
||||
extern volatile config_t config;
|
||||
extern volatile bool store_config;
|
||||
|
||||
// Parses the input string and outputs one of the valid commands
|
||||
void parse_command(uint8_t *command, uint8_t command_len);
|
||||
|
||||
// Routes the provided command to the appropriate function to handle it
|
||||
// If the command is a read command it then returns the current byte.
|
||||
// The position is the byte to read when multiple bytes are being read.
|
||||
uint8_t route_command(int pos);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* COMMAND_HANDLER_H */
|
||||
@ -1,9 +1,17 @@
|
||||
#include "eeprom.h"
|
||||
|
||||
// The start address for the controller data
|
||||
uint16_t EEMEM start_address_controller = 0x1400;
|
||||
uint8_t EEMEM start_address_controller = 0x00;
|
||||
|
||||
// Checks if the EEPROM memory is ready to be written in and waits until it is.
|
||||
// Where the writing of the fans points start
|
||||
uint8_t EEMEM start_address_fan1 = 0x30;
|
||||
uint8_t EEMEM start_address_fan2 = 0x60;
|
||||
|
||||
// The placement for the next datapoint form the fans.
|
||||
uint8_t EEMEM current_address_fan1 = 0x30;
|
||||
uint8_t EEMEM current_address_fan2 = 0x60;
|
||||
|
||||
// Checks if the EEPROM memory is ready to be written in.
|
||||
void check_eeprom_is_ready(){
|
||||
while(1){
|
||||
if (eeprom_is_ready()){
|
||||
@ -15,28 +23,77 @@ void check_eeprom_is_ready(){
|
||||
}
|
||||
|
||||
|
||||
// Takes inn a struct by the form of config_t
|
||||
// Checks if the eeprom is ready to be written in
|
||||
// Checks if it has been written information at the address
|
||||
// If true, the infromation is replaced with the intaken struct
|
||||
// else the intaken struct is written at the address.
|
||||
void write_struct_from_EEPROM(config_t write_struct){
|
||||
// Calculate the required storage size
|
||||
uint8_t struct_size = sizeof(write_struct);
|
||||
|
||||
// Wait for the EEPROM to be ready
|
||||
check_eeprom_is_ready();
|
||||
|
||||
// Update the stored config stuct
|
||||
eeprom_update_block((void*) &write_struct,(void*) &start_address_controller, struct_size);
|
||||
|
||||
}
|
||||
|
||||
// Reads the memory block at the address start_address_controller
|
||||
// returns a struct in form of config_t
|
||||
config_t read_struct_from_EEPROM(){
|
||||
// Create a config struct to hold the received data
|
||||
config_t read_struct = { 0 };
|
||||
//is eeprom ready??
|
||||
config_t read_struct;
|
||||
uint8_t struct_size = sizeof(read_struct);
|
||||
|
||||
// Wait for the EEPROM to be ready
|
||||
check_eeprom_is_ready();
|
||||
|
||||
// Read the data from the EEPROM
|
||||
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
|
||||
|
||||
// Return the data
|
||||
return read_struct;
|
||||
}
|
||||
|
||||
// Takes inn a dataPoint and what data set it belongs to
|
||||
// checks if EEPROM is ready
|
||||
// If the dataset is 1, the datapoint is written at the address.
|
||||
// If the dataset is 2 its written at another point.
|
||||
int write_data_point_in_EEPROM(uint8_t byte, uint8_t fan_num){
|
||||
|
||||
check_eeprom_is_ready();
|
||||
if (fan_num == 1){
|
||||
eeprom_update_byte(current_address_fan1, byte);
|
||||
current_address_fan1++;
|
||||
return 1;
|
||||
} else if (fan_num == 2){
|
||||
eeprom_update_byte(current_address_fan2, byte);
|
||||
current_address_fan2++;
|
||||
return 1;
|
||||
} else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Reads all the datapoints to the choosen data.
|
||||
// it writes the data points in the USART stream.
|
||||
uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array){
|
||||
uint8_t byte = 0;
|
||||
|
||||
if (fan_num == 1){
|
||||
uint8_t len = current_address_fan1 - start_address_fan1;
|
||||
|
||||
check_eeprom_is_ready();
|
||||
|
||||
for (uint8_t i = 0; i <len; i++){
|
||||
byte = eeprom_read_byte(i + start_address_fan1);
|
||||
array[i] = byte;
|
||||
printf("Fanspeed nr %u : %u", i, byte);
|
||||
}
|
||||
} else if (fan_num == 2){
|
||||
uint8_t len = current_address_fan2 - start_address_fan2;
|
||||
check_eeprom_is_ready();
|
||||
|
||||
for (uint8_t i = 0; i <len; i++){
|
||||
byte = eeprom_read_byte(i + start_address_fan2);
|
||||
array[i] = byte;
|
||||
printf("Fanspeed nr %u : %u", i, byte);
|
||||
}
|
||||
}
|
||||
return sizeof(array);
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* File: eeprom.h
|
||||
* Author: Helle Augland Grasmo, Sebastian H. Gabrielli
|
||||
* Author: Helle Augland Grasmo
|
||||
*
|
||||
* Created on 06 March 2024, 15:30
|
||||
*/
|
||||
@ -19,24 +19,28 @@ extern "C" {
|
||||
#include <avr/eeprom.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// The code is inspired by "EEPROM handling" form avr-libc
|
||||
// web link: https://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html
|
||||
|
||||
|
||||
// Struct for information on the controller.
|
||||
typedef struct {
|
||||
uint16_t ms_fanspeed_sample_rate;
|
||||
uint8_t fanSpeed;
|
||||
} config_t;
|
||||
|
||||
// Check if EEPROM is ready to be written to
|
||||
// Check if EEPROM is ready to be written in
|
||||
void check_eeprom_is_ready();
|
||||
|
||||
// Writes a config_t struct to the EEPROM
|
||||
// Writes a struct in EEPROM
|
||||
void write_struct_from_EEPROM(config_t write_struct);
|
||||
|
||||
// Read data from EEPROM and return it as a config_t struct
|
||||
// Read data from EEPROM and return it as a controller struct
|
||||
config_t read_struct_from_EEPROM();
|
||||
|
||||
// Writes a datapoint in EEPROM
|
||||
int write_data_point_from_EEPROM(uint8_t byte, uint8_t fan_num);
|
||||
|
||||
// Reads all the dataPoints form EEPROM
|
||||
uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -1,127 +0,0 @@
|
||||
#include "fan-speed.h"
|
||||
#include "uart.h"
|
||||
|
||||
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;
|
||||
|
||||
|
||||
void init_TCA0() {
|
||||
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
|
||||
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */
|
||||
}
|
||||
|
||||
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 / (float)((float)time_ms/1000.0) );
|
||||
fan_speed = fan_speed/2;
|
||||
fan_speed = fan_speed * 60/5;
|
||||
edge_counter = 0;
|
||||
return fan_speed;
|
||||
}
|
||||
|
||||
|
||||
void init_fan_gpio() {
|
||||
// CONFIGURE PINS AS ANALOG INPUTS
|
||||
|
||||
// PIN FOR FAN 1
|
||||
PORTD.DIRSET &= ~PIN6_bm;
|
||||
PORTD.PIN6CTRL &= ~ PORT_ISC_gm;
|
||||
PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
||||
|
||||
// PIN FOR FAN 2
|
||||
PORTD.DIRSET &= ~PIN4_bm;
|
||||
PORTD.PIN4CTRL &= ~ PORT_ISC_gm ;
|
||||
PORTD.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
||||
|
||||
// PIN FOR REFRENCE
|
||||
PORTD.DIRSET &= PIN7_bm;
|
||||
PORTD.PIN7CTRL &= ~ PORT_ISC_gm ;
|
||||
PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
||||
}
|
||||
|
||||
void init_AC0(){
|
||||
//Wincontroll disabled
|
||||
AC0.CTRLB = 0x00;
|
||||
|
||||
//SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON
|
||||
// FAN USE PD6 COMPARE WITH PD3
|
||||
AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN2_gc;
|
||||
|
||||
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
|
||||
AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
|
||||
|
||||
// TURN ON INTERUPT
|
||||
AC0.INTCTRL = 0x01;
|
||||
}
|
||||
|
||||
|
||||
void init_AC1(){
|
||||
//Wincontroll disabled
|
||||
AC1.CTRLB = 0x00;
|
||||
|
||||
//SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON
|
||||
// FAN USE PD4, COMPARE WITH PD3
|
||||
AC1.MUXCTRL = AC_MUXPOS_AINP2_gc | AC_MUXNEG_AINN2_gc;
|
||||
|
||||
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
|
||||
AC1.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
|
||||
|
||||
// TURN ON INTERUPT
|
||||
AC1.INTCTRL = 0x01;
|
||||
}
|
||||
|
||||
|
||||
ISR(AC0_AC_vect){ // AC0 vec flag
|
||||
cli();
|
||||
fan1_edge_counter++;
|
||||
AC0.STATUS |= 0x10; //CMP flag to 0
|
||||
sei();
|
||||
}
|
||||
|
||||
|
||||
ISR(AC1_AC_vect){ // AC1 vec flag
|
||||
cli();
|
||||
fan2_edge_counter++;
|
||||
AC1.STATUS |= 0x10; //CMP flag to 0
|
||||
sei();
|
||||
}
|
||||
|
||||
// TIMER INTERUPT
|
||||
ISR (TCA0_OVF_vect) {
|
||||
cli();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
|
||||
sei();
|
||||
}
|
||||
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* File: fanseeeed.h
|
||||
* Author: inami, Helle Augland Grasmo
|
||||
*
|
||||
* Created on 13. mars 2024, 13:38
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef FANSEEEED_H
|
||||
#define FANSEEEED_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <float.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
/*
|
||||
The code has inspiration from "Getting Started with Analog comparator(AC)" by Microchip for setting up analog comparrator.
|
||||
* web link: https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/TB3211-Getting-Started-with-AC-DS90003211.pdf
|
||||
* 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
|
||||
extern volatile uint16_t fan1_history_index;
|
||||
extern volatile uint16_t fan2_history_index;
|
||||
|
||||
// INITALICE TIMER COUNTER
|
||||
void init_TCA0();
|
||||
|
||||
// UPDATE TIMER PERIOD
|
||||
void TCA0_update_period_ms (uint16_t timer_period);
|
||||
|
||||
// TAKES INN A TIME AND A THE COUNTED FAN DIPS
|
||||
// RETURNS THE RPM OF THE FAN
|
||||
uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms);
|
||||
|
||||
// INITIALISING FAN PORTS
|
||||
void init_fan_gpio();
|
||||
|
||||
// INIT AC0 TO COMPARE PD6 AND PD7
|
||||
void init_AC0();
|
||||
|
||||
// INIT AC1 TO COMPARE PD4 AND PD7
|
||||
void init_AC1();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* FANSEEEED_H */
|
||||
|
||||
125
prosjekt.X/i2c.c
125
prosjekt.X/i2c.c
@ -1,125 +0,0 @@
|
||||
#include "i2c.h"
|
||||
#include "command-handler.h"
|
||||
|
||||
// Basic I2C handling structure is heavily inspired by:
|
||||
// https://github.com/microchip-pic-avr-examples/avr128db48-bare-metal-twi-mplab/blob/master/twi-client.X/peripherals/TWI/TWI_client.c
|
||||
|
||||
// We need to keep track of if the stop confition is in regards to a write or
|
||||
// read request
|
||||
volatile bool last_action_write = false;
|
||||
|
||||
// Buffer to hold the received data
|
||||
volatile uint8_t i2c_recv_buf[I2C_RECV_BUF_SIZE] = {0};
|
||||
// Counter to know which datapoint we're on
|
||||
volatile uint8_t i2c_recv_len = 0;
|
||||
|
||||
void init_i2c(void) {
|
||||
// Pin setup
|
||||
PORTA.DIRSET = PIN2_bm | PIN3_bm;
|
||||
PORTA.PINCTRLUPD = PIN2_bm | PIN3_bm;
|
||||
|
||||
// Enable operating in debug
|
||||
TWI0.DBGCTRL = TWI_DBGRUN_bm;
|
||||
|
||||
// Initialize the control A register
|
||||
TWI0.CTRLA = TWI_INPUTLVL_I2C_gc // I2C voltage transition level
|
||||
| TWI_SDASETUP_4CYC_gc // Four clock cycles setup time
|
||||
| TWI_SDAHOLD_50NS_gc // 50ns SDA hold time
|
||||
| TWI_FMPEN_OFF_gc // Standard SPI timing
|
||||
;
|
||||
|
||||
// The device's slave address
|
||||
TWI0.SADDR = 0x42;
|
||||
|
||||
// Enable acting as a slave
|
||||
TWI0.SCTRLA = TWI_DIEN_bm // Enable data interrupt
|
||||
| TWI_APIEN_bm // Enable more interrupts
|
||||
| TWI_PIEN_bm // Enable stop flag interrupt
|
||||
| PIN2_bm // Respond to all TWI addresses
|
||||
| TWI_ENABLE_bm // Enable acting as a slave
|
||||
;
|
||||
|
||||
// Reset the received stuff
|
||||
i2c_reset_recv();
|
||||
}
|
||||
|
||||
// Reset received counter and clear receive buffer
|
||||
void i2c_reset_recv() {
|
||||
i2c_recv_len = 0;
|
||||
for (int i = 0; i < I2C_RECV_BUF_SIZE; i++) {
|
||||
i2c_recv_buf[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void i2c_write_handler(uint8_t data) {
|
||||
last_action_write = true;
|
||||
|
||||
// Validate that we are not overflowing the buffer
|
||||
if (i2c_recv_len >= I2C_RECV_BUF_SIZE) { return; }
|
||||
|
||||
// Write the data to the receive buffer
|
||||
i2c_recv_buf[i2c_recv_len] = data;
|
||||
i2c_recv_len++;
|
||||
}
|
||||
|
||||
void i2c_read_handler() {
|
||||
last_action_write = false;
|
||||
TWI0.SDATA = route_command(i2c_recv_len);
|
||||
i2c_recv_len++; // Increment the counter of the current amount of requests
|
||||
}
|
||||
|
||||
void i2c_stop_handler() {
|
||||
if (last_action_write) {
|
||||
// Parse the received command data
|
||||
parse_command(i2c_recv_buf, i2c_recv_len);
|
||||
|
||||
// If the received command is a write only command we want to route it now.
|
||||
if (i2c_recv_buf[0] == CLEAR_BULK_FAN_SPEED || i2c_recv_buf[0] == WRITE_CONFIG) {
|
||||
route_command(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the buffer for future transmissions
|
||||
i2c_reset_recv();
|
||||
}
|
||||
|
||||
// Address received
|
||||
ISR(TWI0_TWIS_vect) {
|
||||
// Disable interrupts while handling I2C
|
||||
cli();
|
||||
|
||||
// Check for the data interrupt flag
|
||||
if (TWI0.SSTATUS & TWI_DIF_bm) {
|
||||
|
||||
if (((TWI0.SSTATUS & TWI_DIR_bm) >> TWI_DIR_bp) == 0) {
|
||||
// Data write Controller -> Target
|
||||
uint8_t data = TWI0.SDATA;
|
||||
|
||||
// Send the data to the write handler
|
||||
i2c_write_handler(data);
|
||||
} else {
|
||||
// Data read Controller <- Target
|
||||
i2c_read_handler();
|
||||
}
|
||||
|
||||
// Acknowledge having received
|
||||
TWI0.SCTRLB = TWI_ACKACT_ACK_gc | TWI_SCMD_RESPONSE_gc;
|
||||
}
|
||||
|
||||
// Check for address match or STOP
|
||||
if (TWI0.SSTATUS & TWI_APIF_bm) {
|
||||
|
||||
if (TWI0.SSTATUS & TWI_AP_ADR_gc) {
|
||||
// Address match, just send ack
|
||||
TWI0.SCTRLB = TWI_ACKACT_ACK_gc | TWI_SCMD_RESPONSE_gc;
|
||||
} else {
|
||||
// STOP condition received
|
||||
i2c_stop_handler();
|
||||
// Send ACK
|
||||
TWI0.SCTRLB = TWI_ACKACT_ACK_gc | TWI_SCMD_RESPONSE_gc;
|
||||
}
|
||||
}
|
||||
|
||||
// Re-enable interrupts
|
||||
sei();
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
/*
|
||||
* File: i2c.h
|
||||
* Author: sebgab
|
||||
*
|
||||
* Created on March 6, 2024, 1:53 PM
|
||||
*/
|
||||
|
||||
#ifndef I2C_H
|
||||
#define I2C_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Include the IO for I2C
|
||||
#include "command-handler.h"
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#include <stdbool.h>
|
||||
#include <util/twi.h>
|
||||
|
||||
// Received data buffer size
|
||||
// The size is larger than any expected command length
|
||||
#define I2C_RECV_BUF_SIZE 16
|
||||
|
||||
// Reset recv to initial state
|
||||
void i2c_reset_recv();
|
||||
|
||||
// Initialize the I2C bus
|
||||
void init_i2c(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* I2C_H */
|
||||
@ -1,83 +1,19 @@
|
||||
/*
|
||||
* File: main.c
|
||||
* Author: Sebastian H. Gabrielli, Helle Augland Grasmo, Ina Min Rørnes
|
||||
* Author: Sebastian H. Gabrielli, Helle Augland Grasmo
|
||||
*
|
||||
* 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 "eeprom.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#define F_CPU 4E6
|
||||
|
||||
// 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"
|
||||
void main() {
|
||||
|
||||
// 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);
|
||||
while(1){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4,19 +4,7 @@
|
||||
<logicalFolder name="HeaderFiles"
|
||||
displayName="Header Files"
|
||||
projectFiles="true">
|
||||
<itemPath>uart.h</itemPath>
|
||||
<itemPath>voltage.h</itemPath>
|
||||
<itemPath>themistor-temp.h</itemPath>
|
||||
<itemPath>command-handler.h</itemPath>
|
||||
<itemPath>eeprom.h</itemPath>
|
||||
<itemPath>i2c.h</itemPath>
|
||||
<itemPath>fan-speed.h</itemPath>
|
||||
</logicalFolder>
|
||||
<logicalFolder name="ExternalFiles"
|
||||
displayName="Important Files"
|
||||
projectFiles="true">
|
||||
<itemPath>Makefile</itemPath>
|
||||
<itemPath>prosjekt.mc3</itemPath>
|
||||
</logicalFolder>
|
||||
<logicalFolder name="LinkerScript"
|
||||
displayName="Linker Files"
|
||||
@ -26,13 +14,12 @@
|
||||
displayName="Source Files"
|
||||
projectFiles="true">
|
||||
<itemPath>main.c</itemPath>
|
||||
<itemPath>uart.c</itemPath>
|
||||
<itemPath>voltage.c</itemPath>
|
||||
<itemPath>thermistor-temp.c</itemPath>
|
||||
<itemPath>command-handler.c</itemPath>
|
||||
<itemPath>i2c.c</itemPath>
|
||||
<itemPath>eeprom.c</itemPath>
|
||||
<itemPath>fan-speed.c</itemPath>
|
||||
</logicalFolder>
|
||||
<logicalFolder name="ExternalFiles"
|
||||
displayName="Important Files"
|
||||
projectFiles="false">
|
||||
<itemPath>Makefile</itemPath>
|
||||
</logicalFolder>
|
||||
</logicalFolder>
|
||||
<projectmakefile>Makefile</projectmakefile>
|
||||
@ -49,7 +36,7 @@
|
||||
<platform>2</platform>
|
||||
</toolsSet>
|
||||
<packs>
|
||||
<pack name="AVR-Dx_DFP" vendor="Microchip" version="2.4.286"/>
|
||||
<pack name="AVR-Dx_DFP" vendor="Microchip" version="2.3.272"/>
|
||||
</packs>
|
||||
<ScriptingSettings>
|
||||
</ScriptingSettings>
|
||||
@ -114,7 +101,6 @@
|
||||
</HI-TECH-COMP>
|
||||
<HI-TECH-LINK>
|
||||
<property key="additional-options-checksum" value=""/>
|
||||
<property key="additional-options-checksumAVR" value=""/>
|
||||
<property key="additional-options-code-offset" value=""/>
|
||||
<property key="additional-options-command-line" value=""/>
|
||||
<property key="additional-options-errata" value=""/>
|
||||
@ -160,54 +146,7 @@
|
||||
<property key="remove-unused-sections" value="true"/>
|
||||
</HI-TECH-LINK>
|
||||
<Tool>
|
||||
<property key="AutoSelectMemRanges" value="auto"/>
|
||||
<property key="communication.activationmode" value="nohv"/>
|
||||
<property key="communication.interface" value="updi"/>
|
||||
<property key="communication.speed" value="0,500"/>
|
||||
<property key="debugoptions.debug-startup" value="Use system settings"/>
|
||||
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
|
||||
<property key="debugoptions.useswbreakpoints" value="true"/>
|
||||
<property key="event.recorder.debugger.behavior" value="Running"/>
|
||||
<property key="event.recorder.enabled" value="false"/>
|
||||
<property key="event.recorder.scvd.files" value=""/>
|
||||
<property key="firmware.path"
|
||||
value="Press to browse for a specific firmware version"/>
|
||||
<property key="firmware.toolpack"
|
||||
value="Press to select which tool pack to use"/>
|
||||
<property key="firmware.update.action" value="firmware.update.use.latest"/>
|
||||
<property key="freeze.timers" value="false"/>
|
||||
<property key="lastid" value=""/>
|
||||
<property key="memories.aux" value="false"/>
|
||||
<property key="memories.bootflash" value="true"/>
|
||||
<property key="memories.configurationmemory" value="true"/>
|
||||
<property key="memories.configurationmemory2" value="true"/>
|
||||
<property key="memories.dataflash" value="true"/>
|
||||
<property key="memories.eeprom" value="true"/>
|
||||
<property key="memories.exclude.configurationmemory" value="true"/>
|
||||
<property key="memories.flashdata" value="true"/>
|
||||
<property key="memories.id" value="true"/>
|
||||
<property key="memories.instruction.ram.ranges"
|
||||
value="${memories.instruction.ram.ranges}"/>
|
||||
<property key="memories.programmemory" value="true"/>
|
||||
<property key="memories.programmemory.ranges" value="0-ffff"/>
|
||||
<property key="poweroptions.powerenable" value="false"/>
|
||||
<property key="programmerToGoFilePath"
|
||||
value="C:/Users/inami/Documents/mikrokontrollersystemer-prosjekt/prosjekt.X/debug/default/prosjekt_ptg"/>
|
||||
<property key="programoptions.eraseb4program" value="true"/>
|
||||
<property key="programoptions.preservedataflash" value="false"/>
|
||||
<property key="programoptions.preservedataflash.ranges"
|
||||
value="${memories.dataflash.default}"/>
|
||||
<property key="programoptions.preserveeeprom" value="false"/>
|
||||
<property key="programoptions.preserveeeprom.ranges" value="1400-15ff"/>
|
||||
<property key="programoptions.preserveprogram.ranges" value=""/>
|
||||
<property key="programoptions.preserveprogramrange" value="false"/>
|
||||
<property key="programoptions.preserveuserid" value="false"/>
|
||||
<property key="programoptions.programuserotp" value="false"/>
|
||||
<property key="toolpack.updateoptions"
|
||||
value="toolpack.updateoptions.uselatestoolpack"/>
|
||||
<property key="toolpack.updateoptions.packversion"
|
||||
value="Press to select which tool pack to use"/>
|
||||
<property key="voltagevalue" value=""/>
|
||||
</Tool>
|
||||
<XC8-CO>
|
||||
<property key="coverage-enable" value=""/>
|
||||
@ -232,52 +171,7 @@
|
||||
<property key="wpo-lto" value="false"/>
|
||||
</XC8-config-global>
|
||||
<nEdbgTool>
|
||||
<property key="AutoSelectMemRanges" value="auto"/>
|
||||
<property key="communication.activationmode" value="nohv"/>
|
||||
<property key="communication.interface" value="updi"/>
|
||||
<property key="communication.speed" value="0,500"/>
|
||||
<property key="debugoptions.debug-startup" value="Use system settings"/>
|
||||
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
|
||||
<property key="debugoptions.useswbreakpoints" value="true"/>
|
||||
<property key="event.recorder.debugger.behavior" value="Running"/>
|
||||
<property key="event.recorder.enabled" value="false"/>
|
||||
<property key="event.recorder.scvd.files" value=""/>
|
||||
<property key="firmware.path"
|
||||
value="Press to browse for a specific firmware version"/>
|
||||
<property key="firmware.toolpack"
|
||||
value="Press to select which tool pack to use"/>
|
||||
<property key="firmware.update.action" value="firmware.update.use.latest"/>
|
||||
<property key="freeze.timers" value="false"/>
|
||||
<property key="lastid" value=""/>
|
||||
<property key="memories.aux" value="false"/>
|
||||
<property key="memories.bootflash" value="true"/>
|
||||
<property key="memories.configurationmemory" value="true"/>
|
||||
<property key="memories.configurationmemory2" value="true"/>
|
||||
<property key="memories.dataflash" value="true"/>
|
||||
<property key="memories.eeprom" value="true"/>
|
||||
<property key="memories.exclude.configurationmemory" value="true"/>
|
||||
<property key="memories.flashdata" value="true"/>
|
||||
<property key="memories.id" value="true"/>
|
||||
<property key="memories.instruction.ram.ranges"
|
||||
value="${memories.instruction.ram.ranges}"/>
|
||||
<property key="memories.programmemory" value="true"/>
|
||||
<property key="memories.programmemory.ranges" value="0-ffff"/>
|
||||
<property key="poweroptions.powerenable" value="false"/>
|
||||
<property key="programoptions.eraseb4program" value="true"/>
|
||||
<property key="programoptions.preservedataflash" value="false"/>
|
||||
<property key="programoptions.preservedataflash.ranges"
|
||||
value="${memories.dataflash.default}"/>
|
||||
<property key="programoptions.preserveeeprom" value="false"/>
|
||||
<property key="programoptions.preserveeeprom.ranges" value="1400-15ff"/>
|
||||
<property key="programoptions.preserveprogram.ranges" value=""/>
|
||||
<property key="programoptions.preserveprogramrange" value="false"/>
|
||||
<property key="programoptions.preserveuserid" value="false"/>
|
||||
<property key="programoptions.programuserotp" value="false"/>
|
||||
<property key="toolpack.updateoptions"
|
||||
value="toolpack.updateoptions.uselatestoolpack"/>
|
||||
<property key="toolpack.updateoptions.packversion"
|
||||
value="Press to select which tool pack to use"/>
|
||||
<property key="voltagevalue" value=""/>
|
||||
</nEdbgTool>
|
||||
</conf>
|
||||
</confs>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* File: themistor-temp.h
|
||||
* Author: athamantis
|
||||
*
|
||||
* Created on 08 March 2024, 11:46
|
||||
*/
|
||||
|
||||
#ifndef THEMISTOR_TEMP_H
|
||||
#define THEMISTOR_TEMP_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <float.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <avr/io.h>
|
||||
#define R_T0 100E3
|
||||
#define T_0 298.15
|
||||
#define B 3950
|
||||
#define R_1 1000
|
||||
#define ledpin PIN3_bm
|
||||
|
||||
// Takes inn messured value
|
||||
#define ALERT_PIN PIN3_bm
|
||||
|
||||
// Takes inn messured value
|
||||
// Calculates the temperature in celcius
|
||||
// Returns the thermistor themperature
|
||||
float calculate_thermistor_temp(float readValue);
|
||||
|
||||
|
||||
// Returns true if temperatur is higher than max_temp
|
||||
bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp);
|
||||
|
||||
void alert_voltage_threshold_exceeded(bool voltage_threshold_bool);
|
||||
|
||||
// Initialise alarm GPIO
|
||||
void init_alarm_gpio();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* THEMISTOR_TEMP_H */
|
||||
|
||||
@ -1,52 +0,0 @@
|
||||
#include "themistor-temp.h"
|
||||
|
||||
|
||||
void init_alarm_gpio(){
|
||||
PORTB.DIRSET = ALERT_PIN;
|
||||
}
|
||||
|
||||
// The code is inspired by "Arduino thermistor guide" by valtentina Vogelman, 1 november 2023
|
||||
// https://www.build-electronic-circuits.com/arduino-thermistor/
|
||||
|
||||
float calculate_thermistor_temp(float thermistor_voltage){
|
||||
float R_thermistor;
|
||||
float V_1;
|
||||
float ln;
|
||||
float T_thermistor;
|
||||
float V_thermistor;
|
||||
#define V_TOT 3.3
|
||||
// Calculate Voltage over thermistor
|
||||
V_thermistor = (V_TOT/1024)*thermistor_voltage;
|
||||
|
||||
// Voltage accross R_1
|
||||
V_1 = V_TOT - V_thermistor;
|
||||
|
||||
// Calculate Thermistor resistanse
|
||||
R_thermistor = (V_thermistor)/ (V_1 / R_1);
|
||||
|
||||
// Steinhart-Harts formula
|
||||
ln = log(R_thermistor/R_T0);
|
||||
T_thermistor = (1/ ((ln/B) + (1/T_0)));
|
||||
|
||||
// Temperatur in celcius
|
||||
T_thermistor -= 273.15;
|
||||
|
||||
return T_thermistor;
|
||||
}
|
||||
|
||||
// returns error message if the messured thermistor temp is higher than
|
||||
// Choosen max_temp
|
||||
bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){
|
||||
// Return true if temp is higher then max value
|
||||
return (thermistor_temp >= 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");
|
||||
PORTB.OUTSET = ALERT_PIN;
|
||||
} else{
|
||||
PORTB.OUTCLR = ALERT_PIN;
|
||||
}
|
||||
}
|
||||
@ -1,52 +0,0 @@
|
||||
#include "themistor-temp.h"
|
||||
|
||||
|
||||
void init_led(){
|
||||
PORTB.DIRSET = ledpin;
|
||||
}
|
||||
|
||||
|
||||
https://www.build-electronic-circuits.com/arduino-thermistor/
|
||||
|
||||
float calculate_thermistor_temp(float thermistor_voltage){
|
||||
float R_thermistor;
|
||||
float V_1;
|
||||
float ln;
|
||||
float T_thermistor;
|
||||
float V_thermistor;
|
||||
#define V_TOT 5
|
||||
// Calculate Voltage over thermistor
|
||||
V_thermistor = (V_TOT/1024)*thermistor_voltage;
|
||||
|
||||
// Voltage accross R_1
|
||||
V_1 = V_TOT - V_thermistor;
|
||||
|
||||
// Calculate Thermistor resistanse
|
||||
R_thermistor = (V_thermistor)/ (V_1 / R_1);
|
||||
|
||||
// Steinhart-Harts formula
|
||||
ln = log(R_thermistor/R_T0);
|
||||
T_thermistor = (1/ ((ln/B) + (1/T_0)));
|
||||
|
||||
// Temperatur in celcius
|
||||
T_thermistor -= 273.15;
|
||||
|
||||
return T_thermistor;
|
||||
}
|
||||
|
||||
// returns error message if the messured thermistor temp is higher than
|
||||
// Choosen max_temp
|
||||
bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){
|
||||
// Return true if temp is higher then max value
|
||||
return (thermistor_temp >= 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");
|
||||
PORTB.OUTSET = ledpin;
|
||||
} else{
|
||||
PORTB.OUTCLR = ledpin;
|
||||
}
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
void init_uart(uint16_t baud) {
|
||||
// Configure UART pin directions
|
||||
PORTB.DIR &= ~PIN1_bm;
|
||||
PORTB.DIR |= PIN0_bm;
|
||||
// Set the baudrate
|
||||
USART3.BAUD = (uint16_t)USART3_BAUD_RATE(baud);
|
||||
// Enable UART TX & RX
|
||||
USART3.CTRLB |= USART_TXEN_bm;
|
||||
}
|
||||
|
||||
void USART3_sendChar(char c) {
|
||||
// Hold the code while the UART is not ready to send
|
||||
while (!(USART3.STATUS & USART_DREIF_bm)) { ; }
|
||||
// UART is ready, send the character.
|
||||
USART3.TXDATAL = c;
|
||||
|
||||
}
|
||||
|
||||
int USART3_printChar(char c, FILE *stream) {
|
||||
USART3_sendChar(c);
|
||||
return 0;
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* File: uart.h
|
||||
* Author: Sebastian H. Gabrielli
|
||||
*
|
||||
* Created on March 6, 2024, 3:19 PM
|
||||
*/
|
||||
|
||||
#ifndef UART_H
|
||||
#define UART_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef F_CPU
|
||||
#define F_CPU 4E6
|
||||
#endif
|
||||
|
||||
#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)
|
||||
|
||||
// Initialize the USART3 controller
|
||||
void init_uart(uint16_t baud);
|
||||
|
||||
// Send a single character over UART
|
||||
void USART3_sendChar(char c);
|
||||
|
||||
// Send a string of characters over UART
|
||||
int USART3_printChar(char c, FILE *stream);
|
||||
|
||||
static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* UART_H */
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
#include "voltage.h"
|
||||
#include "uart.h"
|
||||
|
||||
void ADC0_init(void) {
|
||||
/* Initializing ADC0 pin*/
|
||||
/*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;
|
||||
PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
|
||||
PORTD.PIN3CTRL &= PORT_PULLUPEN_bm;
|
||||
|
||||
ADC0.CTRLC = ADC_PRESC_DIV4_gc;
|
||||
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
|
||||
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
|
||||
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */
|
||||
|
||||
/* Select ADC channel */
|
||||
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
|
||||
}
|
||||
|
||||
uint16_t ADC0_read(void) {
|
||||
/* Start ADC conversion */
|
||||
ADC0.COMMAND = ADC_STCONV_bm;
|
||||
/* Wait until ADC conversion done */
|
||||
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
|
||||
;
|
||||
}
|
||||
// Clear the interrupt flag by writing 1:
|
||||
ADC0.INTFLAGS = ADC_RESRDY_bm;
|
||||
return ADC0.RES;
|
||||
}
|
||||
|
||||
uint16_t thermistor_voltage_read() {
|
||||
/* Gets value for the diode */
|
||||
ADC0.MUXPOS = 0x03; // Read PD3
|
||||
uint16_t adc_val = ADC0_read();
|
||||
|
||||
return adc_val;
|
||||
}
|
||||
// Gets the value over thermistor
|
||||
uint16_t external_voltage_read() {
|
||||
ADC0.MUXPOS = 0x02; // Read PD6
|
||||
uint16_t adc_val = ADC0_read();
|
||||
|
||||
return adc_val;
|
||||
}
|
||||
|
||||
uint16_t internal_voltage_read() {
|
||||
/* Gets value for the internal voltage reffreance*/
|
||||
|
||||
ADC0.MUXPOS = 0x44;
|
||||
uint8_t adc_val = ADC0_read();
|
||||
|
||||
return adc_val * 10;
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
/* Microchip Technology Inc. and its subsidiaries. You may use this software
|
||||
* and any derivatives exclusively with Microchip products.
|
||||
*
|
||||
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
|
||||
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
|
||||
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
|
||||
* PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
|
||||
* WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
|
||||
*
|
||||
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
|
||||
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
|
||||
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
|
||||
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
|
||||
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
|
||||
* IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
|
||||
* ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
|
||||
*
|
||||
* MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
|
||||
* TERMS.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: Voltage.h
|
||||
* Author: Sebastian Hernø Gabrielli, Helle Augland Grasmo, Ina Min Rørnes
|
||||
* Comments:
|
||||
* Revision history:
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// This is a guard condition so that contents of this file are not included
|
||||
// more than once.
|
||||
#ifndef XC_HEADER_TEMPLATE_H
|
||||
#define XC_HEADER_TEMPLATE_H
|
||||
|
||||
#include <xc.h> // include processor files - each processor file is guarded.
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
// Initializing of the ADC0 pins
|
||||
|
||||
void ADC0_init(void);
|
||||
//
|
||||
//void ADC0_start(void);
|
||||
//Start ADC conversion
|
||||
uint16_t ADC0_read(void);
|
||||
// Gets the value from sensor and internal voltage
|
||||
uint16_t internal_voltage_read();
|
||||
// Gets the value over thermistor
|
||||
uint16_t thermistor_voltage_read();
|
||||
// Gets internal voltage
|
||||
uint16_t external_voltage_read();
|
||||
// Converts value to voltage
|
||||
uint16_t convert_to_voltage(uint16_t adc_val);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* XC_HEADER_TEMPLATE_H */
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user