1 /*
2 * Copyright (c) 2022 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 <hi_stdlib.h>
17 #include <hi_uart.h>
18 #include <app_demo_uart.h>
19 #include <iot_uart.h>
20 #include <hi_gpio.h>
21 #include <hi_io.h>
22 #include "iot_gpio_ex.h"
23 #include "iot_gpio.h"
24 #include "ohos_init.h"
25 #include "cmsis_os2.h"
26
27 #define LED_TEST_GPIO 9
28 #define LED_INTERVAL_TIME_US 300000
29
30 UartDefConfig uartDefConfig = {0};
31
Uart1GpioCOnfig(void)32 static void Uart1GpioCOnfig(void)
33 {
34 #ifdef ROBOT_BOARD
35 IoSetFunc(HI_IO_NAME_GPIO_5, IOT_IO_FUNC_GPIO_5_UART1_RXD);
36 IoSetFunc(HI_IO_NAME_GPIO_6, IOT_IO_FUNC_GPIO_6_UART1_TXD);
37 /* IOT_BOARD */
38 #elif defined (EXPANSION_BOARD)
39 IoSetFunc(HI_IO_NAME_GPIO_0, IOT_IO_FUNC_GPIO_0_UART1_TXD);
40 IoSetFunc(HI_IO_NAME_GPIO_1, IOT_IO_FUNC_GPIO_1_UART1_RXD);
41 #endif
42 }
43
44
UartDemoTask(char * param)45 static hi_void *UartDemoTask(char *param)
46 {
47 hi_u8 uartBuff[UART_BUFF_SIZE] = {0};
48 hi_unref_param(param);
49 printf("Initialize uart demo successfully, please enter some datas via DEMO_UART_NUM port...\n");
50 Uart1GpioCOnfig();
51 for (;;) {
52 uartDefConfig.g_uartLen = IoTUartRead(DEMO_UART_NUM, uartBuff, UART_BUFF_SIZE);
53 if ((uartDefConfig.g_uartLen > 0) && (uartBuff[0] == 0xaa) && (uartBuff[1] == 0x55)) {
54 for (int i = 0; i < UART_BUFF_SIZE; i++) {
55 printf("0x%x", uartBuff[i]);
56 }
57 printf("\r\n");
58 }
59 IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
60 usleep(LED_INTERVAL_TIME_US);
61 IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
62 usleep(LED_INTERVAL_TIME_US);
63 TaskMsleep(20); /* 20:sleep 20ms */
64 }
65 return HI_NULL;
66 }
67
68 /*
69 * This demo simply shows how to read datas from UART2 port and then echo back.
70 */
UartTransmit(hi_void)71 hi_void UartTransmit(hi_void)
72 {
73 hi_u32 ret = 0;
74 IoTGpioInit(LED_TEST_GPIO);
75 IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
76 IotUartAttribute uartAttr = {
77 .baudRate = 115200, /* baudRate: 115200 */
78 .dataBits = 8, /* dataBits: 8bits */
79 .stopBits = 1, /* stop bit */
80 .parity = 0,
81 };
82 /* Initialize uart driver */
83 ret = IoTUartInit(DEMO_UART_NUM, &uartAttr);
84 if (ret != HI_ERR_SUCCESS) {
85 printf("Failed to init uart! Err code = %d\n", ret);
86 return;
87 }
88 /* Create a task to handle uart communication */
89 osThreadAttr_t attr = {0};
90 attr.name = "uart demo";
91 attr.attr_bits = 0U;
92 attr.cb_mem = NULL;
93 attr.cb_size = 0U;
94 attr.stack_mem = NULL;
95 attr.stack_size = 1024; /* ��ջ��СΪ1024 */
96 attr.priority = osPriorityNormal;
97 if (osThreadNew((osThreadFunc_t)UartDemoTask, NULL, &attr) == NULL) {
98 printf("Failed to create uart demo task!\n");
99 }
100 }
101 SYS_RUN(UartTransmit);