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

106 lines
3.0 KiB
C

#include "eeprom.h"
// The start address for the controller data
uint8_t EEMEM startAddressController = 0x00;
// Where the writing of the fans points start
uint8_t EEMEM startAddressFan1 = 0x30;
uint8_t EEMEM startAddressFan2 = 0x60;
// The placement for the next datapoint form the fans.
uint8_t EEMEM currentAddressFan1 = 0x30;
uint8_t EEMEM currentAddressFan2 = 0x60;
// Checks if its written a struct in the memmory space.
bool alreadyWrittenABlock = false;
// Checks if the EEPROM memory is ready to be written in.
void checkEEpromIsReady(){
while(1){
if (eeprom_is_ready()){
break;
printf("it is");
}else{
;
printf("its not");
}
}
}
// 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 WriteStructInEEPROM(config_t writeStruct){
uint8_t structSize;
structSize = sizeof(writeStruct);
checkEEpromIsReady();
if (alreadyWrittenABlock){
eeprom_update_block((void*) &writeStruct,(void*) &startAddressController, structSize);
}else{
eeprom_write_block((void*)&writeStruct,(void*) &startAddressController, structSize);
alreadyWrittenABlock = true;
}
}
// Reads the memory block at the address 0x00
// returns a struct in form of config_t
config_t ReadStructInEEPROM(){
//is eeprom ready??
config_t readStruct;
uint8_t structsize = sizeof(readStruct);
checkEEpromIsReady();
eeprom_read_block((void *) &readStruct,(void*) &startAddressController, structsize);
return readStruct;
}
// 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.
void WriteDataPointInEEPROM(uint8_t dataPoint, uint8_t data){
checkEEpromIsReady();
if (data == 1){
eeprom_write_byte(currentAddressFan1, dataPoint);
currentAddressFan1++;
} else if (data == 2){
eeprom_write_byte(currentAddressFan2, dataPoint);
currentAddressFan2++;
} else{
// error???
;
}
}
// Reads all the datapoints to the choosen data.
// it writes the data points in the USART stream.
void ReadDataPointSpeedInfo(uint8_t data){
uint8_t dataPoint = 0;
if (data == 1){
uint8_t len = currentAddressFan1 - startAddressFan1;
checkEEpromIsReady();
for (uint8_t i = 0; i <len; i++){
dataPoint = eeprom_read_byte(i);
printf("Fanspeed nr %u : %u", i, dataPoint);
}
} else if (data == 2){
uint8_t len = currentAddressFan2 - startAddressFan2;
checkEEpromIsReady();
for (uint8_t i = 0; i <len; i++){
dataPoint = eeprom_read_byte(i);
printf("Fanspeed nr %u : %u", i, dataPoint);
}
}
}