Преглед на файлове

Merge pull request #287 from grissiom/device-fix

Device fix
Bernard Xiong преди 11 години
родител
ревизия
bab1763003
променени са 2 файла, в които са добавени 12 реда и са изтрити 9 реда
  1. 5 4
      components/finsh/cmd.c
  2. 7 5
      src/device.c

+ 5 - 4
components/finsh/cmd.c

@@ -471,17 +471,18 @@ static long _list_device(struct rt_list_node *list)
         "Unknown"
     };
 
-    rt_kprintf("device    type      \n");
-    rt_kprintf("-------- ---------- \n");
+    rt_kprintf("device   type                 ref count\n");
+    rt_kprintf("-------- -------------------- ----------\n");
     for (node = list->next; node != list; node = node->next)
     {
         device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
-        rt_kprintf("%-8.*s %-8s \n",
+        rt_kprintf("%-8.*s %-20s %-8d\n",
                    RT_NAME_MAX,
                    device->parent.name,
                    (device->type <= RT_Device_Class_Unknown) ?
                    device_type_str[device->type] :
-                   device_type_str[RT_Device_Class_Unknown]);
+                   device_type_str[RT_Device_Class_Unknown],
+                   device->ref_count);
     }
 
     return 0;

+ 7 - 5
src/device.c

@@ -239,11 +239,6 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
         return -RT_EBUSY;
     }
 
-    dev->ref_count++;
-    /* don't let bad things happen silently. If you are bitten by this assert,
-     * please set the ref_count to a bigger type. */
-    RT_ASSERT(dev->ref_count != 0);
-
     /* call device open interface */
     if (dev->open != RT_NULL)
     {
@@ -252,8 +247,15 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
 
     /* set open flag */
     if (result == RT_EOK || result == -RT_ENOSYS)
+    {
         dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
 
+        dev->ref_count++;
+        /* don't let bad things happen silently. If you are bitten by this assert,
+         * please set the ref_count to a bigger type. */
+        RT_ASSERT(dev->ref_count != 0);
+    }
+
     return result;
 }
 RTM_EXPORT(rt_device_open);