/* * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include "iot_gpio_ex.h" #include "iot_gpio.h" #include "ohos_init.h" #include "cmsis_os2.h" #define LED_TEST_GPIO 9 #define LED_INTERVAL_TIME_US 300000 UartDefConfig uartDefConfig = {0}; static void Uart1GpioCOnfig(void) { #ifdef ROBOT_BOARD IoSetFunc(HI_IO_NAME_GPIO_5, IOT_IO_FUNC_GPIO_5_UART1_RXD); IoSetFunc(HI_IO_NAME_GPIO_6, IOT_IO_FUNC_GPIO_6_UART1_TXD); /* IOT_BOARD */ #elif defined (EXPANSION_BOARD) IoSetFunc(HI_IO_NAME_GPIO_0, IOT_IO_FUNC_GPIO_0_UART1_TXD); IoSetFunc(HI_IO_NAME_GPIO_1, IOT_IO_FUNC_GPIO_1_UART1_RXD); #endif } static hi_void *UartDemoTask(char *param) { hi_u8 uartBuff[UART_BUFF_SIZE] = {0}; hi_unref_param(param); printf("Initialize uart demo successfully, please enter some datas via DEMO_UART_NUM port...\n"); Uart1GpioCOnfig(); for (;;) { uartDefConfig.g_uartLen = IoTUartRead(DEMO_UART_NUM, uartBuff, UART_BUFF_SIZE); if ((uartDefConfig.g_uartLen > 0) && (uartBuff[0] == 0xaa) && (uartBuff[1] == 0x55)) { for (int i = 0; i < UART_BUFF_SIZE; i++) { printf("0x%x", uartBuff[i]); } printf("\r\n"); } IoTGpioSetOutputVal(LED_TEST_GPIO, 1); usleep(LED_INTERVAL_TIME_US); IoTGpioSetOutputVal(LED_TEST_GPIO, 0); usleep(LED_INTERVAL_TIME_US); TaskMsleep(20); /* 20:sleep 20ms */ } return HI_NULL; } /* * This demo simply shows how to read datas from UART2 port and then echo back. */ hi_void UartTransmit(hi_void) { hi_u32 ret = 0; IoTGpioInit(LED_TEST_GPIO); IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT); IotUartAttribute uartAttr = { .baudRate = 115200, /* baudRate: 115200 */ .dataBits = 8, /* dataBits: 8bits */ .stopBits = 1, /* stop bit */ .parity = 0, }; /* Initialize uart driver */ ret = IoTUartInit(DEMO_UART_NUM, &uartAttr); if (ret != HI_ERR_SUCCESS) { printf("Failed to init uart! Err code = %d\n", ret); return; } /* Create a task to handle uart communication */ osThreadAttr_t attr = {0}; attr.name = "uart demo"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024; /* ��ջ��СΪ1024 */ attr.priority = osPriorityNormal; if (osThreadNew((osThreadFunc_t)UartDemoTask, NULL, &attr) == NULL) { printf("Failed to create uart demo task!\n"); } } SYS_RUN(UartTransmit);