[PATCH rtems-lwip 3/5] RTEMS port of lwIP for STM32 and STM32F4 BSP

Duc Doan dtbpkmte at gmail.com
Sun Sep 4 01:25:00 UTC 2022


---
 rtemslwip/stm32f4/lwipopts.h     |  24 ++-
 rtemslwip/stm32f4/netstart.c     |  29 +++-
 rtemslwip/stm32f4/stm32f4_lwip.c |  14 ++
 rtemslwip/stm32f4/stm32f4_lwip.h |   9 +
 stm32/ethernetif.c               | 288 +++++++++++++++++++++++++++++--
 stm32/ethernetif.h               |  14 +-
 stm32/lwip.c                     | 103 +++--------
 stm32/lwip.h                     |   2 +
 8 files changed, 374 insertions(+), 109 deletions(-)
 create mode 100644 rtemslwip/stm32f4/stm32f4_lwip.c
 create mode 100644 rtemslwip/stm32f4/stm32f4_lwip.h

diff --git a/rtemslwip/stm32f4/lwipopts.h b/rtemslwip/stm32f4/lwipopts.h
index 49a19e4..41cc68f 100644
--- a/rtemslwip/stm32f4/lwipopts.h
+++ b/rtemslwip/stm32f4/lwipopts.h
@@ -28,7 +28,7 @@
 
 /* Within 'USER CODE' section, code will be kept by default at each generation */
 /* USER CODE BEGIN 0 */
-
+#include <stm32f4xx_hal.h>
 /* USER CODE END 0 */
 
 #ifdef __cplusplus
@@ -38,7 +38,7 @@
 /* STM32CubeMX Specific Parameters (not defined in opt.h) ---------------------*/
 /* Parameters set in STM32CubeMX LwIP Configuration GUI -*/
 /*----- WITH_RTOS disabled (Since FREERTOS is not set) -----*/
-#define WITH_RTOS 0
+#define WITH_RTOS 1
 /*----- CHECKSUM_BY_HARDWARE disabled -----*/
 #define CHECKSUM_BY_HARDWARE 0
 /*-----------------------------------------------------------------------------*/
@@ -50,11 +50,11 @@
 /*----- Value in opt.h for NO_SYS: 0 -----*/
 #define NO_SYS 0
 /*----- Value in opt.h for SYS_LIGHTWEIGHT_PROT: 1 -----*/
-#define SYS_LIGHTWEIGHT_PROT 0
+#define SYS_LIGHTWEIGHT_PROT 1
 /*----- Value in opt.h for MEM_ALIGNMENT: 1 -----*/
 #define MEM_ALIGNMENT 4
 /*----- Default Value for H7 devices: 0x30044000 -----*/
-#define LWIP_RAM_HEAP_POINTER 0x20017f58
+//#define LWIP_RAM_HEAP_POINTER 0x20017f58
 /*----- Value supported for H7 devices: 1 -----*/
 #define LWIP_SUPPORT_CUSTOM_PBUF 1
 /*----- Value in opt.h for LWIP_ETHERNET: LWIP_ARP || PPPOE_SUPPORT -*/
@@ -70,13 +70,13 @@
 /*----- 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
+//#define LWIP_NETIF_LINK_CALLBACK 0
 /*----- Value in opt.h for LWIP_NETCONN: 1 -----*/
 #define LWIP_NETCONN 1
 /*----- Value in opt.h for LWIP_SOCKET: 1 -----*/
 #define LWIP_SOCKET 1
 /*----- Value in opt.h for RECV_BUFSIZE_DEFAULT: INT_MAX -----*/
-#define RECV_BUFSIZE_DEFAULT 2000000000
+//#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 -----*/
@@ -115,9 +115,19 @@
 #define LWIP_IPV6                       1
 
 #define LWIP_TCPIP_CORE_LOCKING         1
