初版程序
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
#include "bsp_Flash.h"
|
||||
#include "string.h"
|
||||
#include "bsp_Wdg.h"
|
||||
#include "bsp_W5500.h"
|
||||
#include "bsp_W5500_1.h"
|
||||
#include "proto_HSMS.h"
|
||||
/* FLASH Memory Definitions */
|
||||
#define BSP_FLASH_SIZE (0x10000UL)
|
||||
#define BSP_FLASH_PAGE_SIZE (FLASH_PAGE_SIZE)
|
||||
#define BSP_FLASH_PAGE_NUM (BSP_FLASH_SIZE/BSP_FLASH_PAGE_SIZE)
|
||||
|
||||
#define BSP_FLASH_ADDR_RW(n) ((uint32_t)(FLASH_BASE + (BSP_FLASH_PAGE_NUM - (n)) * BSP_FLASH_PAGE_SIZE))
|
||||
#define BSP_FLASH_DATASAVE_ADDR BSP_FLASH_ADDR_RW(1)
|
||||
|
||||
static void bsp_Flash_Init(void);
|
||||
static void bsp_FlashDataWrite(void);
|
||||
static void bsp_FlashDataRead(void);
|
||||
static void bsp_FlashReset(void);
|
||||
|
||||
bsp_Flash_t Usr_Flash =
|
||||
{
|
||||
.Init = bsp_Flash_Init,
|
||||
.Write = bsp_FlashDataWrite,
|
||||
.Read = bsp_FlashDataRead,
|
||||
.Reset = bsp_FlashReset,
|
||||
};
|
||||
|
||||
// 擦除指定页
|
||||
static HAL_StatusTypeDef bsp_FLASH_ErasePage(uint32_t PageAddress)
|
||||
{
|
||||
FLASH_EraseInitTypeDef EraseInitStruct;
|
||||
uint32_t PageError = 0;
|
||||
|
||||
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
||||
EraseInitStruct.PageAddress = PageAddress;
|
||||
EraseInitStruct.NbPages = 1;
|
||||
|
||||
return HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
|
||||
}
|
||||
|
||||
// 读取数据 - 按32位读取
|
||||
static void bsp_Flash_STMFLASH_Read(uint32_t ReadAddr, void *pBuffer, uint32_t size)
|
||||
{
|
||||
uint8_t *pBuf = (uint8_t*)pBuffer;
|
||||
uint32_t *addr = (uint32_t*)ReadAddr;
|
||||
uint32_t words = size / 4;
|
||||
uint32_t bytes_remaining = size % 4;
|
||||
|
||||
// 读取完整的32位字
|
||||
for(uint32_t i = 0; i < words; i++)
|
||||
{
|
||||
*((uint32_t*)pBuf) = addr[i];
|
||||
pBuf += 4;
|
||||
}
|
||||
|
||||
// 读取剩余的字节
|
||||
if(bytes_remaining > 0)
|
||||
{
|
||||
uint32_t last_word = addr[words];
|
||||
uint8_t *last_bytes = (uint8_t*)&last_word;
|
||||
|
||||
for(uint32_t i = 0; i < bytes_remaining; i++)
|
||||
{
|
||||
pBuf[i] = last_bytes[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 写入数据 - 按32位写入
|
||||
static HAL_StatusTypeDef bsp_Flash_STMFLASH_Write(uint32_t WriteAddr, void *pBuffer, uint32_t size)
|
||||
{
|
||||
HAL_StatusTypeDef status = HAL_OK;
|
||||
uint8_t *pBuf = (uint8_t*)pBuffer;
|
||||
uint32_t words = size / 4;
|
||||
uint32_t bytes_remaining = size % 4;
|
||||
uint32_t current_addr = WriteAddr;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
// 擦除目标页
|
||||
status = bsp_FLASH_ErasePage(WriteAddr);
|
||||
if(status != HAL_OK)
|
||||
{
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
// 写入完整的32位字
|
||||
for(uint32_t i = 0; i < words; i++)
|
||||
{
|
||||
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
|
||||
current_addr,
|
||||
*((uint32_t*)pBuf));
|
||||
if(status != HAL_OK) break;
|
||||
|
||||
current_addr += 4;
|
||||
pBuf += 4;
|
||||
}
|
||||
|
||||
// 写入剩余的字节
|
||||
if(status == HAL_OK && bytes_remaining > 0)
|
||||
{
|
||||
uint32_t last_word = 0xFFFFFFFF; // 默认填充0xFF
|
||||
uint8_t *last_bytes = (uint8_t*)&last_word;
|
||||
|
||||
// 复制剩余数据
|
||||
for(uint32_t i = 0; i < bytes_remaining; i++)
|
||||
{
|
||||
last_bytes[i] = pBuf[i];
|
||||
}
|
||||
|
||||
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, current_addr, last_word);
|
||||
}
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
static void bsp_FlashReset(void)
|
||||
{
|
||||
u16 port = 5002;
|
||||
Usr_Flash.FlashData.IP_Addr[0] = 10;
|
||||
Usr_Flash.FlashData.IP_Addr[1] = 21;
|
||||
Usr_Flash.FlashData.IP_Addr[2] = 54;
|
||||
Usr_Flash.FlashData.IP_Addr[3] = 2;
|
||||
|
||||
Usr_Flash.FlashData.Port[0] = port >> 8;
|
||||
Usr_Flash.FlashData.Port[1] = port & 0x00ff;
|
||||
|
||||
Usr_Flash.FlashData.Sub_Mask[0] = 255;
|
||||
Usr_Flash.FlashData.Sub_Mask[1] = 255;
|
||||
Usr_Flash.FlashData.Sub_Mask[2] = 252;
|
||||
Usr_Flash.FlashData.Sub_Mask[3] = 0;
|
||||
|
||||
Usr_Flash.FlashData.Gateway_IP[0] = 10;
|
||||
Usr_Flash.FlashData.Gateway_IP[1] = 21;
|
||||
Usr_Flash.FlashData.Gateway_IP[2] = 52;
|
||||
Usr_Flash.FlashData.Gateway_IP[3] = 1;
|
||||
|
||||
Usr_Flash.FlashData.Devic_Id = 0;
|
||||
|
||||
/*客户端主机网络配置*/
|
||||
u16 port_1 = 5000;
|
||||
u16 port_2 = 502;
|
||||
Usr_Flash.FlashData.IP_Addr_1[0] = 192;
|
||||
Usr_Flash.FlashData.IP_Addr_1[1] = 168;
|
||||
Usr_Flash.FlashData.IP_Addr_1[2] = 100;
|
||||
Usr_Flash.FlashData.IP_Addr_1[3] = 103;
|
||||
|
||||
Usr_Flash.FlashData.Port_1[0] = port_1 >> 8;
|
||||
Usr_Flash.FlashData.Port_1[1] = port_1 & 0x00ff;
|
||||
|
||||
Usr_Flash.FlashData.Sub_Mask_1[0] = 255;
|
||||
Usr_Flash.FlashData.Sub_Mask_1[1] = 255;
|
||||
Usr_Flash.FlashData.Sub_Mask_1[2] = 255;
|
||||
Usr_Flash.FlashData.Sub_Mask_1[3] = 0;
|
||||
|
||||
Usr_Flash.FlashData.Gateway_IP_1[0] = 192;
|
||||
Usr_Flash.FlashData.Gateway_IP_1[1] = 168;
|
||||
Usr_Flash.FlashData.Gateway_IP_1[2] = 100;
|
||||
Usr_Flash.FlashData.Gateway_IP_1[3] = 1;
|
||||
|
||||
/*客户端目标IP*/
|
||||
Usr_Flash.FlashData.IP_Addr_2[0] = 192;
|
||||
Usr_Flash.FlashData.IP_Addr_2[1] = 168;
|
||||
Usr_Flash.FlashData.IP_Addr_2[2] = 100;
|
||||
Usr_Flash.FlashData.IP_Addr_2[3] = 61;
|
||||
|
||||
Usr_Flash.FlashData.Port_2[0] = port_2 >> 8;
|
||||
Usr_Flash.FlashData.Port_2[1] = port_2 & 0x00ff;
|
||||
|
||||
Usr_Flash.FlashData.Devic_Id_1 = 0;
|
||||
|
||||
bsp_FlashDataWrite();
|
||||
}
|
||||
|
||||
static void bsp_flash_device_resset(void)
|
||||
{
|
||||
|
||||
/* ================== 清空所有字符串字段 ================== */
|
||||
memset(Usr_Flash.FlashData.Device_SN, 0, sizeof(Usr_Flash.FlashData.Device_SN));
|
||||
memset(Usr_Flash.FlashData.Device_Model, 0, sizeof(Usr_Flash.FlashData.Device_Model));
|
||||
memset(Usr_Flash.FlashData.Station_Type, 0, sizeof(Usr_Flash.FlashData.Station_Type));
|
||||
memset(Usr_Flash.FlashData.Device_ID, 0, sizeof(Usr_Flash.FlashData.Device_ID));
|
||||
memset(Usr_Flash.FlashData.Version, 0, sizeof(Usr_Flash.FlashData.Version));
|
||||
memset(Usr_Flash.FlashData.Manufacturer, 0, sizeof(Usr_Flash.FlashData.Manufacturer));
|
||||
memset(Usr_Flash.FlashData.Station_Name, 0, sizeof(Usr_Flash.FlashData.Station_Name));
|
||||
memset(Usr_Flash.FlashData.Chamber_Name, 0, sizeof(Usr_Flash.FlashData.Chamber_Name));
|
||||
|
||||
// 1. 机台和位置:OS_S_ABDDE01_A_31P (20字符 → 10个 u16)
|
||||
Usr_Flash.FlashData.Device_Model[0] = ((u16)'S' << 8) | 'O'; // "OS"
|
||||
Usr_Flash.FlashData.Device_Model[1] = ((u16)'S' << 8) | '_'; // "_S"
|
||||
Usr_Flash.FlashData.Device_Model[2] = ((u16)'A' << 8) | '_'; // "_A"
|
||||
Usr_Flash.FlashData.Device_Model[3] = ((u16)'D' << 8) | 'B'; // "BD"
|
||||
Usr_Flash.FlashData.Device_Model[4] = ((u16)'E' << 8) | 'D'; // "DE"
|
||||
Usr_Flash.FlashData.Device_Model[5] = ((u16)'0' << 8) | '1'; // "01"
|
||||
Usr_Flash.FlashData.Device_Model[6] = ((u16)'A' << 8) | '_'; // "_A"
|
||||
Usr_Flash.FlashData.Device_Model[7] = ((u16)'3' << 8) | '_'; // "_3"
|
||||
Usr_Flash.FlashData.Device_Model[8] = ((u16)'P' << 8) | '1'; // "1P"
|
||||
// 第9个元素已经清零 (Device_Model[9] = 0)
|
||||
|
||||
// 2. 设备型号:SJPW1500DB-650S-208 (19字符)
|
||||
Usr_Flash.FlashData.Device_ID[0] = ((u16)'J' << 8) | 'S'; // "SJ"
|
||||
Usr_Flash.FlashData.Device_ID[1] = ((u16)'W' << 8) | 'P'; // "PW"
|
||||
Usr_Flash.FlashData.Device_ID[2] = ((u16)'5' << 8) | '1'; // "15"
|
||||
Usr_Flash.FlashData.Device_ID[3] = ((u16)'0' << 8) | '0'; // "00"
|
||||
Usr_Flash.FlashData.Device_ID[4] = ((u16)'B' << 8) | 'D'; // "DB"
|
||||
Usr_Flash.FlashData.Device_ID[5] = ((u16)'6' << 8) | '-'; // "-6"
|
||||
Usr_Flash.FlashData.Device_ID[6] = ((u16)'0' << 8) | '5'; // "50"
|
||||
Usr_Flash.FlashData.Device_ID[7] = ((u16)'-' << 8) | 'S'; // "S-"
|
||||
Usr_Flash.FlashData.Device_ID[8] = ((u16)'0' << 8) | '2'; // "20"
|
||||
Usr_Flash.FlashData.Device_ID[9] = ((u16)0 << 8) | '8'; // "8" + '\0'
|
||||
|
||||
// 3. 设备类型:SCRUBBER (8字符 → 4个u16)
|
||||
Usr_Flash.FlashData.Station_Type[0] = ((u16)'C' << 8) | 'S'; // "SC"
|
||||
Usr_Flash.FlashData.Station_Type[1] = ((u16)'U' << 8) | 'R'; // "RU"
|
||||
Usr_Flash.FlashData.Station_Type[2] = ((u16)'B' << 8) | 'B'; // "BB"
|
||||
Usr_Flash.FlashData.Station_Type[3] = ((u16)'R' << 8) | 'E'; // "ER"
|
||||
// 其余已清零
|
||||
|
||||
// 4. 机台名称:ABCDE11 (7字符 → 4个u16,最后一对为 '1' + '\0')
|
||||
Usr_Flash.FlashData.Station_Name[0] = ((u16)'B' << 8) | 'A'; // "AB"
|
||||
Usr_Flash.FlashData.Station_Name[1] = ((u16)'D' << 8) | 'C'; // "CD"
|
||||
Usr_Flash.FlashData.Station_Name[2] = ((u16)'1' << 8) | 'E'; // "E1"
|
||||
Usr_Flash.FlashData.Station_Name[3] = ((u16)0 << 8) | '1'; // "1"
|
||||
// 其余已清零
|
||||
|
||||
// 5. 腔体名称:ABCDE11 (与机台名称相同)
|
||||
Usr_Flash.FlashData.Chamber_Name[0] = ((u16)'B' << 8) | 'A'; // "AB"
|
||||
Usr_Flash.FlashData.Chamber_Name[1] = ((u16)'D' << 8) | 'C'; // "CD"
|
||||
Usr_Flash.FlashData.Chamber_Name[2] = ((u16)'1' << 8) | 'E'; // "E1"
|
||||
Usr_Flash.FlashData.Chamber_Name[3] = ((u16)0 << 8) | '1'; // "1"
|
||||
// 其余已清零
|
||||
|
||||
// 6. 厂商名称:SJ (2字符 → 1个u16)
|
||||
Usr_Flash.FlashData.Manufacturer[0] = ((u16)'J' << 8) | 'S'; // "SJ"
|
||||
// 其余已清零
|
||||
|
||||
// 7. 软件版本:V02.04.00.00_00.07_V4.1 (23字符数)
|
||||
Usr_Flash.FlashData.Version[0] = ((u16)'0' << 8) | 'V'; // "V0"
|
||||
Usr_Flash.FlashData.Version[1] = ((u16)'.' << 8) | '2'; // "2."
|
||||
Usr_Flash.FlashData.Version[2] = ((u16)'4' << 8) | '0'; // "04"
|
||||
Usr_Flash.FlashData.Version[3] = ((u16)'0' << 8) | '.'; // ".0"
|
||||
Usr_Flash.FlashData.Version[4] = ((u16)'.' << 8) | '0'; // "0."
|
||||
Usr_Flash.FlashData.Version[5] = ((u16)'0' << 8) | '0'; // "00"
|
||||
Usr_Flash.FlashData.Version[6] = ((u16)'0' << 8) | '_'; // "_0"
|
||||
Usr_Flash.FlashData.Version[7] = ((u16)'.' << 8) | '0'; // "0."
|
||||
Usr_Flash.FlashData.Version[8] = ((u16)'7' << 8) | '0'; // "07"
|
||||
Usr_Flash.FlashData.Version[9] = ((u16)'V' << 8) | '_'; // "_V"
|
||||
Usr_Flash.FlashData.Version[10] = ((u16)'.' << 8) | '4'; // "4."
|
||||
Usr_Flash.FlashData.Version[11] = ((u16)0 << 8) | '1'; // "1"
|
||||
|
||||
/* 其他字段无需改动,写入 Flash */
|
||||
bsp_FlashDataWrite();
|
||||
|
||||
}
|
||||
|
||||
static void bsp_Flash_Init(void)
|
||||
{
|
||||
u8 i;
|
||||
|
||||
bsp_FlashDataRead();
|
||||
if( Usr_Flash.FlashData.IP_Addr[0] == 0xff
|
||||
&& Usr_Flash.FlashData.IP_Addr[1] == 0xff
|
||||
&& Usr_Flash.FlashData.IP_Addr[2] == 0xff
|
||||
&& Usr_Flash.FlashData.IP_Addr[3] == 0xff
|
||||
)
|
||||
{
|
||||
bsp_FlashReset();
|
||||
}
|
||||
|
||||
if( Usr_Flash.FlashData.Device_Model[0] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[1] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[2] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[3] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[4] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[5] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[6] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[7] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[8] == 0xffff
|
||||
&& Usr_Flash.FlashData.Device_Model[9] == 0xffff
|
||||
)
|
||||
{
|
||||
bsp_flash_device_resset();
|
||||
}
|
||||
|
||||
memcpy(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t));
|
||||
W5500.IP_Addr[0] = Usr_Flash.FlashData.IP_Addr[0];
|
||||
W5500.IP_Addr[1] = Usr_Flash.FlashData.IP_Addr[1];
|
||||
W5500.IP_Addr[2] = Usr_Flash.FlashData.IP_Addr[2];
|
||||
W5500.IP_Addr[3] = Usr_Flash.FlashData.IP_Addr[3];
|
||||
|
||||
W5500.Gateway_IP[0] =Usr_Flash.FlashData.Gateway_IP[0];
|
||||
W5500.Gateway_IP[1]=Usr_Flash.FlashData.Gateway_IP[1];
|
||||
W5500.Gateway_IP[2]=Usr_Flash.FlashData.Gateway_IP[2];
|
||||
W5500.Gateway_IP[3]=Usr_Flash.FlashData.Gateway_IP[3];
|
||||
|
||||
W5500.W5500_Class[0].ConfigData.Port[0] = Usr_Flash.FlashData.Port[0];
|
||||
W5500.W5500_Class[0].ConfigData.Port[1] = Usr_Flash.FlashData.Port[1];
|
||||
|
||||
W5500.Sub_Mask[0] = Usr_Flash.FlashData.Sub_Mask[0];
|
||||
W5500.Sub_Mask[1] = Usr_Flash.FlashData.Sub_Mask[1];
|
||||
W5500.Sub_Mask[2] = Usr_Flash.FlashData.Sub_Mask[2];
|
||||
W5500.Sub_Mask[3] = Usr_Flash.FlashData.Sub_Mask[3];
|
||||
|
||||
/*客户端主机网络配置*/
|
||||
W5500_1.IP_Addr[0] = Usr_Flash.FlashData.IP_Addr_1[0];
|
||||
W5500_1.IP_Addr[1] = Usr_Flash.FlashData.IP_Addr_1[1];
|
||||
W5500_1.IP_Addr[2] = Usr_Flash.FlashData.IP_Addr_1[2];
|
||||
W5500_1.IP_Addr[3] = Usr_Flash.FlashData.IP_Addr_1[3];
|
||||
|
||||
W5500_1.Gateway_IP[0] =Usr_Flash.FlashData.Gateway_IP_1[0];
|
||||
W5500_1.Gateway_IP[1]=Usr_Flash.FlashData.Gateway_IP_1[1];
|
||||
W5500_1.Gateway_IP[2]=Usr_Flash.FlashData.Gateway_IP_1[2];
|
||||
W5500_1.Gateway_IP[3]=Usr_Flash.FlashData.Gateway_IP_1[3];
|
||||
|
||||
W5500_1.W5500_Class[0].ConfigData.Port[0] = Usr_Flash.FlashData.Port_1[0];
|
||||
W5500_1.W5500_Class[0].ConfigData.Port[1] = Usr_Flash.FlashData.Port_1[1];
|
||||
|
||||
W5500_1.Sub_Mask[0] = Usr_Flash.FlashData.Sub_Mask_1[0];
|
||||
W5500_1.Sub_Mask[1] = Usr_Flash.FlashData.Sub_Mask_1[1];
|
||||
W5500_1.Sub_Mask[2] = Usr_Flash.FlashData.Sub_Mask_1[2];
|
||||
W5500_1.Sub_Mask[3] = Usr_Flash.FlashData.Sub_Mask_1[3];
|
||||
|
||||
/*客户端目标网络配置*/
|
||||
W5500_1.W5500_Class[0].ConfigData.DIP[0] = Usr_Flash.FlashData.IP_Addr_2[0];
|
||||
W5500_1.W5500_Class[0].ConfigData.DIP[1] = Usr_Flash.FlashData.IP_Addr_2[1];
|
||||
W5500_1.W5500_Class[0].ConfigData.DIP[2] = Usr_Flash.FlashData.IP_Addr_2[2];
|
||||
W5500_1.W5500_Class[0].ConfigData.DIP[3] = Usr_Flash.FlashData.IP_Addr_2[3];
|
||||
|
||||
W5500_1.W5500_Class[0].ConfigData.DPort[0] = Usr_Flash.FlashData.Port_2[0];
|
||||
W5500_1.W5500_Class[0].ConfigData.DPort[1] = Usr_Flash.FlashData.Port_2[1];
|
||||
|
||||
Usr_Flash.test = 0;
|
||||
|
||||
HSMS.Flash_ConfigData.Device_Id = Usr_Flash.FlashData.Devic_Id;
|
||||
|
||||
}
|
||||
|
||||
static void bsp_FlashDataWrite(void)
|
||||
{
|
||||
/*防止重复擦写相同数据*/
|
||||
if(memcmp(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t)) != 0)
|
||||
{
|
||||
Wdg.Feed();
|
||||
|
||||
__disable_irq(); // 禁用全局中断
|
||||
|
||||
HAL_StatusTypeDef status = bsp_Flash_STMFLASH_Write(BSP_FLASH_DATASAVE_ADDR,&Usr_Flash.FlashData,sizeof(bsp_FlashData_t));
|
||||
|
||||
if(status == HAL_OK)
|
||||
{
|
||||
// 写入成功,更新临时数据
|
||||
memcpy(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
__enable_irq(); // 恢复中断
|
||||
Wdg.Feed();
|
||||
}
|
||||
}
|
||||
|
||||
static void bsp_FlashDataRead(void)
|
||||
{
|
||||
Wdg.Feed();
|
||||
bsp_Flash_STMFLASH_Read(BSP_FLASH_DATASAVE_ADDR,
|
||||
&Usr_Flash.FlashData,
|
||||
sizeof(bsp_FlashData_t));
|
||||
Wdg.Feed();
|
||||
}
|
||||
Reference in New Issue
Block a user