fp@145: /****************************************************************************** fp@145: * fp@145: * $Id$ fp@145: * fp@1326: * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH fp@197: * fp@197: * This file is part of the IgH EtherCAT Master. fp@197: * fp@1326: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1326: * modify it under the terms of the GNU General Public License version 2, as fp@1326: * published by the Free Software Foundation. fp@1326: * fp@1326: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1326: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1326: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1326: * Public License for more details. fp@1326: * fp@1326: * You should have received a copy of the GNU General Public License along fp@1326: * 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@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. 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@1313: \return Pointer to mailbox datagram data, or ERR_PTR() code. 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@1313: int ret; fp@145: fp@834: if (unlikely(!slave->sii.mailbox_protocols)) { fp@1921: EC_SLAVE_ERR(slave, "Slave does not support mailbox" fp@1921: " communication!\n"); fp@1313: return ERR_PTR(-EPROTONOSUPPORT); fp@145: } fp@145: fp@1217: total_size = EC_MBOX_HEADER_SIZE + size; fp@1337: fp@1338: if (unlikely(total_size > slave->configured_rx_mailbox_size)) { fp@1921: EC_SLAVE_ERR(slave, "Data size (%zu) does not fit in mailbox (%u)!\n", fp@1338: total_size, slave->configured_rx_mailbox_size); fp@1313: return ERR_PTR(-EOVERFLOW); fp@1313: } fp@1313: fp@1313: ret = ec_datagram_fpwr(datagram, slave->station_address, fp@1338: slave->configured_rx_mailbox_offset, fp@1338: slave->configured_rx_mailbox_size); fp@1313: if (ret) fp@1313: return ERR_PTR(ret); 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@1217: return datagram->data + EC_MBOX_HEADER_SIZE; fp@293: } fp@293: fp@293: /*****************************************************************************/ fp@293: fp@293: /** fp@293: Prepares a datagram for checking the mailbox state. fp@786: \todo Determine sync manager used for receive mailbox 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@1313: int ret = ec_datagram_fprd(datagram, slave->station_address, 0x808, 8); fp@1313: if (ret) fp@1313: return ret; fp@145: fp@1225: ec_datagram_zero(datagram); 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@1338: int ret = ec_datagram_fprd(datagram, slave->station_address, fp@1338: slave->configured_tx_mailbox_offset, fp@1338: slave->configured_tx_mailbox_size); fp@1313: if (ret) fp@1313: return ret; fp@1225: fp@1225: ec_datagram_zero(datagram); fp@145: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@390: Mailbox error codes. fp@390: */ fp@390: fp@390: const ec_code_msg_t mbox_error_messages[] = { fp@390: {0x00000001, "MBXERR_SYNTAX"}, fp@390: {0x00000002, "MBXERR_UNSUPPORTEDPROTOCOL"}, fp@390: {0x00000003, "MBXERR_INVAILDCHANNEL"}, fp@390: {0x00000004, "MBXERR_SERVICENOTSUPPORTED"}, fp@390: {0x00000005, "MBXERR_INVALIDHEADER"}, fp@390: {0x00000006, "MBXERR_SIZETOOSHORT"}, fp@390: {0x00000007, "MBXERR_NOMOREMEMORY"}, fp@390: {0x00000008, "MBXERR_INVALIDSIZE"}, fp@390: {} fp@390: }; fp@390: fp@390: /*****************************************************************************/ fp@390: fp@1313: /** Processes received mailbox data. fp@1313: * fp@1313: * \return Pointer to the received data, or ERR_PTR() code. fp@1313: */ fp@279: uint8_t *ec_slave_mbox_fetch(const ec_slave_t *slave, /**< slave */ fp@2488: const ec_datagram_t *datagram, /**< datagram */ fp@390: 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@431: data_size = EC_READ_U16(datagram->data); fp@431: fp@1338: if (data_size + EC_MBOX_HEADER_SIZE > slave->configured_tx_mailbox_size) { fp@1921: EC_SLAVE_ERR(slave, "Corrupt mailbox response received!\n"); fp@1338: ec_print_data(datagram->data, slave->configured_tx_mailbox_size); fp@1313: return ERR_PTR(-EPROTO); fp@293: } fp@293: fp@390: *type = EC_READ_U8(datagram->data + 5) & 0x0F; fp@390: *size = data_size; fp@390: fp@390: if (*type == 0x00) { fp@390: const ec_code_msg_t *mbox_msg; fp@1217: uint16_t code = EC_READ_U16(datagram->data + 8); fp@390: fp@1921: EC_SLAVE_ERR(slave, "Mailbox error response received - "); fp@1217: fp@1217: for (mbox_msg = mbox_error_messages; mbox_msg->code; mbox_msg++) { fp@1337: if (mbox_msg->code != code) fp@1337: continue; fp@431: printk("Code 0x%04X: \"%s\".\n", fp@1337: mbox_msg->code, mbox_msg->message); fp@390: break; fp@390: } fp@390: fp@390: if (!mbox_msg->code) fp@431: printk("Unknown error reply code 0x%04X.\n", code); fp@431: fp@431: if (slave->master->debug_level) fp@1217: ec_print_data(datagram->data + EC_MBOX_HEADER_SIZE, data_size); fp@1217: fp@1313: return ERR_PTR(-EPROTO); fp@1217: } fp@1217: fp@1217: return datagram->data + EC_MBOX_HEADER_SIZE; fp@1217: } fp@1217: fp@1217: /*****************************************************************************/