|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 OR MIT |
5 | 5 | */ |
6 | 6 |
|
| 7 | + |
7 | 8 | #include <stdint.h> |
| 9 | +#include <stddef.h> |
8 | 10 |
|
9 | 11 | #include <rtc_clk.h> |
| 12 | +#include <reg_base.h> |
10 | 13 |
|
11 | 14 | // These functions are defined in the ROM |
12 | 15 | 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 | +} |
14 | 60 |
|
15 | 61 | void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate) |
16 | 62 | { |
| 63 | + (void)baudrate; |
17 | 64 | 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); |
19 | 69 | } |
0 commit comments