fp@145: /****************************************************************************** fp@145: * fp@145: * m a i l b o x . c fp@145: * fp@145: * Mailbox-Funktionen fp@145: * fp@145: * $Id$ fp@145: * fp@145: *****************************************************************************/ fp@145: 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@145: Bereitet ein Mailbox-Send-Kommando vor. fp@145: */ fp@145: fp@145: uint8_t *ec_slave_mbox_prepare_send(ec_slave_t *slave, /**< Slave */ fp@145: uint8_t type, /**< Mailbox-Protokoll */ fp@145: size_t size /**< Datengröße */ 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@145: EC_WRITE_U16(command->data, size); // Mailbox service data length fp@145: EC_WRITE_U16(command->data + 2, slave->station_address); // Station address fp@145: EC_WRITE_U8 (command->data + 4, 0x00); // Channel & priority fp@145: 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@145: Bereitet ein Kommando zum Abfragen des Mailbox-Zustandes vor. fp@145: */ fp@145: fp@145: int ec_slave_mbox_prepare_check(ec_slave_t *slave /**< Slave */) fp@145: { fp@145: ec_command_t *command = &slave->mbox_command; fp@145: fp@145: // FIXME: Zweiter Sync-Manager nicht immer TX-Mailbox? 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@145: Liest den Mailbox-Zustand aus einem empfangenen Kommando. fp@145: */ fp@145: fp@145: 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@145: Bereitet ein Kommando zum Laden von Daten von der Mailbox vor. fp@145: */ fp@145: fp@145: 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@145: Verarbeitet empfangene Mailbox-Daten. fp@145: */ fp@145: fp@145: uint8_t *ec_slave_mbox_fetch(ec_slave_t *slave, /**< Slave */ fp@145: uint8_t type, /**< Protokoll */ fp@145: size_t *size /**< Größe der empfangenen fp@145: Daten */ 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@145: Sendet und wartet auf den Empfang eines Mailbox-Kommandos. fp@145: */ fp@145: fp@145: uint8_t *ec_slave_mbox_simple_io(ec_slave_t *slave, /**< Slave */ fp@145: size_t *size /**< Größe der gelesenen fp@145: Daten */ 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@145: Wartet auf den Empfang eines Mailbox-Kommandos. fp@145: */ fp@145: fp@145: uint8_t *ec_slave_mbox_simple_receive(ec_slave_t *slave, /**< Slave */ fp@145: uint8_t type, /**< Protokoll */ fp@145: size_t *size /**< Größe der gelesenen fp@145: Daten */ 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@145: 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: /*****************************************************************************/ fp@145: fp@145: #if 0 fp@145: /** fp@145: Sendet ein Mailbox-Kommando. fp@145: */ fp@145: fp@145: int ec_slave_mbox_send(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@145: uint8_t type, /**< Unterliegendes Protokoll */ fp@145: const uint8_t *prot_data, /**< Protokoll-Daten */ fp@145: size_t size /**< Datengröße */ fp@145: ) fp@145: { fp@145: uint8_t *data; fp@145: ec_command_t command; fp@145: fp@145: fp@145: } fp@145: fp@145: if (!(data = kmalloc(slave->sii_rx_mailbox_size, GFP_KERNEL))) { fp@145: EC_ERR("Failed to allocate %i bytes of memory for mailbox data!\n", fp@145: slave->sii_rx_mailbox_size); fp@145: return -1; fp@145: } fp@145: fp@145: memset(data, 0x00, slave->sii_rx_mailbox_size); fp@145: EC_WRITE_U16(data, size); // Length of the Mailbox service data fp@145: EC_WRITE_U16(data + 2, slave->station_address); // Station address fp@145: EC_WRITE_U8 (data + 4, 0x00); // Channel & priority fp@145: EC_WRITE_U8 (data + 5, type); // Underlying protocol type fp@145: memcpy(data + 6, prot_data, size); fp@145: fp@145: ec_command_init_npwr(&command, slave->station_address, fp@145: slave->sii_rx_mailbox_offset, fp@145: slave->sii_rx_mailbox_size, data); fp@145: if (unlikely(ec_master_simple_io(slave->master, &command))) { fp@145: EC_ERR("Mailbox sending failed on slave %i!\n", slave->ring_position); fp@145: kfree(data); fp@145: return -1; fp@145: } fp@145: fp@145: kfree(data); fp@145: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: /** fp@145: Sendet ein Mailbox-Kommando. fp@145: */ fp@145: fp@145: int ec_slave_mailbox_receive(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@145: uint8_t type, /**< Unterliegendes Protokoll */ fp@145: uint8_t *prot_data, /**< Protokoll-Daten */ fp@145: size_t *size /**< Datengröße des Puffers, später fp@145: Größe der gelesenen Daten */ fp@145: ) fp@145: { fp@145: ec_command_t command; fp@145: size_t data_size; fp@145: cycles_t start, end, timeout; fp@145: fp@145: // Read "written bit" of Sync-Manager fp@145: start = get_cycles(); fp@145: timeout = (cycles_t) 100 * cpu_khz; // 100ms fp@145: fp@145: while (1) fp@145: { fp@145: // FIXME: Zweiter Sync-Manager nicht immer TX-Mailbox? fp@145: ec_command_init_nprd(&command, slave->station_address, 0x808, 8); 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 -1; fp@145: } fp@145: fp@145: end = get_cycles(); fp@145: fp@145: if (EC_READ_U8(command.data + 5) & 8) fp@145: break; // Proceed with received 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 -1; fp@145: } fp@145: fp@145: udelay(100); fp@145: } fp@145: fp@145: ec_command_init_nprd(&command, slave->station_address, fp@145: slave->sii_tx_mailbox_offset, fp@145: slave->sii_tx_mailbox_size); 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 -1; fp@145: } 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 -1; 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: if ((data_size = EC_READ_U16(command.data)) > *size) { fp@145: EC_ERR("Mailbox service data does not fit into buffer (%i > %i).\n", fp@145: data_size, *size); fp@145: return -1; fp@145: } fp@145: fp@145: if (data_size > slave->sii_tx_mailbox_size - 6) { fp@145: EC_ERR("Currupt mailbox response detected!\n"); fp@145: return -1; fp@145: } fp@145: fp@145: memcpy(prot_data, command.data + 6, data_size); fp@145: *size = data_size; fp@145: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@145: uint8_t *ec_slave_init_mbox_send_cmd(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@145: ec_command_t *command, /**< Kommando */ fp@145: uint8_t type, /**< Protokolltyp */ fp@145: size_t size /**< Datengröße */ fp@145: ) fp@145: { fp@145: size_t total_size; fp@145: uint8_t *data; 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 into mailbox of slave %i!\n", fp@145: slave->ring_position); fp@145: return NULL; fp@145: } fp@145: fp@145: data = command->data; fp@145: fp@145: memset(data, 0x00, slave->sii_rx_mailbox_size); fp@145: EC_WRITE_U16(data, size); // Length of the Mailbox service data fp@145: EC_WRITE_U16(data + 2, slave->station_address); // Station address fp@145: EC_WRITE_U8 (data + 4, 0x00); // Channel & priority fp@145: EC_WRITE_U8 (data + 5, type); // Underlying protocol type fp@145: fp@145: return data + 6; fp@145: } fp@145: #endif fp@145: fp@145: /*****************************************************************************/