master/device.c
branchstable-1.2
changeset 1739 5fcbd29151d2
parent 1732 1cc865ba17c2
child 1744 7bc131b92039
equal deleted inserted replaced
1738:bc89e3fba1a5 1739:5fcbd29151d2
    55 */
    55 */
    56 
    56 
    57 int ec_device_init(ec_device_t *device, /**< EtherCAT device */
    57 int ec_device_init(ec_device_t *device, /**< EtherCAT device */
    58                    ec_master_t *master, /**< master owning the device */
    58                    ec_master_t *master, /**< master owning the device */
    59                    struct net_device *net_dev, /**< net_device structure */
    59                    struct net_device *net_dev, /**< net_device structure */
    60                    ec_isr_t isr, /**< pointer to device's ISR */
    60                    ec_pollfunc_t poll, /**< pointer to device's poll function */
    61                    struct module *module /**< pointer to the owning module */
    61                    struct module *module /**< the device's module */
    62                    )
    62                    )
    63 {
    63 {
    64     struct ethhdr *eth;
    64     struct ethhdr *eth;
    65 
    65 
    66     device->master = master;
    66     device->master = master;
    67     device->dev = net_dev;
    67     device->dev = net_dev;
    68     device->isr = isr;
    68     device->poll = poll;
    69     device->module = module;
    69     device->module = module;
    70 
    70 
    71     device->open = 0;
    71     device->open = 0;
    72     device->link_state = 0; // down
    72     device->link_state = 0; // down
       
    73 
       
    74     device->tx_count = 0;
       
    75     device->rx_count = 0;
    73 
    76 
    74 #ifdef EC_DBG_IF
    77 #ifdef EC_DBG_IF
    75     if (ec_debug_init(&device->dbg)) {
    78     if (ec_debug_init(&device->dbg)) {
    76         EC_ERR("Failed to init debug device!\n");
    79         EC_ERR("Failed to init debug device!\n");
    77         goto out_return;
    80         goto out_return;
   141         EC_WARN("Device already opened!\n");
   144         EC_WARN("Device already opened!\n");
   142         return 0;
   145         return 0;
   143     }
   146     }
   144 
   147 
   145     // device could have received frames before
   148     // device could have received frames before
   146     for (i = 0; i < 4; i++) ec_device_call_isr(device);
   149     for (i = 0; i < 4; i++) ec_device_poll(device);
   147 
   150 
   148     device->link_state = 0;
   151     device->link_state = 0;
       
   152     device->tx_count = 0;
       
   153     device->rx_count = 0;
   149 
   154 
   150     if (device->dev->open(device->dev) == 0) device->open = 1;
   155     if (device->dev->open(device->dev) == 0) device->open = 1;
   151 
   156 
   152     return device->open ? 0 : -1;
   157     return device->open ? 0 : -1;
   153 }
   158 }
   215     ec_debug_send(&device->dbg, device->tx_skb->data, ETH_HLEN + size);
   220     ec_debug_send(&device->dbg, device->tx_skb->data, ETH_HLEN + size);
   216 #endif
   221 #endif
   217 
   222 
   218     // start sending
   223     // start sending
   219     device->dev->hard_start_xmit(device->tx_skb, device->dev);
   224     device->dev->hard_start_xmit(device->tx_skb, device->dev);
   220 }
   225     device->tx_count++;
   221 
   226 }
   222 /*****************************************************************************/
   227 
   223 
   228 /*****************************************************************************/
   224 /**
   229 
   225    Calls the interrupt service routine of the assigned net_device.
   230 /**
       
   231    Calls the poll function of the assigned net_device.
   226    The master itself works without using interrupts. Therefore the processing
   232    The master itself works without using interrupts. Therefore the processing
   227    of received data and status changes of the network device has to be
   233    of received data and status changes of the network device has to be
   228    done by the master calling the ISR "manually".
   234    done by the master calling the ISR "manually".
   229 */
   235 */
   230 
   236 
   231 void ec_device_call_isr(ec_device_t *device /**< EtherCAT device */)
   237 void ec_device_poll(ec_device_t *device /**< EtherCAT device */)
   232 {
   238 {
   233     device->cycles_isr = get_cycles();
   239     device->cycles_poll = get_cycles();
   234     device->jiffies_isr = jiffies;
   240     device->jiffies_poll = jiffies;
   235     if (likely(device->isr)) device->isr(0, device->dev, NULL);
   241     device->poll(device->dev);
   236 }
   242 }
   237 
   243 
   238 /******************************************************************************
   244 /******************************************************************************
   239  *  Device interface
   245  *  Device interface
   240  *****************************************************************************/
   246  *****************************************************************************/
   249 void ecdev_receive(ec_device_t *device, /**< EtherCAT device */
   255 void ecdev_receive(ec_device_t *device, /**< EtherCAT device */
   250                    const void *data, /**< pointer to received data */
   256                    const void *data, /**< pointer to received data */
   251                    size_t size /**< number of bytes received */
   257                    size_t size /**< number of bytes received */
   252                    )
   258                    )
   253 {
   259 {
       
   260     device->rx_count++;
       
   261 
   254     if (unlikely(device->master->debug_level > 1)) {
   262     if (unlikely(device->master->debug_level > 1)) {
   255         EC_DBG("Received frame:\n");
   263         EC_DBG("Received frame:\n");
   256         ec_print_data_diff(device->tx_skb->data + ETH_HLEN,
   264         ec_print_data_diff(device->tx_skb->data + ETH_HLEN,
   257                            data + ETH_HLEN, size - ETH_HLEN);
   265                            data + ETH_HLEN, size - ETH_HLEN);
   258     }
   266     }