Browse Source

[DeviceDriver]can add bus_hook

Aubr.Cool 10 years ago
parent
commit
7051f01f48
2 changed files with 23 additions and 8 deletions
  1. 18 6
      components/drivers/can/can.c
  2. 5 2
      components/drivers/include/drivers/can.h

+ 18 - 6
components/drivers/can/can.c

@@ -645,7 +645,11 @@ static rt_err_t rt_can_control(struct rt_device *dev,
         }
         break;
 #endif /*RT_CAN_USING_HDR*/
-
+#ifdef RT_CAN_USING_BUS_HOOK
+    case RT_CAN_CMD_SET_BUS_HOOK:
+        can->bus_hook = (rt_can_bus_hook) args;
+        break;
+#endif /*RT_CAN_USING_BUS_HOOK*/
     default :
         /* control device */
         if (can->ops->control != RT_NULL)
@@ -666,15 +670,21 @@ static void cantimeout(void *arg)
     rt_can_t can = (rt_can_t)arg;
 
     rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
-    if (can->timerinitflag == 1)
-    {
-        can->timerinitflag = 0xFF;
-    }
 
     if (can->status_indicate.ind != RT_NULL)
     {
         can->status_indicate.ind(can, can->status_indicate.args);
     }
+#ifdef RT_CAN_USING_BUS_HOOK
+    if(can->bus_hook)
+    {
+        can->bus_hook(can);
+    }
+#endif /*RT_CAN_USING_BUS_HOOK*/
+    if (can->timerinitflag == 1)
+    {
+        can->timerinitflag = 0xFF;
+    }
 }
 
 /*
@@ -699,7 +709,9 @@ rt_err_t rt_hw_can_register(struct rt_can_device *can,
     can->can_rx         = RT_NULL;
     can->can_tx         = RT_NULL;
     rt_mutex_init(&(can->lock), "can", RT_IPC_FLAG_PRIO);
-
+#ifdef RT_CAN_USING_BUS_HOOK
+    can->bus_hook       = RT_NULL;
+#endif /*RT_CAN_USING_BUS_HOOK*/
     device->init        = rt_can_init;
     device->open        = rt_can_open;
     device->close       = rt_can_close;

+ 5 - 2
components/drivers/include/drivers/can.h

@@ -139,6 +139,7 @@ struct rt_can_ops;
 #define RT_CAN_CMD_SET_PRIV         0x16
 #define RT_CAN_CMD_GET_STATUS       0x17
 #define RT_CAN_CMD_SET_STATUS_IND   0x18
+#define RT_CAN_CMD_SET_BUS_HOOK     0x19
 
 #define RT_DEVICE_CAN_INT_ERR       0x1000
 
@@ -195,7 +196,7 @@ typedef struct rt_can_status_ind_type
     rt_canstatus_ind ind;
     void *args;
 } *rt_can_status_ind_type_t;
-
+typedef void (*rt_can_bus_hook)(struct rt_can_device *);
 struct rt_can_device
 {
     struct rt_device parent;
@@ -211,7 +212,9 @@ struct rt_can_device
 #ifdef RT_CAN_USING_HDR
     struct rt_can_hdr *hdr;
 #endif
-
+#ifdef RT_CAN_USING_BUS_HOOK
+    rt_can_bus_hook bus_hook;
+#endif /*RT_CAN_USING_BUS_HOOK*/
     struct rt_mutex lock;
     void *can_rx;
     void *can_tx;