免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1079 | 回复: 0
打印 上一主题 下一主题

LED测试 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-18 16:34 |只看该作者 |倒序浏览

LED测试
由HELLO驱动模块熟悉了驱动模块的基本编写方法。下面要开始做一个真正有用的驱动—LED驱动。象普通单片机一样,先从IO控制开始。IO控制首先就要知道地址和个控制寄存器,先翻DATASHEET了。由于开发板的LED是接在PIOA上的。先找地址

再看寄存器
#1 PIO Controller PIO Enable Register
Name:PIO_PER
Access Type: Write-only
?P0-P31: PIO Enable
0 = No effect.
1 = Enables the PIO to control the corresponding pin (disables peripheral control of the pin).
#2 PIO Controller PIO Disable Register
Name: PIO_PDR
Access Type: Write-only
?P0-P31: PIO Disable
0 = No effect.
1 = Disables the PIO from controlling the corresponding pin (enables peripheral control of the pin).
#3 PIO Controller PIO Status Register
Name: PIO_PSR
Access Type: Read-only
?P0-P31: PIO Status
0 = PIO is inactive on the corresponding I/O line (peripheral is active).
1 = PIO is active on the corresponding I/O line (peripheral is inactive).
#4 PIO Controller Output Enable Register
Name: PIO_OER
Access Type: Write-only
?P0-P31: Output Enable
0 = No effect.
1 = Enables the output on the I/O line.
#5 PIO Controller Output Disable Register
Name: PIO_ODR
Access Type: Write-only
?P0-P31: Output Disable
0 = No effect.
1 = Disables the output on the I/O line.
#6 PIO Controller Output Status Register
Name: PIO_OSR
Access Type: Read-only
?P0-P31: Output Status
0 = The I/O line is a pure input.
1 = The I/O line is enabled in output.
#7 PIO Controller Input Filter Enable Register
Name: PIO_IFER
Access Type: Write-only
?P0-P31: Input Filter Enable
0 = No effect.
1 = Enables the input glitch filter on the I/O line.
#8 PIO Controller Input Filter Disable Register
Name: PIO_IFDR
Access Type: Write-only
?P0-P31: Input Filter Disable
0 = No effect.
1 = Disables the input glitch filter on the I/O line.
#9 PIO Controller Input Filter Status Register
Name: PIO_IFSR
Access Type: Read-only
?P0-P31: Input Filer Status
0 = The input glitch filter is disabled on the I/O line.
1 = The input glitch filter is enabled on the I/O line.
#10 PIO Controller Set Output Data Register
Name: PIO_SODR
Access Type: Write-only
?P0-P31: Set Output Data
0 = No effect.
1 = Sets the data to be driven on the I/O line.
#11 PIO Controller Clear Output Data Register
Name: PIO_CODR
Access Type: Write-only
?P0-P31: Set Output Data
0 = No effect.
1 = Clears the data to be driven on the I/O line.
#12 PIO Controller Output Data Status Register
Name: PIO_ODSR
Access Type: Read-only or Read/Write
?P0-P31: Output Data Status
0 = The data to be driven on the I/O line is 0.
1 = The data to be driven on the I/O line is 1.
#13 PIO Controller Pin Data Status Register
Name: PIO_PDSR
Access Type: Read-only
?P0-P31: Output Data Status
0 = The I/O line is at level 0.
1 = The I/O line is at level 1.
#14 PIO Controller Interrupt Enable Register
Name: PIO_IER
Access Type: Write-only
?P0-P31: Input Change Interrupt Enable
0 = No effect.
1 = Enables the Input Change Interrupt on the I/O line.
#15 PIO Controller Interrupt Disable Register
Name: PIO_IDR
Access Type: Write-only
?P0-P31: Input Change Interrupt Disable
0 = No effect.
1 = Disables the Input Change Interrupt on the I/O line.
#16 PIO Controller Interrupt Mask Register
Name: PIO_IMR
Access Type: Read-only
?P0-P31: Input Change Interrupt Mask
0 = Input Change Interrupt is disabled on the I/O line.
1 = Input Change Interrupt is enabled on the I/O line.
#17 PIO Controller Interrupt Status Register
Name: PIO_ISR
Access Type: Read-only
?P0-P31: Input Change Interrupt Status
0 = No Input Change has been detected on the I/O line since PIO_ISR was last read or since reset.
1 = At least one Input Change has been detected on the I/O line since PIO_ISR was last read or since reset.
#18 PIO Multi-driver Enable Register
Name: PIO_MDER
Access Type: Write-only
?P0-P31: Multi Drive Enable.
0 = No effect.
1 = Enables Multi Drive on the I/O line.
#19 PIO Multi-driver Disable Register
Name: PIO_MDDR
Access Type: Write-only
?P0-P31: Multi Drive Disable.
0 = No effect.
1 = Disables Multi Drive on the I/O line.
#20 PIO Multi-driver Status Register
Name: PIO_MDSR
Access Type: Read-only
?P0-P31: Multi Drive Status.
0 = The Multi Drive is disabled on the I/O line. The pin is driven at high and low level.
1 = The Multi Drive is enabled on the I/O line. The pin is driven at low level only.
#21 PIO Pull Up Disable Register
Name: PIO_PUDR
Access Type: Write-only
?P0-P31: Pull Up Disable.
0 = No effect.
1 = Disables the pull up resistor on the I/O line.
#22 PIO Pull Up Enable Register
Name: PIO_PUER
Access Type: Write-only
?P0-P31: Pull Up Enable.
0 = No effect.
1 = Enables the pull up resistor on the I/O line.
#23 PIO Pull Up Status Register
Name: PIO_PUSR
Access Type: Read-only
?P0-P31: Pull Up Status.
0 = Pull Up resistor is enabled on the I/O line.
1 = Pull Up resistor is disabled on the I/O line.
#24 PIO Peripheral A Select Register
Name: PIO_ASR
Access Type: Write-only
?P0-P31: Peripheral A Select.
0 = No effect.
1 = Assigns the I/O line to the Peripheral A function.
#25 PIO Peripheral B Select Register
Name: PIO_BSR
Access Type: Write-only
?P0-P31: Peripheral B Select.
0 = No effect.
1 = Assigns the I/O line to the peripheral B function.
#26 PIO Peripheral A B Status Register
Name: PIO_ABSR
Access Type: Read-only
?P0-P31: Peripheral A B Status.
0 = The I/O line is assigned to the Peripheral A.
1 = The I/O line is assigned to the Peripheral B.
#27 PIO Output Write Enable Register
Name: PIO_OWER
Access Type: Write-only
?P0-P31: Output Write Enable.
0 = No effect.
1 = Enables writing PIO_ODSR for the I/O line.
#28 PIO Output Write Disable Register
Name: PIO_OWDR
Access Type: Write-only
?P0-P31: Output Write Disable.
0 = No effect.
1 = Disables writing PIO_ODSR for the I/O line.
#29 PIO Output Write Status Register
Name: PIO_OWSR
Access Type: Read-only
?P0-P31: Output Write Status.
0 = Writing PIO_ODSR does not affect the I/O line.
1 = Writing PIO_ODSR affects the I/O line.

