Преглед изворни кода

1、Makefile指定客户的教程编译工具链。2、修复light_ring的bug

qidong.liu пре 1 месец
родитељ
комит
b2a859f52e
3 измењених фајлова са 141 додато и 85 уклоњено
  1. 15 7
      Makefile
  2. 18 0
      Makefile_ubuntu
  3. 108 78
      light_ring.c

+ 15 - 7
Makefile

@@ -1,18 +1,26 @@
-CROSS_COMPILE=arm-poky-linux-gnueabi-  
- 
 
-obj-m := coral.o
+ 
+MODULE_NAME := opal
+obj-m := opal.o
 
-coral-objs := main.o led.o light_ring.o ssegment.o ec_version.o buzzer.o fan.o writeprotect.o myname.o cash_drawers.o batteryled.o watchdog.o power.o switches.o backlight.o gsensor.o lcd_2x20.o smart_battery.o
+opal-objs := main.o led.o light_ring.o ssegment.o ec_version.o buzzer.o fan.o writeprotect.o myname.o cash_drawers.o batteryled.o watchdog.o power.o switches.o backlight.o gsensor.o lcd_2x20.o smart_battery.o
 
 
-KERNELDIR := /lib/modules/$(shell uname -r)/build
+KERNELDIR := ~/timesys/SDK64Bit-V6_02_00/kernel-source/linux-5.15/
 ccflags-y += -I./include
 EXTRA_CFLAGS +=-g -I$(PWD)/include
+
+INSTALL_DIR := /lib/modules/$(shell uname -r)/extra
  
 PWD = $(shell pwd)
 all:
-	make -C $(KERNELDIR) M=$(PWD) modules
+	make CROSS_COMPILE=~/timesys/SDK64Bit-V6_02_00/toolchain/bin/x86_64-timesys-linux-gnu- -C $(KERNELDIR) M=$(PWD) modules
 clean:
 	rm -rf *.o
-	rm *.ko *.order *.mod.c *.symvers
+	rm *.ko *.order *.mod.c *.symvers \.*\.o\.cmd
+
+module_install:
+	@echo "Installing $(MODULE_NAME).ko to $(INSTALL_DIR)"
+	@sudo mkdir -p $(INSTALL_DIR)
+	@sudo cp $(MODULE_NAME).ko $(INSTALL_DIR)/
+	@echo "Module installed successfully"

+ 18 - 0
Makefile_ubuntu

@@ -0,0 +1,18 @@
+CROSS_COMPILE=arm-poky-linux-gnueabi-  
+ 
+
+obj-m := opal.o
+
+opal-objs := main.o led.o light_ring.o ssegment.o ec_version.o buzzer.o fan.o writeprotect.o myname.o cash_drawers.o batteryled.o watchdog.o power.o switches.o backlight.o gsensor.o lcd_2x20.o smart_battery.o
+
+
+KERNELDIR := /lib/modules/$(shell uname -r)/build
+ccflags-y += -I./include
+EXTRA_CFLAGS +=-g -I$(PWD)/include
+ 
+PWD = $(shell pwd)
+all:
+	make -C $(KERNELDIR) M=$(PWD) modules
+clean:
+	rm -rf *.o
+	rm *.ko *.order *.mod.c *.symvers

+ 108 - 78
light_ring.c

@@ -97,6 +97,7 @@ struct lightring_data
     u32 max_fade_brightness;
     u32 mode;
     u32 flash_time;
+    u32 brightness_index;
 };
 
 struct light_ring_i2c_dev
@@ -113,13 +114,26 @@ struct light_ring_i2c_dev
     struct lightring_data *lightring; 
     struct delayed_work delay_work1;
 };
