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

42 lines
1.1 KiB
C
Raw Permalink 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-04-23 13:31:32 +00:00
uint16_t EEMEM start_address_controller = 0x1400;
2024-03-06 14:49:48 +00:00
// Checks if the EEPROM memory is ready to be written in and waits until it is.
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
2024-03-13 11:03:40 +00:00
void write_struct_from_EEPROM(config_t write_struct){
// Calculate the required storage size
2024-03-20 09:53:46 +00:00
uint8_t struct_size = sizeof(write_struct);
2024-03-06 14:49:48 +00:00
// Wait for the EEPROM to be ready
2024-03-13 10:48:58 +00:00
check_eeprom_is_ready();
2024-03-06 14:49:48 +00:00
// Update the stored config stuct
2024-03-13 10:48:58 +00:00
eeprom_update_block((void*) &write_struct,(void*) &start_address_controller, struct_size);
2024-03-06 14:49:48 +00:00
}
2024-03-13 10:48:58 +00:00
config_t read_struct_from_EEPROM(){
// Create a config struct to hold the received data
config_t read_struct = { 0 };
2024-03-13 10:48:58 +00:00
uint8_t struct_size = sizeof(read_struct);
2024-03-06 14:49:48 +00:00
// Wait for the EEPROM to be ready
2024-03-13 10:48:58 +00:00
check_eeprom_is_ready();
2024-03-06 14:49:48 +00:00
// Read the data from the EEPROM
2024-03-13 10:48:58 +00:00
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
2024-04-23 13:31:32 +00:00
// Return the data
return read_struct;
}