+#define TCPIP_THREAD_STACKSIZE 1024
+#define TCPIP_THREAD_PRIO 24
+#define TCPIP_MBOX_SIZE 6
+#define SLIPIF_THREAD_STACKSIZE 1024
+#define SLIPIF_THREAD_PRIO 3
+#define DEFAULT_THREAD_STACKSIZE 1024
+#define DEFAULT_THREAD_PRIO 3
+#define DEFAULT_UDP_RECVMBOX_SIZE 6
+#define DEFAULT_TCP_RECVMBOX_SIZE 6
+#define DEFAULT_ACCEPTMBOX_SIZE 6
 
 #define LWIP_DHCP                       1
-#define DHCP_DOES_ARP_CHECK             0
+#define DHCP_DOES_ARP_CHECK             1
 
 #define LWIP_DNS                        1
 
diff --git a/rtemslwip/stm32f4/netstart.c b/rtemslwip/stm32f4/netstart.c
index 88e846a..6ddcc81 100644
--- a/rtemslwip/stm32f4/netstart.c
+++ b/rtemslwip/stm32f4/netstart.c
@@ -26,7 +26,10 @@
 
 #include <netstart.h>
 #include <lwip/tcpip.h>
-#include <ethernetif.h>
+#include "lwip.h"
+#include "lwip/init.h"
+#include "lwip/netif.h"
+#include "ethernetif.h"
 
 int start_networking(
   struct netif  *net_interface,
@@ -38,15 +41,35 @@ int start_networking(
 {
   tcpip_init( NULL, NULL );
   
-  netif_add(net_interface, ipaddr, netmask, gw, NULL, &ethernetif_init, &tcpip_input);
+  set_mac_addr(mac_ethernet_address);
+
+  netif_add(
+    net_interface, 
+    &ipaddr->u_addr.ip4, 
+    &netmask->u_addr.ip4, 
+    &gateway->u_addr.ip4, 
+    NULL, 
+    ethernetif_init, 
+    tcpip_input
+  );
   
   netif_set_default(net_interface);
   
   if (netif_is_link_up(net_interface)) {
     netif_set_up(net_interface);
+    
+    sys_thread_new(
+      "stm32f4_ethernet_link_thread",
+      ethernet_link_thread,
+      net_interface,
+      1024,
+      DEFAULT_THREAD_PRIO
+    );
+        
+    return 0;
   } else {
     netif_set_down(net_interface);
   }
 
-  return 0;
+  return -1;
 }
diff --git a/rtemslwip/stm32f4/stm32f4_lwip.c b/rtemslwip/stm32f4/stm32f4_lwip.c
new file mode 100644
index 0000000..1f4f07e
--- /dev/null
+++ b/rtemslwip/stm32f4/stm32f4_lwip.c
@@ -0,0 +1,14 @@
+#include "stm32f4_lwip.h"
+
+extern ETH_HandleTypeDef heth;
+
+__attribute__((weak)) void Error_Handler(void) {
+    __disable_irq();
+    while (1)
+    {
+    }
+}
+
+void ETH_IRQHandler(void) {
+    HAL_ETH_IRQHandler(&heth);
+}
diff --git a/rtemslwip/stm32f4/stm32f4_lwip.h b/rtemslwip/stm32f4/stm32f4_lwip.h
new file mode 100644
index 0000000..8a2b03a
--- /dev/null
+++ b/rtemslwip/stm32f4/stm32f4_lwip.h
@@ -0,0 +1,9 @@
+#ifndef LIPLWIP_RTEMSLWIP_STM32F4_STM32F4_LWIP_H
+#define LIPLWIP_RTEMSLWIP_STM32F4_STM32F4_LWIP_H
+
+#include <stm32f4xx_hal.h>
+
+void ErrorHandler(void);
+void ETH_IRQHandler(void);
+
+#endif /* LIPLWIP_RTEMSLWIP_STM32F4_STM32F4_LWIP_H */
diff --git a/stm32/ethernetif.c b/stm32/ethernetif.c
index e54c8b6..a8e9f76 100644
--- a/stm32/ethernetif.c
+++ b/stm32/ethernetif.c
@@ -19,10 +19,14 @@
 /* USER CODE END Header */
 
 /* Includes ------------------------------------------------------------------*/
+#ifndef __rtems__
 #include "main.h"
+#endif /* __rtems__ */
+#ifdef __rtems__
+#include <bsp.h>
+#include <bsp/irq.h>
+#endif /* __rtems__ */
 #include "lwip/opt.h"
-#include "lwip/mem.h"
-#include "lwip/memp.h"
 #include "lwip/timeouts.h"
 #include "netif/ethernet.h"
 #include "netif/etharp.h"
@@ -30,6 +34,10 @@
 #include "ethernetif.h"
 #include "dp83848.h"
 #include <string.h>
+#ifndef __rtems__
+#include "cmsis_os.h"
+#endif /* __rtems__ */
+#include "lwip/tcpip.h"
 
 /* Within 'USER CODE' section, code will be kept by default at each generation */
 /* USER CODE BEGIN 0 */
@@ -37,7 +45,18 @@
 /* USER CODE END 0 */
 
 /* Private define ------------------------------------------------------------*/
-
+/* The time to block waiting for input. */
+#ifndef __rtems__
+#define TIME_WAITING_FOR_INPUT ( portMAX_DELAY )
+#endif /* __rtems__ */
+#ifdef __rtems__
+#define TIME_WAITING_FOR_INPUT ( RTEMS_NO_TIMEOUT )
+#endif /* __rtems__ */
+
+/* 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'
@@ -104,9 +123,20 @@ ETH_DMADescTypeDef  DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptor
 
 /* USER CODE END 2 */
 
+#ifndef __rtems__
+osSemaphoreId RxPktSemaphore = NULL;   /* Semaphore to signal incoming packets */
+osSemaphoreId TxPktSemaphore = NULL;   /* Semaphore to signal transmit packet complete */
+#else
+rtems_id RxPktSemaphore;   /* Semaphore to signal incoming packets */
+rtems_id TxPktSemaphore;   /* Semaphore to signal transmit packet complete */
+#endif /* __rtems__ */
+
 /* Global Ethernet handle */
 ETH_HandleTypeDef heth;
 ETH_TxPacketConfig TxConfig;
+#ifdef __rtems__
+static uint8_t *MACAddr;
+#endif /* __rtems__ */
 
 /* Private function prototypes -----------------------------------------------*/
 int32_t ETH_PHY_IO_Init(void);
@@ -129,8 +159,56 @@ dp83848_IOCtx_t  DP83848_IOCtx = {ETH_PHY_IO_Init,
 /* Private functions ---------------------------------------------------------*/
 void pbuf_free_custom(struct pbuf *p);
 
-/* USER CODE BEGIN 4 */
+/**
+  * @brief  Ethernet Rx Transfer completed callback
+  * @param  handlerEth: ETH handler
+  * @retval None
+  */
+void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *handlerEth)
+{
+#ifndef __rtems__
+  osSemaphoreRelease(RxPktSemaphore);
+#else
+  rtems_semaphore_release(RxPktSemaphore);
+#endif /* __rtems__ */
+}
+/**
+  * @brief  Ethernet Tx Transfer completed callback
+  * @param  handlerEth: ETH handler
+  * @retval None
+  */
+void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *handlerEth)
+{
+#ifndef __rtems__
+  osSemaphoreRelease(TxPktSemaphore);
+#else
+  rtems_semaphore_release(TxPktSemaphore);
+#endif /* __rtems__ */
+}
+/**
+  * @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_DMASR_RBUS) == ETH_DMASR_RBUS)
+  {
+#ifndef __rtems__
+    osSemaphoreRelease(RxPktSemaphore);
+#else
+    rtems_semaphore_release(RxPktSemaphore);
+#endif /* __rtems__ */
+  }
+}
 
