flash存储IP地址,存储在最后一个扇区

This commit is contained in:
王绪洁 2025-03-07 09:47:45 +08:00
parent c4cbbdcd38
commit e23fe78a83
10 changed files with 5868 additions and 5691 deletions

View File

@ -34,6 +34,7 @@
#include "ad7124.h"
#include "ht1200m.h"
#include "uart_lcd.h"
#include "user_flash.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -215,9 +216,11 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
// ip地址修改处理
IP4_ADDR(&ipaddr, usb_uart1.rx_data_temp[0], usb_uart1.rx_data_temp[1], usb_uart1.rx_data_temp[2], usb_uart1.rx_data_temp[3]);
gnetif.ip_addr = ipaddr;
// netif_set_ipaddr(&gnetif, &ipaddr);
if (flash_write_data(FLASH_USER_START_ADDR, usb_uart1.rx_data_temp, Size) == HAL_OK)
{
uart_lcd_draw_ipaddr();
}
}
if (huart == &huart4)
{
__HAL_UNLOCK(huart);

View File

@ -100,8 +100,8 @@ typedef struct
* @{
*/
#define FLASH_TYPEPROGRAM_BYTE 0x00000000U /*!< Program byte (8-bit) at a specified address */
#define FLASH_TYPEPROGRAM_HALFWORD 0x00000001U /*!< Program a half-word (16-bit) at a specified address */
#define FLASH_TYPEPROGRAM_WORD 0x00000002U /*!< Program a word (32-bit) at a specified address */
#define FLASH_TYPEPROGRAM_HALFWORD 0x00000001U /*!< Program a half-word (16-bit) at a specified address */
#define FLASH_TYPEPROGRAM_DOUBLEWORD 0x00000003U /*!< Program a double word (64-bit) at a specified address */
/**
* @}

View File

@ -28,7 +28,7 @@
#include "ethernetif.h"
/* USER CODE BEGIN 0 */
#include "user_flash.h"
/* USER CODE END 0 */
/* Private function prototypes -----------------------------------------------*/
static void ethernet_link_status_updated(struct netif *netif);
@ -109,7 +109,12 @@ void MX_LWIP_Init(void)
/* USER CODE END H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
/* USER CODE BEGIN 3 */
flash_read_data(FLASH_USER_START_ADDR, IP_ADDRESS, 4);
if (IP_ADDRESS[0] == 192)
{
IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
gnetif.ip_addr = ipaddr;
}
/* USER CODE END 3 */
}
@ -211,4 +216,3 @@ u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
return recved_bytes;
}
#endif /* MDK ARM Compiler */

View File

