ソースを参照

add buzzer module

liu qidong [url ssh://qidong.liu@10.2.90.253:29418/] 1 ヶ月 前
コミット
fd5fff7a59
8 ファイル変更532 行追加1 行削除
  1. 1 1
      Makefile
  2. 351 0
      buzzer.c
  3. 6 0
      buzzer.h
  4. 6 0
      include/gpioregs.h
  5. 3 0
      main.c
  6. 99 0
      test_app/Beep_userspace.c
  7. 6 0
      test_app/build.sh
  8. 60 0
      test_app/test_beep.c

+ 1 - 1
Makefile

@@ -3,7 +3,7 @@ CROSS_COMPILE=arm-poky-linux-gnueabi-
 #也可以同时编译多个模块  obj-m += export_symbol.o export_symbol1.o export_symbol2.o
 obj-m := coral.o
 
-coral-objs := main.o led.o light_ring.o ssegment.o ev_version.o
+coral-objs := main.o led.o light_ring.o ssegment.o ev_version.o buzzer.o
 
 KERNELDIR := /lib/modules/$(shell uname -r)/build
 ccflags-y += -I./include

+ 351 - 0
buzzer.c

@@ -0,0 +1,351 @@
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/cdev.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+#include <linux/device.h>
+#include <linux/stat.h>
+
+#include <linux/sched.h>
+#include <linux/timer.h>
+#include <linux/workqueue.h>
+
+#include <linux/io.h>
+#include "gpioregs.h"
+
+#define DEVICE_NAME "buzzer"
+#define CLASS_NAME "buzzer_class"
+#define DRIVER_NAME     "buzzer_driver"
+#define BUFFER_SIZE 1024
+
+#define note(x) ((119318200+(x)/2)/(x))
+
+// 可以通过模块参数指定主设备号和次设备号
+static int buzzer_major = 56; // 默认主设备号
+static int buzzer_minor = 100;   // 默认次设备号
+module_param(buzzer_major, int, S_IRUGO);
+module_param(buzzer_minor, int, S_IRUGO);
+MODULE_PARM_DESC(buzzer_major, "Major device number");
+MODULE_PARM_DESC(buzzer_minor, "Minor device number");
+
+static struct class *char_class = NULL;
+static struct device *char_device = NULL;
+static struct cdev my_cdev;
+static dev_t dev_num;
+
+// 设备结构体
+struct buzzer_dev
+{
+    char *buffer;
+    size_t size;
+    struct mutex lock;
+    struct cdev cdev;
+    struct delayed_work delay_work1;
+};
+
+static struct buzzer_dev *dev = NULL;
+
+void IoWrite8 (uint16_t addr, uint8_t data)
+{
+    outb(data,addr);
+}
+
+uint8_t IoRead8 (uint16_t addr)
+{
+    return inb(addr);
+}
+
+void BeepOff(void)
+{
+	IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) & 0xfc);
+}
+
+void BeepOn(uint16_t freq)
+{
+	uint16_t Frequency = note(freq);
+	//set up channel 1 (used for delays)
+	IoWrite8(BUZZER_CHANNEL, 0x54);
+	IoWrite8(BUZZER_CHAN_1, 0x12);
+	//set up channel 2 (used by speaker)
+	IoWrite8(BUZZER_CHANNEL, 0xb6);
+	IoWrite8(BUZZER_FREQ, (uint8_t)Frequency);
+	IoWrite8(BUZZER_FREQ, (uint8_t)(Frequency >> 8));
+	//turn the speaker on
+	IoWrite8(BUZZER_PORT, IoRead8(BUZZER_PORT) | 3);
+}
+static void delay_work_func(struct work_struct *work)
+{
+    int status_flag = 0;
+    int freq = 0;
+    int duration = 0;
+    if(dev->size < 4)
+    {
+        printk(KERN_INFO "buzzer: size < 4\n");
+        return;
+    }
+    // 关闭buzzer
+    BeepOff();
+    printk(KERN_INFO "delay_work_func\n");
+}
+
+// 文件打开操作
+static int buzzer_open(struct inode *inode, struct file *filp)
+{
+    struct buzzer_dev *dev;
+
+    // if (!request_region(PORT_80, 1, DRIVER_NAME))
+    // {
+    //     pr_err("Port 80 I/O region busy\n");
+    //     return -EBUSY;
+    // }
+
+    dev = container_of(inode->i_cdev, struct buzzer_dev, cdev);
+    filp->private_data = dev;
+
+    printk(KERN_INFO "buzzer: Device opened (major=%d, minor=%d)\n",
+           imajor(inode), iminor(inode));
+    return 0;
+}
+
+// 文件释放操作
+static int buzzer_release(struct inode *inode, struct file *filp)
+{
+    // release_region(PORT_80, 1);
+    printk(KERN_INFO "buzzer: Device closed\n");
+    return 0;
+}
+
+// 读操作 - 支持cat命令
+static ssize_t buzzer_read(struct file *filp, char __user *buf,
+                             size_t count, loff_t *f_pos)
+{
+    struct buzzer_dev *dev = filp->private_data;
+    ssize_t retval = 0;
+    size_t available;
+    int read_count = 0;
+    int ret = 0;
+
+    if (mutex_lock_interruptible(&dev->lock))
+        return -ERESTARTSYS;
+
+    // 这里read_count没有固定为4个字节,是为了便于后续扩展
+    if (count > dev->size)
+    {
+        read_count = dev->size;
+    }
+    else
+    {
+        read_count = count;
+    }
+    ret = copy_to_user(buf, dev->buffer, read_count);
+    if (ret != 0)
+    {
+        printk(KERN_INFO "buzzer: copy_to_user failed\n");
+        goto out;
+    }
+
+    printk(KERN_INFO "buzzer: Read %zu bytes\n", count);
+
+out:
+    mutex_unlock(&dev->lock);
+    return ret;
+}
+
+// 写操作
+static ssize_t buzzer_write(struct file *filp, const char __user *buf,
+                              size_t count, loff_t *f_pos)
+{
+    struct buzzer_dev *dev = filp->private_data;
+    ssize_t retval = 0;
+    size_t available;
+    int ret = 0;
+    int status_flag = 0;
+    int freq = 0;
+    int duration = 0;    
+
+    // 加锁
+    if (mutex_lock_interruptible(&dev->lock))
+    {
+        return -ERESTARTSYS;
+    }
+
+    // 计算可写数据量
+    if (count > BUFFER_SIZE)
+    {
+        count = BUFFER_SIZE;
+    }
+
+    if(count < 4)
+    {
+        printk(KERN_INFO "buzzer: count < 4\n");
+        ret = -1;
+        goto out;
+    }
+
+    // 拷贝数据
+    ret = copy_from_user(dev->buffer, buf, count);
+    if (ret != 0)
+    {
+        printk(KERN_INFO "buzzer: copy_from_user failed\n");
+        goto out;
+    }
+
+    dev->size = count;
+
+    // 解析数据
+    status_flag = dev->buffer[0];
+    freq = dev->buffer[2]<<8 + dev->buffer[1];
+    duration = dev->buffer[3];
+
+    // 打开buzzer
+    if(status_flag == 1)
+    {
+        BeepOn(freq);
+    }
+    else
+    {
+        BeepOff();
+    }
+
+    printk(KERN_INFO "buzzer: Written %zu bytes\n", count);
+
+out:
+    mutex_unlock(&dev->lock);
+
+    // 让buzzer工作duration秒
+    if (ret == 0)
+    {
+        schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(duration*1000));
+    }
+    return count;
+}
+
+// 文件操作结构体
+static struct file_operations fops = {
+    .owner = THIS_MODULE,
+    .open = buzzer_open,
+    .release = buzzer_release,
+    .read = buzzer_read,
+    .write = buzzer_write,
+};
+
+// 模块初始化
+int buzzer_init(void)
+{
+    int result;
+
+    printk(KERN_INFO "buzzer: Initializing driver with major=%d, minor=%d\n",
+           buzzer_major, buzzer_minor);
+
+    // 检查主设备号是否有效
+    if (buzzer_major <= 0)
+    {
+        printk(KERN_ALERT "buzzer: Invalid major number %d\n", buzzer_major);
+        return -EINVAL;
+    }
+
+    // 构建设备号
+    dev_num = MKDEV(buzzer_major, buzzer_minor);
+
+    // 注册设备号 - 使用指定的主设备号
+    result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
+    if (result < 0)
+    {
+        printk(KERN_ALERT "buzzer: Failed to register major number %d\n", buzzer_major);
+        printk(KERN_ALERT "buzzer: Try using a different major number\n");
+        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)
+    {
+        result = -ENOMEM;
+        goto fail_malloc;
+    }
+    memset(dev, 0, sizeof(struct buzzer_dev));
+
+    // 分配缓冲区
+    dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
+    if (!dev->buffer)
+    {
+        result = -ENOMEM;
+        goto fail_buffer;
+    }
+
+    // 初始化互斥锁
+    mutex_init(&dev->lock);
+
+    INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
+
+    // 初始化字符设备
+    cdev_init(&dev->cdev, &fops);
+    dev->cdev.owner = THIS_MODULE;
+
+    // 添加字符设备到系统
+    result = cdev_add(&dev->cdev, dev_num, 1);
+    if (result)
+    {
+        printk(KERN_ALERT "buzzer: Failed to add cdev\n");
+        goto fail_cdev;
+    }
+
+    // 创建设备类
+    char_class = class_create(THIS_MODULE, CLASS_NAME);
+    if (IS_ERR(char_class))
+    {
+        result = PTR_ERR(char_class);
+        printk(KERN_ALERT "buzzer: Failed to create class\n");
+        goto fail_class;
+    }
+
+    // 创建设备
+    char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
+    if (IS_ERR(char_device))
+    {
+        result = PTR_ERR(char_device);
+        printk(KERN_ALERT "buzzer: Failed to create device\n");
+        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:
+    class_destroy(char_class);
+fail_class:
+    cdev_del(&dev->cdev);
+fail_cdev:
+    kfree(dev->buffer);
+fail_buffer:
+    kfree(dev);
+fail_malloc:
+    unregister_chrdev_region(dev_num, 1);
+    return result;
+}
+
+// 模块退出
+void buzzer_exit(void)
+{
+    cancel_delayed_work_sync(&dev->delay_work1);
+    device_destroy(char_class, dev_num);
+    class_destroy(char_class);
+
+    if (dev)
+    {
+        cdev_del(&dev->cdev);
+        if (dev->buffer)
+            kfree(dev->buffer);
+        kfree(dev);
+    }
+
+    unregister_chrdev_region(dev_num, 1);
+    printk(KERN_INFO "buzzer: Driver removed (major=%d, minor=%d)\n",
+           buzzer_major, buzzer_minor);
+}

+ 6 - 0
buzzer.h

@@ -0,0 +1,6 @@
+#ifndef __BUZZER_H__
+#define __BUZZER_H__
+
+void buzzer_init(void);
+void buzzer_exit(void);
+#endif

+ 6 - 0
include/gpioregs.h

@@ -3,4 +3,10 @@
 
 // ssegment display
 #define PORT_80 0x80
+
+// buzzer
+#define BUZZER_PORT 0x61
+#define BUZZER_CHANNEL 0x43
+#define BUZZER_CHAN_1   0X41
+#define BUZZER_FREQ   0X42
 #endif

+ 3 - 0
main.c

@@ -20,6 +20,7 @@
 #include "led.h"
 #include "ssegment.h"
 #include "ec_version.h"
+#include "buzzer.h"
 
 struct kobject *vfiec_kobj = NULL;
 
@@ -33,6 +34,7 @@ static int __init all_driver_init(void)
         ret = -ENOMEM;
     }
 
