基于STM32MP157的Linux内核按键驱动开发实践
1 GPIO操作核心流程
GPIO操作包含四个关键步骤:
- 激活外设时钟门控
- 配置引脚复用功能
- 设置端口方向模式
- 读取输入寄存器状态
2 STM32MP157按键硬件配置
2.1 硬件特性
开发板配置双按键电路,采用低电平待机设计,按键按下时输出高电平。对应GPIO引脚为PG2和PG3。
2.2 寄存器配置流程
- 时钟使能配置
通过RCC_MP_AHB4ENSETR寄存器(偏移0xA28)使能GPIOG模块,设置第6位为1 - 模式配置
配置GPIOx_MODER寄存器,PG2对应MODER[5:4],PG3对应MODER[7:6],设置为00选择输入模式 - 状态读取
通过GPIOx_IDR寄存器获取引脚电平状态
3 驱动程序实现
3.1 核心驱动模块
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/io.h>
#define DEVICE_COUNT 2
struct button_dev {
struct cdev cdev;
int minor;
};
static dev_t dev_num;
static struct class *button_class;
static struct button_dev *button_devices;
static int button_open(struct inode *inode, struct file *file)
{
int minor = iminor(inode);
// 初始化硬件配置
hw_init(minor);
return 0;
}
static ssize_t button_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
int minor = iminor(file->f_inode);
char level = read_gpio(minor);
copy_to_user(buf, &level, 1);
return 1;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
};
static int __init button_drv_init(void)
{
alloc_chrdev_region(&dev_num, 0, DEVICE_COUNT, "stm32_button");
button_class = class_create(THIS_MODULE, "stm32_button");
button_devices = kzalloc(sizeof(struct button_dev)*DEVICE_COUNT, GFP_KERNEL);
for(int i=0; i<DEVICE_COUNT; i++) {
cdev_init(&button_devices[i].cdev, &fops);
cdev_add(&button_devices[i].cdev, MKDEV(MAJOR(dev_num), i), 1);
device_create(button_class, NULL, MKDEV(MAJOR(dev_num), i), NULL, "button%d", i);
}
return 0;
}
static void __exit button_drv_exit(void)
{
for(int i=0; i<DEVICE_COUNT; i++) {
device_destroy(button_class, MKDEV(MAJOR(dev_num), i));
}
class_unregister(button_class);
class_destroy(button_class);
unregister_chrdev_region(dev_num, DEVICE_COUNT);
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
3.2 硬件抽象层实现
struct gpio_regs {
volatile u32 moder; // 0x00
volatile u32 otyper; // 0x04
volatile u32 ospeedr; // 0x08
volatile u32 pupdr; // 0x0C
volatile u32 idr; // 0x10
volatile u32 odr; // 0x14
volatile u32 bsrr; // 0x18
volatile u32 afr[2]; // 0x20
};
static void __iomem *rcc_base;
static void __iomem *gpio_base;
static void hw_init(int which)
{
if(!rcc_base) {
rcc_base = ioremap(0x50000000 + 0xA28, 0x1000);
gpio_base = ioremap(0x50008000, sizeof(struct gpio_regs));
}
// 使能GPIOG时钟
writel(readl(rcc_base + 0x0) | (1<<6), rcc_base + 0x0);
// 配置输入模式
if(which == 0) {
// PG3配置
u32 moder_val = readl(gpio_base + 0x0);
moder_val &= ~(0x3 << 6);
writel(moder_val, gpio_base + 0x0);
} else {
// PG2配置
u32 moder_val = readl(gpio_base + 0x0);
moder_val &= ~(0x3 << 4);
writel(moder_val, gpio_base + 0x0);
}
}
static int read_gpio(int which)
{
u32 data = readl(gpio_base + 0x10);
return (which == 0) ? ((data & (1<<3)) ? 1 : 0) : ((data & (1<<2)) ? 1 : 0);
}
4 测试验证
执行以下测试步骤:
- 挂载NFS文件系统:
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs/ /mnt - 加载驱动模块:
insmod button_drv.ko
insmod button_hw.ko - 执行测试程序:
./button_test /dev/button0
./button_test /dev/button1
测试结果验证:按键按下时返回1,释放时返回0