Difference between revisions of "Hammer LED Driver Module"

From eLinux.org
Jump to: navigation, search
Line 1: Line 1:
 
this is an example kernel module that will blink the onboard LED connected to GPIO F0 ([[media:ledblink.tar.gz|source]])
 
this is an example kernel module that will blink the onboard LED connected to GPIO F0 ([[media:ledblink.tar.gz|source]])
  
 +
==Usage==
  
 +
<pre>
 +
#tar zxvf ledblink.tar.gz
 +
#cd ledblink
 +
#make KERNEL_SRC=/home/foo/bar/hammer/linux-2.6.22
 +
</pre>
 +
 +
this will generate the ledblink.ko file which can be transfer to your hammer and loaded using the insmod command
 +
<pre>
 +
#insmod ledblink.ko
 +
LED Blink Module Loaded
 +
Dec 31 17:10:05 Hammer user.info kernel: LED Blink Module Loaded
 +
</pre>
 +
 +
to remove the module use the rmmod command
 +
<pre>
 +
#rmmod ledblink.ko
 +
LED Blink Module Unloaded
 +
Dec 31 17:10:20 Hammer user.info kernel: LED Blink Module Unloaded
 +
</pre>
 +
 +
==Source Code==
 
<pre>
 
<pre>
 
/*   
 
/*   

Revision as of 15:31, 12 May 2008

this is an example kernel module that will blink the onboard LED connected to GPIO F0 (source)

Usage

#tar zxvf ledblink.tar.gz
#cd ledblink
#make KERNEL_SRC=/home/foo/bar/hammer/linux-2.6.22

this will generate the ledblink.ko file which can be transfer to your hammer and loaded using the insmod command

#insmod ledblink.ko
LED Blink Module Loaded
Dec 31 17:10:05 Hammer user.info kernel: LED Blink Module Loaded

to remove the module use the rmmod command

#rmmod ledblink.ko
LED Blink Module Unloaded
Dec 31 17:10:20 Hammer user.info kernel: LED Blink Module Unloaded

Source Code

/*  
 *  ledblink.c - basic led blinking kernel module
 *
 *	Copyright (c) 2008 TinCanTools
 *	David Anders <danders@amltd.com>
 */
#include <linux/module.h>	/* Needed by all modules */
#include <linux/kernel.h>	/* Needed for KERN_INFO */
#include <linux/timer.h>	/* Needed for kernel timer */
#include <asm/arch/regs-gpio.h>	/* Needed for GPIO defines */
#include <asm/io.h>		/* Needed for s3c2410 functions */


static int blinkinterval = HZ / 2;
static struct timer_list blink_timer;


static void led_blink(unsigned long dummy)
{

    /* the LED is active(on) when low(0) */
    
    if (s3c2410_gpio_getpin(S3C2410_GPF0) == 0)	/* check the current value of GPIO F0 */
	s3c2410_gpio_setpin(S3C2410_GPF0, 1);	/* turn LED off */

    else 
	s3c2410_gpio_setpin(S3C2410_GPF0, 0);	/* turn LED on */

    mod_timer (&blink_timer, jiffies + blinkinterval); /* restart the timer */

}

int init_module(void)
{

	s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPIO_OUTPUT);	/* set the GPIO line F0 to an output */
	s3c2410_gpio_setpin(S3C2410_GPF0, 1); /* make sure the LED starts in the off setting */

	printk(KERN_INFO "LED Blink Module Loaded\n");

	init_timer (&blink_timer);
	blink_timer.function = led_blink;
	mod_timer (&blink_timer, jiffies + blinkinterval);

	/* 
	 * A non 0 return means init_module failed; module can't be loaded. 
	 */
	return 0;
}

void cleanup_module(void)
{
	s3c2410_gpio_setpin(S3C2410_GPF0, 1);	/* make sure the LED is off when exiting */

	del_timer_sync(&blink_timer);		/* sync and delete the timer before exiting */
	printk(KERN_INFO "LED Blink Module Unloaded\n");
}

MODULE_LICENSE("GPL");