下面是程序

#include
#include
#include
#include
#include
#include  /* get the user-level API */
#include
#include
#include
#include
#include "at91_pio.h"
MODULE_LICENSE("GPL");
static void __iomem *pio_base;
size_t led_read (struct file *filp, char __user *buf, size_t count, loff_t *pos){
     printk("watchdog read.\r\n");
     return 0;
}
ssize_t led_write (struct file *filp, const char __user *buf, size_t count,off_t *pos){
     printk("watchdog write.\r\n");
     return count;
}
int led_ioctl (struct inode *inode, struct file *filp,unsigned int cmd, unsigned long arg){
    // printk("led ioctl cmd:%d%d\r\n",cmd,arg);
    long  mode;
   
    switch(cmd)
    {
     case 1:
           mode=0x00000100;
     break;
     case 2:
           mode=0x00000080;
     break;
     case 3:
           mode=0x00000040;
     break;
     case 4:
           mode=0x00000020;
     break;
      
     default:
           mode=0x000001e0;
     break;
     
    }
   
    //printk("led ioctl cmd:%d%d%ld\r\n",cmd,arg,mode);
    if(arg ==0 ) {writel(mode, pio_base + PIO_CODR);}
    else
    {writel(mode, pio_base + PIO_SODR);}
     return 0;
}
static struct file_operations led_fops = {
     .owner = THIS_MODULE,
     .read = led_read,
     .write = led_write,
     .ioctl = led_ioctl,
};
static int led_init(void){
     int result;
     printk(KERN_ALERT "led_test:%s,%s\r\n",__DATE__,__TIME__);
     pio_base = ioremap(0xFFFFF400,512);            
     writel(0x000001e0, pio_base + PIO_PER);     
     writel(0x000001e0, pio_base + PIO_PUER);   
     
     writel(0x00000ff0, pio_base + PIO_ODSR);   
     writel(0x000001e0, pio_base + PIO_OER);     
     
     result = register_chrdev(60, "led", &led_fops);
     if (result
         printk("register device fail.\r\n");
return result;
     }
     return 0;
}
static void led_exit(void){
printk("stop clear led\r\n");
     unregister_chrdev(60, "led");
     iounmap(pio_base);
     printk(KERN_ALERT "stop clear led\r\n");
}
module_init(led_init);
module_exit(led_exit);

编译好后,在运行之前,先要创建一个设备文件
# mknod "/dev/led" c 60 1

即创建一个主设备号为60,次设备号为1名称为LED的字符型设备。
然后运行这个函数即可看见LED开始闪烁。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/68643/showart_1085516.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP