|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | #include <stdint.h> |
| 8 | +#include <stddef.h> |
8 | 9 |
|
9 | 10 | #include <rtc_clk.h> |
| 11 | +#include <reg_base.h> |
10 | 12 |
|
11 | 13 | // These functions are defined in the ROM |
12 | 14 | 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 | +} |
14 | 58 |
|
15 | 59 | void stub_target_uart_init(uint8_t uart_num, uint32_t baudrate) |
16 | 60 | { |
| 61 | + (void)baudrate; |
17 | 62 | 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); |
19 | 67 | } |
0 commit comments