Skip to content

Commit 20728e0

Browse files
committed
uart: fix esp32h2, esp32c6, and esp32 initialization for the early stages
1 parent b983e0c commit 20728e0

File tree

9 files changed

+78
-31
lines changed

9 files changed

+78
-31
lines changed

src/esp32/src/uart.c

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,63 @@
55
*/
66

77
#include <stdint.h>
8+
#include <stddef.h>
89

910
#include <rtc_clk.h>
11+
#include <reg_base.h>
1012

1113
// These functions are defined in the ROM
1214
extern void uartAttach(void);
13-
extern void uart_div_modify(uint8_t uart_no, uint32_t DivLatchValue);
15+
void Uart_Init(uint8_t uart_no, uint32_t clock);
16+
extern void ets_install_putc1(void (*p)(char c));
17+
extern void ets_install_putc2(void (*p)(char c));
18+
extern uint32_t ets_get_detected_xtal_freq(void);
19+
20+
#define RTC_STORE5 (DR_REG_RTCCNTL_BASE + 0xb4)
21+
22+
#define REG_WRITE(_r, _v) (*(volatile uint32_t *)(_r)) = (_v)
23+
#define REG_READ(_r) (*(volatile uint32_t *)(_r))
24+
25+
/*
26+
* The symbol 'ets_get_detected_xtal_freq_patch' is defined as:
27+
* PROVIDE ( ets_get_detected_xtal_freq_patch = 40065c18 );
28+
*
29+
* This symbol exists in ROM revision 3 but is absent in earlier revisions.
30+
* It is not expected to have a significant impact on the stub size.
31+
*/
32+
static uint32_t ets_get_detected_xtal_freq_patch()
33+
{
34+
// Original detection function, reads from RTC_STORE5
35+
// or CK8 detected frequency otherwise
36+
uint32_t clock = ets_get_detected_xtal_freq();
37+
38+
// Quantize the detected clock to either 40MHz or 26MHz
39+
// mid-point is 30.5MHz based on data from ATE team about
40+
// 8M clock error rates
41+
if (clock < 30500 * 1000) {
42+
clock = 26 * 1000 * 1000;
43+
} else {
44+
clock = 40 * 1000 * 1000;
45+
}
46+
47+
const uint32_t rtc_store5 = REG_READ(RTC_STORE5);
48+
const uint32_t high = rtc_store5 >> 16;
49+
const uint32_t low = rtc_store5 & 0xffff;
50+
if (high == low && high != 0 && high != 0xffff) {
51+
clock = high << 12;
52+
} else {
53+
REG_WRITE(RTC_STORE5, (clock >> 12) | ((clock >> 12) << 16));
54+
}
55+
56+
return clock;
57+
}
1458

1559
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
1660
{
61+
(void)baudrate;
1762
uartAttach();
18-
uart_div_modify(uart_num, (stub_lib_rtc_clk_apb_freq_get() << 4) / baudrate);
63+
Uart_Init(uart_num, ets_get_detected_xtal_freq_patch());
64+
65+
ets_install_putc1(NULL);
66+
ets_install_putc2(NULL);
1967
}

src/esp32c2/src/uart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ extern void Uart_Init(uint8_t uart_no, uint32_t clock);
1414
extern uint32_t ets_get_apb_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
1616

17-
#define UART_CLK_FREQ_ROM (40 * 1000000)
17+
#define UART_CLK_FREQ_ROM (40 * 1000000)
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
extern bool g_uart_print;
2323
uartAttach(NULL);
24-
uint32_t clock = ets_get_apb_freq();
25-
ets_update_cpu_frequency(clock / 1000000);
24+
ets_update_cpu_frequency(ets_get_apb_freq() / 1000000);
2625
Uart_Init(uart_num, UART_CLK_FREQ_ROM);
2726
g_uart_print = true;
2827
}

src/esp32c3/src/uart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ extern void Uart_Init(uint8_t uart_no, uint32_t clock);
1414
extern uint32_t ets_get_apb_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
1616

17-
#define UART_CLK_FREQ_ROM (40 * 1000000)
17+
#define UART_CLK_FREQ_ROM (40 * 1000000)
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
extern bool g_uart_print;
2323
uartAttach(NULL);
24-
uint32_t clock = ets_get_apb_freq();
25-
ets_update_cpu_frequency(clock / 1000000);
24+
ets_update_cpu_frequency(ets_get_apb_freq() / 1000000);
2625
Uart_Init(uart_num, UART_CLK_FREQ_ROM);
2726
g_uart_print = true;
2827
}

src/esp32c5/src/uart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ extern void Uart_Init(uint8_t uart_no, uint32_t clock);
1414
extern uint32_t ets_clk_get_xtal_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
1616

