1 /* 2 * Copyright (c) 2016-2017 ARM Limited 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * \file arm_uart_drv.h 19 * \brief Generic driver for ARM UART. 20 */ 21 22 #ifndef __ARM_UART_DRV_H__ 23 #define __ARM_UART_DRV_H__ 24 25 #include <stdint.h> 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 /* ARM UART device configuration structure */ 32 struct arm_uart_dev_cfg_t { 33 const uint32_t base; /*!< UART base address */ 34 const uint32_t default_baudrate; /*!< Default baudrate */ 35 }; 36 37 /* ARM UART device data structure */ 38 struct arm_uart_dev_data_t { 39 uint32_t state; /*!< Indicates if the uart driver 40 is initialized and enabled */ 41 uint32_t system_clk; /*!< System clock */ 42 uint32_t baudrate; /*!< Baudrate */ 43 }; 44 45 /* ARM UART device structure */ 46 struct arm_uart_dev_t { 47 const struct arm_uart_dev_cfg_t* const cfg; /*!< UART configuration */ 48 struct arm_uart_dev_data_t* const data; /*!< UART data */ 49 }; 50 51 /* ARM UART enumeration types */ 52 enum arm_uart_error_t { 53 ARM_UART_ERR_NONE = 0, /*!< No error */ 54 ARM_UART_ERR_INVALID_ARG, /*!< Error invalid input argument */ 55 ARM_UART_ERR_INVALID_BAUD, /*!< Invalid baudrate */ 56 ARM_UART_ERR_NOT_INIT, /*!< Error UART not initialized */ 57 ARM_UART_ERR_NOT_READY, /*!< Error UART not ready */ 58 }; 59 60 enum arm_uart_irq_t { 61 ARM_UART_IRQ_RX, /*!< RX interrupt source */ 62 ARM_UART_IRQ_TX, /*!< TX interrupt source */ 63 ARM_UART_IRQ_COMBINED /*!< RX-TX combined interrupt source */ 64 }; 65 66 /** 67 * \brief Initializes UART. It uses the default baudrate to configure 68 * the peripheral at this point. 69 * 70 * \param[in] dev UART device struct \ref arm_uart_dev_t 71 * \param[in] system_clk System clock used by the device. 72 * 73 * \return Returns error code as specified in \ref arm_uart_error_t 74 * 75 * \note This function doesn't check if dev is NULL. 76 */ 77 enum arm_uart_error_t arm_uart_init(struct arm_uart_dev_t* dev, 78 uint32_t system_clk); 79 80 /** 81 * \brief Sets the UART baudrate. 82 * 83 * \param[in] dev UART device struct \ref arm_uart_dev_t 84 * \param[in] baudrate New baudrate. 85 * 86 * \return Returns error code as specified in \ref arm_uart_error_t 87 * 88 * \note This function doesn't check if dev is NULL. 89 */ 90 enum arm_uart_error_t arm_uart_set_baudrate(struct arm_uart_dev_t* dev, 91 uint32_t baudrate); 92 93 /** 94 * \brief Gets the UART baudrate. 95 * 96 * \param[in] dev UART device struct \ref arm_uart_dev_t 97 * 98 * \return Returns the UART baudrate. 99 * 100 * \note This function doesn't check if dev is NULL. 101 */ 102 uint32_t arm_uart_get_baudrate(struct arm_uart_dev_t* dev); 103 104 /** 105 * \brief Sets system clock. 106 * 107 * \param[in] dev UART device struct \ref arm_uart_dev_t 108 * \param[in] system_clk System clock used by the device. 109 * 110 * \return Returns error code as specified in \ref arm_uart_error_t 111 * 112 * \note This function doesn't check if dev is NULL. 113 */ 114 enum arm_uart_error_t arm_uart_set_clock(struct arm_uart_dev_t* dev, 115 uint32_t system_clk); 116 /** 117 * \brief Reads one byte from UART dev. 118 * 119 * \param[in] dev UART device struct \ref arm_uart_dev_t 120 * \param[in] byte Pointer to byte. 121 * 122 * \return Returns error code as specified in \ref arm_uart_error_t 123 * 124 * \note For better performance, this function doesn't check if dev and byte 125 * pointer are NULL, and if the driver is initialized. 126 */ 127 enum arm_uart_error_t arm_uart_read(struct arm_uart_dev_t* dev, uint8_t* byte); 128 129 /** 130 * \brief Writes a byte to UART dev. 131 * 132 * \param[in] dev UART device struct \ref arm_uart_dev_t 133 * \param[in] byte Byte to write. 134 * 135 * \return Returns error code as specified in \ref arm_uart_error_t 136 * 137 * \note For better performance, this function doesn't check if dev is NULL and 138 * if the driver is initialized to have better performance. 139 */ 140 enum arm_uart_error_t arm_uart_write(struct arm_uart_dev_t* dev, uint8_t byte); 141 142 /** 143 * \brief Enables TX interrupt. 144 * 145 * \param[in] dev UART device struct \ref arm_uart_dev_t 146 * 147 * \return Returns error code as specified in \ref arm_uart_error_t 148 * 149 * \note This function doesn't check if dev is NULL. 150 */ 151 enum arm_uart_error_t arm_uart_irq_tx_enable(struct arm_uart_dev_t* dev); 152 153 /** 154 * \brief Disables TX interrupt. 155 * 156 * \param[in] dev UART device struct \ref arm_uart_dev_t 157 * 158 * \note This function doesn't check if dev is NULL. 159 */ 160 void arm_uart_irq_tx_disable(struct arm_uart_dev_t* dev); 161 162 /** 163 * \brief Verifies if Tx is ready to send more data. 164 * 165 * \param[in] dev UART device struct \ref arm_uart_dev_t 166 * 167 * \return 1 if TX is ready, 0 otherwise. 168 * 169 * \note This function doesn't check if dev is NULL. 170 */ 171 uint32_t arm_uart_tx_ready(struct arm_uart_dev_t* dev); 172 173 /** 174 * \brief Enables RX interrupt. 175 * 176 * \param[in] dev UART device struct \ref arm_uart_dev_t 177 * 178 * \return Returns error code as specified in \ref arm_uart_error_t 179 * 180 * \note This function doesn't check if dev is NULL. 181 */ 182 enum arm_uart_error_t arm_uart_irq_rx_enable(struct arm_uart_dev_t* dev); 183 184 /** 185 * \brief Disables RX interrupt 186 * 187 * \param[in] dev UART device struct \ref arm_uart_dev_t 188 * 189 * \note This function doesn't check if dev is NULL. 190 */ 191 void arm_uart_irq_rx_disable(struct arm_uart_dev_t* dev); 192 193 /** 194 * \brief Verifies if Rx has data. 195 * 196 * \param[in] dev UART device struct \ref arm_uart_dev_t 197 * 198 * \return 1 if RX has data, 0 otherwise. 199 * 200 * \note This function doesn't check if dev is NULL. 201 */ 202 uint32_t arm_uart_rx_ready(struct arm_uart_dev_t* dev); 203 204 /** 205 * \brief Clears UART interrupt. 206 * 207 * \param[in] dev UART device struct \ref arm_uart_dev_t 208 * \param[in] irq IRQ source to clean \ref arm_uart_irq_t 209 * 210 * \note This function doesn't check if dev is NULL. 211 */ 212 void arm_uart_clear_interrupt(struct arm_uart_dev_t* dev, 213 enum arm_uart_irq_t irq); 214 215 #ifdef __cplusplus 216 } 217 #endif 218 #endif /* __ARM_UART_DRV_H__ */ 219