@ -0,0 +1,48 @@
// File: STM32F405_415_407_417_427_437_429_439.dbgconf
// Version: 1.0.0
// Note: refer to STM32F405/415 STM32F407/417 STM32F427/437 STM32F429/439 reference manual (RM0090)
// refer to STM32F40x STM32F41x datasheets
// refer to STM32F42x STM32F43x datasheets
// <<< Use Configuration Wizard in Context Menu >>>
// <h> Debug MCU configuration register (DBGMCU_CR)
// <o.2> DBG_STANDBY <i> Debug Standby Mode
// <o.1> DBG_STOP <i> Debug Stop Mode
// <o.0> DBG_SLEEP <i> Debug Sleep Mode
// </h>
DbgMCU_CR = 0x00000007;
// <h> Debug MCU APB1 freeze register (DBGMCU_APB1_FZ)
// <i> Reserved bits must be kept at reset value
// <o.26> DBG_CAN2_STOP <i> CAN2 stopped when core is halted
// <o.25> DBG_CAN1_STOP <i> CAN2 stopped when core is halted
// <o.23> DBG_I2C3_SMBUS_TIMEOUT <i> I2C3 SMBUS timeout mode stopped when core is halted
// <o.22> DBG_I2C2_SMBUS_TIMEOUT <i> I2C2 SMBUS timeout mode stopped when core is halted
// <o.21> DBG_I2C1_SMBUS_TIMEOUT <i> I2C1 SMBUS timeout mode stopped when core is halted
// <o.12> DBG_IWDG_STOP <i> Independent watchdog stopped when core is halted
// <o.11> DBG_WWDG_STOP <i> Window watchdog stopped when core is halted
// <o.10> DBG_RTC_STOP <i> RTC stopped when core is halted
// <o.8> DBG_TIM14_STOP <i> TIM14 counter stopped when core is halted
// <o.7> DBG_TIM13_STOP <i> TIM13 counter stopped when core is halted
// <o.6> DBG_TIM12_STOP <i> TIM12 counter stopped when core is halted
// <o.5> DBG_TIM7_STOP <i> TIM7 counter stopped when core is halted
// <o.4> DBG_TIM6_STOP <i> TIM6 counter stopped when core is halted
// <o.3> DBG_TIM5_STOP <i> TIM5 counter stopped when core is halted
// <o.2> DBG_TIM4_STOP <i> TIM4 counter stopped when core is halted
// <o.1> DBG_TIM3_STOP <i> TIM3 counter stopped when core is halted
// <o.0> DBG_TIM2_STOP <i> TIM2 counter stopped when core is halted
// </h>
DbgMCU_APB1_Fz = 0x00000000;
// <h> Debug MCU APB2 freeze register (DBGMCU_APB2_FZ)
// <i> Reserved bits must be kept at reset value
// <o.18> DBG_TIM11_STOP <i> TIM11 counter stopped when core is halted
// <o.17> DBG_TIM10_STOP <i> TIM10 counter stopped when core is halted
// <o.16> DBG_TIM9_STOP <i> TIM9 counter stopped when core is halted
// <o.1> DBG_TIM8_STOP <i> TIM8 counter stopped when core is halted
// <o.0> DBG_TIM1_STOP <i> TIM1 counter stopped when core is halted
// </h>
DbgMCU_APB2_Fz = 0x00000000;
// <<< end of configuration section >>>

View File

@ -145,59 +145,10 @@
<SetRegEntry>
<Number>0</Number>
<Key>ST-LINKIII-KEIL_SWO</Key>
<Name>-U090F00028316303030303032 -O2254 -SF1000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC800 -FN1 -FF0STM32F4xx_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F407VGTx$CMSIS\Flash\STM32F4xx_1024.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2</Name>
<Name>-U090F00028316303030303032 -O2254 -SF1000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC800 -FN1 -FF0STM32F4xx_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F407VGTx$CMSIS\Flash\STM32F4xx_1024.FLM)</Name>
</SetRegEntry>
</TargetDriverDllRegistry>
<Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>216</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134232022</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>../Core/Src/main.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\semi_finished_product_testing\../Core/Src/main.c\216</Expression>
</Bp>
<Bp>
<Number>1</Number>
<Type>0</Type>
<LineNumber>481</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134260196</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>../Middlewares/Third_Party/LwIP/src/core/netif.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\semi_finished_product_testing\../Middlewares/Third_Party/LwIP/src/core/netif.c\481</Expression>
</Bp>
<Bp>
<Number>2</Number>
<Type>0</Type>
<LineNumber>384</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134256024</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>../LWIP/Target/ethernetif.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\semi_finished_product_testing\../LWIP/Target/ethernetif.c\384</Expression>
</Bp>
</Breakpoint>
<Breakpoint/>
<WatchWindow1>
<Ww>
<count>0</count>
@ -282,7 +233,7 @@
<Ww>
<count>16</count>
<WinNumber>1</WinNumber>
<ItemText>IP_ADDRESS,0x0A</ItemText>
<ItemText>IP_ADDRESS,0x10</ItemText>
</Ww>
<Ww>
<count>17</count>
@ -344,7 +295,7 @@
<Mm>
<WinNumber>1</WinNumber>
<SubType>0</SubType>
<ItemText>\\semi_finished_product_testing\../LWIP/Target/ethernetif.c\heth.Init.MACAddr</ItemText>
<ItemText>0x080E0000</ItemText>
<AccSizeX>0</AccSizeX>
</Mm>
</MemoryWindow1>
@ -2106,7 +2057,7 @@
<Group>
<GroupName>User/system</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -2134,6 +2085,18 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>13</GroupNumber>
<FileNumber>137</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\User\system\user_flash.c</PathWithFileName>
<FilenameWithoutPath>user_flash.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>