17-
#define APB_CLK_FREQ (40 * 1000000)
17+
#define APB_CLK_FREQ (40 * 1000000)
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
uartAttach(NULL);
23-
uint32_t clock = ets_clk_get_xtal_freq();
24-
ets_update_cpu_frequency(clock / 1000000);
23+
ets_update_cpu_frequency(ets_clk_get_xtal_freq() / 1000000);
2524
Uart_Init(uart_num, APB_CLK_FREQ);
2625
}

src/esp32c6/src/uart.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010

1111
// These functions are defined in the ROM
1212
extern void uartAttach(void *rxBuffer);
13-
extern void Uart_Init(uint8_t uart_no, uint32_t clock);
13+
extern void Uart_Init(uint8_t uart_no);
1414
extern uint32_t ets_clk_get_xtal_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
16-
17-
#define APB_CLK_FREQ_ROM (40 * 1000000)
16+
extern void ets_install_putc1(void (*p)(char c));
17+
extern void ets_install_putc2(void (*p)(char c));
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
uartAttach(NULL);
2323
uint32_t clock = ets_clk_get_xtal_freq();
24-
ets_update_cpu_frequency(clock / 1000000);
25-
Uart_Init(uart_num, APB_CLK_FREQ_ROM);
24+
ets_update_cpu_frequency(ets_clk_get_xtal_freq() / 1000000);
25+
Uart_Init(uart_num);
26+
27+
ets_install_putc1(NULL);
28+
ets_install_putc2(NULL);
2629
}

src/esp32c61/src/uart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ extern void Uart_Init(uint8_t uart_no, uint32_t clock);
1414
extern uint32_t ets_clk_get_xtal_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
1616

17-
#define APB_CLK_FREQ (40 * 1000000)
17+
#define APB_CLK_FREQ (40 * 1000000)
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
uartAttach(NULL);
23-
uint32_t clock = ets_clk_get_xtal_freq();
24-
ets_update_cpu_frequency(clock / 1000000);
23+
ets_update_cpu_frequency(ets_clk_get_xtal_freq() / 1000000);
2524
Uart_Init(uart_num, APB_CLK_FREQ);
2625
}

src/esp32h2/src/uart.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010

1111
// These functions are defined in the ROM
1212
extern void uartAttach(void *rxBuffer);
13-
extern void Uart_Init(uint8_t uart_no, uint32_t clock);
13+
extern void Uart_Init(uint8_t uart_no);
1414
extern uint32_t ets_clk_get_xtal_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
16-
17-
#define APB_CLK_FREQ_ROM (32 * 1000000)
16+
extern void ets_install_putc1(void (*p)(char c));
17+
extern void ets_install_putc2(void (*p)(char c));
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
uartAttach(NULL);
23-
uint32_t clock = ets_clk_get_xtal_freq();
24-
ets_update_cpu_frequency(clock / 1000000);
25-
Uart_Init(uart_num, APB_CLK_FREQ_ROM);
23+
ets_update_cpu_frequency(ets_clk_get_xtal_freq() / 1000000);
24+
Uart_Init(uart_num);
25+
26+
ets_install_putc1(NULL);
27+
ets_install_putc2(NULL);
2628
}

src/esp32p4/src/uart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ extern void Uart_Init(uint8_t uart_no, uint32_t clock);
1414
extern uint32_t ets_clk_get_xtal_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
1616

17-
#define APB_CLK_FREQ_ROM (10 * 1000000)
17+
#define APB_CLK_FREQ_ROM (10 * 1000000)
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
uartAttach(NULL);
23-
uint32_t clock = ets_clk_get_xtal_freq();
24-
ets_update_cpu_frequency(clock / 1000000);
23+
ets_update_cpu_frequency(ets_clk_get_xtal_freq() / 1000000);
2524
Uart_Init(uart_num, APB_CLK_FREQ_ROM);
2625
}

src/esp32s3/src/uart.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ extern void Uart_Init(uint8_t uart_no, uint32_t clock);
1414
extern uint32_t ets_get_apb_freq(void);
1515
extern void ets_update_cpu_frequency(uint32_t ticks_per_us);
1616

17-
#define UART_CLK_FREQ_ROM (40 * 1000000)
17+
#define UART_CLK_FREQ_ROM (40 * 1000000)
1818

1919
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2020
{
2121
(void)baudrate;
2222
extern bool g_uart_print;
2323
uartAttach(NULL);
24-
uint32_t clock = ets_get_apb_freq();
25-
ets_update_cpu_frequency(clock / 1000000);
24+
ets_update_cpu_frequency(ets_get_apb_freq() / 1000000);
2625
Uart_Init(uart_num, UART_CLK_FREQ_ROM);
2726
g_uart_print = true;
2827
}

0 commit comments

Comments
 (0)