慢慢學Linux驅動開發,第九篇,tiny6410_LED驅動

來源:互聯網
上載者:User

     一直在看代碼,今天準備真正去試試,就拿tiny6410的LED燈開刀,雖說是自己寫,但實際上也是參考常式來的。不過還好基本的思路還是蠻清晰的。

 

     定義本次驅動為misc device(雜項裝置驅動),包含標頭檔miscdevice.h,其實所謂的雜項驅動程式就是主裝置號為10的字元裝置驅動,其實就是用主裝置號10調用了函數register_chrdev()。且misc device會自動在/dev目錄下建立裝置節點,而不需用mkmod手動建立。

 

開發板IO定義:

 

 

S3C6410 GPK口:

 

 

 

#include <linux/miscdevice.h>  
#include <linux/fs.h>
#include <linux/pci.h>
#include <mach/map.h>
#include <mach/regs-gpio.h>
#include <mach/gpio-bank-k.h>
#include "leds.h"
#define DEVICE_NAME "tiny6410_leds"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("pang123hui");
static long sbc2440_leds_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
unsigned tmp;
/*
case 0:
case 1:
if(arg > 4)
{
//非法參數
return -EINVAL;
}
tmp = readl(S3C64XX_GPKDAT);
tmp &= ~(1 << (3 + arg));
tmp |= ((cmd) << (3 + arg));
writel(tmp, S3C64XX_GPKDAT);
return 0;
*/

case 1:
case 2:
case 3:
case 4:
tmp = readl(S3C64XX_GPKDAT);
tmp = (tmp & ~(1<<(cmd - 1 + 4)))|(arg<<(cmd -1 + 4));
writel(tmp, S3C64XX_GPKDAT);
return 0;

default:
return -EINVAL;
}
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = sbc2440_leds_ioctl,
};
//misc device:雜項裝置,即主裝置號為10的特殊字元裝置
static struct miscdevice misc = {
//次裝置號,注意不要與/proc/misc中已有雜項裝置次裝置號衝突
//MISC_DYNAMIC_MINOR來動態擷取次裝置號
.minor = MISC_DYNAMIC_MINOR,
//裝置名稱
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
{
unsigned tmp;
tmp = readl(S3C64XX_GPKCON);
//GPKCON配置GPK4到GPK7配置為0001輸出
tmp = (tmp & ~(0xffffU<<16))|(0x1111U<<16);
writel(tmp, S3C64XX_GPKCON);

//GPKDAT[7:4] = 1
//燈滅
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0xF << 4);
writel(tmp, S3C64XX_GPKDAT);
//禁止上拉下拉
tmp = readl(S3C64XX_GPKPUD);
tmp &= (0x00 << 8);
writel(tmp, S3C64XX_GPKPUD);
}
//該函數會自動建立裝置節點,即裝置檔案
ret = misc_register(&misc);
printk (DEVICE_NAME"/tinitialized/n");
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
module_init(dev_init);
module_exit(dev_exit);

 

makefile

ifneq ($(KERNELRELEASE),)    
obj-m := leds.o
else
KERNELDIR := /opt/FriendlyARM/mini6410/linux/linux-2.6.38
PWD:=$(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.ko *.o *.mod.c *.mod.o *.symvers
endif

 

leds_test

 

#include <stdio.h>  
//exit
#include <stdlib.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int num = 0;
int led_state = 1;
int fd = 0;
//參數賦值
if(argc != 3 || sscanf(argv[1],"%d", &num) != 1 || sscanf(argv[2], "%d", &led_state) != 1 || num > 4 || led_state > 1)
{
fprintf(stderr, "error/n");
exit(1);
}
printf("num = %d, led_state = %d/n", num, led_state);
fd = open("/dev/tiny6410_leds", 0);
if (fd < 0) {
fd = open("/dev/tiny6410_leds0", 0);
}
if (fd < 0) {
perror("open device leds");
exit(1);
}
ioctl(fd, num, led_state);
printf("led%d/n", num);
close(fd);
return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.