add eeprom code, not work
This commit is contained in:
parent
39c91a18dd
commit
8b73051ece
2
.gitignore
vendored
2
.gitignore
vendored
@ -27,3 +27,5 @@
|
||||
*.exe
|
||||
*.zip
|
||||
*.pdf
|
||||
/prosjekt.X/build/
|
||||
/prosjekt.X/dist/
|
||||
@ -4,15 +4,163 @@
|
||||
*
|
||||
* Created on March 6, 2024, 12:34 PM
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: main.c
|
||||
* Author: athamantis
|
||||
*
|
||||
* Created on 01 March 2024, 10:34
|
||||
*/
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#define F_CPU 4E6
|
||||
#include <util/delay.h>
|
||||
#include <avr/eeprom.h>
|
||||
#include <stdbool.h>
|
||||
#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 *(float) BAUD_RATE)) + 0.5)
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
int main(int argc, char** argv) {
|
||||
// Init UART comunication
|
||||
void USART3_init(void) {
|
||||
// Enable resiver pin
|
||||
PORTB.DIR &= ~PIN1_bm;
|
||||
// Enable sender pin
|
||||
PORTB.DIR |= PIN0_bm;
|
||||
USART3.BAUD = (uint16_t) USART3_BAUD_RATE(115200);
|
||||
USART3.CTRLB |= USART_TXEN_bm;
|
||||
|
||||
return (EXIT_SUCCESS);
|
||||
// Enabled in the USART register
|
||||
USART3.CTRLB |= USART_RXEN_bm;
|
||||
}
|
||||
|
||||
// Use printline __________________________
|
||||
static void USART3_sendChar(char c) {
|
||||
while (!(USART3.STATUS & USART_DREIF_bm)) {
|
||||
;
|
||||
}
|
||||
USART3.TXDATAL = c;
|
||||
}
|
||||
|
||||
static int USART3_printChar(char c, FILE *stream) {
|
||||
USART3_sendChar(c);
|
||||
return 0;
|
||||
}
|
||||
static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
uint8_t USART3_read() {
|
||||
while (!(USART3.STATUS & USART_RXCIF_bm)) {
|
||||
;
|
||||
}
|
||||
return USART3.RXDATAL;
|
||||
}
|
||||
|
||||
/*
|
||||
* make a struct
|
||||
* a space is reserved for that struct
|
||||
* make a function that write the struct to the space
|
||||
*
|
||||
* a running counter for where the fanspeed data should be set
|
||||
* make a function that write the mesured fanSpeed
|
||||
*
|
||||
* read the memory for the struct
|
||||
* read the memory for the fanspeed
|
||||
*/
|
||||
|
||||
uint8_t fanControllerStartAddress = 0x00;
|
||||
bool alreadyWrittenAblock = false;//false
|
||||
|
||||
uint8_t fanSpeedStartAddress = 0x30;
|
||||
uint8_t currentFanSpeedAddress = 0x30;
|
||||
|
||||
typedef struct {
|
||||
uint8_t fanSpeed;
|
||||
} config_t;
|
||||
|
||||
void checkEEpromIsReady(){
|
||||
printf("checking eeprom");
|
||||
if (eeprom_is_ready()){
|
||||
printf("it is");
|
||||
}else{
|
||||
printf("its not");
|
||||
}
|
||||
}
|
||||
|
||||
void WriteStructInEEPROM(config_t writeStruct){
|
||||
uint8_t structSize;
|
||||
structSize = sizeof(writeStruct);
|
||||
|
||||
//check if eeprom_is_ready()
|
||||
checkEEpromIsReady();
|
||||
|
||||
if (alreadyWrittenAblock){
|
||||
eeprom_update_block((void*) &writeStruct,(void*) &fanControllerStartAddress, structSize);
|
||||
}else{
|
||||
eeprom_write_block((config_t*) &writeStruct, &fanControllerStartAddress, structSize);
|
||||
alreadyWrittenAblock = true;//true
|
||||
//reurn something
|
||||
}
|
||||
}
|
||||
|
||||
config_t ReadStructInEEPROM(){
|
||||
//is eeprom ready??
|
||||
config_t readStruct;
|
||||
uint8_t structsize = sizeof(readStruct);
|
||||
|
||||
//checkEEpromIsReady();
|
||||
|
||||
eeprom_read_block((void *) &readStruct,(void*) &fanControllerStartAddress, structsize);
|
||||
return readStruct;
|
||||
}
|
||||
|
||||
void WriteFanSpeedInEEPROM(uint8_t fanSpeed){
|
||||
|
||||
//checkEEpromIsReady();
|
||||
|
||||
eeprom_write_byte(currentFanSpeedAddress, fanSpeed);
|
||||
currentFanSpeedAddress++;
|
||||
}
|
||||
|
||||
void ReadFanSpeedInfo(){
|
||||
//read the space as an array or??
|
||||
//eeprom_read_block();
|
||||
//this write it too or?
|
||||
uint8_t len = currentFanSpeedAddress - fanSpeedStartAddress;
|
||||
uint8_t fanSpeedIterate = 0;
|
||||
|
||||
//checkEEpromIsReady();
|
||||
|
||||
for (uint8_t i = 0; i <len; i++){
|
||||
fanSpeedIterate = eeprom_read_byte(i);
|
||||
printf("Fanspeed nr %u : %u", i, fanSpeedIterate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void main() {
|
||||
USART3_init();
|
||||
stdout = &USART_stream;
|
||||
|
||||
config_t fanController;
|
||||
|
||||
fanController.fanSpeed = 8;
|
||||
|
||||
while(1){
|
||||
printf("in loop\n");
|
||||
printf("Write the struct \n");
|
||||
WriteStructInEEPROM(fanController);
|
||||
|
||||
printf("Read struct\n");
|
||||
config_t recivedFancontroller = ReadStructInEEPROM();
|
||||
|
||||
printf("writing in eeprom\n");
|
||||
WriteFanSpeedInEEPROM(2);
|
||||
WriteFanSpeedInEEPROM(3);
|
||||
WriteFanSpeedInEEPROM(5);
|
||||
|
||||
printf("reading from eeprom\n");
|
||||
ReadFanSpeedInfo();
|
||||
_delay_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user