1/*
2 * Copyright (c) 2020-2021 Huawei Device 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 "uart.h"
17#include "arm_uart_drv.h"
18#include "stdio.h"
19#include "los_config.h"
20#include "los_reg.h"
21#include "los_interrupt.h"
22#include "los_event.h"
23
24#ifdef __cplusplus
25#if __cplusplus
26extern "C" {
27#endif
28#endif
29
30static const struct arm_uart_dev_cfg_t g_uartCfg = {UART0_BASE, 115200};
31static struct arm_uart_dev_data_t g_uartData = {0};
32struct arm_uart_dev_t g_uartDev;
33
34INT32 UartGetc(VOID)
35{
36    UINT8 c;
37    if (arm_uart_read(&g_uartDev, &c) == ARM_UART_ERR_NOT_READY) {
38        return 0;
39    }
40
41    return c;
42}
43
44INT32 UartPutc(INT32 c, VOID *file)
45{
46    return arm_uart_write(&g_uartDev, (UINT8)c);
47}
48
49VOID UartReceiveHandler(VOID)
50{
51    if (arm_uart_get_interrupt_status(&g_uartDev) == ARM_UART_IRQ_RX) {
52        (void)LOS_EventWrite(&g_shellInputEvent, 0x1);
53        (void)arm_uart_clear_interrupt(&g_uartDev, ARM_UART_IRQ_RX);
54    }
55    return;
56}
57
58VOID UartInit(VOID)
59{
60    g_uartDev.cfg = &g_uartCfg;
61    g_uartDev.data = &g_uartData;
62    (void)arm_uart_init(&g_uartDev, UART0_CLK_FREQ);
63    return;
64}
65
66VOID Uart0RxIrqRegister(VOID)
67{
68    (void)arm_uart_irq_rx_enable(&g_uartDev);
69    (void)LOS_HwiCreate(Uart0_Rx_IRQn, 0, 0, (HWI_PROC_FUNC)UartReceiveHandler, 0);
70    return;
71}
72#ifdef __cplusplus
73#if __cplusplus
74}
75#endif /* __cplusplus */
76#endif /* __cplusplus */
77