master/slave.c
changeset 133 b36d187ecc0b
parent 132 63a5b40eb7da
child 135 80d493917205
equal deleted inserted replaced
132:63a5b40eb7da 133:b36d187ecc0b
    46     slave->sii_alias = 0;
    46     slave->sii_alias = 0;
    47     slave->sii_vendor_id = 0;
    47     slave->sii_vendor_id = 0;
    48     slave->sii_product_code = 0;
    48     slave->sii_product_code = 0;
    49     slave->sii_revision_number = 0;
    49     slave->sii_revision_number = 0;
    50     slave->sii_serial_number = 0;
    50     slave->sii_serial_number = 0;
       
    51     slave->sii_mailbox_protocols = 0;
    51     slave->type = NULL;
    52     slave->type = NULL;
    52     slave->registered = 0;
    53     slave->registered = 0;
    53     slave->fmmu_count = 0;
    54     slave->fmmu_count = 0;
    54     slave->eeprom_name = NULL;
    55     slave->eeprom_name = NULL;
    55     slave->eeprom_group = NULL;
    56     slave->eeprom_group = NULL;
   158     }
   159     }
   159 
   160 
   160     if (unlikely(ec_slave_sii_read(slave, 0x000E,
   161     if (unlikely(ec_slave_sii_read(slave, 0x000E,
   161                                    &slave->sii_serial_number))) {
   162                                    &slave->sii_serial_number))) {
   162         EC_ERR("Could not read SII serial number!\n");
   163         EC_ERR("Could not read SII serial number!\n");
       
   164         return -1;
       
   165     }
       
   166 
       
   167     if (unlikely(ec_slave_sii_read(slave, 0x001C,
       
   168                                    &slave->sii_mailbox_protocols))) {
       
   169         EC_ERR("Could not read SII supported mailbox protocols!\n");
   163         return -1;
   170         return -1;
   164     }
   171     }
   165 
   172 
   166     if (unlikely(ec_slave_fetch_categories(slave))) {
   173     if (unlikely(ec_slave_fetch_categories(slave))) {
   167         EC_ERR("Could not fetch category data!\n");
   174         EC_ERR("Could not fetch category data!\n");
   783 void ec_slave_print(const ec_slave_t *slave /**< EtherCAT-Slave */)
   790 void ec_slave_print(const ec_slave_t *slave /**< EtherCAT-Slave */)
   784 {
   791 {
   785     ec_eeprom_sync_t *sync;
   792     ec_eeprom_sync_t *sync;
   786     ec_eeprom_pdo_t *pdo;
   793     ec_eeprom_pdo_t *pdo;
   787     ec_eeprom_pdo_entry_t *entry;
   794     ec_eeprom_pdo_entry_t *entry;
       
   795     int first;
   788 
   796 
   789     EC_INFO("x-- EtherCAT slave information ---------------\n");
   797     EC_INFO("x-- EtherCAT slave information ---------------\n");
   790 
   798 
   791     if (slave->type) {
   799     if (slave->type) {
   792         EC_INFO("| Vendor \"%s\", Product \"%s\": %s\n",
   800         EC_INFO("| Vendor \"%s\", Product \"%s\": %s\n",
   803     EC_INFO("| Base information:\n");
   811     EC_INFO("| Base information:\n");
   804     EC_INFO("|   Type %u, Revision %i, Build %i\n",
   812     EC_INFO("|   Type %u, Revision %i, Build %i\n",
   805             slave->base_type, slave->base_revision, slave->base_build);
   813             slave->base_type, slave->base_revision, slave->base_build);
   806     EC_INFO("|   Supported FMMUs: %i, Sync managers: %i\n",
   814     EC_INFO("|   Supported FMMUs: %i, Sync managers: %i\n",
   807             slave->base_fmmu_count, slave->base_sync_count);
   815             slave->base_fmmu_count, slave->base_sync_count);
       
   816 
       
   817     EC_INFO("| Supported mailbox protocols: ");
       
   818     if (!slave->sii_mailbox_protocols) {
       
   819         printk("(none)");
       
   820     }
       
   821     else {
       
   822         first = 1;
       
   823         if (slave->sii_mailbox_protocols & EC_MBOX_AOE) {
       
   824             printk("AoE");
       
   825             first = 0;
       
   826         }
       
   827         if (slave->sii_mailbox_protocols & EC_MBOX_EOE) {
       
   828             if (!first) printk(", ");
       
   829             printk("EoE");
       
   830             first = 0;
       
   831         }
       
   832         if (slave->sii_mailbox_protocols & EC_MBOX_COE) {
       
   833             if (!first) printk(", ");
       
   834             printk("CoE");
       
   835             first = 0;
       
   836         }
       
   837         if (slave->sii_mailbox_protocols & EC_MBOX_FOE) {
       
   838             if (!first) printk(", ");
       
   839             printk("FoE");
       
   840             first = 0;
       
   841         }
       
   842         if (slave->sii_mailbox_protocols & EC_MBOX_SOE) {
       
   843             if (!first) printk(", ");
       
   844             printk("SoE");
       
   845             first = 0;
       
   846         }
       
   847         if (slave->sii_mailbox_protocols & EC_MBOX_VOE) {
       
   848             if (!first) printk(", ");
       
   849             printk("VoE");
       
   850         }
       
   851     }
       
   852     printk("\n");
   808 
   853 
   809     EC_INFO("| EEPROM data:\n");
   854     EC_INFO("| EEPROM data:\n");
   810 
   855 
   811     if (slave->sii_alias)
   856     if (slave->sii_alias)
   812         EC_INFO("|   Configured station alias: 0x%04X (%i)\n",
   857         EC_INFO("|   Configured station alias: 0x%04X (%i)\n",
   906     return 0;
   951     return 0;
   907 }
   952 }
   908 
   953 
   909 /*****************************************************************************/
   954 /*****************************************************************************/
   910 
   955 
       
   956 /**
       
   957    Sendet ein Mailbox-Kommando.
       
   958  */
       
   959 
       
   960 int ec_slave_mailbox_send(ec_slave_t *slave, /**< EtherCAT-Slave */
       
   961                           uint8_t type, /**< Unterliegendes Protokoll */
       
   962                           const uint8_t *prot_data, /**< Protokoll-Daten */
       
   963                           size_t size /**< Datengröße */
       
   964                           )
       
   965 {
       
   966     uint8_t data[0xF6];
       
   967     ec_command_t command;
       
   968 
       
   969     if (unlikely(size + 6 > 0xF6)) {
       
   970         EC_ERR("Data size does not fit in mailbox!\n");
       
   971         return -1;
       
   972     }
       
   973 
       
   974     memset(data, 0x00, 0xF6);
       
   975 
       
   976     EC_WRITE_U16(data,      size); // Length of the Mailbox service data
       
   977     EC_WRITE_U16(data + 2,  slave->station_address); // Station address
       
   978     EC_WRITE_U8 (data + 4,  0x00); // Channel & priority
       
   979     EC_WRITE_U8 (data + 5,  type); // Underlying protocol type
       
   980 
       
   981     memcpy(data + 6, prot_data, size);
       
   982 
       
   983     ec_command_init_npwr(&command, slave->station_address, 0x1800, 0xF6, data);
       
   984     if (unlikely(ec_master_simple_io(slave->master, &command))) {
       
   985         EC_ERR("Mailbox sending failed on slave %i!\n", slave->ring_position);
       
   986         return -1;
       
   987     }
       
   988 
       
   989     return 0;
       
   990 }
       
   991 
       
   992 /*****************************************************************************/
       
   993 
       
   994 /**
       
   995    Sendet ein Mailbox-Kommando.
       
   996  */
       
   997 
       
   998 int ec_slave_mailbox_receive(ec_slave_t *slave, /**< EtherCAT-Slave */
       
   999                              uint8_t type, /**< Unterliegendes Protokoll */
       
  1000                              uint8_t *prot_data, /**< Protokoll-Daten */
       
  1001                              size_t *size /**< Datengröße des Puffers, später
       
  1002                                              Größe der gelesenen Daten */
       
  1003                              )
       
  1004 {
       
  1005     ec_command_t command;
       
  1006     size_t data_size;
       
  1007     cycles_t start, end, timeout;
       
  1008 
       
  1009     // Read "written bit" of Sync-Manager
       
  1010     start = get_cycles();
       
  1011     timeout = (cycles_t) 100 * cpu_khz; // 100ms
       
  1012 
       
  1013     while (1)
       
  1014     {
       
  1015         ec_command_init_nprd(&command, slave->station_address, 0x808, 8);
       
  1016         if (unlikely(ec_master_simple_io(slave->master, &command))) {
       
  1017             EC_ERR("Mailbox checking failed on slave %i!\n",
       
  1018                    slave->ring_position);
       
  1019             return -1;
       
  1020         }
       
  1021 
       
  1022         end = get_cycles();
       
  1023 
       
  1024         if (EC_READ_U8(command.data + 5) & 8) break; // Written bit is high
       
  1025 
       
  1026         if ((end - start) >= timeout) {
       
  1027             EC_ERR("Mailbox check - Slave %i timed out.\n",
       
  1028                    slave->ring_position);
       
  1029             return -1;
       
  1030         }
       
  1031 
       
  1032         udelay(100);
       
  1033     }
       
  1034 
       
  1035     if (unlikely(slave->master->debug_level) > 1)
       
  1036         EC_DBG("SDO download took %ius.\n", ((u32) (end - start) * 1000
       
  1037                                              / cpu_khz));
       
  1038 
       
  1039     ec_command_init_nprd(&command, slave->station_address, 0x18F6, 0xF6);
       
  1040     if (unlikely(ec_master_simple_io(slave->master, &command))) {
       
  1041         EC_ERR("Mailbox receiving failed on slave %i!\n",
       
  1042                slave->ring_position);
       
  1043         return -1;
       
  1044     }
       
  1045 
       
  1046     if (EC_READ_U8(command.data + 5) != type) { // nicht CoE
       
  1047         EC_ERR("Invalid mailbox response (non-CoE) at slave %i!\n",
       
  1048                slave->ring_position);
       
  1049         return -1;
       
  1050     }
       
  1051 
       
  1052     if ((data_size = EC_READ_U16(command.data)) > *size) {
       
  1053         EC_ERR("CoE data does not fit in buffer (%i > %i).\n",
       
  1054                data_size, *size);
       
  1055         return -1;
       
  1056     }
       
  1057 
       
  1058     memcpy(prot_data, command.data + 6, data_size);
       
  1059     *size = data_size;
       
  1060     return 0;
       
  1061 }
       
  1062 
       
  1063 /*****************************************************************************/
       
  1064 
   911 /* Emacs-Konfiguration
  1065 /* Emacs-Konfiguration
   912 ;;; Local Variables: ***
  1066 ;;; Local Variables: ***
   913 ;;; c-basic-offset:4 ***
  1067 ;;; c-basic-offset:4 ***
   914 ;;; End: ***
  1068 ;;; End: ***
   915 */
  1069 */