+    buzzer_init();
     ec_version_init();
     light_ring_init();
     led_init();
@@ -44,6 +46,7 @@ static int __init all_driver_init(void)
 
 static void __exit all_driver_exit(void)
 {
+    buzzer_exit();
     ec_version_exit();
     ssegment_exit();
     led_exit();

+ 99 - 0
test_app/Beep_userspace.c

@@ -0,0 +1,99 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <stdint.h>
+#include <sys/io.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+
+typedef unsigned char         UINT8;
+typedef unsigned short        UINT16;
+
+#define note(x) ((119318200+(x)/2)/(x))
+
+void printfHelp(char *argv0)
+{
+	printf("Ver: 20221223\n\n");
+	printf("<%s> <times>\n", argv0);
+	printf("For Example:\n");
+	printf("%s 4  \n", argv0);
+	printf("\n%s -h:for help! \n", argv0);
+}
+
+UINT8 IoRead8 (UINT16 addr)
+{
+    return inb(addr);
+}
+
+void IoWrite8 (UINT16  addr, UINT8  data)
+{
+    outb(data,addr);
+}
+
+void DelayMs(int ms)
+{
+	unsigned int i = 0, n = 656.36*ms;
+	for (i = 0; i < n; i++)
+		IoRead8(0x0b);
+}
+
+void BeepOn(UINT8 note, UINT8 octave)
+{
+	// beep tones
+	UINT16 tones[8] = { note(26163),note(29366),note(32963),note(34923),note(39200),note(44000),note(49388),note(26163 * 2) };
+	UINT16 Frequency = tones[(note % 8)];
+
+	if (octave - 1 >= 0)
+		Frequency >>= octave - 1;
+	else
+		Frequency <<= 1 - octave;
+
+	//set up channel 1 (used for delays)
+	IoWrite8(0x43, 0x54);
+	IoWrite8(0x41, 0x12);
+	//set up channel 2 (used by speaker)
+	IoWrite8(0x43, 0xb6);
+	IoWrite8(0x42, (UINT8)Frequency);
+	IoWrite8(0x42, (UINT8)(Frequency >> 8));
+	//turn the speaker on
+	IoWrite8(0x61, IoRead8(0x61) | 3);
+}
+
+void BeepOff(void)
+{
+	IoWrite8(0x61, IoRead8(0x61) & 0xfc);
+}
+
+void Beep(UINT8 note, UINT8 octave, unsigned int duration)
+{
+	BeepOn(note, octave);
+	DelayMs(duration);
+	BeepOff();
+}
+
+int main(int argc, char* argv[])
+{
+    int i = 0;
+    UINT8 mytimes;
+
+    if (argv[1] == NULL || !strcmp(argv[1], "-h"))
+	{
+		printfHelp(argv[0]);
+		return 0;
+	}
+
+    if (iopl(3) == -1) {
+    fprintf(stderr, "Failed to set IOPL: %s\nRun with sudo!\n", strerror(errno));
+    return EXIT_FAILURE;
+    }
+
+	mytimes = (UINT8)strtoul(argv[1], NULL, 10);
+	for (i = 0; i < mytimes; i++)
+	{
+        Beep(6, 1, 120);
+		DelayMs(120);
+	}    
+}

+ 6 - 0
test_app/build.sh

@@ -0,0 +1,6 @@
+#!/bin/bash
+
+gcc Beep_userspace.c -o Beep_userspace
+gcc setss.c -o setss
+gcc test_beep.c -o test_beep
+gcc test_ssegment.c -o test_ssegment

+ 60 - 0
test_app/test_beep.c

@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+    int flag = 0;
+    int freq = 0;
+    int duration = 0;
+    unsigned char buf[4];
+    int fd = 0;
+    int ret = 0;
+    if(argc != 4)
+    {
+        printf("Usage: %s on 2 2400\n", argv[0]);
+        return 1;
+    }
+
+    if(strcmp(argv[1], "on") == 0)
+    {
+        flag = 1;
+    }
+    else if(strcmp(argv[1], "off") == 0)
+    {
+        flag = 0;
+    }
+    else
+    {
+        printf("Usage: %s on 2 2400\n", argv[0]);
+        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);
+    if(fd < 0)
+    {
+        printf("open /dev/buzzer failed\n");
+        return 1;
+    }
+
+    ret = write(fd, buf, sizeof(buf));
+    if(ret < 0)
+    {
+        printf("write /dev/buzzer failed\n");
+        return 1;
+    }
+    close(fd);
+}