View File

@ -16,8 +16,8 @@
<TargetCommonOption>
<Device>STM32F407VGTx</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32F4xx_DFP.2.16.0</PackID>
<PackURL>http://www.keil.com/pack/</PackURL>
<PackID>Keil.STM32F4xx_DFP.2.17.1</PackID>
<PackURL>https://www.keil.com/pack/</PackURL>
<Cpu>IRAM(0x20000000-0x2001BFFF) IRAM2(0x2001C000-0x2001FFFF) IROM(0x8000000-0x80FFFFF) CLOCK(25000000) FPU2 CPUTYPE("Cortex-M4") TZ</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
@ -186,7 +186,6 @@
<RvdsVP>2</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<nBranchProt>0</nBranchProt>
<hadIRAM2>1</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -1124,6 +1123,11 @@
<FileType>1</FileType>
<FilePath>..\User\system\user_spi.c</FilePath>
</File>
<File>
<FileName>user_flash.c</FileName>
<FileType>1</FileType>
<FilePath>..\User\system\user_flash.c</FilePath>
</File>
</Files>
</Group>
<Group>

53
User/system/user_flash.c Normal file
View File

@ -0,0 +1,53 @@
#include "user_flash.h"
/* 写入原始数据 ---------------------------------------------*/
HAL_StatusTypeDef flash_write_data(uint32_t addr, uint8_t *data, uint16_t size)
{
HAL_StatusTypeDef status = HAL_OK;
uint32_t i;
uint32_t write_addr = addr;
FLASH_EraseInitTypeDef erase_init;
uint32_t sector_error;
// 1. 解锁Flash
HAL_FLASH_Unlock();
// 2. 擦除目标扇区
erase_init.TypeErase = FLASH_TYPEERASE_SECTORS;
erase_init.Sector = FLASH_USER_SECTOR;
erase_init.NbSectors = 1; // 擦除1个扇区
erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_3; // 根据电压范围选择
// 执行擦除操作
if (HAL_FLASHEx_Erase(&erase_init, &sector_error) != HAL_OK)
{
status = HAL_ERROR;
goto exit;
}
// 3. 写入数据
for (i = 0; i < size; i += 4)
{ // 以32位4字节为单位写入
uint32_t data_to_write = *(uint32_t *)(data + i);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
write_addr,
data_to_write) != HAL_OK)
{
status = HAL_ERROR;
break;
}
write_addr += 4; // 地址递增
}
exit:
// 4. 锁定Flash
HAL_FLASH_Lock();
return status;
}
/* 读取原始数据 ---------------------------------------------*/
void flash_read_data(uint32_t addr, uint8_t *data, uint32_t size)
{
memcpy(data, (uint32_t *)addr, size);
}

15
User/system/user_flash.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __USER_FLASH_H__
#define __USER_FLASH_H__
#include "main.h"
/* Flash配置 -------------------------------------------------*/
#define FLASH_USER_START_ADDR 0x080E0000 // Sector11起始地址(128KB)
#define FLASH_USER_SECTOR FLASH_SECTOR_11
/* 函数声明 -------------------------------------------------*/
HAL_StatusTypeDef flash_write_data(uint32_t addr, uint8_t *data, uint16_t size);
void flash_read_data(uint32_t addr, uint8_t *data, uint32_t size);
#endif