+/* USER CODE BEGIN 4 */
+#ifdef __rtems__
+void set_mac_addr(uint8_t *mac_addr)
+{
+  MACAddr = mac_addr;
+}
+#endif /* __rtems__ */
 /* USER CODE END 4 */
 
 /*******************************************************************************
@@ -146,17 +224,33 @@ void pbuf_free_custom(struct pbuf *p);
 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 */
+#ifndef __rtems__
+  osThreadAttr_t attributes;
+#endif /* __rtems__ */
+/* 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 */
 
+#ifndef __rtems__
    uint8_t MACAddr[6] ;
+#endif /* __rtems__ */
   heth.Instance = ETH;
+#ifndef __rtems__
   MACAddr[0] = 0x02;
   MACAddr[1] = 0x00;
   MACAddr[2] = 0x00;
   MACAddr[3] = 0x00;
   MACAddr[4] = 0x00;
   MACAddr[5] = 0x01;
+#endif /* __rtems__ */
+#ifndef __rtems__
   heth.Init.MACAddr = &MACAddr[0];
+#else
+  heth.Init.MACAddr = MACAddr;
+#endif /* __rtems__ */
   heth.Init.MediaInterface = HAL_ETH_RMII_MODE;
   heth.Init.TxDesc = DMATxDscrTab;
   heth.Init.RxDesc = DMARxDscrTab;
