master/master.c
changeset 114 e4b4b5a85e75
parent 113 a3dbd6bc8fce
child 116 231b22fc034b
equal deleted inserted replaced
113:a3dbd6bc8fce 114:e4b4b5a85e75
   522 
   522 
   523 /**
   523 /**
   524    Wandelt eine ASCII-kodierte Bus-Adresse in einen Slave-Zeiger.
   524    Wandelt eine ASCII-kodierte Bus-Adresse in einen Slave-Zeiger.
   525 
   525 
   526    Gültige Adress-Strings sind Folgende:
   526    Gültige Adress-Strings sind Folgende:
   527 
       
   528    - \a "X" = der X. Slave im Bus,
   527    - \a "X" = der X. Slave im Bus,
   529    - \a "X:Y" = der Y. Slave hinter dem X. Buskoppler,
   528    - \a "X:Y" = der Y. Slave hinter dem X. Buskoppler,
   530    - \a "#X" = der Slave mit der SSID X,
   529    - \a "#X" = der Slave mit dem Alias X,
   531    - \a "#X:Y" = der Y. Slave hinter dem Buskoppler mit der SSID X.
   530    - \a "#X:Y" = der Y. Slave hinter dem Buskoppler mit dem Alias X.
       
   531 
       
   532    X und Y fangen immer bei 0 an und können auch hexadezimal oder oktal
       
   533    angegeben werden (mit entsprechendem Prefix).
   532 
   534 
   533    \return Zeiger auf Slave bei Erfolg, sonst NULL
   535    \return Zeiger auf Slave bei Erfolg, sonst NULL
   534 */
   536 */
   535 
   537 
   536 ec_slave_t *ec_master_slave_address(const ec_master_t *master,
   538 ec_slave_t *ec_master_slave_address(const ec_master_t *master,
   539                                     /**< Address-String */
   541                                     /**< Address-String */
   540                                     )
   542                                     )
   541 {
   543 {
   542     unsigned long first, second;
   544     unsigned long first, second;
   543     char *remainder, *remainder2;
   545     char *remainder, *remainder2;
   544     unsigned int i;
   546     unsigned int i, alias_requested, alias_slave_index, alias_found;
   545     int coupler_idx, slave_idx;
   547     int coupler_idx, slave_idx;
   546     ec_slave_t *slave;
   548     ec_slave_t *slave;
   547 
   549 
   548     if (!address || address[0] == 0) return NULL;
   550     if (!address || address[0] == 0) return NULL;
   549 
   551 
       
   552     alias_requested = 0;
       
   553     alias_slave_index = 0;
   550     if (address[0] == '#') {
   554     if (address[0] == '#') {
   551         EC_ERR("Bus ID \"%s\" - #<SSID> not implemented yet!\n", address);
   555         alias_requested = 1;
   552         return NULL;
   556         address++;
   553     }
   557     }
   554 
   558 
   555     first = simple_strtoul(address, &remainder, 0);
   559     first = simple_strtoul(address, &remainder, 0);
   556     if (remainder == address) {
   560     if (remainder == address) {
   557         EC_ERR("Bus ID \"%s\" - First number empty!\n", address);
   561         EC_ERR("Slave address \"%s\" - First number empty!\n", address);
   558         return NULL;
   562         return NULL;
   559     }
   563     }
   560 
   564 
       
   565     if (alias_requested) {
       
   566         alias_found = 0;
       
   567         for (i = 0; i < master->slave_count; i++) {
       
   568             if (master->slaves[i].sii_alias == first) {
       
   569                 alias_slave_index = i;
       
   570                 alias_found = 1;
       
   571                 break;
       
   572             }
       
   573         }
       
   574         if (!alias_found) {
       
   575             EC_ERR("Slave address \"%s\" - Alias not found!\n", address);
       
   576             return NULL;
       
   577         }
       
   578     }
       
   579 
   561     if (!remainder[0]) { // absolute position
   580     if (!remainder[0]) { // absolute position
   562         if (first < master->slave_count) {
   581         if (alias_requested) {
   563             return master->slaves + first;
   582             return master->slaves + alias_slave_index;
   564         }
   583         }
   565 
   584         else {
   566         EC_ERR("Bus ID \"%s\" - Absolute position invalid!\n", address);
   585             if (first < master->slave_count) {
   567     }
   586                 return master->slaves + first;
   568 
   587             }
       
   588             EC_ERR("Slave address \"%s\" - Absolute position invalid!\n",
       
   589                    address);
       
   590         }
       
   591     }
   569     else if (remainder[0] == ':') { // field position
   592     else if (remainder[0] == ':') { // field position
   570 
       
   571         remainder++;
   593         remainder++;
   572         second = simple_strtoul(remainder, &remainder2, 0);
   594         second = simple_strtoul(remainder, &remainder2, 0);
   573 
   595 
   574         if (remainder2 == remainder) {
   596         if (remainder2 == remainder) {
   575             EC_ERR("Bus ID \"%s\" - Sencond number empty!\n", address);
   597             EC_ERR("Slave address \"%s\" - Second number empty!\n", address);
   576             return NULL;
   598             return NULL;
   577         }
   599         }
   578 
   600 
   579         if (remainder2[0]) {
   601         if (remainder2[0]) {
   580             EC_ERR("Bus ID \"%s\" - Invalid trailer (2)!\n", address);
   602             EC_ERR("Slave address \"%s\" - Invalid trailer!\n", address);
   581             return NULL;
   603             return NULL;
   582         }
   604         }
   583 
   605 
   584         coupler_idx = -1;
   606         if (alias_requested) {
   585         slave_idx = 0;
   607             for (i = alias_slave_index + 1; i < master->slave_count; i++) {
   586         for (i = 0; i < master->slave_count; i++, slave_idx++) {
   608                 slave = master->slaves + i;
   587             slave = master->slaves + i;
   609                 if (!slave->type || slave->type->bus_coupler) break;
   588             if (!slave->type) continue;
   610                 if (i - alias_slave_index - 1 == second) return slave;
   589 
       
   590             if (slave->type->bus_coupler) {
       
   591                 coupler_idx++;
       
   592                 slave_idx = 0;
       
   593             }
   611             }
   594 
   612             EC_ERR("Slave address \"%s\" - Bus coupler %i has no %lu. slave"
   595             if (coupler_idx == first && slave_idx == second) return slave;
   613                    " following!\n", address,
   596         }
   614                    (master->slaves + alias_slave_index)->ring_position,
   597     }
   615                    second);
   598 
   616             return NULL;
       
   617         }
       
   618         else {
       
   619             coupler_idx = -1;
       
   620             slave_idx = 0;
       
   621             for (i = 0; i < master->slave_count; i++, slave_idx++) {
       
   622                 slave = master->slaves + i;
       
   623                 if (!slave->type) continue; // FIXME
       
   624                 if (slave->type->bus_coupler) {
       
   625                     coupler_idx++;
       
   626                     slave_idx = 0;
       
   627                 }
       
   628                 if (coupler_idx == first && slave_idx == second) return slave;
       
   629             }
       
   630         }
       
   631     }
   599     else
   632     else
   600         EC_ERR("Bus ID \"%s\" - Invalid trailer!\n", address);
   633         EC_ERR("Slave address \"%s\" - Invalid format!\n", address);
   601 
       
   602     // FIXME ???
       
   603 
   634 
   604     return NULL;
   635     return NULL;
   605 }
   636 }
   606 
   637 
   607 /*****************************************************************************/
   638 /*****************************************************************************/
  1028     EC_INFO("*** End master information ***\n");
  1059     EC_INFO("*** End master information ***\n");
  1029 }
  1060 }
  1030 
  1061 
  1031 /*****************************************************************************/
  1062 /*****************************************************************************/
  1032 
  1063 
       
  1064 /**
       
  1065    Schreibt den "Configured station alias".
       
  1066 
       
  1067    \return 0, wenn alles ok, sonst < 0
       
  1068 */
       
  1069 
       
  1070 int ecrt_master_write_slave_alias(ec_master_t *master,
       
  1071                                   /** EtherCAT-Master */
       
  1072                                   const char *slave_address,
       
  1073                                   /** Slave-Adresse,
       
  1074                                       siehe ec_master_slave_address() */
       
  1075                                   uint16_t alias
       
  1076                                   /** Neuer Alias */
       
  1077                                   )
       
  1078 {
       
  1079     ec_slave_t *slave;
       
  1080     if (!(slave = ec_master_slave_address(master, slave_address)))
       
  1081         return -1;
       
  1082     return ec_slave_sii_write(slave, 0x0004, alias);
       
  1083 }
       
  1084 
       
  1085 /*****************************************************************************/
       
  1086 
  1033 EXPORT_SYMBOL(ecrt_master_create_domain);
  1087 EXPORT_SYMBOL(ecrt_master_create_domain);
  1034 EXPORT_SYMBOL(ecrt_master_activate);
  1088 EXPORT_SYMBOL(ecrt_master_activate);
  1035 EXPORT_SYMBOL(ecrt_master_deactivate);
  1089 EXPORT_SYMBOL(ecrt_master_deactivate);
  1036 EXPORT_SYMBOL(ecrt_master_prepare_async_io);
  1090 EXPORT_SYMBOL(ecrt_master_prepare_async_io);
  1037 EXPORT_SYMBOL(ecrt_master_sync_io);
  1091 EXPORT_SYMBOL(ecrt_master_sync_io);
  1038 EXPORT_SYMBOL(ecrt_master_async_send);
  1092 EXPORT_SYMBOL(ecrt_master_async_send);
  1039 EXPORT_SYMBOL(ecrt_master_async_receive);
  1093 EXPORT_SYMBOL(ecrt_master_async_receive);
  1040 EXPORT_SYMBOL(ecrt_master_debug);
  1094 EXPORT_SYMBOL(ecrt_master_debug);
  1041 EXPORT_SYMBOL(ecrt_master_print);
  1095 EXPORT_SYMBOL(ecrt_master_print);
       
  1096 EXPORT_SYMBOL(ecrt_master_write_slave_alias);
  1042 
  1097 
  1043 /*****************************************************************************/
  1098 /*****************************************************************************/
  1044 
  1099 
  1045 /* Emacs-Konfiguration
  1100 /* Emacs-Konfiguration
  1046 ;;; Local Variables: ***
  1101 ;;; Local Variables: ***