From bf3753a012bec032bad77f44f3680432f702c0ce Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 23 Apr 2024 15:31:32 +0200 Subject: [PATCH] I2C bulk fan read works --- prosjekt.X/command-handler.c | 48 +++++++++++++------------ prosjekt.X/command-handler.h | 11 ++++-- prosjekt.X/eeprom.c | 26 ++++++++++---- prosjekt.X/eeprom.h | 4 ++- prosjekt.X/main.c | 14 ++++---- prosjekt.X/nbproject/configurations.xml | 2 ++ 6 files changed, 66 insertions(+), 39 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index e9ade45..ff9071a 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -38,7 +38,7 @@ void parse_command(uint8_t command[], uint8_t command_len) { context.command = WRITE_CONFIG; break; case 0x22: // Clear stored fan speed data - context.command = CLEAR_BULK_FAN_SPEED; + context.command = READ_BULK_FAN_SPEED; break; default: // Unrecognized command context.command = UNKNOWN_COMMAND; @@ -132,27 +132,29 @@ void parse_command(uint8_t command[], uint8_t command_len) { } uint8_t route_command(int pos) { - switch (context.command) { - WRITE_CONFIG: - return WRITE_CONFIG; - break; - READ_CONFIG: - return READ_CONFIG; - break; - READ_VOLTAGE: - return READ_VOLTAGE; - break; - READ_TERMPERATURE: - return READ_TERMPERATURE; - break; - READ_FAN_SPEED: - return READ_FAN_SPEED; - break; - CLEAR_BULK_FAN_SPEED: - return CLEAR_BULK_FAN_SPEED; - break; - UNKNOWN_COMMAND: - return 0xFF; - break; + switch (context.command) { + case WRITE_CONFIG: + return WRITE_CONFIG; + case READ_CONFIG: + return READ_CONFIG; + case READ_VOLTAGE: + return READ_VOLTAGE; + case READ_TERMPERATURE: + return READ_TERMPERATURE; + case READ_FAN_SPEED: + return READ_FAN_SPEED; + case READ_BULK_FAN_SPEED: + { + if (context.fan == FAN1) { + return fan1_history[pos]; + } else if (context.fan == FAN2) { + return fan2_history[pos]; + } else { + return 0; + } + } + case UNKNOWN_COMMAND: + default: + return 0xFF; } } diff --git a/prosjekt.X/command-handler.h b/prosjekt.X/command-handler.h index e09afda..3956d07 100644 --- a/prosjekt.X/command-handler.h +++ b/prosjekt.X/command-handler.h @@ -12,10 +12,11 @@ extern "C" { #endif +#include "eeprom.h" #include #include #include - + // Enum of all valid command types typedef enum { WRITE_CONFIG, // Change the configuration @@ -45,8 +46,8 @@ typedef enum { // Enum of all valid fans // TODO: Consider moving into it's own fan page typedef enum { - FAN1, // The first fan - FAN2, // The second fan + FAN1 = 1, // The first fan + FAN2 = 2, // The second fan FAN_NONE // No fan } fans_t; @@ -59,6 +60,10 @@ typedef struct { // TODO: Add config value field for writing } command_context_t; +// Fan history variables +extern volatile uint16_t fan1_history[512]; +extern volatile uint16_t fan2_history[512]; + // Parses the input string and outputs one of the valid commands void parse_command(uint8_t *command, uint8_t command_len); diff --git a/prosjekt.X/eeprom.c b/prosjekt.X/eeprom.c index 5dd4616..2463a0b 100644 --- a/prosjekt.X/eeprom.c +++ b/prosjekt.X/eeprom.c @@ -1,15 +1,15 @@ #include "eeprom.h" // The start address for the controller data -uint8_t EEMEM start_address_controller = 0x00; +uint16_t EEMEM start_address_controller = 0x1400; // Where the writing of the fans points start -uint8_t EEMEM start_address_fan1 = 0x30; -uint8_t EEMEM start_address_fan2 = 0x60; +uint16_t EEMEM start_address_fan1 = 0x1400 + 0x30; +uint16_t EEMEM start_address_fan2 = 0x1400 + 0x60; // The placement for the next datapoint form the fans. -uint8_t EEMEM current_address_fan1 = 0x30; -uint8_t EEMEM current_address_fan2 = 0x60; +uint16_t EEMEM current_address_fan1 = 0x1400 + 0x30; +uint16_t EEMEM current_address_fan2 = 0x1400 + 0x60; // Checks if the EEPROM memory is ready to be written in. void check_eeprom_is_ready(){ @@ -58,7 +58,7 @@ 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); + eeprom_write_byte(0x30, byte); current_address_fan1++; return 1; } else if (fan_num == 2){ @@ -97,3 +97,17 @@ uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array){ } return sizeof(array); } + +// Reads all the datapoints to the choosen data. +// it writes the data points in the USART stream. +uint8_t read_single_data_point_speed_info(uint8_t fan_num, uint8_t pos){ + uint8_t byte = 0; + + if (fan_num == 1){ + check_eeprom_is_ready(); + return eeprom_read_byte(0x30 + pos); + } else if (fan_num == 2){ + check_eeprom_is_ready(); + return eeprom_read_byte(start_address_fan2 + pos); + } +} diff --git a/prosjekt.X/eeprom.h b/prosjekt.X/eeprom.h index b584713..098074b 100644 --- a/prosjekt.X/eeprom.h +++ b/prosjekt.X/eeprom.h @@ -36,11 +36,13 @@ void write_struct_from_EEPROM(config_t write_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); +int write_data_point_in_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); +uint8_t read_single_data_point_speed_info(uint8_t fan_num, uint8_t pos); + #ifdef __cplusplus } diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 428b4ff..2370b1c 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,24 +4,28 @@ * * Created on March 6, 2024, 12:34 PM */ +#include #include "uart.h" #include "voltage.h" #define RTC_PERIOD (511) #define DELAY_TIME 1000 #include "eeprom.h" #include -#include #include #include #include #define F_CPU 4E6 #include "command-handler.h" #include "i2c.h" +#include "themistor-temp.h" #include "uart.h" #include -#include #include -#include "themistor-temp.h" +#include + +// Fan history variables +volatile uint16_t fan1_history[512] = {1, 2, 3, 4}; +volatile uint16_t fan2_history[512] = {2, 3, 4, 5}; int main() { init_uart((uint16_t)9600); @@ -29,12 +33,10 @@ int main() { stdout = &USART_stream; PORTB.DIRSET = PIN3_bm; - + sei(); while (1) { - // uint16_t adcVal = ADC0_read(); - // printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); ; } } diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 9e4c7b2..12d6430 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -27,6 +27,8 @@ uart.c eeprom.c voltage.c + i2c.c + command-handler.c Makefile