diff --git a/.gitignore b/.gitignore index d14efc5..f31b37b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,6 @@ # Large Files *.exe *.zip -*.pdf \ No newline at end of file +*.pdf +/prosjekt.X/build/ +/prosjekt.X/dist/ \ No newline at end of file diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 5f964b3..9e43e92 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -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 #include #include +#include +#define F_CPU 4E6 +#include +#include +#include +#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 *(float) BAUD_RATE)) + 0.5) +#include -/* - * - */ -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