1 /*
2 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include "esp_attr.h"
10 #include "sdkconfig.h"
11 #include "hal/uart_ll.h"
12 #include "hal/efuse_hal.h"
13 #include "esp_rom_caps.h"
14 #include "rom/uart.h"
15
16 #if CONFIG_IDF_TARGET_ESP32
17 /**
18 * The function defined in ROM code has a bug, so we re-implement it here.
19 */
esp_rom_uart_tx_wait_idle(uint8_t uart_no)20 IRAM_ATTR void esp_rom_uart_tx_wait_idle(uint8_t uart_no)
21 {
22 while (!uart_ll_is_tx_idle(UART_LL_GET_HW(uart_no))) {};
23 }
24 #endif
25
esp_rom_uart_set_clock_baudrate(uint8_t uart_no,uint32_t clock_hz,uint32_t baud_rate)26 IRAM_ATTR void esp_rom_uart_set_clock_baudrate(uint8_t uart_no, uint32_t clock_hz, uint32_t baud_rate)
27 {
28 uart_ll_set_baudrate(UART_LL_GET_HW(uart_no), baud_rate, clock_hz);
29 }
30
31 #if CONFIG_IDF_TARGET_ESP32C3
32 /**
33 * The ESP32-C3 ROM has released three versions, ECO7 (v1.1), ECO3, and
34 * the version before ECO3 (include ECO0 ECO1 ECO2).
35 * These three versions of the ROM code do not list uart_tx_switch wrap
36 * function in the ROM interface, so here use the uart_tx_switch direct
37 * address instead.
38 */
esp_rom_uart_set_as_console(uint8_t uart_no)39 IRAM_ATTR void esp_rom_uart_set_as_console(uint8_t uart_no)
40 {
41 typedef void (*rom_func_t)(uint8_t);
42 rom_func_t uart_tx_switch = NULL;
43
44 if (efuse_hal_chip_revision() < 3) {
45 uart_tx_switch = (rom_func_t)0x4004b8ca;
46 } else if (efuse_hal_chip_revision() >= 101) {
47 uart_tx_switch = (rom_func_t)0x40001c44;
48 } else {
49 uart_tx_switch = (rom_func_t)0x4004c166;
50 }
51 uart_tx_switch(uart_no);
52 }
53 #endif
54
55 #if !ESP_ROM_HAS_UART_BUF_SWITCH
esp_rom_uart_switch_buffer(uint8_t uart_no)56 IRAM_ATTR void esp_rom_uart_switch_buffer(uint8_t uart_no)
57 {
58 UartDevice *uart = GetUartDevice();
59 uart->buff_uart_no = uart_no;
60 }
61 #endif // !ESP_ROM_HAS_UART_BUF_SWITCH
62