249 lines
7.9 KiB
C
249 lines
7.9 KiB
C
#include "bsp_Flash.h"
|
|
#include "string.h"
|
|
#include "bsp_Wdg.h"
|
|
#include "bsp_W5500.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 = 5000;
|
|
Usr_Flash.FlashData.IP_Addr[0] = 192;
|
|
Usr_Flash.FlashData.IP_Addr[1] = 168;
|
|
Usr_Flash.FlashData.IP_Addr[2] = 0;
|
|
Usr_Flash.FlashData.IP_Addr[3] = 100;
|
|
|
|
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] = 255;
|
|
Usr_Flash.FlashData.Sub_Mask[3] = 0;
|
|
|
|
Usr_Flash.FlashData.Gateway_IP[0] = 192;
|
|
Usr_Flash.FlashData.Gateway_IP[1] = 168;
|
|
Usr_Flash.FlashData.Gateway_IP[2] = 0;
|
|
Usr_Flash.FlashData.Gateway_IP[3] = 1;
|
|
|
|
Usr_Flash.FlashData.Devic_Id = 0;
|
|
|
|
// /*设备型号 - 'Atlas Abatement' */
|
|
// Usr_Flash.FlashData.Device_ID[0] = ('A' << 8) | 't'; // 'At'
|
|
// Usr_Flash.FlashData.Device_ID[1] = ('l' << 8) | 'a'; // 'la'
|
|
// Usr_Flash.FlashData.Device_ID[2] = ('s' << 8) | ' '; // 's '
|
|
// Usr_Flash.FlashData.Device_ID[3] = ('A' << 8) | 'b'; // 'Ab'
|
|
// Usr_Flash.FlashData.Device_ID[4] = ('a' << 8) | 't'; // 'at'
|
|
// Usr_Flash.FlashData.Device_ID[5] = ('e' << 8) | 'm'; // 'em'
|
|
// Usr_Flash.FlashData.Device_ID[6] = ('e' << 8) | 'n'; // 'en'
|
|
// Usr_Flash.FlashData.Device_ID[7] = ('t' << 8) | 0x00; // 't\0'
|
|
//
|
|
// /*设备标识 - 'AHTWA07CHAAC' */
|
|
// Usr_Flash.FlashData.Device_Model[0] = ('A' << 8) | 'H'; // 'AH'
|
|
// Usr_Flash.FlashData.Device_Model[1] = ('T' << 8) | 'W'; // 'TW'
|
|
// Usr_Flash.FlashData.Device_Model[2] = ('A' << 8) | '0'; // 'A0'
|
|
// Usr_Flash.FlashData.Device_Model[3] = ('7' << 8) | 'C'; // '7C'
|
|
// Usr_Flash.FlashData.Device_Model[4] = ('H' << 8) | 'A'; // 'HA'
|
|
// Usr_Flash.FlashData.Device_Model[5] = ('A' << 8) | 'C'; // 'AC'
|
|
//
|
|
// Usr_Flash.FlashData.Data_ID1 = 0;
|
|
// Usr_Flash.FlashData.Collection_event_ID1 = 111101; // 文档中的CEID
|
|
//// Usr_Flash.FlashData.Report_ID1 = 2329004; // 文档中的Report ID
|
|
// Usr_Flash.FlashData.Alarm_ID = 2320614; // 文档中的AlarmID
|
|
//
|
|
// /* 报警类型初始化 */
|
|
// const char *alarm_types[] = {"Major Warning", "Minor Warning", "Major Alarm", "Minor Alarm"};
|
|
// for(int i = 0; i < 4; i++) {
|
|
// const char *type = alarm_types[i];
|
|
// for(int j = 0; j < 13 && type[j]; j++) {
|
|
// if(j % 2 == 0) {
|
|
// Usr_Flash.FlashData.Alarm_Type[i*2 + j/2] = (type[j] << 8) | (type[j+1] ? type[j+1] : 0);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
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();
|
|
}
|
|
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];
|
|
|
|
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();
|
|
}
|