代码工程的GITHUB连接:
https://github.com/sj15712795029/stm32f1_marvell88w8801_marvell8801_wifi
Marvell自己实现驱动系列文章分为几篇介绍:
每篇更新打开专栏可以看到:
功能如下:
①串口配置区,默认是921600的波特率
②Wi-Fi功能开启区
③STA功能调试区
④AP功能调试区
⑤PING功能调试区
⑥TCP功能调试区
⑦UDP功能调试区
⑧云服务器调试区
⑨Debug日志区
跟UDP有关的界面如下:
只介绍①②地方,其他地方很明了
①协议类型,主要是TCP客户端,TCP服务端
②如果对于TCP客户端,那么蓝色button是连接TCP Server端,红色是断开连接
如果是TCP 服务端,那么蓝色button是建立TCP server端,红色是取消TCP server端
在介绍SDIO wifi Marvell8801/Marvell88w8801之前先附上模块链接: //
Marvell自己实现驱动系列文章分为几篇介绍:
SDIO wifi Marvell8801/Marvell88w8801 介绍(一) ---- 芯片介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(二) ---- SDIO协议介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(三) ---- 寄存器介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(四) ---- 命令/事件/数据格式
SDIO wifi Marvell8801/Marvell88w8801 介绍(五) ---- TLV
SDIO wifi Marvell8801/Marvell88w8801 介绍(六) ---- 实现初始化功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(七) ---- 实现搜索功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(八) ---- 实现STA功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(九) ---- 实现AP功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(十) ---- 移植TCP/IP协议栈LWIP
SDIO wifi Marvell8801/Marvell88w8801 介绍(十一) ---- 自己编写LWIP没有的DHCP server
SDIO wifi Marvell8801/Marvell88w8801 介绍(十二) ---- MQTT介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(十三) ---- 百度云操作说明
SDIO wifi Marvell8801/Marvell88w8801 介绍(十四) ---- 上位机STA操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十五) ---- 上位机AP操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十六) ---- 上位机UDP操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十七) ---- 上位机TCP操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十八) ---- 上位机PING操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十九) ---- 上位机云服务器(百度云)操作/代码
每篇更新打开专栏可以看到:
功能如下:
①串口配置区,默认是921600的波特率
②Wi-Fi功能开启区
③STA功能调试区
④AP功能调试区
⑤PING功能调试区
⑥TCP功能调试区
⑦UDP功能调试区
⑧云服务器调试区
⑨Debug日志区
跟PING有关的界面如下:
①要PING的IP地址
②开始PING
③停止PING
④清空PING结果
⑤显示PING结果
目前上位机跟开发板是通过串口来通信,通信格式是json,如果你没听过json,那么自行百度,跟WIFI UDP相关的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 udp相关的operate为:
string operate_wifi_udp_connect = "WIFI_UDP_CONNECT";
string operate_wifi_udp_disconnect = "WIFI_UDP_DISCONNECT";
string operate_wifi_udp_send = "WIFI_UDP_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_UDP_CONNECT") == 0)
{
udp_local_port = atoi((const char*)para2);
udp_remote_port = atoi((const char*)para3);
HW_DEBUG("UART PARSE DEBUG:operate WIFI_UDP_CONNECT\n");
ip4addr_aton((const char *)para1,&udp_remote_ipaddr);
udp_active_pcb = udp_new();
udp_bind(udp_active_pcb, IP4_ADDR_ANY, udp_local_port);
udp_recv(udp_active_pcb, app_udp_recv, 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_UDP_DISCONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_UDP_DISCONNECT\n");
if(udp_active_pcb)
{
udp_remove(udp_active_pcb);
udp_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_UDP_SEND") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_UDP_SEND\n");
if(udp_active_pcb)
{
uint16_t send_len = atoi((const char*)para2);
struct pbuf *send;
send = pbuf_alloc(PBUF_TRANSPORT, send_len, PBUF_ROM);
if (send != NULL)
{
HW_DEBUG("Sending UDP packets...\n");
send->payload = para1;
udp_sendto(udp_active_pcb, send, &udp_remote_ipaddr, udp_remote_port);
pbuf_free(send);
}
}
}
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_UDP_RECV")
{
/* PARAM1:DATA PARAM2:LENGTH */
int udp_recv_count = Convert.ToInt32(ludp_recv_count.Text) Convert.ToInt32(status.PARAM2);
ludp_recv_count.Text = udp_recv_count.ToString();
tudp_recv.AppendText(status.PARAM1 '\r' '\n');
}
}
}
private void budp_connect_Click(object sender, EventArgs e)
{
if (tlocal_udp_port.Text == "")
{
MessageBox.Show("请填入要开启的本地端口", "错误提示");
return;
}
if (tremote_udp_port.Text == "")
{
MessageBox.Show("请填入要开启的远端端口", "错误提示");
return;
}
if (tremote_udp_ip.Text == "")
{
MessageBox.Show("请填入要开启的远端IP", "错误提示");
return;
}
budp_connect.Enabled = false;
budp_disconnect.Enabled = true;
json_construction_send(wifi_func, operate_wifi_udp_connect, tremote_udp_ip.Text, tlocal_udp_port.Text, tremote_udp_port.Text, null, null, null);
}
if(hw_strcmp((const char *)operate_value,"WIFI_UDP_CONNECT") == 0)
{
udp_local_port = atoi((const char*)para2);
udp_remote_port = atoi((const char*)para3);
HW_DEBUG("UART PARSE DEBUG:operate WIFI_UDP_CONNECT\n");
ip4addr_aton((const char *)para1,&udp_remote_ipaddr);
udp_active_pcb = udp_new();
udp_bind(udp_active_pcb, IP4_ADDR_ANY, udp_local_port);
udp_recv(udp_active_pcb, app_udp_recv, NULL);
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
private void budp_disconnect_Click(object sender, EventArgs e)
{
budp_connect.Enabled = true;
budp_disconnect.Enabled = false;
json_construction_send(wifi_func, operate_wifi_udp_disconnect, null, null, null, null, null, null);
}
if(hw_strcmp((const char *)operate_value,"WIFI_UDP_DISCONNECT") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_UDP_DISCONNECT\n");
if(udp_active_pcb)
{
udp_remove(udp_active_pcb);
udp_active_pcb = NULL;
}
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
private void budp_send_Click(object sender, EventArgs e)
{
if (tudp_send.Text == "")
{
MessageBox.Show("请填入要发送的内容", "错误提示");
return;
}
json_construction_send(wifi_func, operate_wifi_udp_send, tudp_send.Text, tudp_send.Text.Length.ToString(), null, null, null, null);
int udp_send_count = Convert.ToInt32(ludp_send_count.Text) tudp_send.Text.Length;
ludp_send_count.Text = udp_send_count.ToString();
}
if(hw_strcmp((const char *)operate_value,"WIFI_UDP_SEND") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate WIFI_UDP_SEND\n");
if(udp_active_pcb)
{
uint16_t send_len = atoi((const char*)para2);
struct pbuf *send;
send = pbuf_alloc(PBUF_TRANSPORT, send_len, PBUF_ROM);
if (send != NULL)
{
HW_DEBUG("Sending UDP packets...\n");
send->payload = para1;
udp_sendto(udp_active_pcb, send, &udp_remote_ipaddr, udp_remote_port);
pbuf_free(send);
}
}
else
HW_DEBUG("no activer udp pcb\n");
operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
goto exit;
}
void app_udp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
if (p != NULL)
{
uint8_t udp_recv_str[8] = {0};
hw_sprintf((char*)udp_recv_str,"%d",p->tot_len);
HW_DEBUG("app_udp_recv len %d\n",p->tot_len);
memset(app_udp_recv_buffer,0,sizeof(app_udp_recv_buffer));
pbuf_copy_partial(p, app_udp_recv_buffer, p->tot_len, 0);
uart_send_json("WIFI","WIFI_UDP_RECV","SUCCESS",app_udp_recv_buffer,udp_recv_str,0,0,0);
pbuf_free(p);
}
}
if (status.OPERATE == "WIFI_UDP_RECV")
{
/* PARAM1:DATA PARAM2:LENGTH */
int udp_recv_count = Convert.ToInt32(ludp_recv_count.Text) Convert.ToInt32(status.PARAM2);
ludp_recv_count.Text = udp_recv_count.ToString();
tudp_recv.AppendText(status.PARAM1 '\r' '\n');
}