Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions TFT/src/User/API/FlashStore.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ static uint32_t byteToWord(uint8_t * bytes, uint8_t len)

void readStoredPara(void)
{
uint8_t data[PARA_SIZE];
uint32_t data[PARA_SIZE];
uint32_t index = 0;
uint32_t sign = 0;

#ifdef I2C_EEPROM // added I2C_EEPROM suppport for MKS_TFT35_V1_0
EEPROM_FlashRead(data, PARA_SIZE);
EEPROM_FlashRead((uint8_t*)data, PARA_SIZE);
#else
HAL_FlashRead(data, PARA_SIZE);
HAL_FlashRead((uint8_t*)data, PARA_SIZE);
#endif

sign = byteToWord(data + (index += 4), 4);
sign = data[index++];

if (sign == TSC_SIGN)
{
paraStatus |= PARA_TSC_EXIST; // if the touch screen calibration parameter already exists

for (int i = 0; i < sizeof(TS_CalPara) / sizeof(TS_CalPara[0]); i++)
{
TS_CalPara[i] = byteToWord(data + (index += 4), 4);
TS_CalPara[i] = data[index++];
}
}

sign = byteToWord(data + (index += 4), 4);
sign = data[index++];

if (sign != PARA_SIGN) // if the settings parameter is illegal, reset settings parameter
{
Expand All @@ -78,30 +78,31 @@ void readStoredPara(void)
}
else
{
memcpy(&infoSettings, data + (index += 4), sizeof(SETTINGS));
memcpy(&infoSettings, &data[index], sizeof(SETTINGS));
//if ((paraStatus & PARA_TSC_EXIST) == 0) infoSettings.rotated_ui = DISABLED; // unecessarily rotates UI to Default?
}
}

