preloader定时器timer使用

握手的时候,会监听主机是否发来同步消息(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值