需要板子一起学习的可以这里购买(含资料):点击跳转
一、内核时钟
1、内核时钟
内核时钟(Kernel Clock),也称为系统时钟(System Clock)或滴答时钟(Tick Timer),是操作系统内核中用于时间管理的一个重要组件。它提供了精确的时间基准,用于调度、延时等各种系统功能。
S5p6818 + linux ---->操作系统的时钟频率: HZ =1000 具体的参考值是由官方提供。
│ Symbol: HZ_100 [=n]
│ Type : boolean
│ Prompt: 100 HZ
│ Defined at kernel/Kconfig.hz:19
│ Depends on: <choice>
│ Location:
│ -> System Type
│ -> Timer frequency (<choice> [=y])┌──────────── Timer frequency───────────┐
│ Use the arrow keys to navigate this window or press the hotkey of │
│ the item you wish to select followed by the <SPACE BAR>. Press │
│ <?> for additional information about this option. │
│ ┌────────────────────────────────────────────────────────────────┐ │
│ │ ( ) 100 HZ │ │
│ │ ( ) 250 HZ │ │
│ │ ( ) 300 HZ │ │
│ │ (X) 1000 HZ │ │
│ │ │ │
2、内核时钟频率与硬件平台有关
1) 找到linux内核源码中对S5P6818硬件初始化的源文件
kernel\arch\arm\mach-s5p6818\cpu.c
2) 找到机器宏
extern struct sys_timer nxp_cpu_sys_timer;
MACHINE_START(S5P6818, CFG_SYS_CPU_NAME) //S5P6818 4330
	.atag_offset	=  0x00000100,
	.fixup			=  cpu_fixup,
	.map_io			=  cpu_map_io,
	.init_irq		=  nxp_cpu_irq_init,
	.handle_irq 	=  gic_handle_irq,
	.timer			= &nxp_cpu_sys_timer,  //extern struct sys_timer nxp_cpu_sys_timer;
	.init_machine	=  cpu_init_machine,
#if defined CONFIG_CMA && defined CONFIG_ION
	.reserve        = cpu_mem_reserve,
#endif
MACHINE_END
struct sys_timer nxp_cpu_sys_timer = {
	.init	= timer_initialize, //关于时钟频率和定时器的入口
};
static void __init timer_initialize(void)
{
	pr_debug("%s\n", __func__);
	timer_source_init(CFG_TIMER_SYS_TICK_CH); //timer0  1MHZ
	timer_event_init(CFG_TIMER_EVT_TICK_CH); //timer1   1MHZ
	return;
}
/*------------------------------------------------------------------------------
 * 	Timer List (SYS = Source, EVT = Event, WDT = WatchDog)
 */
#define	CFG_TIMER_SYS_TICK_CH					0
#define	CFG_TIMER_EVT_TICK_CH					13、内核时钟频率
HZ:1000
时钟频率(HZ)对系统有哪些好处或者缺点?
         好处:HZ频率越高,则系统的计时就越精确,系统的实时性就越高
         缺点:系统处理的时钟频率越高,系统的担负也越重
二、关于HZ
1、HZ本身就是一个全局的常数
#ifdef __KERNEL__
# define HZ		CONFIG_HZ	/* Internal kernel timer frequency */
# define USER_HZ	100		/* User interfaces are in "ticks" */
# define CLOCKS_PER_SEC	(USER_HZ)	/* like times() */
#else
# define HZ		100
#endifCONFIG_HZ ---->是由配置内核时,设置的一个值    
  CONFIG_HZ ---->.config或者 GEC6818_defconfig -----> autoconf.h(该头文件是在编译内核时,自动根据配置生成的头文件)该常数可以任何位置直接使用,比如:printk("HZ = %d",HZ);
2、修改该HZ的值 ---->配置内核
│ Symbol: HZ_100 [=n]                                                                                                                                                          
│ Type  : boolean                                                                                                                                                             
│ Prompt: 100 HZ                                                                                                                                                               
│   Defined at kernel/Kconfig.hz:19                                                                                                                                            
│   Depends on: <choice>                                                                                                                                                       
│   Location:                                                                                                                                                                  
│     -> System Type                                                                                                                                                           
│       -> Timer frequency (<choice> [=y]) 三、jiffies
1、jiffies也是一个全局的常数。它是内核源码中的全局变量,记录了linux内核从启动到现在经过了多少个内核时间周期,1秒钟内,jiffies增加的HZ的次数。jiffies/HZ ---->linux系统启动到现在用了多少秒。
# define jiffies	raid6_jiffies()
static inline uint32_t raid6_jiffies(void)
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv.tv_sec*1000 + tv.tv_usec/1000; //时间的值
}//在内核源码中的应用:
unsigned long timeout = jiffies + 10*HZ/100;
/*
	jiffies -----> 表示当前时间
	10*HZ/100 ---> 表示时间间隔为多少秒  1/10s
	比如:5*HZ --->表示时间间隔为5秒
	timeout:表示时间间隔秒以后的时间,也就是超时时间
*/
if(jiffies > timeout){
	 //超时
}else{
	 //未超时
}四、linux内核动态定时器
linux内核动态定时器是依赖于内核时钟,周期是内核时钟的整数倍。动态定时器不是硬件定时器,跟硬件无关,利用内核动态定时器产生的时钟周期。
//[1] 定义动态定时器
static struct timer_list gec6818_timer;
void gec6818timer_function(unsigned long data)
{
	printk("jiffies = %ld\n",jiffies);
	printk("data = %ld\n",data);
	//[5] 修改时间
	mod_timer(&gec6818_timer,jiffies + 1*HZ);
}
//[2] 初始化动态定时器
init_timer(&gec6818_timer);
//[3]初始化成员
gec6818_timer.function = gec6818timer_function;//函数的地址:void (*function)(unsigned long);
gec6818_timer.expires = jiffies + 1*HZ; //设置超时时间
gec6818_timer.data = 10;
//[4] 把动态定时器加入到内核中,
add_timer(&gec6818_timer);
//[6] 把动态定时器从内核中删除
del_timer(&gec6818_timer);
觉得有帮助的话,打赏一下呗。。
 
           



















