master/device.c
changeset 692 fe7cf37c33f1
parent 687 6de97f276423
child 693 e341f1788608
equal deleted inserted replaced
691:77b79a29e0e7 692:fe7cf37c33f1
    44 #include <linux/netdevice.h>
    44 #include <linux/netdevice.h>
    45 
    45 
    46 #include "device.h"
    46 #include "device.h"
    47 #include "master.h"
    47 #include "master.h"
    48 
    48 
       
    49 #ifdef EC_DEBUG_RING
       
    50 #define timersub(a, b, result) \
       
    51     do { \
       
    52         (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
       
    53         (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
       
    54         if ((result)->tv_usec < 0) { \
       
    55             --(result)->tv_sec; \
       
    56             (result)->tv_usec += 1000000; \
       
    57         } \
       
    58     } while (0)
       
    59 #endif
       
    60 
    49 /*****************************************************************************/
    61 /*****************************************************************************/
    50 
    62 
    51 /**
    63 /**
    52    Device constructor.
    64    Device constructor.
    53    \return 0 in case of success, else < 0
    65    \return 0 in case of success, else < 0
    58         )
    70         )
    59 {
    71 {
    60 #ifdef EC_DEBUG_IF
    72 #ifdef EC_DEBUG_IF
    61     char ifname[10];
    73     char ifname[10];
    62     char mb = 'x';
    74     char mb = 'x';
       
    75 #endif
       
    76 #ifdef EC_DEBUG_RING
       
    77     device->debug_frame_index = 0;
       
    78     device->debug_frame_count = 0;
    63 #endif
    79 #endif
    64 
    80 
    65     device->master = master;
    81     device->master = master;
    66 
    82 
    67 #ifdef EC_DEBUG_IF
    83 #ifdef EC_DEBUG_IF
   247     }
   263     }
   248 
   264 
   249 #ifdef EC_DEBUG_IF
   265 #ifdef EC_DEBUG_IF
   250     ec_debug_send(&device->dbg, device->tx_skb->data, ETH_HLEN + size);
   266     ec_debug_send(&device->dbg, device->tx_skb->data, ETH_HLEN + size);
   251 #endif
   267 #endif
       
   268 #ifdef EC_DEBUG_RING
       
   269     ec_device_debug_ring_append(
       
   270             device, TX, device->tx_skb->data + ETH_HLEN, size);
       
   271 #endif
   252 
   272 
   253     // start sending
   273     // start sending
   254     device->dev->hard_start_xmit(device->tx_skb, device->dev);
   274     device->dev->hard_start_xmit(device->tx_skb, device->dev);
   255     device->tx_count++;
   275     device->tx_count++;
   256 }
   276 }
       
   277 
       
   278 /*****************************************************************************/
       
   279 
       
   280 #ifdef EC_DEBUG_RING
       
   281 /**
       
   282  * Appends frame data to the debug ring.
       
   283  */
       
   284 
       
   285 void ec_device_debug_ring_append(
       
   286         ec_device_t *device, /**< EtherCAT device */
       
   287         ec_debug_frame_dir_t dir, /**< direction */
       
   288         const void *data, /**< frame data */
       
   289         size_t size /**< data size */
       
   290         )
       
   291 {
       
   292     ec_debug_frame_t *df = &device->debug_frames[device->debug_frame_index];
       
   293 
       
   294     df->dir = dir;
       
   295     if (dir == TX)
       
   296         do_gettimeofday(&df->t);
       
   297     else
       
   298         df->t = device->timeval_poll;
       
   299     memcpy(df->data, data, size);
       
   300     df->data_size = size;
       
   301 
       
   302     device->debug_frame_index++;
       
   303     device->debug_frame_index %= EC_DEBUG_RING_SIZE;
       
   304     if (unlikely(device->debug_frame_count < EC_DEBUG_RING_SIZE))
       
   305         device->debug_frame_count++;
       
   306 }
       
   307 
       
   308 /*****************************************************************************/
       
   309 
       
   310 /**
       
   311  * Outputs the debug ring.
       
   312  */
       
   313 
       
   314 void ec_device_debug_ring_print(
       
   315         const ec_device_t *device /**< EtherCAT device */
       
   316         )
       
   317 {
       
   318     int i;
       
   319     unsigned int ring_index;
       
   320     const ec_debug_frame_t *df;
       
   321     struct timeval t0, diff;
       
   322 
       
   323     // calculate index of the newest frame in the ring to get its time
       
   324     ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE - 1)
       
   325         % EC_DEBUG_RING_SIZE;
       
   326     t0 = device->debug_frames[ring_index].t;
       
   327 
       
   328     EC_DBG("Debug ring %i:\n", ring_index);
       
   329 
       
   330     // calculate index of the oldest frame in the ring
       
   331     ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE
       
   332             - device->debug_frame_count) % EC_DEBUG_RING_SIZE;
       
   333 
       
   334     for (i = 0; i < device->debug_frame_count; i++) {
       
   335         df = &device->debug_frames[ring_index];
       
   336         timersub(&t0, &df->t, &diff);
       
   337 
       
   338         EC_DBG("Frame %i, dt=%u.%06u s, %s:\n",
       
   339                 i + 1 - device->debug_frame_count,
       
   340                 (unsigned int) diff.tv_sec,
       
   341                 (unsigned int) diff.tv_usec,
       
   342                 (df->dir == TX) ? "TX" : "RX");
       
   343         ec_print_data(df->data, df->data_size);
       
   344 
       
   345         ring_index++;
       
   346         ring_index %= EC_DEBUG_RING_SIZE;
       
   347     }
       
   348 }
       
   349 #endif
   257 
   350 
   258 /*****************************************************************************/
   351 /*****************************************************************************/
   259 
   352 
   260 /**
   353 /**
   261    Calls the poll function of the assigned net_device.
   354    Calls the poll function of the assigned net_device.
   266 
   359 
   267 void ec_device_poll(ec_device_t *device /**< EtherCAT device */)
   360 void ec_device_poll(ec_device_t *device /**< EtherCAT device */)
   268 {
   361 {
   269     device->cycles_poll = get_cycles();
   362     device->cycles_poll = get_cycles();
   270     device->jiffies_poll = jiffies;
   363     device->jiffies_poll = jiffies;
       
   364 #ifdef EC_DEBUG_RING
       
   365     do_gettimeofday(&device->timeval_poll);
       
   366 #endif
   271     device->poll(device->dev);
   367     device->poll(device->dev);
   272 }
   368 }
   273 
   369 
   274 /******************************************************************************
   370 /******************************************************************************
   275  *  Device interface
   371  *  Device interface
   285 void ecdev_receive(ec_device_t *device, /**< EtherCAT device */
   381 void ecdev_receive(ec_device_t *device, /**< EtherCAT device */
   286                    const void *data, /**< pointer to received data */
   382                    const void *data, /**< pointer to received data */
   287                    size_t size /**< number of bytes received */
   383                    size_t size /**< number of bytes received */
   288                    )
   384                    )
   289 {
   385 {
       
   386     const void *ec_data = data + ETH_HLEN;
       
   387     size_t ec_size = size - ETH_HLEN;
   290     device->rx_count++;
   388     device->rx_count++;
   291 
   389 
   292     if (unlikely(device->master->debug_level > 1)) {
   390     if (unlikely(device->master->debug_level > 1)) {
   293         EC_DBG("Received frame:\n");
   391         EC_DBG("Received frame:\n");
   294         ec_print_data_diff(device->tx_skb->data + ETH_HLEN,
   392         ec_print_data_diff(device->tx_skb->data + ETH_HLEN, ec_data, ec_size);
   295                            data + ETH_HLEN, size - ETH_HLEN);
       
   296     }
   393     }
   297 
   394 
   298 #ifdef EC_DEBUG_IF
   395 #ifdef EC_DEBUG_IF
   299     ec_debug_send(&device->dbg, data, size);
   396     ec_debug_send(&device->dbg, data, size);
   300 #endif
   397 #endif
   301 
   398 #ifdef EC_DEBUG_RING
   302     ec_master_receive_datagrams(device->master,
   399     ec_device_debug_ring_append(device, RX, ec_data, ec_size);
   303                                 data + ETH_HLEN,
   400 #endif
   304                                 size - ETH_HLEN);
   401 
       
   402     ec_master_receive_datagrams(device->master, ec_data, ec_size);
   305 }
   403 }
   306 
   404 
   307 /*****************************************************************************/
   405 /*****************************************************************************/
   308 
   406 
   309 /**
   407 /**