changeset 2415:da8bd66a1561

* cdl/io_can.cdl: Added option CYGOPT_IO_CAN_SUPPORT_CALLBACK * include/canio.h: Added struct cyg_can_callback_cfg for setting callback configurations. * include/can.h: Added declaration and initialization of callback configuration in struct can_channel. * src/can.c: Added callback configuration changing and application function call.
author asl
date Mon, 13 Aug 2007 07:36:34 +0000
parents c6f0c6300d8b
children 49d6269df2c2
files packages/io/can/current/ChangeLog packages/io/can/current/cdl/io_can.cdl packages/io/can/current/include/can.h packages/io/can/current/include/canio.h packages/io/can/current/src/can.c
diffstat 5 files changed, 99 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/packages/io/can/current/ChangeLog
+++ b/packages/io/can/current/ChangeLog
@@ -1,3 +1,17 @@
+2007-08-06 Alexey Shusharin <mrfinch@mail.ru>
+           Andrew Lunn <andrew.lunn@ascom.ch>
+	
+	* cdl/io_can.cdl: Added option CYGOPT_IO_CAN_SUPPORT_CALLBACK
+      
+	* include/canio.h: Added struct cyg_can_callback_cfg for setting
+          callback configurations.
+       
+	* include/can.h: Added declaration and initialization of callback 
+          configuration in struct can_channel.
+       
+	* src/can.c: Added callback configuration changing and
+          application function call.
+
 2007-07-02 Uwe Kindler <uwe_kindler@web.de>
     
 	* cdl/io_can.cdl: Added interface CYGINT_IO_CAN_CHANNELS for
--- a/packages/io/can/current/cdl/io_can.cdl
+++ b/packages/io/can/current/cdl/io_can.cdl
@@ -217,7 +217,18 @@ cdl_package CYGPKG_IO_CAN {
             which allows clients to switch read() and write() call
             semantics from blocking to non-blocking."
     }
-    
+
+    cdl_option CYGOPT_IO_CAN_SUPPORT_CALLBACK {
+        display       "Support callback on events"
+        default_value 0
+        description   "
+            This option enables extra code in the generic CAN driver
+            which allows application to register a callback for
+            events. The callback function is called from DSR
+            context so you should be careful to only call API
+            functions that are safe in DSR context."
+    }
+
     cdl_component CYGOPT_IO_CAN_SUPPORT_TIMEOUTS {
         display       "Support read/write timeouts"
         flavor        bool
--- a/packages/io/can/current/include/can.h
+++ b/packages/io/can/current/include/can.h
@@ -168,15 +168,23 @@ typedef struct can_cbuf_st
 //
 struct can_channel 
 {
-    can_lowlevel_funs  *funs;
-    can_callbacks_t    *callbacks;
-    void               *dev_priv;  // Whatever is needed by actual device routines
-    cyg_can_info_t      config;    // Current configuration
-    bool                init;      // true if driver is already initialized
-    can_cbuf_t          out_cbuf;  // buffer for transmit can messages
-    can_cbuf_t          in_cbuf;   // buffer with received can events
+    can_lowlevel_funs   *funs;
+    can_callbacks_t     *callbacks;
+    void                *dev_priv;     // Whatever is needed by actual device routines
+    cyg_can_info_t       config;       // Current configuration
+    bool                 init;         // true if driver is already initialized
+    can_cbuf_t           out_cbuf;     // buffer for transmit can messages
+    can_cbuf_t           in_cbuf;      // buffer with received can events
+#ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
+    cyg_can_callback_cfg callback_cfg; // Callback configuration
+#endif
 };
 
+#ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
+#define CYG_CAN_CALLBACK_INIT  , {(cyg_can_event_cb_t) 0, 0, 0}
+#else
+#define CYG_CAN_CALLBACK_INIT
+#endif
 
 
 #define CAN_CHANNEL_USING_INTERRUPTS(_l,                                \
@@ -193,6 +201,7 @@ can_channel _l = {                      
     false,                                                              \
     CBUF_INIT(_out_buf, _out_buflen, _TX_TIMEOUT),                      \
     CBUF_INIT(_in_buf, _in_buflen, _RX_TIMEOUT)                         \
+    CYG_CAN_CALLBACK_INIT                                               \
 };
 
 
--- a/packages/io/can/current/include/canio.h
+++ b/packages/io/can/current/include/canio.h
@@ -327,6 +327,24 @@ typedef struct cyg_can_hdi_st
 #define CYGNUM_CAN_HDI_TIMESTAMP               0x10 // driver supports timestamps
 
 
+//
+// Callback configuration structure.
+//
+
+typedef void (*cyg_can_event_cb_t)(cyg_uint16, cyg_addrword_t);
+//
+// flag_mask should be set with a combination of CYGNUM_CAN_EVENT_* flags.
+// If one of these events happens, the callback function will be called,
+// with the actually event flags passed as a parameter.
+//
+typedef struct cyg_can_callback_cfg_st
+{
+    cyg_can_event_cb_t callback_func;              // callback function
+    cyg_uint16  flag_mask;                         // flags mask
+    cyg_addrword_t data;                           // data passed to callback
+} cyg_can_callback_cfg;
+
+
 //===========================================================================
 //                      CAN MESSAGE ACCESS MACROS
 //
--- a/packages/io/can/current/src/can.c
+++ b/packages/io/can/current/src/can.c
@@ -665,6 +665,27 @@ static Cyg_ErrNo can_set_config(cyg_io_h
              }
              break;
         
+
+#ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
+        //
+        // Set waking calls configuration
+        // To disable waking calls set waking_mask = 0
+        //
+        case CYG_IO_SET_CONFIG_CAN_CALLBACK:
+             {
+                 if (*len != sizeof(cyg_can_callback_cfg))
+                 {
+                         return -EINVAL;
+                 }
+            
+                 // Copy data under DSR locking
+                 cyg_drv_dsr_lock();
+                 chan->callback_cfg = *((cyg_can_callback_cfg*) xbuf);
+                 cyg_drv_dsr_unlock();
+             }
+             break;
+#endif //CYGOPT_IO_CAN_SUPPORT_CALLBACK
+
         default:
             //
             // pass down to lower layers
@@ -695,6 +716,9 @@ static void can_rcv_event(can_channel *c
 {
     can_cbuf_t       *cbuf   = &chan->in_cbuf;
     CYG_CAN_EVENT_T  *prxbuf = (CYG_CAN_EVENT_T *)cbuf->pdata;
+#ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
+    cyg_uint16        flags;
+#endif
     
     //
     // cbuf is a ring buffer - if the buffer is full, then we overwrite the
@@ -722,7 +746,11 @@ static void can_rcv_event(can_channel *c
             prxbuf[cbuf->put].flags |= CYGNUM_CAN_EVENT_OVERRUN_RX;
             cbuf->get = (cbuf->get + 1) % cbuf->len;
         }
-        
+
+#ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
+        flags = prxbuf[cbuf->put].flags;
+#endif
+
         cbuf->put = (cbuf->put + 1) % cbuf->len;
         
         if (cbuf->waiting) 
@@ -730,6 +758,16 @@ static void can_rcv_event(can_channel *c
             cbuf->waiting = false;
             cyg_drv_cond_broadcast(&cbuf->wait);
         }
+#ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
+        // Call application callback function, if any of the flag events 
+        // are unmasked.
+        if((flags & chan->callback_cfg.flag_mask) &&
+           (chan->callback_cfg.callback_func))
+        {
+            chan->callback_cfg.callback_func(flags,
+                                             chan->callback_cfg.data);
+        }
+#endif
     }
     
     cyg_drv_dsr_unlock();