diff --git a/Core/Inc/EEPROM.h b/Core/Inc/EEPROM.h index a345c8d..b4c0a30 100644 --- a/Core/Inc/EEPROM.h +++ b/Core/Inc/EEPROM.h @@ -1,52 +1,79 @@ #include "stm32f1xx_hal.h" -#define AT24C_ADRESS 0x50 // i2c slave adress EEPROM +#ifndef INC_EEPROM_H_ +#define INC_EEPROM_H_ -#define FLASH_BLOCKSIZE 64 -#define START_ADR_SHOT 0x0000 //00000 -#define START_ADR_PROGRAM 0x3000 //12288 -#define START_ADR_MACRO 0x6000 //24576 +#define AT24C_ADRESS 0x50 // i2c slave adress EEPROM -#define MAX_NUMBER_MACRO 100 -#define MAX_NUMBER_SHOTS 160 -#define MAX_NUMBER_PROGRAMS 100 -#define MAX_NUMBER_SHOTS_IN_PROGRAMS 31 -#define MAX_NUMBER_PROGRAMS_IN_MACRO 16 +#define START_ADR_STAT 0x0000 //00000 +#define STAT_BLOCKSIZE 20 -typedef struct MacroProgram{ - unsigned char id; - unsigned char speedScrew; - unsigned char countRepeat; - unsigned char options; - }MacroProgram; - -typedef struct ProgramShot{ - unsigned char id; - unsigned char speedScrew; - }ProgramShot; +#define START_ADR_SHOT (START_ADR_STAT+STAT_BLOCKSIZE) // 0x0014 = 20 +#define SHOT_BLOCKSIZE 10 +#define MAX_NUMBER_SHOTS 255 + +#define START_ADR_PROGRAM (START_ADR_SHOT + (SHOT_BLOCKSIZE*MAX_NUMBER_SHOTS)) // 0x0A0A = 2570 +#define PROGRAM_BLOCKSIZE 203 +#define MAX_NUMBER_PROGRAMS 100 +#define MAX_NUMBER_SHOTS_IN_PROGRAMS \ + (PROGRAM_BLOCKSIZE-sizeof(ProgramHeader)) /sizeof(ProgramShot) // 100 + +#define START_ADR_MACRO (START_ADR_PROGRAM + (PROGRAM_BLOCKSIZE*MAX_NUMBER_PROGRAMS)) // 0x5956 = 22870 +#define MACRO_BLOCKSIZE 81 +#define MAX_NUMBER_MACRO 100 +#define MAX_NUMBER_PROGRAMS_IN_MACRO \ + (MACRO_BLOCKSIZE-sizeof(MacroHeader)) /sizeof(MacroProgram) // 20 + +#define MEMORY_END (START_ADR_MACRO + (MACRO_BLOCKSIZE*MAX_NUMBER_MACRO)) // 0x78FA = 30970 typedef struct Shot { + unsigned char isExist; unsigned char countRepeatShot; - unsigned char speedRollerTop; - unsigned char speedRollerBottom; - unsigned char speedScrew; - unsigned char rotationAxial; - unsigned char rotationHorizontal; - unsigned char rotationVertical; + unsigned char speedRollerTop; // 0 .. 100 .. 200 + unsigned char speedRollerBottom; // 0 .. 100 .. 200 + unsigned char speedScrew; // 0 .. 100 + unsigned char rotationAxial; // 0 .. 90 .. 180 + unsigned char rotationHorizontal; // 0 .. 90 .. 180 + unsigned char rotationVertical; // 0 .. 90 .. 180 }Shot; + +typedef struct ProgramHeader { + unsigned char isExist; + unsigned char countRepeat; + unsigned char options; + }ProgramHeader; +typedef struct ProgramShot { + unsigned char id; + unsigned char speedScrew; + }ProgramShot; + typedef struct Program { - unsigned char countRepeat; - unsigned char options; - ProgramShot shots[MAX_NUMBER_SHOTS_IN_PROGRAMS]; - }Program; + ProgramHeader header; + ProgramShot shots[MAX_NUMBER_SHOTS_IN_PROGRAMS]; + }Program; + +typedef struct MacroHeader { + unsigned char isExist; + }MacroHeader; + +typedef struct MacroProgram { + unsigned char id; + unsigned char speedScrew; + unsigned char countRepeat; + unsigned char options; +}MacroProgram; typedef struct Macro { + MacroHeader header; MacroProgram programs[MAX_NUMBER_PROGRAMS_IN_MACRO]; } Macro; -void SaveShot(unsigned char number, struct Shot* shot); -void FLASH_WriteBlock(uint8_t *writeAddr, unsigned char *flashWrBufPtr); +void FLASH_WriteBlock(uint16_t startAddr, uint8_t number,uint8_t *writeData); +void FLASH_ReadBlock(uint16_t startAddr, uint8_t number, uint8_t *outData); + +void SaveShot(unsigned char number, struct Shot* shot); Shot GetShot( unsigned char number ); +#endif /* INC_EEPROM_H_ */ diff --git a/Core/Src/EEPROM.c b/Core/Src/EEPROM.c index e97f415..e1a0d40 100644 --- a/Core/Src/EEPROM.c +++ b/Core/Src/EEPROM.c @@ -5,13 +5,10 @@ void SaveShot(unsigned char number, struct Shot* shot) { - uint16_t blockAddr16 = (uint16_t)(START_ADR_SHOT + (uint16_t)(number*FLASH_BLOCKSIZE)); - - uint8_t blockAddr[2] = {HIBYTE(blockAddr16), LOBYTE(blockAddr16)}; - - unsigned char Buf[FLASH_BLOCKSIZE]; - - for( unsigned char i = 0; i < FLASH_BLOCKSIZE; i++ ) Buf[i] = 0; + print("SaveShot "); + printNumber(number); + unsigned char Buf[SHOT_BLOCKSIZE]; + memset(Buf, 0x00, sizeof(Buf)); Buf[0] = number; Buf[1] = shot->countRepeatShot; @@ -21,26 +18,17 @@ void SaveShot(unsigned char number, struct Shot* shot) Buf[5] = shot->rotationAxial; Buf[6] = shot->rotationHorizontal; Buf[7] = shot->rotationVertical; - - // Writes buffer contents to current block - FLASH_WriteBlock(blockAddr, Buf); + + FLASH_WriteBlock(START_ADR_SHOT, number, Buf); } -struct Shot GetShot( unsigned char number ) +struct Shot GetShot( unsigned char number) { struct Shot shot; - uint16_t blockAddr16 = (uint16_t)(START_ADR_SHOT + (uint16_t)(number*FLASH_BLOCKSIZE)); - uint8_t blockAddr[2] = {HIBYTE(blockAddr16), LOBYTE(blockAddr16)}; - - unsigned char Buf[FLASH_BLOCKSIZE]; - - - HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), blockAddr, 2, 10); - HAL_Delay(1); - HAL_I2C_Master_Receive(&hi2c1, (AT24C_ADRESS << 1), Buf, FLASH_BLOCKSIZE, 10); - HAL_Delay(1); - + unsigned char Buf[SHOT_BLOCKSIZE]; + FLASH_ReadBlock(START_ADR_SHOT, number, Buf); + shot.isExist = Buf[0]; shot.countRepeatShot = Buf[1]; shot.speedRollerTop = Buf[2]; shot.speedRollerBottom = Buf[3]; @@ -52,15 +40,84 @@ struct Shot GetShot( unsigned char number ) return shot; } - -void FLASH_WriteBlock(uint8_t *writeAddr, unsigned char *flashWrBufPtr) +void FLASH_WriteBlock(uint16_t startAddr, uint8_t number, uint8_t *writeData) { - unsigned char Buf[FLASH_BLOCKSIZE+2]; - Buf[0] = writeAddr[0]; - Buf[1] = writeAddr[1]; + uint8_t dataSize; + // protect and select + switch (startAddr) { + case START_ADR_STAT: + number = 0; + dataSize = STAT_BLOCKSIZE; + break; + case START_ADR_SHOT: + dataSize = SHOT_BLOCKSIZE; + if(number > MAX_NUMBER_SHOTS) + return; + break; + case START_ADR_PROGRAM: + dataSize = PROGRAM_BLOCKSIZE; + if(number > MAX_NUMBER_PROGRAMS) + return; + break; + case START_ADR_MACRO: + dataSize = MACRO_BLOCKSIZE; + if(number > MAX_NUMBER_MACRO) + return; + break; + default: + return; + break; + } + + uint16_t blockAddr16 = (uint16_t)(startAddr + (uint16_t)(number*dataSize)); + uint8_t blockAddr[2] = {HIBYTE(blockAddr16), LOBYTE(blockAddr16)}; + + unsigned char Buf[dataSize+2]; + memset(Buf, 0x00, sizeof(Buf)); + Buf[0] = blockAddr[0]; + Buf[1] = blockAddr[1]; - for( unsigned char i = 0; i < (FLASH_BLOCKSIZE); i++ ) Buf[i+2] = flashWrBufPtr[i]; + for( unsigned char i = 0; i < (dataSize); i++ ) Buf[i+2] = writeData[i]; - HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), Buf, (FLASH_BLOCKSIZE + 2), 10); - HAL_Delay(1); + HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), Buf, (dataSize + 2), 10); + HAL_Delay(1); +} + +void FLASH_ReadBlock(uint16_t startAddr, uint8_t number, uint8_t *readData){ + uint8_t dataSize; + // protect and select + switch (startAddr) { + case START_ADR_STAT: + number = 0; + dataSize = STAT_BLOCKSIZE; + break; + case START_ADR_SHOT: + dataSize = SHOT_BLOCKSIZE; + if(number > MAX_NUMBER_SHOTS) + return; + break; + case START_ADR_PROGRAM: + dataSize = PROGRAM_BLOCKSIZE; + if(number > MAX_NUMBER_PROGRAMS) + return; + break; + case START_ADR_MACRO: + dataSize = MACRO_BLOCKSIZE; + if(number > MAX_NUMBER_MACRO) + return; + break; + default: + return; + break; + } + + memset(readData, 0x00, dataSize); + + uint16_t blockAddr16 = (uint16_t)(startAddr + (uint16_t)(number*dataSize)); + uint8_t blockAddr[2] = {HIBYTE(blockAddr16), LOBYTE(blockAddr16)}; + + HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), blockAddr, 2, 10); + HAL_Delay(1); + HAL_I2C_Master_Receive(&hi2c1, (AT24C_ADRESS << 1), readData, dataSize, 10); + HAL_Delay(1); }