Hammer LED Driver Module

From eLinux.org
Jump to: navigation, search

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


#tar zxvf ledblink.tar.gz
#cd ledblink

NOTE: If trying to make against kernel 2.6.29 change #include of arch/arm/regs-gpio to

#include <mach/regs-gpio.h>
#make KERNEL_SRC=/home/foo/bar/hammer/linux-2.6.22

NOTE: for more information about the contents and function of the
included Makefile please see the The Linux Kernel Module Programming Guide

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

NOTE: this source makes reference to the HZ kernel value and also to "jiffies",
for more information on these see the Kernel_Timer_Systems documentation

 *  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
 * 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 */

	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");