24_GPIO读

itop4412学习记录

本章的目的: 掌握gpio的申请、配置、取值和释放。

(1)gpio申请:gpio_request
(2)gpio配置-设置为输入:s3c_gpio_cfgpin(EXYNOS4_GPC0(3),S3C_GPIO_INPUT); 
(3)gpio配置-设置为非上下拉:s3c_gpio_setpull(EXYNOS4_GPC0(3),S3C_GPIO_PULL_NONE);
(4)gpio取值:gpio_get_value
(5)gpio释放:gpio_free(EXYNOS4_GPC0(3));

1. 驱动设计思路

1.1 输入IO选取

利用拨码开关来实现GPIO的输入,通过原理图理清楚拨码开关的3和4对应的IO编号:

管脚编号 → GPIO编号 → 对应GPIO虚拟地址宏
AP_SLEEP→GPC0_3→EXYNOS4_GPC0(3)    
XEINT6→GPX0_6→EXYNOS4_GPX0(6)
    
开关3和4,向内是低电平,输入0。向外是高电平1.8v,输入1。

开关3和4对应IO口需要做的准备: 1.设置为输入;2.不上拉,不下拉;3. 读寄存器
 

1.2 GPIO配置函数

(1)gpio申请:
gpio_request(EXYNOS4_GPC0(3),"GPC0_3");
gpio_request(EXYNOS4_GPX0(6),"GPX0_6");
(2)gpio配置-设置为输入:
s3c_gpio_cfgpin(EXYNOS4_GPC0(3),S3C_GPIO_INPUT); 
s3c_gpio_cfgpin(EXYNOS4_GPX0(6),S3C_GPIO_INPUT);
(3)gpio配置-设置为非上下拉:
s3c_gpio_setpull(EXYNOS4_GPC0(3),S3C_GPIO_PULL_NONE);
s3c_gpio_setpull(EXYNOS4_GPX0(6),S3C_GPIO_PULL_NONE);
(4)gpio取值:
gpio_get_value(EXYNOS4_GPC0(3));
gpio_get_value(EXYNOS4_GPX0(6));
(5)gpio释放:
gpio_free(EXYNOS4_GPC0(3));
gpio_free(EXYNOS4_GPX0(6));

1.3 设备注册

方法一: 在4412平台文件注册设备结构体:

iTop4412_Kernel_3.0/arch/arm/mach-exynos/mach-itop4412.c

    struct platform_device s3c_device_read_gpio_ctl = {
        .name   = "read_gpio_ctl",
        .id     = -1,
    };
     &s3c_device_read_gpio_ctl,

具体参考:06_设备注册

方法二:使用platform_device_register注册设备

#define DRIVER_NAME "gpio_read"
#define DEVICE_NAME "gpio_read_dev"

static int gpio_read_dev_release(struct platform_device *dev)
{
	printk("\tgpio read device.dec.release!!\n");
}

static struct platform_device s3c_device_gpio_read_dev = {
        .name   = DRIVER_NAME,
        .id             = -1,
		.dev = {
			.release = gpio_read_dev_release,
	}

};


Devicestate = platform_device_register(&s3c_device_gpio_read_dev);

参考:linux以module方式注册设备和注册驱动、以及杂项设备

推荐使用方法二,因为通用性强。

2. 代码调试

2.1 代码的编译

模块编译命令:make

应用程序编译命令:arm-none-linux-gnueabi-gcc -o user_read_gpio user_read_gpio.c

2.2 代码调试

(1)加载模块,查看设备节点

[root@iTOP-4412]# insmod gpio_read.ko
[  454.284761]  start gpio init!
[  454.309519] device state is 0 
[  454.311541]  gpio initialized
[  454.371292]  gpio init OK,DriverState is 0!
通过命令 ls /dev,可以发现设备节点:gpio_read_dev。

(2)运行程序程序gpio_read_dev

命令格式:./gpio_read_dev cmd

cmd=0时,读EXYNOS4_GPC0(3)

cmd=1时,读EXYNOS4_GPX0(6)

将开关4接地:

[root@iTOP-4412]# ./user_read_gpio 1
[  466.340700] gpio open
[  466.341593] cmd is 1,arg is 0
[  466.345198] gpio release
cmd is 1,gpio_read is 0

将开关4接1.8V:

[root@iTOP-4412]# ./user_read_gpio 1
[  503.834045] gpio open
[  503.834934] cmd is 1,arg is 0
[  503.838668] gpio release
cmd is 1,gpio_read is 1

3. 驱动代码

(1)gpio_read.c

