原创 【开源硬件小安派-Eyse-S1】+通过wifi 连接TCP服务器

2024-1-16 16:56 887 6 3 分类: MCU/ 嵌入式
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下载原厂代码仓库
代码仓库:
  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 初始化任务
  1. int main(void)
  2. {
  3. board_init();//初始化开发板
  4. xwtx_bl618_init();//初始化指示灯
  5. tcpip_init(NULL, NULL);//初始化TCP
  6. wifi_start_firmware_task();
  7. xTaskCreate(main_task_fun, (const) "main_task", 512, NULL, 1, &main_task);
  8. xTaskCreate(tcp_init_task, (const) "tcp_task", 1024, NULL, 1, &tcp_task);
  9. vTaskStartScheduler();
  10. while (1) {
  11. }
  12. }

2.在main_task_fun 里面执行wifi 连接的任务,wifi连接成功之后,指示灯间隔1秒执行一次亮灭
  1. void main_task_fun(void *param)
  2. {
  3. int ret = 0;
  4. vTaskDelay(5000);
  5. ret = wifi_connect("lvxingzhe2", "Lvxingzhe.123");
  6. LOG_I("ret %d", ret);
  7. while (1) {
  8. if (is_link==1)
  9. {
  10. bflb_gpio_set(gpio_struct, GPIO_PIN_0);
  11. vTaskDelay(1000);
  12. bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
  13. vTaskDelay(1000);
  14. LOG_I("hello xwtx\r\n");
  15. }
  16. else
  17. {
  18. bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
  19. vTaskDelay(1000);
  20. }
  21. }
  22. }