@@ -202,6 +296,51 @@ static void low_level_init(struct netif *netif)
     netif->flags |= NETIF_FLAG_BROADCAST;
   #endif /* LWIP_ARP */
 
+  /* create a binary semaphore used for informing ethernetif of frame reception */
+#ifndef __rtems__
+  RxPktSemaphore = osSemaphoreNew(1, 1, NULL);
+#else
+  rtems_semaphore_create(
+    rtems_build_name('R', 'x', 'p', 'k'),
+    1,
+    RTEMS_SIMPLE_BINARY_SEMAPHORE,
+    0,
+    &RxPktSemaphore
+  );
+#endif /* __rtems__ */
+
+  /* create a binary semaphore used for informing ethernetif of frame transmission */
+#ifndef __rtems__
+  TxPktSemaphore = osSemaphoreNew(1, 1, NULL);
+#else
+  rtems_semaphore_create(
+    rtems_build_name('T', 'x', 'p', 'k'),
+    1,
+    RTEMS_SIMPLE_BINARY_SEMAPHORE,
+    0,
+    &TxPktSemaphore
+  );
+#endif /* __rtems__ */
+
+  /* create the task that handles the ETH_MAC */
+/* USER CODE BEGIN OS_THREAD_NEW_CMSIS_RTOS_V2 */
+#ifndef __rtems__
+  memset(&attributes, 0x0, sizeof(osThreadAttr_t));
+  attributes.name = "EthIf";
+  attributes.stack_size = INTERFACE_THREAD_STACK_SIZE;
+  attributes.priority = osPriorityRealtime;
+  osThreadNew(ethernetif_input, netif, &attributes);
+#else
+  sys_thread_new(
+    "ethernetif_input_thread",
+    ethernetif_input,
+    netif,
+    RTEMS_MINIMUM_STACK_SIZE,
+    DEFAULT_THREAD_PRIO
+  );
+#endif /* __rtem__ */
+/* USER CODE END OS_THREAD_NEW_CMSIS_RTOS_V2 */
+
 /* USER CODE BEGIN PHY_PRE_CONFIG */
 
 /* USER CODE END PHY_PRE_CONFIG */
