Переглянути джерело

修复客户反馈的蜂鸣器问题

qidong.liu 1 місяць тому
батько
коміт
fd43b9beaf
3 змінених файлів з 29 додано та 20 видалено
  1. 1 0
      Makefile
  2. 14 11
      buzzer.c
  3. 14 9
      test_app/test_beep.c

+ 1 - 0
Makefile

@@ -16,6 +16,7 @@ PWD = $(shell pwd)
 all:
 	make CROSS_COMPILE=~/timesys/SDK64Bit-V6_02_00/toolchain/bin/x86_64-timesys-linux-gnu- -C $(KERNELDIR) M=$(PWD) modules
 	~/timesys/SDK64Bit-V6_02_00/toolchain/bin/x86_64-timesys-linux-gnu-gcc test_app/setss.c -DPROJECT=$(PROJECT_NAME) -o test_app/setss
+	~/timesys/SDK64Bit-V6_02_00/toolchain/bin/x86_64-timesys-linux-gnu-gcc test_app/test_beep.c -DPROJECT=$(PROJECT_NAME) -o test_app/beep
 
 clean:
 	rm -rf *.o test_app/setss

+ 14 - 11
buzzer.c

@@ -14,10 +14,15 @@
 #include <linux/io.h>
 #include "gpioregs.h"
 
+#if defined(PROJECT) && (PROJECT == POS)
+#define DEVICE_NAME "buzz0"
+#else
 #define DEVICE_NAME "buzzer"
+#endif
+
 #define CLASS_NAME "buzzer_class"
 #define DRIVER_NAME     "buzzer_driver"
-#define BUFFER_SIZE 1024
+#define BUFFER_SIZE 4
 
 #define note(x) ((119318200+(x)/2)/(x))
 
@@ -81,12 +86,10 @@ static void delay_work_func(struct work_struct *work)
     int duration = 0;
     if(dev->size < 4)
     {
-        printk(KERN_INFO "buzzer: size < 4\n");
         return;
     }
     // Close buzzer
     BeepOff();
-    printk(KERN_INFO "delay_work_func\n");
 }
 
 static int buzzer_open(struct inode *inode, struct file *filp)
@@ -110,7 +113,6 @@ static int buzzer_open(struct inode *inode, struct file *filp)
 static int buzzer_release(struct inode *inode, struct file *filp)
 {
     // release_region(PORT_80, 1);
-    printk(KERN_INFO "buzzer: Device closed\n");
     return 0;
 }
 
@@ -142,7 +144,6 @@ static ssize_t buzzer_read(struct file *filp, char __user *buf,
         goto out;
     }
 
-    printk(KERN_INFO "buzzer: Read %zu bytes\n", count);
 
 out:
     mutex_unlock(&dev->lock);
@@ -199,7 +200,6 @@ static ssize_t buzzer_write(struct file *filp, const char __user *buf,
         BeepOff();
     }
 
-    printk(KERN_INFO "buzzer: Written %zu bytes\n", count);
 
 out:
     mutex_unlock(&dev->lock);
@@ -219,6 +219,13 @@ static struct file_operations fops = {
     .write = buzzer_write,
 };
 
+static char *my_devnode(struct device *dev, umode_t *mode) {
+    if (mode) {
+        *mode = 0666;
+    }
+    return NULL;
+}
+
 int buzzer_init(void)
 {
     int result;
@@ -242,8 +249,6 @@ int buzzer_init(void)
         return result;
     }
 
-    printk(KERN_INFO "buzzer: Registered with major=%d, minor=%d\n",
-           MAJOR(dev_num), MINOR(dev_num));
 
     dev = kmalloc(sizeof(struct buzzer_dev), GFP_KERNEL);
     if (!dev)
@@ -281,6 +286,7 @@ int buzzer_init(void)
         printk(KERN_ALERT "buzzer: Failed to create class\n");
         goto fail_class;
     }
+    char_class->devnode = my_devnode;
 
     char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
     if (IS_ERR(char_device))
@@ -290,9 +296,6 @@ int buzzer_init(void)
         goto fail_device;
     }
 
-    printk(KERN_INFO "buzzer: Driver initialized successfully\n");
-    printk(KERN_INFO "buzzer: Device node: /dev/%s (major=%d, minor=%d)\n",
-           DEVICE_NAME, buzzer_major, buzzer_minor);
     return 0;
 
 fail_device:

+ 14 - 9
test_app/test_beep.c

@@ -6,23 +6,31 @@
 #include <string.h>
 #include <stdlib.h>
 
+#if defined(PROJECT) && (PROJECT == POS)
+#define DEVICE_PATH "/dev/buzz0"
+#else
+#define DEVICE_PATH "/dev/buzzer"
+#endif
+
 int main(int argc, char *argv[])
 {
-    int flag = 0;
-    int freq = 0;
-    int duration = 0;
+    unsigned int flag = 0;
+    unsigned int freq = 0;
+    unsigned int duration = 0;
     unsigned char buf[4];
     int fd = 0;
     int ret = 0;
-    if(argc != 4)
+    if(argc != 4 && argc != 2)
     {
         printf("Usage: %s on 2 2400\n", argv[0]);
         return 1;
     }
 
-    if(strcmp(argv[1], "on") == 0)
+    if(strcmp(argv[1], "on") == 0 && argc == 4)
     {
         flag = 1;
+        freq = atoi(argv[3]);
+        duration = atoi(argv[2]);
     }
     else if(strcmp(argv[1], "off") == 0)
     {
@@ -34,16 +42,13 @@ int main(int argc, char *argv[])
         return 1;
     }
 
-    freq = atoi(argv[3]);
-    duration = atoi(argv[2]);
-
     memset(buf, 0, sizeof(buf));
     buf[0] = flag;
     buf[1] = freq&0xff;
     buf[2] = (freq>>8)&0xff;
     buf[3] = duration&0xff;
 
-    fd = open("/dev/buzzer", O_RDWR);
+    fd = open(DEVICE_PATH, O_RDWR);
     if(fd < 0)
     {
         printf("open /dev/buzzer failed\n");