3.调用lwip 原始代码进行TCP数据收发,不知道为什么不能使用非阻塞的方式,使用非阻塞一直连不上服务器
  1. void tcp_init_task(void *param)
  2. {
  3. struct sockaddr_in remote_addr;
  4. int socket_id=-1;
  5. int ret=-1;
  6. fd_set write_set, read_set, error_set;
  7. struct timeval to;
  8. char *rxbuf;
  9. int read_len;
  10. while (1)
  11. {
  12. while (!is_link)
  13. {
  14. LOG_I("wait network");
  15. vTaskDelay(1000);
  16. }
  17. vTaskDelay(3000);
  18. if ((socket_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  19. printf("TCP Client create socket error\r\n");
  20. return;
  21. }
  22. //fcntl(socket_id, F_SETFL, O_NONBLOCK);//设置为非阻塞
  23. // memset(&remote_addr, 0, sizeof(remote_addr));
  24. // remote_addr.sin_family = AF_INET;
  25. // remote_addr.sin_addr.s_addr = IPADDR_ANY;
  26. // remote_addr.sin_port = 0;
  27. // bind(socket_id, (const struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
  28. remote_addr.sin_family=AF_INET;
  29. remote_addr.sin_port=htons(DEMO_SERVER_TCP_PORT);
  30. remote_addr.sin_addr.s_addr = inet_addr(DEMO_SERVER_TCP_IP);
  31. // printf("Server ip Address : %s:%s\r\n", addr, port);
  32. memset(&(remote_addr.sin_zero), 0, sizeof(remote_addr.sin_zero));
  33. if (connect(socket_id, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) != 0) {
  34. LOG_I("TCP client connect server falied!\r\n");
  35. closesocket(socket_id);
  36. }
  37. send(socket_id,"ni hao xwtx",strlen("ni hao xwtx"),0);
  38. rxbuf = malloc(1024);
  39. while (1)
  40. {
  41. read_len = recv(socket_id, rxbuf, 1024, 0);
  42. if (read_len > 0) {
  43. send(socket_id, rxbuf, read_len, 0);
  44. }
  45. else
  46. {
  47. send(socket_id, "heart", 5, 0);
  48. }
  49. vTaskDelay(1000);
  50. }
  51. }
  52. }

4.服务器消息展示

完整代码:
  1. /****************************************************************************
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership. The
  6. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance with the
  8. * License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. * License for the specific language governing permissions and limitations
  16. * under the License.
  17. *
  18. ****************************************************************************/
  19. /****************************************************************************
  20. * Included Files
  21. ****************************************************************************/
  22. #include "FreeRTOS.h"
  23. #include "task.h"
  24. #include "timers.h"
  25. #include "mem.h"
  26. #include <lwip/tcpip.h>
  27. #include <lwip/sockets.h>
  28. #include <lwip/netdb.h>
  29. #include "bl_fw_api.h"
  30. #include "wifi_mgmr_ext.h"
  31. #include "wifi_mgmr.h"
  32. #include "bflb_irq.h"
  33. #include "bflb_uart.h"
  34. #include "bl616_glb.h"
  35. #include "rfparam_adapter.h"
  36. #include "board.h"
  37. #include "shell.h"
  38. #define DBG_TAG "MAIN"
  39. #include "log.h"
  40. struct bflb_device_s *gpio;
  41. struct bflb_device_s *gpio_struct;
  42. static TaskHandle_t main_task;
  43. static TaskHandle_t tcp_task;
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. #define WIFI_STACK_SIZE (1536)
  48. #define TASK_PRIORITY_FW (16)
  49. #define DEMO_SERVER_TCP_IP "112.125.89.8"
  50. #define DEMO_SERVER_TCP_PORT 47451
  51. /****************************************************************************
  52. * Private Types
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Private Data
  56. ****************************************************************************/
  57. static struct bflb_device_s *uart0;
  58. static TaskHandle_t wifi_fw_task;
  59. static uint32_t sta_ConnectStatus = 0;
  60. static uint8_t is_link=0;
  61. static wifi_conf_t conf = {
  62. .country_code = "CN",
  63. };
  64. extern void shell_init_with_task(struct bflb_device_s *shell);
  65. /****************************************************************************
  66. * Private Function Prototypes
  67. ****************************************************************************/
  68. /****************************************************************************
  69. * Functions
  70. ****************************************************************************/
  71. int wifi_start_firmware_task(void)
  72. {
  73. LOG_I("Starting wifi ...\r\n");
  74. /* enable wifi clock */
  75. GLB_PER_Clock_UnGate(GLB_AHB_CLOCK_IP_WIFI_PHY | GLB_AHB_CLOCK_IP_WIFI_MAC_PHY | GLB_AHB_CLOCK_IP_WIFI_PLATFORM);
  76. GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_WIFI);
  77. /* set ble controller EM Size */
  78. GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB);
  79. if (0 != rfparam_init(0, NULL, 0)) {
  80. LOG_I("PHY RF init failed!\r\n");
  81. return 0;
  82. }
  83. LOG_I("PHY RF init success!\r\n");
  84. /* Enable wifi irq */
  85. extern void interrupt0_handler(void);
  86. bflb_irq_attach(WIFI_IRQn, (irq_callback)interrupt0_handler, NULL);
  87. bflb_irq_enable(WIFI_IRQn);
  88. xTaskCreate(wifi_main, (char *)"fw", WIFI_STACK_SIZE, NULL, TASK_PRIORITY_FW, &wifi_fw_task);
  89. return 0;
  90. }
  91. void wifi_event_handler(uint32_t code)
  92. {
  93. sta_ConnectStatus = code;
  94. BaseType_t xHigherPriorityTaskWoken;
  95. switch (code) {
  96. case CODE_WIFI_ON_INIT_DONE: {
  97. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_INIT_DONE", __func__);
  98. wifi_mgmr_init(&conf);
  99. } break;
  100. case CODE_WIFI_ON_MGMR_DONE: {
  101. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_MGMR_DONE", __func__);
  102. } break;
  103. case CODE_WIFI_ON_SCAN_DONE: {
  104. wifi_mgmr_sta_scanlist();
  105. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_SCAN_DONE SSID numbles:%d", __func__, wifi_mgmr_sta_scanlist_nums_get());
  106. } break;
  107. case CODE_WIFI_ON_CONNECTED: {
  108. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_CONNECTED", __func__);
  109. void mm_sec_keydump();
  110. mm_sec_keydump();
  111. } break;
  112. case CODE_WIFI_ON_GOT_IP: {
  113. is_link=1;
  114. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_GOT_IP", __func__);
  115. } break;
  116. case CODE_WIFI_ON_DISCONNECT: {
  117. is_link=0;
  118. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_DISCONNECT", __func__);
  119. } break;
  120. case CODE_WIFI_ON_AP_STARTED: {
  121. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STARTED", __func__);
  122. } break;
  123. case CODE_WIFI_ON_AP_STOPPED: {
  124. LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STOPPED", __func__);
  125. } break;
  126. case CODE_WIFI_ON_AP_STA_ADD: {
  127. LOG_I("[APP] [EVT] [AP] [ADD] %lld", xTaskGetTickCount());
  128. } break;
  129. case CODE_WIFI_ON_AP_STA_DEL: {
  130. LOG_I("[APP] [EVT] [AP] [DEL] %lld", xTaskGetTickCount());
  131. } break;
  132. default: {
  133. LOG_I("[APP] [EVT] Unknown code %u ", code);
  134. }
  135. }
  136. }
  137. uint8_t wifi_connect(char *ssid, char *passwd)
  138. {
  139. int ret = 255;
  140. // struct fhost_vif_ip_addr_cfg ip_cfg = { 0 };
  141. uint32_t ipv4_addr = 0;
  142. if (NULL == ssid || 0 == strlen(ssid)) {
  143. return 1;
  144. }
  145. if (wifi_mgmr_sta_state_get() == 1) {
  146. wifi_sta_disconnect();
  147. }
  148. if (wifi_sta_connect(ssid, passwd, NULL, NULL, 0, 0, 0, 1)) {
  149. return 4;
  150. }
  151. LOG_I("Wating wifi connet");
  152. //等待连接成功
  153. sta_ConnectStatus = 0;
  154. for (int i = 0; i < 10 * 30; i++) {
  155. vTaskDelay(100 / portTICK_PERIOD_MS);
  156. switch (sta_ConnectStatus) {
  157. case CODE_WIFI_ON_MGMR_DONE:
  158. return 3;
  159. case CODE_WIFI_ON_SCAN_DONE:
  160. return 2;
  161. case CODE_WIFI_ON_DISCONNECT: //连接失败(超过了重连次数还没有连接成功的状态)
  162. return 4;
  163. case CODE_WIFI_ON_CONNECTED: //连接成功(表示wifi sta状态的时候表示同时获取IP(DHCP)成功,或者使用静态IP)
  164. LOG_I("Wating wifi connet OK");
  165. break;
  166. case CODE_WIFI_ON_GOT_IP:
  167. return 0;
  168. default:
  169. //等待连接成功
  170. break;
  171. }
  172. }
  173. return 14; //连接超时
  174. }
  175. //初始化任务,负责初始化,硬件任务
  176. void xwtx_bl618_init(void)
  177. {
  178. gpio_struct = bflb_device_get_by_name("gpio");
  179. bflb_gpio_init(gpio_struct, GPIO_PIN_0, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
  180. bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
  181. }
  182. void tcp_init_task(void *param)
  183. {
  184. struct sockaddr_in remote_addr;
  185. int socket_id=-1;
  186. int ret=-1;
  187. fd_set write_set, read_set, error_set;
  188. struct timeval to;
  189. char *rxbuf;
  190. int read_len;
  191. while (1)
  192. {
  193. while (!is_link)
  194. {
  195. LOG_I("wait network");
  196. vTaskDelay(1000);
  197. }
  198. vTaskDelay(3000);
  199. if ((socket_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  200. printf("TCP Client create socket error\r\n");
  201. return;
  202. }
  203. //fcntl(socket_id, F_SETFL, O_NONBLOCK);//设置为非阻塞
  204. // memset(&remote_addr, 0, sizeof(remote_addr));
  205. // remote_addr.sin_family = AF_INET;
  206. // remote_addr.sin_addr.s_addr = IPADDR_ANY;
  207. // remote_addr.sin_port = 0;
  208. // bind(socket_id, (const struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
  209. remote_addr.sin_family=AF_INET;
  210. remote_addr.sin_port=htons(DEMO_SERVER_TCP_PORT);
  211. remote_addr.sin_addr.s_addr = inet_addr(DEMO_SERVER_TCP_IP);
  212. // printf("Server ip Address : %s:%s\r\n", addr, port);
  213. memset(&(remote_addr.sin_zero), 0, sizeof(remote_addr.sin_zero));
  214. if (connect(socket_id, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) != 0) {
  215. LOG_I("TCP client connect server falied!\r\n");
  216. closesocket(socket_id);
  217. }
  218. send(socket_id,"ni hao xwtx",strlen("ni hao xwtx"),0);
  219. rxbuf = malloc(1024);
  220. while (1)
  221. {
  222. read_len = recv(socket_id, rxbuf, 1024, 0);
  223. if (read_len > 0) {
  224. send(socket_id, rxbuf, read_len, 0);
  225. }
  226. else
  227. {
  228. send(socket_id, "heart", 5, 0);
  229. }
  230. vTaskDelay(1000);
  231. }
  232. }
  233. }
  234. void main_task_fun(void *param)
  235. {
  236. int ret = 0;
  237. vTaskDelay(5000);
  238. ret = wifi_connect("lvxingzhe2", "Lvxingzhe.123");
  239. LOG_I("ret %d", ret);
  240. while (1) {
  241. if (is_link==1)
  242. {
  243. bflb_gpio_set(gpio_struct, GPIO_PIN_0);
  244. vTaskDelay(1000);
  245. bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
  246. vTaskDelay(1000);
  247. LOG_I("hello xwtx\r\n");
  248. }
  249. else
  250. {
  251. bflb_gpio_reset(gpio_struct, GPIO_PIN_0);
  252. vTaskDelay(1000);
  253. }
  254. }
  255. }
  256. int main(void)
  257. {
  258. board_init();
  259. xwtx_bl618_init();
  260. tcpip_init(NULL, NULL);
  261. wifi_start_firmware_task();
  262. xTaskCreate(main_task_fun, (const) "main_task", 512, NULL, 1, &main_task);
  263. xTaskCreate(tcp_init_task, (const) "tcp_task", 1024, NULL, 1, &tcp_task);
  264. vTaskStartScheduler();
  265. while (1) {
  266. }
  267. }

作者: 郑工王同学, 来源:面包板社区

链接: https://mbb.eet-china.com/blog/uid-me-3970688.html

版权声明:本文为博主原创,未经本人允许,禁止转载!

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /2 下一条