#include <linux/init.h>
#include <linux/module.h>

/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>
/*注册杂项设备头文件*/
#include <linux/miscdevice.h>
/*注册设备节点的文件结构体*/
#include <linux/fs.h>

/*Linux中申请GPIO的头文件*/
#include <linux/gpio.h>
/*三星平台的GPIO配置函数头文件*/
/*三星平台EXYNOS系列平台,GPIO配置参数宏定义头文件*/
#include <plat/gpio-cfg.h>
#include <mach/gpio.h>

#define DRIVER_NAME "gpio_read"
#define DEVICE_NAME "gpio_read_dev"

static long gpio_ioctl( struct file *files, unsigned int cmd, unsigned long arg){
	printk("cmd is %d,arg is %d\n",cmd,arg);
	if(cmd > 1){
		printk(KERN_EMERG "cmd is 0 or 1\n");
	}
	if(arg > 1){
		printk(KERN_EMERG "arg is only 1\n");
	}
	
	if(cmd==0)
		return gpio_get_value(EXYNOS4_GPC0(3));
	if(cmd==1)
		return gpio_get_value(EXYNOS4_GPX0(6));
}

static int gpio_release(struct inode *inode, struct file *file){
	printk(KERN_EMERG "gpio release\n");
	return 0;
}

static int gpio_open(struct inode *inode, struct file *file){
	printk(KERN_EMERG "gpio open\n");
	return 0;
}

static struct file_operations gpio_ops = {
	.owner = THIS_MODULE,
	.open = gpio_open,
	.release = gpio_release,
	.unlocked_ioctl = gpio_ioctl,
};

static  struct miscdevice gpio_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DEVICE_NAME,
	.fops = &gpio_ops,
};

static int gpio_probe(struct platform_device *pdv){
	int ret;	
	printk(KERN_EMERG "\tgpio initialized\n");
	
	ret = gpio_request(EXYNOS4_GPC0(3),"GPC0_3");	
	if(ret < 0){
		printk(KERN_EMERG "gpio_request EXYNOS4_GPC0(3) failed!\n");
		return ret;
	}
	
	ret = gpio_request(EXYNOS4_GPX0(6),"GPX0_6");	
	if(ret < 0){
		printk(KERN_EMERG "gpio_request EXYNOS4_GPX0(6) failed!\n");
		return ret;
	}	
	
	s3c_gpio_cfgpin(EXYNOS4_GPC0(3),S3C_GPIO_INPUT);	
	s3c_gpio_setpull(EXYNOS4_GPC0(3),S3C_GPIO_PULL_NONE);
	
	s3c_gpio_cfgpin(EXYNOS4_GPX0(6),S3C_GPIO_INPUT);	
	s3c_gpio_setpull(EXYNOS4_GPX0(6),S3C_GPIO_PULL_NONE);
	
	misc_register(&gpio_dev);	
	return 0;
}

static int gpio_remove(struct platform_device *pdv){
	printk(KERN_EMERG "\tgpio removed!\n");
	misc_deregister(&gpio_dev);
	return 0;
}

static void gpio_shutdown(struct platform_device *pdv){
	;
}

static int gpio_suspend(struct platform_device *pdv,pm_message_t pmt){
	return 0;
}

static int gpio_resume(struct platform_device *pdv){
	return 0;
}
static int gpio_read_dev_release(struct platform_device *dev)
{
	printk("\tgpio read device.dec.release!!\n");
}

static struct platform_device s3c_device_gpio_read_dev = {
        .name   = DRIVER_NAME,
        .id             = -1,
		.dev = {
			.release = gpio_read_dev_release,
	}

};


struct platform_driver gpio_driver = {
	.probe = gpio_probe,
	.remove = gpio_remove,
	.shutdown = gpio_shutdown,
	.suspend = gpio_suspend,
	.resume = gpio_resume,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	}
};

static int gpio_init(void)
{
	int DriverState=0,Devicestate=0;	
	printk(KERN_EMERG "\tstart gpio init!\n");
	
	Devicestate = platform_device_register(&s3c_device_gpio_read_dev);
	printk(KERN_EMERG "device state is %d \n",Devicestate);
	
	DriverState = platform_driver_register(&gpio_driver);
	if(DriverState==0)
	{
		printk(KERN_EMERG "\tgpio init OK,DriverState is %d!\n",DriverState);
	}	
	
	return 0;
}

