把sdk拷贝到Ubuntu16.04环境下,进入lwip_socket里面找到main.c,使用gedit打开main文件,增加如下内容
首先设定AP的参数及服务器IP和端口
#define WIFI_SSID ("testlk")
#define WIFI_PASSWORD ("12345678")
#define Serverip_addr ("192.168.43.98")
#define SOCK_TCP_SRV_PORT 6500
定义串口缓存
#define VFIFO_SIZE (256)
ATTR_ZIDATA_IN_NONCACHED_RAM_4BYTE_ALIGN sta
tic uint8_t g_uart_send_buffer [VFIFO_SIZE];
定义WiFi缓存
#define WIFI_TX_BUF_SIZE 100
uint8_t wifi_send_buf[WIFI_TX_BUF_SIZE];
继续定义WiFi透传使用的结构体积相关变量
typedef struct
{
uint16_t size;
uint8_t * data;
}wifi_send_msg_t;
wifi_send_msg_t wifi_msg;
static QueueHandle_t wifi_send_queue;
#define WIFI_SEND_QUEUE_LENGTH 1
typedef struct MSG
{
uint32_t size;
uint8_t data[WIFI_TX_BUF_SIZE];
}MSG_T;
MSG_T g_tMsg;
随后修改tcp客户端代码,代码如下
static void client_test_thread(void *not_used)
{
int ret;
char send_data[] = "send reveive uart data to network!n";
ret = tcp_client_test();
if (ret == 0) {
LOG_I(lwip_socket_example, "example project test success.n");
}
//Keep the task alive
while (1) {
if(xQueueReceive(wifi_send_queue,(void *)&g_tMsg,portMAX_DELAY) == pdPASS )
{
ret = lwip_write(socket_handle,g_uart_send_buffer, g_tMsg.size);
LOG_I(lwip_socket_example, " lwip wrtie uart data success.n");
g_lwip_write_done=true;
}
else
{
LOG_I(lwip_socket_example, " lwip no uart data receive.n");
vTaskDelay(1000 / portTICK_RATE_MS); // release CPU
}
}
}
增加串口初始化和回调函数,代码如下
static void uart_read_from_input(hal_uart_callback_event_t event, void *user_data)
{
if (event == HAL_UART_EVENT_READY_TO_READ)
{
g_uart_receive_event = true;
printf("rcv flag true");
}
}
void Uart_Thread(void * p)
{
hal_uart_config_t basic_config;
hal_uart_dma_config_t dma_config;
uint32_t length;
memset(g_uart_send_buffer, 0x00, sizeof(g_uart_send_buffer));
/* Step1: Call hal_pinmux_set_function() to set GPIO pinmux, if EPT tool was not used to configure the related pinmux.*/
hal_gpio_init(HAL_GPIO_11);
hal_gpio_init(HAL_GPIO_12);
hal_pinmux_set_function(HAL_GPIO_11, HAL_GPIO_11_URXD2);
hal_pinmux_set_function(HAL_GPIO_12, HAL_GPIO_12_UTXD2);
/* Configure UART port with basic function */
basic_config.baudrate = HAL_UART_BAUDRATE_115200;
basic_config.parity = HAL_UART_PARITY_NONE;
basic_config.stop_bit = HAL_UART_STOP_BIT_1;
basic_config.word_length = HAL_UART_WORD_LENGTH_8;
hal_uart_init(HAL_UART_2, &basic_config);
/*Step2: Configure UART port to dma mode. */
dma_config.receive_vfifo_alert_size = RECEIVE_ALERT_SIZE;
dma_config.receive_vfifo_buffer = g_uart_receive_buffer;
dma_config.receive_vfifo_buffer_size = VFIFO_SIZE;
dma_config.receive_vfifo_threshold_size = RECEIVE_THRESHOLD_SIZE;
dma_config.send_vfifo_buffer = g_uart_send_buffer;
dma_config.send_vfifo_buffer_size = VFIFO_SIZE;
dma_config.send_vfifo_threshold_size = SEND_THRESHOLD_SIZE;
hal_uart_set_dma(HAL_UART_2, &dma_config);
hal_uart_register_callback(HAL_UART_2, uart_read_from_input, NULL);
/* Print the prompt content to the test port */
//hal_uart_send_dma(HAL_UART_2, (const uint8_t *)UART_PROMPT_INFO, UART_PROMPT_INFO_SIZE);
LOG_I(lwip_socket_example, "create uart task success.n");
__enable_irq();
__enable_fault_irq();
/*Step3: Loop the data received from the UART input to its output */
while (1) {
if (g_uart_receive_event == true)
{
length = hal_uart_get_available_receive_bytes(HAL_UART_2);
if(length!=0)
{
hal_uart_receive_dma(HAL_UART_2, g_uart_receive_buffer, length);
memset(g_uart_send_buffer, 0x00, sizeof(g_uart_send_buffer));
memcpy(g_uart_send_buffer,g_uart_receive_buffer,length);
g_uart_send_buffer[0]=n++;
g_tMsg.size=length;
hal_uart_send_dma(HAL_UART_2, g_uart_send_buffer, length);
memcpy(g_tMsg.data,g_uart_send_buffer,g_tMsg.size);
}
if(g_lwip_write_done)
{
if(xQueueSend(wifi_send_queue, (void*)&g_tMsg, 0)!= pdPASS)
{
//printf("xQueueSendFromISR failn");
}
else
{
g_lwip_write_done=false;
}
}
g_uart_receive_event = false;
}
vTaskDelay(100 / portTICK_RATE_MS); // release CPU
}
}
完成以上的基础工作后,开始在freertos里面建立线程,代码如下
static void user_entry(void *args)
{
lwip_net_ready();
LOG_I(lwip_socket_example, "Begin to create socket_sample_task");
//xTaskHandle xHandle;
if (pdPASS != xTaskCreate(client_test_thread,
SOCKET_CLINET_EXAMPLE_TASK_NAME,
SOCKET_EXAMPLE_TASK_STACKSIZE / sizeof(portSTACK_TYPE),
NULL,
SOCKET_EXAMPLE_TASK_PRIO,
NULL)) {
LOG_I(lwip_socket_example, "Cannot create socket_sample_task");
}
LOG_I(lwip_socket_example, "Finish to create socket_sample_task");
while (1) {
vTaskDelay(1000 / portTICK_RATE_MS); // release CPU
}
}
在main函数里加入串口和tcp客户端的线程,代码如下
if (pdPASS != xTaskCreate(user_entry,
USER_ENTRY_TASK_NAME,
USER_ENTRY_TASK_STACKSIZE / sizeof(portSTACK_TYPE),
NULL,
USER_ENTRY_TASK_PRIO,
NULL)) {
LOG_E(lwip_socket_example, "create user task fail");
return -1;
}
wifi_send_queue = xQueueCreate(WIFI_SEND_QUEUE_LENGTH,sizeof(struct MSG));
if (pdPASS != xTaskCreate(Uart_Thread,
"Uart_Task",
SOCKET_EXAMPLE_TASK_STACKSIZE / sizeof(portSTACK_TYPE),
NULL,
SOCKET_EXAMPLE_TASK_PRIO,
NULL)) {
LOG_I(lwip_socket_example, "Cannot create Uart_Thread");
}
保存,编译,完成后烧录到mtk7686,在pc及打开网络调试助手,和串口助手就可以进行透传了。由于板卡在单位,不能截图了。
水平有限,希望大神们多指点。感谢mtk的fae给予指导。
提示,mtk7686不支持在线调试,只能从串口1下载flash配置文件,编译生成文件见下图
导入软件FlashTool.exe,选择串口下载即可