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@64: fp@64: #include "master.h" fp@64: fp@64: /*****************************************************************************/ fp@64: fp@64: // Prototypen fp@64: fp@64: /*****************************************************************************/ fp@64: fp@73: /** fp@73: Schreibt ein CANopen-SDO (service data object). fp@73: */ fp@73: fp@73: int EtherCAT_rt_canopen_sdo_write( fp@73: ec_slave_t *slave, /**< EtherCAT-Slave */ fp@73: unsigned int sdo_index, /**< SDO-Index */ fp@73: unsigned char sdo_subindex, /**< SDO-Subindex */ fp@73: unsigned int value, /**< Neuer Wert */ fp@73: unsigned int size /**< Größe des Datenfeldes */ fp@73: ) fp@64: { fp@64: unsigned char data[0xF6]; fp@73: ec_frame_t frame; fp@64: unsigned int tries_left, i; fp@73: ec_master_t *master; 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@64: printk(KERN_ERR "EtherCAT: Illegal SDO data size: %i!\n", size); fp@64: return -1; fp@64: } fp@64: fp@64: data[0] = 0x0A; // Length of the Mailbox service data fp@64: data[1] = 0x00; fp@64: data[2] = slave->station_address & 0xFF; // Station address fp@64: data[3] = (slave->station_address >> 8) & 0xFF; fp@64: data[4] = 0x00; // Channel & priority fp@64: data[5] = 0x03; // CANopen over EtherCAT fp@64: data[6] = 0x00; // Number(7-0) fp@64: data[7] = 0x2 << 4; // Number(8) & Service = SDO Request (0x02) fp@64: data[8] = 0x01 // Size specified fp@64: | (0x1 << 1) // Transfer type = Expedited fp@64: | ((4 - size) << 2) // Data Set Size fp@64: | (0x1 << 5); // Command specifier = Initiate download request (0x01) fp@64: data[9] = sdo_index & 0xFF; fp@64: data[10] = (sdo_index >> 8) & 0xFF; fp@64: data[11] = sdo_subindex; fp@64: fp@64: for (i = 0; i < size; i++) { fp@64: data[12 + i] = value & 0xFF; fp@64: value >>= 8; fp@64: } fp@64: fp@73: ec_frame_init_npwr(&frame, master, slave->station_address, 0x1800, 0xF6, fp@73: data); fp@64: fp@73: if (unlikely(ec_frame_send_receive(&frame) < 0)) return -1; fp@64: fp@73: if (unlikely(frame.working_counter != 1)) { fp@64: printk(KERN_ERR "EtherCAT: Mailbox send - Slave %i did not respond!\n", fp@73: slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@64: // Read "written bit" of Sync-Manager fp@64: fp@64: tries_left = 10; fp@64: while (tries_left) fp@64: { fp@73: ec_frame_init_nprd(&frame, master, slave->station_address, 0x808, 8); fp@64: fp@73: if (unlikely(ec_frame_send_receive(&frame) < 0)) return -1; fp@64: fp@73: if (unlikely(frame.working_counter != 1)) { fp@64: printk(KERN_ERR "EtherCAT: Mailbox check - Slave %i did not" fp@73: " respond!\n", slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@73: if (frame.data[5] & 8) { // Written bit is high fp@64: break; fp@64: } fp@64: fp@64: udelay(1000); fp@64: tries_left--; fp@64: } fp@64: fp@64: if (!tries_left) { fp@64: printk(KERN_ERR "EtherCAT: Mailbox check - Slave %i timed out.\n", fp@73: slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@73: ec_frame_init_nprd(&frame, master, slave->station_address, 0x18F6, 0xF6); fp@64: fp@73: if (unlikely(ec_frame_send_receive(&frame) < 0)) return -1; fp@64: fp@73: if (unlikely(frame.working_counter != 1)) { fp@64: printk(KERN_ERR "EtherCAT: Mailbox receive - Slave %i did not" fp@73: " respond!\n", slave->ring_position); fp@64: return -1; fp@64: } fp@64: fp@73: if (frame.data[5] != 0x03 // COE fp@73: || (frame.data[7] >> 4) != 0x03 // SDO response fp@73: || (frame.data[8] >> 5) != 0x03 // Initiate download response fp@73: || (frame.data[9] != (sdo_index & 0xFF)) // Index fp@73: || (frame.data[10] != ((sdo_index >> 8) & 0xFF)) fp@73: || (frame.data[11] != sdo_subindex)) // Subindex fp@64: { fp@64: printk(KERN_ERR "EtherCAT: 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@64: EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_write); 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: */