新建wifi工程
打开rasc新建wifi_demo工程:


新建debug_uart->stack 用于串口监视

新建wifi_uart->用于连接esp8266
同uart4建立uart9

最后生成工程,用MDK打开工程
工程文件的建立
1、新建bsp文件夹,并建立debug、esp通迅的两对文件:

2、debug_uart.h内容如下:
#ifndef __DEBUG_UART_H
#define __DEBUG_UART_H
#include "hal_data.h"
#include "stdio.h"
void Debug_UART4_Init(void);
#endif
debug_uart4.c内容如下:
#include "debug_uart.h"
volatile bool debug_send_complete_flag = false;
void debug_uart4_callback (uart_callback_args_t * p_args)
{
switch (p_args->event)
{
case UART_EVENT_RX_CHAR:
{
R_SCI_UART_Write(&wif_uart9_ctrl, (uint8_t *)&(p_args->data), 1);
break;
}
case UART_EVENT_TX_DATA_EMPTY:
{
debug_send_complete_flag = true;
break;
}
default:
break;
}
}
void Debug_UART4_Init(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open (&debug_uart4_ctrl, &debug_uart4_cfg);
assert(FSP_SUCCESS == err);
}
#if defined __GNUC__ && !defined __clang__
int _write(int fd, char *pBuffer, int size);
int _write(int fd, char *pBuffer, int size)
{
(void)fd;
R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t *)pBuffer, (uint32_t)size);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return size;
}
#else
int fputc(int ch, FILE *f)
{
(void)f;
R_SCI_UART_Write(&debug_uart4_ctrl, (uint8_t *)&ch, 1);
while(debug_send_complete_flag == false);
debug_send_complete_flag = false;
return ch;
}
#endif
wif_esp8266.h
#ifndef __WIFI_ESP8266_H
#define __WIFI_ESP8266_H
#include "hal_data.h"
#include "stdio.h"
void ESP8266_UART9_Init(void);
#endif
wif_esp8266.c内容如下:
#include "wif_esp8266.h"
volatile bool esp8266_send_complete_flag = false;
void wifi_uart_callback(uart_callback_args_t * p_args)
{
switch (p_args->event)
{
case UART_EVENT_RX_CHAR:
{
R_SCI_UART_Write(&debug_uart4_ctrl, (uint8_t *)&(p_args->data), 1);
break;
}
case UART_EVENT_TX_DATA_EMPTY:
{
esp8266_send_complete_flag = true;
break;
}
default:
break;
}
}
void ESP8266_UART9_Init(void)
{
R_SCI_UART_Open(wif_uart9.p_ctrl, wif_uart9.p_cfg);
}
wifi_esp8266.c主要的功能就是把debug串口的数据转发给uart9,同时把接收到的数据也发给uart4。
debug_uart4的功能也是转发ttl数,这样先完成手工测试连网的状态。
hal_entry.c引用两个头文件,在main.c中初始化两个串口:
#include "hal_data.h"
#include "debug_uart.h"
#include "wif_esp8266.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
ESP8266_UART9_Init();
Debug_UART4_Init();
printf("start...\r\n");
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
测试环境:
1、串口调试助手通过typc接到ch340G连接开发板的uart4。
2、测试用的电脑用tcpserver创建一个TCP服务器,端口号为8080:

测试过程
打开串口调试助手
1、AT命令:

2、关回显ATE0:

3、AT+CWMODE 选择 WIFI 应用模式 1、为station

4、AT+CWJAP=“SSD”,“PWD” 加入AP

5、连接服务器AT+CIPSTART="TCP","IP",端口

服务端显示已经连接:

6、调置透传模式 AT+CIPMODE=1

7、进入透传模式,AT+CIPSEND,出现>就表示可以透传了

8、收发数据见下图:

9、退出透传发送+++(后面不要加回车键)就可以通出秀传了

【总结】通过两个串口的回调函数,就可以很方便的做完这次测试。两个串口表现非常完美。结合自己的工程需求就可以轻松连上网。
后续将继续就esp8266数据秀传做测试。