Linux LED light Driver

Source: Internet
Author: User

1. Drivers

Led.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <asm/uaccess.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <mach/hardware.h>
#include <linux/miscdevice.h>

#define LIGHT_ON 0x01
#define Light_off 0X02

#define GPBCON S3c2410_gpbcon
#define GPBDAT S3c2410_gpbdat
#define GPBUP S3c2410_gpbup

#define LED_ON_1 ~ (1<<5)
#define LED_ON_2 ~ (1<<6)
#define LED_ON_3 ~ (1<<7)
#define LED_ON_4 ~ (1<<8)

#define DEVICE_NAME "Linux_led"

Module_author ("Xiaoheng");

void light_on (void)
{
Writel (led_on_1, Gpbdat);
Mdelay (300);
PRINTK ("one");

Writel (led_on_2, Gpbdat);
Mdelay (300);
PRINTK ("both");

Writel (Led_on_3, Gpbdat);
Mdelay (300);
PRINTK ("three");

Writel (Led_on_4, Gpbdat);
Mdelay (300);
PRINTK ("four");

Writel (0, Gpbdat);
Mdelay (300);
PRINTK ("All on\n");
}

void Light_off (void)
{
Writel (0xffff,gpbdat);
}

int Light_open (struct inode* inode, struct file* filp)
{
PRINTK ("myled open now!");
Writel ((1<<10) | (1<<12) | (1<<14) | (1<<16), Gpbcon);
Writel (0, Gpbdat);
return 0;
}

int light_release (struct inode* inode, struct file* filp)
{
PRINTK ("myled close now!");
return 0;
}

int Light_ioctl (struct inode* inode, struct file* filp, unsigned int cmd,unsigned long T)
{
Switch (CMD)
{
Case LIGHT_ON:
Light_on ();
Break

Case Light_off:
Light_off ();
Break
Default
Return-enotty;
}
return 0;
}

struct File_operations light_fops = {
. Owner = This_module,
. IOCTL = Light_ioctl,
. open = Light_open,
. Release = Light_release,
};

static struct Miscdevice misc = {
. minor = Misc_dynamic_minor,
. Name = Device_name,
. FoPs = &light_fops,
};

int light_init (void)
{
int ret;
PRINTK ("My LED init now\n");

ret = Misc_register (&MISC);
PRINTK (device_name "initialized\n");

return ret;
}

void Light_cleanup (void)
{
PRINTK ("myled over\n");
Misc_deregister (&MISC);
}

Module_init (Light_init);
Module_exit (Light_cleanup);

2. Test procedures

Ledtest.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define LED_ON 0x01
#define Led_off 0x01

void Delay ()
{
unsigned int i, J;
for (i = 0; i < 0xf; i++)
for (j = 0; J < 0xFF; J + +)
;
}

int main (int argc, char **argv)
{
int FD;

FD = open ("/dev/linux_led", 0);

if (strcmp (argv[1], "0"))
{
while (1)
{
Delay ();
IOCTL (FD, led_on, 0);
printf ("1\n");
}
}
else if (strcmp (argv[1], "1"))
{
printf ("0");
IOCTL (FD, Led_off, 0);
}

return 0;

}

3.Makefile

obj-m:=led.o

kdir:=/home/xiaoheng/desktop/2.6.30.4/opt/embedsky/linux-2.6.30.4
All
Make-c $ (kdir) m=$ (shell pwd) modules
Clean
Make-c $ (kdir) m=$ (shell pwd) clean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.