1 /* 2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved. 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 __DUET_UART_H 17 #define __DUET_UART_H 18 19 #include <stdint.h> 20 #include "stdbool.h" 21 #include <errno.h> 22 #include "duet_cm4.h" 23 #include "system_cm4.h" 24 #include "duet_uart_index.h" 25 #include "duet.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #define DUET_UART0_INDEX 0 32 #define DUET_UART1_INDEX 1 33 #define DUET_UART2_INDEX 2 34 #define DUET_UART_NUM 3 35 36 /* UART BAUDRATE */ 37 #define UART_BAUDRATE_110 (110) 38 #define UART_BAUDRATE_300 (300) 39 #define UART_BAUDRATE_600 (600) 40 #define UART_BAUDRATE_1200 (1200) 41 #define UART_BAUDRATE_2400 (2400) 42 #define UART_BAUDRATE_4800 (4800) 43 #define UART_BAUDRATE_9600 (9600) 44 #define UART_BAUDRATE_14400 (14400) 45 #define UART_BAUDRATE_19200 (19200) 46 #define UART_BAUDRATE_38400 (38400) 47 #define UART_BAUDRATE_57600 (57600) 48 #define UART_BAUDRATE_115200 (115200) 49 #define UART_BAUDRATE_230400 (230400) 50 #define UART_BAUDRATE_460800 (460800) 51 #define UART_BAUDRATE_921600 (921600) 52 53 /* fifo depth */ 54 #define UART_TX_FIFO_DEPTH (32) 55 #define UART_RX_FIFO_DEPTH (32) 56 57 /* UART flags */ 58 #define UART_FLAG_TX_FIFO_EMPTY (1<<7) 59 #define UART_FLAG_RX_FIFO_FULL (1<<6) 60 #define UART_FLAG_TX_FIFO_FULL (1<<5) 61 #define UART_FLAG_RX_FIFO_EMPTY (1<<4) 62 #define UART_FLAG_BUSY (1<<3) 63 64 /* UART interrutps */ 65 #define UART_RX_INTERRUPT (1<<4) /* tx interrupt is only set when tx fifo level transisions through a level, not based on fifo level itself */ 66 #define UART_TX_INTERRUPT (1<<5) 67 #define UART_RX_TIMEOUT_INTERRUPT (1<<6) 68 #define UART_ERROR_FRAME_INTERRUPT (1<<7) 69 #define UART_ERROR_PARITY_INTERRUPT (1<<8) 70 #define UART_ERROR_BREAK_INTERRUPT (1<<9) 71 #define UART_ERROR_OVERRUN_INTERRUPT (1<<10) 72 #define UART_DISABLE_ALL_IRQ (0x00) 73 74 #define UART_DMA_TX_EN (1<<1) 75 #define UART_DMA_RX_EN (1<<0) 76 77 #define UART_IRDA_ENABLE (1<<1) 78 #define UART_IRDA_LP_EN (1<<2) 79 #define UART_CLK (52000000) 80 81 typedef void (*duet_uart_callback_func)(char); 82 extern duet_uart_callback_func g_duet_uart_callback_handler[UART_NUM]; 83 /* 84 * UART data width 85 */ 86 typedef enum { 87 DATA_5BIT, 88 DATA_6BIT, 89 DATA_7BIT, 90 DATA_8BIT 91 } uart_data_width_t; 92 93 /* 94 * UART stop bits 95 */ 96 typedef enum { 97 STOP_1BIT, 98 STOP_2BITS 99 } uart_stop_bits_t; 100 101 /* 102 * UART flow control 103 */ 104 typedef enum { 105 FLOW_CTRL_DISABLED, 106 FLOW_CTRL_RTS, 107 FLOW_CTRL_CTS, 108 FLOW_CTRL_CTS_RTS 109 } uart_flow_control_t; 110 111 /* 112 * UART parity 113 */ 114 typedef enum { 115 PARITY_NO, 116 PARITY_ODD, 117 PARITY_EVEN 118 } uart_parity_t; 119 120 /* 121 * UART mode 122 */ 123 typedef enum { 124 TX_MODE = 1, 125 RX_MODE, 126 TX_RX_MODE 127 } uart_mode_t; 128 129 /* 130 * UART fifo_threshold 131 * TX/RX FIFO Level, both FIFO of depth 16 132 */ 133 typedef enum { 134 FIFO_1_8_FULL = 0, // 1/8 135 FIFO_1_4_FULL = 1, // 1/4 136 FIFO_HALF_FULL = 2, // 1/2 137 FIFO_3_4_FULL = 3, // 3/4 138 FIFO_7_8_FULL = 4, // 7/8 139 FIFO_NULL 140 } uart_fifo_threshold_t; 141 142 /* 143 * UART configuration 144 */ 145 typedef struct { 146 uint32_t baud_rate; 147 uart_data_width_t data_width; 148 uart_parity_t parity; 149 uart_stop_bits_t stop_bits; 150 uart_flow_control_t flow_control; 151 uart_mode_t mode; 152 153 } duet_uart_config_t; 154 155 typedef struct { 156 uint8_t port; /* uart port */ 157 duet_uart_config_t config; /* uart config */ 158 void *priv; /* priv data */ 159 } duet_uart_dev_t; 160 161 UART_TypeDef *getUartxViaIdx(uint8_t uart_idx); 162 163 void duet_uart_struct_init(duet_uart_dev_t *UART_InitStruct); 164 int32_t duet_uart_dma_config(duet_uart_dev_t *uart, uint8_t dma_tx_rx_sel, uint8_t new_state); 165 /** 166 * Initialises a UART interface 167 * 168 * 169 * @param[in] uart the interface which should be initialised 170 * 171 * @return 0 : on success, EIO : if an error occurred with any step 172 */ 173 int32_t duet_uart_init(duet_uart_dev_t *uart); 174 175 /** 176 * Transmit data on a UART interface 177 * 178 * @param[in] uart the UART interface 179 * @param[in] data pointer to the start of data 180 * @param[in] size number of bytes to transmit 181 * 182 * @return 0 : on success, EIO : if an error occurred with any step 183 */ 184 int32_t duet_uart_send(duet_uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout); 185 186 /** 187 * Deinitialises a UART interface 188 * 189 * @param[in] uart the interface which should be deinitialised 190 * 191 * @return 0 : on success, EIO : if an error occurred with any step 192 */ 193 int32_t duet_uart_finalize(duet_uart_dev_t *uart); 194 195 void duet_uart_start(UART_TypeDef *UARTx); 196 197 void duet_uart_stop(UART_TypeDef *UARTx); 198 199 void duet_uart_set_callback(uint8_t uart_idx, duet_uart_callback_func func); 200 201 /** 202 * calculate integer divider & fractional divider 203 * 204 */ 205 uint32_t duet_uart_calc_baud(uint32_t baud); 206 207 void duet_uart_interrupt_config(UART_TypeDef *UARTx, uint32_t uart_int, bool new_state); 208 uint8_t duet_uart_clear_interrupt(UART_TypeDef *UARTx, uint32_t uart_interrupt); 209 ITstatus duet_uart_get_raw_interrupt_status(UART_TypeDef *UARTx, uint32_t uart_interrupt); 210 ITstatus duet_uart_get_interrupt_status(UART_TypeDef *UARTx, uint32_t uart_interrupt); 211 ITstatus duet_uart_get_flag_status(UART_TypeDef *UARTx, uint8_t uart_flag); 212 213 void UART_SendData(UART_TypeDef *UARTx, unsigned char Data); 214 uint8_t UART_ReceiveData(UART_TypeDef *UARTx); 215 216 #ifdef __cplusplus 217 } 218 #endif 219 #endif // __DUET_UART_H 220