📄 gpio.c
字号:
#include <linux/kernel.h>#include <linux/module.h>#include <linux/compatmac.h>#include <linux/hdreg.h>#include <linux/vmalloc.h>#include <linux/fs.h>#include <linux/module.h>#include <linux/blkpg.h>//#include <asm/arch/hardware.h>//nclude <linux/i2c.h>//#include <linux/i2c-algo-bit.h>//#include <linux/i2c-id.h>#include <linux/slab.h>#include <asm/io.h>//#include <asm/proc-armv/cache.h>#include <linux/mm.h>#include <linux/wrapper.h>//#include <asm/dma.h>#include <linux/miscdevice.h>#include <linux/errno.h>#include <linux/tqueue.h>#include <linux/wait.h>//#include <linux/pm.h>#include <asm/irq.h>#include <asm/arch/hardware.h>#include <asm/arch/irqs.h>#include "type.h"#include "mx1hw.h"#ifdef DEBUG_GPIO#define dprintgpio(str...) printk("<"__FUNCTION__"> "str)#else#define dprintgpio(str...) // nothing#endif#define IOCTL_GPIO_INIT 1#define IOCTL_GPIO_WRITE 2#define IOCTL_GPIO_READ 3#define GPIO_INTR_MODE 0#define GPIO_IRQ 0x07//#define GPIO_IRQSTAT (*((VUINT32*)(IO_ADDRESS(PTA_ISR))))#define NO_DATA_IN 0x000000C0#define NO_DATA_OUT 0x00000030#define I2C_DRIVERID_I2CCSI 0x1001static int gpio_open(struct inode *inode, struct file *filp);static int gpio_release(struct inode *inode, struct file *filp);static ssize_t gpio_read(struct file *filp, char *buf, size_t size, loff_t *l);static ssize_t gpio_write(struct file *filp, const char *buf, size_t size, loff_t *l);static int gpio_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);static void GPIO_init(void);static int GPIO_read();static void GPIO_write(unsigned long arg);static int gMajor=0;wait_queue_head_t ts_wait;struct file_operations gpio_fops = { open: gpio_open, release: gpio_release, read: gpio_read, write: gpio_write, ioctl: gpio_ioctl,};static int gpio_open(struct inode *inode, struct file *filp){ dprintgpio("*** open ***\n"); MOD_INC_USE_COUNT; return 0;}static int gpio_release(struct inode *inode, struct file *filp){ dprintgpio("*** close ***\n"); MOD_DEC_USE_COUNT; return 0;}static ssize_t gpio_read(struct file *filp, char *buf, size_t size, loff_t *l){ dprintgpio("*** read ***\n"); return 0; }static ssize_t gpio_write(struct file *filp, const char *buf, size_t size, loff_t *l){ dprintgpio("*** write ***\n"); return 0;}static int gpio_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ int rv=0; dprintgpio("cmd: 0x%08x, arg: 0x%08x\n", cmd, (int)arg); switch(cmd) { case IOCTL_GPIO_INIT: dprintgpio("Init gpio\n"); { GPIO_init(); } break; case IOCTL_GPIO_WRITE: dprintgpio("set reg\n"); GPIO_write(arg); break; case IOCTL_GPIO_READ: { dprintgpio("get reg value\n"); rv=GPIO_read(); printk("rv=%d\n",rv); // return(rv); } break; default: break; } return rv; } void gpio_isr(U16 irq, void * dev_id, struct pt_regs *regs){ U32 reg_value; reg_value = * (U32 *)PTA_ISR; if ( (reg_value & NO_DATA_IN)==NO_DATA_IN) { wake_up_interruptible(&ts_wait); printk("No data in"); } else { } /* else { printk("other inter \n"); GPIO_IRQSTAT |= 0xf; } */}static void GPIO_init(void){ printk("init the gpio ports \n"); //init PA4~PA7 // * (U32 *)PTA_SSR |= 0x00000000; //initialize input pins I0=0,I1=0 * (U32 *)PTA_DR &= 0xFFFFFF0F; * (U32 *)PTA_DR |= 0x00000030; }#ifdef DEBUG_GPIOWR #define dprintgpiowr(str...) printk("<"__FUNCTION__"> "str)#else#define dprintgpiowr(str...) // nothing#endifstatic void GPIO_write(unsigned long arg){dprintgpio("*** set signals of I1, I0 ***\n"); if( arg==0 ) * (U32 *)PTA_DR &= 0xFFFFFFCF; // _clear_register_bit ((U32*) PTA_DR, 4); else if( arg==1) { * (U32 *)PTA_DR &= 0xFFFFFF0F; * (U32 *)PTA_DR |= 0x00000010; } else if( arg==2 ) { * (U32 *)PTA_DR &= 0xFFFFFF0F; * (U32 *)PTA_DR |= 0x00000020; } else { * (U32 *)PTA_DR &= 0xFFFFFF0F; * (U32 *)PTA_DR |= 0x00000030; }// else dprintgpio("*** error***\n");}static int GPIO_read(){ int ret=-6; dprintgpio("*** read signals of O1,O0***\n"); if ( ( (* (U32 *)PTA_SSR) & 0x000000C0 )==0x00000000) ret = 0; // 子口1输入 else if ( ( (* (U32 *)PTA_SSR) &0x000000C0 ) ==0x00000040) ret = 1; // 子口2输入 else if ( ( (* (U32 *)PTA_SSR) & 0x000000C0 )==0x00000080) ret = 2; // 子口3输入 else ret=3; // else dprintgpio("*** error***\n"); return ret;}static devfs_handle_t devfs_handle;int init_module(){ int result;// printk("GPIOdriver "__DATE__" / "__TIME__"\n"); // printk("begin\n"); // int tmp; /*Request for ISR handler*/ /*tmp = request_irq(GPIO_IRQ, (void * )gpio_isr, GPIO_INTR_MODE, "gpio", NULL); printk("tem=%d \n",tmp); if(tmp < 0) return tmp; */ /* register our character device */ result = devfs_register_chrdev(0, "gpio", &gpio_fops); if ( result < 0 ) { printk("gpio driver: Unable to register driver\n"); return -ENODEV; } // printk("result=%d \n",result); // printk("%s:%i\n", __FILE__, __LINE__); devfs_handle = devfs_register(NULL, "gpio", DEVFS_FL_DEFAULT, result, 0, S_IFCHR | S_IRUSR | S_IWUSR, &gpio_fops, NULL); // printk(":'mknod gpio c %d 0'\n", result); gMajor = result; // printk("ISR reg:%x\n",* (U32 *)PTA_ISR ); //0x000000f0 // * (U32 *)PTA_ISR |= 0x00000000; //initialize input pins I0=0,I1=0 // printk("get the reg:%x\n",* (U32 *)PTA_GIUS); * (U32 *)PTA_GIUS |= 0x000000F0; //0x10027ffe// printk("get the reg:%x\n",* (U32 *)PTA_GIUS);// printk("reg:%x\n",* (U32 *)PTA_DDIR); //0x000100f0 * (U32 *)PTA_DDIR = 0x00000030; // printk("DDIR:%x\n",* (U32 *)PTA_DDIR); //0x000100c0 // printk("reg:%x\n",* (U32 *)PTA_GPR); //0x000000f0 * (U32 *)PTA_GPR |= 0x000000F0;// printk("reg:%x\n",* (U32 *)PTA_GPR); //0x000000f0/* printk("reg:%x\n",* (U32 *)PTA_PUEN ); //0x000000f0 * (U32 *)PTA_PUEN &= 0xFFFFFF0F; printk("reg:%x\n",* (U32 *)PTA_PUEN ); //0x000000f0 */ // printk("CON:%x\n",* (U32 *)PTA_ICONFA1 ); //0xffffffff * (U32 *)PTA_ICONFA1 = 0x11111011; // printk("ICON:%x\n",* (U32 *)PTA_ICONFA1 ); //0x000000f0 // printk("OCR:%x\n",* (U32 *)PTA_OCR1 ); //0x000000f0 * (U32 *)PTA_OCR1 = 0x00000F00;// printk("OCR:%x\n",* (U32 *)PTA_OCR1 ); //0x000000f0 // printk("SWR:%x\n",* (U32 *)PTA_SWR ); //0x00000000 // printk("DR:%x\n",* (U32 *)PTA_DR ); //0x000000f0 * (U32 *)PTA_DR &= 0xFFFFFF0F; * (U32 *)PTA_DR |= 0x00000030; printk("DR:%x\n",* (U32 *)PTA_DR); //0x000000f0 // printk("SSR:%x\n",* (U32 *)PTA_SSR ); //0x000000f0 * (U32 *)PTA_SSR |= 0x00000000; //采样输出// printk("SSR:%x\n",* (U32 *)PTA_SSR ); //0x000000f0 printk("UART1_CR2%x\n",* (U32 *)UART1_CR2); printk("UART2_CR2%x\n",* (U32 *)UART2_CR2); /* printk("%x\n",* (U32 *)UART2_FCR); // * (U32 *)UART2_FCR=0x00000801; printk("%x\n",* (U32 *)UART2_BIR); printk("%x\n",* (U32 *)UART2_BMR); printk("%x\n",* (U32 *)UART1_CR1); printk("%x\n",* (U32 *)UART2_CR1); // * (U32 *)UART2_BIR=0x000000C0;// * (U32 *)UART2_BMR=0x00002711;// printk("Execute here"); */ return 0;}void cleanup_module(){ if (gMajor > 0){ devfs_unregister_chrdev(gMajor, "gpio"); devfs_unregister(devfs_handle); } printk("Say goodbye to gpio\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -