硬汉嵌入式论坛

 找回密码
 立即注册
查看: 473|回复: 0
收起左侧

[网络转载] 网络通信--AT_SURF案例No.24

[复制链接]

29

主题

5

回帖

92

积分

初级会员

积分
92
发表于 2022-8-18 19:57:49 | 显示全部楼层 |阅读模式
网络通信--AT_SURF案例No.24
简介
AT32F437的以太网模块支持通过以太网收发数据(10/100Mbps),符合IEEE 802.3-2002标准。以太网模块支持两种标准接口连接到外接的PHY:IEEE 802.3协议定义的独立于媒体的接口(MII)和简化的独立于媒体的接口(RMII)。
在SUFR板上板载了一颗型号为DM9162的PHY芯片,使用的接口为RMII。本次例程使用LWIP TCP/IP协议栈实现TCP Server功能。
LWIP是轻量型TCP IP协议栈,有无操作系统的支持都可以运行。LWIP实现的重点是在保持TCP协议主要功能的基础上减少对RAM的占用,它只需十几KB的RAM和40K左右的ROM就可以运行,这使LWIP协议栈适合在低端的嵌入式系统中使用。

资源准备
硬件环境:
对应产品型号的AT-SURF-F437 Board
软件环境:
AT32F435_437_Firmware_Library_V2.x.x\project\at_sufr_f437\examples\tcp_server
硬件设计
本案例使用的硬件资源有TFT LCD液晶显示屏、PCA9555 IO扩展芯片、DM9162芯片,对应的引脚如下:
表29. 硬件资源使用
表30. PCA9555使用
对应的电路原理如下:
图68. PHY电路原理图
图69. RJ45电路原理图
图70. PCA9555电路原理图
软件设计
TCP Server测试
- 初始化TFT LCD
- 初始化TCP Server
- 等待客户端连接
- 客户端连接上了后发送“Hello!”给客户端
- 如果收到客户端发来的数据,将接收到的数据显示在LCD屏上
代码介绍
main函数代码描述
  1. [indent]int main(void)[/indent][indent]{[/indent][indent]  /* 初始化系统时钟 */  [/indent][indent]  system_clock_config();  [/indent][indent]  /* 初始化中断优先级分组 */    [/indent][indent]  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);[/indent][indent]  /* 初始化延时函数 */[/indent][indent]  delay_init();[/indent][indent]  /* 初始化LCD */[/indent][indent]  lcd_init(LCD_DISPLAY_VERTICAL);[/indent][indent]  /* 显示信息 */  [/indent][indent]  lcd_string_show(10, 20, 200, 24, 24, (uint8_t *)"TCP Server Test");[/indent][indent]  /* 初始化 pca9555 IO扩展芯片 */[/indent][indent]  pca9555_init(PCA_I2C_CLKCTRL_400K);[/indent][indent]  /* 初始化 emac */[/indent][indent]  if(emac_system_init() == SUCCESS)[/indent][indent]  {[/indent][indent]    lcd_string_show(10, 60, 200, 24, 24, (uint8_t *)"emac init ok");[/indent][indent]  }[/indent][indent]  else[/indent][indent]  {[/indent][indent]    lcd_string_show(10, 60, 200, 24, 24, (uint8_t *)"emac init fail");[/indent][indent]    while(1);[/indent][indent]  }[/indent][indent]  /* 初始化 tcpip */[/indent][indent]  tcpip_stack_init();[/indent][indent]  /* 初始化 tcp 服务器 */[/indent][indent]  tcp_server_init();[/indent][indent]  /* 显示 ip */[/indent][indent]  lcd_string_show(10, 90, 300, 24, 24,  (uint8_t *)"ip  :    .   .   .   ");[/indent][indent]  lcd_num_show(82,  90, 200, 24, 24, local_ip[0], 3);[/indent][indent]  lcd_num_show(130, 90, 200, 24, 24, local_ip[1], 3);[/indent][indent]  lcd_num_show(178, 90, 200, 24, 24, local_ip[2], 3);[/indent][indent]  lcd_num_show(226, 90, 200, 24, 24, local_ip[3], 3);  [/indent][indent]  /* 显示网关 */[/indent][indent]  lcd_string_show(10, 120, 300, 24, 24, (uint8_t *)"gw  :    .   .   .   ");[/indent][indent]  lcd_num_show(82,  120, 200, 24, 24, local_gw[0], 3);[/indent][indent]  lcd_num_show(130, 120, 200, 24, 24, local_gw[1], 3);[/indent][indent]  lcd_num_show(178, 120, 200, 24, 24, local_gw[2], 3);[/indent][indent]  lcd_num_show(226, 120, 200, 24, 24, local_gw[3], 3);  [/indent][indent]  /* 显示掩码 */[/indent][indent]  lcd_string_show(10, 150, 300, 24, 24, (uint8_t *)"mask:    .   .   .   ");[/indent][indent]  lcd_num_show(82,  150, 200, 24, 24, local_mask[0], 3);[/indent][indent]  lcd_num_show(130, 150, 200, 24, 24, local_mask[1], 3);[/indent][indent]  lcd_num_show(178, 150, 200, 24, 24, local_mask[2], 3);[/indent][indent]  lcd_num_show(226, 150, 200, 24, 24, local_mask[3], 3);  [/indent][indent]  /* 显示服务器端口 */[/indent][indent]  lcd_string_show(10, 180, 300, 24, 24, (uint8_t *)"port:    ");[/indent][indent]  lcd_num_show(82,  180, 200, 24, 24, TCP_LOCAL_PORT, 1);[/indent][indent]  while(1)[/indent][indent]  {[/indent][indent]  }[/indent][indent]} [/indent]
复制代码

error_status emac_system_init(void)函数代码描述
  1. /**
  2. * @brief enable emac clock and gpio clock
  3. * @param none
  4. * @retval success or error
  5. */
  6. error_status emac_system_init(void)
复制代码

void tcpip_stack_init(void)函数代码描述
  1. /**
  2. * @brief initializes the lwip stack
  3. * @param none
  4. * @retval none
  5. */
  6. void tcpip_stack_init(void)
复制代码

void tcp_server_init(void)函数代码描述
  1. /**
  2. * @brief initialize tcp server
  3. * @param none
  4. * @retval none
  5. */
  6. void tcp_server_init(void)
复制代码

下载验证
首先使用网线直接连接SUFR板和电脑
然后配置电脑端的网络
图71. 电脑端网络配置
SUFR板上电,初始化TCP Server,Server IP为192.168.1.37,端口为1030
电脑端使用“网络调试助手”连接TCP Server,当成功连接上时,SUFR板将会发送一条欢迎信息“Hello!”
电脑端使用“网络调试助手”向SUFR板发送数据,SUFR板载LCD屏上显示接收到的数据,然后再将接收到的数据发送到电脑端。
图72. PC端效果
图73. SUFR板端效果

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-29 09:47 , Processed in 0.148358 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表