+
+static const u8 gamma_to_pwm[101] = {
+    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
+    0,   0,   0,   0,   0,   0,   0,   0,   0,   1,
+    1,   1,   1,   1,   1,   1,   1,   1,   1,   1,
+    1,   2,   2,   2,   2,   2,   2,   2,   2,   3,
+    3,   3,   3,   3,   4,   4,   4,   4,   5,   5,
+    5,   5,   6,   6,   6,   7,   7,   7,   8,   8,
+    9,   9,  10,  10,  11,  11,  12,  12,  13,  14,
+    14,  15,  16,  17,  17,  18,  19,  20,  21,  22,
+    23,  24,  25,  26,  27,  29,  30,  31,  33,  34,
+    35,  37,  39,  40,  42,  44,  46,  48,  50,  52,
+   100
+};
 int set_color(unsigned int color);
 static struct light_ring_i2c_dev *global_dev = NULL;
 
 static void delay_work_func(struct work_struct *work)
 {
     static int flag = 0;
-    printk("this is delay_work_func !\n");
     if(global_dev->lightring->flash_time != 0)
     {
         if(global_dev->lightring->mode == LIGHT_MODE_FLASH_1SEC || \
@@ -141,11 +155,12 @@ static void delay_work_func(struct work_struct *work)
                 global_dev->lightring->mode == LIGHT_MODE_FLASH_MEDIUM || \
                 global_dev->lightring->mode == LIGHT_MODE_FLASH_FAST)
         {
-            if(global_dev->lightring->brightness >= 100)
+            if(global_dev->lightring->brightness_index >= 100)
             {
-                global_dev->lightring->brightness = 1;
+                global_dev->lightring->brightness_index = 0;
             }
-            global_dev->lightring->brightness += global_dev->lightring->brightness;
+            global_dev->lightring->brightness_index++;
+            global_dev->lightring->brightness = gamma_to_pwm[global_dev->lightring->brightness_index];
             if(global_dev->lightring->brightness >= 100)
             {
                 global_dev->lightring->brightness = 100;
@@ -203,7 +218,7 @@ int set_color(unsigned int color)
     }
     if(ret != 0)
     {
-        printk("set color failed\n");
+        pr_err("set color failed\n");
         return -1;
     }
     return 0;
@@ -224,20 +239,18 @@ static ssize_t brightness_store(struct kobject *kobj, struct kobj_attribute *att
     ret = kstrtou32(buf, 10, &val);
     if (ret < 0)
     {
-        printk("Lightring: brightness format error\n");
+        pr_err("Lightring: brightness format error\n");
         return ret;
     }
 
     if(val > 100)
     {
-        printk("Lightring: brightness out of range\n");
+        pr_err("Lightring: brightness out of range\n");
         return -1;
     }
     global_dev->lightring->brightness = val;
     set_color(global_dev->lightring->color);
 
-    pr_info("Lightring: brightness set to %u\n", val);
-
     return count;
 }
 
@@ -247,8 +260,37 @@ static struct kobj_attribute brightness_attr =
 /* ==================== color ==================== */
 static ssize_t color_show(struct kobject *kobj, struct kobj_attribute *attr,
                           char *buf)
-{
-    return sprintf(buf, "0x%06x\n", global_dev->lightring->color);
+{    const char *mode_str;
+
+    switch (global_dev->lightring->color)
+    {
+    case COLOR_WHITE:
+        mode_str = "white";
+        break;
+    case COLOR_RED:
+        mode_str = "red";
+        break;
+    case COLOR_GREEN:
+        mode_str = "green";
+        break;
+    case COLOR_BLUE:
+        mode_str = "blue";
+        break;
+    case COLOR_FUCHSIA:
+        mode_str = "fuchsia";
+        break;
+    case COLOR_YELLOW:
+        mode_str = "yellow";
+        break;
+    case COLOR_CYAN:
+        mode_str = "cyan";
+        break;
+    default:
+        mode_str = "white";
+        break;
+    }
+
+    return sprintf(buf, "%s\n", mode_str);
 }
 
 static ssize_t color_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -257,44 +299,37 @@ static ssize_t color_store(struct kobject *kobj, struct kobj_attribute *attr,
     u32 val;
     int ret;
 
-    if (strncmp(buf, "White", 5) == 0)
+    if (strncmp(buf, "White", 5) == 0 || strncmp(buf, "white", 5) == 0)
     {
-        printk("Lightring: color set to White\n");
         val = COLOR_WHITE;
     }
-    else if (strncmp(buf, "Red", 3) == 0)
+    else if (strncmp(buf, "Red", 3) == 0 || strncmp(buf, "red", 3) == 0)
     {
-        printk("Lightring: color set to Red\n");
         val = COLOR_RED;
     }
-    else if (strncmp(buf, "Green", 5) == 0)
+    else if (strncmp(buf, "Green", 5) == 0 || strncmp(buf, "green", 5) == 0)
     {
-        printk("Lightring: color set to Green\n");
         val = COLOR_GREEN;
     }
-    else if (strncmp(buf, "Blue", 4) == 0)
+    else if (strncmp(buf, "Blue", 4) == 0 || strncmp(buf, "blue", 4) == 0)
     {
-        printk("Lightring: color set to Blue\n");
         val = COLOR_BLUE;
     }
-    else if (strncmp(buf, "Fuchsia", 7) == 0)
+    else if (strncmp(buf, "Fuchsia", 7) == 0 || strncmp(buf, "fuchsia", 7) == 0)
     {
-        printk("Lightring: color set to Fuchsia\n");
         val = COLOR_FUCHSIA;
     }
-    else if (strncmp(buf, "Yellow", 6) == 0)
+    else if (strncmp(buf, "Yellow", 6) == 0 || strncmp(buf, "yellow", 6) == 0)
     {
-        printk("Lightring: color set to Yellow\n");
         val = COLOR_YELLOW;
     }
-    else if (strncmp(buf, "Cyan", 4) == 0)
+    else if (strncmp(buf, "Cyan", 4) == 0 || strncmp(buf, "cyan", 4) == 0)
     {
-        printk("Lightring: color set to Cyan\n");
         val = COLOR_CYAN;
     }
     else
     {
-        printk("Lightring: color format error\n");
+        pr_err("Lightring: color format error\n");
         return -1;
     }
 
@@ -307,8 +342,6 @@ static ssize_t color_store(struct kobject *kobj, struct kobj_attribute *attr,
         return ret;
     }
 
-    pr_info("Lightring: color set to 0x%06x\n", val);
-
     return count;
 }
 
@@ -335,7 +368,6 @@ static ssize_t max_fade_brightness_store(struct kobject *kobj,
         return ret;
 
     global_dev->lightring->max_fade_brightness = val;
-    pr_info("Lightring: max_fade_brightness set to %u\n", val);
 
     return count;
 }
@@ -352,31 +384,41 @@ static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr,
 
     switch (global_dev->lightring->mode)
     {
-    case 0:
-        mode_str = "static";
+    case LIGHT_MODE_OFF:
+        mode_str = "off";
+        break;
+    case LIGHT_MODE_ON:
+        mode_str = "on";
+        break;
+    case LIGHT_MODE_FLASH_1SEC:
+        mode_str = "flash_1sec";
+        break;
+    case LIGHT_MODE_FLASH_2SEC:
+        mode_str = "flash_2sec";
+        break;
+    case LIGHT_MODE_FLASH_3SEC:
+        mode_str = "flash_3sec";
         break;
-    case 1:
-        mode_str = "breathing";
+    case LIGHT_MODE_FLASH_SLOW:
+        mode_str = "fade_slow";
         break;
-    case 2:
-        mode_str = "rainbow";
+    case LIGHT_MODE_FLASH_MEDIUM:
+        mode_str = "fade_medium";
         break;
-    case 3:
-        mode_str = "fade";
+    case LIGHT_MODE_FLASH_FAST:
+        mode_str = "fade_fast";
         break;
     default:
-        mode_str = "unknown";
+        mode_str = "on";
         break;
     }
 
-    return sprintf(buf, "%s (%u)\n", mode_str, global_dev->lightring->mode);
+    return sprintf(buf, "%s\n", mode_str);
 }
 
 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
                           const char *buf, size_t count)
 {
-    u32 val;
-    int ret;
     int mode = 0;
     int time = 0;
 
@@ -384,55 +426,57 @@ static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
 
     if (strncmp(buf, "off", 3) == 0)
     {
-        printk("Lightring: mode set to off\n");
         mode = LIGHT_MODE_OFF;
         time = 0;
+        global_dev->lightring->brightness = 100;
+        set_color(0);
     }
     else if (strncmp(buf, "on", 2) == 0)
     {
-        printk("Lightring: mode set to on\n");
         mode = LIGHT_MODE_ON;
         time = 0;
+        global_dev->lightring->brightness = 100;
+        set_color(global_dev->lightring->color);
     }
     else if (strncmp(buf, "flash_1sec", 10) == 0)
     {
-        printk("Lightring: mode set to flash_1sec\n");
         mode = LIGHT_MODE_FLASH_1SEC;
         time = 1000;
+        global_dev->lightring->brightness = 100;
     }
     else if (strncmp(buf, "flash_2sec", 10) == 0)
     {
-        printk("Lightring: mode set to flash_2sec\n");
         mode = LIGHT_MODE_FLASH_2SEC;
         time = 2000;
+        global_dev->lightring->brightness = 100;
     }
     else if (strncmp(buf, "flash_3sec", 10) == 0)
     {
-        printk("Lightring: mode set to flash_3sec\n");
         mode = LIGHT_MODE_FLASH_3SEC;
         time = 3000;
+        global_dev->lightring->brightness = 100;
     }
     else if (strncmp(buf, "fade_slow", 9) == 0)
     {
-        printk("Lightring: mode set to flash_slow\n");
         mode = LIGHT_MODE_FLASH_SLOW;
-        time = 400;
+        time = 60;
+        global_dev->lightring->brightness_index = 0;
     }
     else if (strncmp(buf, "fade_medium", 11) == 0)
     {
-        printk("Lightring: mode set to flash_medium\n");
         mode = LIGHT_MODE_FLASH_MEDIUM;
-        time = 200;
+        time = 40;
+        global_dev->lightring->brightness_index = 0;
     }
     else if (strncmp(buf, "fade_fast", 9) == 0)
     {
-        printk("Lightring: mode set to flash_fast\n");
         mode = LIGHT_MODE_FLASH_FAST;
-        time = 50;
+        time = 20;
+        global_dev->lightring->brightness_index = 0;
     }
     else
     {
-        printk("Lightring: mode format error\n");
+        pr_err("Lightring: mode format error\n");
         return -1;
     }
 
@@ -442,9 +486,6 @@ static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
     {
         schedule_delayed_work(&global_dev->delay_work1, msecs_to_jiffies(global_dev->lightring->flash_time));
     }
-
-    pr_info("global_dev->lightring: mode set to %u :flash_time=%d\n", global_dev->lightring->mode, global_dev->lightring->flash_time);
-
     return count;
 }
 
@@ -470,8 +511,9 @@ static struct kobject *lightring_kobj;
 static struct i2c_adapter *find_i2c_adapter_by_pci(struct pci_dev *pdev)
 {
     struct i2c_adapter *adapter = NULL;
-    struct device *dev;
     struct fwnode_handle *fwnode;
+    struct device_link *link;
+    int i;
 
     if (!pdev)
         return NULL;
@@ -483,7 +525,6 @@ static struct i2c_adapter *find_i2c_adapter_by_pci(struct pci_dev *pdev)
         return NULL;
     }
 
-    struct device_link *link;
 
     list_for_each_entry(link, &pdev->dev.links.consumers, s_node)
     {
@@ -503,12 +544,10 @@ static struct i2c_adapter *find_i2c_adapter_by_pci(struct pci_dev *pdev)
     if (adapter)
     {
         i2c_put_adapter(adapter);               
-        adapter = i2c_get_adapter(adapter->nr); 
-        printk("i2c_get_adapter\n");
+        adapter = i2c_get_adapter(adapter->nr);
         return adapter;
     }
 
-    int i;
     for (i = 0; i < 255; i++)
     {
         adapter = i2c_get_adapter(i);
@@ -536,7 +575,6 @@ static struct pci_dev *init_pci_device(void)
         return NULL;
     }
 
-    pr_info("Found PCI device: %04x:%04x\n", pdev->vendor, pdev->device);
     return pdev;
 }
 
@@ -607,32 +645,29 @@ void init_pca9685(void)
 
     ret |= i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE2, MODE2_OUTDRV | MODE2_OCH);
     ret |= i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, MODE1_RESTART | MODE1_AI | MODE1_ALLCALL);
