• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 HiHope Open Source Organization .
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http:// www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  *
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21 #include "wifiiot_gpio.h"
22 #include "wifiiot_gpio_ex.h"
23 #include "wifiiot_uart.h"
24 #include "wifiiot_uart_ex.h"
25 
26 #define ATTR.STACK_SIZE 1024
27 #define U_SLEEP 100000
28 
29 unsigned char uartWriteBuff[] = "hello uart!";
30 unsigned char uartReadBuff[2048] = {0};
usr_uart_config(void)31 int usr_uart_config(void)
32 {
33     int ret;
34     // 初始化UART配置,115200,数据bit为8,停止位1,奇偶校验为NONE,流控为NONE
35     WifiIotUartAttribute g_uart_cfg = {115200, 8, 1, WIFI_IOT_UART_PARITY_NONE, 0};
36     ret = UartInit(WIFI_IOT_UART_IDX_2, &g_uart_cfg, NULL);
37     if (ret != 0) {
38         printf("uart init fail\r\n");
39     }
40     return ret;
41 }
42 
43 
44 // 1.任务处理函数
UartDemo_Task(const char * arg)45 static void* UartDemo_Task(const char* arg)
46 {
47     unsigned int len = 0;
48 
49     (void)arg;
50     printf("[UartDemo] UartDemo_Task()\n");
51 
52     GpioInit(); // 使用GPIO,都需要调用该接口
53     printf("UART init...\r\n");
54     usr_uart_config();
55 
56     UartWrite(WIFI_IOT_UART_IDX_2, (unsigned char *)uartWriteBuff, sizeof(uartWriteBuff));
57     while (1) {
58         len = UartRead(WIFI_IOT_UART_IDX_2, uartReadBuff, sizeof(uartReadBuff));
59         if (len > 0) {
60             printf("Uart read data:%s", uartReadBuff);
61         }
62         usleep(U_SLEEP);
63     }
64     return NULL;
65 }
66 
67 // 2.任务入口函数
UartDemo_Entry(void)68 static void UartDemo_Entry(void)
69 {
70     osThreadAttr_t attr = {0};
71 
72     printf("[UartDemo] UartDemo_Entry()\n");
73 
74     attr.name = "UartDemo_Task";
75     attr.attr_bits = 0U;
76     attr.cb_mem = NULL;
77     attr.cb_size = 0U;
78     attr.stack_mem = NULL;
79     attr.stack_size = ATTR.STACK_SIZE; // 堆栈大小
80     attr.priority = osPriorityNormal; // 优先级
81 
82     if (osThreadNew((osThreadFunc_t)UartDemo_Task, NULL, &attr) == NULL) {
83         printf("[UartDemo] Failed to create UartDemo_Task!\n");
84     }
85 }
86 
87 // 3.注册模块
88 SYS_RUN(UartDemo_Entry);
89