fp@39: /****************************************************************************** fp@0: * fp@54: * s l a v e . c fp@0: * fp@0: * 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@73: #include "frame.h" fp@0: fp@39: /*****************************************************************************/ fp@0: 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@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@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@73: // Nichts freizugeben fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@73: Liest alle benötigten Informationen aus einem Slave. fp@73: */ fp@73: fp@73: int ec_slave_fetch(ec_slave_t *slave /**< EtherCAT-Slave */) fp@73: { fp@73: ec_frame_t frame; fp@73: fp@73: // Read base data fp@73: ec_frame_init_nprd(&frame, slave->master, slave->station_address, fp@73: 0x0000, 6); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame))) return -1; fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: Slave %i did not respond while reading base" fp@73: " data!\n", slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: slave->base_type = frame.data[0]; fp@73: slave->base_revision = frame.data[1]; fp@73: slave->base_build = frame.data[2] | (frame.data[3] << 8); fp@73: slave->base_fmmu_count = frame.data[4]; fp@73: slave->base_sync_count = frame.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@73: if (unlikely(ec_slave_sii_read(slave, 0x0008, &slave->sii_vendor_id))) { fp@73: printk(KERN_ERR "EtherCAT: 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@73: printk(KERN_ERR "EtherCAT: 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@73: printk(KERN_ERR "EtherCAT: 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@73: printk(KERN_ERR "EtherCAT: Could not read SII serial number!\n"); fp@73: return -1; fp@73: } fp@73: 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@73: unsigned short int offset, fp@73: /**< Adresse des zu lesenden SII-Registers */ fp@73: unsigned int *target fp@73: /**< Zeiger auf einen 4 Byte großen Speicher zum Ablegen fp@73: der Daten */ fp@73: ) fp@73: { fp@73: ec_frame_t frame; fp@73: unsigned char data[10]; fp@73: unsigned int tries_left; fp@73: fp@73: // Initiate read operation fp@73: fp@73: data[0] = 0x00; fp@73: data[1] = 0x01; fp@73: data[2] = offset & 0xFF; fp@73: data[3] = (offset & 0xFF00) >> 8; fp@73: data[4] = 0x00; fp@73: data[5] = 0x00; fp@73: fp@73: ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x502, 6, fp@73: data); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame))) return -1; fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: SII-read - Slave %i did not respond!\n", fp@73: slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: // Der Slave legt die Informationen des Slave-Information-Interface fp@73: // in das Datenregister und löscht daraufhin ein Busy-Bit. Solange fp@73: // den Status auslesen, bis das Bit weg ist. fp@73: fp@73: tries_left = 100; fp@73: while (likely(tries_left)) fp@73: { fp@73: udelay(10); fp@73: fp@73: ec_frame_init_nprd(&frame, slave->master, slave->station_address, 0x502, fp@73: 10); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame))) return -1; fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: SII-read status -" fp@73: " Slave %i did not respond!\n", slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: if (likely((frame.data[1] & 0x81) == 0)) { fp@73: memcpy(target, frame.data + 6, 4); fp@73: break; fp@73: } fp@73: fp@73: tries_left--; fp@73: } fp@73: fp@73: if (unlikely(!tries_left)) { fp@73: printk(KERN_WARNING "EtherCAT: SSI-read. Slave %i timed out!\n", fp@73: slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: return 0; fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@73: Bestätigt einen Fehler beim Zustandswechsel. fp@73: fp@73: FIXME Funktioniert noch nicht... fp@73: */ fp@73: fp@73: void ec_slave_state_ack(ec_slave_t *slave, fp@73: /**< Slave, dessen Zustand geändert werden soll */ fp@73: uint8_t state fp@73: /**< Alter Zustand */ fp@73: ) fp@73: { fp@73: ec_frame_t frame; fp@73: unsigned char data[2]; fp@73: unsigned int tries_left; fp@73: fp@73: data[0] = state | EC_ACK; fp@73: data[1] = 0x00; fp@73: fp@73: ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0120, fp@73: 2, data); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame) != 0)) { fp@73: printk(KERN_ERR "EtherCAT: Could no acknowledge state %02X - Unable to" fp@73: " send!\n", state); fp@73: return; fp@73: } fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: Could not acknowledge state %02X - Slave" fp@73: " %i did not respond!\n", state, slave->ring_position); fp@73: return; fp@73: } fp@73: fp@73: tries_left = 100; fp@73: while (likely(tries_left)) fp@73: { fp@73: udelay(10); fp@73: fp@73: ec_frame_init_nprd(&frame, slave->master, slave->station_address, fp@73: 0x0130, 2); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame) != 0)) { fp@73: printk(KERN_ERR "EtherCAT: Could not check state acknowledgement" fp@73: " %02X - Unable to send!\n", state); fp@73: return; fp@73: } fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: Could not check state acknowledgement" fp@73: " %02X - Slave %i did not respond!\n", state, fp@73: slave->ring_position); fp@73: return; fp@73: } fp@73: fp@73: if (unlikely(frame.data[0] != state)) { fp@73: printk(KERN_ERR "EtherCAT: Could not acknowledge state %02X on" fp@73: " slave %i (code %02X)!\n", state, slave->ring_position, fp@73: frame.data[0]); fp@73: return; fp@73: } fp@73: fp@73: if (likely(frame.data[0] == state)) { fp@73: printk(KERN_INFO "EtherCAT: Acknowleged state %02X on slave %i.\n", fp@73: state, slave->ring_position); fp@73: return; fp@73: } fp@73: fp@73: tries_left--; fp@73: } fp@73: fp@73: if (unlikely(!tries_left)) { fp@73: printk(KERN_ERR "EtherCAT: Could not check state acknowledgement %02X" fp@73: " of slave %i - Timeout while checking!\n", state, fp@73: slave->ring_position); fp@73: return; fp@73: } fp@73: } fp@73: fp@73: /*****************************************************************************/ fp@73: fp@73: /** fp@73: Ä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@73: /**< Slave, dessen Zustand geändert werden soll */ fp@73: uint8_t state fp@73: /**< Neuer Zustand */ fp@73: ) fp@73: { fp@73: ec_frame_t frame; fp@73: unsigned char data[2]; fp@73: unsigned int tries_left; fp@73: fp@73: data[0] = state; fp@73: data[1] = 0x00; fp@73: fp@73: ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0120, fp@73: 2, data); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame) != 0)) { fp@73: printk(KERN_ERR "EtherCAT: Could not set state %02X - Unable to" fp@73: " send!\n", state); fp@73: return -1; fp@73: } fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: Could not set state %02X - Slave %i did not" fp@73: " respond!\n", state, slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: tries_left = 100; fp@73: while (likely(tries_left)) fp@73: { fp@73: udelay(10); fp@73: fp@73: ec_frame_init_nprd(&frame, slave->master, slave->station_address, fp@73: 0x0130, 2); fp@73: fp@73: if (unlikely(ec_frame_send_receive(&frame) != 0)) { fp@73: printk(KERN_ERR "EtherCAT: Could not check state %02X - Unable to" fp@73: " send!\n", state); fp@73: return -1; fp@73: } fp@73: fp@73: if (unlikely(frame.working_counter != 1)) { fp@73: printk(KERN_ERR "EtherCAT: Could not check state %02X - Slave %i" fp@73: " did not respond!\n", state, slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: if (unlikely(frame.data[0] & 0x10)) { // State change error fp@73: printk(KERN_ERR "EtherCAT: Could not set state %02X - Slave %i" fp@73: " refused state change (code %02X)!\n", state, fp@73: slave->ring_position, frame.data[0]); fp@73: ec_slave_state_ack(slave, frame.data[0] & 0x0F); fp@73: return -1; fp@73: } fp@73: fp@73: if (likely(frame.data[0] == (state & 0x0F))) { fp@73: // State change successful fp@73: break; fp@73: } fp@73: fp@73: tries_left--; fp@73: } fp@73: fp@73: if (unlikely(!tries_left)) { fp@73: printk(KERN_ERR "EtherCAT: Could not check state %02X of slave %i -" fp@73: " Timeout while checking!\n", state, fp@73: slave->ring_position); fp@73: return -1; fp@73: } fp@73: fp@73: return 0; 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@73: 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@73: 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@73: 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@73: if (slave->fmmu_count >= slave->base_fmmu_count) { fp@73: printk(KERN_ERR "EtherCAT: Slave %i supports only %i FMMUs.\n", fp@73: slave->ring_position, slave->base_fmmu_count); 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@73: 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@73: printk(KERN_INFO "--- EtherCAT slave information ---\n"); fp@73: fp@73: if (slave->type) { fp@73: printk(KERN_INFO " Vendor \"%s\", Product \"%s\": %s\n", fp@73: slave->type->vendor_name, slave->type->product_name, fp@73: slave->type->description); fp@73: } fp@73: else { fp@73: printk(KERN_INFO " *** This slave has no type information! ***\n"); fp@73: } fp@73: fp@73: printk(KERN_INFO " Ring position: %i, Station address: 0x%04X\n", fp@73: slave->ring_position, slave->station_address); fp@73: fp@73: printk(KERN_INFO " Base information:\n"); fp@73: printk(KERN_INFO " Type %u, Revision %i, Build %i\n", fp@73: slave->base_type, slave->base_revision, slave->base_build); fp@73: printk(KERN_INFO " Supported FMMUs: %i, Sync managers: %i\n", fp@73: slave->base_fmmu_count, slave->base_sync_count); fp@73: fp@73: printk(KERN_INFO " Slave information interface:\n"); fp@73: printk(KERN_INFO " Vendor-ID: 0x%08X, Product code: 0x%08X\n", fp@73: slave->sii_vendor_id, slave->sii_product_code); fp@73: printk(KERN_INFO " Revision number: 0x%08X, Serial number: 0x%08X\n", fp@73: slave->sii_revision_number, slave->sii_serial_number); fp@0: } fp@0: fp@39: /*****************************************************************************/ fp@0: fp@74: /** fp@74: 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@74: ec_frame_t frame; fp@74: uint8_t data[4]; fp@74: uint16_t crc[2]; fp@74: fp@74: ec_frame_init_nprd(&frame, slave->master, slave->station_address, 0x0300, fp@74: 4); fp@74: fp@74: if (unlikely(ec_frame_send_receive(&frame))) { fp@74: printk(KERN_WARNING "EtherCAT: Reading CRC fault counters failed" fp@74: " on slave %i - Could not send command!\n", fp@74: slave->ring_position); fp@74: return -1; fp@74: } fp@74: fp@74: if (unlikely(frame.working_counter != 1)) { fp@74: printk(KERN_WARNING "EtherCAT: Reading CRC fault counters -" fp@74: " Slave %i did not respond!\n", slave->ring_position); fp@74: return -1; fp@74: } fp@74: fp@74: crc[0] = frame.data[0] | (frame.data[1] << 8); fp@74: crc[1] = frame.data[2] | (frame.data[3] << 8); fp@74: fp@74: // No CRC faults. fp@74: if (!crc[0] && !crc[1]) return 0; fp@74: fp@74: printk(KERN_INFO "EtherCAT: CRC faults on slave %i. A: %i, B: %i\n", fp@74: slave->ring_position, crc[0], crc[1]); fp@74: fp@74: // Reset CRC counters fp@74: memset(data, 0x00, 4); fp@74: ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0300, fp@74: 4, data); fp@74: fp@74: if (unlikely(ec_frame_send_receive(&frame))) return -1; fp@74: fp@74: if (unlikely(frame.working_counter != 1)) { fp@74: printk(KERN_ERR "EtherCAT: Resetting CRC fault counters - Slave" fp@74: " %i did not respond!\n", 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: */