您的位置:首页 > 其它

基于OK6410的WIFI四驱小车(上)

2016-05-18 08:59 344 查看
mygpio.h文件:

#define MYGPNIO_MAGIC 'K'

#define UP _IO(MYGPNIO_MAGIC,1)

#define BACK _IO(MYGPNIO_MAGIC,0)

#define STOP _IO(MYGPNIO_MAGIC,2)

mygpio.c文件,GPIO驱动

#include <linux/module.h>

#include <linux/init.h>

#include <linux/cdev.h>

#include <linux/fs.h>

#include <linux/io.h>

#include <linux/kernel.h>

#include <linux/device.h>

#include <mach/gpio-bank-k.h>

#include "mygpio.h"

#define GPCCON 0x7f008040 //GPC0、1、2、3、4、5 、6、7八个端口 底板(GUSERIO 6、5、3、4、8、10、9、7端口) 对应左上、左下、右上、右下电机

#define GPCDAT 0x7f008044

unsigned int *gpc_config;

unsigned int *gpc_data;

struct cdev cdev;

dev_t devno;

int mygpio_open(struct inode *node, struct file *filp)

{

unsigned int tmp;

gpc_config=ioremap(GPCCON,4); //GPN地址映射

tmp=readl(gpc_config);

tmp=(tmp&(0x00000000U))|(0x11111111U);

writel(tmp,gpc_config); //设置端口模式 输出模式

gpc_data=ioremap(GPCDAT,4);

return 0;

}

/*

1 GPC0、GPC1为左上电机 GPC0=1 GPC1=0时,电机正转; GPC0=0 GPC1=1 电机反转;GPC0=0 GPC1=0 不转

2 GPC2、GPN3为左下电机 GPC2=1 GPC3=0时,电机正转; GPC2=0 GPC3=1 电机反转;GPC2=0 GPC3=0 不转

3 GPC4、GPC5为右上电机 GPC4=1 GPC5=0时,电机正转; GPC4=0 GPC5=1 电机反转;GPC4=0 GPC5=0 不转

4 GPC6、GPC7为右下电机 GPC6=1 GPC7=0时,电机正转; GPC6=0 GPC7=1 电机反转;GPC6=0 GPC7=0 不转

*/

/*

cmd:命令 UP:前进 BACK:后退

arg:对应的轮子 1、2、3、4

功能:单独控制每一个小车轮子的前进后退、停止

*/

long mygpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

{

unsigned int tmp;

if(arg>4)

{

return -EINVAL;

}

switch(cmd)

{

case UP: //UP =1

tmp=readl(gpc_data);

tmp &=~(1<<(2*arg-1));
//电机正转

//printk("UP tmp:%x\n",tmp);

tmp |=((!cmd) << (arg*2-1)); //1、3、5、7位为0 ;0、2、4、6位为1 轮子正转

tmp &=~(1<<(2*arg-2));

tmp |=((cmd) << (arg*2-2));

printk("<4>""UP tmp:%x\n",tmp);

writel(tmp,gpc_data);

return 0;

case BACK: //BACK=0

tmp=readl(gpc_data); //电机反转

tmp &=~(1<<(2*arg-1));

printk("<4>""BACK tmp:%x\n",tmp);

tmp |=((0x00000001U) << (arg*2-1));

printk("<4>""BACK tmp:%x\n",tmp);

tmp &=~(1<<(2*arg-2));

printk("<4>""BACK tmp:%x\n",tmp);

tmp |=((cmd) << (arg*2-2));

writel(tmp,gpc_data);

printk("<4>""BACK tmp:%x\n",tmp);

return 0;

case STOP: //关闭相应的电机 STOP=3

tmp=readl(gpc_data);

tmp &=~(1<<(2*arg-1));

tmp &=~(1<<(2*arg-2));

writel(tmp,gpc_data);

printk("<4>""STOP tmp:%x\n",tmp);

return 0;

default:

return -EINVAL;

}

}

static struct file_operations mygpio_fops=

{

.open=mygpio_open,

.unlocked_ioctl=mygpio_ioctl,

};

struct class *mygpio_class;

struct class_device *mygpio_class_dev;

int major;

static int mygpio_init()

{

//cdev_init(&cdev,&mygpio_fops);

//alloc_chrdev_region(&devno,0,1,"mygpio");

//cdev_add(&cdev,devno,1);

major=register_chrdev(0,"mygpio",&mygpio_fops); //注册file_operations

mygpio_class=class_create(THIS_MODULE,"mygpio"); //创建类

mygpio_class_dev=device_create(mygpio_class,NULL,MKDEV(major,1),NULL,"mygpio");

//根据类创建主设备号为major,次设备号为1,名为“mygpio”的设备文件

return 0;

}

static void mygpio_exit()

{

//cdev_del(&cdev);

//unregister_chrdev_region(devno,1);

unregister_chrdev(major,"mygpio");//卸载注册的file_operations

device_unregister(mygpio_class_dev);//删除设备文件节点

class_destroy(mygpio_class);//删除类

}

MODULE_LICENSE("GPL");

module_init(mygpio_init);

module_exit(mygpio_exit);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: