/* calibrate.c: default delay calibration
*
* Excised from init/main.c
* Copyright (C) 1991, 1992 Linus Torvalds
*/
#include <stdio.h>
#include "common.h"
unsigned long loops_per_jiffy = (1<<12);
unsigned long cpu_cnt = (2199023U*HZ)>>11;
//unsigned long lpj_fine;
unsigned long preset_lpj = 0;
#ifdef ARCH_HAS_READ_CURRENT_TIMER
/* This routine uses the read_current_timer() routine and gets the
* loops per jiffy directly, instead of guessing it using delay().
* Also, this code tries to handle non-maskable asynchronous events
* (like SMIs)
*/
#define DELAY_CALIBRATION_TICKS ((HZ < 100) ? 1 : (HZ/100))
#define MAX_DIRECT_CALIBRATION_RETRIES 5
static unsigned long calibrate_delay_direct(void)
{
unsigned long pre_start, start, post_start;
unsigned long pre_end, end, post_end;
unsigned long start_jiffies;
unsigned long timer_rate_min, timer_rate_max;
unsigned long good_timer_sum = 0;
unsigned long good_timer_count = 0;
int i;
if (read_current_timer(&pre_start) < 0 )
return 0;
/*
* A simple loop like
* while ( jiffies < start_jiffies+1)
* start = read_current_timer();
* will not do. As we don't really know whether jiffy switch
* happened first or timer_value was read first. And some asynchronous
* event can happen between these two events introducing errors in lpj.
*
* So, we do
* 1. pre_start <- When we are sure that jiffy switch hasn't happened
* 2. check jiffy switch
* 3. start <- timer value before or after jiffy switch
* 4. post_start <- When we are sure that jiffy switch has happened
*
* Note, we don't know anything about order of 2 and 3.
* Now, by looking at post_start and pre_start difference, we can
* check whether any asynchronous event happened or not
*/
for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++)
{
pre_start = 0;
read_current_timer(&start);
start_jiffies = jiffies;
while (jiffies <= (start_jiffies + 1))
{
pre_start = start;
read_current_timer(&start);
}
read_current_timer(&post_start);
pre_end = 0;
end = post_start;
while (jiffies <= (start_jiffies + 1 + DELAY_CALIBRATION_TICKS))
{
pre_end = end;
read_current_timer(&end);
}
read_current_timer(&post_end);
timer_rate_max = (post_end - pre_start) /
DELAY_CALIBRATION_TICKS;
timer_rate_min = (pre_end - post_start) /
DELAY_CALIBRATION_TICKS;
/*
* If the upper limit and lower limit of the timer_rate is
* >= 12.5% apart, redo calibration.
*/
if (pre_start != 0 && pre_end != 0 &&
(timer_rate_max - timer_rate_min) < (timer_rate_max >> 3))
{
good_timer_count++;
good_timer_sum += timer_rate_max;
}
}
if (good_timer_count)
return (good_timer_sum/good_timer_count);
printk("calibrate_delay_direct() failed to get a good estimate for loops_per_jiffy.\nProbably due to long platform interrupts. Consider using \"lpj=\" boot option.\n");
return 0;
}
#else
static unsigned long calibrate_delay_direct(void) {return 0;}
#endif
/*
* This is the number of bits of precision for the loops_per_jiffy. Each
* bit takes on average 1.5/HZ seconds. This (like the original) is a little
* better than 1%
* For the boot cpu we can skip the delay calibration and assign it a value
* calculated based on the timer frequency.
* For the rest of the CPUs we cannot assume that the timer frequency is same as
* the cpu frequency, hence do the calibration for those.
*/
#define LPS_PREC 8
extern void __delay(unsigned long cnt);
static int calibrate_delay(void)
{
unsigned long ticks, loopbit;
int lps_precision = LPS_PREC;
if (preset_lpj)
{
loops_per_jiffy = preset_lpj;
printk("Calibrating delay loop (skipped) preset value.. ");
}
else if ((loops_per_jiffy = calibrate_delay_direct()) != 0)
{
printk("Calibrating d5utine.. %ld\r\n", loops_per_jiffy);
}
else
{
loops_per_jiffy = (1<<12);
printk("Calibrating delay loop... ");
while ((loops_per_jiffy <<= 1) != 0)
{
/* wait for "start of" clock tick */
ticks = jiffies;
while (ticks == jiffies)
/* nothing */;
/* Go .. */
ticks = jiffies;
__delay(loops_per_jiffy);
ticks = jiffies - ticks;
if (ticks)
break;
}
/*
* Do a binary approximation to get loops_per_jiffy set to
* equal one clock (up to lps_precision bits)
*/
loops_per_jiffy >>= 1;
loopbit = loops_per_jiffy;
while (lps_precision-- && (loopbit >>= 1))
{
loops_per_jiffy |= loopbit;
ticks = jiffies;
while (ticks == jiffies)
/* nothing */;
ticks = jiffies;
__delay(loops_per_jiffy);
if (jiffies != ticks) /* longer than 1 tick */
loops_per_jiffy &= ~loopbit;
}
}
printk( "%lu.%02lu BogoMIPS (lpj=%lu)\n",
loops_per_jiffy/(500000/HZ),
(loops_per_jiffy/(5000/HZ)) % 100,
loops_per_jiffy
);
return 0;
}
subsys_initcall(calibrate_delay)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1、个人原创,代码已经在兆讯系列的芯片上验证OK; 2、验证方法,整合代码到系统中,控制GPIO口输出方波,用示波器测量方波的周期,看是否与预期一致; 3、此代码可轻松移植到STM32、BCM58101、K21、MH19XX等系列单片机上,实现准确的延时; 4、代码的实现,参照Linux中udelay()、mdelay()的实现方法,用Linux思想去开发单片机,具有极强的参考意义;
资源推荐
资源详情
资源评论
收起资源包目录
udelay_mdelay.rar (3个子文件)
udelay_mdelay
common.h 3KB
delay-mdk.s 954B
calibrate.c 5KB
共 3 条
- 1
资源评论
hudaolei040414
- 粉丝: 28
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功