itop4412 驱动笔记4(led驱动和应用程序)

1.LED驱动

只需要在前面的基础上

1在probe函数中加入初始化
2ioctl中加入控制即可

#include 
#include 


/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include 


#include 

#include 


#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#include 
#include 
#include 
//#include "gps.h"
#include 


#define DRIVER_NAME "hello"  //必须和设备注册的一致
#define MISC_NAME  "hello_misc"//在/dev中显示的名字/


MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

 long hello_unlocked_ioctl(struct file *pfd, unsigned int cmd, unsigned long arg)
 {
printk(KERN_EMERG "fs unlocked_ioctl call file:%s cmd:%d  arg:%d \n",pfd,cmd,arg);

gpio_set_value( (cmd==0) ? EXYNOS4_GPL2(0):EXYNOS4_GPK1(1),arg ? 1:0);



 return 0;
 }
 
 int hello_release(struct inode *nd, struct file *pfd)
 {
 printk(KERN_EMERG "fs release call nd:%s file: \n",nd,pfd);
 return 0;    
      
 }
  int hello_open(struct inode *nd, struct file *pfd)
  {
      
       printk(KERN_EMERG "fs open call nd:%s file: \n",nd,pfd);
 return 0;    
  }
 
struct file_operations hello_fs={
.owner=THIS_MODULE,
.open=hello_open,
.release=hello_release,
.unlocked_ioctl=hello_unlocked_ioctl,
};

struct miscdevice hello_misc={
        .minor=MISC_DYNAMIC_MINOR,
        .name=MISC_NAME,
        .fops=&hello_fs,
  
};

int hello_probe(struct platform_device *pdev)
{
        int ret = gpio_request(EXYNOS4_GPL2(0), "LED");
        if (ret) {
            printk("%s: request GPIO %d for LED failed, ret = %d\n", DRIVER_NAME,
                    EXYNOS4_GPL2(0), ret);
            return ret;
        }
        
        ret = gpio_request(EXYNOS4_GPK1(1), "LED");
        if (ret) {
            printk("%s: request GPIO %d for LED failed, ret = %d\n", DRIVER_NAME,
                    EXYNOS4_GPK1(1), ret);
            return ret;
        }

        s3c_gpio_cfgpin(EXYNOS4_GPL2(0), S3C_GPIO_OUTPUT);
        gpio_set_value(EXYNOS4_GPL2(0), 1);
        
        s3c_gpio_cfgpin(EXYNOS4_GPK1(1), S3C_GPIO_OUTPUT);
        gpio_set_value(EXYNOS4_GPK1(1), 1);
        
  misc_register(&hello_misc);
  printk(KERN_EMERG "hello_probe call \n");
  return 0;
  
}

int hello_remove(struct platform_device *pdev)
{

    misc_deregister(&hello_misc);
    printk(KERN_EMERG"remove call \n");
    return 0;
}
void hello_shutdown(struct platform_device *pdev)
{
   printk(KERN_EMERG"shutdown call \n");
}
int hello_suspend(struct platform_device *pdev, pm_message_t state)
{
     printk(KERN_EMERG"suspend call \n");
    return 0;
}
int hello_resume(struct platform_device * pdev)
{
    
     printk(KERN_EMERG"resume call \n");
    return 0;
}

struct platform_driver  stu_hello={
        .probe=hello_probe,
        .remove=hello_remove,
        .shutdown=hello_shutdown,
        .suspend=hello_suspend,
        .resume=hello_resume,
       .driver={
           .name = DRIVER_NAME,
           .owner = THIS_MODULE,
       },
    
};

static int hello_init(void)
{
    int err;
    printk(KERN_EMERG "hello world enter\n");
    err=platform_driver_register(&stu_hello);
     printk(KERN_EMERG "register result:%d\n",err);
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_EMERG "hello world exit\n");
    platform_driver_unregister(&stu_hello);

}
module_init(hello_init);
module_exit(hello_exit);



2.应用程序

呼吸灯效果很差,可能需要后面用pwm会好

#include "stdio.h"
#include 
#include 
#include //定义了open函数
#include //定义了close函数
#include //定义了ioctl函数


#include //
#include //



void delay_ms(int ms)
{
    usleep(ms*1000);
    
}

#define LED_ON() ioctl(fd,1,1)
#define LED_OFF()  ioctl(fd,1,0)
void ledfun (int fd)//主函数
{
        unsigned int CYCLE=40,PWM_LOW=0;//定义周期并赋值
          LED_OFF();
        while (1)         //主循环
        {

/*--------整排LED灯呼吸---------*/
         #if 0   
                delay_ms(100);                                                                 //加延时,可以看到熄灭的过程(下面程序同理)
                for(PWM_LOW=1;PWM_LOW0;PWM_LOW--)         //与逐渐变亮相反的过程
                {                                                     //点亮LED 
                    LED_ON();
                    delay_ms(PWM_LOW);
                    LED_OFF();
                    delay_ms(CYCLE-PWM_LOW);                                //主循环中添加其他需要一直工作的程序,延时长度,600次循环中从599减至1
                }
#else
    
                            ioctl(fd,1,1);                                                    //点亮LED 
                           delay_ms(200);
                          ioctl(fd,1,0);                                                  //熄灭LED
                          delay_ms(200);                                //主循环中添加其他需要一直工作的程序,延时长度,600次循环中从599减至1
#endif
        }
}








int main()
{
     char *device="/dev/hello_misc";
     long sleep_time=0;
     int ind=0;
    int fd = open(device,O_RDWR|O_NDELAY);
    if(fd<0)
    {
        printf("open file %s err \n",device);
        
    }else
    {
        
        printf("open file %s seuccess \n",device);
        
ledfun(fd);
      

        }
        
    


    
    
    close(fd);
    
}

你可能感兴趣的:(itop4412 驱动笔记4(led驱动和应用程序))