TECHV-2410开发板,通过IO口 GPG8 与GPG9 两个口控制两个LED灯
驱动程序:
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <asm/hardware.h>
#include <asm/arch/regs-gpio.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/init.h>
#include <asm-arm/arch-s3c2410/irqs.h>
#include <asm-arm/mach/irq.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/delay.h>
#include <asm/system.h>
#include <linux/poll.h>
#include <asm/arch/io.h>
MODULE_LICENSE("Dual BSD/GPL");
unsigned int led_dev;
int flag;
static struct class *myled_class;
void delay(int time)
{
for(;time>0;time--) ;
}
void led_cycle(void)
{
if(flag==0){
s3c2410_gpio_setpin(S3C2410_GPG8,1);
s3c2410_gpio_setpin(S3C2410_GPG9,0);
flag = 1;
}else{
s3c2410_gpio_setpin(S3C2410_GPG8,0);
s3c2410_gpio_setpin(S3C2410_GPG9,1);
flag = 0;}
}
void led_blink(void)
{
if(flag==0){
s3c2410_gpio_setpin(S3C2410_GPG8,1);
s3c2410_gpio_setpin(S3C2410_GPG9,1);
flag = 1;
}else{
s3c2410_gpio_setpin(S3C2410_GPG8,0);
s3c2410_gpio_setpin(S3C2410_GPG9,0);
flag = 0;}
}
static int myled_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
int i;
unsigned int time=0;
switch(arg)
{
case 1:
time = 100000;
break;
case 2:
time = 200000;
break;
}
switch(cmd)
{
case 'l':
{
led_cycle();
delay(time);
}
break;
case 'b':
{
led_blink();
delay(time);
}
break;
default: break;
}
return 0;
}
static struct file_operations myled_fops={
.owner = THIS_MODULE,
.ioctl = myled_ioctl,
};
struct myled_dev_t /* 璁惧缁撴瀯浣?*/
{
struct cdev cdev;
} myled_dev;
static int __init myled_init(void) /* 璁惧椹卞姩妯″潡鍔犺浇鍑芥暟 */
{
int ret;
printk("sdfsga/n");
cdev_init(&myled_dev.cdev, &myled_fops); /* 鍒濆鍖?cdev */
myled_dev.cdev.owner = THIS_MODULE;
ret=alloc_chrdev_region(&led_dev,0,1,"myled");
if (ret<0)
{
printk(KERN_INFO"[FALLED: Cannot register myled_driver!]/n");
return ret;
}
else
printk("[OK]/n");
ret = cdev_add(&myled_dev.cdev, led_dev, 1); /* 娉ㄥ唽璁惧 */
s3c2410_gpio_cfgpin(S3C2410_GPG8,S3C2410_GPG8_OUTP);
s3c2410_gpio_cfgpin(S3C2410_GPG9,S3C2410_GPG9_OUTP);
s3c2410_gpio_pullup(S3C2410_GPG8,0);
s3c2410_gpio_pullup(S3C2410_GPG9,0);
myled_class=class_create(THIS_MODULE,"myled");
class_device_create(myled_class,NULL,led_dev,NULL,"myled");
printk("programs start/n");
return 0;
}
static void __exit myled_exit(void) /*璁惧椹卞姩妯″潡鍗歌浇鍑芥暟*/
{
unregister_chrdev_region(&led_dev,1); /* 閲婃斁鍗犵敤鐨勮澶囧彿 */
cdev_del(&myled_dev.cdev); /* 娉ㄩ攢璁惧 */
}
module_init(myled_init);
module_exit(myled_exit);
应用程序:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
void delay(int time)
{
for(;time>0;time--);
}
int main(int argc, char **argv)
{
int fb;
char dly_time = 0,direction = 0;
char enter_c = 0,cmd;
fb=open("/dev/myled",O_RDWR);
if(fb<0)
{
printf("open failed/n");
}
start:
printf("Please select the delay time !(1 or 2 then Enter)/n");
dly_time = getchar();
enter_c = getchar();
printf("dly_time = %c/n",dly_time);
printf("Please select the direction !(l or b then Enter)/n");
direction = getchar();
enter_c = getchar();
delay(1000);
printf("please enter any but q,when enter q ,quit the select/n");
do
{
cmd = getchar();
enter_c = getchar();
ioctl(fb,direction,dly_time);
}while('q'!= cmd);
printf("please enter a char but q again,when enter q ,exit the application/n");
delay(10000);
cmd = getchar();
delay(10000);
enter_c = getchar();
if ('q' != cmd )
goto start;
printf("quit the led/n");
return 0;
}