fp@145: /****************************************************************************** fp@145: * fp@145: * $Id$ fp@145: * fp@197: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@197: * fp@197: * This file is part of the IgH EtherCAT Master. fp@197: * fp@197: * The IgH EtherCAT Master is free software; you can redistribute it fp@197: * 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@197: * fp@197: * The IgH EtherCAT Master is distributed in the hope that it will be fp@197: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@197: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@197: * GNU General Public License for more details. fp@197: * fp@197: * You should have received a copy of the GNU General Public License fp@197: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@197: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@197: * 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@145: *****************************************************************************/ fp@145: fp@199: /** fp@199: \file fp@199: Mailbox functionality. fp@199: */ fp@199: fp@199: /*****************************************************************************/ fp@199: fp@145: #include fp@145: #include fp@145: fp@145: #include "mailbox.h" fp@293: #include "datagram.h" fp@145: #include "master.h" fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@293: Prepares a mailbox-send datagram. fp@293: \return pointer to mailbox datagram data fp@195: */ fp@195: fp@279: uint8_t *ec_slave_mbox_prepare_send(const ec_slave_t *slave, /**< slave */ fp@293: ec_datagram_t *datagram, /**< datagram */ fp@195: uint8_t type, /**< mailbox protocol */ fp@195: size_t size /**< size of the data */ fp@145: ) fp@145: { 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@293: if (ec_datagram_npwr(datagram, slave->station_address, fp@293: slave->sii_rx_mailbox_offset, fp@293: slave->sii_rx_mailbox_size)) fp@293: return NULL; fp@293: fp@293: EC_WRITE_U16(datagram->data, size); // mailbox service data length fp@293: EC_WRITE_U16(datagram->data + 2, slave->station_address); // station addr. fp@293: EC_WRITE_U8 (datagram->data + 4, 0x00); // channel & priority fp@293: EC_WRITE_U8 (datagram->data + 5, type); // underlying protocol type fp@293: fp@293: return datagram->data + 6; fp@293: } fp@293: fp@293: /*****************************************************************************/ fp@293: fp@293: /** fp@293: Prepares a datagram for checking the mailbox state. fp@195: \return 0 in case of success, else < 0 fp@195: */ fp@195: fp@279: int ec_slave_mbox_prepare_check(const ec_slave_t *slave, /**< slave */ fp@293: ec_datagram_t *datagram /**< datagram */ fp@279: ) fp@279: { fp@195: // FIXME: second sync manager? fp@293: if (ec_datagram_nprd(datagram, 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@293: Processes a mailbox state checking datagram. fp@195: \return 0 in case of success, else < 0 fp@195: */ fp@195: fp@293: int ec_slave_mbox_check(const ec_datagram_t *datagram /**< datagram */) fp@293: { fp@293: return EC_READ_U8(datagram->data + 5) & 8 ? 1 : 0; fp@293: } fp@293: fp@293: /*****************************************************************************/ fp@293: fp@293: /** fp@293: Prepares a datagram to fetch mailbox data. fp@195: \return 0 in case of success, else < 0 fp@195: */ fp@195: fp@279: int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, /**< slave */ fp@293: ec_datagram_t *datagram /**< datagram */ fp@279: ) fp@279: { fp@293: if (ec_datagram_nprd(datagram, slave->station_address, fp@293: slave->sii_tx_mailbox_offset, fp@293: slave->sii_tx_mailbox_size)) return -1; fp@145: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@1731: Mailbox error codes. fp@1731: */ fp@1731: fp@1731: const ec_code_msg_t mbox_error_messages[] = { fp@1731: {0x00000001, "MBXERR_SYNTAX"}, fp@1731: {0x00000002, "MBXERR_UNSUPPORTEDPROTOCOL"}, fp@1731: {0x00000003, "MBXERR_INVAILDCHANNEL"}, fp@1731: {0x00000004, "MBXERR_SERVICENOTSUPPORTED"}, fp@1731: {0x00000005, "MBXERR_INVALIDHEADER"}, fp@1731: {0x00000006, "MBXERR_SIZETOOSHORT"}, fp@1731: {0x00000007, "MBXERR_NOMOREMEMORY"}, fp@1731: {0x00000008, "MBXERR_INVALIDSIZE"}, fp@1731: {} fp@1731: }; fp@1731: fp@1731: /*****************************************************************************/ fp@1731: fp@1731: /** fp@195: Processes received mailbox data. fp@195: \return pointer to the received data fp@195: */ fp@195: fp@279: uint8_t *ec_slave_mbox_fetch(const ec_slave_t *slave, /**< slave */ fp@293: ec_datagram_t *datagram, /**< datagram */ fp@1731: uint8_t *type, /**< expected mailbox protocol */ fp@195: size_t *size /**< size of the received data */ fp@145: ) fp@145: { fp@145: size_t data_size; fp@145: fp@1732: data_size = EC_READ_U16(datagram->data); fp@1732: fp@1732: if (data_size > slave->sii_tx_mailbox_size - 6) { fp@1731: EC_ERR("Corrupt mailbox response detected!\n"); fp@1732: ec_print_data(datagram->data, slave->sii_tx_mailbox_size); fp@293: return NULL; fp@293: } fp@293: fp@1731: *type = EC_READ_U8(datagram->data + 5) & 0x0F; fp@1731: *size = data_size; fp@1731: fp@1731: if (*type == 0x00) { fp@1731: const ec_code_msg_t *mbox_msg; fp@1731: uint16_t code = EC_READ_U16(datagram->data + 8); fp@1731: fp@1732: EC_ERR("Mailbox error response received - "); fp@1732: fp@1731: for (mbox_msg = mbox_error_messages; mbox_msg->code; mbox_msg++) { fp@1731: if (mbox_msg->code != code) continue; fp@1732: printk("Code 0x%04X: \"%s\".\n", fp@1731: mbox_msg->code, mbox_msg->message); fp@1731: break; fp@1731: } fp@1731: fp@1731: if (!mbox_msg->code) fp@1732: printk("Unknown error reply code 0x%04X.\n", code); fp@1732: fp@1732: if (slave->master->debug_level) fp@1732: ec_print_data(datagram->data + 6, data_size); fp@1731: fp@145: return NULL; fp@145: } fp@145: fp@293: return datagram->data + 6; fp@293: } fp@293: fp@293: /*****************************************************************************/