static void gpio_exit(void)
{
	printk(KERN_EMERG "gpio module exit!\n");
	platform_device_unregister(&s3c_device_gpio_read_dev);	
	platform_driver_unregister(&gpio_driver);	
	gpio_free(EXYNOS4_GPC0(3));
	gpio_free(EXYNOS4_GPX0(6));
}
module_init(gpio_init);
module_exit(gpio_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

(2)user_read_gpio.c

#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int agc,char **argv){
	int fd,cmd;
	char *hello_node = "/dev/gpio_read_dev";
	
	if(strcmp(argv[1],"0")==0) 
		cmd=0;
	if(strcmp(argv[1],"1")==0) 
		cmd=1;
	if(cmd > 1)printf("cmd must be 0 or 1!\n");
	
/*O_RDWR只读打开,O_NDELAY非阻塞方式*/	
if((cmd ==0)|(cmd ==1)){
	if((fd = open(hello_node,O_RDWR|O_NDELAY))<0){
		printf("APP open %s failed\n",hello_node);
	}
	else{
		printf("cmd is %d,gpio_read is %d\n",cmd,ioctl(fd,cmd,0));
		
	}
	close(fd);
}
	
}

(3)Makefile

#!/bin/bash
obj-m += gpio_read.o
KDIR := /home/topeet/iTop4412_Kernel_3.0
PWD ?= $(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules
clean:
	rm -rf *.o

4. 收获

学会使用函数platform_device_register在模块中注册设备

  

好的,我会为您解答。 首先,我们需要在代码中添加对按键的识别和处理。您可以使用STM32的HAL库中的GPIO功能来实现。具体来说,您需要定义四个按键的GPIO引脚和按键状态变量,并在while循环中不断地取按键状态,根据按键状态来调节时间。下面是一份示例代码: ``` #include "main.h" #include "stdio.h" /* Define GPIO pins for buttons */ #define BUTTON1_Pin GPIO_PIN_0 #define BUTTON1_GPIO_Port GPIOA #define BUTTON2_Pin GPIO_PIN_1 #define BUTTON2_GPIO_Port GPIOA #define BUTTON3_Pin GPIO_PIN_2 #define BUTTON3_GPIO_Port GPIOA #define BUTTON4_Pin GPIO_PIN_3 #define BUTTON4_GPIO_Port GPIOA /* Define variables for button states */ uint8_t button1_state = 0; uint8_t button2_state = 0; uint8_t button3_state = 0; uint8_t button4_state = 0; /* Function to handle button press */ void handle_button_press(uint8_t button_number) { /* TODO: Implement time adjustment logic here */ } int main(void) { /* Initialize HAL library */ HAL_Init(); /* Initialize RTC */ /* TODO: Add RTC initialization code here */ /* Initialize GPIO pins for buttons */ __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = BUTTON1_Pin | BUTTON2_Pin | BUTTON3_Pin | BUTTON4_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Main loop */ while (1) { /* Read button states */ button1_state = HAL_GPIO_ReadPin(BUTTON1_GPIO_Port, BUTTON1_Pin); button2_state = HAL_GPIO_ReadPin(BUTTON2_GPIO_Port, BUTTON2_Pin); button3_state = HAL_GPIO_ReadPin(BUTTON3_GPIO_Port, BUTTON3_Pin); button4_state = HAL_GPIO_ReadPin(BUTTON4_GPIO_Port, BUTTON4_Pin); /* Handle button presses */ if (button1_state == GPIO_PIN_RESET) { handle_button_press(1); } if (button2_state == GPIO_PIN_RESET) { handle_button_press(2); } if (button3_state == GPIO_PIN_RESET) { handle_button_press(3); } if (button4_state == GPIO_PIN_RESET) { handle_button_press(4); } /* Toggle LED */ HAL_GPIO_TogglePin(GPIOF, LED_Pin); /* Update time and date on LCD */ HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); sprintf(time_str, "%02d:%02d:%02d", sTime.Hours, sTime.Minutes, sTime.Seconds); sprintf(date_str, "20%02d-%02d-%02d %d", sDate.Year, sDate.Month, sDate.Date, sDate.WeekDay); LCD_ShowString(10, 30, tftlcd_data.width, tftlcd_data.height, 24, date_str); LCD_ShowString(10, 60, tftlcd_data.width, tftlcd_data.height, 24, time_str); /* Delay for 500ms */ delay_ms(500); } } ``` 在上面的代码中,我们定义了四个按键的GPIO引脚和按键状态变量。在while循环中,我们不断地取按键状态,并调用handle_button_press函数来处理按键事件。在handle_button_press函数中,您需要实现时间调节逻辑。例如,如果用户按下第一个按键,则您可以递增当前时间的小时数,等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snaking616

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值