關於(在2410+linux平台上,關於led 的一個簡單應用程式)的驅動程式

來源:互聯網
上載者:User

//4led.c

 

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/irq.h>
#include <linux/delay.h>

#include <asm/hardware.h>

#define DEVICE_NAME "ledwen"
#define LED_MAJOR 240

 

MODULE_LICENSE("GPL"); //用於消除著作權警告訴

 

static unsigned long led_table [] = {
 GPIO_B7,
 GPIO_B8,
 GPIO_B9,
 GPIO_B10,
};

/*************************************************************************/
static int matrix4_leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
     switch(cmd)

    {
         case 0:
         case 1:
          if (arg > 4) 
          return -EINVAL;
          write_gpio_bit(led_table[arg], !cmd);
          printk("led : %d   %d",arg , cmd /n);
          default:
          return -EINVAL;
     }
}

/*************************************************************************/
static struct file_operations matrix4_leds_fops = {
 owner: THIS_MODULE,
 ioctl:  matrix4_leds_ioctl,
};

static devfs_handle_t devfs_handle;

static int __init matrix4_leds_init(void)
{
 int ret;
 int i;

 ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &matrix4_leds_fops);
 if (ret < 0) {
   printk(DEVICE_NAME " can't register major number/n");
   return ret;
 }
 devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT,
    LED_MAJOR, 0, S_IFCHR | S_IRUSR | S_IWUSR, &matrix4_leds_fops, NULL);
 for (i = 0; i < 8; i++) {
  set_gpio_ctrl (led_table[i] | GPIO_PULLUP_EN | GPIO_MODE_OUT);
  write_gpio_bit(led_table[i], 1);
 }

 printk(DEVICE_NAME " initialized/n");
 return 0;
}

static void __exit matrix4_leds_exit(void)
{
 devfs_unregister(devfs_handle);
 unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

module_init(matrix4_leds_init);
module_exit(matrix4_leds_exit);

 

上面代碼在虛擬機器中用命令:

#arm-linux-gcc -D__KERNEL__ -I/friendly-arm/kernel/include -DKBUILD_BASENAME=matrix4-leds -DMODULE -c -o 4led.o 4led.c  

將其進行編譯。

上面的編譯命令中  -DKBUILD_BASENAME=matrix4-leds  段不知道為何意思,實踐操作發現這段去掉也可以編譯通過,並能運行。

 

編譯通過後,下傳到目標板,用命令:insmod 4led.o  將其載入。(注意檔案名稱不是led了,而是;ledwen)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.