@@ -213,8 +352,64 @@ static void low_level_init(struct netif *netif)
 
   if (hal_eth_init_status == HAL_OK)
   {
-  /* Get link state */
-  ethernet_link_check_state(netif);
+    PHYLinkState = DP83848_GetLinkState(&DP83848);
+
+    /* Get link state */
+    if(PHYLinkState <= DP83848_STATUS_LINK_DOWN)
+    {
+      netif_set_link_down(netif);
+      netif_set_down(netif);
+    }
+    else
+    {
+      switch (PHYLinkState)
+      {
+      case DP83848_STATUS_100MBITS_FULLDUPLEX:
+        duplex = ETH_FULLDUPLEX_MODE;
+        speed = ETH_SPEED_100M;
+        break;
+      case DP83848_STATUS_100MBITS_HALFDUPLEX:
+        duplex = ETH_HALFDUPLEX_MODE;
+        speed = ETH_SPEED_100M;
+        break;
+      case DP83848_STATUS_10MBITS_FULLDUPLEX:
+        duplex = ETH_FULLDUPLEX_MODE;
+        speed = ETH_SPEED_10M;
+        break;
+      case DP83848_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);
+
+#ifdef __rtems__
+    rtems_interrupt_handler_install(
+            STM32F4_IRQ_ETH,
+            NULL,
+            RTEMS_INTERRUPT_UNIQUE,
+            HAL_ETH_IRQHandler,
+            &heth
+    );
+#endif /* __rtems__ */
+    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
   {
@@ -277,7 +472,20 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
   TxConfig.TxBuffer = Txbuffer;
   TxConfig.pData = p;
 
-  HAL_ETH_Transmit(&heth, &TxConfig, ETH_DMA_TRANSMIT_TIMEOUT);
+  pbuf_ref(p);
+
+  HAL_ETH_Transmit_IT(&heth, &TxConfig);
+#ifndef __rtems__
+  while(osSemaphoreAcquire(TxPktSemaphore, TIME_WAITING_FOR_INPUT)!=osOK)
+#else
+  while (rtems_semaphore_obtain(TxPktSemaphore, RTEMS_DEFAULT_OPTIONS, 
+          TIME_WAITING_FOR_INPUT) != RTEMS_SUCCESSFUL) 
+#endif /* __rtems__ */
+
+  {
+  }
+
+  HAL_ETH_ReleaseTxPacket(&heth);
 
   return errval;
 }
@@ -311,21 +519,33 @@ static struct pbuf * low_level_input(struct netif *netif)
  *
  * @param netif the lwip network interface structure for this ethernetif
  */
-void ethernetif_input(struct netif *netif)
+void ethernetif_input(void* argument)
 {
   struct pbuf *p = NULL;
+  struct netif *netif = (struct netif *) argument;
 
-  do
+  for( ;; )
   {
-    p = low_level_input( netif );
-    if (p != NULL)
+#ifndef __rtems__
+    if (osSemaphoreAcquire(RxPktSemaphore, TIME_WAITING_FOR_INPUT) == osOK)
+#else
+    if (rtems_semaphore_obtain(RxPktSemaphore, RTEMS_DEFAULT_OPTIONS,
+            TIME_WAITING_FOR_INPUT) == RTEMS_SUCCESSFUL)
+#endif /* __rtems__ */
     {
-      if (netif->input( p, netif) != ERR_OK )
+      do
       {
-        pbuf_free(p);
-      }
+        p = low_level_input( netif );
+        if (p != NULL)
+        {
+          if (netif->input( p, netif) != ERR_OK )
+          {
+            pbuf_free(p);
+          }
+        }
+      } while(p!=NULL);
     }
-  } while(p!=NULL);
+  }
 }
 
 #if !LWIP_ARP
@@ -423,12 +643,17 @@ void pbuf_free_custom(struct pbuf *p)
   if (RxAllocStatus == RX_ALLOC_ERROR)
   {
     RxAllocStatus = RX_ALLOC_OK;
-    RxPkt = 1 ;
+#ifndef __rtems__
+    osSemaphoreRelease(RxPktSemaphore);
+#else
+    rtems_semaphore_release(RxPktSemaphore);
+#endif /* __rtems__ */
   }
 }
 
 /* USER CODE BEGIN 6 */
 
+#ifndef __rtems__
 /**
 * @brief  Returns the current time in milliseconds
 *         when LWIP_TIMERS == 1 and NO_SYS == 1
@@ -439,6 +664,7 @@ u32_t sys_now(void)
 {
   return HAL_GetTick();
 }
+#endif /* __rtems__ */
 
 /* USER CODE END 6 */
 
@@ -488,6 +714,11 @@ void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
     GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
+    /* Peripheral interrupt init */
+#ifndef __rtems__
+    HAL_NVIC_SetPriority(ETH_IRQn, 5, 0);
+    HAL_NVIC_EnableIRQ(ETH_IRQn);
+#endif /* __rtems__ */
   /* USER CODE BEGIN ETH_MspInit 1 */
 
   /* USER CODE END ETH_MspInit 1 */
