lib/master.c
changeset 2060 8b67602f5161
parent 2042 8b358effa78b
child 2066 b544025bd696
equal deleted inserted replaced
2059:ab0b96ac18bb 2060:8b67602f5161
     1 /******************************************************************************
     1 /******************************************************************************
     2  *  
     2  *
     3  *  $Id$
     3  *  $Id$
     4  *  
     4  *
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
     6  *  
     6  *
     7  *  This file is part of the IgH EtherCAT master userspace library.
     7  *  This file is part of the IgH EtherCAT master userspace library.
     8  *  
     8  *
     9  *  The IgH EtherCAT master userspace library is free software; you can
     9  *  The IgH EtherCAT master userspace library is free software; you can
    10  *  redistribute it and/or modify it under the terms of the GNU Lesser General
    10  *  redistribute it and/or modify it under the terms of the GNU Lesser General
    11  *  Public License as published by the Free Software Foundation; version 2.1
    11  *  Public License as published by the Free Software Foundation; version 2.1
    12  *  of the License.
    12  *  of the License.
    13  *
    13  *
    17  *  GNU Lesser General Public License for more details.
    17  *  GNU Lesser General Public License for more details.
    18  *
    18  *
    19  *  You should have received a copy of the GNU Lesser General Public License
    19  *  You should have received a copy of the GNU Lesser General Public License
    20  *  along with the IgH EtherCAT master userspace library. If not, see
    20  *  along with the IgH EtherCAT master userspace library. If not, see
    21  *  <http://www.gnu.org/licenses/>.
    21  *  <http://www.gnu.org/licenses/>.
    22  *  
    22  *
    23  *  ---
    23  *  ---
    24  *  
    24  *
    25  *  The license mentioned above concerns the source code only. Using the
    25  *  The license mentioned above concerns the source code only. Using the
    26  *  EtherCAT technology and brand is only permitted in compliance with the
    26  *  EtherCAT technology and brand is only permitted in compliance with the
    27  *  industrial property and similar rights of Beckhoff Automation GmbH.
    27  *  industrial property and similar rights of Beckhoff Automation GmbH.
    28  *
    28  *
    29  *****************************************************************************/
    29  *****************************************************************************/
   116     domain = malloc(sizeof(ec_domain_t));
   116     domain = malloc(sizeof(ec_domain_t));
   117     if (!domain) {
   117     if (!domain) {
   118         fprintf(stderr, "Failed to allocate memory.\n");
   118         fprintf(stderr, "Failed to allocate memory.\n");
   119         return 0;
   119         return 0;
   120     }
   120     }
   121     
   121 
   122     index = ioctl(master->fd, EC_IOCTL_CREATE_DOMAIN, NULL);
   122     index = ioctl(master->fd, EC_IOCTL_CREATE_DOMAIN, NULL);
   123     if (index == -1) {
   123     if (index == -1) {
   124         fprintf(stderr, "Failed to create domain: %s\n", strerror(errno));
   124         fprintf(stderr, "Failed to create domain: %s\n", strerror(errno));
   125         free(domain);
   125         free(domain);
   126         return 0; 
   126         return 0;
   127     }
   127     }
   128 
   128 
   129     domain->next = NULL;
   129     domain->next = NULL;
   130     domain->index = (unsigned int) index;
   130     domain->index = (unsigned int) index;
   131     domain->master = master;
   131     domain->master = master;
   164     sc = malloc(sizeof(ec_slave_config_t));
   164     sc = malloc(sizeof(ec_slave_config_t));
   165     if (!sc) {
   165     if (!sc) {
   166         fprintf(stderr, "Failed to allocate memory.\n");
   166         fprintf(stderr, "Failed to allocate memory.\n");
   167         return 0;
   167         return 0;
   168     }
   168     }
   169     
   169 
   170     data.alias = alias;
   170     data.alias = alias;
   171     data.position = position;
   171     data.position = position;
   172     data.vendor_id = vendor_id;
   172     data.vendor_id = vendor_id;
   173     data.product_code = product_code;
   173     data.product_code = product_code;
   174     
   174 
   175     if (ioctl(master->fd, EC_IOCTL_CREATE_SLAVE_CONFIG, &data) == -1) {
   175     if (ioctl(master->fd, EC_IOCTL_CREATE_SLAVE_CONFIG, &data) == -1) {
   176         fprintf(stderr, "Failed to create slave config: %s\n",
   176         fprintf(stderr, "Failed to create slave config: %s\n",
   177                 strerror(errno));
   177                 strerror(errno));
   178         free(sc);
   178         free(sc);
   179         return 0; 
   179         return 0;
   180     }
   180     }
   181 
   181 
   182     sc->next = NULL;
   182     sc->next = NULL;
   183     sc->master = master;
   183     sc->master = master;
   184     sc->index = data.config_index;
   184     sc->index = data.config_index;
   214 
   214 
   215 int ecrt_master_get_slave(ec_master_t *master, uint16_t slave_position,
   215 int ecrt_master_get_slave(ec_master_t *master, uint16_t slave_position,
   216         ec_slave_info_t *slave_info)
   216         ec_slave_info_t *slave_info)
   217 {
   217 {
   218     ec_ioctl_slave_t data;
   218     ec_ioctl_slave_t data;
   219     int index;
   219     int index, i;
   220 
   220 
   221     data.position = slave_position;
   221     data.position = slave_position;
   222 
   222 
   223     if (ioctl(master->fd, EC_IOCTL_SLAVE, &data) == -1) {
   223     if (ioctl(master->fd, EC_IOCTL_SLAVE, &data) == -1) {
   224         fprintf(stderr, "Failed to get slave info: %s\n", strerror(errno));
   224         fprintf(stderr, "Failed to get slave info: %s\n", strerror(errno));
   230     slave_info->product_code = data.product_code;
   230     slave_info->product_code = data.product_code;
   231     slave_info->revision_number = data.revision_number;
   231     slave_info->revision_number = data.revision_number;
   232     slave_info->serial_number = data.serial_number;
   232     slave_info->serial_number = data.serial_number;
   233     slave_info->alias = data.alias;
   233     slave_info->alias = data.alias;
   234     slave_info->current_on_ebus = data.current_on_ebus;
   234     slave_info->current_on_ebus = data.current_on_ebus;
       
   235     for ( i = 0; i < EC_MAX_PORTS; i++ ) {
       
   236     	slave_info->ports[i].desc = data.ports[i].desc;
       
   237     	slave_info->ports[i].link.link_up = data.ports[i].link.link_up;
       
   238     	slave_info->ports[i].link.loop_closed = data.ports[i].link.loop_closed;
       
   239     	slave_info->ports[i].link.signal_detected = data.ports[i].link.signal_detected;
       
   240     	slave_info->ports[i].receive_time = data.ports[i].receive_time;
       
   241     	slave_info->ports[i].next_slave = data.ports[i].next_slave;
       
   242     	slave_info->ports[i].delay_to_next_dc = data.ports[i].delay_to_next_dc;
       
   243     }
   235     slave_info->al_state = data.al_state;
   244     slave_info->al_state = data.al_state;
   236     slave_info->error_flag = data.error_flag;
   245     slave_info->error_flag = data.error_flag;
   237     slave_info->sync_count = data.sync_count;
   246     slave_info->sync_count = data.sync_count;
   238     slave_info->sdo_count = data.sdo_count;
   247     slave_info->sdo_count = data.sdo_count;
   239     strncpy(slave_info->name, data.name, EC_MAX_STRING_LENGTH);
   248     strncpy(slave_info->name, data.name, EC_MAX_STRING_LENGTH);
   453             master->process_data_size = 0;
   462             master->process_data_size = 0;
   454             return -1; // FIXME
   463             return -1; // FIXME
   455         }
   464         }
   456 
   465 
   457         // Access the mapped region to cause the initial page fault
   466         // Access the mapped region to cause the initial page fault
   458         printf("pd: %x\n", master->process_data[0]);
   467         memset(master->process_data, 0, master->process_data_size);
   459     }
   468     }
   460 
   469 
   461     return 0;
   470     return 0;
   462 }
   471 }
   463 
   472 
   513     }
   522     }
   514 }
   523 }
   515 
   524 
   516 /*****************************************************************************/
   525 /*****************************************************************************/
   517 
   526 
       
   527 void ecrt_master_configured_slaves_state(const ec_master_t *master,
       
   528                                          ec_master_state_t *state)
       
   529 {
       
   530     if (ioctl(master->fd, EC_IOCTL_MASTER_SC_STATE, state) == -1) {
       
   531         fprintf(stderr, "Failed to get master state: %s\n", strerror(errno));
       
   532     }
       
   533 }
       
   534 
       
   535 /*****************************************************************************/
       
   536 
   518 void ecrt_master_application_time(ec_master_t *master, uint64_t app_time)
   537 void ecrt_master_application_time(ec_master_t *master, uint64_t app_time)
   519 {
   538 {
   520     ec_ioctl_app_time_t data;
   539     ec_ioctl_app_time_t data;
   521 
   540 
   522     data.app_time = app_time;
   541     data.app_time = app_time;