fp@145: /****************************************************************************** fp@145: * fp@145: * $Id$ fp@145: * fp@1618: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1618: * fp@1618: * This file is part of the IgH EtherCAT Master. fp@1618: * fp@1618: * The IgH EtherCAT Master is free software; you can redistribute it fp@1618: * and/or modify it under the terms of the GNU General Public License fp@1619: * as published by the Free Software Foundation; either version 2 of the fp@1619: * License, or (at your option) any later version. fp@1618: * fp@1618: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1618: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1618: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1618: * GNU General Public License for more details. fp@1618: * fp@1618: * You should have received a copy of the GNU General Public License fp@1618: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1618: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1618: * fp@1619: * The right to use EtherCAT Technology is granted and comes free of fp@1619: * charge under condition of compatibility of product made by fp@1619: * Licensee. People intending to distribute/sell products based on the fp@1619: * code, have to sign an agreement to guarantee that products using fp@1619: * software based on IgH EtherCAT master stay compatible with the actual fp@1619: * EtherCAT specification (which are released themselves as an open fp@1619: * standard) as the (only) precondition to have the right to use EtherCAT fp@1619: * Technology, IP and trade marks. fp@1619: * fp@145: *****************************************************************************/ fp@145: fp@1618: /** fp@1618: \file fp@1618: Mailbox functionality. fp@1618: */ fp@1618: fp@1618: /*****************************************************************************/ fp@1618: fp@145: #include fp@145: #include fp@145: fp@145: #include "mailbox.h" fp@145: #include "command.h" fp@145: #include "master.h" fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Prepares a mailbox-send command. fp@195: \return pointer to mailbox command data fp@195: */ fp@195: fp@195: uint8_t *ec_slave_mbox_prepare_send(ec_slave_t *slave, /**< slave */ fp@195: uint8_t type, /**< mailbox protocol */ fp@195: size_t size /**< size of the data */ fp@145: ) fp@145: { fp@145: ec_command_t *command = &slave->mbox_command; fp@145: size_t total_size; fp@145: fp@145: if (unlikely(!slave->sii_mailbox_protocols)) { fp@145: EC_ERR("Slave %i does not support mailbox communication!\n", fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: total_size = size + 6; fp@145: if (unlikely(total_size > slave->sii_rx_mailbox_size)) { fp@145: EC_ERR("Data size does not fit in mailbox!\n"); fp@145: return NULL; fp@145: } fp@145: fp@145: if (ec_command_npwr(command, slave->station_address, fp@145: slave->sii_rx_mailbox_offset, fp@145: slave->sii_rx_mailbox_size)) fp@145: return NULL; fp@145: fp@195: EC_WRITE_U16(command->data, size); // mailbox service data length fp@195: EC_WRITE_U16(command->data + 2, slave->station_address); // station address fp@195: EC_WRITE_U8 (command->data + 4, 0x00); // hhannel & priority fp@195: EC_WRITE_U8 (command->data + 5, type); // underlying protocol type fp@145: fp@145: return command->data + 6; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Prepares a command for checking the mailbox state. fp@195: \return 0 in case of success, else < 0 fp@195: */ fp@195: fp@195: int ec_slave_mbox_prepare_check(ec_slave_t *slave /**< slave */) fp@195: { fp@195: ec_command_t *command = &slave->mbox_command; fp@195: fp@195: // FIXME: second sync manager? fp@145: if (ec_command_nprd(command, slave->station_address, 0x808, 8)) fp@145: return -1; fp@145: fp@145: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Processes a mailbox state checking command. fp@195: \return 0 in case of success, else < 0 fp@195: */ fp@195: fp@195: int ec_slave_mbox_check(const ec_slave_t *slave /**< slave */) fp@145: { fp@145: return EC_READ_U8(slave->mbox_command.data + 5) & 8 ? 1 : 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Prepares a command to fetch mailbox data. fp@195: \return 0 in case of success, else < 0 fp@195: */ fp@195: fp@195: int ec_slave_mbox_prepare_fetch(ec_slave_t *slave /**< slave */) fp@145: { fp@145: ec_command_t *command = &slave->mbox_command; fp@145: fp@145: if (ec_command_nprd(command, slave->station_address, fp@145: slave->sii_tx_mailbox_offset, fp@145: slave->sii_tx_mailbox_size)) return -1; fp@145: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Processes received mailbox data. fp@195: \return pointer to the received data fp@195: */ fp@195: fp@195: uint8_t *ec_slave_mbox_fetch(ec_slave_t *slave, /**< slave */ fp@195: uint8_t type, /**< expected mailbox protocol */ fp@195: size_t *size /**< size of the received data */ fp@145: ) fp@145: { fp@145: ec_command_t *command = &slave->mbox_command; fp@145: size_t data_size; fp@145: fp@145: if ((EC_READ_U8(command->data + 5) & 0x0F) != type) { fp@145: EC_ERR("Unexpected mailbox protocol 0x%02X (exp.: 0x%02X) at" fp@145: " slave %i!\n", EC_READ_U8(command->data + 5), type, fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: if ((data_size = EC_READ_U16(command->data)) > fp@145: slave->sii_tx_mailbox_size - 6) { fp@145: EC_ERR("Currupt mailbox response detected!\n"); fp@145: return NULL; fp@145: } fp@145: fp@145: *size = data_size; fp@145: return command->data + 6; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Sends a mailbox command and waits for its reception. fp@195: \return pointer to the received data fp@195: */ fp@195: fp@195: uint8_t *ec_slave_mbox_simple_io(ec_slave_t *slave, /**< slave */ fp@195: size_t *size /**< size of the received data */ fp@145: ) fp@145: { fp@145: uint8_t type; fp@145: ec_command_t *command; fp@145: fp@145: command = &slave->mbox_command; fp@145: type = EC_READ_U8(command->data + 5); fp@145: fp@145: if (unlikely(ec_master_simple_io(slave->master, command))) { fp@145: EC_ERR("Mailbox checking failed on slave %i!\n", fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: return ec_slave_mbox_simple_receive(slave, type, size); fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@195: Waits for the reception of a mailbox command. fp@195: \return pointer to the received data fp@195: */ fp@195: fp@195: uint8_t *ec_slave_mbox_simple_receive(ec_slave_t *slave, /**< slave */ fp@195: uint8_t type, /**< expected protocol */ fp@195: size_t *size /**< received data size */ fp@145: ) fp@145: { fp@145: cycles_t start, end, timeout; fp@145: ec_command_t *command; fp@145: fp@145: command = &slave->mbox_command; fp@145: start = get_cycles(); fp@145: timeout = (cycles_t) 100 * cpu_khz; // 100ms fp@145: fp@145: while (1) fp@145: { fp@145: if (ec_slave_mbox_prepare_check(slave)) return NULL; fp@145: if (unlikely(ec_master_simple_io(slave->master, command))) { fp@145: EC_ERR("Mailbox checking failed on slave %i!\n", fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: end = get_cycles(); fp@145: fp@145: if (ec_slave_mbox_check(slave)) fp@195: break; // proceed with receiving data fp@145: fp@145: if ((end - start) >= timeout) { fp@145: EC_ERR("Mailbox check - Slave %i timed out.\n", fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: udelay(100); fp@145: } fp@145: fp@145: if (ec_slave_mbox_prepare_fetch(slave)) return NULL; fp@145: if (unlikely(ec_master_simple_io(slave->master, command))) { fp@145: EC_ERR("Mailbox receiving failed on slave %i!\n", fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: if (unlikely(slave->master->debug_level) > 1) fp@145: EC_DBG("Mailbox receive took %ius.\n", ((u32) (end - start) * 1000 fp@145: / cpu_khz)); fp@145: fp@145: return ec_slave_mbox_fetch(slave, type, size); fp@145: } fp@145: fp@145: /*****************************************************************************/