1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 <app_demo_uart.h>
17
18 hi_u32 g_uart_demo_task_id = 0;
19
uart_demo_task(hi_void * param)20 static hi_void *uart_demo_task(hi_void *param)
21 {
22 hi_u8 uart_buff[UART_BUFF_SIZE] = {0};
23 hi_u8 *uart_buff_ptr = uart_buff;
24 hi_unref_param(param);
25 printf("Initialize uart demo successfully, please enter some datas via DEMO_UART_NUM port...\n");
26
27 for (;;) {
28 hi_s32 len = hi_uart_read(DEMO_UART_NUM, uart_buff_ptr, UART_BUFF_SIZE);
29 if (len > 0) {
30 #ifdef WRITE_BY_INT
31 hi_uart_write(DEMO_UART_NUM, uart_buff_ptr, len);
32 #else
33 hi_uart_write_immediately(DEMO_UART_NUM, uart_buff_ptr, len);
34 #endif
35 } else {
36 printf("Read nothing!\n");
37 hi_sleep(1000); /* sleep 1000ms */
38 }
39 }
40
41 hi_task_delete(g_uart_demo_task_id);
42 g_uart_demo_task_id = 0;
43
44 return HI_NULL;
45 }
46
47 /*
48 * This demo simply shows how to read datas from UART2 port and then echo back.
49 */
uart_demo(hi_void)50 hi_void uart_demo(hi_void)
51 {
52 hi_u32 ret;
53 hi_uart_attribute uart_attr = {
54 .baud_rate = 115200, /* baud_rate: 115200 */
55 .data_bits = 8, /* data_bits: 8bits */
56 .stop_bits = 1,
57 .parity = 0,
58 };
59
60 /* Initialize uart driver */
61 ret = hi_uart_init(DEMO_UART_NUM, &uart_attr, HI_NULL);
62 if (ret != HI_ERR_SUCCESS) {
63 printf("Failed to init uart! Err code = %d\n", ret);
64 return;
65 }
66
67 /* Create a task to handle uart communication */
68 hi_task_attr attr = {0};
69 attr.stack_size = UART_DEMO_TASK_STAK_SIZE;
70 attr.task_prio = UART_DEMO_TASK_PRIORITY;
71 attr.task_name = (hi_char*)"uart_demo";
72 ret = hi_task_create(&g_uart_demo_task_id, &attr, uart_demo_task, HI_NULL);
73 if (ret != HI_ERR_SUCCESS) {
74 printf("Falied to create uart demo task!\n");
75 }
76 }
77