fp@64: /****************************************************************************** fp@64: * fp@64: * c a n o p e n . c fp@64: * fp@64: * CANopen over EtherCAT fp@64: * fp@64: * $Id$ fp@64: * fp@64: *****************************************************************************/ fp@64: fp@64: #include fp@80: #include fp@80: #include fp@64: fp@77: #include "../include/EtherCAT_si.h" fp@64: #include "master.h" fp@64: fp@64: /*****************************************************************************/ fp@64: fp@73: /** fp@73: Schreibt ein CANopen-SDO (service data object). fp@73: */ fp@73: fp@77: int EtherCAT_rt_canopen_sdo_write(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@77: uint16_t sdo_index, /**< SDO-Index */ fp@77: uint8_t sdo_subindex, /**< SDO-Subindex */ fp@77: uint32_t value, /**< Neuer Wert */ fp@77: size_t size /**< Größe des Datenfeldes */ fp@77: ) fp@64: { fp@98: uint8_t data[0xF6]; fp@98: ec_command_t command; fp@98: unsigned int i; fp@73: ec_master_t *master; fp@98: cycles_t start, end, timeout; fp@64: fp@73: memset(data, 0x00, 0xF6); fp@73: fp@73: master = slave->master; fp@64: fp@64: if (size == 0 || size > 4) { fp@84: EC_ERR("Illegal SDO data size: %i!\n", size); fp@64: return -1; fp@64: } fp@64: fp@77: EC_WRITE_U16(data, 0x000A); // Length of the Mailbox service data fp@77: EC_WRITE_U16(data + 2, slave->station_address); // Station address fp@77: EC_WRITE_U8 (data + 4, 0x00); // Channel & priority fp@77: EC_WRITE_U8 (data + 5, 0x03); // CANopen over EtherCAT fp@80: EC_WRITE_U16(data + 6, 0x2000); // Number (0), Service (SDO request) fp@77: EC_WRITE_U8 (data + 8, 0x13 | ((4 - size) << 2)); // Spec., exp., init. fp@77: EC_WRITE_U16(data + 9, sdo_index); fp@77: EC_WRITE_U8 (data + 11, sdo_subindex); fp@64: fp@64: for (i = 0; i < size; i++) { fp@77: EC_WRITE_U8(data + 12 + i, value & 0xFF); fp@64: value >>= 8; fp@64: } fp@64: fp@98: ec_command_init_npwr(&command, slave->station_address, 0x1800, 0xF6, data); fp@98: if (unlikely(ec_master_simple_io(master, &command))) { fp@89: EC_ERR("Mailbox sending failed on slave %i!\n", slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@64: // Read "written bit" of Sync-Manager fp@98: start = get_cycles(); fp@98: timeout = cpu_khz; // 1ms fp@98: fp@98: do fp@98: { fp@98: ec_command_init_nprd(&command, slave->station_address, 0x808, 8); fp@98: if (unlikely(ec_master_simple_io(master, &command))) { fp@89: EC_ERR("Mailbox checking failed on slave %i!\n", fp@84: slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@98: end = get_cycles(); fp@98: fp@98: if (EC_READ_U8(command.data + 5) & 8) break; // Written bit is high fp@98: } fp@98: while ((end - start) < timeout); fp@98: fp@98: if ((end - start) >= timeout) { fp@84: EC_ERR("Mailbox check - Slave %i timed out.\n", slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@98: ec_command_init_nprd(&command, slave->station_address, 0x18F6, 0xF6); fp@98: if (unlikely(ec_master_simple_io(master, &command))) { fp@89: EC_ERR("Mailbox receiving failed on slave %i!\n", fp@84: slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@98: if (EC_READ_U8 (command.data + 5) != 0x03 || // COE fp@98: EC_READ_U16(command.data + 6) != 0x3000 || // SDO response fp@98: EC_READ_U8 (command.data + 8) >> 5 != 0x03 || // Download response fp@98: EC_READ_U16(command.data + 9) != sdo_index || // Index fp@98: EC_READ_U8 (command.data + 11) != sdo_subindex) // Subindex fp@64: { fp@84: EC_ERR("Illegal mailbox response at slave %i!\n", fp@73: slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@64: return 0; fp@64: } fp@64: fp@64: /*****************************************************************************/ fp@64: fp@80: /** fp@91: Liest ein CANopen-SDO (service data object). fp@80: */ fp@80: fp@80: int EtherCAT_rt_canopen_sdo_read(ec_slave_t *slave, /**< EtherCAT-Slave */ fp@80: uint16_t sdo_index, /**< SDO-Index */ fp@80: uint8_t sdo_subindex, /**< SDO-Subindex */ fp@80: uint32_t *value /**< Speicher für gel. Wert */ fp@80: ) fp@80: { fp@80: unsigned char data[0xF6]; fp@98: ec_command_t command; fp@80: ec_master_t *master; fp@98: cycles_t start, end, timeout; fp@80: fp@80: memset(data, 0x00, 0xF6); fp@80: master = slave->master; fp@80: fp@80: EC_WRITE_U16(data, 0x0006); // Length of the Mailbox service data fp@80: EC_WRITE_U16(data + 2, slave->station_address); // Station address fp@80: EC_WRITE_U8 (data + 4, 0x00); // Channel & priority fp@80: EC_WRITE_U8 (data + 5, 0x03); // CANopen over EtherCAT fp@80: EC_WRITE_U16(data + 6, 0x2000); // Number (0), Service (SDO request) fp@80: EC_WRITE_U8 (data + 8, 0x1 << 1 | 0x2 << 5); // Exp., Upload request fp@80: EC_WRITE_U16(data + 9, sdo_index); fp@80: EC_WRITE_U8 (data + 11, sdo_subindex); fp@80: fp@98: ec_command_init_npwr(&command, slave->station_address, 0x1800, 0xF6, data); fp@98: if (unlikely(ec_master_simple_io(master, &command))) { fp@89: EC_ERR("Mailbox sending failed on slave %i!\n", slave->ring_position); fp@80: return -1; fp@80: } fp@80: fp@80: // Read "written bit" of Sync-Manager fp@80: fp@98: start = get_cycles(); fp@98: timeout = cpu_khz; // 1ms fp@98: fp@98: do fp@98: { fp@98: ec_command_init_nprd(&command, slave->station_address, 0x808, 8); fp@98: if (unlikely(ec_master_simple_io(master, &command))) { fp@89: EC_ERR("Mailbox checking failed on slave %i!\n", fp@84: slave->ring_position); fp@80: return -1; fp@80: } fp@80: fp@98: end = get_cycles(); fp@98: fp@98: if (EC_READ_U8(command.data + 5) & 8) { // Written bit is high fp@80: break; fp@80: } fp@98: } fp@98: while (likely((end - start) < timeout)); fp@98: fp@98: if (unlikely((end - start) >= timeout)) { fp@84: EC_ERR("Mailbox check - Slave %i timed out.\n", slave->ring_position); fp@80: return -1; fp@80: } fp@80: fp@98: ec_command_init_nprd(&command, slave->station_address, 0x18F6, 0xF6); fp@98: if (unlikely(ec_master_simple_io(master, &command))) { fp@89: EC_ERR("Mailbox receiving failed on slave %i!\n", fp@84: slave->ring_position); fp@80: return -1; fp@80: } fp@80: fp@98: if (EC_READ_U8 (command.data + 5) != 0x03 || // COE fp@98: EC_READ_U16(command.data + 6) != 0x3000 || // SDO response fp@98: EC_READ_U8 (command.data + 8) >> 5 != 0x02 || // Upload response fp@98: EC_READ_U16(command.data + 9) != sdo_index || // Index fp@98: EC_READ_U8 (command.data + 11) != sdo_subindex) // Subindex fp@80: { fp@84: EC_ERR("Illegal mailbox response at slave %i!\n", fp@80: slave->ring_position); fp@80: return -1; fp@80: } fp@80: fp@98: *value = EC_READ_U32(command.data + 12); fp@80: fp@80: return 0; fp@80: } fp@80: fp@80: /*****************************************************************************/ fp@80: fp@91: /** fp@91: Schweibt ein CANopen-SDO (Variante mit Angabe des Masters und der Adresse). fp@91: fp@91: Siehe EtherCAT_rt_canopen_sdo_write() fp@91: fp@91: \return 0 wenn alles ok, < 0 bei Fehler fp@91: */ fp@91: fp@80: int EtherCAT_rt_canopen_sdo_addr_write(ec_master_t *master, fp@80: /**< EtherCAT-Master */ fp@80: const char *addr, fp@98: /**< Addresse, siehe fp@98: ec_master_slave_address() */ fp@80: uint16_t index, fp@80: /**< SDO-Index */ fp@80: uint8_t subindex, fp@80: /**< SDO-Subindex */ fp@80: uint32_t value, fp@80: /**< Neuer Wert */ fp@80: size_t size fp@80: /**< Größe des Datenfeldes */ fp@80: ) fp@80: { fp@80: ec_slave_t *slave; fp@98: if (!(slave = ec_master_slave_address(master, addr))) return -1; fp@80: return EtherCAT_rt_canopen_sdo_write(slave, index, subindex, value, size); fp@80: } fp@80: fp@80: /*****************************************************************************/ fp@80: fp@91: /** fp@91: Liest ein CANopen-SDO (Variante mit Angabe des Masters und der Adresse). fp@91: fp@91: Siehe EtherCAT_rt_canopen_sdo_read() fp@91: fp@91: \return 0 wenn alles ok, < 0 bei Fehler fp@91: */ fp@91: fp@80: int EtherCAT_rt_canopen_sdo_addr_read(ec_master_t *master, fp@80: /**< EtherCAT-Slave */ fp@80: const char *addr, fp@98: /**< Addresse, siehe fp@98: ec_master_slave_address() */ fp@80: uint16_t index, fp@80: /**< SDO-Index */ fp@80: uint8_t subindex, fp@80: /**< SDO-Subindex */ fp@80: uint32_t *value fp@80: /**< Speicher für gel. Wert */ fp@80: ) fp@80: { fp@80: ec_slave_t *slave; fp@98: if (!(slave = ec_master_slave_address(master, addr))) return -1; fp@80: return EtherCAT_rt_canopen_sdo_read(slave, index, subindex, value); fp@80: } fp@80: fp@80: /*****************************************************************************/ fp@80: fp@64: EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_write); fp@80: EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_read); fp@80: EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_addr_write); fp@80: EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_addr_read); fp@64: fp@64: /*****************************************************************************/ fp@64: fp@64: /* Emacs-Konfiguration fp@64: ;;; Local Variables: *** fp@64: ;;; c-basic-offset:4 *** fp@64: ;;; End: *** fp@64: */