• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Description: Provides hal uart \n
16  *
17  * History: \n
18  * 2023-02-17, Create file. \n
19  */
20 
21 #include <stdint.h>
22 #include <stdio.h>
23 #include "common_def.h"
24 #include "uart_porting.h"
25 #include "hal_uart.h"
26 
27 uintptr_t g_hal_uarts_regs[UART_BUS_MAX_NUM] = { 0 };
28 hal_uart_funcs_t *g_hal_uarts_funcs[UART_BUS_MAX_NUM] = { 0 };
29 
hal_uart_base_addrs_get(uart_bus_t bus)30 uintptr_t hal_uart_base_addrs_get(uart_bus_t bus)
31 {
32     return (uintptr_t)uart_porting_base_addr_get(bus);
33 }
34 
hal_uart_regs_init(uart_bus_t bus)35 errcode_t hal_uart_regs_init(uart_bus_t bus)
36 {
37     if (hal_uart_base_addrs_get(bus) == 0) {
38         return ERRCODE_UART_REG_ADDR_INVALID;
39     }
40     g_hal_uarts_regs[bus] = hal_uart_base_addrs_get(bus);
41     return ERRCODE_SUCC;
42 }
43 
hal_uart_register_funcs(uart_bus_t bus,hal_uart_funcs_t * funcs)44 errcode_t hal_uart_register_funcs(uart_bus_t bus, hal_uart_funcs_t *funcs)
45 {
46     if (bus >= UART_BUS_MAX_NUM || funcs == NULL) {
47         return ERRCODE_INVALID_PARAM;
48     }
49     g_hal_uarts_funcs[bus] = funcs;
50     return ERRCODE_SUCC;
51 }
52 
hal_uart_unregister_funcs(uart_bus_t bus)53 errcode_t hal_uart_unregister_funcs(uart_bus_t bus)
54 {
55     if (bus >= UART_BUS_MAX_NUM) {
56         return ERRCODE_INVALID_PARAM;
57     }
58     g_hal_uarts_funcs[bus] = NULL;
59     return ERRCODE_SUCC;
60 }
61 
hal_uart_get_funcs(uart_bus_t bus)62 hal_uart_funcs_t *hal_uart_get_funcs(uart_bus_t bus)
63 {
64     if (bus >= UART_BUS_MAX_NUM) {
65         return NULL;
66     }
67     return g_hal_uarts_funcs[bus];
68 }
69 
hal_uart_set_rx_fifo_int_level(uart_bus_t bus,uart_fifo_rx_int_lvl_t level)70 errcode_t hal_uart_set_rx_fifo_int_level(uart_bus_t bus, uart_fifo_rx_int_lvl_t level)
71 {
72     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
73     if (hal_funcs == NULL) {
74         return ERRCODE_UART_NOT_IMPLEMENT;
75     }
76     return hal_funcs->ctrl(bus, UART_CTRL_SET_RX_FIFO_LEVEL, (uintptr_t)(uint32_t)level);
77 }
78 
hal_uart_set_tx_fifo_int_level(uart_bus_t bus,uart_fifo_tx_int_lvl_t level)79 errcode_t hal_uart_set_tx_fifo_int_level(uart_bus_t bus, uart_fifo_tx_int_lvl_t level)
80 {
81     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
82     if (hal_funcs == NULL) {
83         return ERRCODE_UART_NOT_IMPLEMENT;
84     }
85     return hal_funcs->ctrl(bus, UART_CTRL_SET_TX_FIFO_LEVEL, (uintptr_t)(uint32_t)level);
86 }
87 
hal_uart_init(uart_bus_t bus,hal_uart_callback_t callback,const hal_uart_pin_config_t * pins,const hal_uart_attr_t * attr,hal_uart_flow_ctrl_t flow_ctrl)88 errcode_t hal_uart_init(uart_bus_t bus, hal_uart_callback_t callback, const hal_uart_pin_config_t *pins,
89                         const hal_uart_attr_t *attr, hal_uart_flow_ctrl_t flow_ctrl)
90 {
91     uart_port_register_hal_funcs(bus);
92     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
93     if (hal_funcs == NULL || attr == NULL) {
94         return ERRCODE_UART_NOT_IMPLEMENT;
95     }
96     return hal_funcs->init(bus, callback, (hal_uart_pin_config_t *)pins, (hal_uart_attr_t *)attr, flow_ctrl);
97 }
98 
hal_uart_deinit(uart_bus_t bus)99 errcode_t hal_uart_deinit(uart_bus_t bus)
100 {
101     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
102     return hal_funcs->deinit(bus);
103 }
104 
hal_uart_write(uart_bus_t bus,const uint8_t * data,uint16_t len)105 errcode_t hal_uart_write(uart_bus_t bus, const uint8_t *data, uint16_t len)
106 {
107     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
108     return hal_funcs->write(bus, data, len);
109 }
110 
hal_uart_read(uart_bus_t bus,const uint8_t * data,uint16_t len)111 int32_t hal_uart_read(uart_bus_t bus, const uint8_t *data, uint16_t len)
112 {
113     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
114     return hal_funcs->read(bus, data, len);
115 }
116 
hal_uart_ctrl(uart_bus_t bus,hal_uart_ctrl_id_t id,uintptr_t param)117 errcode_t hal_uart_ctrl(uart_bus_t bus, hal_uart_ctrl_id_t id, uintptr_t param)
118 {
119     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
120     if (hal_funcs == NULL) {
121         return ERRCODE_UART_NOT_IMPLEMENT;
122     }
123     return hal_funcs->ctrl(bus, id, param);
124 }
125 
126 #if defined(CONFIG_UART_SUPPORT_DMA)
hal_uart_set_dma_config(uart_bus_t bus,const hal_uart_extra_attr_t * extra_attr)127 void hal_uart_set_dma_config(uart_bus_t bus, const hal_uart_extra_attr_t *extra_attr)
128 {
129     hal_uart_funcs_t *hal_funcs = g_hal_uarts_funcs[bus];
130     hal_funcs->dma_cfg(bus, (hal_uart_extra_attr_t *)extra_attr);
131     return;
132 }
133 #endif