DM8168 GPIO驱动与測试程序

DM8168 GPIO驱动与測试程序

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

本次測试针对GPIO1进行,挑选了GP1[31],引脚的复用默认的就是GPIO

还是老规矩,贴上driver.c,Makefile,test.c:

dm8168_gpio.c:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h> /* copy_to_user,copy_from_user */
#include <linux/miscdevice.h>
#include <linux/device.h>
#include <asm/io.h>

static struct class  *gpio_class;

volatile unsigned long *DIR;
volatile unsigned long *DAT;

int gpio_open (struct inode *inode,struct file *filp)

{
	*DIR = 0x0;  //output
	return 0;
}

ssize_t gpio_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos)
{
	return 0;
}

ssize_t gpio_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)
{
	char val_buf[2];
	int ret;
	ret = copy_from_user(val_buf,buf,count);
		
	switch(val_buf[0])
	{
		case 0x31 :
			*DAT = 0xf0000000;  //gp1[31]
			break;
		case 0x30 :
			*DAT = 0x0;         //gp1[31]
			break;
		default :
			break;
	}
	return count;
}

struct file_operations gpio_fops =
{
	.owner   = THIS_MODULE,
	.open    = gpio_open,
	.read    = gpio_read,
	.write   = gpio_write,
} ;

int major;
int gpio_init (void)
{ 	
	
	major = register_chrdev(0,"DM8168_gpio",&gpio_fops);
	gpio_class = class_create(THIS_MODULE, "DM8168_gpio");
	device_create(gpio_class,NULL,MKDEV(major,0),NULL,"gpio");

	DIR = (volatile unsigned long *)ioremap(0x4804C134,4);
	DAT = (volatile unsigned long *)ioremap(0x4804C13C,4);

	printk ("gpio is ready\n");
	return 0;
}

void gpio_exit (void)
{
	unregister_chrdev(major,"DM8168_gpio");
	device_destroy(gpio_class,MKDEV(major,0));
	class_destroy(gpio_class);

	iounmap(DIR);
	iounmap(DAT);

	printk ("module exit\n");
	return ;
}

MODULE_LICENSE("GPL");
module_init(gpio_init);
module_exit(gpio_exit);

Makefile:

obj-m:= dm8168_gpio.o

CROSSCOMPILE := /opt/codesourcery/arm-2009q1/bin/arm-none-linux-gnueabi-

CC  := $(CROSSCOMPILE)gcc 

KDIR:=/home/***/ti-ezsdk_dm816x-evm_5_03_01_15/board-support/linux-2.6.37-psp04.00.01.13.patch2

PWD :=$(shell pwd)

default:
	$(MAKE) -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
clean:
	rm -rf *.ko *.o .*cmd *.mod.c .tmp_versions 

gpio_test:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int  fd;
	int  val=0;

	fd=open("/dev/gpio",O_RDWR);

	if(fd<0)
	{
		printf("open device failed !\n");
		exit(1);
	}
	else
	{
		printf("open success ! \n");
	}
	
	write(fd,argv[1],1);
	
	close(fd);
	return 0;
}


測试 :

模块编译后载入:insmod dm8168_gpio.ko

交叉编译測试程序:arm-none-linux-gnueabi-gcc -o gpio_test gpio_test.c

执行:./gpio_test 1

GP1[31]为3.3V

执行:./gpio_test 0

GP1[31]为0V

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118926.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Html动态点击按钮实现“+”和“-”功能

    Html动态点击按钮实现“+”和“-”功能  Html动态点击按钮实现“+”和“-”功能&lt;!DOCTYPE html&gt;&lt;html lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html;"&gt; &lt;title&gt;html动态实现加减&lt;

    2022年6月13日
    62
  • vimrc配置[通俗易懂]

    vimrc配置[通俗易懂]插件管理gitclonehttps://github.com/VundleVim/Vundle.vim.git~/.vim/bundle/Vundle.vim主题monokaihttps://github.com/sickill/vim-monokaivimrcbashrctmuxzsh/oh-my-zshz

    2022年5月1日
    29
  • jdbc fetchsize_jdbc和odbc的关系

    jdbc fetchsize_jdbc和odbc的关系DBUtilscommons-dbutils是Apache组织提供的一个开源JDBC工具类库,封装了针对于数据库的增删改查操作APIQueryRunnerResulSetHandlerDbutils插入举例Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JBBCUtils.getConnections3();

    2022年8月8日
    6
  • c语言gdi绘图_程序设计的基本步骤是

    c语言gdi绘图_程序设计的基本步骤是本文将实现对基本图形的绘制:windows程序画图,大体上有3种方法:(1)你告诉系统点的坐标和颜色,系统通过SetPixel来画。类似的,通过GetPixel来获取某一点像素值。(2)使用MoveToEx、LineTo来划线,MoveToEx设置起点坐标,LineTo设置终点坐标,或者使用Polyline函数,这个函数接受一个POINT类型的数组,通过数组里的点连线。(3)windows…

    2022年8月18日
    5
  • mysql是什么类型数据库_MySQL是一种

    mysql是什么类型数据库_MySQL是一种在学习MySQL之前,首先需要了解数据库和SQL。如果您已经知道数据库和SQL,那么可以直接跳转到下一章节的学习。1.数据库简介当您想收听最喜欢的歌曲时,可以从智能手机上打开播放列表。在这种情

    2022年8月1日
    4
  • spss中聚类分析_spss modeler聚类分析

    spss中聚类分析_spss modeler聚类分析spss基本分析总结聚类分析和判断分析聚类分析Kmeans快速聚类分析HierarchicalCluster层次聚类分析Q型聚类R型聚类当观察值个数较多或文件非常大时,应使用快速聚类分析。层次聚类在大数据下图形过于分散,不易解释。连续变量的样本距离测量方法测量方法计算公式欧式距离Chebychev距离Block距离Minkowski距离…

    2022年10月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号