步骤1:同LED

步骤2:同LED

步骤3:编译

  cd workdir/driver/Linux3.2Drivers/fs210_beep_pwm/

  vim Makefile

  修改同LED。

  make

步骤4:将ko文件复制到NFS系统目录中。

  cp fs210_pwm.ko /source/rootfs

  make pwm_test

步骤5:启动开发版

  加载驱动

  insmod fs210_pwm.ko
Disabling lock debugging due to kernel taint
pwm: driver installed with major 251!
  mknod /dev/pwm c 251 0
  chmod 777 /dev/pwm
  ./pwm_test

 

运行结果

附:程序源码

  fs210_pwm.c

  1 #include <linux/module.h> 
  2 #include <linux/kernel.h>
  3 #include <linux/init.h>
  4 #include <linux/fs.h>
  5 #include <linux/cdev.h>
  6 #include <asm/io.h>
  7 //#include <plat/regs-timer.h>
  8 //#include <mach/regs-gpio.h>
  9 #include "fs210_pwm.h"
 10 
 11 
 12 MODULE_LICENSE("Dual BSD/GPL");
 13 MODULE_AUTHOR("farsight");
 14 
 15 #define GPD0CON  0xE02000A0
 16 
 17 struct pwm_timer
 18 {
 19     unsigned int tcfg0;
 20     unsigned int tcfg1;
 21     unsigned int tcon;
 22     unsigned int tcntb0;
 23     unsigned int tcmpb0;
 24     unsigned int tcnto0;
 25     unsigned int tcntb1;
 26     unsigned int tcmpb1;
 27 } *pwm_timer;
 28 
 29 static int pwm_major = 251;
 30 static int pwm_minor = 0;
 31 static struct cdev pwm_cdev;
 32 void *gpd0con;
 33 
 34 static void beep_init(void)
 35 {
 36     writel((readl(gpd0con) & (~0xf<<4)) | (0x2<<4), gpd0con);
 37     writel(readl(&pwm_timer->tcfg0) | 0xff, &pwm_timer->tcfg0);
 38     writel((readl(&pwm_timer->tcfg1) & ~(0xf<<4)) | (0x4<<4), &pwm_timer->tcfg1);
 39     writel(30, &pwm_timer->tcntb1);
 40     writel(15, &pwm_timer->tcmpb1);
 41     writel((readl(&pwm_timer->tcon) & ~(0xf<<8)) | (0x2<<8), &pwm_timer->tcon);
 42 }
 43 
 44 static void beep_on(void)
 45 {
 46     writel((readl(&pwm_timer->tcon) & ~(0xf<<8)) | (0x9<<8), &pwm_timer->tcon);
 47 }
 48 
 49 static void beep_off(void)
 50 {
 51     writel((readl(&pwm_timer->tcon) & ~(0xf<<8)), &pwm_timer->tcon);
 52 }
 53 
 54 static void set_cnt(unsigned long count)
 55 {
 56     writel(count, &pwm_timer->tcntb1);
 57     writel(count/2, &pwm_timer->tcmpb1);
 58 }
 59 
 60 static int fs210_pwm_open(struct inode *inode, struct file *file)
 61 {
 62     printk("pwm: device open\n");
 63     beep_init();
 64     return 0;
 65 }
 66 
 67 static int fs210_pwm_close(struct inode *inode, struct file *file)
 68 {
 69     printk("pwm: device close\n");
 70     beep_off();
 71     return 0;
 72 }
 73 
 74 static long fs210_pwm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 75 {
 76     //printk("pwm: device ioctl\n");
 77     switch(cmd)
 78     {
 79     case BEEP_ON:
 80         printk("pwm: BEEP ON\n");
 81         beep_on();
 82         break;
 83     case BEEP_OFF:
 84         printk("pwm: BEEP OFF\n");
 85         beep_off();
 86         break;
 87     case SET_CNT:
 88         printk("pwm: SET CNT\n");
 89         set_cnt(arg);
 90         break;
 91 
 92     default:
 93         printk("pwm: available command\n");
 94         return -EINVAL;
 95     }
 96     return 0;
 97 }
 98 
 99 static struct file_operations fs210_pwm_ops = {
100     .owner         = THIS_MODULE,
101     .open         = fs210_pwm_open,
102     .release     = fs210_pwm_close,
103     .unlocked_ioctl        = fs210_pwm_ioctl
104 };
105 
106 static int pwm_setup_cdev(struct cdev *cdev, 
107         struct file_operations *fops)
108 {
109     int result;
110 
111     dev_t devno = MKDEV(pwm_major, pwm_minor);
112     cdev_init(cdev, fops);
113     cdev->owner = THIS_MODULE;
114     result = cdev_add(cdev, devno, 1);
115     if (result)
116     {
117         printk("pwm: fail to add cdev\n");
118         return result;
119     }
120     return 0;
121 }
122 
123 static int __init fs210_pwm_init(void)
124 {
125     int result;
126     dev_t devno = MKDEV(pwm_major, pwm_minor);
127     result = register_chrdev_region(devno, 1, "fs210_pwm");
128     if (result)
129     {
130         printk("pwm: unable to get major %d\n", pwm_major);
131         return result;
132     }
133 
134     result = pwm_setup_cdev(&pwm_cdev, &fs210_pwm_ops);
135     if (result)
136         return result;
137 
138     gpd0con = ioremap(GPD0CON, 4);
139     pwm_timer = ioremap(0xE2500000, sizeof(struct pwm_timer));
140 
141     printk("pwm: driver installed with major %d!\n", pwm_major);
142     return 0;
143 }
144 
145 static void __exit fs210_pwm_exit(void)
146 {
147     dev_t devno = MKDEV(pwm_major, pwm_minor);
148     cdev_del(&pwm_cdev);
149     unregister_chrdev_region(devno, 1);
150     iounmap(gpd0con);
151     iounmap(pwm_timer);
152     printk("pwm: driver uninstalled!\n");
153 }
154 
155 module_init(fs210_pwm_init);
156 module_exit(fs210_pwm_exit);

  pwm_test.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <fcntl.h>
 5 #include <string.h>
 6 #include <sys/types.h>
 7 #include <sys/stat.h>
 8 #include <sys/ioctl.h>
 9 
10 #include "fs210_pwm.h"
11 
12 int main()
13 {
14     int i = 0;
15     int n = 2;
16     int dev_fd;
17     
18     if ((dev_fd = open("/dev/pwm",O_RDWR | O_NONBLOCK)) < 0)
19     {
20         perror("open");
21         exit(-1);
22     }
23 
24     while ( 1 )
25     {
26         ioctl(dev_fd, BEEP_ON, 0);
27         sleep(1);
28         ioctl(dev_fd, BEEP_OFF, 0);
29         sleep(1);
30     }
31 
32     return 0;
33 }

 

posted on 2018-08-06 20:52  kingofloong  阅读(907)  评论(0编辑  收藏  举报