fp@0: /**************************************************************** fp@0: * fp@0: * e c _ m a s t e r . c fp@0: * fp@0: * Methoden für einen EtherCAT-Master. fp@0: * fp@0: * $Date$ fp@0: * $Author$ fp@0: * fp@0: ***************************************************************/ fp@0: fp@0: #include fp@0: #include fp@0: #include fp@0: #include fp@0: fp@0: #include "ec_globals.h" fp@0: #include "ec_master.h" fp@0: #include "ec_dbg.h" fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Konstruktor des EtherCAT-Masters. fp@0: fp@0: @param master Zeiger auf den zu initialisierenden fp@0: EtherCAT-Master fp@19: @param dev Zeiger auf das EtherCAT-Gerät, mit dem der fp@0: Master arbeiten soll fp@0: fp@0: @return 0 bei Erfolg, sonst < 0 (= dev ist NULL) fp@0: */ fp@0: fp@0: int EtherCAT_master_init(EtherCAT_master_t *master, fp@0: EtherCAT_device_t *dev) fp@0: { fp@0: if (!dev) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Master init without device!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: master->slaves = NULL; fp@0: master->slave_count = 0; fp@0: master->dev = dev; fp@0: master->command_index = 0x00; fp@0: master->tx_data_length = 0; fp@0: master->process_data = NULL; fp@0: master->process_data_length = 0; fp@2: master->debug_level = 0; fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Destruktor eines EtherCAT-Masters. fp@0: fp@0: Entfernt alle Kommandos aus der Liste, löscht den Zeiger fp@0: auf das Slave-Array und gibt die Prozessdaten frei. fp@0: fp@0: @param master Zeiger auf den zu löschenden Master fp@0: */ fp@0: fp@0: void EtherCAT_master_clear(EtherCAT_master_t *master) fp@0: { fp@0: // Remove all slaves fp@0: EtherCAT_clear_slaves(master); fp@0: fp@0: if (master->process_data) fp@0: { fp@0: kfree(master->process_data); fp@0: master->process_data = NULL; fp@0: } fp@0: fp@0: master->process_data_length = 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@11: Sendet ein einzelnes Kommando in einem Frame und fp@11: wartet auf dessen Empfang. fp@11: fp@11: @param master EtherCAT-Master fp@11: @param cmd Kommando zum Senden/Empfangen fp@11: fp@11: @return 0 bei Erfolg, sonst < 0 fp@11: */ fp@11: fp@13: int EtherCAT_simple_send_receive(EtherCAT_master_t *master, fp@13: EtherCAT_command_t *cmd) fp@13: { fp@13: unsigned int tries_left; fp@13: fp@13: if (EtherCAT_simple_send(master, cmd) < 0) return -1; fp@13: fp@19: EtherCAT_device_call_isr(master->dev); fp@19: fp@19: tries_left = 1000; fp@19: while (master->dev->state == ECAT_DS_SENT && tries_left) fp@19: { fp@19: udelay(1); fp@13: EtherCAT_device_call_isr(master->dev); fp@13: tries_left--; fp@13: } fp@13: fp@13: if (EtherCAT_simple_receive(master, cmd) < 0) return -1; fp@13: fp@13: return 0; fp@13: } fp@13: fp@13: /***************************************************************/ fp@13: fp@13: /** fp@13: Sendet ein einzelnes Kommando in einem Frame. fp@13: fp@13: @param master EtherCAT-Master fp@13: @param cmd Kommando zum Senden fp@13: fp@13: @return 0 bei Erfolg, sonst < 0 fp@13: */ fp@13: fp@13: int EtherCAT_simple_send(EtherCAT_master_t *master, fp@13: EtherCAT_command_t *cmd) fp@13: { fp@13: unsigned int length, framelength, i; fp@11: fp@11: if (master->debug_level > 0) fp@11: { fp@11: EC_DBG(KERN_DEBUG "EtherCAT_send_receive_command\n"); fp@11: } fp@11: fp@11: if (cmd->state != ECAT_CS_READY) fp@11: { fp@11: EC_DBG(KERN_WARNING "EtherCAT_send_receive_command: Command not in ready state!\n"); fp@11: } fp@11: fp@11: length = cmd->data_length + 12; fp@11: framelength = length + 2; fp@19: fp@19: if (framelength > ECAT_FRAME_BUFFER_SIZE) fp@19: { fp@19: EC_DBG(KERN_ERR "EtherCAT: Frame too long (%i)!\n", framelength); fp@19: return -1; fp@19: } fp@19: fp@11: if (framelength < 46) framelength = 46; fp@11: fp@11: if (master->debug_level > 0) fp@11: { fp@11: EC_DBG(KERN_DEBUG "Frame length: %i\n", framelength); fp@11: } fp@11: fp@11: master->tx_data[0] = length & 0xFF; fp@11: master->tx_data[1] = ((length & 0x700) >> 8) | 0x10; fp@11: fp@11: cmd->index = master->command_index; fp@11: master->command_index = (master->command_index + 1) % 0x0100; fp@11: fp@11: if (master->debug_level > 0) fp@11: { fp@11: EC_DBG(KERN_DEBUG "Sending command index %i\n", cmd->index); fp@11: } fp@11: fp@11: cmd->state = ECAT_CS_SENT; fp@11: fp@11: master->tx_data[2 + 0] = cmd->type; fp@11: master->tx_data[2 + 1] = cmd->index; fp@11: master->tx_data[2 + 2] = cmd->address.raw[0]; fp@11: master->tx_data[2 + 3] = cmd->address.raw[1]; fp@11: master->tx_data[2 + 4] = cmd->address.raw[2]; fp@11: master->tx_data[2 + 5] = cmd->address.raw[3]; fp@11: master->tx_data[2 + 6] = cmd->data_length & 0xFF; fp@11: master->tx_data[2 + 7] = (cmd->data_length & 0x700) >> 8; fp@11: master->tx_data[2 + 8] = 0x00; fp@11: master->tx_data[2 + 9] = 0x00; fp@11: fp@11: if (cmd->type == ECAT_CMD_APWR fp@11: || cmd->type == ECAT_CMD_NPWR fp@11: || cmd->type == ECAT_CMD_BWR fp@11: || cmd->type == ECAT_CMD_LRW) // Write fp@11: { fp@11: for (i = 0; i < cmd->data_length; i++) master->tx_data[2 + 10 + i] = cmd->data[i]; fp@11: } fp@11: else // Read fp@11: { fp@11: for (i = 0; i < cmd->data_length; i++) master->tx_data[2 + 10 + i] = 0x00; fp@11: } fp@11: fp@11: master->tx_data[2 + 10 + cmd->data_length] = 0x00; fp@11: master->tx_data[2 + 11 + cmd->data_length] = 0x00; fp@11: fp@11: // Pad with zeros fp@11: for (i = cmd->data_length + 12 + 2; i < 46; i++) master->tx_data[i] = 0x00; fp@11: fp@11: master->tx_data_length = framelength; fp@11: fp@11: if (master->debug_level > 0) fp@11: { fp@11: EC_DBG(KERN_DEBUG "device send...\n"); fp@11: } fp@11: fp@11: // Send frame fp@11: if (EtherCAT_device_send(master->dev, master->tx_data, framelength) != 0) fp@11: { fp@11: EC_DBG(KERN_ERR "EtherCAT: Could not send!\n"); fp@11: return -1; fp@11: } fp@11: fp@11: if (master->debug_level > 0) fp@11: { fp@11: EC_DBG(KERN_DEBUG "EtherCAT_send done.\n"); fp@11: } fp@11: fp@13: return 0; fp@13: } fp@13: fp@13: /***************************************************************/ fp@13: fp@13: /** fp@13: Wartet auf den Empfang eines einzeln gesendeten fp@13: Kommandos. fp@13: fp@13: @param master EtherCAT-Master fp@13: @param cmd Gesendetes Kommando fp@13: fp@13: @return 0 bei Erfolg, sonst < 0 fp@13: */ fp@13: fp@13: int EtherCAT_simple_receive(EtherCAT_master_t *master, fp@13: EtherCAT_command_t *cmd) fp@13: { fp@19: unsigned int length; fp@19: int receive_ret; fp@13: unsigned char command_type, command_index; fp@11: fp@19: if ((receive_ret = EtherCAT_device_receive(master->dev, fp@19: master->rx_data)) < 0) fp@19: { fp@19: return -1; fp@19: } fp@19: fp@19: master->rx_data_length = (unsigned int) receive_ret; fp@19: fp@19: if (master->rx_data_length < 2) fp@11: { fp@11: EC_DBG(KERN_ERR "EtherCAT: Received frame with incomplete EtherCAT header!\n"); fp@19: output_debug_data(master); fp@11: return -1; fp@11: } fp@11: fp@11: // Länge des gesamten Frames prüfen fp@11: length = ((master->rx_data[1] & 0x07) << 8) | (master->rx_data[0] & 0xFF); fp@11: fp@19: if (length > master->rx_data_length) fp@11: { fp@11: EC_DBG(KERN_ERR "EtherCAT: Received corrupted frame (length does not match)!\n"); fp@19: output_debug_data(master); fp@11: return -1; fp@11: } fp@11: fp@11: command_type = master->rx_data[2]; fp@11: command_index = master->rx_data[2 + 1]; fp@11: length = (master->rx_data[2 + 6] & 0xFF) | ((master->rx_data[2 + 7] & 0x07) << 8); fp@11: fp@19: if (master->rx_data_length - 2 < length + 12) fp@11: { fp@11: EC_DBG(KERN_ERR "EtherCAT: Received frame with incomplete command data!\n"); fp@19: output_debug_data(master); fp@11: return -1; fp@11: } fp@11: fp@11: if (cmd->state == ECAT_CS_SENT fp@11: && cmd->type == command_type fp@11: && cmd->index == command_index fp@11: && cmd->data_length == length) fp@11: { fp@11: cmd->state = ECAT_CS_RECEIVED; fp@11: fp@11: // Empfangene Daten in Kommandodatenspeicher kopieren fp@11: memcpy(cmd->data, master->rx_data + 2 + 10, length); fp@11: fp@11: // Working-Counter setzen fp@11: cmd->working_counter = ((master->rx_data[length + 2 + 10] & 0xFF) fp@11: | ((master->rx_data[length + 2 + 11] & 0xFF) << 8)); fp@11: } fp@11: else fp@11: { fp@11: EC_DBG(KERN_WARNING "EtherCAT: WARNING - Send/Receive anomaly!\n"); fp@19: output_debug_data(master); fp@0: } fp@0: fp@0: master->dev->state = ECAT_DS_READY; fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@17: /** fp@17: Überprüft die angeschlossenen Slaves. fp@17: fp@17: Vergleicht die an den Bus angeschlossenen Slaves mit fp@17: den im statischen-Slave-Array vorgegebenen Konfigurationen. fp@17: Stimmen Anzahl oder Typen nicht überein, gibt diese fp@17: Methode einen Fehler aus. fp@17: fp@17: @param master Der EtherCAT-Master fp@17: @param slaves Zeiger auf ein statisches Slave-Array fp@17: @param slave_count Anzahl der Slaves im Array fp@17: fp@17: @return 0 bei Erfolg, sonst < 0 fp@17: */ fp@17: fp@17: int EtherCAT_check_slaves(EtherCAT_master_t *master, fp@17: EtherCAT_slave_t *slaves, fp@17: unsigned int slave_count) fp@17: { fp@17: EtherCAT_command_t cmd; fp@17: EtherCAT_slave_t *cur; fp@17: unsigned int i, j, found, length, pos; fp@17: unsigned char data[2]; fp@17: fp@17: // EtherCAT_clear_slaves() must be called before! fp@17: if (master->slaves || master->slave_count) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT duplicate slave check!"); fp@17: return -1; fp@17: } fp@17: fp@17: // No slaves. fp@17: if (slave_count == 0) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: No slaves in list!"); fp@17: return -1; fp@17: } fp@17: fp@17: // Determine number of slaves on bus fp@17: fp@17: EtherCAT_command_broadcast_read(&cmd, 0x0000, 4); fp@17: fp@17: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@17: fp@17: if (cmd.working_counter != slave_count) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Wrong number of slaves on bus: %i / %i\n", fp@17: cmd.working_counter, slave_count); fp@17: return -1; fp@17: } fp@17: else fp@17: { fp@17: EC_DBG("EtherCAT: Found all %i slaves.\n", slave_count); fp@17: } fp@17: fp@17: // For every slave in the list fp@17: for (i = 0; i < slave_count; i++) fp@17: { fp@17: cur = &slaves[i]; fp@17: fp@17: if (!cur->desc) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Slave %i has no description.\n", i); fp@17: return -1; fp@17: } fp@17: fp@17: // Set ring position fp@17: cur->ring_position = -i; fp@17: cur->station_address = i + 1; fp@17: fp@17: // Write station address fp@17: fp@17: data[0] = cur->station_address & 0x00FF; fp@17: data[1] = (cur->station_address & 0xFF00) >> 8; fp@17: fp@17: EtherCAT_command_position_write(&cmd, cur->ring_position, 0x0010, 2, data); fp@17: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@17: fp@17: if (cmd.working_counter != 1) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Slave %i did not repond while writing station address!\n", i); fp@17: return -1; fp@17: } fp@17: fp@17: // Read base data fp@17: fp@17: EtherCAT_command_read(&cmd, cur->station_address, 0x0000, 4); fp@17: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@17: fp@17: if (cmd.working_counter != 1) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Slave %i did not respond while reading base data!\n", i); fp@17: return -1; fp@17: } fp@17: fp@17: // Get base data fp@17: cur->type = cmd.data[0]; fp@17: cur->revision = cmd.data[1]; fp@17: cur->build = cmd.data[2] | (cmd.data[3] << 8); fp@17: fp@17: // Read identification from "Slave Information Interface" (SII) fp@17: fp@17: if (EtherCAT_read_slave_information(master, cur->station_address, fp@17: 0x0008, &cur->vendor_id) != 0) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Could not read SII vendor id!\n"); fp@17: return -1; fp@17: } fp@17: fp@17: if (EtherCAT_read_slave_information(master, cur->station_address, fp@17: 0x000A, &cur->product_code) != 0) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Could not read SII product code!\n"); fp@17: return -1; fp@17: } fp@17: fp@17: if (EtherCAT_read_slave_information(master, cur->station_address, fp@17: 0x000C, &cur->revision_number) != 0) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Could not read SII revision number!\n"); fp@17: return -1; fp@17: } fp@17: fp@17: if (EtherCAT_read_slave_information(master, cur->station_address, fp@17: 0x000E, &cur->serial_number) != 0) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Could not read SII serial number!\n"); fp@17: return -1; fp@17: } fp@17: fp@17: // Search for identification in "database" fp@17: fp@17: found = 0; fp@17: fp@17: for (j = 0; j < slave_idents_count; j++) fp@17: { fp@17: if (slave_idents[j].vendor_id == cur->vendor_id fp@17: && slave_idents[j].product_code == cur->product_code) fp@2: { fp@17: found = 1; fp@17: fp@17: if (cur->desc != slave_idents[j].desc) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Unexpected slave device \"%s %s\"" fp@17: " at position %i. Expected: \"%s %s\"\n", fp@17: slave_idents[j].desc->vendor_name, fp@17: slave_idents[j].desc->product_name, i, fp@17: cur->desc->vendor_name, cur->desc->product_name); fp@17: return -1; fp@17: } fp@17: fp@17: break; fp@2: } fp@17: } fp@17: fp@17: if (!found) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Unknown slave device" fp@17: " (vendor %X, code %X) at position %i.\n", fp@17: i, cur->vendor_id, cur->product_code); fp@2: return -1; fp@2: } fp@2: } fp@0: fp@17: length = 0; fp@17: for (i = 0; i < slave_count; i++) fp@17: { fp@17: length += slaves[i].desc->data_length; fp@17: } fp@17: fp@17: if ((master->process_data = (unsigned char *) fp@17: kmalloc(sizeof(unsigned char) * length, GFP_KERNEL)) == NULL) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: Could not allocate %i bytes for process data.\n", length); fp@17: return -1; fp@17: } fp@17: fp@17: master->process_data_length = length; fp@17: memset(master->process_data, 0x00, length); fp@17: fp@17: pos = 0; fp@17: for (i = 0; i < slave_count; i++) fp@17: { fp@17: slaves[i].process_data = master->process_data + pos; fp@17: slaves[i].logical_address0 = pos; fp@17: fp@17: EC_DBG(KERN_DEBUG "EtherCAT: Slave %i - Address 0x%X, \"%s %s\", s/n %u\n", fp@17: i, pos, slaves[i].desc->vendor_name, slaves[i].desc->product_name, fp@17: slaves[i].serial_number); fp@17: fp@17: pos += slaves[i].desc->data_length; fp@17: } fp@17: fp@17: master->slaves = slaves; fp@17: master->slave_count = slave_count; fp@17: fp@17: return 0; fp@17: } fp@17: fp@17: /***************************************************************/ fp@17: fp@17: /** fp@17: Entfernt den Zeiger auf das Slave-Array. fp@17: fp@17: @param master EtherCAT-Master fp@17: */ fp@17: fp@17: void EtherCAT_clear_slaves(EtherCAT_master_t *master) fp@17: { fp@17: master->slaves = NULL; fp@17: master->slave_count = 0; fp@17: } fp@17: fp@17: /***************************************************************/ fp@17: fp@17: /** fp@17: Liest Daten aus dem Slave-Information-Interface fp@17: eines EtherCAT-Slaves. fp@17: fp@17: @param master EtherCAT-Master fp@17: @param node_address Knotenadresse des Slaves fp@17: @param offset Adresse des zu lesenden SII-Registers fp@17: @param target Zeiger auf einen 4 Byte großen Speicher fp@17: zum Ablegen der Daten fp@17: fp@17: @return 0 bei Erfolg, sonst < 0 fp@17: */ fp@17: fp@17: int EtherCAT_read_slave_information(EtherCAT_master_t *master, fp@17: unsigned short int node_address, fp@17: unsigned short int offset, fp@17: unsigned int *target) fp@17: { fp@17: EtherCAT_command_t cmd; fp@17: unsigned char data[10]; fp@17: unsigned int tries_left; fp@17: fp@17: // Initiate read operation fp@17: fp@17: data[0] = 0x00; fp@17: data[1] = 0x01; fp@17: data[2] = offset & 0xFF; fp@17: data[3] = (offset & 0xFF00) >> 8; fp@17: data[4] = 0x00; fp@17: data[5] = 0x00; fp@17: fp@17: EtherCAT_command_write(&cmd, node_address, 0x502, 6, data); fp@17: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -3; fp@17: fp@17: if (cmd.working_counter != 1) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: SII-read - Slave %04X did not respond!\n", fp@17: node_address); fp@17: return -4; fp@17: } fp@17: fp@17: // Der Slave legt die Informationen des Slave-Information-Interface fp@17: // in das Datenregister und löscht daraufhin ein Busy-Bit. Solange fp@17: // den Status auslesen, bis das Bit weg ist. fp@17: fp@17: tries_left = 100; fp@17: while (tries_left) fp@17: { fp@17: udelay(10); fp@17: fp@17: EtherCAT_command_read(&cmd, node_address, 0x502, 10); fp@17: if (EtherCAT_simple_send_receive(master, &cmd) != 0) return -3; fp@17: fp@17: if (cmd.working_counter != 1) fp@17: { fp@17: EC_DBG(KERN_ERR "EtherCAT: SII-read status - Slave %04X did not respond!\n", fp@17: node_address); fp@17: return -4; fp@17: } fp@17: fp@17: if ((cmd.data[1] & 0x81) == 0) fp@17: { fp@17: memcpy(target, cmd.data + 6, 4); fp@17: break; fp@17: } fp@17: fp@17: tries_left--; fp@17: } fp@17: fp@17: if (!tries_left) fp@17: { fp@17: EC_DBG(KERN_WARNING "EtherCAT: SSI-read. Slave %04X timed out!\n", fp@17: node_address); fp@17: return -1; fp@17: } fp@17: fp@17: return 0; fp@17: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Ändert den Zustand eines Slaves (asynchron). fp@0: fp@0: Führt eine (asynchrone) Zustandsänderung bei einem Slave durch. fp@0: fp@0: @param master EtherCAT-Master fp@0: @param slave Slave, dessen Zustand geändert werden soll fp@0: @param state_and_ack Neuer Zustand, evtl. mit gesetztem fp@0: Acknowledge-Flag fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_state_change(EtherCAT_master_t *master, fp@0: EtherCAT_slave_t *slave, fp@0: unsigned char state_and_ack) fp@0: { fp@13: EtherCAT_command_t cmd; fp@0: unsigned char data[2]; fp@0: unsigned int tries_left; fp@0: fp@0: data[0] = state_and_ack; fp@0: data[1] = 0x00; fp@0: fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0120, 2, data); fp@13: fp@13: if (EtherCAT_simple_send_receive(master, &cmd) != 0) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Unable to send!\n", state_and_ack); fp@0: return -2; fp@0: } fp@0: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Device did not respond!\n", state_and_ack); fp@0: return -3; fp@0: } fp@0: fp@0: slave->requested_state = state_and_ack & 0x0F; fp@0: fp@13: tries_left = 100; fp@0: while (tries_left) fp@0: { fp@0: udelay(10); fp@0: fp@13: EtherCAT_command_read(&cmd, slave->station_address, 0x0130, 2); fp@13: fp@13: if (EtherCAT_simple_send_receive(master, &cmd) != 0) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Unable to send!\n", state_and_ack); fp@0: return -2; fp@0: } fp@8: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Device did not respond!\n", state_and_ack); fp@0: return -3; fp@0: } fp@0: fp@13: if (cmd.data[0] & 0x10) // State change error fp@13: { fp@13: EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Device refused state change (code %02X)!\n", state_and_ack, cmd.data[0]); fp@0: return -4; fp@0: } fp@0: fp@13: if (cmd.data[0] == (state_and_ack & 0x0F)) // State change successful fp@13: { fp@0: break; fp@0: } fp@0: fp@0: tries_left--; fp@0: } fp@0: fp@0: if (!tries_left) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Timeout while checking!\n", state_and_ack); fp@0: return -5; fp@0: } fp@0: fp@0: slave->current_state = state_and_ack & 0x0F; fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Konfiguriert einen Slave und setzt den Operational-Zustand. fp@0: fp@0: Führt eine komplette Konfiguration eines Slaves durch, fp@0: setzt Sync-Manager und FMMU's, führt die entsprechenden fp@0: Zustandsübergänge durch, bis der Slave betriebsbereit ist. fp@0: fp@0: @param master EtherCAT-Master fp@0: @param slave Zu aktivierender Slave fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_activate_slave(EtherCAT_master_t *master, fp@0: EtherCAT_slave_t *slave) fp@0: { fp@13: EtherCAT_command_t cmd; fp@0: const EtherCAT_slave_desc_t *desc; fp@0: unsigned char fmmu[16]; fp@0: unsigned char data[256]; fp@0: fp@0: desc = slave->desc; fp@0: fp@0: if (EtherCAT_state_change(master, slave, ECAT_STATE_INIT) != 0) fp@0: { fp@0: return -1; fp@0: } fp@0: fp@0: // Resetting FMMU's fp@0: fp@0: memset(data, 0x00, 256); fp@0: fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0600, 256, data); fp@13: fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Resetting FMMUs - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -2; fp@0: } fp@0: fp@0: // Resetting Sync Manager channels fp@0: fp@0: if (desc->type != ECAT_ST_SIMPLE_NOSYNC) fp@0: { fp@0: memset(data, 0x00, 256); fp@0: fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0800, 256, data); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Resetting SMs - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -2; fp@0: } fp@0: } fp@0: fp@0: // Init Mailbox communication fp@0: fp@0: if (desc->type == ECAT_ST_MAILBOX) fp@0: { fp@0: if (desc->sm0) fp@0: { fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0800, 8, desc->sm0); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting SM0 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -3; fp@0: } fp@0: } fp@0: fp@0: if (desc->sm1) fp@0: { fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0808, 8, desc->sm1); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting SM1 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -2; fp@0: } fp@0: } fp@0: } fp@0: fp@0: // Change state to PREOP fp@0: fp@0: if (EtherCAT_state_change(master, slave, ECAT_STATE_PREOP) != 0) fp@0: { fp@0: return -5; fp@0: } fp@0: fp@0: // Set FMMU's fp@0: fp@0: if (desc->fmmu0) fp@0: { fp@0: memcpy(fmmu, desc->fmmu0, 16); fp@0: fp@0: fmmu[0] = slave->logical_address0 & 0x000000FF; fp@8: fmmu[1] = (slave->logical_address0 & 0x0000FF00) >> 8; fp@0: fmmu[2] = (slave->logical_address0 & 0x00FF0000) >> 16; fp@0: fmmu[3] = (slave->logical_address0 & 0xFF000000) >> 24; fp@0: fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0600, 16, fmmu); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting FMMU0 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -2; fp@0: } fp@0: } fp@0: fp@0: // Set Sync Managers fp@0: fp@0: if (desc->type != ECAT_ST_MAILBOX) fp@0: { fp@0: if (desc->sm0) fp@0: { fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0800, 8, desc->sm0); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting SM0 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -3; fp@0: } fp@0: } fp@0: fp@0: if (desc->sm1) fp@0: { fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0808, 8, desc->sm1); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting SM1 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -3; fp@0: } fp@8: } fp@8: } fp@8: fp@0: if (desc->sm2) fp@0: { fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0810, 8, desc->sm2); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting SM2 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -3; fp@0: } fp@0: } fp@0: fp@0: if (desc->sm3) fp@0: { fp@13: EtherCAT_command_write(&cmd, slave->station_address, 0x0818, 8, desc->sm3); fp@13: if (EtherCAT_simple_send_receive(master, &cmd) < 0) return -1; fp@13: fp@13: if (cmd.working_counter != 1) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Setting SM3 - Slave %04X did not respond!\n", fp@0: slave->station_address); fp@0: return -3; fp@0: } fp@0: } fp@0: fp@0: // Change state to SAVEOP fp@0: if (EtherCAT_state_change(master, slave, ECAT_STATE_SAVEOP) != 0) fp@0: { fp@0: return -12; fp@0: } fp@0: fp@0: // Change state to OP fp@0: if (EtherCAT_state_change(master, slave, ECAT_STATE_OP) != 0) fp@0: { fp@0: return -13; fp@0: } fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Setzt einen Slave zurück in den Init-Zustand. fp@0: fp@0: @param master EtherCAT-Master fp@0: @param slave Zu deaktivierender Slave fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_deactivate_slave(EtherCAT_master_t *master, fp@0: EtherCAT_slave_t *slave) fp@0: { fp@0: if (EtherCAT_state_change(master, slave, ECAT_STATE_INIT) != 0) fp@0: { fp@0: return -1; fp@0: } fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Aktiviert alle Slaves. fp@0: fp@0: @see EtherCAT_activate_slave fp@0: fp@0: @param master EtherCAT-Master fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_activate_all_slaves(EtherCAT_master_t *master) fp@0: { fp@0: unsigned int i; fp@0: fp@0: for (i = 0; i < master->slave_count; i++) fp@0: { fp@0: if (EtherCAT_activate_slave(master, &master->slaves[i]) < 0) fp@0: { fp@0: return -1; fp@0: } fp@0: } fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Deaktiviert alle Slaves. fp@0: fp@0: @see EtherCAT_deactivate_slave fp@0: fp@0: @param master EtherCAT-Master fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_deactivate_all_slaves(EtherCAT_master_t *master) fp@0: { fp@0: unsigned int i; fp@0: int ret = 0; fp@0: fp@0: for (i = 0; i < master->slave_count; i++) fp@0: { fp@0: if (EtherCAT_deactivate_slave(master, &master->slaves[i]) < 0) fp@0: { fp@0: ret = -1; fp@0: } fp@0: } fp@0: fp@0: return ret; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Sendet alle Prozessdaten an die Slaves. fp@0: fp@0: Erstellt ein "logical read write"-Kommando mit den fp@0: Prozessdaten des Masters und sendet es an den Bus. fp@0: fp@0: @param master EtherCAT-Master fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_write_process_data(EtherCAT_master_t *master) fp@0: { fp@13: EtherCAT_command_logical_read_write(&master->process_data_command, fp@13: 0, master->process_data_length, fp@13: master->process_data); fp@13: fp@13: if (EtherCAT_simple_send(master, &master->process_data_command) < 0) fp@13: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not send process data command!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: /***************************************************************/ fp@0: fp@0: /** fp@0: Empfängt alle Prozessdaten von den Slaves. fp@0: fp@0: Empfängt ein zuvor gesendetes "logical read write"-Kommando fp@0: und kopiert die empfangenen daten in den Prozessdatenspeicher fp@0: des Masters. fp@0: fp@0: @param master EtherCAT-Master fp@8: fp@0: @return 0 bei Erfolg, sonst < 0 fp@0: */ fp@0: fp@0: int EtherCAT_read_process_data(EtherCAT_master_t *master) fp@0: { fp@19: unsigned int tries_left; fp@19: fp@13: EtherCAT_device_call_isr(master->dev); fp@13: fp@19: tries_left = 1000; fp@19: while (master->dev->state == ECAT_DS_SENT && tries_left) fp@19: { fp@19: udelay(1); fp@19: EtherCAT_device_call_isr(master->dev); fp@19: tries_left--; fp@19: } fp@19: fp@19: if (!tries_left) fp@19: { fp@19: EC_DBG(KERN_ERR "EtherCAT: Timeout while receiving process data!\n"); fp@19: return -1; fp@19: } fp@19: fp@13: if (EtherCAT_simple_receive(master, &master->process_data_command) < 0) fp@13: { fp@13: EC_DBG(KERN_ERR "EtherCAT: Could not receive cyclic command!\n"); fp@0: return -1; fp@0: } fp@0: fp@13: if (master->process_data_command.state != ECAT_CS_RECEIVED) fp@13: { fp@13: EC_DBG(KERN_WARNING "EtherCAT: Process data command not received!\n"); fp@13: return -1; fp@13: } fp@13: fp@13: // Daten von Kommando in Prozessdaten des Master kopieren fp@13: memcpy(master->process_data, master->process_data_command.data, fp@13: master->process_data_length); fp@13: fp@13: return 0; fp@13: } fp@13: fp@13: /***************************************************************/ fp@13: fp@13: /** fp@13: Verwirft das zuletzt gesendete Prozessdatenkommando. fp@13: fp@13: @param master EtherCAT-Master fp@13: */ fp@13: fp@13: void EtherCAT_clear_process_data(EtherCAT_master_t *master) fp@13: { hm@6: EtherCAT_device_call_isr(master->dev); fp@13: master->dev->state = ECAT_DS_READY; fp@2: } fp@2: fp@2: /***************************************************************/ fp@2: fp@2: /** fp@11: Gibt Frame-Inhalte zwecks Debugging aus. fp@11: fp@20: @param master EtherCAT-Master fp@11: */ fp@11: fp@19: void output_debug_data(const EtherCAT_master_t *master) fp@11: { fp@11: unsigned int i; fp@11: fp@19: EC_DBG(KERN_DEBUG "EtherCAT: tx_data content (%i Bytes):\n", fp@19: master->tx_data_length); fp@19: fp@11: EC_DBG(KERN_DEBUG); fp@19: for (i = 0; i < master->tx_data_length; i++) fp@19: { fp@19: EC_DBG("%02X ", master->tx_data[i]); fp@11: if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG); fp@11: } fp@11: EC_DBG("\n"); fp@19: fp@19: EC_DBG(KERN_DEBUG "EtherCAT: rx_data content (%i Bytes):\n", fp@19: master->rx_data_length); fp@19: fp@19: EC_DBG(KERN_DEBUG); fp@19: for (i = 0; i < master->rx_data_length; i++) fp@19: { fp@19: EC_DBG("%02X ", master->rx_data[i]); fp@19: if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG); fp@19: } fp@19: EC_DBG("\n"); fp@19: } fp@19: fp@19: /***************************************************************/