rtthread 以太网 (LAN8720A)
查询以太网配置过程
文件路径: /drivers/board.h
1.使能rtt以太网驱动
打开 /drivers/board.h
2.初始化RMII接口硬件
通过 stm32cubemx工具使能RMII硬件,在一下路径
\Core\Src\stm32f7xx_hal_msp.c中将HAL_ETH_MspInit拷贝到 /drivers/board.c中
并根据硬件添加phy复位函数phy_reset.
void HAL_ETH_MspInit(ETH_HandleTypeDef *heth) { GPIO_InitTypeDef GPIO_Initure; __HAL_RCC_GPIOA_CLK_ENABLE(); //开启GPIOA时钟 __HAL_RCC_GPIOB_CLK_ENABLE(); //开启GPIOB时钟 __HAL_RCC_GPIOC_CLK_ENABLE(); //开启GPIOC时钟 __HAL_RCC_GPIOG_CLK_ENABLE(); //开启GPIOG时钟 __HAL_RCC_ETH1MAC_CLK_ENABLE(); //使能ETH1 MAC时钟 __HAL_RCC_ETH1TX_CLK_ENABLE(); //使能ETH1发送时钟 __HAL_RCC_ETH1RX_CLK_ENABLE(); //使能ETH1接收时钟 GPIO_Initure.Pin=GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7; GPIO_Initure.Mode=GPIO_MODE_AF_PP; //推挽复用 GPIO_Initure.Pull=GPIO_NOPULL; //不带上下拉 GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH; //高速 GPIO_Initure.Alternate=GPIO_AF11_ETH; //复用为ETH功能 HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化 //PB11 GPIO_Initure.Pin=GPIO_PIN_11; //PB11 HAL_GPIO_Init(GPIOB,&GPIO_Initure); //始化 //PC1,4,5 GPIO_Initure.Pin=GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5; //PC1,4,5 HAL_GPIO_Init(GPIOC,&GPIO_Initure); //初始化 //PG13,14 GPIO_Initure.Pin=GPIO_PIN_13|GPIO_PIN_14; //PG13,14 HAL_GPIO_Init(GPIOG,&GPIO_Initure); //初始化 HAL_NVIC_SetPriority(ETH_IRQn,1,0); //网络中断优先级应该高一点 HAL_NVIC_EnableIRQ(ETH_IRQn); }
#define RESET_IO GET_PIN(D, 0)
void phy_reset(void)
{
rt_pin_mode(RESET_IO, PIN_MODE_OUTPUT);
rt_pin_write(RESET_IO, PIN_HIGH);
rt_thread_mdelay(50);
rt_pin_write(RESET_IO, PIN_LOW);
rt_thread_mdelay(50);
rt_pin_write(RESET_IO, PIN_HIGH);
}
3.打开相应的HAL库
打开 /drivers/ stm32f7xx_hal_conf.h
使能 #define HAL_ETH_MODULE_ENABLED
4.添加lwip协议栈
打开 RT-Thread Settings->选择lwip
5.验证
6.添加网络接口设备 套接字抽象层
#include <rtthread.h> #include <arpa/inet.h> #include <netdev.h> #include <sal_socket.h> #include <sys/socket.h> #include <stdio.h> #include <string.h> #include <rtthread.h> #include <sys/socket.h> #include <netdb.h> /* RT-Thread 官网,支持 TLS 功能 */ //#define SAL_TLS_HOST "www.rt-thread.org" //#define SAL_TLS_PORT 443 #define SAL_TLS_HOST "192.168.1.100" #define SAL_TLS_PORT 8088 #define SAL_TLS_BUFSZ 1024 static const char *send_data = "GET /download/rt-thread.txt HTTP/1.1\r\n" "Host: www.rt-thread.org\r\n" "User-Agent: rtthread/4.0.1 rtt\r\n\r\n"; void sal_tls_test(void) { int ret, i; char *recv_data; struct hostent *host; int sock = -1, bytes_received; struct sockaddr_in server_addr; /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = gethostbyname(SAL_TLS_HOST); recv_data = rt_calloc(1, SAL_TLS_BUFSZ); if (recv_data == RT_NULL) { rt_kprintf("No memory\n"); return; } /* 创建一个socket,类型是SOCKET_STREAM,TCP 协议, TLS 类型 */ // if ((sock = socket(AF_INET, SOCK_STREAM, PROTOCOL_TLS)) < 0) if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { rt_kprintf("Socket error\n"); goto __exit; } /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SAL_TLS_PORT); server_addr.sin_addr = *((struct in_addr *)host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0) { rt_kprintf("Connect fail!\n"); goto __exit; } /* 发送数据到 socket 连接 */ ret = send(sock, send_data, strlen(send_data), 0); if (ret <= 0) { rt_kprintf("send error,close the socket.\n"); goto __exit; } /* 接收并打印响应的数据,使用加密数据传输 */ bytes_received = recv(sock, recv_data, SAL_TLS_BUFSZ - 1, 0); if (bytes_received <= 0) { rt_kprintf("received error,close the socket.\n"); goto __exit; } rt_kprintf("recv data:\n"); for (i = 0; i < bytes_received; i++) { rt_kprintf("%c", recv_data[i]); } __exit: if (recv_data) rt_free(recv_data); if (sock >= 0) closesocket(sock); } #ifdef FINSH_USING_MSH #include <finsh.h> MSH_CMD_EXPORT(sal_tls_test, SAL TLS function test); #endif /* FINSH_USING_MSH */