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

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);


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值