@@ -521,6 +752,9 @@ void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
 
     HAL_GPIO_DeInit(GPIOB, 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 */
@@ -605,17 +839,24 @@ int32_t ETH_PHY_IO_GetTick(void)
   * @param  argument: netif
   * @retval None
   */
-void ethernet_link_check_state(struct netif *netif)
+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 = DP83848_GetLinkState(&DP83848);
 
   if(netif_is_link_up(netif) && (PHYLinkState <= DP83848_STATUS_LINK_DOWN))
   {
-    HAL_ETH_Stop(&heth);
+    HAL_ETH_Stop_IT(&heth);
     netif_set_down(netif);
     netif_set_link_down(netif);
   }
@@ -660,6 +901,16 @@ void ethernet_link_check_state(struct netif *netif)
     }
   }
 
+/* USER CODE BEGIN ETH link Thread core code for User BSP */
+
+/* USER CODE END ETH link Thread core code for User BSP */
+
+#ifndef __rtems__
+    osDelay(100);
+#else
+    sys_arch_delay(100);
+#endif /* __rtems__ */
+  }
 }
 
 void HAL_ETH_RxAllocateCallback(uint8_t **buff)
@@ -735,3 +986,4 @@ void HAL_ETH_TxFreeCallback(uint32_t * buff)
 
 /* USER CODE END 8 */
 
+
diff --git a/stm32/ethernetif.h b/stm32/ethernetif.h
index 76eb91b..552671a 100644
--- a/stm32/ethernetif.h
+++ b/stm32/ethernetif.h
@@ -23,23 +23,31 @@
 
 #include "lwip/err.h"
 #include "lwip/netif.h"
+#ifndef __rtems__
+#include "cmsis_os.h"
+#endif /* __rtems__ */
 
 /* Within 'USER CODE' section, code will be kept by default at each generation */
 /* USER CODE BEGIN 0 */
-
+#ifdef __rtems__
+void set_mac_addr(uint8_t *mac_addr);
+#endif /* __rtems__ */
 /* USER CODE END 0 */
 
 /* Exported functions ------------------------------------------------------- */
 err_t ethernetif_init(struct netif *netif);
 
-void ethernetif_input(struct netif *netif);
-void ethernet_link_check_state(struct netif *netif);
+void ethernetif_input(void* argument);
+void ethernet_link_thread(void* argument );
 
 void Error_Handler(void);
 u32_t sys_jiffies(void);
+#ifndef __rtems__
 u32_t sys_now(void);
+#endif /* __rtems__ */
 
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
 #endif
+
diff --git a/stm32/lwip.c b/stm32/lwip.c
index 9b2af8a..82a09fe 100644
--- a/stm32/lwip.c
+++ b/stm32/lwip.c
@@ -19,6 +19,7 @@
 /* USER CODE END Header */
 
 /* Includes ------------------------------------------------------------------*/
+#ifndef __rtems__
 #include "lwip.h"
 #include "lwip/init.h"
 #include "lwip/netif.h"
@@ -26,29 +27,29 @@
 #include "lwip/sio.h"
 #endif /* MDK ARM Compiler */
 #include "ethernetif.h"
+#include <string.h>
 
 /* USER CODE BEGIN 0 */
 
 /* USER CODE END 0 */
 /* Private function prototypes -----------------------------------------------*/
 static void ethernet_link_status_updated(struct netif *netif);
-static void Ethernet_Link_Periodic_Handle(struct netif *netif);
 /* ETH Variables initialization ----------------------------------------------*/
 void Error_Handler(void);
 
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
-uint32_t EthernetLinkTimer;
 
 /* Variables Initialization */
 struct netif gnetif;
 ip4_addr_t ipaddr;
 ip4_addr_t netmask;
 ip4_addr_t gw;
-uint8_t IP_ADDRESS[4];
-uint8_t NETMASK_ADDRESS[4];
-uint8_t GATEWAY_ADDRESS[4];
+/* 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 */
 
