Merge I2C command handler into main #18
@ -3,15 +3,7 @@
|
|||||||
// The start address for the controller data
|
// The start address for the controller data
|
||||||
uint16_t EEMEM start_address_controller = 0x1400;
|
uint16_t EEMEM start_address_controller = 0x1400;
|
||||||
|
|
||||||
// Where the writing of the fans points start
|
// Checks if the EEPROM memory is ready to be written in and waits until it is.
|
||||||
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.
|
|
||||||
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(){
|
void check_eeprom_is_ready(){
|
||||||
while(1){
|
while(1){
|
||||||
if (eeprom_is_ready()){
|
if (eeprom_is_ready()){
|
||||||
@ -23,91 +15,28 @@ 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){
|
void write_struct_from_EEPROM(config_t write_struct){
|
||||||
|
// Calculate the required storage size
|
||||||
uint8_t struct_size = sizeof(write_struct);
|
uint8_t struct_size = sizeof(write_struct);
|
||||||
|
|
||||||
|
// Wait for the EEPROM to be ready
|
||||||
check_eeprom_is_ready();
|
check_eeprom_is_ready();
|
||||||
|
|
||||||
|
// Update the stored config stuct
|
||||||
eeprom_update_block((void*) &write_struct,(void*) &start_address_controller, struct_size);
|
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(){
|
config_t read_struct_from_EEPROM(){
|
||||||
//is eeprom ready??
|
// Create a config struct to hold the received data
|
||||||
config_t read_struct;
|
config_t read_struct = { 0 };
|
||||||
uint8_t struct_size = sizeof(read_struct);
|
uint8_t struct_size = sizeof(read_struct);
|
||||||
|
|
||||||
|
// Wait for the EEPROM to be ready
|
||||||
check_eeprom_is_ready();
|
check_eeprom_is_ready();
|
||||||
|
|
||||||
|
// Read the data from the EEPROM
|
||||||
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
|
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
|
||||||
|
|
||||||
|
// Return the data
|
||||||
return read_struct;
|
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_write_byte(0x30, 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* File: eeprom.h
|
* File: eeprom.h
|
||||||
* Author: Helle Augland Grasmo
|
* Author: Helle Augland Grasmo, Sebastian H. Gabrielli
|
||||||
*
|
*
|
||||||
* Created on 06 March 2024, 15:30
|
* Created on 06 March 2024, 15:30
|
||||||
*/
|
*/
|
||||||
@ -19,30 +19,20 @@ extern "C" {
|
|||||||
#include <avr/eeprom.h>
|
#include <avr/eeprom.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Struct for information on the controller.
|
// Struct for information on the controller.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t ms_fanspeed_sample_rate;
|
uint16_t ms_fanspeed_sample_rate;
|
||||||
} config_t;
|
} config_t;
|
||||||
|
|
||||||
// Check if EEPROM is ready to be written in
|
// Check if EEPROM is ready to be written to
|
||||||
void check_eeprom_is_ready();
|
void check_eeprom_is_ready();
|
||||||
|
|
||||||
// Writes a struct in EEPROM
|
// Writes a config_t struct to the EEPROM
|
||||||
void write_struct_from_EEPROM(config_t write_struct);
|
void write_struct_from_EEPROM(config_t write_struct);
|
||||||
|
|
||||||
// Read data from EEPROM and return it as a controller struct
|
// Read data from EEPROM and return it as a config_t struct
|
||||||
config_t read_struct_from_EEPROM();
|
config_t read_struct_from_EEPROM();
|
||||||
|
|
||||||
// Writes a datapoint in EEPROM
|
|
||||||
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ extern "C" {
|
|||||||
#include <util/twi.h>
|
#include <util/twi.h>
|
||||||
|
|
||||||
// Received data buffer size
|
// Received data buffer size
|
||||||
// The size is larger than any expected command lenght
|
// The size is larger than any expected command length
|
||||||
#define I2C_RECV_BUF_SIZE 16
|
#define I2C_RECV_BUF_SIZE 16
|
||||||
|
|
||||||
// Reset recv to initial state
|
// Reset recv to initial state
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user