Merge branch 'eeprom' into main

This commit is contained in:
Elp03 2024-04-09 12:33:46 +02:00
commit 81ebcd2c79
6 changed files with 160 additions and 2 deletions

View File

Binary file not shown.

99
prosjekt.X/eeprom.c Normal file
View File

@ -0,0 +1,99 @@
#include "eeprom.h"
// The start address for the controller data
uint8_t EEMEM start_address_controller = 0x00;
// Where the writing of the fans points start
uint8_t EEMEM start_address_fan1 = 0x30;
uint8_t EEMEM start_address_fan2 = 0x60;
// The placement for the next datapoint form the fans.
uint8_t EEMEM current_address_fan1 = 0x30;
uint8_t EEMEM current_address_fan2 = 0x60;
// Checks if the EEPROM memory is ready to be written in.
void check_eeprom_is_ready(){
while(1){
if (eeprom_is_ready()){
break;
}else{
;
}
}
}
// 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){
uint8_t struct_size = sizeof(write_struct);
check_eeprom_is_ready();
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(){
//is eeprom ready??
config_t read_struct;
uint8_t struct_size = sizeof(read_struct);
check_eeprom_is_ready();
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
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_update_byte(current_address_fan1, 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);
}

50
prosjekt.X/eeprom.h Normal file
View File

@ -0,0 +1,50 @@
/*
* File: eeprom.h
* Author: Helle Augland Grasmo
*
* Created on 06 March 2024, 15:30
*/
#ifndef EEPROM_H
#define EEPROM_H
#ifdef __cplusplus
extern "C" {
#endif
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <avr/eeprom.h>
#include <stdbool.h>
// Struct for information on the controller.
typedef struct {
uint8_t fanSpeed;
} config_t;
// Check if EEPROM is ready to be written in
void check_eeprom_is_ready();
// Writes a struct in EEPROM
void write_struct_from_EEPROM(config_t write_struct);
// Read data from EEPROM and return it as a controller 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);
// Reads all the dataPoints form EEPROM
uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array);
#ifdef __cplusplus
}
#endif
#endif /* EEPROM_H */

View File

@ -1,14 +1,15 @@
/* /*
* File: main.c * File: main.c
* Author: Sebastian H. Gabrielli * Author: Sebastian H. Gabrielli, Helle Augland Grasmo
* *
* Created on March 6, 2024, 12:34 PM * Created on March 6, 2024, 12:34 PM
*/ */
#define F_CPU 4E6
#include "eeprom.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define F_CPU 4E6
#include "uart.h" #include "uart.h"
#include <util/delay.h> #include <util/delay.h>

View File

@ -5,6 +5,7 @@
displayName="Header Files" displayName="Header Files"
projectFiles="true"> projectFiles="true">
<itemPath>uart.h</itemPath> <itemPath>uart.h</itemPath>
<itemPath>eeprom.h</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="LinkerScript" <logicalFolder name="LinkerScript"
displayName="Linker Files" displayName="Linker Files"
@ -15,6 +16,7 @@
projectFiles="true"> projectFiles="true">
<itemPath>main.c</itemPath> <itemPath>main.c</itemPath>
<itemPath>uart.c</itemPath> <itemPath>uart.c</itemPath>
<itemPath>eeprom.c</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="ExternalFiles" <logicalFolder name="ExternalFiles"
displayName="Important Files" displayName="Important Files"
@ -145,6 +147,9 @@
<property key="program-the-device-with-default-config-words" value="false"/> <property key="program-the-device-with-default-config-words" value="false"/>
<property key="remove-unused-sections" value="true"/> <property key="remove-unused-sections" value="true"/>
</HI-TECH-LINK> </HI-TECH-LINK>
<Tool>
<property key="debugoptions.useswbreakpoints" value="true"/>
</Tool>
<XC8-CO> <XC8-CO>
<property key="coverage-enable" value=""/> <property key="coverage-enable" value=""/>
<property key="stack-guidance" value="false"/> <property key="stack-guidance" value="false"/>
@ -167,6 +172,9 @@
<property key="user-pack-device-support" value=""/> <property key="user-pack-device-support" value=""/>
<property key="wpo-lto" value="false"/> <property key="wpo-lto" value="false"/>
</XC8-config-global> </XC8-config-global>
<nEdbgTool>
<property key="debugoptions.useswbreakpoints" value="true"/>
</nEdbgTool>
</conf> </conf>
</confs> </confs>
</configurationDescriptor> </configurationDescriptor>