Hammer LED Driver Module

From eLinux.org
Revision as of 08:39, 12 May 2008 by Prpplague (talk | contribs)
Jump to: navigation, search

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>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#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");