#include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME "led1" unsigned long GPIOK_VA_BASE ; #define GPIOK_CON0 GPIOK_VA_BASE #define GPIOK_CON1 GPIOK_VA_BASE+0X4 #define GPIOK_DAT GPIOK_VA_BASE+ 0X8 #define GPIOK_PUD GPIOK_VA_BASE +0XC #define GPIOK_PA_BASE 0X7F008800 struct resource led_resource = { .name = "led io-mem ", .start = GPIOK_PA_BASE, .end = GPIOK_PA_BASE +0X10, .flags = IORESOURCE_MEM, }; static void led_pin_setup(void) { unsigned long start = led_resource.start; unsigned long size = led_resource.end - led_resource.start; unsigned long tmp; request_mem_region(start,size,led_resource.name); GPIOK_VA_BASE = (unsigned long)ioremap(start,size); printk ("<1> [GPIOK_VA_BASE = 0x%lx]n", GPIOK_VA_BASE); tmp = readl(GPIOK_CON0); tmp = (tmp & ~(0xffffu<<16))|(0x1111u<<16); writel(tmp,GPIOK_CON0); tmp = readl(GPIOK_DAT); tmp |= (0xf <<4); writel(tmp,GPIOK_DAT); } static void led_pin_release(void) { iounmap((void*)GPIOK_VA_BASE); release_mem_region(led_resource.start,led_resource.end - led_resource.start); } static unsigned long led_getdata(void) { return ((readl(GPIOK_DAT)>>4)& 0XF); } static void led_setdata(int data) { unsigned long temp; temp = readl(GPIOK_DAT); temp = ~(0xF<<4) | ((data&0xF)<<4); writel(temp,GPIOK_DAT); } static ssize_t led_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { int ret = 0; char buffer[1]; memset(buffer,0,sizeof(buffer)); buffer[0] = (char)led_getdata() ; if(copy_to_user(buf,buffer,size)) { ret = -EFAULT; } return size; } static ssize_t led_write(struct file *flip,const char __user *buf,size_t size,loff_t *ppos) { int ret = 0; unsigned long buffer[1]; memset(buffer,0,sizeof(buffer)); if(copy_from_user(buffer,buf,size)) { printk("errorn"); ret = -EFAULT; } else{ led_setdata(0x00); led_setdata((int )buffer[0]); } return ret; } static struct file_operations dev_fops ={ .owner = THIS_MODULE, .read = led_read, .write = led_write, }; static struct miscdevice misc ={ .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, }; static int __init dev_init(void) { int ret; led_pin_setup(); ret = misc_register(&misc); printk(DEVICE_NAME"initialize minor =%dn",misc.minor); return ret; } static void __exit dev_exit(void) { led_pin_release(); misc_deregister(&misc); } module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("GOAT.ZHOU"); 以上的是led的驱动文件,以write 向led里写数据,read读取led的状态。 以下的是测试程序 led_app.c #include #include #include #include #include #include #include char binstr_to_int(char *binstr) { char ret = 0, i = 0; char bnum[5]; memset(bnum, '0', 4); int len = strlen(binstr); if (len > 4) strcpy(bnum, binstr+len-4); else strcpy(bnum+4-len, binstr); for (i=0; i<4; i++) { ret <<= 1; ret += (bnum[i]=='0'? 1: 0); } return ret; } int main(int argc, char *argv[]) { int fd; unsigned char buff[1]; fd = open("/dev/led1",O_RDWR); if(fd == -1){ printf("open dev error"); _exit(EXIT_FAILURE); } buff[0] = binstr_to_int(argv[1]); write(fd,buff,sizeof(buff)); memset(buff,0,sizeof(buff)); printf("%dn", read(fd,buff,sizeof(buff))); printf("%d",(int)buff[0]); close(fd); return 0; }
上一篇:s3c6410 uart 裸机 轮询 非fifo
下一篇:Linux内核模块驱动之---led驱动
推荐阅读最新更新时间:2024-11-05 13:22
设计资源 培训 开发板 精华推荐
- LTC4367HDD 过压电源保护控制器的典型应用
- 使用 Analog Devices 的 LT1317BIS8 的参考设计
- 征集令 |物联网智能喂食器
- AM1D-0512DH30-RZ ±12V 1 瓦 DC-DC 转换器的典型应用
- VM800B43A-PL,基于支持3.3/5V MCU转接板的FT800基本开发模块,4.3英寸TFT LCD显示屏,珍珠边框外壳
- 用于双极晶体管的 Tc110501Ect Pfm/Pwm 升压 Dc/Dc 控制器的典型应用
- AD8532ARZ-REEL7 单电源立体声耳机驱动器的典型应用
- 使用具有最大功率点跟踪功能的 LTC4162EUFD-L40M 太阳能供电 36 节 3.2A 充电器的典型应用
- 具有最大功率点跟踪功能的 LTC4162IUFD-FSTM 太阳能供电 3 节 3.2A 充电器的典型应用
- 【物联网】鸿蒙智能WiFi开关+4244212A