mikrokontrollersystemer-pro.../prosjekt.X/eeprom.c

100 lines
2.9 KiB
C
Raw Normal View History

2024-03-06 14:49:48 +00:00
#include "eeprom.h"
2024-03-06 19:05:52 +00:00
// The start address for the controller data
2024-03-13 10:48:58 +00:00
uint8_t EEMEM start_address_controller = 0x00;
2024-03-06 14:49:48 +00:00
2024-03-06 19:05:52 +00:00
// Where the writing of the fans points start
2024-03-13 10:48:58 +00:00
uint8_t EEMEM start_address_fan1 = 0x30;
uint8_t EEMEM start_address_fan2 = 0x60;
2024-03-06 14:49:48 +00:00
2024-03-06 19:05:52 +00:00
// The placement for the next datapoint form the fans.
2024-03-13 10:48:58 +00:00
uint8_t EEMEM current_address_fan1 = 0x30;
uint8_t EEMEM current_address_fan2 = 0x60;
2024-03-06 14:49:48 +00:00
2024-03-06 19:05:52 +00:00
// Checks if the EEPROM memory is ready to be written in.
2024-03-13 10:48:58 +00:00
void check_eeprom_is_ready(){
2024-03-06 14:49:48 +00:00
while(1){
if (eeprom_is_ready()){
break;
}else{
;
}
}
}
2024-03-06 19:05:52 +00:00
// 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.
2024-03-13 11:03:40 +00:00
void write_struct_from_EEPROM(config_t write_struct){
2024-03-20 09:53:46 +00:00
uint8_t struct_size = sizeof(write_struct);
2024-03-06 14:49:48 +00:00
2024-03-13 10:48:58 +00:00
check_eeprom_is_ready();
2024-03-06 14:49:48 +00:00
2024-03-13 10:48:58 +00:00
eeprom_update_block((void*) &write_struct,(void*) &start_address_controller, struct_size);
2024-03-13 09:34:46 +00:00
2024-03-06 14:49:48 +00:00
}
2024-03-13 11:03:40 +00:00
// Reads the memory block at the address start_address_controller
2024-03-06 19:05:52 +00:00
// returns a struct in form of config_t
2024-03-13 10:48:58 +00:00
config_t read_struct_from_EEPROM(){
2024-03-06 14:49:48 +00:00
//is eeprom ready??
2024-03-13 10:48:58 +00:00
config_t read_struct;
uint8_t struct_size = sizeof(read_struct);
2024-03-06 14:49:48 +00:00
2024-03-13 10:48:58 +00:00
check_eeprom_is_ready();
2024-03-06 14:49:48 +00:00
2024-03-13 10:48:58 +00:00
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
return read_struct;
2024-03-06 14:49:48 +00:00
}
2024-03-06 19:05:52 +00:00
// 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.
2024-03-20 09:52:38 +00:00
int write_data_point_in_EEPROM(uint8_t byte, uint8_t fan_num){
2024-03-06 14:49:48 +00:00
2024-03-13 10:48:58 +00:00
check_eeprom_is_ready();
2024-03-13 11:03:40 +00:00
if (fan_num == 1){
eeprom_update_byte(current_address_fan1, byte);
2024-03-13 10:48:58 +00:00
current_address_fan1++;
2024-03-20 09:52:38 +00:00
return 1;
2024-03-13 11:03:40 +00:00
} else if (fan_num == 2){
eeprom_update_byte(current_address_fan2, byte);
2024-03-13 10:48:58 +00:00
current_address_fan2++;
2024-03-20 09:52:38 +00:00
return 1;
2024-03-06 19:05:52 +00:00
} else{
2024-03-20 09:52:38 +00:00
return 0;
2024-03-06 19:05:52 +00:00
}
2024-03-06 14:49:48 +00:00
}
2024-03-06 19:05:52 +00:00
// Reads all the datapoints to the choosen data.
// it writes the data points in the USART stream.
2024-03-19 13:02:10 +00:00
uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array){
2024-03-13 11:03:40 +00:00
uint8_t byte = 0;
2024-03-06 14:49:48 +00:00
2024-03-13 11:03:40 +00:00
if (fan_num == 1){
2024-03-13 10:48:58 +00:00
uint8_t len = current_address_fan1 - start_address_fan1;
2024-03-06 19:05:52 +00:00
2024-03-13 10:48:58 +00:00
check_eeprom_is_ready();
2024-03-06 19:05:52 +00:00
for (uint8_t i = 0; i <len; i++){
2024-03-19 13:02:10 +00:00
byte = eeprom_read_byte(i + start_address_fan1);
array[i] = byte;
2024-03-13 11:03:40 +00:00
printf("Fanspeed nr %u : %u", i, byte);
2024-03-06 19:05:52 +00:00
}
2024-03-13 11:03:40 +00:00
} else if (fan_num == 2){
2024-03-13 10:48:58 +00:00
uint8_t len = current_address_fan2 - start_address_fan2;
check_eeprom_is_ready();
2024-03-06 19:05:52 +00:00
for (uint8_t i = 0; i <len; i++){
2024-03-19 13:02:10 +00:00
byte = eeprom_read_byte(i + start_address_fan2);
array[i] = byte;
2024-03-13 11:03:40 +00:00
printf("Fanspeed nr %u : %u", i, byte);
2024-03-06 19:05:52 +00:00
}
2024-03-06 14:49:48 +00:00
}
2024-03-19 13:02:10 +00:00
return sizeof(array);
2024-03-06 14:49:48 +00:00
}