fp@238: /****************************************************************************** fp@238: * fp@238: * $Id$ fp@238: * fp@238: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@238: * fp@238: * This file is part of the IgH EtherCAT Master. fp@238: * fp@238: * The IgH EtherCAT Master is free software; you can redistribute it fp@238: * and/or modify it under the terms of the GNU General Public License fp@246: * as published by the Free Software Foundation; either version 2 of the fp@246: * License, or (at your option) any later version. fp@238: * fp@238: * The IgH EtherCAT Master is distributed in the hope that it will be fp@238: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@238: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@238: * GNU General Public License for more details. fp@238: * fp@238: * You should have received a copy of the GNU General Public License fp@238: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@238: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@238: * fp@246: * The right to use EtherCAT Technology is granted and comes free of fp@246: * charge under condition of compatibility of product made by fp@246: * Licensee. People intending to distribute/sell products based on the fp@246: * code, have to sign an agreement to guarantee that products using fp@246: * software based on IgH EtherCAT master stay compatible with the actual fp@246: * EtherCAT specification (which are released themselves as an open fp@246: * standard) as the (only) precondition to have the right to use EtherCAT fp@246: * Technology, IP and trade marks. fp@246: * fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@238: \file fp@238: EtherCAT finite state machines. fp@238: */ fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: #include "globals.h" fp@238: #include "fsm.h" fp@238: #include "master.h" fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: #define EC_CAT_MEM 0x100 fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: void ec_fsm_master_start(ec_fsm_t *); fp@238: void ec_fsm_master_wait(ec_fsm_t *); fp@238: void ec_fsm_master_slave(ec_fsm_t *); fp@238: void ec_fsm_master_calc(ec_fsm_t *); fp@238: void ec_fsm_master_finished(ec_fsm_t *); fp@238: fp@238: void ec_fsm_slave_start(ec_fsm_t *); fp@238: void ec_fsm_slave_read_base(ec_fsm_t *); fp@238: void ec_fsm_slave_read_dl(ec_fsm_t *); fp@238: void ec_fsm_slave_prepare_sii(ec_fsm_t *); fp@238: void ec_fsm_slave_read_sii(ec_fsm_t *); fp@238: void ec_fsm_slave_categories(ec_fsm_t *); fp@238: void ec_fsm_slave_category_header(ec_fsm_t *); fp@238: void ec_fsm_slave_category_data(ec_fsm_t *); fp@238: void ec_fsm_slave_finished(ec_fsm_t *); fp@238: fp@238: void ec_fsm_sii_start_reading(ec_fsm_t *); fp@238: void ec_fsm_sii_check(ec_fsm_t *); fp@238: void ec_fsm_sii_fetch(ec_fsm_t *); fp@238: void ec_fsm_sii_finished(ec_fsm_t *); fp@238: void ec_fsm_sii_error(ec_fsm_t *); fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: int ec_fsm_init(ec_fsm_t *fsm, ec_master_t *master) fp@238: { fp@238: fsm->master = master; fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_slaves_responding = 0; fp@238: fsm->master_slave_states = EC_SLAVE_STATE_UNKNOWN; fp@238: fsm->slave_cat_data = NULL; fp@238: fp@238: ec_command_init(&fsm->command); fp@238: if (ec_command_prealloc(&fsm->command, EC_MAX_DATA_SIZE)) { fp@238: EC_ERR("FSM failed to allocate FSM command.\n"); fp@238: return -1; fp@238: } fp@238: fp@238: return 0; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: void ec_fsm_clear(ec_fsm_t *fsm) fp@238: { fp@238: if (fsm->slave_cat_data) kfree(fsm->slave_cat_data); fp@238: ec_command_clear(&fsm->command); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: void ec_fsm_reset(ec_fsm_t *fsm) fp@238: { fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_slaves_responding = 0; fp@238: fsm->master_slave_states = EC_SLAVE_STATE_UNKNOWN; fp@238: fp@238: if (fsm->slave_cat_data) { fp@238: kfree(fsm->slave_cat_data); fp@238: fsm->slave_cat_data = NULL; fp@238: } fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: void ec_fsm_execute(ec_fsm_t *fsm) fp@238: { fp@238: fsm->master_state(fsm); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: int ec_fsm_idle(const ec_fsm_t *fsm) fp@238: { fp@238: return (fsm->master_state == ec_fsm_master_start || fp@238: fsm->master_state == ec_fsm_master_wait || fp@238: fsm->master_state == ec_fsm_master_finished); fp@238: } fp@238: fp@238: /****************************************************************************** fp@238: * master state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@238: State: Start. fp@238: Starts with getting slave count and slave states. fp@238: */ fp@238: fp@238: void ec_fsm_master_start(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_brd(&fsm->command, 0x0130, 2); fp@238: ec_master_queue_command(fsm->master, &fsm->command); fp@238: fp@238: fsm->master_state = ec_fsm_master_wait; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: void ec_fsm_master_wait(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: unsigned int first, topology_change, i; fp@238: ec_slave_t *slave; fp@238: fp@238: if (command->state != EC_CMD_RECEIVED) { fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@238: if (command->working_counter == fsm->master_slaves_responding && fp@238: command->data[0] == fsm->master_slave_states) { fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@238: topology_change = command->working_counter != fp@238: fsm->master_slaves_responding; fp@238: fp@238: fsm->master_slaves_responding = command->working_counter; fp@238: fsm->master_slave_states = command->data[0]; fp@238: fp@238: EC_INFO("FSM: %i slave%s responding (", fsm->master_slaves_responding, fp@238: fsm->master_slaves_responding == 1 ? "" : "s"); fp@238: fp@238: first = 1; fp@238: if (fsm->master_slave_states & EC_SLAVE_STATE_INIT) { fp@238: printk("INIT"); fp@238: first = 0; fp@238: } fp@238: if (fsm->master_slave_states & EC_SLAVE_STATE_PREOP) { fp@238: if (!first) printk(", "); fp@238: printk("PREOP"); fp@238: first = 0; fp@238: } fp@238: if (fsm->master_slave_states & EC_SLAVE_STATE_SAVEOP) { fp@238: if (!first) printk(", "); fp@238: printk("SAVEOP"); fp@238: first = 0; fp@238: } fp@238: if (fsm->master_slave_states & EC_SLAVE_STATE_OP) { fp@238: if (!first) printk(", "); fp@238: printk("OP"); fp@238: } fp@238: printk(")\n"); fp@238: fp@238: if (!topology_change || fsm->master->mode == EC_MASTER_MODE_RUNNING) { fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@238: // topology change! fp@238: ec_master_clear_slaves(fsm->master); fp@238: fp@238: if (!fsm->master_slaves_responding) { fp@238: // no slaves present -> finish state machine. fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@238: // init slaves fp@238: for (i = 0; i < fsm->master_slaves_responding; i++) { fp@238: if (!(slave = fp@238: (ec_slave_t *) kmalloc(sizeof(ec_slave_t), GFP_ATOMIC))) { fp@238: EC_ERR("FSM failed to allocate slave %i!\n", i); fp@238: fsm->master_state = ec_fsm_master_finished; fp@238: return; fp@238: } fp@238: fp@238: if (ec_slave_init(slave, fsm->master, i, i + 1)) { fp@238: fsm->master_state = ec_fsm_master_finished; fp@238: return; fp@238: } fp@238: fp@238: if (kobject_add(&slave->kobj)) { fp@238: EC_ERR("FSM failed to add kobject.\n"); fp@238: kobject_put(&slave->kobj); // free fp@238: fsm->master_state = ec_fsm_master_finished; fp@238: return; fp@238: } fp@238: fp@238: list_add_tail(&slave->list, &fsm->master->slaves); fp@238: } fp@238: fp@238: // begin scanning of slaves fp@238: fsm->slave = list_entry(fsm->master->slaves.next, fp@238: ec_slave_t, list); fp@238: fsm->slave_state = ec_fsm_slave_start; fp@238: fp@238: fsm->master_state = ec_fsm_master_slave; fp@238: fsm->master_state(fsm); // execute immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: State: Get Slave. fp@238: Executes the sub-statemachine of a slave. fp@238: */ fp@238: fp@238: void ec_fsm_master_slave(ec_fsm_t *fsm) fp@238: { fp@238: ec_master_t *master = fsm->master; fp@238: fp@238: fsm->slave_state(fsm); // execute slave state machine fp@238: fp@238: if (fsm->slave_state != ec_fsm_slave_finished) return; fp@238: fp@238: // have all slaves been fetched? fp@238: if (fsm->slave->list.next == &master->slaves) { fp@238: fsm->master_state = ec_fsm_master_calc; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@238: // process next slave fp@238: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@238: fsm->slave_state = ec_fsm_slave_start; fp@238: fsm->slave_state(fsm); // execute immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Free-Run state: Calc. fp@238: */ fp@238: fp@238: void ec_fsm_master_calc(ec_fsm_t *fsm) fp@238: { fp@238: uint16_t coupler_index, coupler_subindex; fp@238: uint16_t reverse_coupler_index, current_coupler_index; fp@238: ec_slave_t *slave; fp@238: ec_slave_ident_t *ident; fp@238: ec_master_t *master = fsm->master; fp@238: fp@238: coupler_index = 0; fp@238: reverse_coupler_index = 0xFFFF; fp@238: current_coupler_index = 0x3FFF; fp@238: coupler_subindex = 0; fp@238: fp@238: // for every slave on the bus fp@238: list_for_each_entry(slave, &master->slaves, list) fp@238: { fp@238: // search for identification in "database" fp@238: ident = slave_idents; fp@238: while (ident->type) { fp@238: if (unlikely(ident->vendor_id == slave->sii_vendor_id fp@238: && ident->product_code == slave->sii_product_code)) { fp@238: slave->type = ident->type; fp@238: break; fp@238: } fp@238: ident++; fp@238: } fp@238: fp@238: if (!slave->type) { fp@238: EC_WARN("FSM: Unknown slave device (vendor 0x%08X, code 0x%08X) at" fp@238: " position %i.\n", slave->sii_vendor_id, fp@238: slave->sii_product_code, slave->ring_position); fp@238: } fp@238: else if (slave->type->special == EC_TYPE_BUS_COUPLER) { fp@238: if (slave->sii_alias) fp@238: current_coupler_index = reverse_coupler_index--; fp@238: else fp@238: current_coupler_index = coupler_index++; fp@238: coupler_subindex = 0; fp@238: } fp@238: fp@238: slave->coupler_index = current_coupler_index; fp@238: slave->coupler_subindex = coupler_subindex; fp@238: coupler_subindex++; fp@238: } fp@238: fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Free-Run state: Finished. fp@238: End state of the state machine. Does nothing. fp@238: */ fp@238: fp@238: void ec_fsm_master_finished(ec_fsm_t *fsm) fp@238: { fp@238: } fp@238: fp@238: /****************************************************************************** fp@238: * slave state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Start. fp@238: First state of the slave state machine. Writes the station address to the fp@238: slave, according to its ring position. fp@238: */ fp@238: fp@238: void ec_fsm_slave_start(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: fp@238: // write station address fp@238: ec_command_apwr(command, fsm->slave->ring_position, 0x0010, 2); fp@238: EC_WRITE_U16(command->data, fsm->slave->station_address); fp@238: ec_master_queue_command(fsm->master, command); fp@238: fsm->slave_state = ec_fsm_slave_read_base; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Read base. fp@238: */ fp@238: fp@238: void ec_fsm_slave_read_base(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: fp@238: if (command->state != EC_CMD_RECEIVED || command->working_counter != 1) { fp@238: EC_ERR("FSM failed to write station address of slave %i.\n", fp@238: fsm->slave->ring_position); fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: return; fp@238: } fp@238: fp@238: // read base data fp@238: ec_command_nprd(command, fsm->slave->station_address, 0x0000, 6); fp@238: ec_master_queue_command(fsm->master, command); fp@238: fsm->slave_state = ec_fsm_slave_read_dl; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Read DL. fp@238: */ fp@238: fp@238: void ec_fsm_slave_read_dl(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: ec_slave_t *slave = fsm->slave; fp@238: fp@238: if (command->state != EC_CMD_RECEIVED || command->working_counter != 1) { fp@238: EC_ERR("FSM failed to read base data of slave %i.\n", fp@238: slave->ring_position); fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: return; fp@238: } fp@238: fp@238: slave->base_type = EC_READ_U8 (command->data); fp@238: slave->base_revision = EC_READ_U8 (command->data + 1); fp@238: slave->base_build = EC_READ_U16(command->data + 2); fp@238: slave->base_fmmu_count = EC_READ_U8 (command->data + 4); fp@238: slave->base_sync_count = EC_READ_U8 (command->data + 5); fp@238: fp@238: if (slave->base_fmmu_count > EC_MAX_FMMUS) fp@238: slave->base_fmmu_count = EC_MAX_FMMUS; fp@238: fp@238: // read data link status fp@238: ec_command_nprd(command, slave->station_address, 0x0110, 2); fp@238: ec_master_queue_command(slave->master, command); fp@238: fsm->slave_state = ec_fsm_slave_prepare_sii; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Prepare SII. fp@238: */ fp@238: fp@238: void ec_fsm_slave_prepare_sii(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: ec_slave_t *slave = fsm->slave; fp@238: uint16_t dl_status; fp@238: unsigned int i; fp@238: fp@238: if (command->state != EC_CMD_RECEIVED || command->working_counter != 1) { fp@238: EC_ERR("FSM failed to read DL status of slave %i.\n", fp@238: slave->ring_position); fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: return; fp@238: } fp@238: fp@238: dl_status = EC_READ_U16(command->data); fp@238: fp@238: for (i = 0; i < 4; i++) { fp@238: slave->dl_link[i] = dl_status & (1 << (4 + i)) ? 1 : 0; fp@238: slave->dl_loop[i] = dl_status & (1 << (8 + i * 2)) ? 1 : 0; fp@238: slave->dl_signal[i] = dl_status & (1 << (9 + i * 2)) ? 1 : 0; fp@238: } fp@238: fp@238: fsm->sii_offset = 0x0004; fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@238: fsm->slave_sii_num = 0; fp@238: fsm->slave_state = ec_fsm_slave_read_sii; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Read SII. fp@238: */ fp@238: fp@238: void ec_fsm_slave_read_sii(ec_fsm_t *fsm) fp@238: { fp@238: ec_slave_t *slave = fsm->slave; fp@238: fp@238: // execute SII state machine fp@238: fsm->sii_state(fsm); fp@238: fp@238: if (fsm->sii_state == ec_fsm_sii_error) { fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: EC_ERR("FSM failed to read SII data at 0x%04X on slave %i.\n", fp@238: fsm->sii_offset, slave->ring_position); fp@238: return; fp@238: } fp@238: fp@238: if (fsm->sii_state != ec_fsm_sii_finished) return; fp@238: fp@238: switch (fsm->slave_sii_num) { fp@238: case 0: fp@238: slave->sii_alias = fsm->sii_result & 0xFFFF; fp@238: fsm->sii_offset = 0x0008; fp@238: break; fp@238: case 1: fp@238: slave->sii_vendor_id = fsm->sii_result; fp@238: fsm->sii_offset = 0x000A; fp@238: break; fp@238: case 2: fp@238: slave->sii_product_code = fsm->sii_result; fp@238: fsm->sii_offset = 0x000C; fp@238: break; fp@238: case 3: fp@238: slave->sii_revision_number = fsm->sii_result; fp@238: fsm->sii_offset = 0x000E; fp@238: break; fp@238: case 4: fp@238: slave->sii_serial_number = fsm->sii_result; fp@238: fsm->sii_offset = 0x0018; fp@238: break; fp@238: case 5: fp@238: slave->sii_rx_mailbox_offset = fsm->sii_result & 0xFFFF; fp@238: slave->sii_rx_mailbox_size = fsm->sii_result >> 16; fp@238: fsm->sii_offset = 0x001A; fp@238: break; fp@238: case 6: fp@238: slave->sii_tx_mailbox_offset = fsm->sii_result & 0xFFFF; fp@238: slave->sii_tx_mailbox_size = fsm->sii_result >> 16; fp@238: fsm->sii_offset = 0x001C; fp@238: break; fp@238: case 7: fp@238: slave->sii_mailbox_protocols = fsm->sii_result & 0xFFFF; fp@238: fsm->slave_state = ec_fsm_slave_categories; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: return; fp@238: } fp@238: fp@238: fsm->slave_sii_num++; fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Categories. fp@238: */ fp@238: fp@238: void ec_fsm_slave_categories(ec_fsm_t *fsm) fp@238: { fp@238: fsm->slave_cat_offset = 0x0040; fp@238: fp@238: if (fsm->slave_cat_data) { fp@238: EC_INFO("FSM freeing old category data on slave %i...\n", fp@238: fsm->slave->ring_position); fp@238: kfree(fsm->slave_cat_data); fp@238: } fp@238: fp@238: if (!(fsm->slave_cat_data = (uint8_t *) kmalloc(EC_CAT_MEM, GFP_ATOMIC))) { fp@238: EC_ERR("FSM Failed to allocate category data.\n"); fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: return; fp@238: } fp@238: fp@238: // start reading first category header fp@238: fsm->sii_offset = fsm->slave_cat_offset; fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@238: fp@238: fsm->slave_state = ec_fsm_slave_category_header; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Read categories. fp@238: Start reading categories. fp@238: */ fp@238: fp@238: void ec_fsm_slave_category_header(ec_fsm_t *fsm) fp@238: { fp@238: // execute SII state machine fp@238: fsm->sii_state(fsm); fp@238: fp@238: if (fsm->sii_state == ec_fsm_sii_error) { fp@238: kfree(fsm->slave_cat_data); fp@238: fsm->slave_cat_data = NULL; fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: EC_ERR("FSM failed to read category header at 0x%04X on slave %i.\n", fp@238: fsm->slave_cat_offset, fsm->slave->ring_position); fp@238: return; fp@238: } fp@238: fp@238: if (fsm->sii_state != ec_fsm_sii_finished) return; fp@238: fp@238: // last category? fp@238: if ((fsm->sii_result & 0xFFFF) == 0xFFFF) { fp@238: kfree(fsm->slave_cat_data); fp@238: fsm->slave_cat_data = NULL; fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: return; fp@238: } fp@238: fp@238: fsm->slave_cat_type = fsm->sii_result & 0x7FFF; fp@238: fsm->slave_cat_words = (fsm->sii_result >> 16) & 0xFFFF; fp@238: fp@238: if (fsm->slave_cat_words > EC_CAT_MEM * 2) { fp@238: EC_ERR("FSM category memory too small! %i words needed.\n", fp@238: fsm->slave_cat_words); fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: return; fp@238: } fp@238: fp@238: // start reading category data fp@238: fsm->slave_cat_data_offset = 0; fp@238: fsm->sii_offset = (fsm->slave_cat_offset + 2 + fp@238: fsm->slave_cat_data_offset); fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@238: fsm->slave_state = ec_fsm_slave_category_data; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Category data. fp@238: Reads category data. fp@238: */ fp@238: fp@238: void ec_fsm_slave_category_data(ec_fsm_t *fsm) fp@238: { fp@238: // execute SII state machine fp@238: fsm->sii_state(fsm); fp@238: fp@238: if (fsm->sii_state == ec_fsm_sii_error) { fp@238: kfree(fsm->slave_cat_data); fp@238: fsm->slave_cat_data = NULL; fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: EC_ERR("FSM failed to read category 0x%02X data at 0x%04X" fp@238: " on slave %i.\n", fsm->slave_cat_type, fsm->sii_offset, fp@238: fsm->slave->ring_position); fp@238: return; fp@238: } fp@238: fp@238: if (fsm->sii_state != ec_fsm_sii_finished) return; fp@238: fp@238: fsm->slave_cat_data[fsm->slave_cat_data_offset * 2] = fp@238: fsm->sii_result & 0xFF; fp@238: fsm->slave_cat_data[fsm->slave_cat_data_offset * 2 + 1] = fp@238: (fsm->sii_result >> 8) & 0xFF; fp@238: fp@238: // read second word "on the fly" fp@238: if (fsm->slave_cat_data_offset + 1 < fsm->slave_cat_words) { fp@238: fsm->slave_cat_data_offset++; fp@238: fsm->slave_cat_data[fsm->slave_cat_data_offset * 2] = fp@238: (fsm->sii_result >> 16) & 0xFF; fp@238: fsm->slave_cat_data[fsm->slave_cat_data_offset * 2 + 1] = fp@238: (fsm->sii_result >> 24) & 0xFF; fp@238: } fp@238: fp@238: fsm->slave_cat_data_offset++; fp@238: fp@238: if (fsm->slave_cat_data_offset < fsm->slave_cat_words) { fp@238: fsm->sii_offset = (fsm->slave_cat_offset + 2 + fp@238: fsm->slave_cat_data_offset); fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@238: fsm->slave_state = ec_fsm_slave_category_data; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: return; fp@238: } fp@238: fp@238: // category data complete fp@238: switch (fsm->slave_cat_type) fp@238: { fp@238: case 0x000A: fp@238: if (ec_slave_fetch_strings(fsm->slave, fsm->slave_cat_data)) fp@238: goto out_free; fp@238: break; fp@238: case 0x001E: fp@238: if (ec_slave_fetch_general(fsm->slave, fsm->slave_cat_data)) fp@238: goto out_free; fp@238: break; fp@238: case 0x0028: fp@238: break; fp@238: case 0x0029: fp@238: if (ec_slave_fetch_sync(fsm->slave, fsm->slave_cat_data, fp@238: fsm->slave_cat_words)) fp@238: goto out_free; fp@238: break; fp@238: case 0x0032: fp@238: if (ec_slave_fetch_pdo(fsm->slave, fsm->slave_cat_data, fp@238: fsm->slave_cat_words, fp@238: EC_TX_PDO)) fp@238: goto out_free; fp@238: break; fp@238: case 0x0033: fp@238: if (ec_slave_fetch_pdo(fsm->slave, fsm->slave_cat_data, fp@238: fsm->slave_cat_words, fp@238: EC_RX_PDO)) fp@238: goto out_free; fp@238: break; fp@238: default: fp@238: EC_WARN("FSM: Unknown category type 0x%04X in slave %i.\n", fp@238: fsm->slave_cat_type, fsm->slave->ring_position); fp@238: } fp@238: fp@238: // start reading next category header fp@238: fsm->slave_cat_offset += 2 + fsm->slave_cat_words; fp@238: fsm->sii_offset = fsm->slave_cat_offset; fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@238: fsm->slave_state = ec_fsm_slave_category_header; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: return; fp@238: fp@238: out_free: fp@238: kfree(fsm->slave_cat_data); fp@238: fsm->slave_cat_data = NULL; fp@238: fsm->slave_state = ec_fsm_slave_finished; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave state: Finished. fp@238: End state of the slave state machine. fp@238: */ fp@238: fp@238: void ec_fsm_slave_finished(ec_fsm_t *fsm) fp@238: { fp@238: } fp@238: fp@238: /****************************************************************************** fp@238: * SII state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@238: Slave SII state: Start reading. fp@238: Starts reading the slave information interface. fp@238: */ fp@238: fp@238: void ec_fsm_sii_start_reading(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: fp@238: // initiate read operation fp@238: ec_command_npwr(command, fsm->slave->station_address, 0x502, 6); fp@238: EC_WRITE_U8 (command->data, 0x00); // read-only access fp@238: EC_WRITE_U8 (command->data + 1, 0x01); // request read operation fp@238: EC_WRITE_U32(command->data + 2, fsm->sii_offset); fp@238: ec_master_queue_command(fsm->master, command); fp@238: fsm->sii_state = ec_fsm_sii_check; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave SII state: Check. fp@238: Checks, if the SII-read-command has been sent and issues a fetch command. fp@238: */ fp@238: fp@238: void ec_fsm_sii_check(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: fp@238: if (command->state != EC_CMD_RECEIVED || command->working_counter != 1) { fp@238: EC_ERR("FSM SII: Reception of check command failed.\n"); fp@238: fsm->sii_state = ec_fsm_sii_error; fp@238: return; fp@238: } fp@238: fp@238: ec_command_nprd(command, fsm->slave->station_address, 0x502, 10); fp@238: ec_master_queue_command(fsm->master, command); fp@238: fsm->sii_state = ec_fsm_sii_fetch; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave SII state: Fetch. fp@238: Fetches the result of an SII-read command. fp@238: */ fp@238: fp@238: void ec_fsm_sii_fetch(ec_fsm_t *fsm) fp@238: { fp@238: ec_command_t *command = &fsm->command; fp@238: fp@238: if (command->state != EC_CMD_RECEIVED || command->working_counter != 1) { fp@238: EC_ERR("FSM SII: Reception of fetch command failed.\n"); fp@238: fsm->sii_state = ec_fsm_sii_error; fp@238: return; fp@238: } fp@238: fp@238: // check "busy bit" fp@238: if (likely((EC_READ_U8(command->data + 1) & 0x81) == 0)) { fp@238: fsm->sii_result = EC_READ_U32(command->data + 6); fp@238: fsm->sii_state = ec_fsm_sii_finished; fp@238: } fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave SII state: Finished. fp@238: End state of the slave SII state machine. fp@238: */ fp@238: fp@238: void ec_fsm_sii_finished(ec_fsm_t *fsm) fp@238: { fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@238: Slave SII state: Error. fp@238: End state of the slave SII state machine. fp@238: */ fp@238: fp@238: void ec_fsm_sii_error(ec_fsm_t *fsm) fp@238: { fp@238: } fp@238: fp@238: /*****************************************************************************/