修改以太网接口初始化,和USB CoreReset延迟
authorkyz <kongyuzhong@google.com>
Wed, 10 Apr 2024 09:57:59 +0000 (17:57 +0800)
committerkyz <kongyuzhong@google.com>
Wed, 10 Apr 2024 10:55:05 +0000 (18:55 +0800)
Core/Src/main.c
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_usb.c
LWIP/App/lwip.c
LWIP/Target/ethernetif.c

index 395f02fe7c80ad66baaa06db7c0460589d840613..a5abd62b29289b4fff76c2894d63e37d98964216 100644 (file)
@@ -134,7 +134,7 @@ int main(void)
   MX_UART4_Init();
   MX_OCTOSPI1_Init();
   MX_TIM1_Init();
-//  MX_IWDG1_Init();
+  MX_IWDG1_Init();
   /* USER CODE BEGIN 2 */
 
   /* USER CODE END 2 */
index 85d0136023fad19213a9a68bad8ee3befac69169..9d47448089888dbae7dd003d39e60c8f5cc32054 100644 (file)
@@ -1392,23 +1392,32 @@ static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
   /* Wait for AHB master IDLE state. */
   do
   {
+               __NOP();
     count++;
 
-    if (count > 200000U)
+    if (count > 500000U)
     {
       return HAL_TIMEOUT;
     }
   } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
 
   /* Core Soft Reset */
+
+       //2024/4/10
+  //代码编译优化O2后,CoreReset容易失败,
+       //按网上资料加了__NOP指令调用,循环等待200000改成500000 ,依然会超时错误
+       //实际测试在复位前增加延迟100毫秒后正常。
+       HAL_Delay(100);
+
   count = 0U;
   USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
 
   do
   {
+               __NOP();
     count++;
 
-    if (count > 200000U)
+    if (count > 500000U)
     {
       return HAL_TIMEOUT;
     }
index 022c3786dfb5ede73b61a1c2496e67d125a36eb1..c0286d436af460939e7854c58ee9858749388c3f 100644 (file)
@@ -68,7 +68,11 @@ void MX_LWIP_Init(void)
   gw.addr = 0;
 
   /* add the network interface (IPv4/IPv6) with RTOS */
-  netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
+  while( !netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input) ){
+               //以太网接口初始化失败,重试
+               //MX_LWIP_Init 失败这里会堵塞,需要放在单独task中执行
+               osDelay( 5000 );
+       }
 
   /* Registers the default network interface */
   netif_set_default(&gnetif);
index 33023e80a9c1115584d3ef0fe16a20ef23e57f98..3afdc950d4a8d178f21f3cfeb10a95c3a318aab0 100644 (file)
@@ -195,7 +195,7 @@ void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *handlerEth)
  * @param netif the already initialized lwip network interface structure
  *        for this ethernetif
  */
-static void low_level_init(struct netif *netif)
+static  err_t low_level_init(struct netif *netif)
 {
   HAL_StatusTypeDef hal_eth_init_status = HAL_OK;
 /* USER CODE BEGIN OS_THREAD_ATTR_CMSIS_RTOS_V2 */
@@ -231,6 +231,10 @@ static void low_level_init(struct netif *netif)
 
   hal_eth_init_status = HAL_ETH_Init(&heth);
 
+  if (hal_eth_init_status != HAL_OK){
+               return ERR_IF;
+       }
+
   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;
@@ -343,12 +347,12 @@ static void low_level_init(struct netif *netif)
   }
   else
   {
-    Error_Handler();
+                       Error_Handler();
   }
 #endif /* LWIP_ARP || LWIP_ETHERNET */
 
 /* USER CODE BEGIN LOW_LEVEL_INIT */
-
+return ERR_OK;
 /* USER CODE END LOW_LEVEL_INIT */
 }
 
@@ -545,9 +549,9 @@ err_t ethernetif_init(struct netif *netif)
   netif->linkoutput = low_level_output;
 
   /* initialize the hardware */
-  low_level_init(netif);
+  return low_level_init(netif);
 
-  return ERR_OK;
+  //return ERR_OK;
 }
 
 /**