基于SC2440的LED驱动及测试程序编写

本人使用平台如下:

  1. JZ2440开发板
  2. 自制Crosstool交叉编译工具链
  3. busybox-1.20.2
  4. 基于linux-3.14.11  内核

本人学习韦东山的视频,视频使用的是基于linux2.6的内核,驱动程序上有些许不同,不同之处如下:

  1. 将#define DECLARE_MUTEX(name) 改成了 #define DEFINE_SEMAPHORE(name)
  2. 将s3c2410_gpio_setpin改为gpio_set_value
  3. 将class_device_create改为device_create

这些更改在linux内核源代码中能够找到

最后贴上完整驱动代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/gpio.h>
 
#include <asm/uaccess.h>
#include <asm/io.h>
 
#include <mach/hardware.h>
#include <mach/regs-gpio.h>
#include <mach/regs-irq.h>
#include <mach/gpio-samsung.h>
 
#include <plat/gpio-cfg.h>
 
#define DEVICE_NAME "testleds"
#define LED_MAJOR   250
 
static struct class *leds_class;
static struct class_device *leds_class_devs[4];
 
static char leds_status = 0x0;
 
//将#define DECLARE_MUTEX(name)   改成了 #define DEFINE_SEMAPHORE(name) 
static DEFINE_SEMAPHORE(leds_lock);
 
static unsigned long gpio_va;
 
#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))
 
static int s3c2440_leds_open(struct inode *inode, struct file *file)
{
  int minor = MINOR(inode->i_rdev);
 
  switch(minor){
    case 0:{
      GPFCON &= ~(0x3<<(4*2));
      GPFCON |= (1<<(4*2));
      GPFCON &= ~(0x3<<(5*2));
      GPFCON |= (1<<(5*2));
      GPFCON &= ~(0x3<<(6*2));
      GPFCON |= (1<<(6*2));
      GPFDAT &= ~(1<<4);
      GPFDAT &= ~(1<<5);
      GPFDAT &= ~(1<<6);
 
      down(&leds_lock);
      leds_status = 0x0;
      up(&leds_lock);
      break;
    }
    case 1:{
      //将s3c2410_gpio_setpin改为gpio_set_value
      s3c_gpio_cfgpin(S3C2410_GPF(4),S3C_GPIO_SFN(1) );
      gpio_set_value(S3C2410_GPF(4), 0);
 
      down(&leds_lock);
      leds_status &= ~(1<<0);
            up(&leds_lock);
 
            break;
    }
    case 2:{
      s3c_gpio_cfgpin(S3C2410_GPF(5), S3C_GPIO_SFN(1));
      gpio_set_value(S3C2410_GPF(5), 0);
 
      down(&leds_lock);
      leds_status &= ~(1<<1);
            up(&leds_lock);
 
            break;
    }
    case 3:{
      s3c_gpio_cfgpin(S3C2410_GPF(6), S3C_GPIO_SFN(1));
      gpio_set_value(S3C2410_GPF(6), 0);
 
      down(&leds_lock);
      leds_status &= ~(1<<2);
            up(&leds_lock);
 
            break;
    }
  }
 
  return 0;
}
 
 
static int s3c2440_leds_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
  int minor = MINOR(filp->f_dentry->d_inode->i_rdev);
    char val;
  switch (minor)
    {
      case 0: /* /dev/leds */
      {
 
        copy_to_user(buff, (const void *)&leds_status, 1);					  
        break;
      }
 
      case 1: /* /dev/led1 */
      {
        down(&leds_lock);
        val = leds_status & 0x1;
        up(&leds_lock);
        copy_to_user(buff, (const void *)&val, 1);
        break;
      }
 
      case 2: /* /dev/led2 */
      {
        down(&leds_lock);
        val = (leds_status>>1) & 0x1;
        up(&leds_lock);
        copy_to_user(buff, (const void *)&val, 1);
        break;
      }
 
      case 3: /* /dev/led3 */
      {
        down(&leds_lock);
        val = (leds_status>>2) & 0x1;
        up(&leds_lock);
        copy_to_user(buff, (const void *)&val, 1);
        break;
      }
 
    }
 
 
  return 1;
}
 
