fp@39: /****************************************************************************** fp@0: * fp@54: * s l a v e . c fp@0: * fp@123: * Methoden für einen EtherCAT-Slave. fp@0: * fp@39: * $Id$ fp@0: * fp@39: *****************************************************************************/ fp@0: fp@24: #include fp@73: #include fp@0: fp@54: #include "globals.h" fp@54: #include "slave.h" fp@98: #include "command.h" fp@98: #include "master.h" fp@0: fp@39: /*****************************************************************************/ fp@0: fp@118: int ec_slave_fetch_categories(ec_slave_t *); fp@118: int ec_slave_fetch_strings(ec_slave_t *, const uint8_t *); fp@123: int ec_slave_fetch_general(ec_slave_t *, const uint8_t *); fp@126: int ec_slave_fetch_sync(ec_slave_t *, const uint8_t *, size_t); fp@126: int ec_slave_fetch_pdo(ec_slave_t *, const uint8_t *, size_t, ec_pdo_type_t); fp@123: int ec_slave_locate_string(ec_slave_t *, unsigned int, char **); fp@118: fp@118: /*****************************************************************************/ fp@118: fp@0: /** fp@0: EtherCAT-Slave-Konstruktor. fp@73: */ fp@73: fp@73: void ec_slave_init(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@73: ec_master_t *master /**< EtherCAT-Master */ fp@73: ) fp@73: { fp@73: slave->master = master; fp@73: slave->base_type = 0; fp@73: slave->base_revision = 0; fp@73: slave->base_build = 0; fp@73: slave->base_fmmu_count = 0; fp@73: slave->base_sync_count = 0; fp@73: slave->ring_position = 0; fp@73: slave->station_address = 0; fp@114: slave->sii_alias = 0; fp@73: slave->sii_vendor_id = 0; fp@73: slave->sii_product_code = 0; fp@73: slave->sii_revision_number = 0; fp@73: slave->sii_serial_number = 0; fp@73: slave->type = NULL; fp@73: slave->registered = 0; fp@73: slave->fmmu_count = 0; fp@121: slave->eeprom_name = NULL; fp@121: slave->eeprom_group = NULL; fp@121: slave->eeprom_desc = NULL; fp@126: INIT_LIST_HEAD(&slave->eeprom_strings); fp@126: INIT_LIST_HEAD(&slave->eeprom_syncs); fp@126: INIT_LIST_HEAD(&slave->eeprom_pdos); fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@73: EtherCAT-Slave-Destruktor. fp@73: */ fp@73: fp@73: void ec_slave_clear(ec_slave_t *slave /**< EtherCAT-Slave */) fp@73: { fp@126: ec_eeprom_string_t *string, *next_str; fp@126: ec_eeprom_sync_t *sync, *next_sync; fp@126: ec_eeprom_pdo_t *pdo, *next_pdo; fp@126: ec_eeprom_pdo_entry_t *entry, *next_ent; fp@118: fp@118: // Alle Strings freigeben fp@126: list_for_each_entry_safe(string, next_str, &slave->eeprom_strings, list) { fp@118: list_del(&string->list); fp@118: kfree(string); fp@118: } fp@121: fp@126: // Alle Sync-Manager freigeben fp@126: list_for_each_entry_safe(sync, next_sync, &slave->eeprom_syncs, list) { fp@126: list_del(&sync->list); fp@126: kfree(sync); fp@126: } fp@126: fp@126: // Alle PDOs freigeben fp@126: list_for_each_entry_safe(pdo, next_pdo, &slave->eeprom_pdos, list) { fp@126: list_del(&pdo->list); fp@126: if (pdo->name) kfree(pdo->name); fp@126: fp@126: // Alle Entries innerhalb eines PDOs freigeben fp@126: list_for_each_entry_safe(entry, next_ent, &pdo->entries, list) { fp@126: list_del(&entry->list); fp@126: if (entry->name) kfree(entry->name); fp@126: kfree(entry); fp@126: } fp@126: fp@126: kfree(pdo); fp@126: } fp@126: fp@121: if (slave->eeprom_name) kfree(slave->eeprom_name); fp@121: if (slave->eeprom_group) kfree(slave->eeprom_group); fp@121: if (slave->eeprom_desc) kfree(slave->eeprom_desc); fp@121: } fp@121: fp@121: /*****************************************************************************/ fp@121: fp@121: /** fp@122: Liest alle benötigten Informationen aus einem Slave. fp@91: fp@91: \return 0 wenn alles ok, < 0 bei Fehler. fp@73: */ fp@73: fp@73: int ec_slave_fetch(ec_slave_t *slave /**< EtherCAT-Slave */) fp@73: { fp@98: ec_command_t command; fp@73: fp@73: // Read base data fp@98: ec_command_init_nprd(&command, slave->station_address, 0x0000, 6); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_ERR("Reading base datafrom slave %i failed!\n", fp@84: slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@98: slave->base_type = EC_READ_U8 (command.data); fp@98: slave->base_revision = EC_READ_U8 (command.data + 1); fp@98: slave->base_build = EC_READ_U16(command.data + 2); fp@98: slave->base_fmmu_count = EC_READ_U8 (command.data + 4); fp@98: slave->base_sync_count = EC_READ_U8 (command.data + 5); fp@73: fp@73: if (slave->base_fmmu_count > EC_MAX_FMMUS) fp@73: slave->base_fmmu_count = EC_MAX_FMMUS; fp@73: fp@73: // Read identification from "Slave Information Interface" (SII) fp@73: fp@114: if (unlikely(ec_slave_sii_read(slave, 0x0004, fp@114: (uint32_t *) &slave->sii_alias))) { fp@114: EC_ERR("Could not read SII alias!\n"); fp@114: return -1; fp@114: } fp@114: fp@73: if (unlikely(ec_slave_sii_read(slave, 0x0008, &slave->sii_vendor_id))) { fp@84: EC_ERR("Could not read SII vendor id!\n"); fp@73: return -1; fp@73: } fp@73: fp@73: if (unlikely(ec_slave_sii_read(slave, 0x000A, &slave->sii_product_code))) { fp@84: EC_ERR("Could not read SII product code!\n"); fp@73: return -1; fp@73: } fp@73: fp@73: if (unlikely(ec_slave_sii_read(slave, 0x000C, fp@73: &slave->sii_revision_number))) { fp@84: EC_ERR("Could not read SII revision number!\n"); fp@73: return -1; fp@73: } fp@73: fp@73: if (unlikely(ec_slave_sii_read(slave, 0x000E, fp@73: &slave->sii_serial_number))) { fp@84: EC_ERR("Could not read SII serial number!\n"); fp@73: return -1; fp@73: } fp@73: fp@118: if (unlikely(ec_slave_fetch_categories(slave))) { fp@118: EC_ERR("Could not fetch category data!\n"); fp@118: return -1; fp@118: } fp@118: fp@73: return 0; fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@73: Liest Daten aus dem Slave-Information-Interface fp@73: eines EtherCAT-Slaves. fp@73: fp@73: \return 0 bei Erfolg, sonst < 0 fp@73: */ fp@73: fp@73: int ec_slave_sii_read(ec_slave_t *slave, fp@73: /**< EtherCAT-Slave */ fp@101: uint16_t offset, fp@73: /**< Adresse des zu lesenden SII-Registers */ fp@101: uint32_t *target fp@124: /**< Zeiger auf einen 4 Byte großen Speicher zum Ablegen fp@73: der Daten */ fp@73: ) fp@73: { fp@98: ec_command_t command; fp@101: uint8_t data[10]; fp@98: cycles_t start, end, timeout; fp@73: fp@73: // Initiate read operation fp@73: fp@114: EC_WRITE_U8 (data, 0x00); // read-only access fp@114: EC_WRITE_U8 (data + 1, 0x01); // request read operation fp@114: EC_WRITE_U32(data + 2, offset); fp@73: fp@98: ec_command_init_npwr(&command, slave->station_address, 0x502, 6, data); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_ERR("SII-read failed on slave %i!\n", slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: // Der Slave legt die Informationen des Slave-Information-Interface fp@122: // in das Datenregister und löscht daraufhin ein Busy-Bit. Solange fp@73: // den Status auslesen, bis das Bit weg ist. fp@73: fp@98: start = get_cycles(); fp@119: timeout = (cycles_t) 100 * cpu_khz; // 100ms fp@98: fp@113: while (1) fp@73: { fp@113: udelay(10); fp@113: fp@98: ec_command_init_nprd(&command, slave->station_address, 0x502, 10); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_ERR("Getting SII-read status failed on slave %i!\n", fp@84: slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@98: end = get_cycles(); fp@98: fp@98: if (likely((EC_READ_U8(command.data + 1) & 0x81) == 0)) { fp@114: *target = EC_READ_U32(command.data + 6); fp@113: return 0; fp@113: } fp@113: fp@113: if (unlikely((end - start) >= timeout)) { fp@118: EC_ERR("SII-read. Slave %i timed out!\n", slave->ring_position); fp@113: return -1; fp@113: } fp@113: } fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@114: Schreibt Daten in das Slave-Information-Interface fp@114: eines EtherCAT-Slaves. fp@114: fp@114: \return 0 bei Erfolg, sonst < 0 fp@114: */ fp@114: fp@114: int ec_slave_sii_write(ec_slave_t *slave, fp@114: /**< EtherCAT-Slave */ fp@114: uint16_t offset, fp@114: /**< Adresse des zu lesenden SII-Registers */ fp@114: uint16_t value fp@114: /**< Zu schreibender Wert */ fp@114: ) fp@114: { fp@114: ec_command_t command; fp@114: uint8_t data[8]; fp@114: cycles_t start, end, timeout; fp@114: fp@114: EC_INFO("SII-write (slave %i, offset 0x%04X, value 0x%04X)\n", fp@114: slave->ring_position, offset, value); fp@114: fp@114: // Initiate write operation fp@114: fp@114: EC_WRITE_U8 (data, 0x01); // enable write access fp@114: EC_WRITE_U8 (data + 1, 0x02); // request write operation fp@114: EC_WRITE_U32(data + 2, offset); fp@114: EC_WRITE_U16(data + 6, value); fp@114: fp@114: ec_command_init_npwr(&command, slave->station_address, 0x502, 8, data); fp@114: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@114: EC_ERR("SII-write failed on slave %i!\n", slave->ring_position); fp@114: return -1; fp@114: } fp@114: fp@114: // Der Slave legt die Informationen des Slave-Information-Interface fp@122: // in das Datenregister und löscht daraufhin ein Busy-Bit. Solange fp@114: // den Status auslesen, bis das Bit weg ist. fp@114: fp@114: start = get_cycles(); fp@119: timeout = (cycles_t) 100 * cpu_khz; // 100ms fp@114: fp@114: while (1) fp@114: { fp@114: udelay(10); fp@114: fp@114: ec_command_init_nprd(&command, slave->station_address, 0x502, 2); fp@114: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@114: EC_ERR("Getting SII-write status failed on slave %i!\n", fp@114: slave->ring_position); fp@114: return -1; fp@114: } fp@114: fp@114: end = get_cycles(); fp@114: fp@114: if (likely((EC_READ_U8(command.data + 1) & 0x82) == 0)) { fp@114: if (EC_READ_U8(command.data + 1) & 0x40) { fp@114: EC_ERR("SII-write failed!\n"); fp@114: return -1; fp@114: } fp@114: else { fp@114: EC_INFO("SII-write succeeded!\n"); fp@114: return 0; fp@114: } fp@114: } fp@114: fp@114: if (unlikely((end - start) >= timeout)) { fp@118: EC_ERR("SII-write: Slave %i timed out!\n", slave->ring_position); fp@118: return -1; fp@118: } fp@118: } fp@118: } fp@118: fp@118: /*****************************************************************************/ fp@118: fp@118: /** fp@118: Holt Daten aus dem EEPROM. fp@118: fp@118: \return 0, wenn alles ok, sonst < 0 fp@118: */ fp@118: fp@118: int ec_slave_fetch_categories(ec_slave_t *slave /**< EtherCAT-Slave */) fp@118: { fp@126: uint16_t word_offset, cat_type, word_count; fp@118: uint32_t value; fp@118: uint8_t *cat_data; fp@118: unsigned int i; fp@118: fp@118: word_offset = 0x0040; fp@118: fp@118: if (!(cat_data = (uint8_t *) kmalloc(0x10000, GFP_KERNEL))) { fp@118: EC_ERR("Failed to allocate 64k bytes for category data.\n"); fp@118: return -1; fp@118: } fp@118: fp@118: while (1) { fp@126: // read category type fp@118: if (ec_slave_sii_read(slave, word_offset, &value)) { fp@126: EC_ERR("Unable to read category header.\n"); fp@123: goto out_free; fp@118: } fp@118: fp@118: // Last category? fp@118: if ((value & 0xFFFF) == 0xFFFF) break; fp@118: fp@126: cat_type = value & 0x7FFF; fp@118: word_count = (value >> 16) & 0xFFFF; fp@118: fp@118: // Fetch category data fp@118: for (i = 0; i < word_count; i++) { fp@118: if (ec_slave_sii_read(slave, word_offset + 2 + i, &value)) { fp@118: EC_ERR("Unable to read category data word %i.\n", i); fp@123: goto out_free; fp@118: } fp@118: fp@118: cat_data[i * 2] = (value >> 0) & 0xFF; fp@118: cat_data[i * 2 + 1] = (value >> 8) & 0xFF; fp@118: fp@118: // read second word "on the fly" fp@118: if (i + 1 < word_count) { fp@118: i++; fp@118: cat_data[i * 2] = (value >> 16) & 0xFF; fp@118: cat_data[i * 2 + 1] = (value >> 24) & 0xFF; fp@118: } fp@118: } fp@118: fp@126: switch (cat_type) fp@118: { fp@118: case 0x000A: fp@123: if (ec_slave_fetch_strings(slave, cat_data)) fp@123: goto out_free; fp@118: break; fp@118: case 0x001E: fp@123: if (ec_slave_fetch_general(slave, cat_data)) fp@123: goto out_free; fp@118: break; fp@118: case 0x0028: fp@118: break; fp@118: case 0x0029: fp@126: if (ec_slave_fetch_sync(slave, cat_data, word_count)) fp@126: goto out_free; fp@118: break; fp@118: case 0x0032: fp@126: if (ec_slave_fetch_pdo(slave, cat_data, word_count, EC_TX_PDO)) fp@126: goto out_free; fp@118: break; fp@118: case 0x0033: fp@126: if (ec_slave_fetch_pdo(slave, cat_data, word_count, EC_RX_PDO)) fp@126: goto out_free; fp@118: break; fp@118: default: fp@126: EC_WARN("Unknown category type 0x%04X in slave %i.\n", fp@126: cat_type, slave->ring_position); fp@118: } fp@118: fp@118: word_offset += 2 + word_count; fp@118: } fp@118: fp@118: kfree(cat_data); fp@118: return 0; fp@123: fp@123: out_free: fp@123: kfree(cat_data); fp@123: return -1; fp@118: } fp@118: fp@118: /*****************************************************************************/ fp@118: fp@118: /** fp@118: Holt die Daten einer String-Kategorie. fp@118: fp@118: \return 0 wenn alles ok, sonst < 0 fp@118: */ fp@118: fp@118: int ec_slave_fetch_strings(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@118: const uint8_t *data /**< Kategoriedaten */ fp@118: ) fp@118: { fp@118: unsigned int string_count, i; fp@118: size_t size; fp@118: off_t offset; fp@126: ec_eeprom_string_t *string; fp@118: fp@118: string_count = data[0]; fp@118: offset = 1; fp@118: for (i = 0; i < string_count; i++) { fp@118: size = data[offset]; fp@122: // Speicher für String-Objekt und Daten in einem Rutsch allozieren fp@126: if (!(string = (ec_eeprom_string_t *) fp@126: kmalloc(sizeof(ec_eeprom_string_t) + size + 1, GFP_KERNEL))) { fp@118: EC_ERR("Failed to allocate string memory.\n"); fp@118: return -1; fp@118: } fp@121: string->size = size; fp@126: string->data = (char *) string + sizeof(ec_eeprom_string_t); fp@118: memcpy(string->data, data + offset + 1, size); fp@118: string->data[size] = 0x00; fp@121: list_add_tail(&string->list, &slave->eeprom_strings); fp@118: offset += 1 + size; fp@118: } fp@118: fp@118: return 0; fp@118: } fp@118: fp@118: /*****************************************************************************/ fp@118: fp@118: /** fp@118: Holt die Daten einer General-Kategorie. fp@118: */ fp@118: fp@123: int ec_slave_fetch_general(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@123: const uint8_t *data /**< Kategorie-Daten */ fp@123: ) fp@123: { fp@123: if (ec_slave_locate_string(slave, data[0], &slave->eeprom_group)) fp@123: return -1; fp@123: if (ec_slave_locate_string(slave, data[1], &slave->eeprom_name)) fp@123: return -1; fp@123: if (ec_slave_locate_string(slave, data[3], &slave->eeprom_desc)) fp@123: return -1; fp@123: fp@123: return 0; fp@118: } fp@118: fp@118: /*****************************************************************************/ fp@118: fp@118: /** fp@118: Holt die Daten einer Sync-Manager-Kategorie. fp@118: */ fp@118: fp@126: int ec_slave_fetch_sync(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@126: const uint8_t *data, /**< Kategorie-Daten */ fp@126: size_t word_count /**< Anzahl Words */ fp@126: ) fp@126: { fp@126: unsigned int sync_count, i; fp@126: ec_eeprom_sync_t *sync; fp@126: fp@126: sync_count = word_count / 4; // Sync-Manager-Strunktur ist 4 Worte lang fp@126: fp@126: for (i = 0; i < sync_count; i++, data += 8) { fp@127: if (!(sync = (ec_eeprom_sync_t *) fp@127: kmalloc(sizeof(ec_eeprom_sync_t), GFP_KERNEL))) { fp@126: EC_ERR("Failed to allocate Sync-Manager memory.\n"); fp@126: return -1; fp@126: } fp@126: fp@126: sync->index = i; fp@126: sync->physical_start_address = *((uint16_t *) (data + 0)); fp@126: sync->length = *((uint16_t *) (data + 2)); fp@126: sync->control_register = data[4]; fp@126: sync->enable = data[6]; fp@126: fp@126: list_add_tail(&sync->list, &slave->eeprom_syncs); fp@126: } fp@126: fp@126: return 0; fp@118: } fp@118: fp@118: /*****************************************************************************/ fp@118: fp@118: /** fp@118: Holt die Daten einer TXPDO-Kategorie. fp@118: */ fp@118: fp@126: int ec_slave_fetch_pdo(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@126: const uint8_t *data, /**< Kategorie-Daten */ fp@126: size_t word_count, /**< Anzahl Worte */ fp@126: ec_pdo_type_t pdo_type /**< PDO-Typ */ fp@126: ) fp@126: { fp@126: ec_eeprom_pdo_t *pdo; fp@126: ec_eeprom_pdo_entry_t *entry; fp@126: unsigned int entry_count, i; fp@126: fp@126: while (word_count >= 4) { fp@127: if (!(pdo = (ec_eeprom_pdo_t *) fp@127: kmalloc(sizeof(ec_eeprom_pdo_t), GFP_KERNEL))) { fp@126: EC_ERR("Failed to allocate PDO memory.\n"); fp@126: return -1; fp@126: } fp@126: fp@126: INIT_LIST_HEAD(&pdo->entries); fp@126: pdo->type = pdo_type; fp@126: fp@126: pdo->index = *((uint16_t *) data); fp@126: entry_count = data[2]; fp@126: pdo->sync_manager = data[3]; fp@126: pdo->name = NULL; fp@126: ec_slave_locate_string(slave, data[5], &pdo->name); fp@126: fp@126: list_add_tail(&pdo->list, &slave->eeprom_pdos); fp@126: fp@126: word_count -= 4; fp@126: data += 8; fp@126: fp@126: for (i = 0; i < entry_count; i++) { fp@126: if (!(entry = (ec_eeprom_pdo_entry_t *) fp@126: kmalloc(sizeof(ec_eeprom_pdo_entry_t), GFP_KERNEL))) { fp@126: EC_ERR("Failed to allocate PDO entry memory.\n"); fp@126: return -1; fp@126: } fp@126: fp@126: entry->index = *((uint16_t *) data); fp@126: entry->subindex = data[2]; fp@126: entry->name = NULL; fp@126: ec_slave_locate_string(slave, data[3], &entry->name); fp@126: entry->bit_length = data[5]; fp@126: fp@126: list_add_tail(&entry->list, &pdo->entries); fp@126: fp@126: word_count -= 4; fp@126: data += 8; fp@126: } fp@126: } fp@126: fp@126: return 0; fp@114: } fp@114: fp@114: /*****************************************************************************/ fp@114: fp@114: /** fp@123: Durchsucht die temporären Strings und dupliziert den gefundenen String. fp@123: */ fp@123: fp@123: int ec_slave_locate_string(ec_slave_t *slave, unsigned int index, char **ptr) fp@123: { fp@126: ec_eeprom_string_t *string; fp@126: char *err_string; fp@126: fp@126: // Erst alten Speicher freigeben fp@123: if (*ptr) { fp@123: kfree(*ptr); fp@123: *ptr = NULL; fp@123: } fp@123: fp@126: // Index 0 bedeutet "nicht belegt" fp@123: if (!index) return 0; fp@123: fp@126: // EEPROM-String mit Index finden und kopieren fp@123: list_for_each_entry(string, &slave->eeprom_strings, list) { fp@126: if (--index) continue; fp@126: fp@126: if (!(*ptr = (char *) kmalloc(string->size + 1, GFP_KERNEL))) { fp@126: EC_ERR("Unable to allocate string memory.\n"); fp@126: return -1; fp@126: } fp@126: memcpy(*ptr, string->data, string->size + 1); fp@126: return 0; fp@126: } fp@126: fp@126: EC_WARN("String %i not found in slave %i.\n", index, slave->ring_position); fp@126: fp@126: err_string = "(string not found)"; fp@126: fp@126: if (!(*ptr = (char *) kmalloc(strlen(err_string) + 1, GFP_KERNEL))) { fp@126: EC_ERR("Unable to allocate string memory.\n"); fp@126: return -1; fp@126: } fp@126: fp@126: memcpy(*ptr, err_string, strlen(err_string) + 1); fp@123: return 0; fp@123: } fp@123: fp@123: /*****************************************************************************/ fp@123: fp@123: /** fp@122: Bestätigt einen Fehler beim Zustandswechsel. fp@73: fp@91: \todo Funktioniert noch nicht... fp@73: */ fp@73: fp@73: void ec_slave_state_ack(ec_slave_t *slave, fp@122: /**< Slave, dessen Zustand geändert werden soll */ fp@73: uint8_t state fp@73: /**< Alter Zustand */ fp@73: ) fp@73: { fp@98: ec_command_t command; fp@101: uint8_t data[2]; fp@98: cycles_t start, end, timeout; fp@73: fp@77: EC_WRITE_U16(data, state | EC_ACK); fp@73: fp@98: ec_command_init_npwr(&command, slave->station_address, 0x0120, 2, data); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_WARN("State %02X acknowledge failed on slave %i!\n", fp@89: state, slave->ring_position); fp@73: return; fp@73: } fp@73: fp@98: start = get_cycles(); fp@119: timeout = (cycles_t) 10 * cpu_khz; // 10ms fp@98: fp@113: while (1) fp@73: { fp@113: udelay(100); // Dem Slave etwas Zeit lassen... fp@113: fp@98: ec_command_init_nprd(&command, slave->station_address, 0x0130, 2); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_WARN("State %02X acknowledge checking failed on slave %i!\n", fp@89: state, slave->ring_position); fp@73: return; fp@73: } fp@73: fp@98: end = get_cycles(); fp@98: fp@98: if (unlikely(EC_READ_U8(command.data) != state)) { fp@84: EC_WARN("Could not acknowledge state %02X on slave %i (code" fp@84: " %02X)!\n", state, slave->ring_position, fp@98: EC_READ_U8(command.data)); fp@73: return; fp@73: } fp@73: fp@98: if (likely(EC_READ_U8(command.data) == state)) { fp@84: EC_INFO("Acknowleged state %02X on slave %i.\n", state, fp@84: slave->ring_position); fp@73: return; fp@73: } fp@113: fp@113: if (unlikely((end - start) >= timeout)) { fp@113: EC_WARN("Could not check state acknowledgement %02X of slave %i -" fp@113: " Timeout while checking!\n", state, slave->ring_position); fp@113: return; fp@113: } fp@73: } fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@122: Ändert den Zustand eines Slaves. fp@73: fp@73: \return 0 bei Erfolg, sonst < 0 fp@73: */ fp@73: fp@73: int ec_slave_state_change(ec_slave_t *slave, fp@122: /**< Slave, dessen Zustand geändert werden soll */ fp@73: uint8_t state fp@73: /**< Neuer Zustand */ fp@73: ) fp@73: { fp@98: ec_command_t command; fp@101: uint8_t data[2]; fp@98: cycles_t start, end, timeout; fp@73: fp@77: EC_WRITE_U16(data, state); fp@73: fp@98: ec_command_init_npwr(&command, slave->station_address, 0x0120, 2, data); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_ERR("Failed to set state %02X on slave %i!\n", fp@89: state, slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@98: start = get_cycles(); fp@119: timeout = (cycles_t) 10 * cpu_khz; // 10ms fp@98: fp@113: while (1) fp@73: { fp@98: udelay(100); // Dem Slave etwas Zeit lassen... fp@98: fp@98: ec_command_init_nprd(&command, slave->station_address, 0x0130, 2); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_ERR("Failed to check state %02X on slave %i!\n", fp@84: state, slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@98: end = get_cycles(); fp@98: fp@98: if (unlikely(EC_READ_U8(command.data) & 0x10)) { // State change error fp@84: EC_ERR("Could not set state %02X - Slave %i refused state change" fp@84: " (code %02X)!\n", state, slave->ring_position, fp@98: EC_READ_U8(command.data)); fp@98: ec_slave_state_ack(slave, EC_READ_U8(command.data) & 0x0F); fp@73: return -1; fp@73: } fp@73: fp@98: if (likely(EC_READ_U8(command.data) == (state & 0x0F))) { fp@73: // State change successful fp@113: return 0; fp@113: } fp@113: fp@113: if (unlikely((end - start) >= timeout)) { fp@113: EC_ERR("Could not check state %02X of slave %i - Timeout!\n", fp@113: state, slave->ring_position); fp@113: return -1; fp@113: } fp@113: } fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@73: Merkt eine FMMU-Konfiguration vor. fp@73: fp@73: Die FMMU wird so konfiguriert, dass sie den gesamten Datenbereich des fp@122: entsprechenden Sync-Managers abdeckt. Für jede Domäne werden separate fp@73: FMMUs konfiguriert. fp@73: fp@73: Wenn die entsprechende FMMU bereits konfiguriert ist, wird dies als fp@122: Erfolg zurückgegeben. fp@73: fp@73: \return 0 bei Erfolg, sonst < 0 fp@73: */ fp@73: fp@73: int ec_slave_set_fmmu(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@122: const ec_domain_t *domain, /**< Domäne */ fp@73: const ec_sync_t *sync /**< Sync-Manager */ fp@73: ) fp@73: { fp@73: unsigned int i; fp@73: fp@73: // FMMU schon vorgemerkt? fp@73: for (i = 0; i < slave->fmmu_count; i++) fp@73: if (slave->fmmus[i].domain == domain && slave->fmmus[i].sync == sync) fp@73: return 0; fp@73: fp@91: // Neue FMMU reservieren... fp@91: fp@73: if (slave->fmmu_count >= slave->base_fmmu_count) { fp@84: EC_ERR("Slave %i FMMU limit reached!\n", slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: slave->fmmus[slave->fmmu_count].domain = domain; fp@73: slave->fmmus[slave->fmmu_count].sync = sync; fp@73: slave->fmmus[slave->fmmu_count].logical_start_address = 0; fp@73: slave->fmmu_count++; fp@73: slave->registered = 1; fp@73: fp@73: return 0; fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@122: Gibt alle Informationen über einen EtherCAT-Slave aus. fp@73: */ fp@73: fp@73: void ec_slave_print(const ec_slave_t *slave /**< EtherCAT-Slave */) fp@73: { fp@126: ec_eeprom_sync_t *sync; fp@126: ec_eeprom_pdo_t *pdo; fp@126: ec_eeprom_pdo_entry_t *entry; fp@126: fp@123: EC_INFO("x-- EtherCAT slave information ---------------\n"); fp@73: fp@73: if (slave->type) { fp@123: EC_INFO("| Vendor \"%s\", Product \"%s\": %s\n", fp@84: slave->type->vendor_name, slave->type->product_name, fp@84: slave->type->description); fp@73: } fp@73: else { fp@123: EC_INFO("| *** This slave has no type information! ***\n"); fp@123: } fp@123: fp@123: EC_INFO("| Ring position: %i, Station address: 0x%04X\n", fp@84: slave->ring_position, slave->station_address); fp@84: fp@123: EC_INFO("| Base information:\n"); fp@123: EC_INFO("| Type %u, Revision %i, Build %i\n", fp@84: slave->base_type, slave->base_revision, slave->base_build); fp@123: EC_INFO("| Supported FMMUs: %i, Sync managers: %i\n", fp@84: slave->base_fmmu_count, slave->base_sync_count); fp@84: fp@123: EC_INFO("| EEPROM data:\n"); fp@126: fp@123: if (slave->sii_alias) fp@123: EC_INFO("| Configured station alias: 0x%04X (%i)\n", fp@123: slave->sii_alias, slave->sii_alias); fp@126: fp@123: EC_INFO("| Vendor-ID: 0x%08X, Product code: 0x%08X\n", fp@84: slave->sii_vendor_id, slave->sii_product_code); fp@123: EC_INFO("| Revision number: 0x%08X, Serial number: 0x%08X\n", fp@84: slave->sii_revision_number, slave->sii_serial_number); fp@126: fp@121: if (slave->eeprom_name) fp@123: EC_INFO("| Name: %s\n", slave->eeprom_name); fp@121: if (slave->eeprom_group) fp@123: EC_INFO("| Group: %s\n", slave->eeprom_group); fp@121: if (slave->eeprom_desc) fp@123: EC_INFO("| Description: %s\n", slave->eeprom_desc); fp@126: fp@126: if (!list_empty(&slave->eeprom_syncs)) { fp@126: EC_INFO("| Sync-Managers:\n"); fp@126: list_for_each_entry(sync, &slave->eeprom_syncs, list) { fp@126: EC_INFO("| %i: 0x%04X, length %i, control 0x%02X, %s\n", fp@126: sync->index, sync->physical_start_address, sync->length, fp@126: sync->control_register, fp@126: sync->enable ? "enable" : "disable"); fp@126: } fp@126: } fp@126: fp@126: list_for_each_entry(pdo, &slave->eeprom_pdos, list) { fp@126: EC_INFO("| %s \"%s\" (0x%04X), -> Sync-Manager %i\n", fp@126: pdo->type == EC_RX_PDO ? "RXPDO" : "TXPDO", fp@126: pdo->name ? pdo->name : "???", fp@126: pdo->index, pdo->sync_manager); fp@126: fp@126: list_for_each_entry(entry, &pdo->entries, list) { fp@126: EC_INFO("| \"%s\" 0x%04X:%X, %i Bit\n", fp@126: entry->name ? entry->name : "???", fp@126: entry->index, entry->subindex, entry->bit_length); fp@126: } fp@126: } fp@126: fp@123: EC_INFO("x---------------------------------------------\n"); fp@123: } fp@123: fp@123: /*****************************************************************************/ fp@123: fp@123: /** fp@123: Gibt die Zählerstände der CRC-Fault-Counter aus und setzt diese zurück. fp@74: fp@74: \return 0 bei Erfolg, sonst < 0 fp@74: */ fp@74: fp@74: int ec_slave_check_crc(ec_slave_t *slave /**< EtherCAT-Slave */) fp@74: { fp@98: ec_command_t command; fp@74: uint8_t data[4]; fp@74: fp@98: ec_command_init_nprd(&command, slave->station_address, 0x0300, 4); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_WARN("Reading CRC fault counters failed on slave %i!\n", fp@84: slave->ring_position); fp@74: return -1; fp@74: } fp@74: fp@74: // No CRC faults. fp@98: if (!EC_READ_U16(command.data) && !EC_READ_U16(command.data + 2)) return 0; fp@74: fp@84: EC_WARN("CRC faults on slave %i. A: %i, B: %i\n", slave->ring_position, fp@98: EC_READ_U16(command.data), EC_READ_U16(command.data + 2)); fp@74: fp@74: // Reset CRC counters fp@77: EC_WRITE_U16(data, 0x0000); fp@77: EC_WRITE_U16(data + 2, 0x0000); fp@98: ec_command_init_npwr(&command, slave->station_address, 0x0300, 4, data); fp@98: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@89: EC_WARN("Resetting CRC fault counters failed on slave %i!\n", fp@84: slave->ring_position); fp@74: return -1; fp@74: } fp@74: fp@74: return 0; fp@74: } fp@74: fp@74: /*****************************************************************************/ fp@74: fp@42: /* Emacs-Konfiguration fp@42: ;;; Local Variables: *** fp@73: ;;; c-basic-offset:4 *** fp@42: ;;; End: *** fp@42: */ fp@114: