RT-Thread提供了对FRDM-MCXN947板卡的支持,使用env配置环境可以对系统进行裁剪,本帖通过配置以太网功能和TCP服务器软件包,实现通过网络调试助手TCP客户端连接控制板载LED,menuconfig配置如下:
使能PHY驱动
在RT-Thread Components -> Device Drivers中选择Using ethernet phy device drivers
使能网络组件
在RT-Thread Components -> Network中选择网络相关的组件
开启软件包tcpserver
创建一个tcp服务器,电脑上位机作为客户端连接服务器与它通信,服务器句柄变量定义如下:
static struct tcpserver *serv;tcpclient_t hclient;
tcp服务器初始化,绑定5002端口:
serv = tcpserver_create(NULL, 5002); tcpserver_set_notify_callback(serv, tcpserver_event_notify);
RGB LED
板载了RGB LED,原理图如下:
RGB LED管脚定义:
#define LEDR_PIN ((0*32)+10)#define LEDG_PIN ((0*32)+27)#define LEDB_PIN ((1*32)+2)
RGB LED管脚初始化:
rt_pin_mode(LEDR_PIN, PIN_MODE_OUTPUT); rt_pin_mode(LEDG_PIN, PIN_MODE_OUTPUT); rt_pin_mode(LEDB_PIN, PIN_MODE_OUTPUT); rt_pin_write(LEDR_PIN, PIN_HIGH); rt_pin_write(LEDG_PIN, PIN_HIGH); rt_pin_write(LEDB_PIN, PIN_HIGH);
网络数据接收以及RGB LED控制:
static void tcpserver_event_notify(tcpclient_t client, rt_uint8_t event) {int ret;switch (event) {case TCPSERVER_EVENT_CONNECT:LOG_D("client connect:%d", client->sock); hclient = client; break; case TCPSERVER_EVENT_RECV:rt_memset(buf,0,sizeof(buf)); ret = tcpserver_recv(client, buf, sizeof(buf), -1); if (ret > 0){LOG_D("[%d]recv:%s", client->sock,buf); if(rt_strcmp(buf,"LED_R=0")==0){rt_pin_write(LEDR_PIN, PIN_HIGH); rt_kprintf("red led off\n");} else if(rt_strcmp(buf,"LED_R=1")==0) {rt_pin_write(LEDR_PIN, PIN_LOW); rt_kprintf("red led on\n");} else if(rt_strcmp(buf,"LED_G=0")==0) {rt_pin_write(LEDG_PIN, PIN_HIGH); rt_kprintf("green led off\n");} else if(rt_strcmp(buf,"LED_G=1")==0) {rt_pin_write(LEDG_PIN, PIN_LOW); rt_kprintf("green led on\n");} else if(rt_strcmp(buf,"LED_B=0")==0) {rt_pin_write(LEDB_PIN, PIN_HIGH); rt_kprintf("blue led off\n");} else if(rt_strcmp(buf,"LED_B=1")==0) {rt_pin_write(LEDB_PIN, PIN_LOW); rt_kprintf("blue led on\n");}} break; case TCPSERVER_EVENT_DISCONNECT:LOG_D("client disconnect:%d", client->sock); //tcpserver_close(hclient); hclient = NULL;break;default:break;}}
效果:
工程代码分享:frdm-mcxn947.zip