ShipCentralControl/Anjiehui7_DTU/User/flash_config.c

256 lines
7.9 KiB
C
Raw Normal View History

2025-04-03 15:59:48 +08:00
/*
*********************************************************************************************************
* IAR Development Kits
* on the
*
* M451
*
* Filename : flash_config.h
* Version : V1.00
* Programmer(s) : Qian Xianghong
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include "includes.h"
// Flash<73><EFBFBD><EFBFBD><E6BBAE><EFBFBD>£<EFBFBD>
// 0~7K: boot-loader<65><72><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LDROM<4F><4D><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ִ<EFBFBD><D6B4>
// 8~11K: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>2K+2K<32><4B>˫<EFBFBD>ݴ洢<DDB4><E6B4A2>
// 12~15K<35><4B><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// 16~255K: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>16<31><36><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ⣬<D6BF><E2A3AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// 256~511K<31><4B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// 512K~8M: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>GPRS<52>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>ÿ黮<C3BF><E9BBAE>Ϊ4<CEAA><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define CONFIG_DC_SIZE (DATA_BUFF_SIZE)
// <20><><EFBFBD>÷ֿ<C3B7><D6BF><EFBFBD>С<EFBFBD><D0A1>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ4<CEAA><34><EFBFBD>飬ѭ<E9A3AC><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>
#define CONFIG_BLOCK_SIZE (CONFIG_DC_SIZE * 4)
// <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>=<3D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>=<3D>ɲ<EFBFBD><C9B2><EFBFBD>ҳ<EFBFBD><D2B3>С
#define CONFIG_PARTITION_SIZE (CONFIG_BLOCK_SIZE * 4)
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: 0~1, <20>ֿ<EFBFBD><D6BF><EFBFBD>: 0~3<><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: 0~3
#define CONFIG_PARTITION(partition) (partition)
#define CONFIG_BLOCK(block) (block)
#define CONFIG_DC(DC) ((DC) - 1)
// <20><>дFlash<73><68><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
static rd_buff_t rdBuff;
// <20><>дFlash<73>Ļ<EFBFBD><C4BB><EFBFBD>
uint8_t Config_rwBuf[CONFIG_BLOCK_SIZE] = {0}; // <20><>չ<EFBFBD>ڴ<EFBFBD>
static uint32_t Flash_ErasePartition(uint8_t partition)
{
uint8_t i, loop = CONFIG_PARTITION_SIZE / SFLASH_ERASE_SIZE;
uint32_t u32StartAddr = SFLASH_CONFIG_BASE
+ CONFIG_PARTITION(partition) * CONFIG_PARTITION_SIZE;
for(i = 0; i < loop; i++)
{
SFlash_WriteEN();
SFlash_PageErase(u32StartAddr + i * SFLASH_ERASE_SIZE);
}
return TRUE;
}
static uint32_t Flash_ReadDC(uint8_t partition, uint8_t block, uint8_t DC, rd_buff_t *dataBuff)
{
uint32_t u32StartAddr = SFLASH_CONFIG_BASE
+ CONFIG_PARTITION(partition) * CONFIG_PARTITION_SIZE
+ CONFIG_BLOCK(block) * CONFIG_BLOCK_SIZE
+ CONFIG_DC(DC) * CONFIG_DC_SIZE;
SFlash_BufferRead(u32StartAddr, dataBuff->buff, CONFIG_DATA_LEN(DC));
return TRUE;
}
static uint32_t Flash_ReadBlock(uint8_t partition, uint8_t block, uint32_t forTest)
{
// <20>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>2<EFBFBD><32>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(CONFIG_BOARD_SELF == CONFIG_BOARD_SAMPLE || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
{
if(!Flash_ReadDC(partition, block, CAN_DC_BOTTLE_CONFIG, &rdBuff) || !Config_CheckValid(&rdBuff, CAN_DC_BOTTLE_CONFIG))
return FALSE;
if(forTest)
return TRUE;
dcBuff.configBottle = rdBuff.configBottle;
if(!Flash_ReadDC(partition, block, CAN_DC_SENSOR_CONFIG, &rdBuff) || !Config_CheckValid(&rdBuff, CAN_DC_SENSOR_CONFIG))
return FALSE;
dcBuff.configSensor = rdBuff.configSensor;
if(!Flash_ReadDC(partition, block, CAN_DC_DATA_CONFIG, &rdBuff) || !Config_CheckValid(&rdBuff, CAN_DC_DATA_CONFIG))
return FALSE;
dcBuff.configData = rdBuff.configData;
}
// <20><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(CONFIG_BOARD_SELF == CONFIG_BOARD_POWER || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
{
if(!Flash_ReadDC(partition, block, CAN_DC_DISPLAY_CONFIG, &rdBuff) || !Config_CheckValid(&rdBuff, CAN_DC_DISPLAY_CONFIG))
return FALSE;
if(CONFIG_BOARD_SELF == CONFIG_BOARD_POWER)
{
if(forTest)
return TRUE;
}
dcBuff.configDisplay = rdBuff.configDisplay;
}
return TRUE;
}
static uint32_t Flash_ReadConfig(uint8_t partition, uint32_t forTest, uint8_t *blockNo)
{
uint8_t block;
for(block = 0; block < 4; block++)
{
if(Flash_ReadBlock(partition, block, forTest))
{
*blockNo = block;
return TRUE;
}
}
return FALSE;
}
static uint32_t Flash_WriteConfig(uint8_t partition)
{
uint8_t block = 0;
uint8_t i;
uint32_t u32StartAddr;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĿ<DDB5><C4BF><EFBFBD>
if(!Flash_ReadConfig(partition, TRUE, &block))
block = 3;
for(i = 0; i < 4; i++)
{
// ȷ<><C8B7>ѭ<EFBFBD><D1AD>д<EFBFBD><D0B4><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
block = (block + 1) % 4;
if(!Flash_ErasePartition(partition))
return FALSE;
memset(Config_rwBuf, 0xFF, CONFIG_BLOCK_SIZE);
// <20>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>2<EFBFBD><32>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(CONFIG_BOARD_SELF == CONFIG_BOARD_SAMPLE || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
{
rdBuff.configBottle = dcBuff.configBottle;
memmove(Config_rwBuf + CONFIG_DC(CAN_DC_BOTTLE_CONFIG) * CONFIG_DC_SIZE, rdBuff.buff, CONFIG_DATA_LEN(CAN_DC_BOTTLE_CONFIG));
rdBuff.configSensor = dcBuff.configSensor;
memmove(Config_rwBuf + CONFIG_DC(CAN_DC_SENSOR_CONFIG) * CONFIG_DC_SIZE, rdBuff.buff, CONFIG_DATA_LEN(CAN_DC_SENSOR_CONFIG));
rdBuff.configData = dcBuff.configData;
memmove(Config_rwBuf + CONFIG_DC(CAN_DC_DATA_CONFIG) * CONFIG_DC_SIZE, rdBuff.buff, CONFIG_DATA_LEN(CAN_DC_DATA_CONFIG));
}
// <20><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(CONFIG_BOARD_SELF == CONFIG_BOARD_POWER || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
{
rdBuff.configDisplay = dcBuff.configDisplay;
memmove(Config_rwBuf + CONFIG_DC(CAN_DC_DISPLAY_CONFIG) * CONFIG_DC_SIZE, rdBuff.buff, CONFIG_DATA_LEN(CAN_DC_DISPLAY_CONFIG));
}
u32StartAddr = SFLASH_CONFIG_BASE
+ CONFIG_PARTITION(partition) * CONFIG_PARTITION_SIZE
+ CONFIG_BLOCK(block) * CONFIG_BLOCK_SIZE;
if(SFlash_BufferWrite(u32StartAddr, Config_rwBuf, CONFIG_BLOCK_SIZE))
return TRUE;
}
return FALSE;
}
uint32_t Config_LoadConfig()
{
uint8_t block;
if(Flash_ReadConfig(0, FALSE, &block))
return TRUE;
return Flash_ReadConfig(1, FALSE, &block);
}
uint32_t Config_SaveConfig()
{
uint32_t ret = 1, ret1 = 1;
Config_ForceValid((rd_buff_t *) &dcBuff.configBottle, CAN_DC_BOTTLE_CONFIG);
Config_ForceValid((rd_buff_t *) &dcBuff.configSensor, CAN_DC_SENSOR_CONFIG);
Config_ForceValid((rd_buff_t *) &dcBuff.configData, CAN_DC_DATA_CONFIG);
Config_ForceValid((rd_buff_t *) &dcBuff.configDisplay, CAN_DC_DISPLAY_CONFIG);
// <20><>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>
ret = Flash_WriteConfig(0);
ret1= Flash_WriteConfig(1);
return ret || ret1;
}
void Config_Open()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD>ڴ<EFBFBD>
// Config_rwBuf = SRAM_Alloc(CONFIG_BLOCK_SIZE);
delay_ms(20);
SFlash_UnlockBPR();
// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>
// <20><>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Config_setBottleDefParam(&dcBuff);
Config_setSensorDefParam(&dcBuff);
Config_setDataDefParam(&dcBuff);
Config_setDisplayDefParam(&dcBuff);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>
Config_LoadConfig();
// // <20><><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>
2025-04-24 08:10:26 +08:00
dcBuff.configBottle.PSN[0] = 25; // PSN: 2025092980002
2025-04-03 15:59:48 +08:00
dcBuff.configBottle.PSN[1] = 9;
dcBuff.configBottle.PSN[2] = 29;
dcBuff.configBottle.PSN[3] = 80;
dcBuff.configBottle.PSN[4] = 0;
dcBuff.configBottle.PSN[5] = 2;
Config_SaveConfig();
// dcBuff.configData.intervalTrans = 120;
// <20><><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD>ϵͳ<CFB5><CDB3>Ϣ
if(CONFIG_BOARD_SELF == CONFIG_BOARD_SAMPLE || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
Config_fillSampleInfo(&dcBuff);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>ϵͳ<CFB5><CDB3>Ϣ
if(CONFIG_BOARD_SELF == CONFIG_BOARD_POWER || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
Config_fillPowerInfo(&dcBuff);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ϵͳ<CFB5><CDB3>Ϣ
if(CONFIG_BOARD_SELF == CONFIG_BOARD_DISPLAY || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
Config_fillDisplayInfo(&dcBuff);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3>Ϣ
if(CONFIG_BOARD_SELF == CONFIG_BOARD_BLUETOOTH || CONFIG_BOARD_SELF == CONFIG_BOARD_SINGLE)
Config_fillBluetoothInfo(&dcBuff);
// <20><>ʾ<EFBFBD><EFBFBD><E6B1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
printf("\n****\tPSN: 20%02d%02d%02d%02d%03d ****\n", dcBuff.configBottle.PSN[0], dcBuff.configBottle.PSN[1],
dcBuff.configBottle.PSN[2], dcBuff.configBottle.PSN[3],
(dcBuff.configBottle.PSN[4] << 8) | dcBuff.configBottle.PSN[5]);
printf("\n****\tProductVer: %d ***\n", dcBuff.powerInfo.hardVer.minor);
printf("\n****\tHardVer: %d, SoftVer: %d, protcolVer: %d, SoftDate: 20%02d-%02d-%02d ***\n",
dcBuff.powerInfo.hardVer.major, dcBuff.powerInfo.softVer.minor, dcBuff.powerInfo.protocolVer.minor,
dcBuff.powerInfo.softDate.year, dcBuff.powerInfo.softDate.month, dcBuff.powerInfo.softDate.day);
}