static ssize_t s3c2440_leds_write(struct file *file, const char __user *buf, ssize_t count, loff_t * ppos)
{
  int minor = MINOR(file->f_dentry->d_inode->i_rdev);
    char val;
 
    copy_from_user(&val, buf, 1);
 
    switch (minor)
    {
      case 0: /* /dev/leds */
      {			 
        gpio_set_value(S3C2410_GPF(4), (val & 0x1));
        gpio_set_value(S3C2410_GPF(5), (val & 0x1));
        gpio_set_value(S3C2410_GPF(6), (val & 0x1));
        down(&leds_lock);
        leds_status = val;
        up(&leds_lock);
        break;
      }
 
      case 1: /* /dev/led1 */
      {
        gpio_set_value(S3C2410_GPF(4), val);
 
        if (val == 0)
        {
          down(&leds_lock);
          leds_status &= ~(1<<0);
          up(&leds_lock);
        }
        else
        {
          down(&leds_lock);
          leds_status |= (1<<0);				  
          up(&leds_lock);
        }
        break;
      }
 
      case 2: /* /dev/led2 */
      {
        gpio_set_value(S3C2410_GPF(5), val);
        if (val == 0)
        {
          down(&leds_lock);
          leds_status &= ~(1<<1);
          up(&leds_lock);
        }
        else
        {
          down(&leds_lock);
          leds_status |= (1<<1);				  
          up(&leds_lock);
        }
        break;
      }
 
      case 3: /* /dev/led3 */
      {
        gpio_set_value(S3C2410_GPF(6), val);
        if (val == 0)
        {
          down(&leds_lock);
          leds_status &= ~(1<<2);
          up(&leds_lock);
        }
        else
        {
          down(&leds_lock);
          leds_status |= (1<<2);				  
          up(&leds_lock);
        }
        break;
      }
 
    }
 
    return 1;
 
}
static struct file_operations s3c2440_leds_fops = {
  .owner	=	THIS_MODULE,
  .open	=	s3c2440_leds_open,
  .read	=	s3c2440_leds_read,
  .write	=	s3c2440_leds_write,
};
 
 
static int __init s3c2440_leds_init(void)
{
  int ret;
  int minor = 0;
 
  gpio_va = ioremap(0x56000000, 0x100000);
  if (!gpio_va) {
    return -EIO;
  }
 
  ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c2440_leds_fops);
 
  if(ret < 0){
    printk(DEVICE_NAME "can't register major number\n");
    return ret;
  }
 
  leds_class = class_create(THIS_MODULE,"testleds");
  if(IS_ERR(leds_class))
    return PTR_ERR(leds_class);
 
  leds_class_devs[0] = device_create(leds_class, NULL, MKDEV(LED_MAJOR, 0), NULL, "testleds");
  for(minor = 1; minor < 4; minor++){
    leds_class_devs[minor] = device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);
    if(unlikely(IS_ERR(leds_class_devs[minor])))
      return PTR_ERR(leds_class_devs[minor]);
  }
 
  printk(DEVICE_NAME "initialized\n");
  return 0;
}
 
 
static void __exit s3c2440_leds_exit(void)
{
  int minor;
  /* 卸载驱动程序 */
  unregister_chrdev(LED_MAJOR, DEVICE_NAME);
 
  for(minor = 0; minor < 4; minor++)
  {
    device_unregister(leds_class_devs[minor]);
  }
  class_destroy(leds_class);
  iounmap(gpio_va);
 
}
module_init(s3c2440_leds_init);
module_exit(s3c2440_leds_exit);
 
MODULE_AUTHOR("shen chao");
MODULE_LICENSE("GPL");

测试代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
 
/*
  *  ledtest <dev> <on|off>
  */
 
void print_usage(char *file)
{
    printf("Usage:\n");
    printf("%s <dev> <on|off>\n",file);
    printf("eg. \n");
    printf("%s /dev/leds on\n", file);
    printf("%s /dev/leds off\n", file);
    printf("%s /dev/led1 on\n", file);
    printf("%s /dev/led1 off\n", file);
}
 
int main(int argc, char **argv)
{
    int fd;
    char* filename;
    char val;
 
    if (argc != 3)
    {
        print_usage(argv[0]);
        return 0;
    }
 
    filename = argv[1];
 
    fd = open(filename, O_RDWR);
    if (fd < 0)
    {
        printf("error, can't open %s\n", filename);
        return 0;
    }
 
    if (!strcmp("on", argv[2]))
    {
        // 亮灯
        val = 0;
        write(fd, &val, 1);
    }
    else if (!strcmp("off", argv[2]))
    {
        // 灭灯
        val = 1;
        write(fd, &val, 1);
    }
    else
    {
        print_usage(argv[0]);
        return 0;
    }
 
 
    return 0;
}
基于SC2440的LED驱动及测试程序编写

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

5 × 4 =

滚动到顶部
沪ICP备18028346号