• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
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  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 
20 #include "cmsis_os2.h"
21 #include "iot_errno.h"
22 #include "iot_uart.h"
23 #include "ohos_init.h"
24 
25 #define UART_TASK_STACK_SIZE (1024 * 8)
26 #define UART_TASK_PRIO 25
27 #define UART_BUFF_SIZE 1000
28 #define WIFI_IOT_UART_IDX_1 1
29 #define TASK_DELAY_1S 1000000
30 
31 static const char *data = "Hello, BearPi!\r\n";
32 
33 /**
34  * @brief uart task send data through uart1 and receive data through uart1
35  *
36  */
UartTask(void)37 static void UartTask(void)
38 {
39     uint8_t uart_buff[UART_BUFF_SIZE] = { 0 };
40     uint8_t *uart_buff_ptr = uart_buff;
41     uint8_t ret;
42 
43     IotUartAttribute uart_attr = {
44 
45         // baud_rate: 9600
46         .baudRate = 9600,
47 
48         // data_bits: 8bits
49         .dataBits = 8,
50         .stopBits = 1,
51         .parity = 0,
52     };
53 
54     // Initialize uart driver
55     ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
56     if (ret != IOT_SUCCESS) {
57         printf("Failed to init uart! Err code = %d\n", ret);
58         return;
59     }
60 
61     while (1) {
62         printf("=======================================\r\n");
63         printf("*************UART_example**************\r\n");
64         printf("=======================================\r\n");
65 
66         // send data through uart1
67         IoTUartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
68 
69         // receive data through uart1
70         IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
71 
72         printf("Uart1 read data:%s\n", uart_buff_ptr);
73         usleep(TASK_DELAY_1S);
74     }
75 }
76 
77 /**
78  * @brief Main Entry of the UART Example
79  *
80  */
UartExampleEntry(void)81 static void UartExampleEntry(void)
82 {
83     osThreadAttr_t attr;
84 
85     attr.name = "UartTask";
86     attr.attr_bits = 0U;
87     attr.cb_mem = NULL;
88     attr.cb_size = 0U;
89     attr.stack_mem = NULL;
90     attr.stack_size = UART_TASK_STACK_SIZE;
91     attr.priority = UART_TASK_PRIO;
92 
93     if (osThreadNew((osThreadFunc_t)UartTask, NULL, &attr) == NULL) {
94         printf("Failed to create UartTask!\n");
95     }
96 }
97 
98 APP_FEATURE_INIT(UartExampleEntry);