原创
【开源硬件小安派-Eyse-S1】+通过wifi 连接TCP服务器
1.Eyse-S1 简介
Ai-Pi-Eyes-S1/S2
是安信可开源团队专门为Ai-M61-32S设计的开发板,支持WiFi6、BLE5.3。所搭载的Ai-M61-32S
模组具有丰富的外设接口,具体包括 DVP、MJPEG、Dispaly、AudioCodec、USB2.0、SDU、以太网
(EMAC)、SD/MMC(SDH)、SPI、UART、I2C、I2S、PWM、GPDAC、GPADC、ACOMP 和 GPIO
等。集成了SPI屏幕接口,DVP摄像头接口,预留TF卡座,并且引出USB接口,可接入USB摄像头。
2.开发环境搭建
Ai-M61-32S 模组使用的是BL618芯片作为主控,这里我们直接使用原厂的代码仓库。
2.1下载原厂代码仓库
代码仓库:- git clone https:// gitee.com/bouffalolab/bouffalo_sdk.git
2.2下载原厂GCC 工具链,并添加环境变量>GCC 工具添加camke,make环境变量添加之后打开cmd,在cmd 中运行 make -v 确保添加成功
输入 riscv64-unknown-elf-gcc
-v 验证交叉编译工具是否添加成功
2.3 在vs code 中进行编译,并下载1.进入bl_sdk 中examples 目录2.编译helloworld demo
出现:Built target combine 表示编译成功
注意:下载之前需要确保模块进入下载模式
2.4通过TCP发送数据到云平台
1.初始化芯片,设置wifi 参数,并创建wifi 指示灯任务,tcp 初始化任务
- int main(void)
- {
- board_init();//初始化开发板
- xwtx_bl618_init();//初始化指示灯
- tcpip_init(NULL, NULL);//初始化TCP
- wifi_start_firmware_task();
- xTaskCreate(main_task_fun, (const) "main_task", 512, NULL, 1, &main_task);
- xTaskCreate(tcp_init_task, (const) "tcp_task", 1024, NULL, 1, &tcp_task);
- vTaskStartScheduler();
- while (1) {
- }
- }
2.在main_task_fun 里面执行wifi 连接的任务,wifi连接成功之后,指示灯间隔1秒执行一次亮灭
- void main_task_fun(void *param)
- {
- int ret = 0;
- vTaskDelay(5000);
- ret = wifi_connect("lvxingzhe2", "Lvxingzhe.123");
- LOG_I("ret %d", ret);
- while (1) {
- if (is_link==1)
- {
- bflb_gpio_set(gpio_struct, GPIO_PIN_0);
- vTaskDelay(1000);
- bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
- vTaskDelay(1000);
- LOG_I("hello xwtx\r\n");
- }
- else
- {
- bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
- vTaskDelay(1000);
- }
-
-
- }
- }
3.调用lwip 原始代码进行TCP数据收发,不知道为什么不能使用非阻塞的方式,使用非阻塞一直连不上服务器
- void tcp_init_task(void *param)
- {
- struct sockaddr_in remote_addr;
- int socket_id=-1;
- int ret=-1;
- fd_set write_set, read_set, error_set;
- struct timeval to;
- char *rxbuf;
- int read_len;
- while (1)
- {
- while (!is_link)
- {
- LOG_I("wait network");
- vTaskDelay(1000);
- }
- vTaskDelay(3000);
- if ((socket_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- printf("TCP Client create socket error\r\n");
- return;
- }
- //fcntl(socket_id, F_SETFL, O_NONBLOCK);//设置为非阻塞
- // memset(&remote_addr, 0, sizeof(remote_addr));
- // remote_addr.sin_family = AF_INET;
- // remote_addr.sin_addr.s_addr = IPADDR_ANY;
- // remote_addr.sin_port = 0;
- // bind(socket_id, (const struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
- remote_addr.sin_family=AF_INET;
- remote_addr.sin_port=htons(DEMO_SERVER_TCP_PORT);
- remote_addr.sin_addr.s_addr = inet_addr(DEMO_SERVER_TCP_IP);
- // printf("Server ip Address : %s:%s\r\n", addr, port);
- memset(&(remote_addr.sin_zero), 0, sizeof(remote_addr.sin_zero));
- if (connect(socket_id, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) != 0) {
- LOG_I("TCP client connect server falied!\r\n");
- closesocket(socket_id);
- }
- send(socket_id,"ni hao xwtx",strlen("ni hao xwtx"),0);
- rxbuf = malloc(1024);
- while (1)
- {
- read_len = recv(socket_id, rxbuf, 1024, 0);
- if (read_len > 0) {
- send(socket_id, rxbuf, read_len, 0);
- }
- else
- {
- send(socket_id, "heart", 5, 0);
- }
- vTaskDelay(1000);
- }
-
- }
-
- }
4.服务器消息展示
完整代码:
- /****************************************************************************
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership. The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
- /****************************************************************************
- * Included Files
- ****************************************************************************/
- #include "FreeRTOS.h"
- #include "task.h"
- #include "timers.h"
- #include "mem.h"
- #include <lwip/tcpip.h>
- #include <lwip/sockets.h>
- #include <lwip/netdb.h>
- #include "bl_fw_api.h"
- #include "wifi_mgmr_ext.h"
- #include "wifi_mgmr.h"
- #include "bflb_irq.h"
- #include "bflb_uart.h"
- #include "bl616_glb.h"
- #include "rfparam_adapter.h"
- #include "board.h"
- #include "shell.h"
- #define DBG_TAG "MAIN"
- #include "log.h"
- struct bflb_device_s *gpio;
- struct bflb_device_s *gpio_struct;
- static TaskHandle_t main_task;
- static TaskHandle_t tcp_task;
- /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
- #define WIFI_STACK_SIZE (1536)
- #define TASK_PRIORITY_FW (16)
- #define DEMO_SERVER_TCP_IP "112.125.89.8"
- #define DEMO_SERVER_TCP_PORT 47451
- /****************************************************************************
- * Private Types
- ****************************************************************************/
- /****************************************************************************
- * Private Data
- ****************************************************************************/
- static struct bflb_device_s *uart0;
- static TaskHandle_t wifi_fw_task;
- static uint32_t sta_ConnectStatus = 0;
- static uint8_t is_link=0;
- static wifi_conf_t conf = {
- .country_code = "CN",
- };
- extern void shell_init_with_task(struct bflb_device_s *shell);
- /****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
- /****************************************************************************
- * Functions
- ****************************************************************************/
- int wifi_start_firmware_task(void)
- {
- LOG_I("Starting wifi ...\r\n");
- /* enable wifi clock */
- GLB_PER_Clock_UnGate(GLB_AHB_CLOCK_IP_WIFI_PHY | GLB_AHB_CLOCK_IP_WIFI_MAC_PHY | GLB_AHB_CLOCK_IP_WIFI_PLATFORM);
- GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_WIFI);
- /* set ble controller EM Size */
- GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB);
- if (0 != rfparam_init(0, NULL, 0)) {
- LOG_I("PHY RF init failed!\r\n");
- return 0;
- }
- LOG_I("PHY RF init success!\r\n");
- /* Enable wifi irq */
- extern void interrupt0_handler(void);
- bflb_irq_attach(WIFI_IRQn, (irq_callback)interrupt0_handler, NULL);
- bflb_irq_enable(WIFI_IRQn);
- xTaskCreate(wifi_main, (char *)"fw", WIFI_STACK_SIZE, NULL, TASK_PRIORITY_FW, &wifi_fw_task);
- return 0;
- }
- void wifi_event_handler(uint32_t code)
- {
- sta_ConnectStatus = code;
- BaseType_t xHigherPriorityTaskWoken;
- switch (code) {
- case CODE_WIFI_ON_INIT_DONE: {
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_INIT_DONE", __func__);
- wifi_mgmr_init(&conf);
- } break;
- case CODE_WIFI_ON_MGMR_DONE: {
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_MGMR_DONE", __func__);
- } break;
- case CODE_WIFI_ON_SCAN_DONE: {
- wifi_mgmr_sta_scanlist();
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_SCAN_DONE SSID numbles:%d", __func__, wifi_mgmr_sta_scanlist_nums_get());
- } break;
- case CODE_WIFI_ON_CONNECTED: {
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_CONNECTED", __func__);
- void mm_sec_keydump();
- mm_sec_keydump();
- } break;
- case CODE_WIFI_ON_GOT_IP: {
- is_link=1;
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_GOT_IP", __func__);
- } break;
- case CODE_WIFI_ON_DISCONNECT: {
- is_link=0;
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_DISCONNECT", __func__);
- } break;
- case CODE_WIFI_ON_AP_STARTED: {
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STARTED", __func__);
- } break;
- case CODE_WIFI_ON_AP_STOPPED: {
- LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STOPPED", __func__);
- } break;
- case CODE_WIFI_ON_AP_STA_ADD: {
- LOG_I("[APP] [EVT] [AP] [ADD] %lld", xTaskGetTickCount());
- } break;
- case CODE_WIFI_ON_AP_STA_DEL: {
- LOG_I("[APP] [EVT] [AP] [DEL] %lld", xTaskGetTickCount());
- } break;
- default: {
- LOG_I("[APP] [EVT] Unknown code %u ", code);
- }
- }
- }
- uint8_t wifi_connect(char *ssid, char *passwd)
- {
- int ret = 255;
- // struct fhost_vif_ip_addr_cfg ip_cfg = { 0 };
- uint32_t ipv4_addr = 0;
- if (NULL == ssid || 0 == strlen(ssid)) {
- return 1;
- }
- if (wifi_mgmr_sta_state_get() == 1) {
- wifi_sta_disconnect();
- }
- if (wifi_sta_connect(ssid, passwd, NULL, NULL, 0, 0, 0, 1)) {
- return 4;
- }
- LOG_I("Wating wifi connet");
- //等待连接成功
- sta_ConnectStatus = 0;
- for (int i = 0; i < 10 * 30; i++) {
- vTaskDelay(100 / portTICK_PERIOD_MS);
- switch (sta_ConnectStatus) {
- case CODE_WIFI_ON_MGMR_DONE:
- return 3;
- case CODE_WIFI_ON_SCAN_DONE:
- return 2;
- case CODE_WIFI_ON_DISCONNECT: //连接失败(超过了重连次数还没有连接成功的状态)
- return 4;
- case CODE_WIFI_ON_CONNECTED: //连接成功(表示wifi sta状态的时候表示同时获取IP(DHCP)成功,或者使用静态IP)
- LOG_I("Wating wifi connet OK");
- break;
- case CODE_WIFI_ON_GOT_IP:
- return 0;
- default:
- //等待连接成功
- break;
- }
- }
- return 14; //连接超时
- }
- //初始化任务,负责初始化,硬件任务
- void xwtx_bl618_init(void)
- {
- gpio_struct = bflb_device_get_by_name("gpio");
- bflb_gpio_init(gpio_struct, GPIO_PIN_0, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
- bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
- }
- void tcp_init_task(void *param)
- {
- struct sockaddr_in remote_addr;
- int socket_id=-1;
- int ret=-1;
- fd_set write_set, read_set, error_set;
- struct timeval to;
- char *rxbuf;
- int read_len;
- while (1)
- {
- while (!is_link)
- {
- LOG_I("wait network");
- vTaskDelay(1000);
- }
- vTaskDelay(3000);
- if ((socket_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- printf("TCP Client create socket error\r\n");
- return;
- }
- //fcntl(socket_id, F_SETFL, O_NONBLOCK);//设置为非阻塞
- // memset(&remote_addr, 0, sizeof(remote_addr));
- // remote_addr.sin_family = AF_INET;
- // remote_addr.sin_addr.s_addr = IPADDR_ANY;
- // remote_addr.sin_port = 0;
- // bind(socket_id, (const struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
- remote_addr.sin_family=AF_INET;
- remote_addr.sin_port=htons(DEMO_SERVER_TCP_PORT);
- remote_addr.sin_addr.s_addr = inet_addr(DEMO_SERVER_TCP_IP);
- // printf("Server ip Address : %s:%s\r\n", addr, port);
- memset(&(remote_addr.sin_zero), 0, sizeof(remote_addr.sin_zero));
- if (connect(socket_id, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) != 0) {
- LOG_I("TCP client connect server falied!\r\n");
- closesocket(socket_id);
- }
- send(socket_id,"ni hao xwtx",strlen("ni hao xwtx"),0);
- rxbuf = malloc(1024);
- while (1)
- {
- read_len = recv(socket_id, rxbuf, 1024, 0);
- if (read_len > 0) {
- send(socket_id, rxbuf, read_len, 0);
- }
- else
- {
- send(socket_id, "heart", 5, 0);
- }
- vTaskDelay(1000);
- }
-
- }
-
- }
- void main_task_fun(void *param)
- {
- int ret = 0;
- vTaskDelay(5000);
- ret = wifi_connect("lvxingzhe2", "Lvxingzhe.123");
- LOG_I("ret %d", ret);
- while (1) {
- if (is_link==1)
- {
- bflb_gpio_set(gpio_struct, GPIO_PIN_0);
- vTaskDelay(1000);
- bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
- vTaskDelay(1000);
- LOG_I("hello xwtx\r\n");
- }
- else
- {
- bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
- vTaskDelay(1000);
- }
-
-
- }
- }
- int main(void)
- {
- board_init();
- xwtx_bl618_init();
- tcpip_init(NULL, NULL);
- wifi_start_firmware_task();
- xTaskCreate(main_task_fun, (const) "main_task", 512, NULL, 1, &main_task);
- xTaskCreate(tcp_init_task, (const) "tcp_task", 1024, NULL, 1, &tcp_task);
- vTaskStartScheduler();
- while (1) {
- }
- }
作者: 郑工王同学, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-3970688.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论