Skip to content

Commit 200981c

Browse files
committed
uart: fix esp32h2, esp32c6, and esp32 initialization for the early stages
1 parent e8ff7a0 commit 200981c

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/esp32/src/uart.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,66 @@
44
* SPDX-License-Identifier: Apache-2.0 OR MIT
55
*/
66

7+
78
#include <stdint.h>
9+
#include <stddef.h>
810

911
#include <rtc_clk.h>
12+
#include <reg_base.h>
1013

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

1561
void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate)
1662
{
63+
(void)baudrate;
1764
uartAttach();
18-
uart_div_modify(uart_num, (stub_lib_rtc_clk_apb_freq_get() << 4) / baudrate);
65+
Uart_Init(uart_num, ets_get_detected_xtal_freq_patch());
66+
67+
ets_install_putc1(NULL);
68+
ets_install_putc2(NULL);
1969
}

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)