void storePara(void)
{
uint8_t data[PARA_SIZE];
uint32_t data[PARA_SIZE];
uint32_t index = 0;

wordToByte(TSC_SIGN, data + (index += 4));
memset(data, 0xFF, sizeof(data)); // initialise buffer to unwritten flash memory
data[index++] = TSC_SIGN;

for (int i = 0; i < sizeof(TS_CalPara) / sizeof(TS_CalPara[0]); i++)
{
wordToByte(TS_CalPara[i], data + (index += 4));
data[index++] = TS_CalPara[i];
}

wordToByte(PARA_SIGN, data + (index += 4));
memcpy(data + (index += 4), &infoSettings, sizeof(SETTINGS));
data[index++] = PARA_SIGN;

memcpy(&data[index], &infoSettings, sizeof(SETTINGS));
#ifdef I2C_EEPROM // added I2C_EEPROM suppport for MKS_TFT35_V1_0
EEPROM_FlashWrite(data, PARA_SIZE); // store settings in I2C_EEPROM
EEPROM_FlashWrite((uint8_t*)data, PARA_SIZE); // store settings in I2C_EEPROM
#else
HAL_FlashWrite(data, PARA_SIZE);
HAL_FlashWrite((uint8_t*)data, PARA_SIZE);
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion TFT/src/User/API/FlashStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ extern "C" {
#endif

#include <stdbool.h>
#define EMPTY_FLASH_WORD 0xFFFFFFFF

#define PARA_SIZE (128 * 3) // max size of settings buffer to read/write
#define PARA_SIZE (128 * 2) // max size of settings buffer to read/write

void readStoredPara(void); // read settings parameter if exist, or reset settings parameter
void storePara(void);
Expand Down
9 changes: 7 additions & 2 deletions TFT/src/User/API/Settings.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "Settings.h"
#include "includes.h"
#include <assert.h>

SETTINGS infoSettings;

// Ensure PARA_SIZE is defined large enough for user data
static_assert(sizeof(infoSettings) <= PARA_SIZE - 36, "PARA_SIZE is too small"); // 4(TSC_SIGN) + (7 x 4)(TSC VALUES) + 4(PARA_SIGN) = 36 (see FlashStore.c)

MACHINE_SETTINGS infoMachineSettings;

static const uint8_t default_serial_port[] = {SP_1, SP_2, SP_3, SP_4};
Expand Down Expand Up @@ -186,15 +191,15 @@ void initSettings(void)
resetConfig();

// calculate checksum excluding the CRC variable in infoSettings
infoSettings.CRC_checksum = calculateCRC16((uint8_t *) &infoSettings + sizeof(infoSettings.CRC_checksum),
infoSettings.CRC_checksum = calculateCRC32((uint8_t *) &infoSettings + sizeof(infoSettings.CRC_checksum),
sizeof(infoSettings) - sizeof(infoSettings.CRC_checksum));
}

// save settings to Flash only if CRC does not match
void saveSettings(void)
{
// calculate checksum excluding the CRC variable in infoSettings
uint32_t curCRC = calculateCRC16((uint8_t *) &infoSettings + sizeof(infoSettings.CRC_checksum),
uint32_t curCRC = calculateCRC32((uint8_t *) &infoSettings + sizeof(infoSettings.CRC_checksum),
sizeof(infoSettings) - sizeof(infoSettings.CRC_checksum));

if (curCRC != infoSettings.CRC_checksum) // save to Flash only if CRC does not match
Expand Down
2 changes: 1 addition & 1 deletion TFT/src/User/API/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ typedef enum

typedef struct
{
uint16_t CRC_checksum;
uint32_t CRC_checksum;

// General Settings
uint8_t serial_port[MAX_SERIAL_PORT_COUNT];
Expand Down
38 changes: 32 additions & 6 deletions TFT/src/User/Hal/gd32f20x/HAL_Flash.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "HAL_Flash.h"
#include "variants.h" // for fmc_unlock etc.
#include "my_misc.h"
#include "FlashStore.h"

/*
* Page 0 0x0800 0000 - 0x0800 07FF 2 Kbyte
Expand All @@ -13,32 +14,57 @@
*/

#define SIGN_ADDRESS (0x08040000 - 0x800) // reserve the last page (2KB) to save user parameters
#define FLASH_SECTOR_SIZE 0x800 // 2KByte flash sector

uint32_t Find_EmptyFlashSlot()
{
for (uint32_t i = 0; i < (FLASH_SECTOR_SIZE / PARA_SIZE); i++)
{
if (*((volatile uint32_t *)(SIGN_ADDRESS + (i * PARA_SIZE) + 1)) == EMPTY_FLASH_WORD)
return i; // found
}

return (FLASH_SECTOR_SIZE / PARA_SIZE); // nothing found, indicate impossible slot
}

void HAL_FlashRead(uint8_t * data, uint32_t len)
{
uint32_t i = 0;

uint32_t slot = Find_EmptyFlashSlot();
if (slot) // read from slot before empty slot
slot--;

for (i = 0; i < len; i++)
{
data[i] = *((volatile uint8_t *)(SIGN_ADDRESS + i));
data[i] = *((volatile uint8_t *)(SIGN_ADDRESS + (slot * PARA_SIZE) + i));
}
}

void HAL_FlashWrite(uint8_t * data, uint32_t len)
{
uint32_t i = 0;

// find an empty flash slot
uint32_t slot = Find_EmptyFlashSlot();

fmc_unlock();
fmc_page_erase(SIGN_ADDRESS);
fmc_flag_clear(FMC_FLAG_BANK0_END);
fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
fmc_flag_clear(FMC_FLAG_BANK0_PGERR);

if (slot == (FLASH_SECTOR_SIZE / PARA_SIZE)) // no more empty slots, start over
{
fmc_page_erase(SIGN_ADDRESS);
fmc_flag_clear(FMC_FLAG_BANK0_END);
fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
fmc_flag_clear(FMC_FLAG_BANK0_PGERR);

slot = 0; // restart from the first slot
}

for (i = 0; i < len; i += 2)
{
uint16_t data16 = data[i] | (data[MIN(i + 1, len - 1)] << 8); // gd32f20x needs to write at least 16 bits at a time

fmc_halfword_program(SIGN_ADDRESS + i, data16);
fmc_halfword_program(SIGN_ADDRESS + (slot * PARA_SIZE) + i, data16);
fmc_flag_clear(FMC_FLAG_BANK0_END);
fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
Expand Down
38 changes: 32 additions & 6 deletions TFT/src/User/Hal/gd32f30x/HAL_Flash.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "HAL_Flash.h"
#include "variants.h" // for fmc_unlock etc.
#include "my_misc.h"
#include "FlashStore.h"

/*
* Page 0 0x0800 0000 - 0x0800 07FF 2 Kbyte
Expand All @@ -13,32 +14,57 @@
*/

#define SIGN_ADDRESS (0x08040000 - 0x800) // reserve the last page (2KB) to save user parameters
#define FLASH_SECTOR_SIZE 0x800 // 2KBytes flash sector

uint32_t Find_EmptyFlashSlot()
{
for (uint32_t i = 0; i < (FLASH_SECTOR_SIZE / PARA_SIZE); i++)
{
if (*((volatile uint32_t *)(SIGN_ADDRESS + (i * PARA_SIZE) + 1)) == EMPTY_FLASH_WORD)
return i; // found
}

return (FLASH_SECTOR_SIZE / PARA_SIZE); // nothing found, indicate impossible slot
}

void HAL_FlashRead(uint8_t * data, uint32_t len)
{
uint32_t i = 0;

uint32_t slot = Find_EmptyFlashSlot();
if (slot) // read from slot before empty slot
slot--;

for (i = 0; i < len; i++)
{
data[i] = *((volatile uint8_t *)(SIGN_ADDRESS + i));
data[i] = *((volatile uint8_t *)(SIGN_ADDRESS + (slot * PARA_SIZE) + i));
}
}

void HAL_FlashWrite(uint8_t * data, uint32_t len)
{
uint32_t i = 0;

// find an empty flash slot
uint32_t slot = Find_EmptyFlashSlot();

fmc_unlock();
fmc_page_erase(SIGN_ADDRESS);
fmc_flag_clear(FMC_FLAG_BANK0_END);
fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
fmc_flag_clear(FMC_FLAG_BANK0_PGERR);

if (slot == (FLASH_SECTOR_SIZE / PARA_SIZE)) // no more empty slots, start over
{
fmc_page_erase(SIGN_ADDRESS);
fmc_flag_clear(FMC_FLAG_BANK0_END);
fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
fmc_flag_clear(FMC_FLAG_BANK0_PGERR);

slot = 0; // restart from the first slot
}

for (i = 0; i < len; i += 2)
{
uint16_t data16 = data[i] | (data[MIN(i + 1, len - 1)] << 8); // gd32f20x needs to write at least 16 bits at a time

fmc_halfword_program(SIGN_ADDRESS + i, data16);
fmc_halfword_program(SIGN_ADDRESS + (slot * PARA_SIZE) + i, data16);
fmc_flag_clear(FMC_FLAG_BANK0_END);
fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
Expand Down
31 changes: 28 additions & 3 deletions TFT/src/User/Hal/stm32f10x/HAL_Flash.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
#include "HAL_Flash.h"
#include "variants.h" // for FLASH_Unlock etc.
#include "my_misc.h"
#include "FlashStore.h"

#define SIGN_ADDRESS (0x08040000 - 0x800) // reserve the last page (2KB) to save user parameters
#define FLASH_SECTOR_SIZE 0x800 // 2KB flash sector

uint32_t Find_EmptyFlashSlot()
{
for (uint32_t i = 0; i < (FLASH_SECTOR_SIZE / PARA_SIZE); i++)
{
if (*((volatile uint32_t *)(SIGN_ADDRESS + (i * PARA_SIZE) + 1)) == EMPTY_FLASH_WORD)
return i; // found
}

return (FLASH_SECTOR_SIZE / PARA_SIZE); // nothing found, indicate impossible slot
}

void HAL_FlashRead(uint8_t * data, uint32_t len)
{
uint32_t i = 0;

uint32_t slot = Find_EmptyFlashSlot();
if (slot) // read from slot before empty slot
slot--;

for (i = 0; i < len; i++)
{
data[i] = *((volatile uint8_t *)(SIGN_ADDRESS + i));
data[i] = *((volatile uint8_t *)(SIGN_ADDRESS + (slot * PARA_SIZE) + i));
}
}

void HAL_FlashWrite(uint8_t * data, uint32_t len)
{
uint32_t i = 0;

// find an empty flash slot
uint32_t slot = Find_EmptyFlashSlot();

FLASH_Unlock();
FLASH_ErasePage(SIGN_ADDRESS);

if (slot == (FLASH_SECTOR_SIZE / PARA_SIZE)) // no more empty slots, start over
{
FLASH_ErasePage(SIGN_ADDRESS);
slot = 0; // restart from the first slot
}

for (i = 0; i < len; i += 2)
{
uint16_t data16 = data[i] | (data[MIN(i + 1, len - 1)] << 8); // stm32f10x needs to write at least 16 bits at a time

FLASH_ProgramHalfWord(SIGN_ADDRESS + i, data16);
FLASH_ProgramHalfWord(SIGN_ADDRESS + (slot * PARA_SIZE) + i, data16);
}

FLASH_Lock();
Expand Down
Loading