Initial commit: my SECS2 project
This commit is contained in:
248
usr/bsp/bsp_Flash.c
Normal file
248
usr/bsp/bsp_Flash.c
Normal file
@@ -0,0 +1,248 @@
|
||||
#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,
|
||||
};
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ҳ
|
||||
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);
|
||||
}
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> - <20><>32λ<32><CEBB>ȡ
|
||||
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;
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>32λ<32><CEBB>
|
||||
for(uint32_t i = 0; i < words; i++)
|
||||
{
|
||||
*((uint32_t*)pBuf) = addr[i];
|
||||
pBuf += 4;
|
||||
}
|
||||
|
||||
// <20><>ȡʣ<C8A1><CAA3><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <20><>32λд<CEBB><D0B4>
|
||||
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();
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>ҳ
|
||||
status = bsp_FLASH_ErasePage(WriteAddr);
|
||||
if(status != HAL_OK)
|
||||
{
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
// д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>32λ<32><CEBB>
|
||||
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;
|
||||
}
|
||||
|
||||
// д<><D0B4>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
||||
if(status == HAL_OK && bytes_remaining > 0)
|
||||
{
|
||||
uint32_t last_word = 0xFFFFFFFF; // Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0xFF
|
||||
uint8_t *last_bytes = (uint8_t*)&last_word;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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;
|
||||
|
||||
// /*<2A>豸<EFBFBD>ͺ<EFBFBD> - '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'
|
||||
//
|
||||
// /*<2A>豸<EFBFBD><E8B1B8>ʶ - '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; // <20>ĵ<EFBFBD><C4B5>е<EFBFBD>CEID
|
||||
//// Usr_Flash.FlashData.Report_ID1 = 2329004; // <20>ĵ<EFBFBD><C4B5>е<EFBFBD>Report ID
|
||||
// Usr_Flash.FlashData.Alarm_ID = 2320614; // <20>ĵ<EFBFBD><C4B5>е<EFBFBD>AlarmID
|
||||
//
|
||||
// /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ʼ<EFBFBD><CABC> */
|
||||
// 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)
|
||||
{
|
||||
/*<2A><>ֹ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>д<EFBFBD><D0B4>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>*/
|
||||
if(memcmp(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t)) != 0)
|
||||
{
|
||||
Wdg.Feed();
|
||||
|
||||
__disable_irq(); // <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>ж<EFBFBD>
|
||||
|
||||
HAL_StatusTypeDef status = bsp_Flash_STMFLASH_Write(BSP_FLASH_DATASAVE_ADDR,&Usr_Flash.FlashData,sizeof(bsp_FlashData_t));
|
||||
|
||||
if(status == HAL_OK)
|
||||
{
|
||||
// д<><D0B4><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
||||
memcpy(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
__enable_irq(); // <20>ָ<EFBFBD><D6B8>ж<EFBFBD>
|
||||
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