master/frame.c
changeset 84 b4ae98855cea
parent 78 3d74183d6c6b
child 89 e91ef35c36db
equal deleted inserted replaced
83:e8b76a509bc9 84:b4ae98855cea
    50                         unsigned int length
    50                         unsigned int length
    51                         /**< Länge der zu lesenden Daten */
    51                         /**< Länge der zu lesenden Daten */
    52                         )
    52                         )
    53 {
    53 {
    54     if (unlikely(node_address == 0x0000))
    54     if (unlikely(node_address == 0x0000))
    55         printk(KERN_WARNING "EtherCAT: Warning - Using node address 0x0000!\n");
    55         EC_WARN("Using node address 0x0000!\n");
    56 
    56 
    57     EC_FUNC_HEADER;
    57     EC_FUNC_HEADER;
    58 
    58 
    59     frame->type = ec_frame_type_nprd;
    59     frame->type = ec_frame_type_nprd;
    60     frame->address.physical.slave = node_address;
    60     frame->address.physical.slave = node_address;
    84                         const uint8_t *data
    84                         const uint8_t *data
    85                         /**< Zeiger auf Speicher mit zu schreibenden Daten */
    85                         /**< Zeiger auf Speicher mit zu schreibenden Daten */
    86                         )
    86                         )
    87 {
    87 {
    88     if (unlikely(node_address == 0x0000))
    88     if (unlikely(node_address == 0x0000))
    89         printk(KERN_WARNING "EtherCAT: Warning - Using node address 0x0000!\n");
    89         EC_WARN("Using node address 0x0000!\n");
    90 
    90 
    91     EC_FUNC_HEADER;
    91     EC_FUNC_HEADER;
    92 
    92 
    93     frame->type = ec_frame_type_npwr;
    93     frame->type = ec_frame_type_npwr;
    94     frame->address.physical.slave = node_address;
    94     frame->address.physical.slave = node_address;
   253 {
   253 {
   254     unsigned int command_size, frame_size, i;
   254     unsigned int command_size, frame_size, i;
   255     uint8_t *data;
   255     uint8_t *data;
   256 
   256 
   257     if (unlikely(frame->master->debug_level > 0)) {
   257     if (unlikely(frame->master->debug_level > 0)) {
   258         printk(KERN_DEBUG "EtherCAT: ec_frame_send\n");
   258         EC_DBG("ec_frame_send\n");
   259     }
   259     }
   260 
   260 
   261     if (unlikely(frame->state != ec_frame_ready)) {
   261     if (unlikely(frame->state != ec_frame_ready)) {
   262         printk(KERN_WARNING "EtherCAT: Frame not in \"ready\" state!\n");
   262         EC_WARN("Frame not in \"ready\" state!\n");
   263     }
   263     }
   264 
   264 
   265     command_size = frame->data_length + EC_COMMAND_HEADER_SIZE
   265     command_size = frame->data_length + EC_COMMAND_HEADER_SIZE
   266         + EC_COMMAND_FOOTER_SIZE;
   266         + EC_COMMAND_FOOTER_SIZE;
   267     frame_size = command_size + EC_FRAME_HEADER_SIZE;
   267     frame_size = command_size + EC_FRAME_HEADER_SIZE;
   268 
   268 
   269     if (unlikely(frame_size > EC_MAX_FRAME_SIZE)) {
   269     if (unlikely(frame_size > EC_MAX_FRAME_SIZE)) {
   270         printk(KERN_ERR "EtherCAT: Frame too long (%i)!\n", frame_size);
   270         EC_ERR("Frame too long (%i)!\n", frame_size);
   271         return -1;
   271         return -1;
   272     }
   272     }
   273 
   273 
   274     if (frame_size < EC_MIN_FRAME_SIZE) frame_size = EC_MIN_FRAME_SIZE;
   274     if (frame_size < EC_MIN_FRAME_SIZE) frame_size = EC_MIN_FRAME_SIZE;
   275 
   275 
   276     if (unlikely(frame->master->debug_level > 0)) {
   276     if (unlikely(frame->master->debug_level > 0)) {
   277         printk(KERN_DEBUG "EtherCAT: Frame length: %i\n", frame_size);
   277         EC_DBG("Frame length: %i\n", frame_size);
   278     }
   278     }
   279 
   279 
   280     frame->index = frame->master->command_index;
   280     frame->index = frame->master->command_index;
   281     frame->master->command_index = (frame->master->command_index + 1) % 0x0100;
   281     frame->master->command_index = (frame->master->command_index + 1) % 0x0100;
   282 
   282 
   283     if (unlikely(frame->master->debug_level > 0)) {
   283     if (unlikely(frame->master->debug_level > 0)) {
   284         printk(KERN_DEBUG "EtherCAT: Sending command index 0x%X\n",
   284         EC_DBG("Sending command index 0x%X\n", frame->index);
   285                frame->index);
       
   286     }
   285     }
   287 
   286 
   288     frame->state = ec_frame_sent;
   287     frame->state = ec_frame_sent;
   289 
   288 
   290     // Zeiger auf Socket-Buffer holen
   289     // Zeiger auf Socket-Buffer holen
   336     uint8_t *data;
   335     uint8_t *data;
   337     uint8_t command_type, command_index;
   336     uint8_t command_type, command_index;
   338     ec_device_t *device;
   337     ec_device_t *device;
   339 
   338 
   340     if (unlikely(frame->state != ec_frame_sent)) {
   339     if (unlikely(frame->state != ec_frame_sent)) {
   341         printk(KERN_ERR "EtherCAT: Frame was not sent!\n");
   340         EC_ERR("Frame was not sent!\n");
   342         return -1;
   341         return -1;
   343     }
   342     }
   344 
   343 
   345     device = &frame->master->device;
   344     device = &frame->master->device;
   346 
   345 
   347     if (!(received_length = ec_device_received(device))) return -1;
   346     if (!(received_length = ec_device_received(device))) return -1;
   348 
   347 
   349     device->state = EC_DEVICE_STATE_READY;
   348     device->state = EC_DEVICE_STATE_READY;
   350 
   349 
   351     if (unlikely(received_length < EC_FRAME_HEADER_SIZE)) {
   350     if (unlikely(received_length < EC_FRAME_HEADER_SIZE)) {
   352         printk(KERN_ERR "EtherCAT: Received frame with incomplete EtherCAT"
   351         EC_ERR("Received frame with incomplete EtherCAT frame header!\n");
   353                " frame header!\n");
       
   354         ec_device_debug(device);
   352         ec_device_debug(device);
   355         return -1;
   353         return -1;
   356     }
   354     }
   357 
   355 
   358     data = ec_device_data(device);
   356     data = ec_device_data(device);
   360     // Länge des gesamten Frames prüfen
   358     // Länge des gesamten Frames prüfen
   361     frame_length = EC_READ_U16(data) & 0x07FF;
   359     frame_length = EC_READ_U16(data) & 0x07FF;
   362     data += EC_FRAME_HEADER_SIZE;
   360     data += EC_FRAME_HEADER_SIZE;
   363 
   361 
   364     if (unlikely(frame_length > received_length)) {
   362     if (unlikely(frame_length > received_length)) {
   365         printk(KERN_ERR "EtherCAT: Received corrupted frame (length does"
   363         EC_ERR("Received corrupted frame (length does not match)!\n");
   366                " not match)!\n");
       
   367         ec_device_debug(device);
   364         ec_device_debug(device);
   368         return -1;
   365         return -1;
   369     }
   366     }
   370 
   367 
   371     // Command header
   368     // Command header
   374     data_length =   EC_READ_U16(data + 6) & 0x07FF;
   371     data_length =   EC_READ_U16(data + 6) & 0x07FF;
   375     data += EC_COMMAND_HEADER_SIZE;
   372     data += EC_COMMAND_HEADER_SIZE;
   376 
   373 
   377     if (unlikely(EC_FRAME_HEADER_SIZE + EC_COMMAND_HEADER_SIZE
   374     if (unlikely(EC_FRAME_HEADER_SIZE + EC_COMMAND_HEADER_SIZE
   378                  + data_length + EC_COMMAND_FOOTER_SIZE > received_length)) {
   375                  + data_length + EC_COMMAND_FOOTER_SIZE > received_length)) {
   379         printk(KERN_ERR "EtherCAT: Received frame with incomplete command"
   376         EC_ERR("Received frame with incomplete command data!\n");
   380                " data!\n");
       
   381         ec_device_debug(device);
   377         ec_device_debug(device);
   382         return -1;
   378         return -1;
   383     }
   379     }
   384 
   380 
   385     if (unlikely(frame->type != command_type
   381     if (unlikely(frame->type != command_type
   386                  || frame->index != command_index
   382                  || frame->index != command_index
   387                  || frame->data_length != data_length))
   383                  || frame->data_length != data_length))
   388     {
   384     {
   389         printk(KERN_WARNING "EtherCAT: WARNING - Send/Receive anomaly!\n");
   385         EC_WARN("WARNING - Send/Receive anomaly!\n");
   390         ec_device_debug(device);
   386         ec_device_debug(device);
   391         ec_device_call_isr(device); // Empfangenes "vergessen"
   387         ec_device_call_isr(device); // Empfangenes "vergessen"
   392         return -1;
   388         return -1;
   393     }
   389     }
   394 
   390 
   417                           )
   413                           )
   418 {
   414 {
   419     unsigned int tries_left;
   415     unsigned int tries_left;
   420 
   416 
   421     if (unlikely(ec_frame_send(frame) < 0)) {
   417     if (unlikely(ec_frame_send(frame) < 0)) {
   422         printk(KERN_ERR "EtherCAT: Frame sending failed!\n");
   418         EC_ERR("Frame sending failed!\n");
   423         return -1;
   419         return -1;
   424     }
   420     }
   425 
   421 
   426     tries_left = 20;
   422     tries_left = 20;
   427     do
   423     do
   432     }
   428     }
   433     while (unlikely(!ec_device_received(&frame->master->device)
   429     while (unlikely(!ec_device_received(&frame->master->device)
   434                     && tries_left));
   430                     && tries_left));
   435 
   431 
   436     if (unlikely(!tries_left)) {
   432     if (unlikely(!tries_left)) {
   437         printk(KERN_ERR "EtherCAT: Frame timeout!\n");
   433         EC_ERR("Frame timeout!\n");
   438         return -1;
   434         return -1;
   439     }
   435     }
   440 
   436 
   441     if (unlikely(ec_frame_receive(frame) < 0)) {
   437     if (unlikely(ec_frame_receive(frame) < 0)) {
   442         printk(KERN_ERR "EtherCAT: Frame receiving failed!\n");
   438         EC_ERR("Frame receiving failed!\n");
   443         return -1;
   439         return -1;
   444     }
   440     }
   445 
   441 
   446     return 0;
   442     return 0;
   447 }
   443 }