@@ -59,33 +60,16 @@ uint8_t GATEWAY_ADDRESS[4];
   */
 void MX_LWIP_Init(void)
 {
-  /* IP addresses initialization */
-  IP_ADDRESS[0] = 192;
-  IP_ADDRESS[1] = 168;
-  IP_ADDRESS[2] = 68;
-  IP_ADDRESS[3] = 22;
-  NETMASK_ADDRESS[0] = 255;
-  NETMASK_ADDRESS[1] = 255;
-  NETMASK_ADDRESS[2] = 255;
-  NETMASK_ADDRESS[3] = 0;
-  GATEWAY_ADDRESS[0] = 192;
-  GATEWAY_ADDRESS[1] = 168;
-  GATEWAY_ADDRESS[2] = 68;
-  GATEWAY_ADDRESS[3] = 1;
+  /* Initilialize the LwIP stack with RTOS */
+  tcpip_init( NULL, NULL );
 
-/* USER CODE BEGIN IP_ADDRESSES */
-/* USER CODE END IP_ADDRESSES */
+  /* IP addresses initialization with DHCP (IPv4) */
+  ipaddr.addr = 0;
+  netmask.addr = 0;
+  gw.addr = 0;
 
-  /* Initilialize the LwIP stack without RTOS */
-  lwip_init();
-
-  /* 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) without RTOS */
-  netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
+  /* add the network interface (IPv4/IPv6) with RTOS */
+  netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
 
   /* Registers the default network interface */
   netif_set_default(&gnetif);
@@ -105,6 +89,16 @@ void MX_LWIP_Init(void)
   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 */
 
@@ -118,54 +112,6 @@ void MX_LWIP_Init(void)
 /* USER CODE END 4 */
 #endif
 
-/**
-  * @brief  Ethernet Link periodic check
-  * @param  netif
-  * @retval None
-  */
-static void Ethernet_Link_Periodic_Handle(struct netif *netif)
-{
-/* USER CODE BEGIN 4_4_1 */
-/* USER CODE END 4_4_1 */
-
-  /* Ethernet Link every 100ms */
-  if (HAL_GetTick() - EthernetLinkTimer >= 100)
-  {
-    EthernetLinkTimer = HAL_GetTick();
-    ethernet_link_check_state(netif);
-  }
-/* USER CODE BEGIN 4_4 */
-/* USER CODE END 4_4 */
-}
-
-/**
- * ----------------------------------------------------------------------
- * Function given to help user to continue LwIP Initialization
- * Up to user to complete or change this function ...
- * Up to user to call this function in main.c in while (1) of main(void)
- *-----------------------------------------------------------------------
- * Read a received packet from the Ethernet buffers
- * Send it to the lwIP stack for handling
- * Handle timeouts if LWIP_TIMERS is set and without RTOS
- * Handle the llink status if LWIP_NETIF_LINK_CALLBACK is set and without RTOS
- */
-void MX_LWIP_Process(void)
-{
-/* USER CODE BEGIN 4_1 */
-/* USER CODE END 4_1 */
-  ethernetif_input(&gnetif);
-
-/* USER CODE BEGIN 4_2 */
-/* USER CODE END 4_2 */
-  /* Handle timeouts */
-  sys_check_timeouts();
-
-  Ethernet_Link_Periodic_Handle(&gnetif);
-
-/* USER CODE BEGIN 4_3 */
-/* USER CODE END 4_3 */
-}
-
 /**
   * @brief  Notify the User about the network interface config status
   * @param  netif: the network interface
@@ -258,3 +204,4 @@ u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
 }
 #endif /* MDK ARM Compiler */
 
+#endif /* __rtems__ */
diff --git a/stm32/lwip.h b/stm32/lwip.h
index 02df90e..fbaf786 100644
--- a/stm32/lwip.h
+++ b/stm32/lwip.h
@@ -47,6 +47,7 @@
 /* Global Variables ----------------------------------------------------------*/
 extern ETH_HandleTypeDef heth;
 
+#ifndef __rtems__
 /* LWIP init function */
 void MX_LWIP_Init(void);
 
@@ -62,6 +63,7 @@ void MX_LWIP_Process(void);
 /* USER CODE END 1 */
 #endif /* WITH_RTOS */
 
+#endif /* __rtems__ */
 #ifdef __cplusplus
 }
 #endif
-- 
2.37.2



More information about the devel mailing list