From 01d4238731d164a6af4bb0bd25c78e3ca5f6a489 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Tue, 2 Jan 2024 11:56:16 +0800 Subject: [PATCH] update --- .settings/stm32cubeide.project.prefs | 5 +- .vscode/settings.json | 3 +- LWIP/Target/ethernetif.c | 10 +++- STM32F407VETX_FLASH.ld | 2 +- iflytop_xsync/xs_flash.c | 8 ++- iflytop_xsync/xs_flash.h | 2 +- usrc/base_service/config_service.c | 10 ++-- usrc/main.cpp | 9 ++-- usrc/project_configs.h | 1 - usrc/service/network_service.c | 100 +++++++++++++++++++++++++++++++++++ usrc/service/network_service.h | 9 ++++ xsync_stm32 Debug.launch | 6 +-- 12 files changed, 145 insertions(+), 20 deletions(-) create mode 100644 usrc/service/network_service.c create mode 100644 usrc/service/network_service.h diff --git a/.settings/stm32cubeide.project.prefs b/.settings/stm32cubeide.project.prefs index 79688aa..5dbf50e 100644 --- a/.settings/stm32cubeide.project.prefs +++ b/.settings/stm32cubeide.project.prefs @@ -1,5 +1,6 @@ +2F62501ED4689FB349E356AB974DBE57=E20EF8A1CFA8D2AA5E7713614514A9E1 635E684B79701B039C64EA45C3F84D30=C8B026EBE17C208F17FB66CE4235156C 66BE74F758C12D739921AEA421D593D3=1 -8DF89ED150041C4CBC7CB9A9CAA90856=31CD5EEFA9F35C65D8E334D24F421EB1 -DC22A860405A8BF2F2C095E5B6529F12=31CD5EEFA9F35C65D8E334D24F421EB1 +8DF89ED150041C4CBC7CB9A9CAA90856=E20EF8A1CFA8D2AA5E7713614514A9E1 +DC22A860405A8BF2F2C095E5B6529F12=071C30CF380DE7D03307EB9085476C68 eclipse.preferences.version=1 diff --git a/.vscode/settings.json b/.vscode/settings.json index b68d722..233b714 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -84,7 +84,8 @@ "zflash.h": "c", "iflytop_xsync.h": "c", "config.h": "c", - "config_service.h": "c" + "config_service.h": "c", + "base_service.h": "c" }, "files.autoGuessEncoding": false, "files.encoding": "gbk" diff --git a/LWIP/Target/ethernetif.c b/LWIP/Target/ethernetif.c index 289d428..fcacc12 100644 --- a/LWIP/Target/ethernetif.c +++ b/LWIP/Target/ethernetif.c @@ -166,6 +166,8 @@ void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *handlerEth) } /* USER CODE BEGIN 4 */ +// add by zhaohe +extern uint8_t g_mac[]; /* USER CODE END 4 */ @@ -202,7 +204,13 @@ static void low_level_init(struct netif *netif) heth.Init.RxBuffLen = 1536; /* USER CODE BEGIN MACADDRESS */ - + // add by zhaohe + // MACAddr[0] = g_mac[0]; + // MACAddr[1] = g_mac[1]; + // MACAddr[2] = g_mac[2]; + // MACAddr[3] = g_mac[3]; + // MACAddr[4] = g_mac[4]; + // MACAddr[5] = g_mac[5]; /* USER CODE END MACADDRESS */ hal_eth_init_status = HAL_ETH_Init(&heth); diff --git a/STM32F407VETX_FLASH.ld b/STM32F407VETX_FLASH.ld index d7d6fa5..d2fe0f0 100644 --- a/STM32F407VETX_FLASH.ld +++ b/STM32F407VETX_FLASH.ld @@ -47,7 +47,7 @@ MEMORY { CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K - FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K + FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 384K } /* Sections */ diff --git a/iflytop_xsync/xs_flash.c b/iflytop_xsync/xs_flash.c index a6ff809..2f929ff 100644 --- a/iflytop_xsync/xs_flash.c +++ b/iflytop_xsync/xs_flash.c @@ -1,9 +1,11 @@ #include "xs_flash.h" -#include "xs_log.h" + #include #include #include +#include "xs_log.h" + static uint32_t* _rawstartadd; static uint32_t* _defaultdata; static uint32_t _rawsize; @@ -54,7 +56,7 @@ void xs_flash_init(uint32_t* rawstartadd, uint32_t* defaultdata, uint32_t rawsiz _rawsize = rawsize; // 读取flash数据 - memcpy(_rawstartadd, (uint32_t*)(FLASH_START_ADD), _rawsize); + memcpy(_rawstartadd, (uint32_t*)(FLASH_START_ADD), _rawsize * 4); // 校验数据 if (_xs_check_raw_data()) { @@ -82,6 +84,7 @@ bool xs_flash_factory_reset(void) { } bool xs_flash_flush(void) { +#if PC_NVS_ENABLE _rawstartadd[0] = FLASH_MASK_VAL; _rawstartadd[_rawsize - 1] = xs_compute_checksum(_rawstartadd, _rawsize - 1); @@ -95,5 +98,6 @@ bool xs_flash_flush(void) { } } HAL_FLASH_Lock(); // 上锁 +#endif return true; } diff --git a/iflytop_xsync/xs_flash.h b/iflytop_xsync/xs_flash.h index 5521eea..f96f666 100644 --- a/iflytop_xsync/xs_flash.h +++ b/iflytop_xsync/xs_flash.h @@ -25,7 +25,7 @@ */ #define FLASH_MASK_VAL 0xABCD -#define FLASH_START_ADD 0x08078000 // +#define FLASH_START_ADD 0x08060000 // #define FLASH_EARSE_SECTOR FLASH_SECTOR_7 /** diff --git a/usrc/base_service/config_service.c b/usrc/base_service/config_service.c index 37eee53..0342788 100644 --- a/usrc/base_service/config_service.c +++ b/usrc/base_service/config_service.c @@ -25,11 +25,13 @@ static void create_default_config() { // static mac_t mac; xs_id_generate_random_mac(&mac); memcpy(&_default_val_config.mac[0], mac.mac, 6); - ZLOGD(TAG, "random mac is %02x:%02x:%02x:%02x:%02x:%02x", mac.mac[0], mac.mac[1], mac.mac[2], mac.mac[3], mac.mac[4], mac.mac[5]); - dump_config(&_default_val_config); + // ZLOGD(TAG, "random mac is %02x:%02x:%02x:%02x:%02x:%02x", mac.mac[0], mac.mac[1], mac.mac[2], mac.mac[3], mac.mac[4], mac.mac[5]); + // dump_config(&_default_val_config); } void config_init(void) { + ZLOGI(TAG, "config_init"); + /** * @brief 构造默认配置 */ @@ -39,8 +41,8 @@ void config_init(void) { */ xs_flash_init(&_config, &_default_val_config, sizeof(config_t) / 4); - ZLOGI(TAG, "xs_flash_is_first_run: %d", xs_flash_is_first_run()); - ZLOGI(TAG, ""); + // ZLOGI(TAG, "xs_flash_is_first_run: %d", xs_flash_is_first_run()); + // ZLOGI(TAG, ""); /** * @brief 打印配置信息 diff --git a/usrc/main.cpp b/usrc/main.cpp index 6267fb5..8443eff 100644 --- a/usrc/main.cpp +++ b/usrc/main.cpp @@ -3,6 +3,7 @@ // #include "base_service/base_service.h" +#include "service/network_service.h" // #define TAG "main" using namespace std; @@ -11,7 +12,6 @@ extern void umain(); extern "C" { extern void MX_LWIP_Init(void); void StartDefaultTask(void const* argument) { - // MX_LWIP_Init(); umain(); } } @@ -27,13 +27,14 @@ void umain() { * @brief 调试指示灯初始化 */ xs_gpio_init_as_output(&m_debug_led, PC_DEBUG_LIGHT_GPIO, kxs_gpio_nopull, false, false); - /** * @brief 配置初始化 */ - config_init(); - + /** + * @brief 初始化网络服务 + */ + network_service_init(); while (true) { xs_gpio_write(&m_debug_led, true); diff --git a/usrc/project_configs.h b/usrc/project_configs.h index efb9feb..a06f20e 100644 --- a/usrc/project_configs.h +++ b/usrc/project_configs.h @@ -17,4 +17,3 @@ #define PC_IRQ_PREEMPTPRIORITY_DEFAULT 5 #define PC_NVS_ENABLE 1 -#define PC_NVS_CONFIG_FLASH_SECTOR 8 diff --git a/usrc/service/network_service.c b/usrc/service/network_service.c new file mode 100644 index 0000000..ef74e07 --- /dev/null +++ b/usrc/service/network_service.c @@ -0,0 +1,100 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * File Name : LWIP.c + * Description : This file provides initialization code for LWIP + * middleWare. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "lwip.h" +#include "lwip/init.h" +#include "lwip/netif.h" +#if defined(__CC_ARM) +#include "lwip/sio.h" +#endif +#include "ethernetif.h" +// +#include "base_service/base_service.h" + +#define TAG "network" + +static struct netif gnetif; +static ip4_addr_t ipaddr; +static ip4_addr_t netmask; +static ip4_addr_t gw; +static uint8_t IP_ADDRESS[4]; +static uint8_t NETMASK_ADDRESS[4]; +static uint8_t GATEWAY_ADDRESS[4]; + +uint8_t g_mac[6]; + +static void ethernet_link_status_updated(struct netif *netif) { + if (netif_is_up(netif)) { + ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up"); + } else { + ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down"); + } +} + +void network_service_init() { + ZLOGI(TAG, "network_service_init"); + + *(uint32_t *)(IP_ADDRESS) = config_get()->ip; + *(uint32_t *)(NETMASK_ADDRESS) = config_get()->netmask; + *(uint32_t *)(GATEWAY_ADDRESS) = config_get()->gw; + + // IP_ADDRESS[0] = 192; + // IP_ADDRESS[1] = 168; + // IP_ADDRESS[2] = 8; + // IP_ADDRESS[3] = 10; + // NETMASK_ADDRESS[0] = 255; + // NETMASK_ADDRESS[1] = 255; + // NETMASK_ADDRESS[2] = 255; + // NETMASK_ADDRESS[3] = 255; + // GATEWAY_ADDRESS[0] = 192; + // GATEWAY_ADDRESS[1] = 168; + // GATEWAY_ADDRESS[2] = 8; + // GATEWAY_ADDRESS[3] = 1; + + memcpy(g_mac, config_get()->mac, 6); + + /* Initilialize the LwIP stack with RTOS */ + tcpip_init(NULL, NULL); + + /* IP addresses initialization without DHCP (IPv4) */ + IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]); + IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]); + IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]); + + /* add the network interface (IPv4/IPv6) with RTOS */ + netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input); + + /* Registers the default network interface */ + netif_set_default(&gnetif); + + if (netif_is_link_up(&gnetif)) { + /* When the netif is fully configured this function must be called */ + netif_set_up(&gnetif); + } else { + /* When the netif link is down this function must be called */ + netif_set_down(&gnetif); + } + + /* Set the link callback function, this function is called on change of link status*/ + netif_set_link_callback(&gnetif, ethernet_link_status_updated); + osThreadDef(EthLink, ethernet_link_thread, osPriorityBelowNormal, 0, 512); + osThreadCreate(osThread(EthLink), &gnetif); +} diff --git a/usrc/service/network_service.h b/usrc/service/network_service.h new file mode 100644 index 0000000..70ef48e --- /dev/null +++ b/usrc/service/network_service.h @@ -0,0 +1,9 @@ +#pragma once +#ifdef __cplusplus +extern "C" { +#endif +void network_service_init(); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/xsync_stm32 Debug.launch b/xsync_stm32 Debug.launch index 6e46268..632abef 100644 --- a/xsync_stm32 Debug.launch +++ b/xsync_stm32 Debug.launch @@ -33,11 +33,11 @@ - + - + - +