?? gpio.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: gpio.c
** Last modified Date: 2005-04-21
** Last Version: 1.0
** Descriptions: This is a Kernel module for uClinux 2.4.x .** This module let uClinux 2.4.x can use gpio.
**------------------------------------------------------------------------------------------------------
** Created by: Chenmingji
** Created date: 2005-04-21
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#define IN_GPIO#include "config.h"/********************************************************************************************************
function announce********************************************************************************************************/
#if 0static loff_t gpio_llseek(struct file *filp, loff_t off, int whence);static ssize_t gpio_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);static ssize_t gpio_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);#endifstatic int gpio_open(struct inode *inode, struct file *filp);static int gpio_release(struct inode *inode, struct file *filp); static int gpio_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long param);int gpio_init(void);void gpio_cleanup(void);/********************************************************************************************************
function announce********************************************************************************************************/
#define MAJOR_NR majormodule_init(gpio_init);module_exit(gpio_cleanup);MODULE_PARM(major, "i");MODULE_LICENSE("Proprietary");MODULE_DESCRIPTION("Guangzou ZLG-MCU Development Co.,LTD.\ngraduate school\nhttp://www.zlgmcu.com");MODULE_SUPPORTED_DEVICE("uClinux2.4.x LPC2200 GPIO");MODULE_AUTHOR("chenmingji");/*********************************************************************************************************
** "全局和靜態變量在這里定義"
** global variables and static variables define here
********************************************************************************************************/
static int major = GPIO_MAJOR_NR;/********************************************************************************************************/
static struct file_operations gpio_fops = /* driver info */{ owner: THIS_MODULE,#if 0 llseek: gpio_llseek, read: gpio_read, write: gpio_write,#endif ioctl: gpio_ioctl, open: gpio_open, release: gpio_release,};#if 0/*********************************************************************************************************
** Function name: gpio_llseek
** Descriptions: move read and write point
** Input: filp: pointer of file
** off: ofset
** whence: move mode
** 0: seek set
** 1: seek file' current point
** 2: seek file' end
** Output : new point
** Created by: Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static loff_t gpio_llseek(struct file *filp, loff_t off, int whence){ return 0;}/*********************************************************************************************************
** Function name: gpio_read
** Descriptions: read device
** Input: filp: pointer of file
** buf: buf for save data
** count: size for read
** f_pos: *f_pos = read point
** Output : read size
** Created by: Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static ssize_t gpio_read(struct file *filp, char *buf, size_t count, loff_t *f_pos){ return 0;}/*********************************************************************************************************
** Function name: gpio_write
** Descriptions: write device
** Input: filp: pointer of file
** buf: buf to write data
** count: size for read
** f_pos: *f_pos = read point
** Output : write size
** Created by: Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static ssize_t gpio_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos){ return 0;}#endif /*********************************************************************************************************
** Function name: gpio_open
** Descriptions: open device
** Input:inode: information of device
** filp: pointer of file
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static int gpio_open(struct inode *inode, struct file *filp){ MOD_INC_USE_COUNT; return 0; /* success */} /*********************************************************************************************************
** Function name: gpio_release
** Descriptions: release device
** Input:inode: information of device
** filp: pointer of file
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static int gpio_release(struct inode *inode, struct file *filp) { MOD_DEC_USE_COUNT; return(0); } /*********************************************************************************************************
** Function name: gpio_ioctl
** Descriptions: IO control function
** Input:inode: information of device
** filp: pointer of file
** cmd: command
** arg: additive parameter
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#define GPIO_ADDR_BASE 0xE0028000#define GPIO_PIN_OFFSET 0x00000000#define GPIO_SET_OFFSET 0x00000001#define GPIO_DIR_OFFSET 0x00000002#define GPIO_CLR_OFFSET 0x00000003#define GPIO_PORT_ADD 0x00000010 static int gpio_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg){ int num; volatile u32 *Reg; u32 temp1, temp2; num = MINOR(inode->i_rdev); if (num >= MAX_PORT) { return -ENODEV; }
if (_IOC_TYPE(cmd) != GPIO_IOC_MAGIC) { return -ENOTTY; }
if (_IOC_NR(cmd) >= GPIO_MAXNR) { return -ENOTTY; }
Reg = (volatile u32 *)(GPIO_ADDR_BASE + GPIO_PORT_ADD * num); switch(cmd) { case GPIO_SET_PIN: if (arg < 32) { Reg[GPIO_SET_OFFSET] = 1u << arg; } break; case GPIO_SET_ALL_PIN: Reg[GPIO_SET_OFFSET] = arg; break; case GPIO_CLR_PIN: if (arg < 32) { Reg[GPIO_CLR_OFFSET] = 1u << arg; } break; case GPIO_CLR_ALL_PIN: Reg[GPIO_CLR_OFFSET] = arg; break; case GPIO_SET_PIN_OUT: if (arg < 32) { Reg[GPIO_DIR_OFFSET] |= 1u << arg; } break; case GPIO_SET_PIN_IN: if (arg < 32) { Reg[GPIO_DIR_OFFSET] &= ~(1u << arg); } break; case GPIO_SET_MULTI_PIN_OUT: Reg[GPIO_DIR_OFFSET] |= arg;
break;
case GPIO_SET_MULTI_PIN_IN: Reg[GPIO_DIR_OFFSET] &= ~arg;
break;
case GPIO_READ_PORT: if (!access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
if (!access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
temp1 = GPIO_PIN_ERR; get_user(temp2, (u32 *)arg); if (temp2 < 32) { temp1 = Reg[GPIO_SET_OFFSET] & (1u << temp2); if (temp1 != 0) { temp1 = GPIO_PIN_HIGH; } else { temp1 = GPIO_PIN_LOW; } } put_user(temp1, (u32 *)arg); break; case GPIO_READ_ALL_PORT: if (!access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
if (!access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
temp1 = Reg[GPIO_SET_OFFSET]; put_user(temp1, (u32 *)arg); break; case GPIO_READ_PIN: if (!access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
if (!access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
temp1 = GPIO_PIN_ERR; get_user(temp2, (u32 *)arg); if (temp2 < 32) { temp1 = Reg[GPIO_PIN_OFFSET] & (1u << temp2); if (temp1 != 0) { temp1 = GPIO_PIN_HIGH; } else { temp1 = GPIO_PIN_LOW; } } put_user(temp1, (u32 *)arg); break; case GPIO_READ_ALL_PIN: if (!access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
if (!access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)))
{
return -EFAULT; }
temp1 = Reg[GPIO_PIN_OFFSET]; put_user(temp1, (u32 *)arg); break; default: return -ENOTTY; break; } return 0;}/*********************************************************************************************************
** Function name: gpio_init
** Descriptions: init driver
** Input:none
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2005-4-20
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int gpio_init(void){ int result; result = register_chrdev(MAJOR_NR, DEVICE_NAME, &gpio_fops); if (result < 0) { printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n", MAJOR_NR ); return(result); }
if (MAJOR_NR == 0) { MAJOR_NR = result; /* dynamic */ }
printk(KERN_INFO DEVICE_NAME ": init OK\n"); return(0); }/*********************************************************************************************************
** Function name: gpio_cleanup
** Descriptions: exit driver
** Input:none
** Output none
** Created by: Chenmingji
** Created Date: 2005-4-20
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void gpio_cleanup(void){ unregister_chrdev(MAJOR_NR, DEVICE_NAME);}/*********************************************************************************************************** End Of File********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -