1 /* 2 * Copyright (C) 2015-2017 Alibaba Group Holding 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 #ifndef HAL_UART_H 17 #define HAL_UART_H 18 19 #include <stdint.h> 20 #include "los_event.h" 21 #include "los_compiler.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 #define HAL_WAIT_FOREVER 0xFFFFFFFFU 28 /* 29 * UART data width 30 */ 31 typedef enum { 32 DATA_WIDTH_5BIT, 33 DATA_WIDTH_6BIT, 34 DATA_WIDTH_7BIT, 35 DATA_WIDTH_8BIT, 36 DATA_WIDTH_9BIT 37 } hal_uart_data_width_t; 38 39 /* 40 * UART stop bits 41 */ 42 typedef enum { 43 STOP_BITS_1, 44 STOP_BITS_2 45 } hal_uart_stop_bits_t; 46 47 /* 48 * UART flow control 49 */ 50 typedef enum { 51 FLOW_CONTROL_DISABLED, 52 FLOW_CONTROL_CTS, 53 FLOW_CONTROL_RTS, 54 FLOW_CONTROL_CTS_RTS 55 } hal_uart_flow_control_t; 56 57 /* 58 * UART parity 59 */ 60 typedef enum { 61 NO_PARITY, 62 ODD_PARITY, 63 EVEN_PARITY 64 } hal_uart_parity_t; 65 66 /* 67 * UART mode 68 */ 69 typedef enum { 70 MODE_TX, 71 MODE_RX, 72 MODE_TX_RX 73 } hal_uart_mode_t; 74 75 /* 76 * UART configuration 77 */ 78 typedef struct { 79 uint32_t baud_rate; 80 hal_uart_data_width_t data_width; 81 hal_uart_parity_t parity; 82 hal_uart_stop_bits_t stop_bits; 83 hal_uart_flow_control_t flow_control; 84 hal_uart_mode_t mode; 85 } uart_config_t; 86 87 typedef struct { 88 uint8_t port; /* uart port */ 89 uart_config_t config; /* uart config */ 90 void *priv; /* priv data */ 91 } uart_dev_t; 92 93 /** 94 * Initialises a UART interface 95 * 96 * 97 * @param[in] uart the interface which should be initialised 98 * 99 * @return 0 : on success, EIO : if an error occurred with any step 100 */ 101 int32_t hal_uart_init(uart_dev_t *uart); 102 103 /** 104 * Transmit data on a UART interface 105 * 106 * @param[in] uart the UART interface 107 * @param[in] data pointer to the start of data 108 * @param[in] size number of bytes to transmit 109 * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER 110 * if you want to wait forever 111 * 112 * @return 0 : on success, EIO : if an error occurred with any step 113 */ 114 int32_t hal_uart_send(uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout); 115 116 /** 117 * Receive data on a UART interface 118 * 119 * @param[in] uart the UART interface 120 * @param[out] data pointer to the buffer which will store incoming data 121 * @param[in] expect_size number of bytes to receive 122 * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER 123 * if you want to wait forever 124 * 125 * @return 0 : on success, EIO : if an error occurred with any step 126 */ 127 int32_t hal_uart_recv(uart_dev_t *uart, void *data, uint32_t expect_size, uint32_t timeout); 128 129 /** 130 * Receive data on a UART interface 131 * 132 * @param[in] uart the UART interface 133 * @param[out] data pointer to the buffer which will store incoming data 134 * @param[in] expect_size number of bytes to receive 135 * @param[out] recv_size number of bytes received 136 * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER 137 * if you want to wait forever 138 * 139 * @return 0 : on success, EIO : if an error occurred with any step 140 */ 141 int32_t hal_uart_recv_II(uart_dev_t *uart, void *data, uint32_t expect_size, 142 uint32_t *recv_size, uint32_t timeout); 143 144 /** 145 * Deinitialises a UART interface 146 * 147 * @param[in] uart the interface which should be deinitialised 148 * 149 * @return 0 : on success, EIO : if an error occurred with any step 150 */ 151 int32_t hal_uart_finalize(uart_dev_t *uart); 152 153 int32_t UartGetc(void); 154 155 #ifdef __cplusplus 156 } 157 #endif /* __cplusplus */ 158 159 #endif /* HAL_UART_H */ 160