2410 IO端口驱动程序

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;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值