关键词
TC397 官方例程;TC397 以太网例程;TC397 GETH;
简介
本篇为 Aurix TriCore TC397 以太网官方例程分析,重点关注其硬件行为
调试所用的开发板型号:KIT-A2G-TC397-5V-TFT
所使用的例程:Ethernet_1_KIT_TC397_TFT
英飞凌 TriCore 官方例程下载地址:
GitHub - Infineon/AURIX_code_examples
系列文章链接
TC397以太网例程 - 17.ECHO 应用 - 轮询定时器
TC397以太网例程 - 18.ECHO 应用 - 接收报文
目录
LwIP 的设置分为两部分:MAC 地址设置、LwIP 初始化
LwIP 初始化流程中会对内存、调试所使用的串口、网卡、协议等进行初始化
// Cpu0_Main.c
void core0_main (void)
{
...
// --------------------------------
// 设置 MAC 地址
/* Define a MAC Address */
eth_addr_t ethAddr;
ethAddr.addr[0] = 0xDE;
ethAddr.addr[1] = 0xAD;
ethAddr.addr[2] = 0xBE;
ethAddr.addr[3] = 0xEF;
ethAddr.addr[4] = 0xFE;
ethAddr.addr[5] = 0xED;
// --------------------------------
// --------------------------------
// 初始化 LwIP
Ifx_Lwip_init(ethAddr); /* Initialize LwIP with the MAC address */
// --------------------------------
1.MAC 地址设置
MAC 地址变量声明为结构体,成员类型为 u8_t,声明时使用了 __attribute__ ((__packed__)),其作用是取消编译器在编译过程中对结构体的对齐优化,结构体按照实际占用的字节数进行对齐,如下所示:
// Libraries/Ethernet/lwip/port/include/arch/cc.h
#define PACK_STRUCT_STRUCT __attribute__ ((__packed__))
----------------------------------------------------------------
// Libraries/Ethernet/lwip/src/include/lwip/prot/ethernet.h
/** An Ethernet MAC address */
struct eth_addr {
PACK_STRUCT_FLD_8(u8_t addr[ETH_HWADDR_LEN]);
} PACK_STRUCT_STRUCT;
----------------------------------------------------------------
// Cpu0_Main.c
void core0_main (void)
{
...
// 设置 MAC 地址
/* Define a MAC Address */
eth_addr_t ethAddr;
ethAddr.addr[0] = 0xDE;
ethAddr.addr[1] = 0xAD;
ethAddr.addr[2] = 0xBE;
ethAddr.addr[3] = 0xEF;
ethAddr.addr[4] = 0xFE;
ethAddr.addr[5] = 0xED;
LwIP 使用用户定义的 MAC 地址完成相关的初始化工作,如下所示:
// Cpu0_Main.c
void core0_main (void)
{
...
// --------------------------------
// 设置 MAC 地址
/* Define a MAC Address */
eth_addr_t ethAddr;
ethAddr.addr[0] = 0xDE;
ethAddr.addr[1] = 0xAD;
ethAddr.addr[2] = 0xBE;
ethAddr.addr[3] = 0xEF;
ethAddr.addr[4] = 0xFE;
ethAddr.addr[5] = 0xED;
// --------------------------------
// --------------------------------
// 初始化 LwIP
Ifx_Lwip_init(ethAddr); /* Initialize LwIP with the MAC address */
// --------------------------------
2.LwIP 配置相关的宏
LwIP 初始化及程序执行过程中涉及大量宏,其控制相关功能的使能/禁用,英飞凌使用的 LwIP 宏定义如下:
// Configurations/lwipopts.h
#define NO_SYS 1 /* Use LwIP without Operating System (no threads, no semaphores, etc.) */
#define LWIP_NETIF_HOSTNAME 1 /* Enable hostname option in DHCP */
#define BOARDNAME "AURIXTC397TFT" /* Board name, also used as hostname */
#define MEM_ALIGNMENT 4 /* Set memory alignment to 4 byte (32-bit machine) */
#define MEM_SIZE (25 * 1024) /* Size of the Heap */
#define LWIP_DHCP 1 /* Enable DHCP protocol */
#define LWIP_NETCONN 0 /* Disable Netconn API */
#define LWIP_SOCKET 0 /* Disable the Socket API */
#define SYS_LIGHTWEIGHT_PROT 0 /* Disable inter-task protection */
#define ETH_PAD_SIZE 2 /* Add 2 bytes before the Ethernet header to ensure payload alignment */
#define __LWIP_DEBUG__ /* Enable debugging through UART interface */
#define LWIP_NETIF_EXT_STATUS_CALLBACK 1 /* Enable an extended callback function for netif */
#ifdef __LWIP_DEBUG__
#define LWIP_DEBUG /* Enable LwIP debugging */
#endif
#ifndef IFX_LWIP_DEBUG
#define IFX_LWIP_DEBUG LWIP_DBG_OFF /* IFX LwIP debug level set to OFF */
#endif
#define DHCP_DEBUG LWIP_DBG_OFF /* Enable DHCP Debug */
#define NETIF_DEBUG LWIP_DBG_ON /* Enable NETIF Debug */
#define LWIP_DBG_TYPES_ON LWIP_DBG_STATE /* Enable only module state debug messages */
相关宏的含义如下所示:
-
NO_SYS:"1" 表示 LwIP 不依赖操作系统独立运行,不存在线程、信号量、互斥锁等,因此无法使用线程相关的 API(socket、netconn 等 "api" 文件夹中的所有内容),仅可使用回调风格的 API (用户要自己处理同步问题)
-
LWIP_NETIF_HOSTNAME:"1" 表示配置主机名称,允许设置 netif->hostname,可通过 netif_set_hostname() 设置主机名称
-
BOARDNAME:主机名称 - "AURIXTC397TFT"
-
MEM_ALIGNMENT:"4" 表示内存按照 4 字节对齐
-
MEM_SIZE:堆内存大小,如果应用程序需要收发的数据量较大,应该将其设置为较大的值
-
LWIP_DHCP:"1" 表示启用 DHCP 协议
-
LWIP_NETCONN:"0" 表示禁用 Netconn API
-
LWIP_SOCKET:"0" 表示禁用 Socket API
-
SYS_LIGHTWEIGHT_PROT:"0" 表示禁用任务间切换保护,多任务时分配缓冲区防止数据丢失,单线程执行时可以禁用,多线程执行时必须配置启用
-
ETH_PAD_SIZE:"2" 表示在报文帧头部添加 2 个字节确保对齐
-
__LWIP_DEBUG__:调试信息打印的总开关,该宏仅有定义没有值,定义该宏后才可以控制各个协议内部调试信息是否通过 UART 打印
-
LWIP_NETIF_EXT_STATUS_CALLBACK:"1" 表示添加额外的回调函数,允许 netif_add_ext_callback() 设置额外的回调函数
-
LWIP_DEBUG:调试信息打印的开关,该宏仅有定义没有值
-
IFX_LWIP_DEBUG:"LWIP_DBG_OFF" 英飞凌设置的调试信息打印开关,这里设置为禁用
-
DHCP_DEBUG:"LWIP_DBG_OFF" 禁用 DHCP 调试信息
-
NETIF_DEBUG:"LWIP_DBG_ON" 启用 netif 调试信息
-
LWIP_DBG_TYPES_ON:调试信息的类型,具体如下所示:
// Libraries/Ethernet/lwip/src/include/lwip/debug.h
/** @name Debug message types (LWIP_DBG_TYPES_ON) */
/** flag for LWIP_DEBUGF indicating a tracing message (to follow program flow) */
#define LWIP_DBG_TRACE 0x40U
/** flag for LWIP_DEBUGF indicating a state debug message (to follow module states) */
#define LWIP_DBG_STATE 0x20U
/** flag for LWIP_DEBUGF indicating newly added code, not thoroughly tested yet */
#define LWIP_DBG_FRESH 0x10U
/** flag for LWIP_DEBUGF to halt after printing this debug message */
#define LWIP_DBG_HALT 0x08U
3.初始化流程概览
// Cpu0_Main.c
void core0_main (void)
{
...
Ifx_Lwip_init(ethAddr); /* Initialize LwIP with the MAC address */
LwIP 的初始化包含如下几个部分:
-
串口配置
-
声明 IP 地址
-
内存、协议等初始化
-
网卡 netif 配置
-
主机名称设置
-
配置 DHCP
-
回调函数绑定
// Libraries/Ethernet/lwip/port/src/Ifx_Lwip.c
void Ifx_Lwip_init(eth_addr_t ethAddr)
{
// ---------------------------------------
// 配置调试输出所需的 ASCLIN 串口
//Init uart for debugging
initUART();
// ---------------------------------------
// ---------------------------------------
// 声明 IP 地址
ip_addr_t default_ipaddr, default_netmask, default_gw;
IP4_ADDR(&default_gw, 0,0,0,0);
IP4_ADDR(&default_ipaddr, 0,0,0,0);
IP4_ADDR(&default_netmask, 255,0,0,0);
LWIP_DEBUGF(IFX_LWIP_DEBUG, ("Ifx_Lwip_init start!\n"));
// ---------------------------------------
// ---------------------------------------
// netif、内存、协议等初始化
/** - initialise LWIP (lwip_init()) */
lwip_init();
// ---------------------------------------
// ---------------------------------------
// netif 配置
/** - initialise and add a \ref netif */
g_Lwip.eth_addr = ethAddr;
netif_add(&g_Lwip.netif, &default_ipaddr, &default_netmask, &default_gw,
(void *)0, ifx_netif_init, ethernet_input);
netif_set_default(&g_Lwip.netif);
netif_set_up(&g_Lwip.netif);
// ---------------------------------------
// ---------------------------------------
// 设置主机名称
#if LWIP_NETIF_HOSTNAME
g_Lwip.netif.hostname = BOARDNAME;
#endif
// ---------------------------------------
// ---------------------------------------
// 配置 DHCP 并启动
#if LWIP_DHCP
/** - assign \ref dhcp to \ref netif */
dhcp_set_struct(&g_Lwip.netif, &g_Lwip.dhcp);
/* we start the dhcp always here also when we don't have a link here */
dhcp_start(&g_Lwip.netif);
#endif
// ---------------------------------------
// ---------------------------------------
// 添加回调函数,监测 netif 状态变化,即网口监听
#if LWIP_NETIF_EXT_STATUS_CALLBACK
netif_add_ext_callback(&g_extCallback, netif_state_changed);
#endif
// ---------------------------------------
LWIP_DEBUGF(IFX_LWIP_DEBUG, ("Ifx_Lwip_init end!\n"));
}
下一篇: TC397以太网例程 - 4.串口配置