17 * GNU Lesser General Public License for more details. |
17 * GNU Lesser General Public License for more details. |
18 * |
18 * |
19 * You should have received a copy of the GNU Lesser General Public License |
19 * You should have received a copy of the GNU Lesser General Public License |
20 * along with the IgH EtherCAT master userspace library. If not, see |
20 * along with the IgH EtherCAT master userspace library. If not, see |
21 * <http://www.gnu.org/licenses/>. |
21 * <http://www.gnu.org/licenses/>. |
22 * |
22 * |
23 * --- |
23 * --- |
24 * |
24 * |
25 * The license mentioned above concerns the source code only. Using the |
25 * The license mentioned above concerns the source code only. Using the |
26 * EtherCAT technology and brand is only permitted in compliance with the |
26 * EtherCAT technology and brand is only permitted in compliance with the |
27 * industrial property and similar rights of Beckhoff Automation GmbH. |
27 * industrial property and similar rights of Beckhoff Automation GmbH. |
28 * |
28 * |
29 *****************************************************************************/ |
29 *****************************************************************************/ |
45 * ecrt_slave_config_dc() to configure a slave for cyclic operation, and |
45 * ecrt_slave_config_dc() to configure a slave for cyclic operation, and |
46 * ecrt_master_application_time(), ecrt_master_sync_reference_clock() and |
46 * ecrt_master_application_time(), ecrt_master_sync_reference_clock() and |
47 * ecrt_master_sync_slave_clocks() for offset and drift compensation. The |
47 * ecrt_master_sync_slave_clocks() for offset and drift compensation. The |
48 * EC_TIMEVAL2NANO() macro can be used for epoch time conversion, while the |
48 * EC_TIMEVAL2NANO() macro can be used for epoch time conversion, while the |
49 * ecrt_master_sync_monitor_queue() and ecrt_master_sync_monitor_process() |
49 * ecrt_master_sync_monitor_queue() and ecrt_master_sync_monitor_process() |
50 * methods can be used to monitor the synchrony. |
50 * methods can be used to monitor the synchrony. |
51 * - Improved the callback mechanism. ecrt_master_callbacks() now takes two |
51 * - Improved the callback mechanism. ecrt_master_callbacks() now takes two |
52 * callback functions for sending and receiving datagrams. |
52 * callback functions for locking and unlocking the fsm datagram queue. |
53 * ecrt_master_send_ext() is used to execute the sending of non-application |
|
54 * datagrams. |
|
55 * - Added watchdog configuration (method ecrt_slave_config_watchdog(), |
53 * - Added watchdog configuration (method ecrt_slave_config_watchdog(), |
56 * #ec_watchdog_mode_t, \a watchdog_mode parameter in ec_sync_info_t and |
54 * #ec_watchdog_mode_t, \a watchdog_mode parameter in ec_sync_info_t and |
57 * ecrt_slave_config_sync_manager()). |
55 * ecrt_slave_config_sync_manager()). |
58 * - Added ecrt_slave_config_complete_sdo() method to download an SDO during |
56 * - Added ecrt_slave_config_complete_sdo() method to download an SDO during |
59 * configuration via CompleteAccess. |
57 * configuration via CompleteAccess. |
77 * - Added 64-bit data access macros. |
75 * - Added 64-bit data access macros. |
78 * - Added ecrt_slave_config_idn() method for storing SoE IDN configurations, |
76 * - Added ecrt_slave_config_idn() method for storing SoE IDN configurations, |
79 * and ecrt_master_read_idn() and ecrt_master_write_idn() to read/write IDNs |
77 * and ecrt_master_read_idn() and ecrt_master_write_idn() to read/write IDNs |
80 * ad-hoc via the user-space library. |
78 * ad-hoc via the user-space library. |
81 * - Added ecrt_master_reset() to initiate retrying to configure slaves. |
79 * - Added ecrt_master_reset() to initiate retrying to configure slaves. |
|
80 * - Added support for overlapping PDOs which allows inputs to use the same |
|
81 * space as outputs on the frame. This reduces the frame length. |
|
82 * |
82 * |
83 * |
83 * @{ |
84 * @{ |
84 */ |
85 */ |
85 |
86 |
86 /*****************************************************************************/ |
87 /*****************************************************************************/ |
219 * \see ecrt_master(). |
223 * \see ecrt_master(). |
220 */ |
224 */ |
221 typedef struct { |
225 typedef struct { |
222 unsigned int slave_count; /**< Number of slaves in the bus. */ |
226 unsigned int slave_count; /**< Number of slaves in the bus. */ |
223 unsigned int link_up : 1; /**< \a true, if the network link is up. */ |
227 unsigned int link_up : 1; /**< \a true, if the network link is up. */ |
224 uint8_t scan_busy; /**< \a true, while the master is scanning the bus */ |
228 uint8_t scan_busy; /**< \a true, while the master is scanning the bus */ |
225 uint64_t app_time; /**< Application time. */ |
229 uint64_t app_time; /**< Application time. */ |
226 } ec_master_info_t; |
230 } ec_master_info_t; |
|
231 |
|
232 /*****************************************************************************/ |
|
233 |
|
234 /** EtherCAT slave port descriptor. |
|
235 */ |
|
236 typedef enum { |
|
237 EC_PORT_NOT_IMPLEMENTED, /**< Port is not implemented. */ |
|
238 EC_PORT_NOT_CONFIGURED, /**< Port is not configured. */ |
|
239 EC_PORT_EBUS, /**< Port is an e-bus. */ |
|
240 EC_PORT_MII /**< Port is a mii. */ |
|
241 } ec_slave_port_desc_t; |
|
242 |
|
243 /*****************************************************************************/ |
|
244 |
|
245 /** EtherCAT slave port information. |
|
246 */ |
|
247 typedef struct { |
|
248 uint8_t link_up; /**< Link detected. */ |
|
249 uint8_t loop_closed; /**< Loop closed. */ |
|
250 uint8_t signal_detected; /**< Detected signal on RX port. */ |
|
251 } ec_slave_port_link_t; |
227 |
252 |
228 /*****************************************************************************/ |
253 /*****************************************************************************/ |
229 |
254 |
230 /** Slave information. |
255 /** Slave information. |
231 * |
256 * |
239 uint32_t product_code; /**< Product-Code stored on the slave. */ |
264 uint32_t product_code; /**< Product-Code stored on the slave. */ |
240 uint32_t revision_number; /**< Revision-Number stored on the slave. */ |
265 uint32_t revision_number; /**< Revision-Number stored on the slave. */ |
241 uint32_t serial_number; /**< Serial-Number stored on the slave. */ |
266 uint32_t serial_number; /**< Serial-Number stored on the slave. */ |
242 uint16_t alias; /**< The slaves alias if not equal to 0. */ |
267 uint16_t alias; /**< The slaves alias if not equal to 0. */ |
243 int16_t current_on_ebus; /**< Used current in mA. */ |
268 int16_t current_on_ebus; /**< Used current in mA. */ |
|
269 struct { |
|
270 ec_slave_port_desc_t desc; |
|
271 ec_slave_port_link_t link; |
|
272 uint32_t receive_time; |
|
273 uint16_t next_slave; |
|
274 uint32_t delay_to_next_dc; |
|
275 } ports[EC_MAX_PORTS]; |
244 uint8_t al_state; /**< Current state of the slave. */ |
276 uint8_t al_state; /**< Current state of the slave. */ |
245 uint8_t error_flag; /**< Error flag for that slave. */ |
277 uint8_t error_flag; /**< Error flag for that slave. */ |
246 uint8_t sync_count; /**< Number of sync managers. */ |
278 uint8_t sync_count; /**< Number of sync managers. */ |
247 uint16_t sdo_count; /**< Number of SDOs. */ |
279 uint16_t sdo_count; /**< Number of SDOs. */ |
248 char name[EC_MAX_STRING_LENGTH]; /**< Name of the slave. */ |
280 char name[EC_MAX_STRING_LENGTH]; /**< Name of the slave. */ |
310 } ec_pdo_entry_info_t; |
342 } ec_pdo_entry_info_t; |
311 |
343 |
312 /*****************************************************************************/ |
344 /*****************************************************************************/ |
313 |
345 |
314 /** PDO configuration information. |
346 /** PDO configuration information. |
315 * |
347 * |
316 * This is the data type of the \a pdos field in ec_sync_info_t. |
348 * This is the data type of the \a pdos field in ec_sync_info_t. |
317 * |
349 * |
318 * \see ecrt_slave_config_pdos(). |
350 * \see ecrt_slave_config_pdos(). |
319 */ |
351 */ |
320 typedef struct { |
352 typedef struct { |
321 uint16_t index; /**< PDO index. */ |
353 uint16_t index; /**< PDO index. */ |
322 unsigned int n_entries; /**< Number of PDO entries in \a entries to map. |
354 unsigned int n_entries; /**< Number of PDO entries in \a entries to map. |
361 uint32_t product_code; /**< Slave product code. */ |
393 uint32_t product_code; /**< Slave product code. */ |
362 uint16_t index; /**< PDO entry index. */ |
394 uint16_t index; /**< PDO entry index. */ |
363 uint8_t subindex; /**< PDO entry subindex. */ |
395 uint8_t subindex; /**< PDO entry subindex. */ |
364 unsigned int *offset; /**< Pointer to a variable to store the PDO entry's |
396 unsigned int *offset; /**< Pointer to a variable to store the PDO entry's |
365 (byte-)offset in the process data. */ |
397 (byte-)offset in the process data. */ |
366 unsigned int *bit_position; /**< Pointer to a variable to store a bit |
398 unsigned int *bit_position; /**< Pointer to a variable to store a bit |
367 position (0-7) within the \a offset. Can be |
399 position (0-7) within the \a offset. Can be |
368 NULL, in which case an error is raised if the |
400 NULL, in which case an error is raised if the |
369 PDO entry does not byte-align. */ |
401 PDO entry does not byte-align. */ |
370 } ec_pdo_entry_reg_t; |
402 } ec_pdo_entry_reg_t; |
371 |
403 |
407 * \return Value of ECRT_VERSION_MAGIC() at EtherCAT master compile time. |
439 * \return Value of ECRT_VERSION_MAGIC() at EtherCAT master compile time. |
408 */ |
440 */ |
409 unsigned int ecrt_version_magic(void); |
441 unsigned int ecrt_version_magic(void); |
410 |
442 |
411 /** Requests an EtherCAT master for realtime operation. |
443 /** Requests an EtherCAT master for realtime operation. |
412 * |
444 * |
413 * Before an application can access an EtherCAT master, it has to reserve one |
445 * Before an application can access an EtherCAT master, it has to reserve one |
414 * for exclusive use. |
446 * for exclusive use. |
415 * |
447 * |
416 * In userspace, this is a convenience function for ecrt_open_master() and |
448 * In userspace, this is a convenience function for ecrt_open_master() and |
417 * ecrt_master_reserve(). |
449 * ecrt_master_reserve(). |
494 |
526 |
495 #ifdef __KERNEL__ |
527 #ifdef __KERNEL__ |
496 |
528 |
497 /** Sets the locking callbacks. |
529 /** Sets the locking callbacks. |
498 * |
530 * |
499 * For concurrent master access, i. e. if other instances than the application |
531 * For concurrent master access, the application has to provide a locking |
500 * want to send and receive datagrams on the bus, the application has to |
532 * mechanism. The method takes two function pointers and a data value as |
501 * provide a callback mechanism. This method takes two function pointers as |
533 * its parameters. |
502 * its parameters. Asynchronous master access (like EoE processing) is only |
534 * The arbitrary \a cb_data value will be passed as argument on every callback. |
503 * possible if the callbacks have been set. |
535 * |
504 * |
|
505 * The task of the send callback (\a send_cb) is to decide, if the bus is |
|
506 * currently accessible and whether or not to call the ecrt_master_send_ext() |
|
507 * method. |
|
508 * |
|
509 * The task of the receive callback (\a receive_cb) is to decide, if a call to |
|
510 * ecrt_master_receive() is allowed and to execute it respectively. |
|
511 */ |
536 */ |
512 void ecrt_master_callbacks( |
537 void ecrt_master_callbacks( |
513 ec_master_t *master, /**< EtherCAT master */ |
538 ec_master_t *master, /**< EtherCAT master */ |
514 void (*send_cb)(void *), /**< Datagram sending callback. */ |
539 void (*lock_cb)(void *), /**< Lock function. */ |
515 void (*receive_cb)(void *), /**< Receive callback. */ |
540 void (*unlock_cb)(void *), /**< Unlock function. */ |
516 void *cb_data /**< Arbitraty pointer passed to the callback functions. |
541 void *cb_data /**< Arbitrary user data. */ |
517 */ |
542 ); |
518 ); |
|
519 |
|
520 |
543 |
521 /** Returns domain structure pointer |
544 /** Returns domain structure pointer |
522 * |
545 * |
523 * This functions return the domain structure pointer for usage inside the |
546 * This functions return the domain structure pointer for usage inside the |
524 * RTDM-Interface. |
547 * RTDM-Interface. |
830 */ |
853 */ |
831 void ecrt_master_receive( |
854 void ecrt_master_receive( |
832 ec_master_t *master /**< EtherCAT master. */ |
855 ec_master_t *master /**< EtherCAT master. */ |
833 ); |
856 ); |
834 |
857 |
835 /** Sends non-application datagrams. |
|
836 * |
|
837 * This method has to be called in the send callback function passed via |
|
838 * ecrt_master_callbacks() to allow the sending of non-application datagrams. |
|
839 */ |
|
840 void ecrt_master_send_ext( |
|
841 ec_master_t *master /**< EtherCAT master. */ |
|
842 ); |
|
843 |
|
844 /** Reads the current master state. |
858 /** Reads the current master state. |
845 * |
859 * |
846 * Stores the master state information in the given \a state structure. |
860 * Stores the master state information in the given \a state structure. |
847 */ |
861 */ |
848 void ecrt_master_state( |
862 void ecrt_master_state( |
849 const ec_master_t *master, /**< EtherCAT master. */ |
863 const ec_master_t *master, /**< EtherCAT master. */ |
850 ec_master_state_t *state /**< Structure to store the information. */ |
864 ec_master_state_t *state /**< Structure to store the information. */ |
851 ); |
865 ); |
852 |
866 |
|
867 /** Reads the current master state and the al_state of all configured slaves. |
|
868 * |
|
869 * use this function instead of ecrt_master_state if there are unused |
|
870 * slaves on the bus |
|
871 * Stores the master state information in the given \a state structure. |
|
872 * \see ecrt_master_state() |
|
873 */ |
|
874 void ecrt_master_configured_slaves_state( |
|
875 const ec_master_t *master, /**< EtherCAT master. */ |
|
876 ec_master_state_t *state /**< Structure to store the information. */ |
|
877 ); |
|
878 |
853 /** Sets the application time. |
879 /** Sets the application time. |
854 * |
880 * |
855 * The master has to know the application's time when operating slaves with |
881 * The master has to know the application's time when operating slaves with |
856 * distributed clocks. The time is not incremented by the master itself, so |
882 * distributed clocks. The time is not incremented by the master itself, so |
857 * this method has to be called cyclically. |
883 * this method has to be called cyclically. |
858 * |
884 * |
859 * The time is used when setting the slaves' <tt>System Time Offset</tt> and |
885 * The time is used when setting the slaves' <tt>System Time Offset</tt> and |
860 * <tt>Cyclic Operation Start Time</tt> registers and when synchronizing the |
886 * <tt>Cyclic Operation Start Time</tt> registers and when synchronizing the |
861 * DC reference clock to the application time via |
887 * DC reference clock to the application time via |
862 * ecrt_master_sync_reference_clock(). |
888 * ecrt_master_sync_reference_clock(). |
863 * |
889 * |
864 * The time is defined as nanoseconds from 2000-01-01 00:00. Converting an |
890 * The time is defined as nanoseconds from 2000-01-01 00:00. Converting an |
865 * epoch time can be done with the EC_TIMEVAL2NANO() macro. |
891 * epoch time can be done with the EC_TIMEVAL2NANO() macro. |
866 */ |
892 */ |
867 void ecrt_master_application_time( |
893 void ecrt_master_application_time( |
868 ec_master_t *master, /**< EtherCAT master. */ |
894 ec_master_t *master, /**< EtherCAT master. */ |
950 data watchdog. If set to zero, the value |
976 data watchdog. If set to zero, the value |
951 is not written, so the default is used. |
977 is not written, so the default is used. |
952 */ |
978 */ |
953 ); |
979 ); |
954 |
980 |
|
981 /** Configure wether a slave allows overlapping PDOs. |
|
982 * |
|
983 * Overlapping PDOs allows inputs to use the same space as outputs on the frame. |
|
984 * This reduces the frame length. |
|
985 */ |
|
986 void ecrt_slave_config_overlapping_pdos( |
|
987 ec_slave_config_t *sc, /**< Slave configuration. */ |
|
988 uint8_t allow_overlapping_pdos /**< Allow overlapping PDOs */ |
|
989 ); |
|
990 |
|
991 |
955 /** Add a PDO to a sync manager's PDO assignment. |
992 /** Add a PDO to a sync manager's PDO assignment. |
956 * |
993 * |
957 * \see ecrt_slave_config_pdos() |
994 * \see ecrt_slave_config_pdos() |
958 * \return zero on success, else non-zero |
995 * \return zero on success, else non-zero |
959 */ |
996 */ |
967 /** Clear a sync manager's PDO assignment. |
1004 /** Clear a sync manager's PDO assignment. |
968 * |
1005 * |
969 * This can be called before assigning PDOs via |
1006 * This can be called before assigning PDOs via |
970 * ecrt_slave_config_pdo_assign_add(), to clear the default assignment of a |
1007 * ecrt_slave_config_pdo_assign_add(), to clear the default assignment of a |
971 * sync manager. |
1008 * sync manager. |
972 * |
1009 * |
973 * \see ecrt_slave_config_pdos() |
1010 * \see ecrt_slave_config_pdos() |
974 */ |
1011 */ |
975 void ecrt_slave_config_pdo_assign_clear( |
1012 void ecrt_slave_config_pdo_assign_clear( |
976 ec_slave_config_t *sc, /**< Slave configuration. */ |
1013 ec_slave_config_t *sc, /**< Slave configuration. */ |
977 uint8_t sync_index /**< Sync manager index. Must be less |
1014 uint8_t sync_index /**< Sync manager index. Must be less |
1021 * \code |
1058 * \code |
1022 * ec_pdo_entry_info_t el3162_channel1[] = { |
1059 * ec_pdo_entry_info_t el3162_channel1[] = { |
1023 * {0x3101, 1, 8}, // status |
1060 * {0x3101, 1, 8}, // status |
1024 * {0x3101, 2, 16} // value |
1061 * {0x3101, 2, 16} // value |
1025 * }; |
1062 * }; |
1026 * |
1063 * |
1027 * ec_pdo_entry_info_t el3162_channel2[] = { |
1064 * ec_pdo_entry_info_t el3162_channel2[] = { |
1028 * {0x3102, 1, 8}, // status |
1065 * {0x3102, 1, 8}, // status |
1029 * {0x3102, 2, 16} // value |
1066 * {0x3102, 2, 16} // value |
1030 * }; |
1067 * }; |
1031 * |
1068 * |
1032 * ec_pdo_info_t el3162_pdos[] = { |
1069 * ec_pdo_info_t el3162_pdos[] = { |
1033 * {0x1A00, 2, el3162_channel1}, |
1070 * {0x1A00, 2, el3162_channel1}, |
1034 * {0x1A01, 2, el3162_channel2} |
1071 * {0x1A01, 2, el3162_channel2} |
1035 * }; |
1072 * }; |
1036 * |
1073 * |
1037 * ec_sync_info_t el3162_syncs[] = { |
1074 * ec_sync_info_t el3162_syncs[] = { |
1038 * {2, EC_DIR_OUTPUT}, |
1075 * {2, EC_DIR_OUTPUT}, |
1039 * {3, EC_DIR_INPUT, 2, el3162_pdos}, |
1076 * {3, EC_DIR_INPUT, 2, el3162_pdos}, |
1040 * {0xff} |
1077 * {0xff} |
1041 * }; |
1078 * }; |
1042 * |
1079 * |
1043 * if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3162_syncs)) { |
1080 * if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3162_syncs)) { |
1044 * // handle error |
1081 * // handle error |
1045 * } |
1082 * } |
1046 * \endcode |
1083 * \endcode |
1047 * |
1084 * |
1048 * The next example shows, how to configure the PDO assignment only. The |
1085 * The next example shows, how to configure the PDO assignment only. The |
1049 * entries for each assigned PDO are taken from the PDO's default mapping. |
1086 * entries for each assigned PDO are taken from the PDO's default mapping. |
1050 * Please note, that PDO entry registration will fail, if the PDO |
1087 * Please note, that PDO entry registration will fail, if the PDO |
1051 * configuration is left empty and the slave is offline. |
1088 * configuration is left empty and the slave is offline. |
1052 * |
1089 * |
1053 * \code |
1090 * \code |
1054 * ec_pdo_info_t pdos[] = { |
1091 * ec_pdo_info_t pdos[] = { |
1055 * {0x1600}, // Channel 1 |
1092 * {0x1600}, // Channel 1 |
1056 * {0x1601} // Channel 2 |
1093 * {0x1601} // Channel 2 |
1057 * }; |
1094 * }; |
1058 * |
1095 * |
1059 * ec_sync_info_t syncs[] = { |
1096 * ec_sync_info_t syncs[] = { |
1060 * {3, EC_DIR_INPUT, 2, pdos}, |
1097 * {3, EC_DIR_INPUT, 2, pdos}, |
1061 * }; |
1098 * }; |
1062 * |
1099 * |
1063 * if (ecrt_slave_config_pdos(slave_config_ana_in, 1, syncs)) { |
1100 * if (ecrt_slave_config_pdos(slave_config_ana_in, 1, syncs)) { |
1064 * // handle error |
1101 * // handle error |
1065 * } |
1102 * } |
1066 * \endcode |
1103 * \endcode |
1067 * |
1104 * |
1099 int ecrt_slave_config_reg_pdo_entry( |
1136 int ecrt_slave_config_reg_pdo_entry( |
1100 ec_slave_config_t *sc, /**< Slave configuration. */ |
1137 ec_slave_config_t *sc, /**< Slave configuration. */ |
1101 uint16_t entry_index, /**< Index of the PDO entry to register. */ |
1138 uint16_t entry_index, /**< Index of the PDO entry to register. */ |
1102 uint8_t entry_subindex, /**< Subindex of the PDO entry to register. */ |
1139 uint8_t entry_subindex, /**< Subindex of the PDO entry to register. */ |
1103 ec_domain_t *domain, /**< Domain. */ |
1140 ec_domain_t *domain, /**< Domain. */ |
1104 unsigned int *bit_position /**< Optional address if bit addressing |
1141 unsigned int *bit_position /**< Optional address if bit addressing |
1105 is desired */ |
1142 is desired */ |
1106 ); |
1143 ); |
1107 |
1144 |
1108 /** Configure distributed clocks. |
1145 /** Configure distributed clocks. |
1109 * |
1146 * |