Android驱动开发实例(控制LED灯)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Android驱动例子(LED灯控制)

本例子,讲述在Android2.1上完全自已开发一个驱动去控制硬件口并写应用测试该驱动,通过这样一个例子,解析android下的驱动开发流程的应用调用流程,可以说是很好的入门引导

要达到的效果:通过Android的应用,调用驱动程序,在开发板上控制4个LED的亮灭。

一、硬件原理

如上图,通过4个IO口控制这LED,低电平LED亮,

这4个IO口分别是GPM1, GPM2, GPM3, GPM4,

二、驱动程序

1、在kernel文件夹下的driver目录,新键驱动文件夹

# cd kernel_Android_2.6.28.6/drivers

进到开发板的kernel目录,建驱动文件夹

#mkdir ledtest

2、在/driver/ledtest目录下,新建leddriver.c ,leddriver.h , Kconfig, Makefile 等4个文件leddriver.c

leddriver.c

1.#include

2.#include

3.#include

4.#include /* For __init/__exit/... */

5.#include

6.#include

7.#include

8.#include

9.#include

10.#include

11.#include

12.#include

13.#include

14.#include

15.#include

16.#include

17.#include//for register_chrdev()

18.#include

19.#include

20.#include"leddriver.h"

21.#include /* For MODULE_ALIAS_MISCDEV

22.(WATCHDOG_MINOR) */

23.#include /* For the watchdog specific items */

24.#include /* For file operations */

25.#define Viberator_MAJOR 97 //?÷éè±?o?

26.#define SCULL_NR_DEVS 4

27.#define SCULL_QUANTUM 4000

28.#define SCULL_QSET 1000

29.//---do as the GIO driver

30.#define DEVCOUNT 4

31.#define GIO_MINOR 2 /* GIO minor no. */

32.static dev_t dev; //éê ?? μ?μ??÷ éè±? o?

33.static struct cdev *cdev_p;

34.static int openCnt;

35.//--è±?á?------------

36.int VIB_major = 97;//we asigment it for test

37.int VIB_minor = 0;

38.int VIB_nr_devs = SCULL_NR_DEVS;

39.int VIB_quantum = SCULL_QUANTUM;

40.int VIB_qset = SCULL_QSET;

41.

42.static struct class *vib_dev_class;

43.#define GPNCON S3C64XX_GPNCON

44.#define GPNDAT S3C64XX_GPNDAT

45.#define GPNPUD S3C64XX_GPNPUD

46.#define GPMCON S3C64XX_GPMCON

47.#define GPMDAT S3C64XX_GPMDAT

48.#define GPMPUD S3C64XX_GPMPUD

49.

50.#define VIB_ON 0x11

51.#define VIB_OFF 0x22

52.static const struct file_operations GPIO_Viberator_ctl_ops={

53. .owner = THIS_MODULE,

54. .open = GPIO_VIB_open,

55. .read =GPIO_VIB_read,

56. .write =GPIO_VIB_write,

57. .ioctl = GPIO_VIB_ioctl,

58. .release =GPIO_VIB_release,

59.};

60.ssize_t GPIO_VIB_read(struct file * file,char * buf,size_t count,loff_t * f_op

s)

61.{

62. printk(" GPIO_VIB_read \r\n");

63. gpio_direction_output(S3C64XX_GPM(3), 0);//

64.return count ;

65.}

66.ssize_t GPIO_VIB_write (struct file * file,const char * buf, size_t count,loff

_t * f_ops)

67.{

68. printk(" GPIO_VIB_write \r\n");

69. gpio_direction_output(S3C64XX_GPM(3), 1);//

70.return count;

71.}

72.

73.//ssize_t GPIO_VIB_ioctl(struct inode * inode,struct file * file,unsigned int

cmd, long data)

74.static int GPIO_VIB_ioctl(struct inode *inode, struct file *file, unsigned int

cmd, unsigned long arg)

75.{

76. printk(KERN_ERR"VIB:GPIO_VIB_ioctl --CMD=%x \n",cmd);

77.switch(cmd)

78. {

79.case VIB_ON:

80. gpio_direction_output(S3C64XX_GPM(1), 0);//

81. gpio_direction_output(S3C64XX_GPM(2), 0);//

82. gpio_direction_output(S3C64XX_GPM(3), 0);//

83. gpio_direction_output(S3C64XX_GPM(4), 1);//

84. printk(KERN_ERR"VIB:GPIO_VIB_ioctl --VIB_ON\n");

相关文档
最新文档