手柄"/>
v3S移植nes手柄
文章目录
- 一、前言
- 二、FC手柄介绍
- 三、硬件连接
- 四、修改设备树
- 五、编写驱动程序
- 六、运行
一、前言
在上一节我们移植了nes游戏,但是没有手柄不能操作,这只能看不能玩着实无趣。
在家里翻出来了小时候玩的游戏机的手柄,这里移植过来玩。
二、FC手柄介绍
先看一下手柄原型
感觉还挺不错的,哈哈
图片中可以看出,小霸王游戏机的手柄接口从外形上类似我们常说的DB9接口,但是其引脚定义与引脚功能与DB9接口差别较大。
游戏中是高电平有效
三、硬件连接
从上面我们可以知道驱动这个FC手柄需要3个IO,我从自己的开发板选取3个IO进行连接。
选取的是这三个IO。
手柄连接是通过一个DB9的公头,然后用洞洞板连接的。
STM32连接图:
linux连接图:
四、修改设备树
在sun8i-v3s-licheepi-zero-dock.dts中添加
pio节点下添加:
uart1_pins_a: uart1_pins_a@0 {pins = "PE21", "PE22";function = "gpio_out";};data_pins: data_pins@0 {pins = "PB5";function = "gpio_in";bias-pull-up;};
根节点下添加:
handle{compatible = "luatao-handle";#address-cells = <1>;#size-cells = <0>;pinctrl-names = "default";pinctrl-0 = <&data_pins &uart1_pins_a>; // 表示使用pinctrl_key这个节点的pin信息 load_gpios = <&pio 4 22 GPIO_ACTIVE_LOW>; /* E22 */ // load RX1clk_gpios = <&pio 4 21 GPIO_ACTIVE_LOW>; /* PE21 */ // clk TX1data_gpios = <&pio 1 5 GPIO_ACTIVE_HIGH>; /* PB5 */ // data 默认高电平status = "okay";};
五、编写驱动程序
移植的led的驱动,好多描述没改(懒得改,凑合用吧)
handle.c
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>#include <linux/cdev.h>#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>#include <linux/of_gpio.h>/*** file name:gpioled* date: 2021-09-04 10:43* version:1.0* author:luatao* describe:gpioLed device drive*/#define GPIOLED_CNT 1 /* 设备号个数 */
#define GPIOLED_NAME "joypad" /* 设备名*//* 设备结构体 自定义 */
struct gpioled_dev{dev_t devid; /*设备号 */struct cdev cdev; /* cdev */struct class *class; /* 类*/struct device *device; /* 设备 */int major; /* 主设备号 */int minor; /* 次设备号 */struct device_node *nd; /* 设备节点 */int data_gpio; /* data所使用的GPIO编号 */int clk_gpio; /* clk所使用的GPIO编号 */int load_gpio; /* load所使用的GPIO编号 */};/* 定义一个设备结构体 */
struct gpioled_dev gpioled; /* led 设备 *//* 读取手柄的值 */
ssize_t Handle_Get_Data(void)
{int i;ssize_t Data=0;gpio_set_value(gpioled.load_gpio, 1);//将P1-P8的数据锁存到移位寄存器(Q1-Q8)中udelay(2);gpio_set_value(gpioled.load_gpio, 0);//设置成串行输出模式// Data = gpio_get_value(gpioled.data_gpio); //保存手柄数据,先将Q8保存// gpio_set_value(gpioled.clk_gpio, 0);// mdelay(5);for(i=0;i<8;i++){udelay(2);// Data<<=1; if(!gpio_get_value(gpioled.data_gpio)){Data |= 1<<i;}gpio_set_value(gpioled.clk_gpio, 1);//时钟上升沿,数据移位 // Data += gpio_get_value(gpioled.data_gpio);udelay(2);gpio_set_value(gpioled.clk_gpio, 0);//等待下一次上升沿}gpio_set_value(gpioled.clk_gpio, 0);//等待下一次上升沿gpio_set_value(gpioled.load_gpio, 0);//设置成串行输出模式return Data;
}/* 打开设备 */
static int led_open(struct inode *inode, struct file *filp)
{filp->private_data = &gpioled; /* 设置私有数据 */printk("handle open!\r\n");return 0;
}/* 从设备读取数据 */
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{ssize_t val = 0;// printk("handle read !\r\n");val = Handle_Get_Data() & 0x000000ff;if(val != 255)printk("%d\r\n", val);return val; // 获取检测到的值 return 0;
}/* 往设备写数据 */
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{return 0;
}/* 释放设备 */
static int led_release(struct inode *inode, struct file *filp)
{//printk("led release!\r\n");return 0;
}/* 设备操作函数结构体 */
static struct file_operations gpioled_fops = {.owner = THIS_MODULE,.open = led_open,.read = led_read,.write = led_write,.release = led_release,
};/* 驱动入口函数 */
static int __init led_init(void)
{int ret; // 返回值/* 获取设备数中的属性数据 *//* 1. 获取设备节点 /led*/gpioled.nd = of_find_node_by_path("/handle"); // 通过绝对路径查找设备节点if(gpioled.nd == NULL){printk("handle node no find!\r\n");return -EINVAL; /* 无效参数 不知道这个返回值是啥意思,我觉得返回一个负数就可以,这个值是23,不知道有没有处理*/}/* 2. 获取设备树中的gpio属性 得到LED所使用的gpio编号 */gpioled.data_gpio = of_get_named_gpio(gpioled.nd, "data_gpios", 0);if(gpioled.data_gpio < 0 ){printk("can't get data-gpio\r\n");return -EINVAL; /* 无效参数 不知道这个返回值是啥意思,我觉得返回一个负数就可以,这个值是23,不知道有没有处理*/}gpioled.clk_gpio = of_get_named_gpio(gpioled.nd, "clk_gpios", 0);if(gpioled.clk_gpio < 0 ){printk("can't get clk-gpio\r\n");return -EINVAL; /* 无效参数 不知道这个返回值是啥意思,我觉得返回一个负数就可以,这个值是23,不知道有没有处理*/}gpioled.load_gpio = of_get_named_gpio(gpioled.nd, "load_gpios", 0);if(gpioled.load_gpio < 0 ){printk("can't get load-gpio\r\n");return -EINVAL; /* 无效参数 不知道这个返回值是啥意思,我觉得返回一个负数就可以,这个值是23,不知道有没有处理*/}printk("data-gpio num = %d \r\n", gpioled.data_gpio); // 打印获取的led-gpio属性值printk("clk-gpio num = %d \r\n", gpioled.clk_gpio); // 打印获取的led-gpio属性值printk("load-gpio num = %d \r\n", gpioled.load_gpio); // 打印获取的led-gpio属性值/* 3. 设置data-gpio为输入,clk-gpio load-gpio 设置为输出 */ret = gpio_direction_output(gpioled.clk_gpio, 0);if(ret < 0){printk("can't set clk_gpio!\r\n");}ret = gpio_direction_output(gpioled.load_gpio, 0);if(ret < 0){printk("can't set load_gpio!\r\n");}ret = gpio_direction_input(gpioled.data_gpio);if(ret < 0){printk("can't set data_gpio!\r\n");}/* 注册字符设备驱动 *//* 1. 创建设备号 */if(gpioled.major){ // 定义了设备号 gpioled.devid = MKDEV(gpioled.major, 0 ); // 根据主设备号和次设备号合成设备号 register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME); // 注册设备号 }else{ // 没有定义设备号 动态生成 alloc_chrdev_region(&gpioled.devid,0,GPIOLED_CNT, GPIOLED_NAME ); // 申请设备号gpioled.major = MAJOR(gpioled.devid); // 获取主设备号gpioled.minor = MINOR(gpioled.devid); // 获取次设备号}printk("gpioled major = %d,minor = %d\r\n",gpioled.major, gpioled.minor); // 打印主设备号和次设备号/* 2. 初始化 cdev */gpioled.cdev.owner = THIS_MODULE; cdev_init(&gpioled.cdev, &gpioled_fops); // 初始化cdev/* 3. 添加cdev */cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT ); // 向linux系统添加cdev/* 自动创建设备节点文件 *//* 4. 创建类 */gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME); // 创建类 if(IS_ERR(gpioled.class)){return PTR_ERR(gpioled.class);}/* 创建设备 */gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);if(IS_ERR(gpioled.device)){return PTR_ERR(gpioled.device);}return 0;
}/* 驱动出口函数 */
static void __exit led_exit(void)
{/* 注销字符设备驱动 */cdev_del(&gpioled.cdev); /* 删除 cdev */unregister_chrdev_region(gpioled.devid, GPIOLED_CNT ); /* 注销设备号 */device_destroy(gpioled.class, gpioled.devid); /* 注销设备 */class_destroy(gpioled.class); /* 注销类 */printk("handle drive unregsister ok !\r\n");
}/* 加载驱动入口和出口函数 */
module_init(led_init);
module_exit(led_exit);/* LICENSE 和 AUTHOR 信息*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("luatao");
六、运行
加载驱动:
将编译的handle.ko放到开发板运行
然后运行游戏:
按下对应的按键会出来对应的值。
这里不得不说一下
这个游戏的识别默认是高电平有效,也就是说,值为1,2,4,8,16,32,64,128
这个我一直弄成低电平有效了,搞了半天就是没反应,不然的话,很快就完事了。
参考链接:
nes按键
更多推荐
v3S移植nes手柄
发布评论