歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

mini2440 LED驅動程序

mini2440 LED驅動程序:

myled.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/device.h>
#include <mach/hardware.h>
#include <mach/gpio-nrs.h>
#include <mach/regs-gpio.h>
/*#include "gpio-nrs.h"*/

//add tsuibin

#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/io.h>

#include <mach/hardware.h>
#include <mach/gpio-fns.h>
#include <asm/irq.h>

#include <mach/regs-gpio.h>

 

 

#define MYLED_COUNT 1
#define MYLED_NAME "myled"
#define S3C2410_GPB5 S3C2410_GPB(5)
#define S3C2410_GPB6 S3C2410_GPB(6)
#define S3C2410_GPB7 S3C2410_GPB(7)
#define S3C2410_GPB8 S3C2410_GPB(8)

// S3C2410_GPIO_OUTPUT
static struct cdev myled_cdev;
static struct class *myled_class;
static struct device *myled_device;

static unsigned long led_table[] = {
  S3C2410_GPB (5),
  S3C2410_GPB (6),
  S3C2410_GPB (7),
  S3C2410_GPB (8),
};

ssize_t
myled_read (struct file *filp, char __user * buf, size_t count,
     loff_t * f_pos)
{
  return 0;
}

ssize_t
myled_write (struct file * filp, const char __user * buf, size_t count,
      loff_t * f_pos)
{

  unsigned int pin;
  unsigned int led_off = 0;
  unsigned long offs;
  unsigned long flags;
  unsigned long dat;
  void *base;


  pin = buf[0] - '0';
  if (buf[1] == '0')
    {
      led_off = 1;
    }
  if (pin >= 4 || pin < 0)
    return -1;

  pin = led_table[pin];

  base = S3C24XX_GPIO_BASE (pin);
  offs = S3C2410_GPIO_OFFSET (pin);
  local_irq_save (flags);

  dat = __raw_readl (base + 0x04);
  dat &= ~(1 << offs);
  dat |= led_off << offs;
  __raw_writel (dat, base + 0x04);

  local_irq_restore (flags);


  return count;
}

int
myled_ioctl (struct inode *inode, struct file *filp, unsigned int cmd,
      unsigned long arg)
{

  return 0;
}

int
myled_open (struct inode *inode, struct file *filp)
{
  return 0;
}

int
myled_release (struct inode *inode, struct file *filp)
{
  return 0;
}

struct file_operations myled_fops = {
  .owner = THIS_MODULE,
  .read = myled_read,
  .write = myled_write,
  .ioctl = myled_ioctl,
  .open = myled_open,
  .release = myled_release,
};

static int __init
myled_init (void)
{
  int ret;
  dev_t dev;

  /* config gpbcon */
  /* S3C2410_GPIO_BANKB */
  /* S3C2410_GPIO_OUTPUT */
  void *base;
  int i;
  unsigned int pin;
  unsigned int function;
  unsigned long mask;
  unsigned long con;
  unsigned long flags;


  ret = alloc_chrdev_region (&dev, 0, MYLED_COUNT, MYLED_NAME);
  if (ret < 0)
    {
      printk ("get dev error!\n");
      return -1;
    }
  cdev_init (&myled_cdev, &myled_fops);
  cdev_add (&myled_cdev, dev, 1);

  myled_class = class_create (THIS_MODULE, MYLED_NAME);
  myled_device = device_create (myled_class, NULL, dev, NULL, MYLED_NAME);

  for (i = 0; i < 4; i++)
    {
      pin = led_table[i];
      function = S3C2410_GPIO_OUTPUT;
      base = S3C24XX_GPIO_BASE (pin);
      mask = 3 << S3C2410_GPIO_OFFSET (pin) * 2;
      function &= 3;
      function <<= S3C2410_GPIO_OFFSET (pin) * 2;
      local_irq_save (flags);
      con = __raw_readl (base + 0x00);
      con &= ~mask;
      con |= function;
      __raw_writel (con, base + 0x00);
      local_irq_restore (flags);

    }
  printk ("module init\n");

  return ret;
}

static void __exit
myled_exit (void)
{

  dev_t dev;
  printk ("module exit\n");

  dev = myled_cdev.dev;

  device_destroy (myled_class, dev);
  cdev_del (&myled_cdev);
  unregister_chrdev_region (dev, 1);
  class_destroy (myled_class);

  return;
}

module_init (myled_init);
module_exit (myled_exit);
MODULE_LICENSE ("Dual BSD/GPL");
MODULE_AUTHOR("Tsuibin");

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/7月/9日/mini2440 LED驅動程序

Copyright © Linux教程網 All Rights Reserved