嵌入式 Kernel中添加自定义定时器完成循环需要做的事情

首先我先解释一个变量jiffies

jiffies是1/HZ秒 !!!

1、首先我们在内核中添加自己的定时器代码:

/*========================================kj timer begin=====================================*/

#define GPIO_LIGHT_DAT  (IO_ADDRESS(0x201A0000 + 0x03FC))
#define GPIO_LIGHT_DIR  (IO_ADDRESS(0x201A0000 + 0x0400))
#define GPIO_LIGHT_MUX  (IO_ADDRESS(0x200F0000 + 0x009C))


#define GPIO_1_1_DAT    (IO_ADDRESS(0x201503FC))//red
#define GPIO_1_1_DIR    (IO_ADDRESS(0x20150400))
#define GPIO_1_1_MUX    (IO_ADDRESS(0x200F0004))

#define GPIO_6_0_DAT    (IO_ADDRESS(0x201A03FC))//blue
#define GPIO_6_0_DIR     (IO_ADDRESS(0x201A0400))
#define GPIO_6_0_MUX     (IO_ADDRESS(0x200F0080))


/*alter by kj*/
/*
Author : kj
Time : 2014-04-19
Function :
control gpio led
Instruction :
1 - red on
2 - red off
3 - blue on
4 - blue off
5 - light on
6 - light off

writel(0x2, REG_BASE_SCTL+REG_SC_SYSRES);
 */
static void joseph_set_gpio_hi3518c(int led_status)
{
 switch(led_status)
 {
  case 1:
   {
    printk("\nThe uboot set red led on and blue led off begin alter by kj !!!\n"); 
    writel(0x00000002,GPIO_1_1_DIR);
    writel(0x00000000,GPIO_1_1_MUX);
    writel(0x00000002,GPIO_1_1_DAT);// 1 - on ;0 - off

    writel(0x00000001,GPIO_6_0_DIR);
    writel(0x00000000,GPIO_6_0_MUX);
    writel(0x00000000,GPIO_6_0_DAT);// blue off

    break;     
   }
  case 2:
   {
    printk("\nThe uboot  red off light begin alter by kj !!!\n"); 
    writel(0x00000002,GPIO_1_1_DIR);
    writel(0x00000000,GPIO_1_1_MUX);
    writel(0x00000000,GPIO_1_1_DAT);// red off

    break;     
   }
  case 3:
   {

    printk("\nThe uboot set bule on begin alter by kj !!!\n"); 
    writel(0x00000001,GPIO_6_0_DIR);
    writel(0x00000000,GPIO_6_0_MUX);
    writel(0x00000001,GPIO_6_0_DAT);//bule on
    break;     
   }
  case 4:
   {

    printk("\nThe uboot set blue off begin alter by kj !!!\n"); 
    writel(0x00000001,GPIO_6_0_DIR);
    writel(0x00000000,GPIO_6_0_MUX);
    writel(0x00000000,GPIO_6_0_DAT);//blue off
    break;     
   }
  case 5:
   {
    printk("\nThe uboot set light on begin alter by kj !!!\n"); 
    writel(0x00000080,GPIO_LIGHT_DIR);
    writel(0x00000000,GPIO_LIGHT_MUX);
    writel(0x00000000,GPIO_LIGHT_DAT);//light on
 
    break;     
   }
  case 6:
   {

    printk("\nThe uboot set light off begin alter by kj !!!\n"); 
    writel(0x00000080,GPIO_LIGHT_DIR);
    writel(0x00000001,GPIO_LIGHT_MUX);
    writel(0x00000000,GPIO_LIGHT_DAT);//light off
    
    writel(0x00000001,GPIO_6_0_DIR);
    writel(0x00000000,GPIO_6_0_MUX);
    writel(0x00000000,GPIO_6_0_DAT);//blue off

    writel(0x00000002,GPIO_1_1_DIR);
    writel(0x00000000,GPIO_1_1_MUX);
    writel(0x00000002,GPIO_1_1_DAT);// 1 - on ;0 - off

    break;     
   }
  default:
   break;
 }

 

}

struct timer_list kj_timer_on;

struct timer_list kj_timer_off;

static void joseph_add_on_timer(void);
static void joseph_add_off_timer(void);
static void joseph_led_on_timer(unsigned long data);
static void joseph_led_off_timer(unsigned long data);
static void joseph_del_timer(void);
static void joseph_init_timer(void);


EXPORT_SYMBOL(joseph_add_on_timer);
EXPORT_SYMBOL(joseph_add_off_timer);
EXPORT_SYMBOL(joseph_del_timer);
EXPORT_SYMBOL(joseph_led_on_timer);
EXPORT_SYMBOL(joseph_led_off_timer);
EXPORT_SYMBOL(joseph_init_timer);

#ifdef _DE_BUG_JOSEPH_DUBLE_TIME

#else

static void joseph_init_timer(void)
{
 init_timer(&kj_timer_on);
 kj_timer_on.expires = jiffies + 10;
 kj_timer_on.data = 1;
 kj_timer_on.function = joseph_led_on_timer;
 add_timer(&kj_timer_on);


 init_timer(&kj_timer_off);
 kj_timer_off.expires = jiffies + 10;
 kj_timer_off.data = 1;
 kj_timer_off.function = joseph_led_off_timer;
 
}


static void joseph_add_on_timer(void)
{

 add_timer(&kj_timer_on);
}

static void joseph_add_off_timer(void)
{

 add_timer(&kj_timer_off);
}


static void joseph_led_on_timer(unsigned long data)
{
 //  kj add function
 printk(KERN_INFO"\n joseph led on by kj_timer_on ...\n"); 

 joseph_add_off_timer();


}

static void joseph_led_off_timer(unsigned long data)
{
 // kj add function
    printk(KERN_INFO"\n joseph led off by kj_timer_off ...\n"); 

 joseph_add_on_timer();

}

static void joseph_del_timer(void)
{
 del_timer(&kj_timer_off);
 del_timer(&kj_timer_on);
    printk(KERN_INFO"\n\n joseph kj_timer_off and kj_timer_on by delete ...\n\n"); 
}

#endif
/*=========================================kj timer end====================================*/
 

2、我们需要找到在内核使用地方开始增加自己的定时器时钟:
Location:./init/main.c start_kernel 接口中对时钟初始化之后就可以了:

 init_timers();
 hrtimers_init();
 softirq_init();
 timekeeping_init();
 time_init();
 profile_init();
 call_function_init();
 if (!irqs_disabled())
  printk(KERN_CRIT "start_kernel(): bug: interrupts were "
    "enabled early\n");
 early_boot_irqs_disabled = false;
 local_irq_enable();

 /*add a timer by kj, the control led or wdg*/
 joseph_add_on_timer();


3、在kernel进入文件系统的之前撤出时钟就可以了:

Location:./init/main.c init_post接口中:

 /*
  * We try each of these until one succeeds.
  *
  * The Bourne shell can be used instead of init if we are
  * trying to recover a really broken machine.
  */
 if (execute_command) {
  run_init_process(execute_command);
  printk(KERN_WARNING "Failed to execute %s.  Attempting "
    "defaults...\n", execute_command);
 }

 /*delete timer by kj ,stop control led or wdg*/
 joseph_del_timer();

 run_init_process("/sbin/init");
 run_init_process("/etc/init");
 run_init_process("/bin/init");
 run_init_process("/bin/sh");

 

到此结束整个定时器在kernel的添加和撤出。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值