在OK6410 android开发板上添加led驱动模块,并通过NDK测试驱动

步骤:

      1. 类似linux开发板,添加并测试led驱动。过程类似上篇文章所写http://blog.csdn.net/wangben50/article/details/8657180

     

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>    /*copy_to_user,copy_from_user*/
#include <linux/io.h>         /*inl(),outl()*/
#include <linux/miscdevice.h>
#include <linux/pci.h>
//#define S3C64XX_GPMCON (unsigned long)ioremap(0x7F008820,4)
//#define S3C64XX_GPMDAT (unsigned long)ioremap(0x7F008824,4)
//#define S3C64XX_GPMDAT  (unsigned long)ioremap(0x7F008828,4)

static long S3C64XX_GPMCON=0xF4500820;   /*这里是虚拟地址,物理地址可以再S3C6410数据手册上找到。也可以根据物理地址,ioremap()获得虚拟地址。*/
static long S3C64XX_GPMDAT=0xF4500824;
#define LED_MAJOR 240                    /*主设备号*/

int led_open(struct inode *inode,struct file *file)
{
	unsigned tmp;
	tmp=inl(S3C64XX_GPMCON);
	printk("the pre GPMCON is %x",tmp);
	tmp=inl(S3C64XX_GPMDAT);
	printk("the pre GPMDAT is %x",tmp);
	outl(0x00111111,S3C64XX_GPMCON);         /*向GPMCON命令端口写命令字,设置GPM0-5为output口*/
	printk("#############open#############");
	return 0;
}

static ssize_t led_read(struct file *file,char __user *buf,size_t count,loff_t * f_pos)
{
	unsigned tmp=inl(S3C64XX_GPMDAT);
       	int num=copy_to_user(buf,&tmp,count);
	if(num==0)
	   printk("copy successfully");
	else printk("sorry  copy failly");

	printk("the GPMDAT is %x.",tmp);
	return count;
}
static ssize_t led_write(struct file * file,const char __user * buf,size_t count,loff_t * f_pos)/*我是通过write()来控制LED灯的,也可以通过ioctl()来控制*/
{
	char kbuf[10];
	printk("###########write###########");
	int num=copy_from_user(kbuf,buf,count);
	if(num==0)
	     printk("copy successfully");
        else printk("sorry  copy failly");
	printk("##the kbuf is %c",kbuf[0]);
	switch(kbuf[0])
	{
		case 0://off
			outl(0xff,S3C64XX_GPMDAT);  /*拉高GPMDAT[0:5]的引脚,使LED灯灭,因为LED是低电平有电流通过*/
			break;
		case 1://on
			outl(0x00,S3C64XX_GPMDAT);  /*拉低GPMDAT[0:5]的引脚,使LED灯亮*/
			break;
		default:
			break;
	}
	return count;
}

int led_release(struct inode *inode,struct file *file)
{
	printk("#######release##########");
	return 0;
}

struct file_operations led_fops={
	.owner = THIS_MODULE,
	.open = led_open,
	.read = led_read,
	.write = led_write,
	.release = led_release,
};
int __init led_init(void)
{
	int rc;
	printk("Test led dev\n");
	rc=register_chrdev(LED_MAJOR,"led",&led_fops);
	if(rc<0)
	{
		printk("register %s char dev error\n","led");
		return -1;
	}
	printk("OK!\n");
	return 0;
}

void __exit led_exit(void)
{
	unregister_chrdev(LED_MAJOR,"led");
	printk("module exit\n");
}

module_init(led_init);
module_exit(led_exit);


编译驱动生成6410led.ko文件。把.ko文件移到开发板。

在开发板执行

/ # insmod 6410led.ko
6410led: module license 'unspecified' taints kernel.
Disabling lock debugging due to kernel taint
Test led dev
OK!
/ # lsmod
6410led 1839 0 - Live 0xbf000000 (P)
/ # mknod /dev/led c 240 0
/ # chmod 777 /dev/led

驱动模块即添加完成。

      2. 搭建android工程,配合SDK、NDK开发生成apk软件测试驱动。工程结构如图:

                                                                                                  

说明下:1 Led.java为主文件夹。

             2 NDK 比SDK 多一个文件夹jni 在此文件夹下有两个文件。一个是android专用Makefile,另一个为JNI程序。怎么搭建NDK,编译JNIC程序,这里不再讲。

             3 使用NDK编译后,在armeabi文件下生成libled_jni.so 动态库。如果你是在linux下使用NDK,而在windows下使用eclipse,直接把生成的.so文件复制到工程下即可。

                也可以在window下配合cygwin使用NDK工具。

                注意:安装完cygwin后,修改安装目录下 D:\cygwin\home\Administrator中的.bash_profile文件。在文件末尾添加 ndk-build的安装路径,在F盘的android-ndk-r8d下。添加如下:

                            ndk=/cygdrive/f/android-ndk-r8d

                            export NDKROOT

                            使用界面如:

                            

           4 此后Led.java调用libled_jni.so,libled_jni.so调用驱动。即可完成apk软件应用层调用驱动。

 

详细工程代码我上传: http://download.csdn.net/detail/wangben50/5155638
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值