10a7ce71fSopenharmony_ci/*
20a7ce71fSopenharmony_ci * Copyright (C) 2022 HiHope Open Source Organization .
30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License.
50a7ce71fSopenharmony_ci * You may obtain a copy of the License at
60a7ce71fSopenharmony_ci *
70a7ce71fSopenharmony_ci *     http:// www.apache.org/licenses/LICENSE-2.0
80a7ce71fSopenharmony_ci *
90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and
130a7ce71fSopenharmony_ci *
140a7ce71fSopenharmony_ci * limitations under the License.
150a7ce71fSopenharmony_ci */
160a7ce71fSopenharmony_ci
170a7ce71fSopenharmony_ci#include <stdio.h>
180a7ce71fSopenharmony_ci#include <unistd.h>
190a7ce71fSopenharmony_ci#include "ohos_init.h"
200a7ce71fSopenharmony_ci#include "cmsis_os2.h"
210a7ce71fSopenharmony_ci#include "wifiiot_gpio.h"
220a7ce71fSopenharmony_ci#include "wifiiot_gpio_ex.h"
230a7ce71fSopenharmony_ci#include "wifiiot_uart.h"
240a7ce71fSopenharmony_ci#include "wifiiot_uart_ex.h"
250a7ce71fSopenharmony_ci
260a7ce71fSopenharmony_ci#define ATTR.STACK_SIZE 1024
270a7ce71fSopenharmony_ci#define U_SLEEP 100000
280a7ce71fSopenharmony_ci
290a7ce71fSopenharmony_ciunsigned char uartWriteBuff[] = "hello uart!";
300a7ce71fSopenharmony_ciunsigned char uartReadBuff[2048] = {0};
310a7ce71fSopenharmony_ciint usr_uart_config(void)
320a7ce71fSopenharmony_ci{
330a7ce71fSopenharmony_ci    int ret;
340a7ce71fSopenharmony_ci    // 初始化UART配置,115200,数据bit为8,停止位1,奇偶校验为NONE,流控为NONE
350a7ce71fSopenharmony_ci    WifiIotUartAttribute g_uart_cfg = {115200, 8, 1, WIFI_IOT_UART_PARITY_NONE, 0};
360a7ce71fSopenharmony_ci    ret = UartInit(WIFI_IOT_UART_IDX_2, &g_uart_cfg, NULL);
370a7ce71fSopenharmony_ci    if (ret != 0) {
380a7ce71fSopenharmony_ci        printf("uart init fail\r\n");
390a7ce71fSopenharmony_ci    }
400a7ce71fSopenharmony_ci    return ret;
410a7ce71fSopenharmony_ci}
420a7ce71fSopenharmony_ci
430a7ce71fSopenharmony_ci
440a7ce71fSopenharmony_ci// 1.任务处理函数
450a7ce71fSopenharmony_cistatic void* UartDemo_Task(const char* arg)
460a7ce71fSopenharmony_ci{
470a7ce71fSopenharmony_ci    unsigned int len = 0;
480a7ce71fSopenharmony_ci
490a7ce71fSopenharmony_ci    (void)arg;
500a7ce71fSopenharmony_ci    printf("[UartDemo] UartDemo_Task()\n");
510a7ce71fSopenharmony_ci
520a7ce71fSopenharmony_ci    GpioInit(); // 使用GPIO,都需要调用该接口
530a7ce71fSopenharmony_ci    printf("UART init...\r\n");
540a7ce71fSopenharmony_ci    usr_uart_config();
550a7ce71fSopenharmony_ci
560a7ce71fSopenharmony_ci    UartWrite(WIFI_IOT_UART_IDX_2, (unsigned char *)uartWriteBuff, sizeof(uartWriteBuff));
570a7ce71fSopenharmony_ci    while (1) {
580a7ce71fSopenharmony_ci        len = UartRead(WIFI_IOT_UART_IDX_2, uartReadBuff, sizeof(uartReadBuff));
590a7ce71fSopenharmony_ci        if (len > 0) {
600a7ce71fSopenharmony_ci            printf("Uart read data:%s", uartReadBuff);
610a7ce71fSopenharmony_ci        }
620a7ce71fSopenharmony_ci        usleep(U_SLEEP);
630a7ce71fSopenharmony_ci    }
640a7ce71fSopenharmony_ci    return NULL;
650a7ce71fSopenharmony_ci}
660a7ce71fSopenharmony_ci
670a7ce71fSopenharmony_ci// 2.任务入口函数
680a7ce71fSopenharmony_cistatic void UartDemo_Entry(void)
690a7ce71fSopenharmony_ci{
700a7ce71fSopenharmony_ci    osThreadAttr_t attr = {0};
710a7ce71fSopenharmony_ci
720a7ce71fSopenharmony_ci    printf("[UartDemo] UartDemo_Entry()\n");
730a7ce71fSopenharmony_ci
740a7ce71fSopenharmony_ci    attr.name = "UartDemo_Task";
750a7ce71fSopenharmony_ci    attr.attr_bits = 0U;
760a7ce71fSopenharmony_ci    attr.cb_mem = NULL;
770a7ce71fSopenharmony_ci    attr.cb_size = 0U;
780a7ce71fSopenharmony_ci    attr.stack_mem = NULL;
790a7ce71fSopenharmony_ci    attr.stack_size = ATTR.STACK_SIZE; // 堆栈大小
800a7ce71fSopenharmony_ci    attr.priority = osPriorityNormal; // 优先级
810a7ce71fSopenharmony_ci
820a7ce71fSopenharmony_ci    if (osThreadNew((osThreadFunc_t)UartDemo_Task, NULL, &attr) == NULL) {
830a7ce71fSopenharmony_ci        printf("[UartDemo] Failed to create UartDemo_Task!\n");
840a7ce71fSopenharmony_ci    }
850a7ce71fSopenharmony_ci}
860a7ce71fSopenharmony_ci
870a7ce71fSopenharmony_ci// 3.注册模块
880a7ce71fSopenharmony_ciSYS_RUN(UartDemo_Entry);
89