Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 1 | /* |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 2 | * Copyright (c) 2019 Mohamed ElShahawi (extremegtx@hotmail.com) |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 7 | #define DT_DRV_COMPAT espressif_esp32_uart |
| 8 | |
Leandro Pereira | d52de29 | 2017-08-18 15:15:47 -0700 | [diff] [blame] | 9 | /* Include esp-idf headers first to avoid redefining BIT() macro */ |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 10 | #include <rom/ets_sys.h> |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 11 | #include <soc/dport_reg.h> |
Leandro Pereira | d52de29 | 2017-08-18 15:15:47 -0700 | [diff] [blame] | 12 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 13 | #include <rom/gpio.h> |
| 14 | |
| 15 | #include <soc/gpio_sig_map.h> |
| 16 | |
| 17 | #include <device.h> |
Leandro Pereira | 5f22dab | 2017-09-15 16:52:36 -0700 | [diff] [blame] | 18 | #include <soc.h> |
Anas Nashif | d1b2718 | 2019-06-25 15:54:01 -0400 | [diff] [blame] | 19 | #include <drivers/uart.h> |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 21 | #include <sys/util.h> |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 22 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * ESP32 UARTx register map structure |
| 26 | */ |
| 27 | struct uart_esp32_regs_t { |
| 28 | u32_t fifo; |
| 29 | u32_t int_raw; |
| 30 | u32_t int_st; |
| 31 | u32_t int_ena; |
| 32 | u32_t int_clr; |
| 33 | u32_t clk_div; |
| 34 | u32_t auto_baud; |
| 35 | u32_t status; |
| 36 | u32_t conf0; |
| 37 | u32_t conf1; |
| 38 | u32_t lowpulse; |
| 39 | u32_t highpulse; |
| 40 | u32_t rxd_cnt; |
| 41 | u32_t flow_conf; |
| 42 | u32_t sleep_conf; |
| 43 | u32_t swfc_conf; |
| 44 | u32_t idle_conf; |
| 45 | u32_t rs485_conf; |
| 46 | u32_t at_cmd_precnt; |
| 47 | u32_t at_cmd_postcnt; |
| 48 | u32_t at_cmd_gaptout; |
| 49 | u32_t at_cmd_char; |
| 50 | u32_t mem_conf; |
| 51 | u32_t mem_tx_status; |
| 52 | u32_t mem_rx_status; |
| 53 | u32_t mem_cnt_status; |
| 54 | u32_t pospulse; |
| 55 | u32_t negpulse; |
| 56 | u32_t reserved_0; |
| 57 | u32_t reserved_1; |
| 58 | u32_t date; |
| 59 | u32_t id; |
| 60 | }; |
| 61 | |
| 62 | struct uart_esp32_config { |
| 63 | |
| 64 | struct uart_device_config dev_conf; |
| 65 | |
| 66 | const struct { |
| 67 | int tx_out; |
| 68 | int rx_in; |
| 69 | int rts_out; |
| 70 | int cts_in; |
| 71 | } signals; |
| 72 | |
| 73 | const struct { |
| 74 | int tx; |
| 75 | int rx; |
| 76 | int rts; |
| 77 | int cts; |
| 78 | } pins; |
| 79 | |
| 80 | const struct esp32_peripheral peripheral; |
| 81 | |
| 82 | const struct { |
| 83 | int source; |
| 84 | int line; |
| 85 | } irq; |
| 86 | }; |
| 87 | |
| 88 | /* driver data */ |
| 89 | struct uart_esp32_data { |
| 90 | struct uart_config uart_config; |
| 91 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 92 | uart_irq_callback_user_data_t irq_cb; |
| 93 | void *irq_cb_data; |
| 94 | #endif |
| 95 | }; |
| 96 | |
| 97 | #define DEV_CFG(dev) \ |
| 98 | ((const struct uart_esp32_config *const)(dev)->config->config_info) |
| 99 | #define DEV_DATA(dev) \ |
| 100 | ((struct uart_esp32_data *)(dev)->driver_data) |
| 101 | #define DEV_BASE(dev) \ |
| 102 | ((volatile struct uart_esp32_regs_t *)(DEV_CFG(dev))->dev_conf.base) |
| 103 | |
| 104 | #define UART_TXFIFO_COUNT(status_reg) ((status_reg >> 16) & 0xFF) |
| 105 | #define UART_RXFIFO_COUNT(status_reg) ((status_reg >> 0) & 0xFF) |
| 106 | |
| 107 | #define UART_FIFO_LIMIT 127U |
| 108 | #define UART_TX_FIFO_THRESH 0x1 |
| 109 | #define UART_RX_FIFO_THRESH 0x1 |
| 110 | |
| 111 | #define UART_GET_PARITY_ERR(reg) ((reg >> 2) & 0x1) |
| 112 | #define UART_GET_FRAME_ERR(reg) ((reg >> 3) & 0x1) |
| 113 | |
| 114 | #define UART_GET_PARITY(conf0_reg) ((conf0_reg >> 0) & 0x1) |
| 115 | #define UART_GET_PARITY_EN(conf0_reg) ((conf0_reg >> 1) & 0x1) |
| 116 | #define UART_GET_DATA_BITS(conf0_reg) ((conf0_reg >> 2) & 0x3) |
| 117 | #define UART_GET_STOP_BITS(conf0_reg) ((conf0_reg >> 4) & 0x3) |
| 118 | #define UART_GET_TX_FLOW(conf0_reg) ((conf0_reg >> 15) & 0x1) |
| 119 | #define UART_GET_RX_FLOW(conf1_reg) ((conf1_reg >> 23) & 0x1) |
| 120 | |
| 121 | /* FIXME: This should be removed when interrupt support added to ESP32 dts */ |
Kumar Gala | b99ee32 | 2020-03-24 19:37:52 -0500 | [diff] [blame] | 122 | #define INST_0_ESPRESSIF_ESP32_UART_IRQ_0 12 |
| 123 | #define INST_1_ESPRESSIF_ESP32_UART_IRQ_0 17 |
| 124 | #define INST_2_ESPRESSIF_ESP32_UART_IRQ_0 18 |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 125 | |
| 126 | /* ESP-IDF Naming is not consistent for UART0 with UART1/2 */ |
| 127 | #define DPORT_UART0_CLK_EN DPORT_UART_CLK_EN |
| 128 | #define DPORT_UART0_RST DPORT_UART_RST |
| 129 | |
| 130 | static int uart_esp32_poll_in(struct device *dev, unsigned char *p_char) |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 131 | { |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 132 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 133 | if (UART_RXFIFO_COUNT(DEV_BASE(dev)->status) == 0) { |
| 134 | return -1; |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 135 | } |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 136 | |
| 137 | *p_char = DEV_BASE(dev)->fifo; |
| 138 | return 0; |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 141 | static void uart_esp32_poll_out(struct device *dev, |
| 142 | unsigned char c) |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 143 | { |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 144 | /* Wait for space in FIFO */ |
| 145 | while (UART_TXFIFO_COUNT(DEV_BASE(dev)->status) >= UART_FIFO_LIMIT) { |
| 146 | ; /* Wait */ |
| 147 | } |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 148 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 149 | /* Send a character */ |
| 150 | DEV_BASE(dev)->fifo = (u32_t)c; |
| 151 | } |
| 152 | |
| 153 | static int uart_esp32_err_check(struct device *dev) |
| 154 | { |
| 155 | u32_t err = UART_GET_PARITY_ERR(DEV_BASE(dev)->int_st) |
| 156 | | UART_GET_FRAME_ERR(DEV_BASE(dev)->int_st); |
| 157 | |
| 158 | return err; |
| 159 | } |
| 160 | |
| 161 | static int uart_esp32_config_get(struct device *dev, struct uart_config *cfg) |
| 162 | { |
| 163 | struct uart_esp32_data *data = DEV_DATA(dev); |
| 164 | |
| 165 | cfg->baudrate = data->uart_config.baudrate; |
| 166 | |
| 167 | if (UART_GET_PARITY_EN(DEV_BASE(dev)->conf0)) { |
| 168 | cfg->parity = UART_GET_PARITY(DEV_BASE(dev)->conf0); |
| 169 | } else { |
| 170 | cfg->parity = UART_CFG_PARITY_NONE; |
| 171 | } |
| 172 | |
| 173 | cfg->stop_bits = UART_GET_STOP_BITS(DEV_BASE(dev)->conf0); |
| 174 | cfg->data_bits = UART_GET_DATA_BITS(DEV_BASE(dev)->conf0); |
| 175 | |
| 176 | if (UART_GET_TX_FLOW(DEV_BASE(dev)->conf0)) { |
| 177 | cfg->flow_ctrl = UART_CFG_FLOW_CTRL_RTS_CTS; |
| 178 | } |
| 179 | |
| 180 | if (UART_GET_RX_FLOW(DEV_BASE(dev)->conf1)) { |
| 181 | cfg->flow_ctrl = UART_CFG_FLOW_CTRL_DTR_DSR; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int uart_esp32_set_baudrate(struct device *dev, int baudrate) |
| 187 | { |
| 188 | u32_t sys_clk_freq = DEV_CFG(dev)->dev_conf.sys_clk_freq; |
| 189 | u32_t clk_div = (((sys_clk_freq) << 4) / baudrate); |
| 190 | |
| 191 | while (UART_TXFIFO_COUNT(DEV_BASE(dev)->status)) { |
| 192 | ; /* Wait */ |
| 193 | } |
| 194 | |
| 195 | if (clk_div < 16) { |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | DEV_BASE(dev)->clk_div = ((clk_div >> 4) | (clk_div & 0xf)); |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | static int uart_esp32_configure_pins(struct device *dev) |
| 204 | { |
| 205 | const struct uart_esp32_config *const cfg = DEV_CFG(dev); |
| 206 | |
| 207 | esp32_rom_gpio_matrix_out(cfg->pins.tx, |
| 208 | cfg->signals.tx_out, |
| 209 | false, |
| 210 | false); |
| 211 | |
| 212 | esp32_rom_gpio_matrix_in(cfg->pins.rx, |
| 213 | cfg->signals.rx_in, |
| 214 | false); |
| 215 | |
Mohamed ElShahawi | bcd9e49 | 2019-08-23 00:53:22 +0200 | [diff] [blame] | 216 | if (cfg->pins.cts) { |
| 217 | esp32_rom_gpio_matrix_out(cfg->pins.cts, |
| 218 | cfg->signals.cts_in, |
| 219 | false, |
| 220 | false); |
| 221 | } |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 222 | |
Mohamed ElShahawi | bcd9e49 | 2019-08-23 00:53:22 +0200 | [diff] [blame] | 223 | if (cfg->pins.rts) { |
| 224 | esp32_rom_gpio_matrix_in(cfg->pins.rts, |
| 225 | cfg->signals.rts_out, |
| 226 | false); |
| 227 | } |
Ivan Grokhotkov | d58d5d1 | 2017-05-17 18:22:43 +0800 | [diff] [blame] | 228 | |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 232 | static int uart_esp32_configure(struct device *dev, |
| 233 | const struct uart_config *cfg) |
| 234 | { |
| 235 | u32_t conf0 = UART_TICK_REF_ALWAYS_ON; |
| 236 | u32_t conf1 = (UART_RX_FIFO_THRESH << UART_RXFIFO_FULL_THRHD_S) |
| 237 | | (UART_TX_FIFO_THRESH << UART_TXFIFO_EMPTY_THRHD_S); |
| 238 | |
| 239 | uart_esp32_configure_pins(dev); |
| 240 | esp32_enable_peripheral(&DEV_CFG(dev)->peripheral); |
| 241 | |
| 242 | /* |
| 243 | * Reset RX Buffer by reading all received bytes |
| 244 | * Hardware Reset functionality can't be used with UART 1/2 |
| 245 | */ |
| 246 | while (UART_RXFIFO_COUNT(DEV_BASE(dev)->status) != 0) { |
| 247 | (void) DEV_BASE(dev)->fifo; |
| 248 | } |
| 249 | |
| 250 | switch (cfg->parity) { |
| 251 | case UART_CFG_PARITY_NONE: |
| 252 | conf0 &= ~(UART_PARITY_EN); |
| 253 | conf0 &= ~(UART_PARITY); |
| 254 | break; |
| 255 | case UART_CFG_PARITY_EVEN: |
| 256 | conf0 &= ~(UART_PARITY); |
| 257 | break; |
| 258 | case UART_CFG_PARITY_ODD: |
| 259 | conf0 |= UART_PARITY; |
| 260 | break; |
| 261 | default: |
| 262 | return -ENOTSUP; |
| 263 | } |
| 264 | |
| 265 | switch (cfg->stop_bits) { |
| 266 | case UART_CFG_STOP_BITS_1: |
| 267 | case UART_CFG_STOP_BITS_1_5: |
| 268 | case UART_CFG_STOP_BITS_2: |
| 269 | conf0 |= cfg->stop_bits << UART_STOP_BIT_NUM_S; |
| 270 | break; |
| 271 | default: |
| 272 | return -ENOTSUP; |
| 273 | } |
| 274 | |
| 275 | if (cfg->data_bits <= UART_CFG_DATA_BITS_8) { |
| 276 | conf0 |= cfg->data_bits << UART_BIT_NUM_S; |
| 277 | } else { |
| 278 | return -ENOTSUP; |
| 279 | } |
| 280 | |
| 281 | switch (cfg->flow_ctrl) { |
| 282 | case UART_CFG_FLOW_CTRL_NONE: |
| 283 | conf0 &= ~(UART_TX_FLOW_EN); |
| 284 | conf1 &= ~(UART_RX_FLOW_EN); |
| 285 | break; |
| 286 | case UART_CFG_FLOW_CTRL_RTS_CTS: |
| 287 | conf0 |= UART_TX_FLOW_EN; |
| 288 | conf1 |= UART_RX_FLOW_EN; |
| 289 | break; |
| 290 | default: |
| 291 | return -ENOTSUP; |
| 292 | } |
| 293 | |
| 294 | if (uart_esp32_set_baudrate(dev, cfg->baudrate)) { |
| 295 | DEV_DATA(dev)->uart_config.baudrate = cfg->baudrate; |
| 296 | } else { |
| 297 | return -ENOTSUP; |
| 298 | } |
| 299 | |
| 300 | DEV_BASE(dev)->conf0 = conf0; |
| 301 | DEV_BASE(dev)->conf1 = conf1; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int uart_esp32_init(struct device *dev) |
| 307 | { |
| 308 | uart_esp32_configure(dev, &DEV_DATA(dev)->uart_config); |
| 309 | |
| 310 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 311 | DEV_CFG(dev)->dev_conf.irq_config_func(dev); |
| 312 | #endif |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 318 | |
| 319 | static int uart_esp32_fifo_fill(struct device *dev, |
| 320 | const u8_t *tx_data, int len) |
| 321 | { |
| 322 | u8_t num_tx = 0U; |
| 323 | |
| 324 | while ((len - num_tx > 0) && |
| 325 | UART_TXFIFO_COUNT(DEV_BASE(dev)->status) < UART_FIFO_LIMIT) { |
| 326 | DEV_BASE(dev)->fifo = (u32_t)tx_data[num_tx++]; |
| 327 | } |
| 328 | |
| 329 | return num_tx; |
| 330 | } |
| 331 | |
| 332 | static int uart_esp32_fifo_read(struct device *dev, |
| 333 | u8_t *rx_data, const int len) |
| 334 | { |
| 335 | u8_t num_rx = 0U; |
| 336 | |
| 337 | while ((len - num_rx > 0) && |
| 338 | (UART_RXFIFO_COUNT(DEV_BASE(dev)->status) != 0)) { |
| 339 | rx_data[num_rx++] = DEV_BASE(dev)->fifo; |
| 340 | } |
| 341 | |
| 342 | return num_rx; |
| 343 | } |
| 344 | |
| 345 | static void uart_esp32_irq_tx_enable(struct device *dev) |
| 346 | { |
| 347 | DEV_BASE(dev)->int_clr |= UART_TXFIFO_EMPTY_INT_ENA; |
| 348 | DEV_BASE(dev)->int_ena |= UART_TXFIFO_EMPTY_INT_ENA; |
| 349 | } |
| 350 | |
| 351 | static void uart_esp32_irq_tx_disable(struct device *dev) |
| 352 | { |
| 353 | DEV_BASE(dev)->int_ena &= ~(UART_TXFIFO_EMPTY_INT_ENA); |
| 354 | } |
| 355 | |
| 356 | static int uart_esp32_irq_tx_ready(struct device *dev) |
| 357 | { |
| 358 | return (UART_TXFIFO_COUNT(DEV_BASE(dev)->status) < UART_FIFO_LIMIT); |
| 359 | } |
| 360 | |
| 361 | static void uart_esp32_irq_rx_enable(struct device *dev) |
| 362 | { |
| 363 | DEV_BASE(dev)->int_clr |= UART_RXFIFO_FULL_INT_ENA; |
| 364 | DEV_BASE(dev)->int_ena |= UART_RXFIFO_FULL_INT_ENA; |
| 365 | } |
| 366 | |
| 367 | static void uart_esp32_irq_rx_disable(struct device *dev) |
| 368 | { |
| 369 | DEV_BASE(dev)->int_ena &= ~(UART_RXFIFO_FULL_INT_ENA); |
| 370 | } |
| 371 | |
| 372 | static int uart_esp32_irq_tx_complete(struct device *dev) |
| 373 | { |
| 374 | /* check if TX FIFO is empty */ |
| 375 | return (UART_TXFIFO_COUNT(DEV_BASE(dev)->status) == 0 ? 1 : 0); |
| 376 | } |
| 377 | |
| 378 | static int uart_esp32_irq_rx_ready(struct device *dev) |
| 379 | { |
| 380 | return (UART_RXFIFO_COUNT(DEV_BASE(dev)->status) > 0); |
| 381 | } |
| 382 | |
| 383 | static void uart_esp32_irq_err_enable(struct device *dev) |
| 384 | { |
| 385 | /* enable framing, parity */ |
| 386 | DEV_BASE(dev)->int_ena |= UART_FRM_ERR_INT_ENA |
| 387 | | UART_PARITY_ERR_INT_ENA; |
| 388 | } |
| 389 | |
| 390 | static void uart_esp32_irq_err_disable(struct device *dev) |
| 391 | { |
| 392 | DEV_BASE(dev)->int_ena &= ~(UART_FRM_ERR_INT_ENA); |
| 393 | DEV_BASE(dev)->int_ena &= ~(UART_PARITY_ERR_INT_ENA); |
| 394 | } |
| 395 | |
| 396 | static int uart_esp32_irq_is_pending(struct device *dev) |
| 397 | { |
| 398 | return uart_esp32_irq_rx_ready(dev) || uart_esp32_irq_tx_ready(dev); |
| 399 | } |
| 400 | |
| 401 | static int uart_esp32_irq_update(struct device *dev) |
| 402 | { |
| 403 | DEV_BASE(dev)->int_clr |= UART_RXFIFO_FULL_INT_ENA; |
| 404 | DEV_BASE(dev)->int_clr |= UART_TXFIFO_EMPTY_INT_ENA; |
| 405 | |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | static void uart_esp32_irq_callback_set(struct device *dev, |
| 410 | uart_irq_callback_user_data_t cb, |
| 411 | void *cb_data) |
| 412 | { |
| 413 | DEV_DATA(dev)->irq_cb = cb; |
| 414 | DEV_DATA(dev)->irq_cb_data = cb_data; |
| 415 | } |
| 416 | |
| 417 | void uart_esp32_isr(void *arg) |
| 418 | { |
| 419 | struct device *dev = arg; |
| 420 | struct uart_esp32_data *data = DEV_DATA(dev); |
| 421 | |
| 422 | /* Verify if the callback has been registered */ |
| 423 | if (data->irq_cb) { |
| 424 | data->irq_cb(data->irq_cb_data); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
| 429 | |
| 430 | static const struct uart_driver_api uart_esp32_api = { |
| 431 | .poll_in = uart_esp32_poll_in, |
| 432 | .poll_out = uart_esp32_poll_out, |
| 433 | .err_check = uart_esp32_err_check, |
| 434 | .configure = uart_esp32_configure, |
| 435 | .config_get = uart_esp32_config_get, |
| 436 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 437 | .fifo_fill = uart_esp32_fifo_fill, |
| 438 | .fifo_read = uart_esp32_fifo_read, |
| 439 | .irq_tx_enable = uart_esp32_irq_tx_enable, |
| 440 | .irq_tx_disable = uart_esp32_irq_tx_disable, |
| 441 | .irq_tx_ready = uart_esp32_irq_tx_ready, |
| 442 | .irq_rx_enable = uart_esp32_irq_rx_enable, |
| 443 | .irq_rx_disable = uart_esp32_irq_rx_disable, |
| 444 | .irq_tx_complete = uart_esp32_irq_tx_complete, |
| 445 | .irq_rx_ready = uart_esp32_irq_rx_ready, |
| 446 | .irq_err_enable = uart_esp32_irq_err_enable, |
| 447 | .irq_err_disable = uart_esp32_irq_err_disable, |
| 448 | .irq_is_pending = uart_esp32_irq_is_pending, |
| 449 | .irq_update = uart_esp32_irq_update, |
| 450 | .irq_callback_set = uart_esp32_irq_callback_set, |
| 451 | #endif /* CONFIG_UART_INTERRUPT_DRIVEN */ |
Leandro Pereira | 37ea771 | 2017-05-10 17:12:12 -0700 | [diff] [blame] | 452 | }; |
| 453 | |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 454 | |
| 455 | #ifdef CONFIG_UART_INTERRUPT_DRIVEN |
| 456 | #define ESP32_UART_IRQ_HANDLER_DECL(idx) \ |
| 457 | static void uart_esp32_irq_config_func_##idx(struct device *dev) |
| 458 | |
| 459 | #define ESP32_UART_IRQ_HANDLER_FUNC(idx) \ |
| 460 | .irq_config_func = uart_esp32_irq_config_func_##idx, |
| 461 | |
| 462 | #define ESP32_UART_IRQ_HANDLER(idx) \ |
| 463 | static void uart_esp32_irq_config_func_##idx(struct device *dev) \ |
| 464 | { \ |
| 465 | esp32_rom_intr_matrix_set(0, ETS_UART##idx##_INTR_SOURCE, \ |
Kumar Gala | b99ee32 | 2020-03-24 19:37:52 -0500 | [diff] [blame] | 466 | INST_##idx##_ESPRESSIF_ESP32_UART_IRQ_0); \ |
| 467 | IRQ_CONNECT(INST_##idx##_ESPRESSIF_ESP32_UART_IRQ_0, \ |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 468 | 1, \ |
| 469 | uart_esp32_isr, \ |
| 470 | DEVICE_GET(uart_esp32_##idx), \ |
| 471 | 0); \ |
Kumar Gala | b99ee32 | 2020-03-24 19:37:52 -0500 | [diff] [blame] | 472 | irq_enable(INST_##idx##_ESPRESSIF_ESP32_UART_IRQ_0); \ |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 473 | } |
| 474 | #else |
| 475 | #define ESP32_UART_IRQ_HANDLER_DECL(idx) |
| 476 | #define ESP32_UART_IRQ_HANDLER_FUNC(idx) |
| 477 | #define ESP32_UART_IRQ_HANDLER(idx) |
| 478 | |
| 479 | #endif |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 480 | #define ESP32_UART_INIT(idx) \ |
| 481 | ESP32_UART_IRQ_HANDLER_DECL(idx); \ |
| 482 | static const struct uart_esp32_config uart_esp32_cfg_port_##idx = { \ |
| 483 | .dev_conf = { \ |
| 484 | .base = \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 485 | (u8_t *)DT_INST_REG_ADDR(idx), \ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 486 | .sys_clk_freq = \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 487 | DT_PROP(DT_INST(0, cadence_tensilica_xtensa_lx6), clock_frequency),\ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 488 | ESP32_UART_IRQ_HANDLER_FUNC(idx) \ |
| 489 | }, \ |
| 490 | \ |
| 491 | .peripheral = { \ |
| 492 | .clk = DPORT_UART##idx##_CLK_EN, \ |
| 493 | .rst = DPORT_UART##idx##_RST, \ |
| 494 | }, \ |
| 495 | \ |
| 496 | .signals = { \ |
| 497 | .tx_out = U##idx##TXD_OUT_IDX, \ |
| 498 | .rx_in = U##idx##RXD_IN_IDX, \ |
| 499 | .rts_out = U##idx##RTS_OUT_IDX, \ |
| 500 | .cts_in = U##idx##CTS_IN_IDX, \ |
| 501 | }, \ |
| 502 | \ |
| 503 | .pins = { \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 504 | .tx = DT_INST_PROP(idx, tx_pin), \ |
| 505 | .rx = DT_INST_PROP(idx, rx_pin), \ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 506 | IF_ENABLED( \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 507 | DT_INST_PROP(idx, hw_flow_control), \ |
| 508 | (.rts = DT_INST_PROP(idx, rts_pin), \ |
| 509 | .cts = DT_INST_PROP(idx, cts_pin), \ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 510 | )) \ |
| 511 | }, \ |
| 512 | \ |
| 513 | .irq = { \ |
| 514 | .source = ETS_UART##idx##_INTR_SOURCE, \ |
Kumar Gala | b99ee32 | 2020-03-24 19:37:52 -0500 | [diff] [blame] | 515 | .line = INST_##idx##_ESPRESSIF_ESP32_UART_IRQ_0, \ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 516 | } \ |
| 517 | }; \ |
| 518 | \ |
| 519 | static struct uart_esp32_data uart_esp32_data_##idx = { \ |
| 520 | .uart_config = { \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 521 | .baudrate = DT_INST_PROP(idx, current_speed),\ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 522 | .parity = UART_CFG_PARITY_NONE, \ |
| 523 | .stop_bits = UART_CFG_STOP_BITS_1, \ |
| 524 | .data_bits = UART_CFG_DATA_BITS_8, \ |
| 525 | .flow_ctrl = IS_ENABLED( \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 526 | DT_INST_PROP(idx, hw_flow_control)) ?\ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 527 | UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE \ |
| 528 | } \ |
| 529 | }; \ |
| 530 | \ |
| 531 | DEVICE_AND_API_INIT(uart_esp32_##idx, \ |
Kumar Gala | 8ea8925 | 2020-03-24 16:00:26 -0500 | [diff] [blame] | 532 | DT_INST_LABEL(idx), \ |
Krzysztof Chruscinski | d716e3a | 2019-12-17 09:59:57 +0100 | [diff] [blame] | 533 | uart_esp32_init, \ |
| 534 | &uart_esp32_data_##idx, \ |
| 535 | &uart_esp32_cfg_port_##idx, \ |
| 536 | PRE_KERNEL_1, \ |
| 537 | CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 538 | &uart_esp32_api); \ |
| 539 | \ |
Mohamed ElShahawi | 2d035c4 | 2019-08-21 23:54:00 +0200 | [diff] [blame] | 540 | ESP32_UART_IRQ_HANDLER(idx) |
| 541 | |
Martí Bolívar | 87e1743 | 2020-05-05 16:06:32 -0700 | [diff] [blame^] | 542 | DT_INST_FOREACH(ESP32_UART_INIT) |