代码工程的GITHUB连接:
https://github.com/sj15712795029/stm32f1_marvell88w8801_marvell8801_wifi
Marvell自己实现驱动系列文章分为几篇介绍:
每篇更新打开专栏可以看到:
功能如下:
①串口配置区,默认是921600的波特率
②Wi-Fi功能开启区
③STA功能调试区
④AP功能调试区
⑤PING功能调试区
⑥TCP功能调试区
⑦UDP功能调试区
⑧云服务器调试区
⑨Debug日志区
跟PING有关的界面如下:
①要PING的IP地址
②开始PING
③停止PING
④清空PING结果
⑤显示PING结果
目前上位机跟开发板是通过串口来通信,通信格式是json,如果你没听过json,那么自行百度,跟WIFI TCP相关的json格式分别如下:
上位机用的c# winform,用的json库using Newtonsoft.Json,此库需要Newtonsoft.Json.dll库
发送代码如下:
private void json_construction_send(string func, string operate, string param1, string param2, string param3, string param4, string param5, string param6)
{
json_commmand cmd = new json_commmand();
cmd.FUNC = func;
cmd.OPERATE = operate;
cmd.PARAM1 = param1;
cmd.PARAM2 = param2;
cmd.PARAM3 = param3;
cmd.PARAM4 = param4;
cmd.PARAM5 = param5;
cmd.PARAM6 = param6;
string json_cmd = JsonConvert.SerializeObject(cmd);
#if CONSOLE_DEBUG
Console.WriteLine(json_cmd);
#endif
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(json_cmd);
}
}
其中跟wifi ap相关的operate为:
string operate_wifi_tcpc_connect = "WIFI_TCPC_CONNECT";
string operate_wifi_tcpc_disconnect = "WIFI_TCPC_DISCONNECT";
string operate_wifi_tcpc_send = "WIFI_TCPC_SEND";
string operate_wifi_tcps_connect = "WIFI_TCPS_CONNECT";
string operate_wifi_tcps_disconnect = "WIFI_TCPS_DISCONNECT";
string operate_wifi_tcps_send = "WIFI_TCPS_SEND";
uint8_t uart_receive_parse(uint8_t *shell_string)
{
uint8_t result = HW_ERR_OK;
cJSON* parse_json = cJSON_Parse((const char *)shell_string);
uint8_t* func_value = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"FUNC"))->valuestring;
uint8_t* operate_value = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"OPERATE"))->valuestring;
uint8_t* para1 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM1"))->valuestring;
uint8_t* para2 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM2"))->valuestring;
uint8_t* para3 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM3"))->valuestring;
uint8_t* para4 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM4"))->valuestring;
uint8_t* para5 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM5"))->valuestring;
if(strcmp((const char *)func_value,"WIFI") == 0)
{
if(hw_strcmp((const char *)operate_value,"WIFI_TCPC_CONNECT") == 0)
{
err_t err;
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPC_CONNECT\n");
tcp_remote_server_port = atoi((const char*)para3);
tcp_client_active_pcb = tcp_new();
ip4addr_aton((const char*)para1,&tcp_server_ip);
err = tcp_connect(tcp_client_active_pcb, &tcp_server_ip, tcp_remote_server_port, app_tcp_client_connect);
if (err != ERR_OK)
{
HW_DEBUG("Connection failed! err=%d\n", err);
tcp_close(tcp_client_active_pcb);
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPC_DISCONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPC_DISCONNECT\n");
if(tcp_client_active_pcb)
{
tcp_close(tcp_client_active_pcb);
tcp_client_active_pcb = NULL;
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPC_SEND") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPC_SEND\n");
if(tcp_client_active_pcb)
{
uint16_t send_len = atoi((const char*)para2);
HW_DEBUG("Sending TCP client packets...\n");
tcp_write(tcp_client_active_pcb, para1, send_len,1);
}
else
HW_DEBUG("no activer tcp client pcb\n");
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPS_CONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPS_CONNECT\n");
hw_memset(tcp_client,0,sizeof(tcp_server_client_t)*TCP_CLIENT_COUNT);
tcp_server_active_pcb = tcp_new();
tcp_local_server_port = atoi((const char*)para2);
HW_DEBUG("tcp server port %d\n",tcp_local_server_port);
tcp_bind(tcp_server_active_pcb, IP4_ADDR_ANY, tcp_local_server_port);
tcp_server_active_pcb = tcp_listen(tcp_server_active_pcb);
tcp_accept(tcp_server_active_pcb, app_tcp_server_accept);
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPS_DISCONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPS_DISCONNECT\n");
if(tcp_server_active_pcb)
{
hw_memset(tcp_client,0,sizeof(tcp_server_client_t)*TCP_CLIENT_COUNT);
tcp_close(tcp_server_active_pcb);
tcp_server_active_pcb = NULL;
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPS_SEND") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPS_SEND\n");
uint8_t index = 0;
uint16_t send_len = atoi((const char*)para4);
ip_addr_t tcp_temp_client_ip;
ip4addr_aton((const char*)para1,&tcp_temp_client_ip);
for(index = 0; index < TCP_CLIENT_COUNT; index )
{
if(ip4_addr_cmp(&tcp_temp_client_ip,&(tcp_client[index].tcp_client_ip)))
{
HW_DEBUG("tcp server write to client ip %s,port %d\n",(const char*)ipaddr_ntoa(&(tcp_client[index].tcp_client_pcb->remote_ip)),(tcp_client[index].tcp_client_pcb->remote_port));
tcp_write(tcp_client[index].tcp_client_pcb, para3, send_len,1);
break;
}
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
}
if(hw_strcmp((const char *)shell_string,"shop220811498.taobao.com") == 0)
HW_DEBUG("welcome to use our stm32f1 camera wifi board\n");
else
HW_DEBUG("UART PARSE ERR:HW_ERR_SHELL_NO_CMD\n");
result = HW_ERR_SHELL_NO_CMD;
exit:
cJSON_Delete(parse_json);
return result;
}
uint8_t uart_send_json(uint8_t *func,uint8_t *operate,uint8_t *status,uint8_t *para1,uint8_t *para2,uint8_t *para3,uint8_t *para4,uint8_t *para5)
{
uint8_t *wifi_status_string;
cJSON *wifi_json_status = cJSON_CreateObject();
cJSON_AddStringToObject(wifi_json_status, "FUNC", (const char*)func);
cJSON_AddStringToObject(wifi_json_status, "OPERATE", (const char*)operate);
cJSON_AddStringToObject(wifi_json_status, "STATUS", (const char*)status);
if(para1)
cJSON_AddStringToObject(wifi_json_status, "PARAM1", (const char*)para1);
if(para2)
cJSON_AddStringToObject(wifi_json_status, "PARAM2", (const char*)para2);
if(para3)
cJSON_AddStringToObject(wifi_json_status, "PARAM3", (const char*)para3);
if(para4)
cJSON_AddStringToObject(wifi_json_status, "PARAM4", (const char*)para4);
if(para5)
cJSON_AddStringToObject(wifi_json_status, "PARAM5", (const char*)para5);
wifi_status_string = (uint8_t *)cJSON_Print(wifi_json_status);
HW_DEBUG("%s\n",wifi_status_string);
cJSON_Delete(wifi_json_status);
free(wifi_status_string);
return 0;
}
private void json_status_recv_parse(json_status status)
{
#if CONSOLE_DEBUG
Console.WriteLine("----------json_status_recv_parse-------------");
Console.WriteLine("json func:" status.FUNC);
Console.WriteLine("json operate:" status.OPERATE);
Console.WriteLine("json status:" status.STATUS);
Console.WriteLine("json param1:" status.PARAM1);
Console.WriteLine("json param2:" status.PARAM2);
Console.WriteLine("json param3:" status.PARAM3);
Console.WriteLine("json param4:" status.PARAM4);
Console.WriteLine("json param5:" status.PARAM5);
Console.WriteLine("----------json_status_recv_parse end--------");
#endif
if (status.FUNC == "WIFI")
{
if (status.OPERATE == "WIFI_TCPC_RECV")
{
/* PARAM1:DATA PARAM2:LENGTH */
int tcp_recv_count = Convert.ToInt32(ltcp_recv_count.Text) Convert.ToInt32(status.PARAM2);
ltcp_recv_count.Text = tcp_recv_count.ToString();
ttcp_recv.AppendText(status.PARAM1 '\r' '\n');
}
if (status.OPERATE == "WIFI_TCPS_RECV")
{
/* PARAM1:IP PARAM2:PORT PARMA3:DATA PARAM4:LENGTH */
int tcp_recv_count = Convert.ToInt32(ltcp_recv_count.Text) Convert.ToInt32(status.PARAM4);
ltcp_recv_count.Text = tcp_recv_count.ToString();
ttcp_recv.AppendText("收到IP:" status.PARAM1 " 端口:" status.PARAM2 " 数据:" status.PARAM3 '\r' '\n');
}
}
}
连接的button会根据选择的TCP客户端/服务端去选择连接
private void btcp_connect_Click(object sender, EventArgs e)
{
if (tcp_rule == TCP_ROLE_NONE)
{
MessageBox.Show("请填入TCP协议类型", "错误提示");
return;
}
if (tremote_tcp_port.Text == "" && tcp_rule == TCP_ROLE_CLIENT)
{
MessageBox.Show("请填入远端端口", "错误提示");
return;
}
if (tlocal_tcp_port.Text == "" && tcp_rule == TCP_ROLE_SERVER)
{
MessageBox.Show("请填入本地端口", "错误提示");
return;
}
btcp_connect.Enabled = false;
btcp_disconnect.Enabled = true;
if (tcp_rule == TCP_ROLE_CLIENT)
{
if (tremote_tcp_ip.Text == "")
{
MessageBox.Show("请填入远端IP", "错误提示");
return;
}
json_construction_send(wifi_func, operate_wifi_tcpc_connect, tremote_tcp_ip.Text, null, tremote_tcp_port.Text, null, null, null);
}
else if (tcp_rule == TCP_ROLE_SERVER)
{
json_construction_send(wifi_func, operate_wifi_tcps_connect, null, tlocal_tcp_port.Text, null, null, null, null);
tremote_tcp_ip.Enabled = true;
}
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPC_CONNECT") == 0)
{
err_t err;
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPC_CONNECT\n");
tcp_remote_server_port = atoi((const char*)para3);
tcp_client_active_pcb = tcp_new();
ip4addr_aton((const char*)para1,&tcp_server_ip);
err = tcp_connect(tcp_client_active_pcb, &tcp_server_ip, tcp_remote_server_port, app_tcp_client_connect);
if (err != ERR_OK)
{
HW_DEBUG("Connection failed! err=%d\n", err);
tcp_close(tcp_client_active_pcb);
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
断开的button会根据选择的TCP客户端/服务端去选择连接
private void btcp_disconnect_Click(object sender, EventArgs e)
{
btcp_connect.Enabled = true;
btcp_disconnect.Enabled = false;
if (tcp_rule == TCP_ROLE_CLIENT)
{
json_construction_send(wifi_func, operate_wifi_tcpc_disconnect, null, null, null, null, null, null);
}
else if (tcp_rule == TCP_ROLE_SERVER)
{
json_construction_send(wifi_func, operate_wifi_tcps_disconnect, null, null, null, null, null, null);
}
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPC_DISCONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPC_DISCONNECT\n");
if(tcp_client_active_pcb)
{
tcp_close(tcp_client_active_pcb);
tcp_client_active_pcb = NULL;
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
发送的button会根据选择的TCP客户端/服务端去选择
private void btcp_send_Click(object sender, EventArgs e)
{
if (ttcp_send.Text == "")
{
MessageBox.Show("请填入要发送的内容", "错误提示");
return;
}
if (tcp_rule == TCP_ROLE_CLIENT)
{
json_construction_send(wifi_func, operate_wifi_tcpc_send, ttcp_send.Text, ttcp_send.Text.Length.ToString(), null, null, null, null);
}
else if (tcp_rule == TCP_ROLE_SERVER)
{
if (tremote_tcp_ip.Text == "")
{
MessageBox.Show("请填入远端IP", "错误提示");
return;
}
json_construction_send(wifi_func, operate_wifi_tcps_send, tremote_tcp_ip.Text, null, ttcp_send.Text, ttcp_send.Text.Length.ToString(), null, null);
}
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPC_SEND") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPC_SEND\n");
if(tcp_client_active_pcb)
{
uint16_t send_len = atoi((const char*)para2);
HW_DEBUG("Sending TCP client packets...\n");
tcp_write(tcp_client_active_pcb, para1, send_len,1);
}
else
HW_DEBUG("no activer tcp client pcb\n");
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
err_t app_tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
if (p != NULL)
{
uint8_t tcpc_recv_str[8] = {0};
hw_sprintf((char*)tcpc_recv_str,"%d",p->tot_len);
HW_DEBUG("app_tcp_client_recv len %d\n",p->tot_len);
hw_memset(app_tcp_client_recv_buffer,0,sizeof(app_tcp_client_recv_buffer));
pbuf_copy_partial(p, app_tcp_client_recv_buffer, p->tot_len, 0);
uart_send_json("WIFI","WIFI_TCPC_RECV","SUCCESS",app_tcp_client_recv_buffer,tcpc_recv_str,0,0,0);
tcp_recved(tpcb, p->tot_len);
pbuf_free(p);
}
else
{
err = tcp_close(tpcb);
HW_DEBUG("Connection is closed! err=%d\n", err);
}
return ERR_OK;
}
if (status.OPERATE == "WIFI_TCPC_RECV")
{
/* PARAM1:DATA PARAM2:LENGTH */
int tcp_recv_count = Convert.ToInt32(ltcp_recv_count.Text) Convert.ToInt32(status.PARAM2);
ltcp_recv_count.Text = tcp_recv_count.ToString();
ttcp_recv.AppendText(status.PARAM1 '\r' '\n');
}
连接的button会根据选择的TCP客户端/服务端去选择连接
private void btcp_connect_Click(object sender, EventArgs e)
{
if (tcp_rule == TCP_ROLE_NONE)
{
MessageBox.Show("请填入TCP协议类型", "错误提示");
return;
}
if (tremote_tcp_port.Text == "" && tcp_rule == TCP_ROLE_CLIENT)
{
MessageBox.Show("请填入远端端口", "错误提示");
return;
}
if (tlocal_tcp_port.Text == "" && tcp_rule == TCP_ROLE_SERVER)
{
MessageBox.Show("请填入本地端口", "错误提示");
return;
}
btcp_connect.Enabled = false;
btcp_disconnect.Enabled = true;
if (tcp_rule == TCP_ROLE_CLIENT)
{
if (tremote_tcp_ip.Text == "")
{
MessageBox.Show("请填入远端IP", "错误提示");
return;
}
json_construction_send(wifi_func, operate_wifi_tcpc_connect, tremote_tcp_ip.Text, null, tremote_tcp_port.Text, null, null, null);
}
else if (tcp_rule == TCP_ROLE_SERVER)
{
json_construction_send(wifi_func, operate_wifi_tcps_connect, null, tlocal_tcp_port.Text, null, null, null, null);
tremote_tcp_ip.Enabled = true;
}
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPS_CONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPS_CONNECT\n");
hw_memset(tcp_client,0,sizeof(tcp_server_client_t)*TCP_CLIENT_COUNT);
tcp_server_active_pcb = tcp_new();
tcp_local_server_port = atoi((const char*)para2);
HW_DEBUG("tcp server port %d\n",tcp_local_server_port);
tcp_bind(tcp_server_active_pcb, IP4_ADDR_ANY, tcp_local_server_port);
tcp_server_active_pcb = tcp_listen(tcp_server_active_pcb);
tcp_accept(tcp_server_active_pcb, app_tcp_server_accept);
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
断开的button会根据选择的TCP客户端/服务端去选择连接
private void btcp_disconnect_Click(object sender, EventArgs e)
{
btcp_connect.Enabled = true;
btcp_disconnect.Enabled = false;
if (tcp_rule == TCP_ROLE_CLIENT)
{
json_construction_send(wifi_func, operate_wifi_tcpc_disconnect, null, null, null, null, null, null);
}
else if (tcp_rule == TCP_ROLE_SERVER)
{
json_construction_send(wifi_func, operate_wifi_tcps_disconnect, null, null, null, null, null, null);
}
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPS_DISCONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPS_DISCONNECT\n");
if(tcp_server_active_pcb)
{
hw_memset(tcp_client,0,sizeof(tcp_server_client_t)*TCP_CLIENT_COUNT);
tcp_close(tcp_server_active_pcb);
tcp_server_active_pcb = NULL;
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
发送的button会根据选择的TCP客户端/服务端去选择连接
private void btcp_send_Click(object sender, EventArgs e)
{
if (ttcp_send.Text == "")
{
MessageBox.Show("请填入要发送的内容", "错误提示");
return;
}
if (tcp_rule == TCP_ROLE_CLIENT)
{
json_construction_send(wifi_func, operate_wifi_tcpc_send, ttcp_send.Text, ttcp_send.Text.Length.ToString(), null, null, null, null);
}
else if (tcp_rule == TCP_ROLE_SERVER)
{
if (tremote_tcp_ip.Text == "")
{
MessageBox.Show("请填入远端IP", "错误提示");
return;
}
json_construction_send(wifi_func, operate_wifi_tcps_send, tremote_tcp_ip.Text, null, ttcp_send.Text, ttcp_send.Text.Length.ToString(), null, null);
}
}
if(hw_strcmp((const char *)operate_value,"WIFI_TCPS_SEND") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_TCPS_SEND\n");
uint8_t index = 0;
uint16_t send_len = atoi((const char*)para4);
ip_addr_t tcp_temp_client_ip;
ip4addr_aton((const char*)para1,&tcp_temp_client_ip);
for(index = 0; index < TCP_CLIENT_COUNT; index )
{
if(ip4_addr_cmp(&tcp_temp_client_ip,&(tcp_client[index].tcp_client_ip)))
{
HW_DEBUG("tcp server write to client ip %s,port %d\n",(const char*)ipaddr_ntoa(&(tcp_client[index].tcp_client_pcb->remote_ip)),(tcp_client[index].tcp_client_pcb->remote_port));
tcp_write(tcp_client[index].tcp_client_pcb, para3, send_len,1);
break;
}
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
err_t app_tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
if (p != NULL)
{
uint8_t remote_port_str[8] = {0};
uint8_t tcps_recv_str[8] = {0};
hw_sprintf((char*)tcps_recv_str,"%d",p->tot_len);
hw_sprintf((char*)remote_port_str,"%d",tpcb->remote_port);
HW_DEBUG("TCP tester received %d bytes from %s:%d!\n", p->tot_len, ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port);
hw_memset(app_tcp_server_recv_buffer,0,sizeof(app_tcp_server_recv_buffer));
pbuf_copy_partial(p, app_tcp_server_recv_buffer, p->tot_len, 0);
uart_send_json("WIFI","WIFI_TCPS_RECV","SUCCESS",(uint8_t *)ipaddr_ntoa(&tpcb->remote_ip),remote_port_str,app_tcp_server_recv_buffer,tcps_recv_str,0);
tcp_recved(tpcb, p->tot_len);
pbuf_free(p);
}
else
{
uint8_t index = 0;
HW_DEBUG("TCP tester client %s:%d closed!\n", ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port);
for(index = 0; index < TCP_CLIENT_COUNT; index )
{
if(ip4_addr_cmp(&tpcb->remote_ip,&(tcp_client[index].tcp_client_ip)))
{
hw_memset(&(tcp_client[index]),0,sizeof(tcp_server_client_t));
break;
}
}
tcp_close(tpcb);
}
return ERR_OK;
}
if (status.OPERATE == "WIFI_TCPS_RECV")
{
/* PARAM1:IP PARAM2:PORT PARMA3:DATA PARAM4:LENGTH */
int tcp_recv_count = Convert.ToInt32(ltcp_recv_count.Text) Convert.ToInt32(status.PARAM4);
ltcp_recv_count.Text = tcp_recv_count.ToString();
ttcp_recv.AppendText("收到IP:" status.PARAM1 " 端口:" status.PARAM2 " 数据:" status.PARAM3 '\r' '\n');
}