[Jihai APM32F407 Tiny Board] Porting LWIP protocol stack
[Copy link]
In this article, we will learn how to port the LWIP protocol stack and implement pinging the network. The official SDK package contains the lwip-1.4.1 version. Next, we will port the latest version to the development board.
1. Download source code:
Download the latest version of lwip source code
Download address: http://download.savannah.nongnu.org/releases/lwip/
2. Add files to the project
2.1. Unzip the downloaded source code to the Middlewares folder under the project file created in the previous article
2.2. Copy arch file
Copy the arch file in the ETH example under the official SDK to the Middlewares\lwip-2.1.3 file
2.3. Copy LWIP and network card files
2.4. Add files to the project
2.4.1、Add API
2.4.2. Add core
2.4.3. Add netif
2.4.4. Add arch
2.4.5. Add network card driver
2.4.6. Add header files
2.4.5. Add code to systick interrupt function
2.4.6. After adding the file, compile the code and modify the corresponding file according to the error prompt. I will not list it here.
3. Main Program
main.c
#include "main.h"
#include "Board.h"
#include "oled.h"
#include "stdio.h"
#include "usart.h"
/** Global pointers on Rx descriptor used to transmit and receive descriptors */
extern ETH_DMADescConfig_T *DMARxDescToGet;
/** current Ethernet LocalTime value */
volatile uint32_t ETHTimer = 0;
/** lwip network interface structure for ethernetif */
struct netif UserNetif;
/** TCP periodic Timer */
uint32_t TCPTimer = 0;
/** ARP periodic Timer */
uint32_t ARPTimer = 0;
/** MAC address */
uint8_t SetMACaddr[6] = {0,0,0,0,0,8};
int main(void)
{
SysTick_Init();
init_led();
//init_i2c1();
I2CInit();
init_usart();
ConfigEthernet();
LwIP_Init();
//OLED_Init();
// led2_off();
// led3_on();
while (1)
{
/** check if any packet received */
if (ETH_ReadRxPacketSize(DMARxDescToGet))
{
/** process received ethernet packet */
LwIP_Pkt_Handle();
}
/** handle periodic timers for LwIP */
LwIP_Periodic_Handle(ETHTimer);
}
}
void LwIP_Init(void)
{
ip4_addr_t ipaddr;
ip4_addr_t netmask;
ip4_addr_t gw;
/** Initializes the dynamic memory heap */
mem_init();
/** Initializes the memory pools */
memp_init();
IP4_ADDR(&ipaddr, 192, 168, 1, 22);
IP4_ADDR(&netmask, 255, 255 , 255, 0);
IP4_ADDR(&gw, 192, 168, 1, 1);
/** Config MAC Address */
ETH_ConfigMACAddress(ETH_MAC_ADDRESS0, SetMACaddr);
/** Add a network interface to the list of lwIP netifs */
netif_add(&UserNetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/** Registers the default network interface */
netif_set_default(&UserNetif);
/** When the netif is fully configured this function must be called */
netif_set_up(&UserNetif);
/** Use Com printf static IP address*/
printf("\n Static IP address \r\n");
printf("IP: %d.%d.%d.%d\r\n",192,168,1,22);
printf("NETMASK: %d.%d.%d.%d\r\n",255,255,255,0);
printf("Gateway: %d.%d.%d.%d\r\n",192,168,1,1);
}
/*!
* @brief This function received ethernet packet
*
* @param None
*
* @retval None
*/
void LwIP_Pkt_Handle(void)
{
/** Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
ethernetif_input(&UserNetif);
}
/*!
* @brief This function LwIP periodic tasks
*
* @param ETHTimer the current Ethernet Timer value
*
* @retval None
*/
void LwIP_Periodic_Handle(__IO uint32_t ETHTimer)
{
#if LWIP_TCP
/** TCP periodic process every 250 ms */
if (ETHTimer - TCPTimer >= TCP_TMR_INTERVAL)
{
TCPTimer = ETHTimer;
tcp_tmr();
}
#endif
/** ARP periodic process every 5s */
if ((ETHTimer - ARPTimer) >= ARP_TMR_INTERVAL)
{
ARPTimer = ETHTimer;
etharp_tmr();
}
}
4. Program operation
4.1. Connect the network cable to the hardware
4.2 Serial port output
4.3. Ping the network
|