master/master.c
branchstable-1.0
changeset 1624 9dc190591c0f
parent 1621 4bbe090553f7
equal deleted inserted replaced
1623:05622513f627 1624:9dc190591c0f
    48 #include "globals.h"
    48 #include "globals.h"
    49 #include "master.h"
    49 #include "master.h"
    50 #include "slave.h"
    50 #include "slave.h"
    51 #include "types.h"
    51 #include "types.h"
    52 #include "device.h"
    52 #include "device.h"
    53 #include "command.h"
    53 #include "datagram.h"
    54 #include "ethernet.h"
    54 #include "ethernet.h"
    55 
    55 
    56 /*****************************************************************************/
    56 /*****************************************************************************/
    57 
    57 
    58 void ec_master_freerun(void *);
    58 void ec_master_idle_run(void *);
    59 void ec_master_eoe_run(unsigned long);
    59 void ec_master_eoe_run(unsigned long);
    60 ssize_t ec_show_master_attribute(struct kobject *, struct attribute *, char *);
    60 ssize_t ec_show_master_attribute(struct kobject *, struct attribute *, char *);
    61 ssize_t ec_store_master_attribute(struct kobject *, struct attribute *,
    61 ssize_t ec_store_master_attribute(struct kobject *, struct attribute *,
    62                                   const char *, size_t);
    62                                   const char *, size_t);
    63 
    63 
    66 /** \cond */
    66 /** \cond */
    67 
    67 
    68 EC_SYSFS_READ_ATTR(slave_count);
    68 EC_SYSFS_READ_ATTR(slave_count);
    69 EC_SYSFS_READ_ATTR(mode);
    69 EC_SYSFS_READ_ATTR(mode);
    70 EC_SYSFS_READ_WRITE_ATTR(eeprom_write_enable);
    70 EC_SYSFS_READ_WRITE_ATTR(eeprom_write_enable);
       
    71 EC_SYSFS_READ_WRITE_ATTR(debug_level);
    71 
    72 
    72 static struct attribute *ec_def_attrs[] = {
    73 static struct attribute *ec_def_attrs[] = {
    73     &attr_slave_count,
    74     &attr_slave_count,
    74     &attr_mode,
    75     &attr_mode,
    75     &attr_eeprom_write_enable,
    76     &attr_eeprom_write_enable,
       
    77     &attr_debug_level,
    76     NULL,
    78     NULL,
    77 };
    79 };
    78 
    80 
    79 static struct sysfs_ops ec_sysfs_ops = {
    81 static struct sysfs_ops ec_sysfs_ops = {
    80     .show = &ec_show_master_attribute,
    82     .show = &ec_show_master_attribute,
    96    \return 0 in case of success, else < 0
    98    \return 0 in case of success, else < 0
    97 */
    99 */
    98 
   100 
    99 int ec_master_init(ec_master_t *master, /**< EtherCAT master */
   101 int ec_master_init(ec_master_t *master, /**< EtherCAT master */
   100                    unsigned int index, /**< master index */
   102                    unsigned int index, /**< master index */
   101                    unsigned int eoe_devices /**< number of EoE devices */
   103                    unsigned int eoeif_count /**< number of EoE interfaces */
   102                    )
   104                    )
   103 {
   105 {
   104     ec_eoe_t *eoe, *next_eoe;
   106     ec_eoe_t *eoe, *next_eoe;
   105     unsigned int i;
   107     unsigned int i;
   106 
   108 
   108 
   110 
   109     master->index = index;
   111     master->index = index;
   110     master->device = NULL;
   112     master->device = NULL;
   111     master->reserved = 0;
   113     master->reserved = 0;
   112     INIT_LIST_HEAD(&master->slaves);
   114     INIT_LIST_HEAD(&master->slaves);
   113     INIT_LIST_HEAD(&master->command_queue);
   115     INIT_LIST_HEAD(&master->datagram_queue);
   114     INIT_LIST_HEAD(&master->domains);
   116     INIT_LIST_HEAD(&master->domains);
   115     INIT_LIST_HEAD(&master->eoe_handlers);
   117     INIT_LIST_HEAD(&master->eoe_handlers);
   116     ec_command_init(&master->simple_command);
   118     ec_datagram_init(&master->simple_datagram);
   117     INIT_WORK(&master->freerun_work, ec_master_freerun, (void *) master);
   119     INIT_WORK(&master->idle_work, ec_master_idle_run, (void *) master);
   118     init_timer(&master->eoe_timer);
   120     init_timer(&master->eoe_timer);
   119     master->eoe_timer.function = ec_master_eoe_run;
   121     master->eoe_timer.function = ec_master_eoe_run;
   120     master->eoe_timer.data = (unsigned long) master;
   122     master->eoe_timer.data = (unsigned long) master;
   121     master->internal_lock = SPIN_LOCK_UNLOCKED;
   123     master->internal_lock = SPIN_LOCK_UNLOCKED;
   122     master->eoe_running = 0;
   124     master->eoe_running = 0;
   126         EC_ERR("Failed to create master workqueue.\n");
   128         EC_ERR("Failed to create master workqueue.\n");
   127         goto out_return;
   129         goto out_return;
   128     }
   130     }
   129 
   131 
   130     // create EoE handlers
   132     // create EoE handlers
   131     for (i = 0; i < eoe_devices; i++) {
   133     for (i = 0; i < eoeif_count; i++) {
   132         if (!(eoe = (ec_eoe_t *) kmalloc(sizeof(ec_eoe_t), GFP_KERNEL))) {
   134         if (!(eoe = (ec_eoe_t *) kmalloc(sizeof(ec_eoe_t), GFP_KERNEL))) {
   133             EC_ERR("Failed to allocate EoE-Object.\n");
   135             EC_ERR("Failed to allocate EoE-Object.\n");
   134             goto out_clear_eoe;
   136             goto out_clear_eoe;
   135         }
   137         }
   136         if (ec_eoe_init(eoe)) {
   138         if (ec_eoe_init(eoe)) {
   169 
   171 
   170 /*****************************************************************************/
   172 /*****************************************************************************/
   171 
   173 
   172 /**
   174 /**
   173    Master destructor.
   175    Master destructor.
   174    Removes all pending commands, clears the slave list, clears all domains
   176    Removes all pending datagrams, clears the slave list, clears all domains
   175    and frees the device.
   177    and frees the device.
   176 */
   178 */
   177 
   179 
   178 void ec_master_clear(struct kobject *kobj /**< kobject of the master */)
   180 void ec_master_clear(struct kobject *kobj /**< kobject of the master */)
   179 {
   181 {
   182 
   184 
   183     EC_INFO("Clearing master %i...\n", master->index);
   185     EC_INFO("Clearing master %i...\n", master->index);
   184 
   186 
   185     ec_master_reset(master);
   187     ec_master_reset(master);
   186     ec_fsm_clear(&master->fsm);
   188     ec_fsm_clear(&master->fsm);
   187     ec_command_clear(&master->simple_command);
   189     ec_datagram_clear(&master->simple_datagram);
   188     destroy_workqueue(master->workqueue);
   190     destroy_workqueue(master->workqueue);
   189 
   191 
   190     // clear EoE objects
   192     // clear EoE objects
   191     list_for_each_entry_safe(eoe, next_eoe, &master->eoe_handlers, list) {
   193     list_for_each_entry_safe(eoe, next_eoe, &master->eoe_handlers, list) {
   192         list_del(&eoe->list);
   194         list_del(&eoe->list);
   210    called, to free the slave list, domains etc.
   212    called, to free the slave list, domains etc.
   211 */
   213 */
   212 
   214 
   213 void ec_master_reset(ec_master_t *master /**< EtherCAT master */)
   215 void ec_master_reset(ec_master_t *master /**< EtherCAT master */)
   214 {
   216 {
   215     ec_command_t *command, *next_c;
   217     ec_datagram_t *datagram, *next_c;
   216     ec_domain_t *domain, *next_d;
   218     ec_domain_t *domain, *next_d;
   217 
   219 
   218     ec_master_eoe_stop(master);
   220     ec_master_eoe_stop(master);
   219     ec_master_freerun_stop(master);
   221     ec_master_idle_stop(master);
   220     ec_master_clear_slaves(master);
   222     ec_master_clear_slaves(master);
   221 
   223 
   222     // empty command queue
   224     // empty datagram queue
   223     list_for_each_entry_safe(command, next_c, &master->command_queue, queue) {
   225     list_for_each_entry_safe(datagram, next_c,
   224         list_del_init(&command->queue);
   226                              &master->datagram_queue, queue) {
   225         command->state = EC_CMD_ERROR;
   227         list_del_init(&datagram->queue);
       
   228         datagram->state = EC_CMD_ERROR;
   226     }
   229     }
   227 
   230 
   228     // clear domains
   231     // clear domains
   229     list_for_each_entry_safe(domain, next_d, &master->domains, list) {
   232     list_for_each_entry_safe(domain, next_d, &master->domains, list) {
   230         list_del(&domain->list);
   233         list_del(&domain->list);
   231         kobject_del(&domain->kobj);
   234         kobject_del(&domain->kobj);
   232         kobject_put(&domain->kobj);
   235         kobject_put(&domain->kobj);
   233     }
   236     }
   234 
   237 
   235     master->command_index = 0;
   238     master->datagram_index = 0;
   236     master->debug_level = 0;
   239     master->debug_level = 0;
   237     master->timeout = 500; // 500us
   240     master->timeout = 500; // 500us
   238 
   241 
   239     master->stats.timeouts = 0;
   242     master->stats.timeouts = 0;
   240     master->stats.delayed = 0;
   243     master->stats.delayed = 0;
   241     master->stats.corrupted = 0;
   244     master->stats.corrupted = 0;
   242     master->stats.unmatched = 0;
   245     master->stats.unmatched = 0;
   243     master->stats.t_last = 0;
   246     master->stats.t_last = 0;
   244 
   247 
   245     master->mode = EC_MASTER_MODE_IDLE;
   248     master->mode = EC_MASTER_MODE_ORPHANED;
   246 
   249 
   247     master->request_cb = NULL;
   250     master->request_cb = NULL;
   248     master->release_cb = NULL;
   251     master->release_cb = NULL;
   249     master->cb_data = NULL;
   252     master->cb_data = NULL;
   250 
   253 
   272 }
   275 }
   273 
   276 
   274 /*****************************************************************************/
   277 /*****************************************************************************/
   275 
   278 
   276 /**
   279 /**
   277    Places a command in the command queue.
   280    Places a datagram in the datagram queue.
   278 */
   281 */
   279 
   282 
   280 void ec_master_queue_command(ec_master_t *master, /**< EtherCAT master */
   283 void ec_master_queue_datagram(ec_master_t *master, /**< EtherCAT master */
   281                              ec_command_t *command /**< command */
   284                               ec_datagram_t *datagram /**< datagram */
   282                              )
   285                               )
   283 {
   286 {
   284     ec_command_t *queued_command;
   287     ec_datagram_t *queued_datagram;
   285 
   288 
   286     // check, if the command is already queued
   289     // check, if the datagram is already queued
   287     list_for_each_entry(queued_command, &master->command_queue, queue) {
   290     list_for_each_entry(queued_datagram, &master->datagram_queue, queue) {
   288         if (queued_command == command) {
   291         if (queued_datagram == datagram) {
   289             command->state = EC_CMD_QUEUED;
   292             datagram->state = EC_CMD_QUEUED;
   290             if (unlikely(master->debug_level))
   293             if (unlikely(master->debug_level))
   291                 EC_WARN("command already queued.\n");
   294                 EC_WARN("datagram already queued.\n");
   292             return;
   295             return;
   293         }
   296         }
   294     }
   297     }
   295 
   298 
   296     list_add_tail(&command->queue, &master->command_queue);
   299     list_add_tail(&datagram->queue, &master->datagram_queue);
   297     command->state = EC_CMD_QUEUED;
   300     datagram->state = EC_CMD_QUEUED;
   298 }
   301 }
   299 
   302 
   300 /*****************************************************************************/
   303 /*****************************************************************************/
   301 
   304 
   302 /**
   305 /**
   303    Sends the commands in the queue.
   306    Sends the datagrams in the queue.
   304    \return 0 in case of success, else < 0
   307    \return 0 in case of success, else < 0
   305 */
   308 */
   306 
   309 
   307 void ec_master_send_commands(ec_master_t *master /**< EtherCAT master */)
   310 void ec_master_send_datagrams(ec_master_t *master /**< EtherCAT master */)
   308 {
   311 {
   309     ec_command_t *command;
   312     ec_datagram_t *datagram;
   310     size_t command_size;
   313     size_t datagram_size;
   311     uint8_t *frame_data, *cur_data;
   314     uint8_t *frame_data, *cur_data;
   312     void *follows_word;
   315     void *follows_word;
   313     cycles_t t_start, t_end;
   316     cycles_t t_start, t_end;
   314     unsigned int frame_count, more_commands_waiting;
   317     unsigned int frame_count, more_datagrams_waiting;
   315 
   318 
   316     frame_count = 0;
   319     frame_count = 0;
   317     t_start = get_cycles();
   320     t_start = get_cycles();
   318 
   321 
   319     if (unlikely(master->debug_level > 0))
   322     if (unlikely(master->debug_level > 1))
   320         EC_DBG("ec_master_send_commands\n");
   323         EC_DBG("ec_master_send_datagrams\n");
   321 
   324 
   322     do {
   325     do {
   323         // fetch pointer to transmit socket buffer
   326         // fetch pointer to transmit socket buffer
   324         frame_data = ec_device_tx_data(master->device);
   327         frame_data = ec_device_tx_data(master->device);
   325         cur_data = frame_data + EC_FRAME_HEADER_SIZE;
   328         cur_data = frame_data + EC_FRAME_HEADER_SIZE;
   326         follows_word = NULL;
   329         follows_word = NULL;
   327         more_commands_waiting = 0;
   330         more_datagrams_waiting = 0;
   328 
   331 
   329         // fill current frame with commands
   332         // fill current frame with datagrams
   330         list_for_each_entry(command, &master->command_queue, queue) {
   333         list_for_each_entry(datagram, &master->datagram_queue, queue) {
   331             if (command->state != EC_CMD_QUEUED) continue;
   334             if (datagram->state != EC_CMD_QUEUED) continue;
   332 
   335 
   333             // does the current command fit in the frame?
   336             // does the current datagram fit in the frame?
   334             command_size = EC_COMMAND_HEADER_SIZE + command->data_size
   337             datagram_size = EC_DATAGRAM_HEADER_SIZE + datagram->data_size
   335                 + EC_COMMAND_FOOTER_SIZE;
   338                 + EC_DATAGRAM_FOOTER_SIZE;
   336             if (cur_data - frame_data + command_size > ETH_DATA_LEN) {
   339             if (cur_data - frame_data + datagram_size > ETH_DATA_LEN) {
   337                 more_commands_waiting = 1;
   340                 more_datagrams_waiting = 1;
   338                 break;
   341                 break;
   339             }
   342             }
   340 
   343 
   341             command->state = EC_CMD_SENT;
   344             datagram->state = EC_CMD_SENT;
   342             command->t_sent = t_start;
   345             datagram->t_sent = t_start;
   343             command->index = master->command_index++;
   346             datagram->index = master->datagram_index++;
   344 
   347 
   345             if (unlikely(master->debug_level > 0))
   348             if (unlikely(master->debug_level > 1))
   346                 EC_DBG("adding command 0x%02X\n", command->index);
   349                 EC_DBG("adding datagram 0x%02X\n", datagram->index);
   347 
   350 
   348             // set "command following" flag in previous frame
   351             // set "datagram following" flag in previous frame
   349             if (follows_word)
   352             if (follows_word)
   350                 EC_WRITE_U16(follows_word, EC_READ_U16(follows_word) | 0x8000);
   353                 EC_WRITE_U16(follows_word, EC_READ_U16(follows_word) | 0x8000);
   351 
   354 
   352             // EtherCAT command header
   355             // EtherCAT datagram header
   353             EC_WRITE_U8 (cur_data,     command->type);
   356             EC_WRITE_U8 (cur_data,     datagram->type);
   354             EC_WRITE_U8 (cur_data + 1, command->index);
   357             EC_WRITE_U8 (cur_data + 1, datagram->index);
   355             EC_WRITE_U32(cur_data + 2, command->address.logical);
   358             EC_WRITE_U32(cur_data + 2, datagram->address.logical);
   356             EC_WRITE_U16(cur_data + 6, command->data_size & 0x7FF);
   359             EC_WRITE_U16(cur_data + 6, datagram->data_size & 0x7FF);
   357             EC_WRITE_U16(cur_data + 8, 0x0000);
   360             EC_WRITE_U16(cur_data + 8, 0x0000);
   358             follows_word = cur_data + 6;
   361             follows_word = cur_data + 6;
   359             cur_data += EC_COMMAND_HEADER_SIZE;
   362             cur_data += EC_DATAGRAM_HEADER_SIZE;
   360 
   363 
   361             // EtherCAT command data
   364             // EtherCAT datagram data
   362             memcpy(cur_data, command->data, command->data_size);
   365             memcpy(cur_data, datagram->data, datagram->data_size);
   363             cur_data += command->data_size;
   366             cur_data += datagram->data_size;
   364 
   367 
   365             // EtherCAT command footer
   368             // EtherCAT datagram footer
   366             EC_WRITE_U16(cur_data, 0x0000); // reset working counter
   369             EC_WRITE_U16(cur_data, 0x0000); // reset working counter
   367             cur_data += EC_COMMAND_FOOTER_SIZE;
   370             cur_data += EC_DATAGRAM_FOOTER_SIZE;
   368         }
   371         }
   369 
   372 
   370         if (cur_data - frame_data == EC_FRAME_HEADER_SIZE) {
   373         if (cur_data - frame_data == EC_FRAME_HEADER_SIZE) {
   371             if (unlikely(master->debug_level > 0))
   374             if (unlikely(master->debug_level > 1))
   372                 EC_DBG("nothing to send.\n");
   375                 EC_DBG("nothing to send.\n");
   373             break;
   376             break;
   374         }
   377         }
   375 
   378 
   376         // EtherCAT frame header
   379         // EtherCAT frame header
   379 
   382 
   380         // pad frame
   383         // pad frame
   381         while (cur_data - frame_data < ETH_ZLEN - ETH_HLEN)
   384         while (cur_data - frame_data < ETH_ZLEN - ETH_HLEN)
   382             EC_WRITE_U8(cur_data++, 0x00);
   385             EC_WRITE_U8(cur_data++, 0x00);
   383 
   386 
   384         if (unlikely(master->debug_level > 0))
   387         if (unlikely(master->debug_level > 1))
   385             EC_DBG("frame size: %i\n", cur_data - frame_data);
   388             EC_DBG("frame size: %i\n", cur_data - frame_data);
   386 
   389 
   387         // send frame
   390         // send frame
   388         ec_device_send(master->device, cur_data - frame_data);
   391         ec_device_send(master->device, cur_data - frame_data);
   389         frame_count++;
   392         frame_count++;
   390     }
   393     }
   391     while (more_commands_waiting);
   394     while (more_datagrams_waiting);
   392 
   395 
   393     if (unlikely(master->debug_level > 0)) {
   396     if (unlikely(master->debug_level > 1)) {
   394         t_end = get_cycles();
   397         t_end = get_cycles();
   395         EC_DBG("ec_master_send_commands sent %i frames in %ius.\n",
   398         EC_DBG("ec_master_send_datagrams sent %i frames in %ius.\n",
   396                frame_count, (u32) (t_end - t_start) * 1000 / cpu_khz);
   399                frame_count, (unsigned int) (t_end - t_start) * 1000 / cpu_khz);
   397     }
   400     }
   398 }
   401 }
   399 
   402 
   400 /*****************************************************************************/
   403 /*****************************************************************************/
   401 
   404 
   409                        const uint8_t *frame_data, /**< received data */
   412                        const uint8_t *frame_data, /**< received data */
   410                        size_t size /**< size of the received data */
   413                        size_t size /**< size of the received data */
   411                        )
   414                        )
   412 {
   415 {
   413     size_t frame_size, data_size;
   416     size_t frame_size, data_size;
   414     uint8_t command_type, command_index;
   417     uint8_t datagram_type, datagram_index;
   415     unsigned int cmd_follows, matched;
   418     unsigned int cmd_follows, matched;
   416     const uint8_t *cur_data;
   419     const uint8_t *cur_data;
   417     ec_command_t *command;
   420     ec_datagram_t *datagram;
   418 
   421 
   419     if (unlikely(size < EC_FRAME_HEADER_SIZE)) {
   422     if (unlikely(size < EC_FRAME_HEADER_SIZE)) {
   420         master->stats.corrupted++;
   423         master->stats.corrupted++;
   421         ec_master_output_stats(master);
   424         ec_master_output_stats(master);
   422         return;
   425         return;
   434         return;
   437         return;
   435     }
   438     }
   436 
   439 
   437     cmd_follows = 1;
   440     cmd_follows = 1;
   438     while (cmd_follows) {
   441     while (cmd_follows) {
   439         // process command header
   442         // process datagram header
   440         command_type  = EC_READ_U8 (cur_data);
   443         datagram_type  = EC_READ_U8 (cur_data);
   441         command_index = EC_READ_U8 (cur_data + 1);
   444         datagram_index = EC_READ_U8 (cur_data + 1);
   442         data_size     = EC_READ_U16(cur_data + 6) & 0x07FF;
   445         data_size      = EC_READ_U16(cur_data + 6) & 0x07FF;
   443         cmd_follows   = EC_READ_U16(cur_data + 6) & 0x8000;
   446         cmd_follows    = EC_READ_U16(cur_data + 6) & 0x8000;
   444         cur_data += EC_COMMAND_HEADER_SIZE;
   447         cur_data += EC_DATAGRAM_HEADER_SIZE;
   445 
   448 
   446         if (unlikely(cur_data - frame_data
   449         if (unlikely(cur_data - frame_data
   447                      + data_size + EC_COMMAND_FOOTER_SIZE > size)) {
   450                      + data_size + EC_DATAGRAM_FOOTER_SIZE > size)) {
   448             master->stats.corrupted++;
   451             master->stats.corrupted++;
   449             ec_master_output_stats(master);
   452             ec_master_output_stats(master);
   450             return;
   453             return;
   451         }
   454         }
   452 
   455 
   453         // search for matching command in the queue
   456         // search for matching datagram in the queue
   454         matched = 0;
   457         matched = 0;
   455         list_for_each_entry(command, &master->command_queue, queue) {
   458         list_for_each_entry(datagram, &master->datagram_queue, queue) {
   456             if (command->state == EC_CMD_SENT
   459             if (datagram->state == EC_CMD_SENT
   457                 && command->type == command_type
   460                 && datagram->type == datagram_type
   458                 && command->index == command_index
   461                 && datagram->index == datagram_index
   459                 && command->data_size == data_size) {
   462                 && datagram->data_size == data_size) {
   460                 matched = 1;
   463                 matched = 1;
   461                 break;
   464                 break;
   462             }
   465             }
   463         }
   466         }
   464 
   467 
   465         // no matching command was found
   468         // no matching datagram was found
   466         if (!matched) {
   469         if (!matched) {
   467             master->stats.unmatched++;
   470             master->stats.unmatched++;
   468             ec_master_output_stats(master);
   471             ec_master_output_stats(master);
   469             cur_data += data_size + EC_COMMAND_FOOTER_SIZE;
   472             cur_data += data_size + EC_DATAGRAM_FOOTER_SIZE;
   470             continue;
   473             continue;
   471         }
   474         }
   472 
   475 
   473         // copy received data in the command memory
   476         // copy received data in the datagram memory
   474         memcpy(command->data, cur_data, data_size);
   477         memcpy(datagram->data, cur_data, data_size);
   475         cur_data += data_size;
   478         cur_data += data_size;
   476 
   479 
   477         // set the command's working counter
   480         // set the datagram's working counter
   478         command->working_counter = EC_READ_U16(cur_data);
   481         datagram->working_counter = EC_READ_U16(cur_data);
   479         cur_data += EC_COMMAND_FOOTER_SIZE;
   482         cur_data += EC_DATAGRAM_FOOTER_SIZE;
   480 
   483 
   481         // dequeue the received command
   484         // dequeue the received datagram
   482         command->state = EC_CMD_RECEIVED;
   485         datagram->state = EC_CMD_RECEIVED;
   483         list_del_init(&command->queue);
   486         list_del_init(&datagram->queue);
   484     }
   487     }
   485 }
   488 }
   486 
   489 
   487 /*****************************************************************************/
   490 /*****************************************************************************/
   488 
   491 
   489 /**
   492 /**
   490    Sends a single command and waits for its reception.
   493    Sends a single datagram and waits for its reception.
   491    If the slave doesn't respond, the command is sent again.
   494    If the slave doesn't respond, the datagram is sent again.
   492    \return 0 in case of success, else < 0
   495    \return 0 in case of success, else < 0
   493 */
   496 */
   494 
   497 
   495 int ec_master_simple_io(ec_master_t *master, /**< EtherCAT master */
   498 int ec_master_simple_io(ec_master_t *master, /**< EtherCAT master */
   496                         ec_command_t *command /**< command */
   499                         ec_datagram_t *datagram /**< datagram */
   497                         )
   500                         )
   498 {
   501 {
   499     unsigned int response_tries_left;
   502     unsigned int response_tries_left;
   500 
   503 
   501     response_tries_left = 10000;
   504     response_tries_left = 10000;
   502 
   505 
   503     while (1)
   506     while (1)
   504     {
   507     {
   505         ec_master_queue_command(master, command);
   508         ec_master_queue_datagram(master, datagram);
   506         ecrt_master_sync_io(master);
   509         ecrt_master_sync_io(master);
   507 
   510 
   508         if (command->state == EC_CMD_RECEIVED) {
   511         if (datagram->state == EC_CMD_RECEIVED) {
   509             if (likely(command->working_counter))
   512             if (likely(datagram->working_counter))
   510                 return 0;
   513                 return 0;
   511         }
   514         }
   512         else if (command->state == EC_CMD_TIMEOUT) {
   515         else if (datagram->state == EC_CMD_TIMEOUT) {
   513             EC_ERR("Simple-IO TIMEOUT!\n");
   516             EC_ERR("Simple-IO TIMEOUT!\n");
   514             return -1;
   517             return -1;
   515         }
   518         }
   516         else if (command->state == EC_CMD_ERROR) {
   519         else if (datagram->state == EC_CMD_ERROR) {
   517             EC_ERR("Simple-IO command error!\n");
   520             EC_ERR("Simple-IO datagram error!\n");
   518             return -1;
   521             return -1;
   519         }
   522         }
   520 
   523 
   521         // no direct response, wait a little bit...
   524         // no direct response, wait a little bit...
   522         udelay(100);
   525         udelay(100);
   538 
   541 
   539 int ec_master_bus_scan(ec_master_t *master /**< EtherCAT master */)
   542 int ec_master_bus_scan(ec_master_t *master /**< EtherCAT master */)
   540 {
   543 {
   541     ec_slave_t *slave;
   544     ec_slave_t *slave;
   542     ec_slave_ident_t *ident;
   545     ec_slave_ident_t *ident;
   543     ec_command_t *command;
   546     ec_datagram_t *datagram;
   544     unsigned int i;
   547     unsigned int i;
   545     uint16_t coupler_index, coupler_subindex;
   548     uint16_t coupler_index, coupler_subindex;
   546     uint16_t reverse_coupler_index, current_coupler_index;
   549     uint16_t reverse_coupler_index, current_coupler_index;
   547 
   550 
   548     if (!list_empty(&master->slaves)) {
   551     if (!list_empty(&master->slaves)) {
   549         EC_ERR("Slave scan already done!\n");
   552         EC_ERR("Slave scan already done!\n");
   550         return -1;
   553         return -1;
   551     }
   554     }
   552 
   555 
   553     command = &master->simple_command;
   556     datagram = &master->simple_datagram;
   554 
   557 
   555     // determine number of slaves on bus
   558     // determine number of slaves on bus
   556     if (ec_command_brd(command, 0x0000, 4)) return -1;
   559     if (ec_datagram_brd(datagram, 0x0000, 4)) return -1;
   557     if (unlikely(ec_master_simple_io(master, command))) return -1;
   560     if (unlikely(ec_master_simple_io(master, datagram))) return -1;
   558     master->slave_count = command->working_counter;
   561     master->slave_count = datagram->working_counter;
   559     EC_INFO("Found %i slaves on bus.\n", master->slave_count);
   562     EC_INFO("Found %i slaves on bus.\n", master->slave_count);
   560 
   563 
   561     if (!master->slave_count) return 0;
   564     if (!master->slave_count) return 0;
   562 
   565 
   563     // init slaves
   566     // init slaves
   586 
   589 
   587     // for every slave on the bus
   590     // for every slave on the bus
   588     list_for_each_entry(slave, &master->slaves, list) {
   591     list_for_each_entry(slave, &master->slaves, list) {
   589 
   592 
   590         // write station address
   593         // write station address
   591         if (ec_command_apwr(command, slave->ring_position, 0x0010,
   594         if (ec_datagram_apwr(datagram, slave->ring_position, 0x0010,
   592                             sizeof(uint16_t))) goto out_free;
   595                             sizeof(uint16_t))) goto out_free;
   593         EC_WRITE_U16(command->data, slave->station_address);
   596         EC_WRITE_U16(datagram->data, slave->station_address);
   594         if (unlikely(ec_master_simple_io(master, command))) {
   597         if (unlikely(ec_master_simple_io(master, datagram))) {
   595             EC_ERR("Writing station address failed on slave %i!\n",
   598             EC_ERR("Writing station address failed on slave %i!\n",
   596                    slave->ring_position);
   599                    slave->ring_position);
   597             goto out_free;
   600             goto out_free;
   598         }
   601         }
   599 
   602 
   648 {
   651 {
   649     cycles_t t_now = get_cycles();
   652     cycles_t t_now = get_cycles();
   650 
   653 
   651     if (unlikely((u32) (t_now - master->stats.t_last) / cpu_khz > 1000)) {
   654     if (unlikely((u32) (t_now - master->stats.t_last) / cpu_khz > 1000)) {
   652         if (master->stats.timeouts) {
   655         if (master->stats.timeouts) {
   653             EC_WARN("%i commands TIMED OUT!\n", master->stats.timeouts);
   656             EC_WARN("%i datagrams TIMED OUT!\n", master->stats.timeouts);
   654             master->stats.timeouts = 0;
   657             master->stats.timeouts = 0;
   655         }
   658         }
   656         if (master->stats.delayed) {
   659         if (master->stats.delayed) {
   657             EC_WARN("%i frame(s) DELAYED!\n", master->stats.delayed);
   660             EC_WARN("%i frame(s) DELAYED!\n", master->stats.delayed);
   658             master->stats.delayed = 0;
   661             master->stats.delayed = 0;
   660         if (master->stats.corrupted) {
   663         if (master->stats.corrupted) {
   661             EC_WARN("%i frame(s) CORRUPTED!\n", master->stats.corrupted);
   664             EC_WARN("%i frame(s) CORRUPTED!\n", master->stats.corrupted);
   662             master->stats.corrupted = 0;
   665             master->stats.corrupted = 0;
   663         }
   666         }
   664         if (master->stats.unmatched) {
   667         if (master->stats.unmatched) {
   665             EC_WARN("%i command(s) UNMATCHED!\n", master->stats.unmatched);
   668             EC_WARN("%i datagram(s) UNMATCHED!\n", master->stats.unmatched);
   666             master->stats.unmatched = 0;
   669             master->stats.unmatched = 0;
   667         }
   670         }
   668         master->stats.t_last = t_now;
   671         master->stats.t_last = t_now;
   669     }
   672     }
   670 }
   673 }
   671 
   674 
   672 /*****************************************************************************/
   675 /*****************************************************************************/
   673 
   676 
   674 /**
   677 /**
   675    Starts the Free-Run mode.
   678    Starts the Idle mode.
   676 */
   679 */
   677 
   680 
   678 void ec_master_freerun_start(ec_master_t *master /**< EtherCAT master */)
   681 void ec_master_idle_start(ec_master_t *master /**< EtherCAT master */)
   679 {
   682 {
   680     if (master->mode == EC_MASTER_MODE_FREERUN) return;
   683     if (master->mode == EC_MASTER_MODE_IDLE) return;
   681 
   684 
   682     if (master->mode == EC_MASTER_MODE_RUNNING) {
   685     if (master->mode == EC_MASTER_MODE_RUNNING) {
   683         EC_ERR("ec_master_freerun_start: Master already running!\n");
   686         EC_ERR("ec_master_idle_start: Master already running!\n");
   684         return;
   687         return;
   685     }
   688     }
   686 
   689 
   687     EC_INFO("Starting Free-Run mode.\n");
   690     EC_INFO("Starting Idle mode.\n");
   688 
   691 
   689     master->mode = EC_MASTER_MODE_FREERUN;
   692     master->mode = EC_MASTER_MODE_IDLE;
   690     ec_fsm_reset(&master->fsm);
   693     ec_fsm_reset(&master->fsm);
   691     queue_delayed_work(master->workqueue, &master->freerun_work, 1);
   694     queue_delayed_work(master->workqueue, &master->idle_work, 1);
   692 }
   695 }
   693 
   696 
   694 /*****************************************************************************/
   697 /*****************************************************************************/
   695 
   698 
   696 /**
   699 /**
   697    Stops the Free-Run mode.
   700    Stops the Idle mode.
   698 */
   701 */
   699 
   702 
   700 void ec_master_freerun_stop(ec_master_t *master /**< EtherCAT master */)
   703 void ec_master_idle_stop(ec_master_t *master /**< EtherCAT master */)
   701 {
   704 {
   702     if (master->mode != EC_MASTER_MODE_FREERUN) return;
   705     if (master->mode != EC_MASTER_MODE_IDLE) return;
   703 
   706 
   704     ec_master_eoe_stop(master);
   707     ec_master_eoe_stop(master);
   705 
   708 
   706     EC_INFO("Stopping Free-Run mode.\n");
   709     EC_INFO("Stopping Idle mode.\n");
   707     master->mode = EC_MASTER_MODE_IDLE;
   710     master->mode = EC_MASTER_MODE_ORPHANED; // this is important for the
   708 
   711                                             // IDLE work function to not
   709     if (!cancel_delayed_work(&master->freerun_work)) {
   712                                             // queue itself again
       
   713 
       
   714     if (!cancel_delayed_work(&master->idle_work)) {
   710         flush_workqueue(master->workqueue);
   715         flush_workqueue(master->workqueue);
   711     }
   716     }
   712 
   717 
   713     ec_master_clear_slaves(master);
   718     ec_master_clear_slaves(master);
   714 }
   719 }
   715 
   720 
   716 /*****************************************************************************/
   721 /*****************************************************************************/
   717 
   722 
   718 /**
   723 /**
   719    Free-Run mode function.
   724    Idle mode function.
   720 */
   725 */
   721 
   726 
   722 void ec_master_freerun(void *data /**< master pointer */)
   727 void ec_master_idle_run(void *data /**< master pointer */)
   723 {
   728 {
   724     ec_master_t *master = (ec_master_t *) data;
   729     ec_master_t *master = (ec_master_t *) data;
   725 
   730 
   726     // aquire master lock
   731     // aquire master lock
   727     spin_lock_bh(&master->internal_lock);
   732     spin_lock_bh(&master->internal_lock);
   734     ecrt_master_async_send(master);
   739     ecrt_master_async_send(master);
   735 
   740 
   736     // release master lock
   741     // release master lock
   737     spin_unlock_bh(&master->internal_lock);
   742     spin_unlock_bh(&master->internal_lock);
   738 
   743 
   739     if (master->mode == EC_MASTER_MODE_FREERUN)
   744     if (master->mode == EC_MASTER_MODE_IDLE)
   740         queue_delayed_work(master->workqueue, &master->freerun_work, 1);
   745         queue_delayed_work(master->workqueue, &master->idle_work, 1);
   741 }
   746 }
   742 
   747 
   743 /*****************************************************************************/
   748 /*****************************************************************************/
   744 
   749 
   745 /**
   750 /**
   769    Initializes a sync manager configuration page with EEPROM data.
   774    Initializes a sync manager configuration page with EEPROM data.
   770    The referenced memory (\a data) must be at least EC_SYNC_SIZE bytes.
   775    The referenced memory (\a data) must be at least EC_SYNC_SIZE bytes.
   771 */
   776 */
   772 
   777 
   773 void ec_eeprom_sync_config(const ec_eeprom_sync_t *sync, /**< sync manager */
   778 void ec_eeprom_sync_config(const ec_eeprom_sync_t *sync, /**< sync manager */
       
   779                            const ec_slave_t *slave, /**< EtherCAT slave */
   774                            uint8_t *data /**> configuration memory */
   780                            uint8_t *data /**> configuration memory */
   775                            )
   781                            )
   776 {
   782 {
       
   783     size_t sync_size;
       
   784 
       
   785     sync_size = ec_slave_calc_eeprom_sync_size(slave, sync);
       
   786 
       
   787     if (slave->master->debug_level) {
       
   788         EC_INFO("Slave %i, sync manager %i:\n", slave->ring_position,
       
   789                 sync->index);
       
   790         EC_INFO("  Address: 0x%04X\n", sync->physical_start_address);
       
   791         EC_INFO("     Size: %i\n", sync_size);
       
   792         EC_INFO("  Control: 0x%02X\n", sync->control_register);
       
   793     }
       
   794 
   777     EC_WRITE_U16(data,     sync->physical_start_address);
   795     EC_WRITE_U16(data,     sync->physical_start_address);
   778     EC_WRITE_U16(data + 2, sync->length);
   796     EC_WRITE_U16(data + 2, sync_size);
   779     EC_WRITE_U8 (data + 4, sync->control_register);
   797     EC_WRITE_U8 (data + 4, sync->control_register);
   780     EC_WRITE_U8 (data + 5, 0x00); // status byte (read only)
   798     EC_WRITE_U8 (data + 5, 0x00); // status byte (read only)
   781     EC_WRITE_U16(data + 6, sync->enable ? 0x0001 : 0x0000); // enable
   799     EC_WRITE_U16(data + 6, sync->enable ? 0x0001 : 0x0000); // enable
   782 }
   800 }
   783 
   801 
   825     if (attr == &attr_slave_count) {
   843     if (attr == &attr_slave_count) {
   826         return sprintf(buffer, "%i\n", master->slave_count);
   844         return sprintf(buffer, "%i\n", master->slave_count);
   827     }
   845     }
   828     else if (attr == &attr_mode) {
   846     else if (attr == &attr_mode) {
   829         switch (master->mode) {
   847         switch (master->mode) {
       
   848             case EC_MASTER_MODE_ORPHANED:
       
   849                 return sprintf(buffer, "ORPHANED\n");
   830             case EC_MASTER_MODE_IDLE:
   850             case EC_MASTER_MODE_IDLE:
   831                 return sprintf(buffer, "IDLE\n");
   851                 return sprintf(buffer, "IDLE\n");
   832             case EC_MASTER_MODE_FREERUN:
       
   833                 return sprintf(buffer, "FREERUN\n");
       
   834             case EC_MASTER_MODE_RUNNING:
   852             case EC_MASTER_MODE_RUNNING:
   835                 return sprintf(buffer, "RUNNING\n");
   853                 return sprintf(buffer, "RUNNING\n");
   836         }
   854         }
       
   855     }
       
   856     else if (attr == &attr_debug_level) {
       
   857         return sprintf(buffer, "%i\n", master->debug_level);
       
   858     }
       
   859     else if (attr == &attr_eeprom_write_enable) {
       
   860         return sprintf(buffer, "%i\n", master->eeprom_write_enable);
   837     }
   861     }
   838 
   862 
   839     return 0;
   863     return 0;
   840 }
   864 }
   841 
   865 
   870 
   894 
   871         if (master->eeprom_write_enable) {
   895         if (master->eeprom_write_enable) {
   872             master->eeprom_write_enable = 0;
   896             master->eeprom_write_enable = 0;
   873             EC_INFO("Slave EEPROM writing disabled.\n");
   897             EC_INFO("Slave EEPROM writing disabled.\n");
   874         }
   898         }
       
   899     }
       
   900     else if (attr == &attr_debug_level) {
       
   901         if (!strcmp(buffer, "0\n")) {
       
   902             master->debug_level = 0;
       
   903         }
       
   904         else if (!strcmp(buffer, "1\n")) {
       
   905             master->debug_level = 1;
       
   906         }
       
   907         else if (!strcmp(buffer, "2\n")) {
       
   908             master->debug_level = 2;
       
   909         }
       
   910         else {
       
   911             EC_ERR("Invalid debug level value!\n");
       
   912             return -EINVAL;
       
   913         }
       
   914 
       
   915         EC_INFO("Master debug level set to %i.\n", master->debug_level);
       
   916         return size;
   875     }
   917     }
   876 
   918 
   877     return -EINVAL;
   919     return -EINVAL;
   878 }
   920 }
   879 
   921 
   912                 slave->requested_state = EC_SLAVE_STATE_OP;
   954                 slave->requested_state = EC_SLAVE_STATE_OP;
   913             }
   955             }
   914             else {
   956             else {
   915                 slave->requested_state = EC_SLAVE_STATE_INIT;
   957                 slave->requested_state = EC_SLAVE_STATE_INIT;
   916             }
   958             }
   917             slave->state_error = 0;
   959             slave->error_flag = 0;
   918             break;
   960             break;
   919         }
   961         }
   920 
   962 
   921         if (!found) {
   963         if (!found) {
   922             EC_WARN("No EoE handler for slave %i!\n", slave->ring_position);
   964             EC_WARN("No EoE handler for slave %i!\n", slave->ring_position);
   923             slave->requested_state = EC_SLAVE_STATE_INIT;
   965             slave->requested_state = EC_SLAVE_STATE_INIT;
   924             slave->state_error = 0;
   966             slave->error_flag = 0;
   925         }
   967         }
   926     }
   968     }
   927 
   969 
   928     if (!coupled) {
   970     if (!coupled) {
   929         EC_INFO("No EoE handlers coupled.\n");
   971         EC_INFO("No EoE handlers coupled.\n");
   957 
   999 
   958     // decouple all EoE handlers
  1000     // decouple all EoE handlers
   959     list_for_each_entry(eoe, &master->eoe_handlers, list) {
  1001     list_for_each_entry(eoe, &master->eoe_handlers, list) {
   960         if (eoe->slave) {
  1002         if (eoe->slave) {
   961             eoe->slave->requested_state = EC_SLAVE_STATE_INIT;
  1003             eoe->slave->requested_state = EC_SLAVE_STATE_INIT;
   962             eoe->slave->state_error = 0;
  1004             eoe->slave->error_flag = 0;
   963             eoe->slave = NULL;
  1005             eoe->slave = NULL;
   964         }
  1006         }
   965     }
  1007     }
   966 
  1008 
   967     master->eoe_running = 0;
  1009     master->eoe_running = 0;
   987     if (master->mode == EC_MASTER_MODE_RUNNING) {
  1029     if (master->mode == EC_MASTER_MODE_RUNNING) {
   988         // request_cb must return 0, if the lock has been aquired!
  1030         // request_cb must return 0, if the lock has been aquired!
   989         if (master->request_cb(master->cb_data))
  1031         if (master->request_cb(master->cb_data))
   990             goto queue_timer;
  1032             goto queue_timer;
   991     }
  1033     }
   992     else if (master->mode == EC_MASTER_MODE_FREERUN) {
  1034     else if (master->mode == EC_MASTER_MODE_IDLE) {
   993         spin_lock(&master->internal_lock);
  1035         spin_lock(&master->internal_lock);
   994     }
  1036     }
   995     else
  1037     else
   996         goto queue_timer;
  1038         goto queue_timer;
   997 
  1039 
  1004 
  1046 
  1005     // release lock...
  1047     // release lock...
  1006     if (master->mode == EC_MASTER_MODE_RUNNING) {
  1048     if (master->mode == EC_MASTER_MODE_RUNNING) {
  1007         master->release_cb(master->cb_data);
  1049         master->release_cb(master->cb_data);
  1008     }
  1050     }
  1009     else if (master->mode == EC_MASTER_MODE_FREERUN) {
  1051     else if (master->mode == EC_MASTER_MODE_IDLE) {
  1010         spin_unlock(&master->internal_lock);
  1052         spin_unlock(&master->internal_lock);
  1011     }
  1053     }
  1012 
  1054 
  1013  queue_timer:
  1055  queue_timer:
  1014     master->eoe_timer.expires += HZ / EC_EOE_FREQUENCY;
  1056     master->eoe_timer.expires += HZ / EC_EOE_FREQUENCY;
  1073 
  1115 
  1074 int ecrt_master_activate(ec_master_t *master /**< EtherCAT master */)
  1116 int ecrt_master_activate(ec_master_t *master /**< EtherCAT master */)
  1075 {
  1117 {
  1076     unsigned int j;
  1118     unsigned int j;
  1077     ec_slave_t *slave;
  1119     ec_slave_t *slave;
  1078     ec_command_t *command;
  1120     ec_datagram_t *datagram;
  1079     const ec_sync_t *sync;
  1121     const ec_sync_t *sync;
  1080     const ec_slave_type_t *type;
  1122     const ec_slave_type_t *type;
  1081     const ec_fmmu_t *fmmu;
  1123     const ec_fmmu_t *fmmu;
  1082     uint32_t domain_offset;
  1124     uint32_t domain_offset;
  1083     ec_domain_t *domain;
  1125     ec_domain_t *domain;
  1084     ec_eeprom_sync_t *eeprom_sync, mbox_sync;
  1126     ec_eeprom_sync_t *eeprom_sync, mbox_sync;
  1085 
  1127 
  1086     command = &master->simple_command;
  1128     datagram = &master->simple_datagram;
  1087 
  1129 
  1088     // allocate all domains
  1130     // allocate all domains
  1089     domain_offset = 0;
  1131     domain_offset = 0;
  1090     list_for_each_entry(domain, &master->domains, list) {
  1132     list_for_each_entry(domain, &master->domains, list) {
  1091         if (ec_domain_alloc(domain, domain_offset)) {
  1133         if (ec_domain_alloc(domain, domain_offset)) {
  1110         // check and reset CRC fault counters
  1152         // check and reset CRC fault counters
  1111         ec_slave_check_crc(slave);
  1153         ec_slave_check_crc(slave);
  1112 
  1154 
  1113         // reset FMMUs
  1155         // reset FMMUs
  1114         if (slave->base_fmmu_count) {
  1156         if (slave->base_fmmu_count) {
  1115             if (ec_command_npwr(command, slave->station_address, 0x0600,
  1157             if (ec_datagram_npwr(datagram, slave->station_address, 0x0600,
  1116                                 EC_FMMU_SIZE * slave->base_fmmu_count))
  1158                                  EC_FMMU_SIZE * slave->base_fmmu_count))
  1117                 return -1;
  1159                 return -1;
  1118             memset(command->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count);
  1160             memset(datagram->data, 0x00,
  1119             if (unlikely(ec_master_simple_io(master, command))) {
  1161                    EC_FMMU_SIZE * slave->base_fmmu_count);
       
  1162             if (unlikely(ec_master_simple_io(master, datagram))) {
  1120                 EC_ERR("Resetting FMMUs failed on slave %i!\n",
  1163                 EC_ERR("Resetting FMMUs failed on slave %i!\n",
  1121                        slave->ring_position);
  1164                        slave->ring_position);
  1122                 return -1;
  1165                 return -1;
  1123             }
  1166             }
  1124         }
  1167         }
  1125 
  1168 
  1126         // reset sync managers
  1169         // reset sync managers
  1127         if (slave->base_sync_count) {
  1170         if (slave->base_sync_count) {
  1128             if (ec_command_npwr(command, slave->station_address, 0x0800,
  1171             if (ec_datagram_npwr(datagram, slave->station_address, 0x0800,
  1129                                 EC_SYNC_SIZE * slave->base_sync_count))
  1172                                 EC_SYNC_SIZE * slave->base_sync_count))
  1130                 return -1;
  1173                 return -1;
  1131             memset(command->data, 0x00, EC_SYNC_SIZE * slave->base_sync_count);
  1174             memset(datagram->data, 0x00,
  1132             if (unlikely(ec_master_simple_io(master, command))) {
  1175                    EC_SYNC_SIZE * slave->base_sync_count);
       
  1176             if (unlikely(ec_master_simple_io(master, datagram))) {
  1133                 EC_ERR("Resetting sync managers failed on slave %i!\n",
  1177                 EC_ERR("Resetting sync managers failed on slave %i!\n",
  1134                        slave->ring_position);
  1178                        slave->ring_position);
  1135                 return -1;
  1179                 return -1;
  1136             }
  1180             }
  1137         }
  1181         }
  1138 
  1182 
  1139         // configure sync managers
  1183         // configure sync managers
  1140         if (type) { // known slave type, take type's SM information
  1184 
       
  1185         // does the slave provide sync manager information?
       
  1186         if (!list_empty(&slave->eeprom_syncs)) {
       
  1187             list_for_each_entry(eeprom_sync, &slave->eeprom_syncs, list) {
       
  1188                 if (ec_datagram_npwr(datagram, slave->station_address,
       
  1189                                      0x800 + eeprom_sync->index *
       
  1190                                      EC_SYNC_SIZE,
       
  1191                                      EC_SYNC_SIZE)) return -1;
       
  1192                 ec_eeprom_sync_config(eeprom_sync, slave, datagram->data);
       
  1193                 if (unlikely(ec_master_simple_io(master, datagram))) {
       
  1194                     EC_ERR("Setting sync manager %i failed on slave %i!\n",
       
  1195                            eeprom_sync->index, slave->ring_position);
       
  1196                     return -1;
       
  1197                 }
       
  1198             }
       
  1199         }
       
  1200 
       
  1201         else if (type) { // known slave type, take type's SM information
  1141             for (j = 0; type->sync_managers[j] && j < EC_MAX_SYNC; j++) {
  1202             for (j = 0; type->sync_managers[j] && j < EC_MAX_SYNC; j++) {
  1142                 sync = type->sync_managers[j];
  1203                 sync = type->sync_managers[j];
  1143                 if (ec_command_npwr(command, slave->station_address,
  1204                 if (ec_datagram_npwr(datagram, slave->station_address,
  1144                                     0x0800 + j * EC_SYNC_SIZE, EC_SYNC_SIZE))
  1205                                      0x0800 + j * EC_SYNC_SIZE, EC_SYNC_SIZE))
  1145                     return -1;
  1206                     return -1;
  1146                 ec_sync_config(sync, slave, command->data);
  1207                 ec_sync_config(sync, slave, datagram->data);
  1147                 if (unlikely(ec_master_simple_io(master, command))) {
  1208                 if (unlikely(ec_master_simple_io(master, datagram))) {
  1148                     EC_ERR("Setting sync manager %i failed on slave %i!\n",
  1209                     EC_ERR("Setting sync manager %i failed on slave %i!\n",
  1149                            j, slave->ring_position);
  1210                            j, slave->ring_position);
  1150                     return -1;
  1211                     return -1;
  1151                 }
  1212                 }
  1152             }
  1213             }
  1153         }
  1214         }
  1154         else if (slave->sii_mailbox_protocols) { // unknown type, but mailbox
  1215 
  1155             // does the device supply SM configurations in its EEPROM?
  1216         // no sync manager information; guess mailbox settings
  1156 	    if (!list_empty(&slave->eeprom_syncs)) {
  1217         else if (slave->sii_mailbox_protocols) { 
  1157 		list_for_each_entry(eeprom_sync, &slave->eeprom_syncs, list) {
  1218             mbox_sync.physical_start_address =
  1158 		    EC_INFO("Sync manager %i...\n", eeprom_sync->index);
  1219                 slave->sii_rx_mailbox_offset;
  1159 		    if (ec_command_npwr(command, slave->station_address,
  1220             mbox_sync.length = slave->sii_rx_mailbox_size;
  1160 					0x800 + eeprom_sync->index *
  1221             mbox_sync.control_register = 0x26;
  1161 					EC_SYNC_SIZE,
  1222             mbox_sync.enable = 1;
  1162 					EC_SYNC_SIZE)) return -1;
  1223             if (ec_datagram_npwr(datagram, slave->station_address,
  1163 		    ec_eeprom_sync_config(eeprom_sync, command->data);
  1224                                  0x800,EC_SYNC_SIZE)) return -1;
  1164 		    if (unlikely(ec_master_simple_io(master, command))) {
  1225             ec_eeprom_sync_config(&mbox_sync, slave, datagram->data);
  1165 			EC_ERR("Setting sync manager %i failed on slave %i!\n",
  1226             if (unlikely(ec_master_simple_io(master, datagram))) {
  1166 			       eeprom_sync->index, slave->ring_position);
  1227                 EC_ERR("Setting sync manager 0 failed on slave %i!\n",
  1167 			return -1;
  1228                        slave->ring_position);
  1168 		    }
  1229                 return -1;
  1169 		}
       
  1170             }
  1230             }
  1171 	    else { // no sync manager information; guess mailbox settings
  1231 
  1172 		mbox_sync.physical_start_address =
  1232             mbox_sync.physical_start_address =
  1173                     slave->sii_rx_mailbox_offset;
  1233                 slave->sii_tx_mailbox_offset;
  1174 		mbox_sync.length = slave->sii_rx_mailbox_size;
  1234             mbox_sync.length = slave->sii_tx_mailbox_size;
  1175 		mbox_sync.control_register = 0x26;
  1235             mbox_sync.control_register = 0x22;
  1176 		mbox_sync.enable = 1;
  1236             mbox_sync.enable = 1;
  1177 		if (ec_command_npwr(command, slave->station_address,
  1237             if (ec_datagram_npwr(datagram, slave->station_address,
  1178 				    0x800,EC_SYNC_SIZE)) return -1;
  1238                                  0x808, EC_SYNC_SIZE)) return -1;
  1179 		ec_eeprom_sync_config(&mbox_sync, command->data);
  1239             ec_eeprom_sync_config(&mbox_sync, slave, datagram->data);
  1180 		if (unlikely(ec_master_simple_io(master, command))) {
  1240             if (unlikely(ec_master_simple_io(master, datagram))) {
  1181 		    EC_ERR("Setting sync manager 0 failed on slave %i!\n",
       
  1182 			   slave->ring_position);
       
  1183 		    return -1;
       
  1184 		}
       
  1185 
       
  1186 		mbox_sync.physical_start_address =
       
  1187                     slave->sii_tx_mailbox_offset;
       
  1188 		mbox_sync.length = slave->sii_tx_mailbox_size;
       
  1189 		mbox_sync.control_register = 0x22;
       
  1190 		mbox_sync.enable = 1;
       
  1191 		if (ec_command_npwr(command, slave->station_address,
       
  1192 				    0x808, EC_SYNC_SIZE)) return -1;
       
  1193 		ec_eeprom_sync_config(&mbox_sync, command->data);
       
  1194 		if (unlikely(ec_master_simple_io(master, command))) {
       
  1195 		    EC_ERR("Setting sync manager 1 failed on slave %i!\n",
  1241 		    EC_ERR("Setting sync manager 1 failed on slave %i!\n",
  1196 			   slave->ring_position);
  1242 			   slave->ring_position);
  1197 		    return -1;
  1243 		    return -1;
  1198 		}
  1244             }
  1199 	    }
       
  1200 	    EC_INFO("Mailbox configured for unknown slave %i\n",
       
  1201 		    slave->ring_position);
       
  1202         }
  1245         }
  1203 
  1246 
  1204         // change state to PREOP
  1247         // change state to PREOP
  1205         if (unlikely(ec_slave_state_change(slave, EC_SLAVE_STATE_PREOP)))
  1248         if (unlikely(ec_slave_state_change(slave, EC_SLAVE_STATE_PREOP)))
  1206             return -1;
  1249             return -1;
  1216         }
  1259         }
  1217 
  1260 
  1218         // configure FMMUs
  1261         // configure FMMUs
  1219         for (j = 0; j < slave->fmmu_count; j++) {
  1262         for (j = 0; j < slave->fmmu_count; j++) {
  1220             fmmu = &slave->fmmus[j];
  1263             fmmu = &slave->fmmus[j];
  1221             if (ec_command_npwr(command, slave->station_address,
  1264             if (ec_datagram_npwr(datagram, slave->station_address,
  1222                                 0x0600 + j * EC_FMMU_SIZE, EC_FMMU_SIZE))
  1265                                  0x0600 + j * EC_FMMU_SIZE, EC_FMMU_SIZE))
  1223                 return -1;
  1266                 return -1;
  1224             ec_fmmu_config(fmmu, slave, command->data);
  1267             ec_fmmu_config(fmmu, slave, datagram->data);
  1225             if (unlikely(ec_master_simple_io(master, command))) {
  1268             if (unlikely(ec_master_simple_io(master, datagram))) {
  1226                 EC_ERR("Setting FMMU %i failed on slave %i!\n",
  1269                 EC_ERR("Setting FMMU %i failed on slave %i!\n",
  1227                        j, slave->ring_position);
  1270                        j, slave->ring_position);
  1228                 return -1;
  1271                 return -1;
  1229             }
  1272             }
  1230         }
  1273         }
  1286 }
  1329 }
  1287 
  1330 
  1288 /*****************************************************************************/
  1331 /*****************************************************************************/
  1289 
  1332 
  1290 /**
  1333 /**
  1291    Sends queued commands and waits for their reception.
  1334    Sends queued datagrams and waits for their reception.
  1292    \ingroup RealtimeInterface
  1335    \ingroup RealtimeInterface
  1293 */
  1336 */
  1294 
  1337 
  1295 void ecrt_master_sync_io(ec_master_t *master /**< EtherCAT master */)
  1338 void ecrt_master_sync_io(ec_master_t *master /**< EtherCAT master */)
  1296 {
  1339 {
  1297     ec_command_t *command, *n;
  1340     ec_datagram_t *datagram, *n;
  1298     unsigned int commands_sent;
  1341     unsigned int datagrams_sent;
  1299     cycles_t t_start, t_end, t_timeout;
  1342     cycles_t t_start, t_end, t_timeout;
  1300 
  1343 
  1301     // send commands
  1344     // send datagrams
  1302     ecrt_master_async_send(master);
  1345     ecrt_master_async_send(master);
  1303 
  1346 
  1304     t_start = get_cycles(); // measure io time
  1347     t_start = get_cycles(); // measure io time
  1305     t_timeout = (cycles_t) master->timeout * (cpu_khz / 1000);
  1348     t_timeout = (cycles_t) master->timeout * (cpu_khz / 1000);
  1306 
  1349 
  1308         ec_device_call_isr(master->device);
  1351         ec_device_call_isr(master->device);
  1309 
  1352 
  1310         t_end = get_cycles(); // take current time
  1353         t_end = get_cycles(); // take current time
  1311         if (t_end - t_start >= t_timeout) break; // timeout!
  1354         if (t_end - t_start >= t_timeout) break; // timeout!
  1312 
  1355 
  1313         commands_sent = 0;
  1356         datagrams_sent = 0;
  1314         list_for_each_entry_safe(command, n, &master->command_queue, queue) {
  1357         list_for_each_entry_safe(datagram, n, &master->datagram_queue, queue) {
  1315             if (command->state == EC_CMD_RECEIVED)
  1358             if (datagram->state == EC_CMD_RECEIVED)
  1316                 list_del_init(&command->queue);
  1359                 list_del_init(&datagram->queue);
  1317             else if (command->state == EC_CMD_SENT)
  1360             else if (datagram->state == EC_CMD_SENT)
  1318                 commands_sent++;
  1361                 datagrams_sent++;
  1319         }
  1362         }
  1320 
  1363 
  1321         if (!commands_sent) break;
  1364         if (!datagrams_sent) break;
  1322     }
  1365     }
  1323 
  1366 
  1324     // timeout; dequeue all commands
  1367     // timeout; dequeue all datagrams
  1325     list_for_each_entry_safe(command, n, &master->command_queue, queue) {
  1368     list_for_each_entry_safe(datagram, n, &master->datagram_queue, queue) {
  1326         switch (command->state) {
  1369         switch (datagram->state) {
  1327             case EC_CMD_SENT:
  1370             case EC_CMD_SENT:
  1328             case EC_CMD_QUEUED:
  1371             case EC_CMD_QUEUED:
  1329                 command->state = EC_CMD_TIMEOUT;
  1372                 datagram->state = EC_CMD_TIMEOUT;
  1330                 master->stats.timeouts++;
  1373                 master->stats.timeouts++;
  1331                 ec_master_output_stats(master);
  1374                 ec_master_output_stats(master);
  1332                 break;
  1375                 break;
  1333             case EC_CMD_RECEIVED:
  1376             case EC_CMD_RECEIVED:
  1334                 master->stats.delayed++;
  1377                 master->stats.delayed++;
  1335                 ec_master_output_stats(master);
  1378                 ec_master_output_stats(master);
  1336                 break;
  1379                 break;
  1337             default:
  1380             default:
  1338                 break;
  1381                 break;
  1339         }
  1382         }
  1340         list_del_init(&command->queue);
  1383         list_del_init(&datagram->queue);
  1341     }
  1384     }
  1342 }
  1385 }
  1343 
  1386 
  1344 /*****************************************************************************/
  1387 /*****************************************************************************/
  1345 
  1388 
  1346 /**
  1389 /**
  1347    Asynchronous sending of commands.
  1390    Asynchronous sending of datagrams.
  1348    \ingroup RealtimeInterface
  1391    \ingroup RealtimeInterface
  1349 */
  1392 */
  1350 
  1393 
  1351 void ecrt_master_async_send(ec_master_t *master /**< EtherCAT master */)
  1394 void ecrt_master_async_send(ec_master_t *master /**< EtherCAT master */)
  1352 {
  1395 {
  1353     ec_command_t *command, *n;
  1396     ec_datagram_t *datagram, *n;
  1354 
  1397 
  1355     if (unlikely(!master->device->link_state)) {
  1398     if (unlikely(!master->device->link_state)) {
  1356         // link is down, no command can be sent
  1399         // link is down, no datagram can be sent
  1357         list_for_each_entry_safe(command, n, &master->command_queue, queue) {
  1400         list_for_each_entry_safe(datagram, n, &master->datagram_queue, queue) {
  1358             command->state = EC_CMD_ERROR;
  1401             datagram->state = EC_CMD_ERROR;
  1359             list_del_init(&command->queue);
  1402             list_del_init(&datagram->queue);
  1360         }
  1403         }
  1361 
  1404 
  1362         // query link state
  1405         // query link state
  1363         ec_device_call_isr(master->device);
  1406         ec_device_call_isr(master->device);
  1364         return;
  1407         return;
  1365     }
  1408     }
  1366 
  1409 
  1367     // send frames
  1410     // send frames
  1368     ec_master_send_commands(master);
  1411     ec_master_send_datagrams(master);
  1369 }
  1412 }
  1370 
  1413 
  1371 /*****************************************************************************/
  1414 /*****************************************************************************/
  1372 
  1415 
  1373 /**
  1416 /**
  1374    Asynchronous receiving of commands.
  1417    Asynchronous receiving of datagrams.
  1375    \ingroup RealtimeInterface
  1418    \ingroup RealtimeInterface
  1376 */
  1419 */
  1377 
  1420 
  1378 void ecrt_master_async_receive(ec_master_t *master /**< EtherCAT master */)
  1421 void ecrt_master_async_receive(ec_master_t *master /**< EtherCAT master */)
  1379 {
  1422 {
  1380     ec_command_t *command, *next;
  1423     ec_datagram_t *datagram, *next;
  1381     cycles_t t_received, t_timeout;
  1424     cycles_t t_received, t_timeout;
  1382 
  1425 
  1383     ec_device_call_isr(master->device);
  1426     ec_device_call_isr(master->device);
  1384 
  1427 
  1385     t_received = get_cycles();
  1428     t_received = get_cycles();
  1386     t_timeout = (cycles_t) master->timeout * (cpu_khz / 1000);
  1429     t_timeout = (cycles_t) master->timeout * (cpu_khz / 1000);
  1387 
  1430 
  1388     // dequeue all received commands
  1431     // dequeue all received datagrams
  1389     list_for_each_entry_safe(command, next, &master->command_queue, queue)
  1432     list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue)
  1390         if (command->state == EC_CMD_RECEIVED) list_del_init(&command->queue);
  1433         if (datagram->state == EC_CMD_RECEIVED)
  1391 
  1434             list_del_init(&datagram->queue);
  1392     // dequeue all commands that timed out
  1435 
  1393     list_for_each_entry_safe(command, next, &master->command_queue, queue) {
  1436     // dequeue all datagrams that timed out
  1394         switch (command->state) {
  1437     list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) {
       
  1438         switch (datagram->state) {
  1395             case EC_CMD_SENT:
  1439             case EC_CMD_SENT:
  1396             case EC_CMD_QUEUED:
  1440             case EC_CMD_QUEUED:
  1397                 if (t_received - command->t_sent > t_timeout) {
  1441                 if (t_received - datagram->t_sent > t_timeout) {
  1398                     list_del_init(&command->queue);
  1442                     list_del_init(&datagram->queue);
  1399                     command->state = EC_CMD_TIMEOUT;
  1443                     datagram->state = EC_CMD_TIMEOUT;
  1400                     master->stats.timeouts++;
  1444                     master->stats.timeouts++;
  1401                     ec_master_output_stats(master);
  1445                     ec_master_output_stats(master);
  1402                 }
  1446                 }
  1403                 break;
  1447                 break;
  1404             default:
  1448             default:
  1409 
  1453 
  1410 /*****************************************************************************/
  1454 /*****************************************************************************/
  1411 
  1455 
  1412 /**
  1456 /**
  1413    Prepares synchronous IO.
  1457    Prepares synchronous IO.
  1414    Queues all domain commands and sends them. Then waits a certain time, so
  1458    Queues all domain datagrams and sends them. Then waits a certain time, so
  1415    that ecrt_master_sasync_receive() can be called securely.
  1459    that ecrt_master_sasync_receive() can be called securely.
  1416    \ingroup RealtimeInterface
  1460    \ingroup RealtimeInterface
  1417 */
  1461 */
  1418 
  1462 
  1419 void ecrt_master_prepare_async_io(ec_master_t *master /**< EtherCAT master */)
  1463 void ecrt_master_prepare_async_io(ec_master_t *master /**< EtherCAT master */)
  1420 {
  1464 {
  1421     ec_domain_t *domain;
  1465     ec_domain_t *domain;
  1422     cycles_t t_start, t_end, t_timeout;
  1466     cycles_t t_start, t_end, t_timeout;
  1423 
  1467 
  1424     // queue commands of all domains
  1468     // queue datagrams of all domains
  1425     list_for_each_entry(domain, &master->domains, list)
  1469     list_for_each_entry(domain, &master->domains, list)
  1426         ecrt_domain_queue(domain);
  1470         ecrt_domain_queue(domain);
  1427 
  1471 
  1428     ecrt_master_async_send(master);
  1472     ecrt_master_async_send(master);
  1429 
  1473