mirror of
https://github.com/DashyFox/StackSport.git
synced 2025-12-16 01:09:57 +00:00
fix and update save funcs
This commit is contained in:
@ -127,10 +127,9 @@ MemoryStatus EEPROM_INIT() {
|
||||
}
|
||||
//************************************************************//
|
||||
|
||||
|
||||
MemoryStatus saveInfoBlock() {
|
||||
return FLASH_WriteBlock(START_ADR_STAT, 0, (uint8_t*) &infoBlock,
|
||||
sizeof(InfoBlock));
|
||||
sizeof(InfoBlock), sizeof(InfoBlock));
|
||||
}
|
||||
|
||||
MemoryStatus getInfoBlock() {
|
||||
@ -146,7 +145,7 @@ MemoryStatus getInfoBlock() {
|
||||
|
||||
MemoryStatus saveShot(unsigned char number, Shot *shot) {
|
||||
if (FLASH_WriteBlock(START_ADR_SHOT, number, (uint8_t*) shot,
|
||||
SHOT_BLOCKSIZE) == EEPROM_OK) {
|
||||
SHOT_BLOCKSIZE, SHOT_BLOCKSIZE) == EEPROM_OK) {
|
||||
return EEPROM_OK;
|
||||
}
|
||||
return EEPROM_FAIL;
|
||||
@ -163,9 +162,16 @@ MemoryStatus getShot(unsigned char number, Shot *shot) {
|
||||
return EEPROM_OK;
|
||||
}
|
||||
|
||||
MemoryStatus delShot(unsigned char number) {
|
||||
if (FLASH_DelBlock(START_ADR_SHOT, number, SHOT_BLOCKSIZE) == EEPROM_OK) {
|
||||
return EEPROM_OK;
|
||||
}
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
|
||||
MemoryStatus saveProg(unsigned char number, Program *prog) {
|
||||
MemoryStatus result = EEPROM_OK;
|
||||
for (uint16_t i = 0; i < MAX_SHOT_COUNT_IN_PROGRAMS; ++i) {
|
||||
for (uint16_t i = 0; i < prog->header.shotCount; ++i) {
|
||||
Shot shot;
|
||||
MemoryStatus stat = getShot(prog->shots[i].id, &shot);
|
||||
if (!(stat == EEPROM_OK || stat == EEPROM_MISSING_ELEMENT)) {
|
||||
@ -177,7 +183,10 @@ MemoryStatus saveProg(unsigned char number, Program *prog) {
|
||||
}
|
||||
}
|
||||
|
||||
if (FLASH_WriteBlock(START_ADR_PROGRAM, number, (uint8_t*) prog,
|
||||
uint16_t totalSize = sizeof(ProgramHeader)
|
||||
+ sizeof(ProgramShot) * prog->header.shotCount;
|
||||
|
||||
if (FLASH_WriteBlock(START_ADR_PROGRAM, number, (uint8_t*) prog, totalSize,
|
||||
PROGRAM_BLOCKSIZE) != EEPROM_OK) {
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
@ -190,28 +199,39 @@ MemoryStatus getProg(unsigned char number, Program *prog) {
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
|
||||
if (!prog->header.isExist) {
|
||||
if (!prog->header.shotCount) {
|
||||
return EEPROM_MISSING_ELEMENT;
|
||||
}
|
||||
|
||||
return EEPROM_OK;
|
||||
}
|
||||
|
||||
MemoryStatus delProg(unsigned char number) {
|
||||
if (FLASH_DelBlock(START_ADR_PROGRAM, number, PROGRAM_BLOCKSIZE)
|
||||
== EEPROM_OK) {
|
||||
return EEPROM_OK;
|
||||
}
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
|
||||
MemoryStatus saveMacro(unsigned char number, Macro *macro) {
|
||||
MemoryStatus result = EEPROM_OK;
|
||||
for (uint16_t i = 0; i < MAX_PROGRAM_COUNT_IN_MACRO; ++i) {
|
||||
for (uint16_t i = 0; i < macro->header.programmCount; ++i) {
|
||||
Program prog;
|
||||
MemoryStatus stat = getProg(macro->programs[i].id, &prog);
|
||||
if (!(stat == EEPROM_OK || stat == EEPROM_MISSING_ELEMENT)) {
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
if (!prog.header.isExist) {
|
||||
if (!prog.header.shotCount) {
|
||||
result = EEPROM_MISSING_ELEMENT;
|
||||
// todo: добавить в запросы для загрузки программ
|
||||
}
|
||||
}
|
||||
|
||||
if (FLASH_WriteBlock(START_ADR_MACRO, number, (uint8_t*) macro,
|
||||
uint16_t totalSize = sizeof(MacroHeader)
|
||||
+ sizeof(MacroProgram) * macro->header.programmCount;
|
||||
|
||||
if (FLASH_WriteBlock(START_ADR_MACRO, number, (uint8_t*) macro, totalSize,
|
||||
MACRO_BLOCKSIZE) != EEPROM_OK) {
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
@ -225,19 +245,26 @@ MemoryStatus getMacro(unsigned char number, Macro *macro) {
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
|
||||
if (!macro->header.isExist) {
|
||||
if (!macro->header.programmCount) {
|
||||
return EEPROM_MISSING_ELEMENT;
|
||||
}
|
||||
return EEPROM_OK;
|
||||
}
|
||||
|
||||
MemoryStatus delMacro(unsigned char number) {
|
||||
if (FLASH_DelBlock(START_ADR_MACRO, number, MACRO_BLOCKSIZE) == EEPROM_OK) {
|
||||
return EEPROM_OK;
|
||||
}
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
|
||||
MemoryStatus EEPROM_EARSE() {
|
||||
uint16_t addr = 0;
|
||||
uint16_t old_addr = 0;
|
||||
do {
|
||||
uint8_t Buf[255];
|
||||
memset(Buf, 0xFF, sizeof(Buf));
|
||||
FLASH_WriteBlock(addr, 0, Buf, (uint8_t) sizeof(Buf));
|
||||
FLASH_WriteBlock(addr, 0, Buf, sizeof(Buf), sizeof(Buf));
|
||||
old_addr = addr;
|
||||
addr += sizeof(Buf);
|
||||
} while (old_addr <= addr);
|
||||
@ -246,7 +273,42 @@ MemoryStatus EEPROM_EARSE() {
|
||||
}
|
||||
|
||||
MemoryStatus FLASH_WriteBlock(uint16_t startAddr, uint8_t number,
|
||||
uint8_t *writeData, uint16_t dataSize) {
|
||||
uint8_t *writeData, uint16_t dataSize, uint16_t blockSize) {
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
// Проверка на корректность входных данных
|
||||
if ((startAddr == START_ADR_SHOT && number >= MAX_SHOT_COUNT)
|
||||
|| (startAddr == START_ADR_PROGRAM && number >= MAX_PROGRAM_COUNT)
|
||||
|| (startAddr == START_ADR_MACRO && number >= MAX_MACRO_COUNT)) {
|
||||
return EEPROM_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
uint16_t blockAddr16 = (uint16_t) (startAddr
|
||||
+ (uint16_t) (number * blockSize));
|
||||
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 < dataSize; i++)
|
||||
Buf[i + 2] = writeData[i];
|
||||
|
||||
result = HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), Buf,
|
||||
(dataSize + 2), 10);
|
||||
print("Written ");
|
||||
printNumber(dataSize);
|
||||
print("bytes\n");
|
||||
HAL_Delay(1);
|
||||
if (result != HAL_OK) {
|
||||
return EEPROM_FAIL;
|
||||
}
|
||||
return EEPROM_OK;
|
||||
}
|
||||
|
||||
MemoryStatus FLASH_DelBlock(uint16_t startAddr, uint8_t number,
|
||||
uint16_t dataSize) {
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
// Проверка на корректность входных данных
|
||||
@ -260,16 +322,14 @@ MemoryStatus FLASH_WriteBlock(uint16_t startAddr, uint8_t number,
|
||||
+ (uint16_t) (number * dataSize));
|
||||
uint8_t blockAddr[2] = { HIBYTE(blockAddr16), LOBYTE(blockAddr16) };
|
||||
|
||||
unsigned char Buf[dataSize + 2];
|
||||
unsigned char Buf[2 + 1];
|
||||
memset(Buf, 0x00, sizeof(Buf));
|
||||
Buf[0] = blockAddr[0];
|
||||
Buf[1] = blockAddr[1];
|
||||
|
||||
for (unsigned char i = 0; i < dataSize; i++)
|
||||
Buf[i + 2] = writeData[i];
|
||||
Buf[2] = 0;
|
||||
|
||||
result = HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), Buf,
|
||||
(dataSize + 2), 10);
|
||||
sizeof(Buf), 10);
|
||||
HAL_Delay(1);
|
||||
if (result != HAL_OK) {
|
||||
return EEPROM_FAIL;
|
||||
|
||||
Reference in New Issue
Block a user