From 0014061bda03d7e4d9b28e4eebaecb20400d5f24 Mon Sep 17 00:00:00 2001 From: kyz Date: Fri, 29 Mar 2024 18:27:49 +0800 Subject: [PATCH] add lwip --- Core/Inc/stm32h7xx_it.h | 1 + Core/Src/gpio.c | 2 +- Core/Src/main.c | 50 ++ Core/Src/stm32h7xx_it.c | 15 + LWIP/App/lwip.c | 197 ++++++ LWIP/App/lwip.h | 76 +++ LWIP/Target/ethernetif.c | 910 ++++++++++++++++++++++++++ LWIP/Target/ethernetif.h | 46 ++ LWIP/Target/lwipopts.h | 124 ++++ MDK-ARM/welding_controller_v2.uvprojx | 7 +- user/user.h | 2 + user/user_app.c | 23 +- user/user_debug.c | 1 + user/user_eth.c | 97 +++ user/user_flowmeter.c | 2 +- user/user_wifi.c | 13 +- welding_controller_v2.ioc | 28 +- 17 files changed, 1578 insertions(+), 16 deletions(-) create mode 100644 LWIP/App/lwip.c create mode 100644 LWIP/App/lwip.h create mode 100644 LWIP/Target/ethernetif.c create mode 100644 LWIP/Target/ethernetif.h create mode 100644 LWIP/Target/lwipopts.h create mode 100644 user/user_eth.c diff --git a/Core/Inc/stm32h7xx_it.h b/Core/Inc/stm32h7xx_it.h index 7940bec..59038c1 100644 --- a/Core/Inc/stm32h7xx_it.h +++ b/Core/Inc/stm32h7xx_it.h @@ -59,6 +59,7 @@ void USART1_IRQHandler(void); void UART4_IRQHandler(void); void UART5_IRQHandler(void); void TIM7_IRQHandler(void); +void ETH_IRQHandler(void); void OTG_HS_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 8e732c2..a74db6e 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -58,7 +58,7 @@ void MX_GPIO_Init(void) HAL_GPIO_WritePin(GPIOE, TWO_MOTOR_Pin|LCM_EN_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(ENET_RESET_GPIO_Port, ENET_RESET_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(ENET_RESET_GPIO_Port, ENET_RESET_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET); diff --git a/Core/Src/main.c b/Core/Src/main.c index b3a000c..8802609 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -61,6 +61,7 @@ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void PeriphCommonClock_Config(void); +static void MPU_Config(void); void MX_FREERTOS_Init(void); /* USER CODE BEGIN PFP */ @@ -84,6 +85,9 @@ int main(void) //在这里手动修改VTOR SCB->VTOR = 0x1000; /* USER CODE END 1 */ + + /* MPU Configuration--------------------------------------------------------*/ + MPU_Config(); /* Enable the CPU Cache */ /* Enable I-Cache---------------------------------------------------------*/ @@ -245,6 +249,52 @@ void PeriphCommonClock_Config(void) /* USER CODE END 4 */ +/* MPU Configuration */ + +void MPU_Config(void) +{ + MPU_Region_InitTypeDef MPU_InitStruct = {0}; + + /* Disables the MPU */ + HAL_MPU_Disable(); + + /** Initializes and configures the Region and the memory to be protected + */ + MPU_InitStruct.Enable = MPU_REGION_ENABLE; + MPU_InitStruct.Number = MPU_REGION_NUMBER0; + MPU_InitStruct.BaseAddress = 0x30000000; + MPU_InitStruct.Size = MPU_REGION_SIZE_16KB; + MPU_InitStruct.SubRegionDisable = 0x0; + MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1; + MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; + MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; + MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE; + MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; + MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; + + HAL_MPU_ConfigRegion(&MPU_InitStruct); + + /** Initializes and configures the Region and the memory to be protected + */ + MPU_InitStruct.Number = MPU_REGION_NUMBER1; + MPU_InitStruct.BaseAddress = 0x30004000; + + HAL_MPU_ConfigRegion(&MPU_InitStruct); + + /** Initializes and configures the Region and the memory to be protected + */ + MPU_InitStruct.Number = MPU_REGION_NUMBER2; + MPU_InitStruct.BaseAddress = 0x24000000; + MPU_InitStruct.Size = MPU_REGION_SIZE_128KB; + MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE; + MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE; + + HAL_MPU_ConfigRegion(&MPU_InitStruct); + /* Enables the MPU */ + HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT); + +} + /** * @brief Period elapsed callback in non blocking mode * @note This function is called when TIM7 interrupt took place, inside diff --git a/Core/Src/stm32h7xx_it.c b/Core/Src/stm32h7xx_it.c index 140a680..d6a9c6f 100644 --- a/Core/Src/stm32h7xx_it.c +++ b/Core/Src/stm32h7xx_it.c @@ -55,6 +55,7 @@ /* USER CODE END 0 */ /* External variables --------------------------------------------------------*/ +extern ETH_HandleTypeDef heth; extern PCD_HandleTypeDef hpcd_USB_OTG_HS; extern DMA_HandleTypeDef hdma_adc1; extern DMA_HandleTypeDef hdma_adc2; @@ -264,6 +265,20 @@ void TIM7_IRQHandler(void) /* USER CODE END TIM7_IRQn 1 */ } +/** + * @brief This function handles Ethernet global interrupt. + */ +void ETH_IRQHandler(void) +{ + /* USER CODE BEGIN ETH_IRQn 0 */ + + /* USER CODE END ETH_IRQn 0 */ + HAL_ETH_IRQHandler(&heth); + /* USER CODE BEGIN ETH_IRQn 1 */ + + /* USER CODE END ETH_IRQn 1 */ +} + /** * @brief This function handles USB On The Go HS global interrupt. */ diff --git a/LWIP/App/lwip.c b/LWIP/App/lwip.c new file mode 100644 index 0000000..022c378 --- /dev/null +++ b/LWIP/App/lwip.c @@ -0,0 +1,197 @@ +/* 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 ) /* MDK ARM Compiler */ +#include "lwip/sio.h" +#endif /* MDK ARM Compiler */ +#include "ethernetif.h" +#include + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/* Private function prototypes -----------------------------------------------*/ +static void ethernet_link_status_updated(struct netif *netif); +/* ETH Variables initialization ----------------------------------------------*/ +void Error_Handler(void); + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/* Variables Initialization */ +struct netif gnetif; +ip4_addr_t ipaddr; +ip4_addr_t netmask; +ip4_addr_t gw; +/* USER CODE BEGIN OS_THREAD_ATTR_CMSIS_RTOS_V2 */ +#define INTERFACE_THREAD_STACK_SIZE ( 1024 ) +osThreadAttr_t attributes; +/* USER CODE END OS_THREAD_ATTR_CMSIS_RTOS_V2 */ + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ + +/** + * LwIP initialization function + */ +void MX_LWIP_Init(void) +{ + /* Initilialize the LwIP stack with RTOS */ + tcpip_init( NULL, NULL ); + + /* IP addresses initialization with DHCP (IPv4) */ + ipaddr.addr = 0; + netmask.addr = 0; + gw.addr = 0; + + /* 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); + + /* We must always bring the network interface up connection or not... */ + netif_set_up(&gnetif); + + /* Set the link callback function, this function is called on change of link status*/ + netif_set_link_callback(&gnetif, ethernet_link_status_updated); + + /* Create the Ethernet link handler thread */ +/* USER CODE BEGIN H7_OS_THREAD_NEW_CMSIS_RTOS_V2 */ + memset(&attributes, 0x0, sizeof(osThreadAttr_t)); + attributes.name = "EthLink"; + attributes.stack_size = INTERFACE_THREAD_STACK_SIZE; + attributes.priority = osPriorityBelowNormal; + osThreadNew(ethernet_link_thread, &gnetif, &attributes); +/* USER CODE END H7_OS_THREAD_NEW_CMSIS_RTOS_V2 */ + + /* Start DHCP negotiation for a network interface (IPv4) */ + dhcp_start(&gnetif); + +/* USER CODE BEGIN 3 */ + +/* USER CODE END 3 */ +} + +#ifdef USE_OBSOLETE_USER_CODE_SECTION_4 +/* Kept to help code migration. (See new 4_1, 4_2... sections) */ +/* Avoid to use this user section which will become obsolete. */ +/* USER CODE BEGIN 4 */ +/* USER CODE END 4 */ +#endif + +/** + * @brief Notify the User about the network interface config status + * @param netif: the network interface + * @retval None + */ +static void ethernet_link_status_updated(struct netif *netif) +{ + if (netif_is_up(netif)) + { +/* USER CODE BEGIN 5 */ +/* USER CODE END 5 */ + } + else /* netif is down */ + { +/* USER CODE BEGIN 6 */ +/* USER CODE END 6 */ + } +} + +#if defined ( __CC_ARM ) /* MDK ARM Compiler */ +/** + * Opens a serial device for communication. + * + * @param devnum device number + * @return handle to serial device if successful, NULL otherwise + */ +sio_fd_t sio_open(u8_t devnum) +{ + sio_fd_t sd; + +/* USER CODE BEGIN 7 */ + sd = 0; // dummy code +/* USER CODE END 7 */ + + return sd; +} + +/** + * Sends a single character to the serial device. + * + * @param c character to send + * @param fd serial device handle + * + * @note This function will block until the character can be sent. + */ +void sio_send(u8_t c, sio_fd_t fd) +{ +/* USER CODE BEGIN 8 */ +/* USER CODE END 8 */ +} + +/** + * Reads from the serial device. + * + * @param fd serial device handle + * @param data pointer to data buffer for receiving + * @param len maximum length (in bytes) of data to receive + * @return number of bytes actually received - may be 0 if aborted by sio_read_abort + * + * @note This function will block until data can be received. The blocking + * can be cancelled by calling sio_read_abort(). + */ +u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) +{ + u32_t recved_bytes; + +/* USER CODE BEGIN 9 */ + recved_bytes = 0; // dummy code +/* USER CODE END 9 */ + return recved_bytes; +} + +/** + * Tries to read from the serial device. Same as sio_read but returns + * immediately if no data is available and never blocks. + * + * @param fd serial device handle + * @param data pointer to data buffer for receiving + * @param len maximum length (in bytes) of data to receive + * @return number of bytes actually received + */ +u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len) +{ + u32_t recved_bytes; + +/* USER CODE BEGIN 10 */ + recved_bytes = 0; // dummy code +/* USER CODE END 10 */ + return recved_bytes; +} +#endif /* MDK ARM Compiler */ + diff --git a/LWIP/App/lwip.h b/LWIP/App/lwip.h new file mode 100644 index 0000000..b14adc0 --- /dev/null +++ b/LWIP/App/lwip.h @@ -0,0 +1,76 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * File Name : LWIP.h + * Description : This file provides code for the configuration + * of the LWIP. + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __mx_lwip_H +#define __mx_lwip_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "lwip/opt.h" +#include "lwip/mem.h" +#include "lwip/memp.h" +#include "netif/etharp.h" +#include "lwip/dhcp.h" +#include "lwip/netif.h" +#include "lwip/timeouts.h" +#include "ethernetif.h" + +/* Includes for RTOS ---------------------------------------------------------*/ +#if WITH_RTOS +#include "lwip/tcpip.h" +#endif /* WITH_RTOS */ + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* Global Variables ----------------------------------------------------------*/ +extern ETH_HandleTypeDef heth; + +/* LWIP init function */ +void MX_LWIP_Init(void); + +#if !WITH_RTOS +/* USER CODE BEGIN 1 */ +/* Function defined in lwip.c to: + * - Read a received packet from the Ethernet buffers + * - Send it to the lwIP stack for handling + * - Handle timeouts if NO_SYS_NO_TIMERS not set + */ +void MX_LWIP_Process(void); + +/* USER CODE END 1 */ +#endif /* WITH_RTOS */ + +#ifdef __cplusplus +} +#endif +#endif /*__ mx_lwip_H */ + +/** + * @} + */ + +/** + * @} + */ diff --git a/LWIP/Target/ethernetif.c b/LWIP/Target/ethernetif.c new file mode 100644 index 0000000..33023e8 --- /dev/null +++ b/LWIP/Target/ethernetif.c @@ -0,0 +1,910 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * File Name : ethernetif.c + * Description : This file provides code for the configuration + * of the ethernetif.c 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 "main.h" +#include "lwip/opt.h" +#include "lwip/timeouts.h" +#include "netif/ethernet.h" +#include "netif/etharp.h" +#include "lwip/ethip6.h" +#include "ethernetif.h" +#include "lan8742.h" +#include +#include "cmsis_os.h" +#include "lwip/tcpip.h" + +/* Within 'USER CODE' section, code will be kept by default at each generation */ +/* USER CODE BEGIN 0 */ +#include "user.h" +/* USER CODE END 0 */ + +/* Private define ------------------------------------------------------------*/ +/* The time to block waiting for input. */ +#define TIME_WAITING_FOR_INPUT ( portMAX_DELAY ) +/* USER CODE BEGIN OS_THREAD_STACK_SIZE_WITH_RTOS */ +/* Stack size of the interface thread */ +#define INTERFACE_THREAD_STACK_SIZE ( 350 ) +/* USER CODE END OS_THREAD_STACK_SIZE_WITH_RTOS */ +/* Network interface name */ +#define IFNAME0 's' +#define IFNAME1 't' + +/* ETH Setting */ +#define ETH_DMA_TRANSMIT_TIMEOUT ( 20U ) +#define ETH_TX_BUFFER_MAX ((ETH_TX_DESC_CNT) * 2U) +/* ETH_RX_BUFFER_SIZE parameter is defined in lwipopts.h */ + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/* Private variables ---------------------------------------------------------*/ +/* +@Note: This interface is implemented to operate in zero-copy mode only: + - Rx buffers will be allocated from LwIP stack memory heap, + then passed to ETH HAL driver. + - Tx buffers will be allocated from LwIP stack memory heap, + then passed to ETH HAL driver. + +@Notes: + 1.a. ETH DMA Rx descriptors must be contiguous, the default count is 4, + to customize it please redefine ETH_RX_DESC_CNT in ETH GUI (Rx Descriptor Length) + so that updated value will be generated in stm32xxxx_hal_conf.h + 1.b. ETH DMA Tx descriptors must be contiguous, the default count is 4, + to customize it please redefine ETH_TX_DESC_CNT in ETH GUI (Tx Descriptor Length) + so that updated value will be generated in stm32xxxx_hal_conf.h + + 2.a. Rx Buffers number must be between ETH_RX_DESC_CNT and 2*ETH_RX_DESC_CNT + 2.b. Rx Buffers must have the same size: ETH_RX_BUFFER_SIZE, this value must + passed to ETH DMA in the init field (heth.Init.RxBuffLen) + 2.c The RX Ruffers addresses and sizes must be properly defined to be aligned + to L1-CACHE line size (32 bytes). +*/ + +/* Data Type Definitions */ +typedef enum +{ + RX_ALLOC_OK = 0x00, + RX_ALLOC_ERROR = 0x01 +} RxAllocStatusTypeDef; + +typedef struct +{ + struct pbuf_custom pbuf_custom; + uint8_t buff[(ETH_RX_BUFFER_SIZE + 31) & ~31] __ALIGNED(32); +} RxBuff_t; + +/* Memory Pool Declaration */ +#define ETH_RX_BUFFER_CNT 12U +LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool"); + +/* Variable Definitions */ +static uint8_t RxAllocStatus; + +#if defined ( __ICCARM__ ) /*!< IAR Compiler */ + +#pragma location=0x30000000 +ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */ +#pragma location=0x30000200 +ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */ + +#elif defined ( __CC_ARM ) /* MDK ARM Compiler */ + +__attribute__((at(0x30000000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */ +__attribute__((at(0x30000200))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */ + +#elif defined ( __GNUC__ ) /* GNU Compiler */ + +ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */ +ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */ + +#endif + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ + +osSemaphoreId RxPktSemaphore = NULL; /* Semaphore to signal incoming packets */ +osSemaphoreId TxPktSemaphore = NULL; /* Semaphore to signal transmit packet complete */ + +/* Global Ethernet handle */ +ETH_HandleTypeDef heth; +ETH_TxPacketConfig TxConfig; + +/* Private function prototypes -----------------------------------------------*/ +int32_t ETH_PHY_IO_Init(void); +int32_t ETH_PHY_IO_DeInit (void); +int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal); +int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal); +int32_t ETH_PHY_IO_GetTick(void); + +lan8742_Object_t LAN8742; +lan8742_IOCtx_t LAN8742_IOCtx = {ETH_PHY_IO_Init, + ETH_PHY_IO_DeInit, + ETH_PHY_IO_WriteReg, + ETH_PHY_IO_ReadReg, + ETH_PHY_IO_GetTick}; + +/* USER CODE BEGIN 3 */ + +/* USER CODE END 3 */ + +/* Private functions ---------------------------------------------------------*/ +void pbuf_free_custom(struct pbuf *p); + +/** + * @brief Ethernet Rx Transfer completed callback + * @param handlerEth: ETH handler + * @retval None + */ +void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *handlerEth) +{ + osSemaphoreRelease(RxPktSemaphore); +} +/** + * @brief Ethernet Tx Transfer completed callback + * @param handlerEth: ETH handler + * @retval None + */ +void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *handlerEth) +{ + osSemaphoreRelease(TxPktSemaphore); +} +/** + * @brief Ethernet DMA transfer error callback + * @param handlerEth: ETH handler + * @retval None + */ +void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *handlerEth) +{ + if((HAL_ETH_GetDMAError(handlerEth) & ETH_DMACSR_RBU) == ETH_DMACSR_RBU) + { + osSemaphoreRelease(RxPktSemaphore); + } +} + +/* USER CODE BEGIN 4 */ + +/* USER CODE END 4 */ + +/******************************************************************************* + LL Driver Interface ( LwIP stack --> ETH) +*******************************************************************************/ +/** + * @brief In this function, the hardware should be initialized. + * Called from ethernetif_init(). + * + * @param netif the already initialized lwip network interface structure + * for this ethernetif + */ +static void low_level_init(struct netif *netif) +{ + HAL_StatusTypeDef hal_eth_init_status = HAL_OK; +/* USER CODE BEGIN OS_THREAD_ATTR_CMSIS_RTOS_V2 */ + osThreadAttr_t attributes; +/* USER CODE END OS_THREAD_ATTR_CMSIS_RTOS_V2 */ + uint32_t duplex, speed = 0; + int32_t PHYLinkState = 0; + ETH_MACConfigTypeDef MACConf = {0}; + /* Start ETH HAL Init */ + + uint8_t MACAddr[6] ; + heth.Instance = ETH; + MACAddr[0] = 0x00; + MACAddr[1] = 0x80; + MACAddr[2] = 0xE1; + MACAddr[3] = 0x00; + MACAddr[4] = 0x00; + MACAddr[5] = 0x00; + heth.Init.MACAddr = &MACAddr[0]; + heth.Init.MediaInterface = HAL_ETH_RMII_MODE; + heth.Init.TxDesc = DMATxDscrTab; + heth.Init.RxDesc = DMARxDscrTab; + heth.Init.RxBuffLen = 1536; + + /* USER CODE BEGIN MACADDRESS */ + + device_get_mac(heth.Init.MACAddr); + printf( "init mac addr: %02X:%02X:%02X:%02X:%02X:%02X\n", + MACAddr[0],MACAddr[1],MACAddr[2], + MACAddr[3],MACAddr[4],MACAddr[5] ); + + /* USER CODE END MACADDRESS */ + + hal_eth_init_status = HAL_ETH_Init(&heth); + + memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig)); + TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD; + TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC; + TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT; + + /* End ETH HAL Init */ + + /* Initialize the RX POOL */ + LWIP_MEMPOOL_INIT(RX_POOL); + +#if LWIP_ARP || LWIP_ETHERNET + + /* set MAC hardware address length */ + netif->hwaddr_len = ETH_HWADDR_LEN; + + /* set MAC hardware address */ + netif->hwaddr[0] = heth.Init.MACAddr[0]; + netif->hwaddr[1] = heth.Init.MACAddr[1]; + netif->hwaddr[2] = heth.Init.MACAddr[2]; + netif->hwaddr[3] = heth.Init.MACAddr[3]; + netif->hwaddr[4] = heth.Init.MACAddr[4]; + netif->hwaddr[5] = heth.Init.MACAddr[5]; + + /* maximum transfer unit */ + netif->mtu = ETH_MAX_PAYLOAD; + + /* Accept broadcast address and ARP traffic */ + /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ + #if LWIP_ARP + netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; + #else + netif->flags |= NETIF_FLAG_BROADCAST; + #endif /* LWIP_ARP */ + + /* create a binary semaphore used for informing ethernetif of frame reception */ + RxPktSemaphore = osSemaphoreNew(1, 0, NULL); + + /* create a binary semaphore used for informing ethernetif of frame transmission */ + TxPktSemaphore = osSemaphoreNew(1, 0, NULL); + + /* create the task that handles the ETH_MAC */ +/* USER CODE BEGIN OS_THREAD_NEW_CMSIS_RTOS_V2 */ + memset(&attributes, 0x0, sizeof(osThreadAttr_t)); + attributes.name = "EthIf"; + attributes.stack_size = INTERFACE_THREAD_STACK_SIZE; + attributes.priority = osPriorityRealtime; + osThreadNew(ethernetif_input, netif, &attributes); +/* USER CODE END OS_THREAD_NEW_CMSIS_RTOS_V2 */ + +/* USER CODE BEGIN PHY_PRE_CONFIG */ + +/* USER CODE END PHY_PRE_CONFIG */ + /* Set PHY IO functions */ + LAN8742_RegisterBusIO(&LAN8742, &LAN8742_IOCtx); + + /* Initialize the LAN8742 ETH PHY */ + LAN8742_Init(&LAN8742); + + if (hal_eth_init_status == HAL_OK) + { + PHYLinkState = LAN8742_GetLinkState(&LAN8742); + + /* Get link state */ + if(PHYLinkState <= LAN8742_STATUS_LINK_DOWN) + { + netif_set_link_down(netif); + netif_set_down(netif); + } + else + { + switch (PHYLinkState) + { + case LAN8742_STATUS_100MBITS_FULLDUPLEX: + duplex = ETH_FULLDUPLEX_MODE; + speed = ETH_SPEED_100M; + break; + case LAN8742_STATUS_100MBITS_HALFDUPLEX: + duplex = ETH_HALFDUPLEX_MODE; + speed = ETH_SPEED_100M; + break; + case LAN8742_STATUS_10MBITS_FULLDUPLEX: + duplex = ETH_FULLDUPLEX_MODE; + speed = ETH_SPEED_10M; + break; + case LAN8742_STATUS_10MBITS_HALFDUPLEX: + duplex = ETH_HALFDUPLEX_MODE; + speed = ETH_SPEED_10M; + break; + default: + duplex = ETH_FULLDUPLEX_MODE; + speed = ETH_SPEED_100M; + break; + } + + /* Get MAC Config MAC */ + HAL_ETH_GetMACConfig(&heth, &MACConf); + MACConf.DuplexMode = duplex; + MACConf.Speed = speed; + HAL_ETH_SetMACConfig(&heth, &MACConf); + + HAL_ETH_Start_IT(&heth); + netif_set_up(netif); + netif_set_link_up(netif); + +/* USER CODE BEGIN PHY_POST_CONFIG */ + +/* USER CODE END PHY_POST_CONFIG */ + } + + } + else + { + Error_Handler(); + } +#endif /* LWIP_ARP || LWIP_ETHERNET */ + +/* USER CODE BEGIN LOW_LEVEL_INIT */ + +/* USER CODE END LOW_LEVEL_INIT */ +} + +/** + * @brief This function should do the actual transmission of the packet. The packet is + * contained in the pbuf that is passed to the function. This pbuf + * might be chained. + * + * @param netif the lwip network interface structure for this ethernetif + * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) + * @return ERR_OK if the packet could be sent + * an err_t value if the packet couldn't be sent + * + * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to + * strange results. You might consider waiting for space in the DMA queue + * to become available since the stack doesn't retry to send a packet + * dropped because of memory failure (except for the TCP timers). + */ + +static err_t low_level_output(struct netif *netif, struct pbuf *p) +{ + uint32_t i = 0U; + struct pbuf *q = NULL; + err_t errval = ERR_OK; + ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT] = {0}; + + memset(Txbuffer, 0 , ETH_TX_DESC_CNT*sizeof(ETH_BufferTypeDef)); + + for(q = p; q != NULL; q = q->next) + { + if(i >= ETH_TX_DESC_CNT) + return ERR_IF; + + Txbuffer[i].buffer = q->payload; + Txbuffer[i].len = q->len; + + if(i>0) + { + Txbuffer[i-1].next = &Txbuffer[i]; + } + + if(q->next == NULL) + { + Txbuffer[i].next = NULL; + } + + i++; + } + + TxConfig.Length = p->tot_len; + TxConfig.TxBuffer = Txbuffer; + TxConfig.pData = p; + + pbuf_ref(p); + + if (HAL_ETH_Transmit_IT(&heth, &TxConfig) == HAL_OK) { + while(osSemaphoreAcquire(TxPktSemaphore, TIME_WAITING_FOR_INPUT)!=osOK) + + { + } + + HAL_ETH_ReleaseTxPacket(&heth); + } else { + pbuf_free(p); + } + + return errval; +} + +/** + * @brief Should allocate a pbuf and transfer the bytes of the incoming + * packet from the interface into the pbuf. + * + * @param netif the lwip network interface structure for this ethernetif + * @return a pbuf filled with the received packet (including MAC header) + * NULL on memory error + */ +static struct pbuf * low_level_input(struct netif *netif) +{ + struct pbuf *p = NULL; + + if(RxAllocStatus == RX_ALLOC_OK) + { + HAL_ETH_ReadData(&heth, (void **)&p); + } + + return p; +} + +/** + * @brief This function should be called when a packet is ready to be read + * from the interface. It uses the function low_level_input() that + * should handle the actual reception of bytes from the network + * interface. Then the type of the received packet is determined and + * the appropriate input function is called. + * + * @param netif the lwip network interface structure for this ethernetif + */ +void ethernetif_input(void* argument) +{ + struct pbuf *p = NULL; + struct netif *netif = (struct netif *) argument; + + for( ;; ) + { + if (osSemaphoreAcquire(RxPktSemaphore, TIME_WAITING_FOR_INPUT) == osOK) + { + do + { + p = low_level_input( netif ); + if (p != NULL) + { + if (netif->input( p, netif) != ERR_OK ) + { + pbuf_free(p); + } + } + } while(p!=NULL); + } + } +} + +#if !LWIP_ARP +/** + * This function has to be completed by user in case of ARP OFF. + * + * @param netif the lwip network interface structure for this ethernetif + * @return ERR_OK if ... + */ +static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) +{ + err_t errval; + errval = ERR_OK; + +/* USER CODE BEGIN 5 */ + +/* USER CODE END 5 */ + + return errval; + +} +#endif /* LWIP_ARP */ + +/** + * @brief Should be called at the beginning of the program to set up the + * network interface. It calls the function low_level_init() to do the + * actual setup of the hardware. + * + * This function should be passed as a parameter to netif_add(). + * + * @param netif the lwip network interface structure for this ethernetif + * @return ERR_OK if the loopif is initialized + * ERR_MEM if private data couldn't be allocated + * any other err_t on error + */ +err_t ethernetif_init(struct netif *netif) +{ + LWIP_ASSERT("netif != NULL", (netif != NULL)); + +#if LWIP_NETIF_HOSTNAME + /* Initialize interface hostname */ + netif->hostname = "lwip"; +#endif /* LWIP_NETIF_HOSTNAME */ + + /* + * Initialize the snmp variables and counters inside the struct netif. + * The last argument should be replaced with your link speed, in units + * of bits per second. + */ + // MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); + + netif->name[0] = IFNAME0; + netif->name[1] = IFNAME1; + /* We directly use etharp_output() here to save a function call. + * You can instead declare your own function an call etharp_output() + * from it if you have to do some checks before sending (e.g. if link + * is available...) */ + +#if LWIP_IPV4 +#if LWIP_ARP || LWIP_ETHERNET +#if LWIP_ARP + netif->output = etharp_output; +#else + /* The user should write its own code in low_level_output_arp_off function */ + netif->output = low_level_output_arp_off; +#endif /* LWIP_ARP */ +#endif /* LWIP_ARP || LWIP_ETHERNET */ +#endif /* LWIP_IPV4 */ + +#if LWIP_IPV6 + netif->output_ip6 = ethip6_output; +#endif /* LWIP_IPV6 */ + + netif->linkoutput = low_level_output; + + /* initialize the hardware */ + low_level_init(netif); + + return ERR_OK; +} + +/** + * @brief Custom Rx pbuf free callback + * @param pbuf: pbuf to be freed + * @retval None + */ +void pbuf_free_custom(struct pbuf *p) +{ + struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p; + LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf); + + /* If the Rx Buffer Pool was exhausted, signal the ethernetif_input task to + * call HAL_ETH_GetRxDataBuffer to rebuild the Rx descriptors. */ + + if (RxAllocStatus == RX_ALLOC_ERROR) + { + RxAllocStatus = RX_ALLOC_OK; + osSemaphoreRelease(RxPktSemaphore); + } +} + +/* USER CODE BEGIN 6 */ + +/** +* @brief Returns the current time in milliseconds +* when LWIP_TIMERS == 1 and NO_SYS == 1 +* @param None +* @retval Current Time value +*/ +u32_t sys_now(void) +{ + return HAL_GetTick(); +} + +/* USER CODE END 6 */ + +/** + * @brief Initializes the ETH MSP. + * @param ethHandle: ETH handle + * @retval None + */ + +void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(ethHandle->Instance==ETH) + { + /* USER CODE BEGIN ETH_MspInit 0 */ + + /* USER CODE END ETH_MspInit 0 */ + /* Enable Peripheral clock */ + __HAL_RCC_ETH1MAC_CLK_ENABLE(); + __HAL_RCC_ETH1TX_CLK_ENABLE(); + __HAL_RCC_ETH1RX_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOG_CLK_ENABLE(); + /**ETH GPIO Configuration + PC1 ------> ETH_MDC + PA1 ------> ETH_REF_CLK + PA2 ------> ETH_MDIO + PA7 ------> ETH_CRS_DV + PC4 ------> ETH_RXD0 + PC5 ------> ETH_RXD1 + PG11 ------> ETH_TX_EN + PG12 ------> ETH_TXD1 + PG13 ------> ETH_TXD0 + */ + GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF11_ETH; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF11_ETH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF11_ETH; + HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); + + /* Peripheral interrupt init */ + HAL_NVIC_SetPriority(ETH_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(ETH_IRQn); + /* USER CODE BEGIN ETH_MspInit 1 */ + + /* USER CODE END ETH_MspInit 1 */ + } +} + +void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle) +{ + if(ethHandle->Instance==ETH) + { + /* USER CODE BEGIN ETH_MspDeInit 0 */ + + /* USER CODE END ETH_MspDeInit 0 */ + /* Disable Peripheral clock */ + __HAL_RCC_ETH1MAC_CLK_DISABLE(); + __HAL_RCC_ETH1TX_CLK_DISABLE(); + __HAL_RCC_ETH1RX_CLK_DISABLE(); + + /**ETH GPIO Configuration + PC1 ------> ETH_MDC + PA1 ------> ETH_REF_CLK + PA2 ------> ETH_MDIO + PA7 ------> ETH_CRS_DV + PC4 ------> ETH_RXD0 + PC5 ------> ETH_RXD1 + PG11 ------> ETH_TX_EN + PG12 ------> ETH_TXD1 + PG13 ------> ETH_TXD0 + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5); + + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7); + + HAL_GPIO_DeInit(GPIOG, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13); + + /* Peripheral interrupt Deinit*/ + HAL_NVIC_DisableIRQ(ETH_IRQn); + + /* USER CODE BEGIN ETH_MspDeInit 1 */ + + /* USER CODE END ETH_MspDeInit 1 */ + } +} + +/******************************************************************************* + PHI IO Functions +*******************************************************************************/ +/** + * @brief Initializes the MDIO interface GPIO and clocks. + * @param None + * @retval 0 if OK, -1 if ERROR + */ +int32_t ETH_PHY_IO_Init(void) +{ + /* We assume that MDIO GPIO configuration is already done + in the ETH_MspInit() else it should be done here + */ + + /* Configure the MDIO Clock */ + HAL_ETH_SetMDIOClockRange(&heth); + + return 0; +} + +/** + * @brief De-Initializes the MDIO interface . + * @param None + * @retval 0 if OK, -1 if ERROR + */ +int32_t ETH_PHY_IO_DeInit (void) +{ + return 0; +} + +/** + * @brief Read a PHY register through the MDIO interface. + * @param DevAddr: PHY port address + * @param RegAddr: PHY register address + * @param pRegVal: pointer to hold the register value + * @retval 0 if OK -1 if Error + */ +int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal) +{ + if(HAL_ETH_ReadPHYRegister(&heth, DevAddr, RegAddr, pRegVal) != HAL_OK) + { + return -1; + } + + return 0; +} + +/** + * @brief Write a value to a PHY register through the MDIO interface. + * @param DevAddr: PHY port address + * @param RegAddr: PHY register address + * @param RegVal: Value to be written + * @retval 0 if OK -1 if Error + */ +int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal) +{ + if(HAL_ETH_WritePHYRegister(&heth, DevAddr, RegAddr, RegVal) != HAL_OK) + { + return -1; + } + + return 0; +} + +/** + * @brief Get the time in millisecons used for internal PHY driver process. + * @retval Time value + */ +int32_t ETH_PHY_IO_GetTick(void) +{ + return HAL_GetTick(); +} + +/** + * @brief Check the ETH link state then update ETH driver and netif link accordingly. + * @retval None + */ +void ethernet_link_thread(void* argument) +{ + ETH_MACConfigTypeDef MACConf = {0}; + int32_t PHYLinkState = 0; + uint32_t linkchanged = 0U, speed = 0U, duplex = 0U; + + struct netif *netif = (struct netif *) argument; +/* USER CODE BEGIN ETH link init */ + +/* USER CODE END ETH link init */ + + for(;;) + { + PHYLinkState = LAN8742_GetLinkState(&LAN8742); + + if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN)) + { + HAL_ETH_Stop_IT(&heth); + netif_set_down(netif); + netif_set_link_down(netif); + } + else if(!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN)) + { + switch (PHYLinkState) + { + case LAN8742_STATUS_100MBITS_FULLDUPLEX: + duplex = ETH_FULLDUPLEX_MODE; + speed = ETH_SPEED_100M; + linkchanged = 1; + break; + case LAN8742_STATUS_100MBITS_HALFDUPLEX: + duplex = ETH_HALFDUPLEX_MODE; + speed = ETH_SPEED_100M; + linkchanged = 1; + break; + case LAN8742_STATUS_10MBITS_FULLDUPLEX: + duplex = ETH_FULLDUPLEX_MODE; + speed = ETH_SPEED_10M; + linkchanged = 1; + break; + case LAN8742_STATUS_10MBITS_HALFDUPLEX: + duplex = ETH_HALFDUPLEX_MODE; + speed = ETH_SPEED_10M; + linkchanged = 1; + break; + default: + break; + } + + if(linkchanged) + { + /* Get MAC Config MAC */ + HAL_ETH_GetMACConfig(&heth, &MACConf); + MACConf.DuplexMode = duplex; + MACConf.Speed = speed; + HAL_ETH_SetMACConfig(&heth, &MACConf); + HAL_ETH_Start_IT(&heth); + netif_set_up(netif); + netif_set_link_up(netif); + } + } + +/* USER CODE BEGIN ETH link Thread core code for User BSP */ + +/* USER CODE END ETH link Thread core code for User BSP */ + + osDelay(100); + } +} + +void HAL_ETH_RxAllocateCallback(uint8_t **buff) +{ +/* USER CODE BEGIN HAL ETH RxAllocateCallback */ + struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL); + if (p) + { + /* Get the buff from the struct pbuf address. */ + *buff = (uint8_t *)p + offsetof(RxBuff_t, buff); + p->custom_free_function = pbuf_free_custom; + /* Initialize the struct pbuf. + * This must be performed whenever a buffer's allocated because it may be + * changed by lwIP or the app, e.g., pbuf_free decrements ref. */ + pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUFFER_SIZE); + } + else + { + RxAllocStatus = RX_ALLOC_ERROR; + *buff = NULL; + } +/* USER CODE END HAL ETH RxAllocateCallback */ +} + +void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length) +{ +/* USER CODE BEGIN HAL ETH RxLinkCallback */ + + struct pbuf **ppStart = (struct pbuf **)pStart; + struct pbuf **ppEnd = (struct pbuf **)pEnd; + struct pbuf *p = NULL; + + /* Get the struct pbuf from the buff address. */ + p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff)); + p->next = NULL; + p->tot_len = 0; + p->len = Length; + + /* Chain the buffer. */ + if (!*ppStart) + { + /* The first buffer of the packet. */ + *ppStart = p; + } + else + { + /* Chain the buffer to the end of the packet. */ + (*ppEnd)->next = p; + } + *ppEnd = p; + + /* Update the total length of all the buffers of the chain. Each pbuf in the chain should have its tot_len + * set to its own length, plus the length of all the following pbufs in the chain. */ + for (p = *ppStart; p != NULL; p = p->next) + { + p->tot_len += Length; + } + + /* Invalidate data cache because Rx DMA's writing to physical memory makes it stale. */ + SCB_InvalidateDCache_by_Addr((uint32_t *)buff, Length); + +/* USER CODE END HAL ETH RxLinkCallback */ +} + +void HAL_ETH_TxFreeCallback(uint32_t * buff) +{ +/* USER CODE BEGIN HAL ETH TxFreeCallback */ + + pbuf_free((struct pbuf *)buff); + +/* USER CODE END HAL ETH TxFreeCallback */ +} + +/* USER CODE BEGIN 8 */ + +/* USER CODE END 8 */ + diff --git a/LWIP/Target/ethernetif.h b/LWIP/Target/ethernetif.h new file mode 100644 index 0000000..4dafa5e --- /dev/null +++ b/LWIP/Target/ethernetif.h @@ -0,0 +1,46 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * File Name : ethernetif.h + * 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 */ + +#ifndef __ETHERNETIF_H__ +#define __ETHERNETIF_H__ + +#include "lwip/err.h" +#include "lwip/netif.h" +#include "cmsis_os.h" + +/* Within 'USER CODE' section, code will be kept by default at each generation */ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* Exported functions ------------------------------------------------------- */ +err_t ethernetif_init(struct netif *netif); + +void ethernetif_input(void* argument); +void ethernet_link_thread(void* argument ); + +void Error_Handler(void); +u32_t sys_jiffies(void); +u32_t sys_now(void); + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ +#endif diff --git a/LWIP/Target/lwipopts.h b/LWIP/Target/lwipopts.h new file mode 100644 index 0000000..8ecd77b --- /dev/null +++ b/LWIP/Target/lwipopts.h @@ -0,0 +1,124 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * File Name : Target/lwipopts.h + * Description : This file overrides LwIP stack default configuration + * done in opt.h file. + ****************************************************************************** + * @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 */ + +/* Define to prevent recursive inclusion --------------------------------------*/ +#ifndef __LWIPOPTS__H__ +#define __LWIPOPTS__H__ + +#include "main.h" + +/*-----------------------------------------------------------------------------*/ +/* Current version of LwIP supported by CubeMx: 2.1.2 -*/ +/*-----------------------------------------------------------------------------*/ + +/* Within 'USER CODE' section, code will be kept by default at each generation */ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +#ifdef __cplusplus + extern "C" { +#endif + +/* STM32CubeMX Specific Parameters (not defined in opt.h) ---------------------*/ +/* Parameters set in STM32CubeMX LwIP Configuration GUI -*/ +/*----- WITH_RTOS enabled (Since FREERTOS is set) -----*/ +#define WITH_RTOS 1 +/*----- CHECKSUM_BY_HARDWARE enabled -----*/ +#define CHECKSUM_BY_HARDWARE 1 +/*-----------------------------------------------------------------------------*/ + +/* LwIP Stack Parameters (modified compared to initialization value in opt.h) -*/ +/* Parameters set in STM32CubeMX LwIP Configuration GUI -*/ +/*----- Value in opt.h for LWIP_DHCP: 0 -----*/ +#define LWIP_DHCP 1 +/*----- Default value in ETH configuration GUI in CubeMx: 1524 -----*/ +#define ETH_RX_BUFFER_SIZE 1536 +/*----- Default Value for LWIP_DNS: 0 ---*/ +#define LWIP_DNS 1 +/*----- Value in opt.h for MEM_ALIGNMENT: 1 -----*/ +#define MEM_ALIGNMENT 32 +/*----- Default Value for H7 devices: 0x30044000 -----*/ +#define LWIP_RAM_HEAP_POINTER 0x30004000 +/*----- Value supported for H7 devices: 1 -----*/ +#define LWIP_SUPPORT_CUSTOM_PBUF 1 +/*----- Value in opt.h for LWIP_ETHERNET: LWIP_ARP || PPPOE_SUPPORT -*/ +#define LWIP_ETHERNET 1 +/*----- Value in opt.h for LWIP_DNS_SECURE: (LWIP_DNS_SECURE_RAND_XID | LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT) -*/ +#define LWIP_DNS_SECURE 7 +/*----- Value in opt.h for TCP_SND_QUEUELEN: (4*TCP_SND_BUF + (TCP_MSS - 1))/TCP_MSS -----*/ +#define TCP_SND_QUEUELEN 9 +/*----- Value in opt.h for TCP_SNDLOWAT: LWIP_MIN(LWIP_MAX(((TCP_SND_BUF)/2), (2 * TCP_MSS) + 1), (TCP_SND_BUF) - 1) -*/ +#define TCP_SNDLOWAT 1071 +/*----- Value in opt.h for TCP_SNDQUEUELOWAT: LWIP_MAX(TCP_SND_QUEUELEN)/2, 5) -*/ +#define TCP_SNDQUEUELOWAT 5 +/*----- Value in opt.h for TCP_WND_UPDATE_THRESHOLD: LWIP_MIN(TCP_WND/4, TCP_MSS*4) -----*/ +#define TCP_WND_UPDATE_THRESHOLD 536 +/*----- Value in opt.h for LWIP_NETIF_LINK_CALLBACK: 0 -----*/ +#define LWIP_NETIF_LINK_CALLBACK 1 +/*----- Value in opt.h for TCPIP_THREAD_STACKSIZE: 0 -----*/ +#define TCPIP_THREAD_STACKSIZE 1024 +/*----- Value in opt.h for TCPIP_THREAD_PRIO: 1 -----*/ +#define TCPIP_THREAD_PRIO 24 +/*----- Value in opt.h for TCPIP_MBOX_SIZE: 0 -----*/ +#define TCPIP_MBOX_SIZE 6 +/*----- Value in opt.h for SLIPIF_THREAD_STACKSIZE: 0 -----*/ +#define SLIPIF_THREAD_STACKSIZE 1024 +/*----- Value in opt.h for SLIPIF_THREAD_PRIO: 1 -----*/ +#define SLIPIF_THREAD_PRIO 3 +/*----- Value in opt.h for DEFAULT_THREAD_STACKSIZE: 0 -----*/ +#define DEFAULT_THREAD_STACKSIZE 1024 +/*----- Value in opt.h for DEFAULT_THREAD_PRIO: 1 -----*/ +#define DEFAULT_THREAD_PRIO 3 +/*----- Value in opt.h for DEFAULT_UDP_RECVMBOX_SIZE: 0 -----*/ +#define DEFAULT_UDP_RECVMBOX_SIZE 6 +/*----- Value in opt.h for DEFAULT_TCP_RECVMBOX_SIZE: 0 -----*/ +#define DEFAULT_TCP_RECVMBOX_SIZE 6 +/*----- Value in opt.h for DEFAULT_ACCEPTMBOX_SIZE: 0 -----*/ +#define DEFAULT_ACCEPTMBOX_SIZE 6 +/*----- Value in opt.h for RECV_BUFSIZE_DEFAULT: INT_MAX -----*/ +#define RECV_BUFSIZE_DEFAULT 2000000000 +/*----- Value in opt.h for LWIP_STATS: 1 -----*/ +#define LWIP_STATS 0 +/*----- Value in opt.h for CHECKSUM_GEN_IP: 1 -----*/ +#define CHECKSUM_GEN_IP 0 +/*----- Value in opt.h for CHECKSUM_GEN_UDP: 1 -----*/ +#define CHECKSUM_GEN_UDP 0 +/*----- Value in opt.h for CHECKSUM_GEN_TCP: 1 -----*/ +#define CHECKSUM_GEN_TCP 0 +/*----- Value in opt.h for CHECKSUM_GEN_ICMP6: 1 -----*/ +#define CHECKSUM_GEN_ICMP6 0 +/*----- Value in opt.h for CHECKSUM_CHECK_IP: 1 -----*/ +#define CHECKSUM_CHECK_IP 0 +/*----- Value in opt.h for CHECKSUM_CHECK_UDP: 1 -----*/ +#define CHECKSUM_CHECK_UDP 0 +/*----- Value in opt.h for CHECKSUM_CHECK_TCP: 1 -----*/ +#define CHECKSUM_CHECK_TCP 0 +/*----- Value in opt.h for CHECKSUM_CHECK_ICMP6: 1 -----*/ +#define CHECKSUM_CHECK_ICMP6 0 +/*-----------------------------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +#ifdef __cplusplus +} +#endif +#endif /*__LWIPOPTS__H__ */ diff --git a/MDK-ARM/welding_controller_v2.uvprojx b/MDK-ARM/welding_controller_v2.uvprojx index d0273af..dd61466 100644 --- a/MDK-ARM/welding_controller_v2.uvprojx +++ b/MDK-ARM/welding_controller_v2.uvprojx @@ -276,7 +276,7 @@ 1 0x1000 - 0x1f000 + 0x3f000 1 @@ -1113,6 +1113,11 @@ 1 ..\user\at_parser.c + + user_eth.c + 1 + ..\user\user_eth.c + diff --git a/user/user.h b/user/user.h index 2d966d1..7024745 100644 --- a/user/user.h +++ b/user/user.h @@ -212,4 +212,6 @@ uint32_t get_u32_big( uint8_t *p ); void put_u16_big( uint8_t *p, uint16_t d ); void put_u32_big( uint8_t *p, uint32_t d ); +//获取mac地址, 从uuid生成 +void device_get_mac( uint8_t mac[6] ); #endif //__USER_H__ diff --git a/user/user_app.c b/user/user_app.c index da1eead..32588f3 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -53,11 +53,15 @@ dev_info_t g_dev; //#define UID_BASE (0x1FF1E800UL) /*!< Unique device ID register base address */ extern void debug_task_start( void ); extern void wifi_task_start( void ); - +extern void eth_task_start( void ); + void StartDefaultTask(void *argument) { /* init code for USB_DEVICE */ MX_USB_DEVICE_Init(); + /* init code for LWIP */ + MX_LWIP_Init(); + #if 0 while(0){ const char*method[3] = {"TIG", "MIG", "MAG" }; @@ -90,6 +94,7 @@ void StartDefaultTask(void *argument) beepTaskHandle = osThreadNew(user_beep_task, NULL, &beepTask_attributes); debug_task_start(); //支持通过usb cdc 接口调试 wifi_task_start(); //wifi + eth_task_start(); //ethernet //osThreadExit(); while(1){ osDelay(5*1000); @@ -368,3 +373,19 @@ void welding_data_store( uint32_t index ) CDC_Transmit_HS( buf, pkt_sz ); } } + +void device_get_mac( uint8_t mac[6] ) +{ + uint32_t uid0; + uint32_t uid1; + + uid0 = HAL_GetUIDw0(); + uid1 = HAL_GetUIDw1(); + + mac[0] = (uid0>>24)&0xff; + mac[1] = (uid0>>16)&0xff; + mac[2] = (uid0>>8)&0xff; + mac[3] = (uid0>>0)&0xff; + mac[4] = (uid1>>24)&0xff; + mac[5] = (uid1>>16)&0xff; +} diff --git a/user/user_debug.c b/user/user_debug.c index 09a8518..6bb9011 100644 --- a/user/user_debug.c +++ b/user/user_debug.c @@ -8,6 +8,7 @@ uint32_t dbg_report_raw_current = 0; //采集上报原始AD值,用于获取交 int32_t fputc( int32_t data, FILE *fd ) { + HAL_UART_Transmit( &huart2, (uint8_t*)&data, 1, 10 ); return 0; } diff --git a/user/user_eth.c b/user/user_eth.c new file mode 100644 index 0000000..30fcd4a --- /dev/null +++ b/user/user_eth.c @@ -0,0 +1,97 @@ +#include "user.h" +#include "at_parser.h" + +static char mac_str[20]; //"00:00:00:00:00:00"; +static const char *fmt = "{\n\ + \"cmd\":\"rt\",\n\ + \"cid\":%d,\n\ + \"mac\":\"%s\",\n\ + \"val\":\n\ + {\n\ + \"c\":\"%0.1f\",\n\ + \"s\":\"%d\",\n\ + \"g\":\"%0.1f\",\n\ + }\n\ +}"; + +void eth_mac_str_init( void ) +{ + uint32_t uid0; + uint32_t uid1; + uint32_t mac[6]; + + uid0 = HAL_GetUIDw0(); + uid1 = HAL_GetUIDw1(); + mac[0] = (uid0>>24)&0xff; + mac[1] = (uid0>>16)&0xff; + mac[2] = (uid0>>8)&0xff; + mac[3] = (uid0>>0)&0xff; + mac[4] = (uid1>>24)&0xff; + mac[5] = (uid1>>16)&0xff; + + sprintf( mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); +} + +int32_t eth_devinfo_report( void ) +{ + static uint32_t cid = 0; + static uint8_t tx_buf[200]; + float c,g; + uint32_t s; + + c = g_dev.welding_current; + g = g_dev.flowmeter.flow_speed; + s = g_dev.welding_start; + sprintf( (char*)tx_buf, fmt, cid++, mac_str, c, s, g ); + + return 0; +} +#include "lwip/netif.h" +extern struct netif gnetif; +void user_eth_task( void* unused ) +{ + TickType_t last_ticks; + g_dev.connected = 0; + + //1. init + while( 1 ){ + //eth init fail + printf( "netif ip_addr:%d.%d.%d.%d\n", + (gnetif.ip_addr.addr)&0xff, + (gnetif.ip_addr.addr>>8)&0xff, + (gnetif.ip_addr.addr>>16)&0xff, + (gnetif.ip_addr.addr>>24)&0xff ); + osDelay( 10*1000 ); + } + + //if( tcp_server_connect( g_dev.cfg.net.tcp_server, g_dev.cfg.net.tcp_port ) == 0 ){ + + last_ticks = xTaskGetTickCount(); + + while( g_dev.connected == 2 ){ + vTaskDelayUntil(&last_ticks, 1000 ); + osDelay(1000); + //定时上报 + if( eth_devinfo_report() < 0 ){ + //reconnect +// connected = 0; +// g_dev.connected = 1; + break; + } + } + //} +} + +//static osThreadId_t ethTaskHandle; + +const osThreadAttr_t ethTask_attributes = { + .name = "ethTask", + .stack_size = 1024, + .priority = (osPriority_t) osPriorityNormal, +}; + +void eth_task_start( void ) +{ + // ethTaskHandle = osThreadNew(user_eth_task, NULL, ðTask_attributes); + osThreadNew(user_eth_task, NULL, ðTask_attributes); +} diff --git a/user/user_flowmeter.c b/user/user_flowmeter.c index 9ce296d..aa018c0 100644 --- a/user/user_flowmeter.c +++ b/user/user_flowmeter.c @@ -116,7 +116,7 @@ int32_t mb_master_read_regs( uint8_t device_addr, uint16_t reg, uint16_t* reg_va } } }else{ - printf( "rx err!\n" ); + //printf( "rx err!\n" ); } return -1; } diff --git a/user/user_wifi.c b/user/user_wifi.c index 586c81e..893cab7 100644 --- a/user/user_wifi.c +++ b/user/user_wifi.c @@ -202,17 +202,10 @@ void mac_str_init( void ) { uint32_t uid0; uint32_t uid1; - uint32_t mac[6]; - - uid0 = HAL_GetUIDw0(); - uid1 = HAL_GetUIDw1(); - mac[0] = (uid0>>24)&0xff; - mac[1] = (uid0>>16)&0xff; - mac[2] = (uid0>>8)&0xff; - mac[3] = (uid0>>0)&0xff; - mac[4] = (uid1>>24)&0xff; - mac[5] = (uid1>>16)&0xff; + uint8_t mac[6]; + device_get_mac(mac); + sprintf( mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); } diff --git a/welding_controller_v2.ioc b/welding_controller_v2.ioc index 6ee52b5..0f74f53 100644 --- a/welding_controller_v2.ioc +++ b/welding_controller_v2.ioc @@ -51,9 +51,27 @@ ADC3.SamplingTime-3\#ChannelRegularConversion=ADC3_SAMPLETIME_2CYCLES_5 CAD.formats= CAD.pinconfig= CAD.provider= +CORTEX_M7.AccessPermission-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_REGION_FULL_ACCESS +CORTEX_M7.AccessPermission-Cortex_Memory_Protection_Unit_Region1_Settings=MPU_REGION_FULL_ACCESS +CORTEX_M7.AccessPermission-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_REGION_FULL_ACCESS +CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region0_Settings=0x30000000 +CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region1_Settings=0x30004000 +CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region2_Settings=0x24000000 CORTEX_M7.CPU_DCache=Enabled CORTEX_M7.CPU_ICache=Enabled -CORTEX_M7.IPParameters=CPU_ICache,CPU_DCache +CORTEX_M7.Enable-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_REGION_ENABLE +CORTEX_M7.Enable-Cortex_Memory_Protection_Unit_Region1_Settings=MPU_REGION_ENABLE +CORTEX_M7.Enable-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_REGION_ENABLE +CORTEX_M7.IPParameters=CPU_ICache,CPU_DCache,MPU_Control,Enable-Cortex_Memory_Protection_Unit_Region0_Settings,Enable-Cortex_Memory_Protection_Unit_Region1_Settings,Enable-Cortex_Memory_Protection_Unit_Region2_Settings,BaseAddress-Cortex_Memory_Protection_Unit_Region0_Settings,Size-Cortex_Memory_Protection_Unit_Region0_Settings,AccessPermission-Cortex_Memory_Protection_Unit_Region0_Settings,BaseAddress-Cortex_Memory_Protection_Unit_Region1_Settings,Size-Cortex_Memory_Protection_Unit_Region1_Settings,TypeExtField-Cortex_Memory_Protection_Unit_Region1_Settings,TypeExtField-Cortex_Memory_Protection_Unit_Region0_Settings,BaseAddress-Cortex_Memory_Protection_Unit_Region2_Settings,Size-Cortex_Memory_Protection_Unit_Region2_Settings,TypeExtField-Cortex_Memory_Protection_Unit_Region2_Settings,IsCacheable-Cortex_Memory_Protection_Unit_Region2_Settings,IsBufferable-Cortex_Memory_Protection_Unit_Region2_Settings,AccessPermission-Cortex_Memory_Protection_Unit_Region1_Settings,AccessPermission-Cortex_Memory_Protection_Unit_Region2_Settings +CORTEX_M7.IsBufferable-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_ACCESS_BUFFERABLE +CORTEX_M7.IsCacheable-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_ACCESS_CACHEABLE +CORTEX_M7.MPU_Control=MPU_PRIVILEGED_DEFAULT +CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_REGION_SIZE_16KB +CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region1_Settings=MPU_REGION_SIZE_16KB +CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_REGION_SIZE_128KB +CORTEX_M7.TypeExtField-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_TEX_LEVEL1 +CORTEX_M7.TypeExtField-Cortex_Memory_Protection_Unit_Region1_Settings=MPU_TEX_LEVEL1 +CORTEX_M7.TypeExtField-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_TEX_LEVEL1 DAC1.DAC_Channel-DAC_OUT2=DAC_CHANNEL_2 DAC1.DAC_Trigger-DAC_OUT2=DAC_TRIGGER_SOFTWARE DAC1.IPParameters=DAC_Channel-DAC_OUT2,DAC_Trigger-DAC_OUT2 @@ -130,6 +148,10 @@ I2C2.Speed=100 I2C2.Timing=0x2040C4DF KeepUserPlacement=false LWIP.BSP.number=1 +LWIP.IPParameters=LWIP_DNS,MEM_ALIGNMENT,LWIP_RAM_HEAP_POINTER +LWIP.LWIP_DNS=1 +LWIP.LWIP_RAM_HEAP_POINTER=0x30004000 +LWIP.MEM_ALIGNMENT=4 LWIP.Version=v2.1.2_Cube LWIP0.BSP.STBoard=false LWIP0.BSP.api=BSP_COMPONENT_DRIVER @@ -249,6 +271,7 @@ NVIC.DMA1_Stream1_IRQn=true\:5\:0\:false\:false\:true\:false\:false\:true\:true NVIC.DMA1_Stream2_IRQn=true\:5\:0\:false\:false\:true\:false\:false\:true\:true NVIC.DMA1_Stream3_IRQn=true\:5\:0\:false\:false\:true\:false\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false +NVIC.ETH_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false @@ -445,9 +468,10 @@ PF10.GPIO_Label=PF10_QSPI_CLK PF10.Locked=true PF10.Mode=O1_P1_CLK PF10.Signal=OCTOSPIM_P1_CLK -PF11.GPIOParameters=GPIO_Label +PF11.GPIOParameters=PinState,GPIO_Label PF11.GPIO_Label=ENET_RESET PF11.Locked=true +PF11.PinState=GPIO_PIN_SET PF11.Signal=GPIO_Output PF8.GPIOParameters=GPIO_Label PF8.GPIO_Label=PF8_PWM_SPK_TIM13_CH1 -- 2.34.1