×
单片机 > 单片机程序设计 > 详情

Linux-2.6.32.2内核在mini2440上的移植(十六)---中断的按键驱动

发布时间:2020-06-16 发布时间:
|

移植环境

1,主机环境:VMare下CentOS 5.5 ,1G内存。

2,集成开发环境:Elipse IDE

3,编译编译环境:arm-linux-gcc v4.4.3,arm-none-linux-gnueabi-gcc v4.5.1。

4,开发板:mini2440,2M nor flash,128M nand flash。

5,u-boot版本:u-boot-2009.08

6,linux 版本:linux-2.6.32.2

7,参考文章:

嵌入式linux应用开发完全手册,韦东山,编著。

Mini2440 之Linux 移植开发实战指南

【1】硬件原理

Mini2440 具有6 个用户测试按键,它们都是连接到CPU 的中断引脚。如图:

 从图中可以看出,6 个用户按键分别对应如下CPU 资源引脚:


 按键  对应的端口寄存器 对应的中断 对应的复用功能
 K1  GPG0 EINT8 仅有GPIO和中断功能
 K2  GPG3 EINT11 nSS1
 K3  GPG5 EINT13 SPIMISO
 K4  GPG6 EINT14 SPIMOSI  
 K5  GPG7 EINT15 SPICLK
 K6  GPG11 EINT19 TCLK

   


 为何如此安排这些按键资源呢?
首先,它们都具备中断功能,因此可以直接做一些中断相关的实验,其次 GPG3,5,6,7这一组合可以形成一个全功能的SPI 接口,我们知道,有些全键盘就是通过SPI 接口扩展实现的,比如三星的公板SMDK2440 就带有这种接口的键盘,只不过它需要添加一个SPI 接口的键盘芯片来实现。所以,我们不但在开发板上直接把这些引脚接到按键上,而且还特意增加了CON12 座以方便把这些按键引出到面板使用,或者作为扩展全功能键盘的接口。这也是mini2440 精心设计的细节之一。

【2】驱动程序分析及编写

在/linux-2.6.32.2/drivers/misc目录下创建一个新的驱动程序文件mini2440_buttons.c,内容及详细注释如下:

#include

 if(ret < 0)
  {
        printk(DEVICE_NAME "register falid!\n");
        return ret;
   }
 printk (DEVICE_NAME"\tinitialized\n");
 return 0;
}
/*注销设备*/
static void __exit dev_exit(void)
{
 misc_deregister(&misc);
}
module_init(dev_init); //模块初始化,仅当使用insmod/podprobe 命令加载时有用,如果设备不是通过模块方式加载,此处将不会被调用
module_exit(dev_exit); //卸载模块,当该设备通过模块方式加载后,可以通过rmmod 命令卸载,将调用此函数
MODULE_LICENSE("GPL"); //版权信息
MODULE_AUTHOR("singleboy."); //作者名字

【3】为内核添加按键设备的内核配置选项

把按键驱动加入到内核中,打开 linux-2.6.32.2/drivers/misc/Kconfig 文件,定位到16行附近,加入如下红色部分内容:

if MISC_DEVICES

 config MINI2440_BUTTONS
  tristate "Buttons driver for FriendlyARM Mini2440 development boards"
  depends on MACH_MINI2440
  default y if MACH_MINI2440
  help
   this is buttons driver for FriendlyARM Mini2440 development boards

config LEDS_MINI2440
 tristate "LED Support for Mini2440 GPIO LEDs"
 depends on MACH_MINI2440
 default y if MACH_MINI2440
 help
  This option enables support for LEDs connected to GPIO lines
  on Mini2440 boards.

【4】对应的驱动目标文件加入内核

打开linux-2.6.32.2/drivers/misc/Makefile 文件,添加如下红色部分内容:

obj-$(CONFIG_EP93XX_PWM) += ep93xx_pwm.o
obj-$(CONFIG_C2PORT)  += c2port/
obj-$(CONFIG_MINI2440_BUTTONS) += mini2440_buttons.o
obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o
obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o
obj-y    += eeprom/
obj-y    += cb710/

【5】确认内核配置

接上面的步骤,在内核源代码目录下执行:make menuconfig 重新配置内核,依次选择进入如下子菜单项:

Device Drivers --->
    [*] Misc devices  ---> 

       

退出并保存内核配置。

然后退出保存所选配置, 在命令行执行: make uImage , 将会生成arch/arm/boot/uImage,然后将其复制到/nfsboot目录下后启动开发板。可以在看到串口终端中启动信息:

... ...

brd: module loaded
buttons     initialized!
S3C24XX NAND Driver, (c) 2004 Simtec Electronics

... ...

说明leds设备加载成功。

【6】buttons测试

为了测试该驱动程序,我们还需要编写一个简单的测试程序,在友善官方提供的光盘中已经提供了该测试程序的源代码,它位于\linux 示例代码\examples\buttons目录中,文件名为:buttons_test.c。将其复制到主机/root/linux-test/codetest目录下,下面是其中的代码:

#include

int main(void)
{
 int buttons_fd;
 char buttons[6] = {'0', '0', '0', '0', '0', '0'}; //定义按键值变量,对于驱动函数中的key_values 数组

 buttons_fd = open("/dev/buttons", 0); /*打开按键设备/dev/buttons*/
 if (buttons_fd < 0) {
  perror("open device buttons"); /*打开失败则退出*/
  exit(1);
 }

 for (;;) { /*永读按键并打印键值和状态*/
  char current_buttons[6];
  int count_of_changed_key;
  int i;

/*使用read 函数读取一组按键值(6 个)*/
  if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
   perror("read buttons:");
   exit(1);
  }

/*逐个分析读取到的按键值*/

  for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
   if (buttons[i] != current_buttons[i]) {
    buttons[i] = current_buttons[i];

/*打印按键值,并标明按键按下/抬起的状态*/
    printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down");
    count_of_changed_key++;
   }
  }
  if (count_of_changed_key) {
   printf("\n");
  }
 }

 close(buttons_fd); /*关闭按键设备文件*/
 return 0;
}

在终端中进入到codetest目录,然后执行:
[root@localhost codetest]# ls
adc_test    adc_test.c~     backlight_test.c  i2c  led.c   tstest.c
adc_test.c  backlight_test  buttons_test.c    led  tstest
[root@localhost codetest]# arm-linux-gcc -o buttons_test buttons_test.c
[root@localhost codetest]# cp buttons_test /nfsboot/nfs
[root@localhost codetest]#

将生成可执行目标文件buttons_test,复制到与开发板共享的nfsboot/nfs中,在开发板的命令行终端执行:

[root@mini2440 /]#ls -l /dev/buttons
crw-rw----    1 root     root      10,  63 Jan  1 00:00 /dev/buttons
[root@mini2440 /]#ls -l /dev/leds
crw-rw----    1 root     root      10,  62 Jan  1 00:00 /dev/leds
[root@mini2440 /]#cd /mnt/nfs
[root@mini2440 nfs]#ls
adc_test        buttons_test    test1.wav
backlight_test  i2c             tstest
bigworld.wav    led             yesterday.mp3
[root@mini2440 nfs]#./buttons_test
key 6 is down
key 6 is up
key 4 is down
key 4 is up
key 5 is down
key 5 is up
key 3 is down
key 3 is up
key 2 is down
key 2 is up
key 1 is down
key 1 is up
可以看到已经产生的动作。

接下来,将进行pwm驱动的移植。


关键字:Linux  内核  mini2440  移植 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何为单片机选择合适的负载电容