握手的时候,会监听主机是否发来同步消息(UART或者USB)
#define UART_SYNC_TIME (150) /* in ms */
// 150 ms 内没有反馈,则表示握手失败
// 接着往下面跑: load uboot / kernel
static bool uart_listen(struct bldr_comport *comport, uint8 *data, uint32 size, int retry, uint32 tmo_ms) { u8 c = 0; int rx_cnt = 0; int tmo_en = (tmo_ms) ? 1 : 0; ulong start_time; for (; retry > 0; retry--) { start_time = get_timer(0); while(1) { if (tmo_en && (get_timer(start_time) > tmo_ms)) break; /* kick watchdog to avoid cpu reset */ if (!tmo_en) platform_wdt_kick(); GetUARTBytes(&c, 1, 10); if (c != 0) { *data++ = (uint8)c; rx_cnt++; } if (rx_cnt == size) return TRUE; } } return FALSE; }
其中 get_timer 就是获得定时器转换成MS的整数:
ulong get_timer (ulong base) { ulong current_timestamp = 0; ulong temp = 0; current_timestamp = get_timer_masked (); if (current_timestamp >= base) { /* timestamp normal */ return (current_timestamp - base); } /* timestamp overflow */ //print("return = 0x%x\n",MAX_TIMESTAMP_MS - ( base - current_timestamp )); temp = base - current_timestamp; return (MAX_TIMESTAMP_MS - temp); }
// 得到定时器的值,这里我们用的是定时器4
static ulong get_timer_masked (void) { volatile U32 now; now = gpt4_tick2time_ms (*GPT4_DAT); if (now >= lastinc) { timestamp = timestamp + now - lastinc; /* normal */ } else { timestamp = timestamp + MAX_REG_MS - lastinc + now; /* overflow */ } lastinc = now;
gpt4_tick2time_ms (*GPT4_DAT);
由定时器tick数,转换成ms数
U32 gpt4_tick2time_ms (U32 tick) { return ((tick + (GPT4_1MS_TICK - 1)) / GPT4_1MS_TICK); }
其中: #define GPT4_1MS_TICK ((U32)13000) // 1000000 / 76.92ns = 13000.520
表示这个时候的晶振是:13M.