-    printk("init_pca9685: %d\n", ret);
 
     ret = pca9685_set_pwm_freq(50);
     if (ret != 0)
     {
-        printk("pca9685_set_pwm_freq failed: %d\n", ret);
+        pr_err("pca9685_set_pwm_freq failed: %d\n", ret);
     }
     ret = pca9685_all_off();
     if (ret != 0)
     {
-        printk("pca9685_all_off failed: %d\n", ret);
+        pr_err("pca9685_all_off failed: %d\n", ret);
     }
 
     ret = pca9685_set_open_drain(true);
     if (ret != 0)
     {
-        printk("pca9685_set_open_drain failed: %d\n", ret);
+        pr_err("pca9685_set_open_drain failed: %d\n", ret);
     }
 }
 int light_ring_init(void)
 {
     int ret;
-    struct i2c_board_info info;
     struct light_ring_i2c_dev *dev;
 
-    pr_info("light_ring_i2c_dev initializing...\n");
 
     dev = kzalloc(sizeof(struct light_ring_i2c_dev), GFP_KERNEL);
     if (!dev)
@@ -660,7 +695,6 @@ int light_ring_init(void)
     }
 
     dev->bus_number = dev->adapter->nr;
-    pr_info("Found I2C adapter on bus %d\n", dev->bus_number);
 
     if (!i2c_check_functionality(dev->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA |
                                                    I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
@@ -668,10 +702,6 @@ int light_ring_init(void)
         dev_err(&dev->adapter->dev, "Adapter does not support required SMBus features\n");
         return -ENOTSUPP; 
     }
-    else
-    {
-        pr_info("Adapter supports required SMBus features\n");
-    }
 
     dev->client.adapter = dev->adapter;      
     dev->client.addr = PCA9685_DEFAULT_ADDR; 
@@ -689,10 +719,11 @@ int light_ring_init(void)
         goto err_put_pci;
     }
 
-    global_dev->lightring->brightness = 60;
-    global_dev->lightring->color = 0xFFFFFF;
-    global_dev->lightring->max_fade_brightness = 255;
-    global_dev->lightring->mode = 0; /* static */
+    global_dev->lightring->brightness = 100;
+    global_dev->lightring->color = COLOR_WHITE;
+    global_dev->lightring->max_fade_brightness = 0;
+    global_dev->lightring->mode = LIGHT_MODE_ON; /* static */
+    set_color(global_dev->lightring->color);
 
 
     lightring_kobj = kobject_create_and_add("lightring", vfiec_kobj);
@@ -734,7 +765,6 @@ void light_ring_exit(void)
     kobject_put(lightring_kobj);
     // kobject_put(vfiec_kobj);
     kfree(global_dev->lightring);
-    printk(KERN_INFO "LED module unloaded\n");
     if (dev->adapter)
         i2c_put_adapter(dev->adapter);