Skip to content

Commit 22d6f13

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

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

src/esp32/src/uart.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,64 @@
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+
/* The symbol
26+
27+
PROVIDE ( ets_get_detected_xtal_freq_patch = 40065c18 );
28+
29+
exists in the ROM rev.3, but not in earlier revisions.
30+
I don't think it affects the stub size much.
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+
do {
48+
uint32_t high, low;
49+
high = REG_READ(RTC_STORE5) >> 16;
50+
low = REG_READ(RTC_STORE5) & 0xffff;
51+
if (high == low && high != 0 && high != 0xffff) {
52+
clock = high << 12;
53+
} else {
54+
REG_WRITE(RTC_STORE5, (clock >> 12) | ((clock >> 12) << 16));
55+
}
56+
} while (0);
57+
return clock;
58+
}
1459

1560
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
1661
{
62+
(void)baudrate;
1763
uartAttach();
18-
uart_div_modify(uart_num, (stub_lib_rtc_clk_apb_freq_get() << 4) / baudrate);
64+
Uart_Init(uart_num, ets_get_detected_xtal_freq_patch());
65+
66+
ets_install_putc1(NULL);
67+
ets_install_putc2(NULL);
1968
}

src/esp32c6/src/uart.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
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);
1616

17-
#define APB_CLK_FREQ_ROM (40 * 1000000)
17+
extern void ets_install_putc1(void (*p)(char c));
18+
extern void ets_install_putc2(void (*p)(char c));
1819

1920
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2021
{
2122
(void)baudrate;
2223
uartAttach(NULL);
2324
uint32_t clock = ets_clk_get_xtal_freq();
2425
ets_update_cpu_frequency(clock / 1000000);
25-
Uart_Init(uart_num, APB_CLK_FREQ_ROM);
26+
Uart_Init(uart_num);
27+
28+
ets_install_putc1(NULL);
29+
ets_install_putc2(NULL);
2630
}

src/esp32h2/src/uart.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
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);
1616

17-
#define APB_CLK_FREQ_ROM (32 * 1000000)
17+
extern void ets_install_putc1(void (*p)(char c));
18+
extern void ets_install_putc2(void (*p)(char c));
1819

1920
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
2021
{
2122
(void)baudrate;
2223
uartAttach(NULL);
2324
uint32_t clock = ets_clk_get_xtal_freq();
2425
ets_update_cpu_frequency(clock / 1000000);
25-
Uart_Init(uart_num, APB_CLK_FREQ_ROM);
26+
Uart_Init(uart_num);
27+
28+
ets_install_putc1(NULL);
29+
ets_install_putc2(NULL);
2630
}

0 commit comments

Comments
 (0)