master/master.h
changeset 2060 8b67602f5161
parent 2045 ff2a13a4603c
child 2094 83e9160319ec
equal deleted inserted replaced
2059:ab0b96ac18bb 2060:8b67602f5161
    41 #include <linux/list.h>
    41 #include <linux/list.h>
    42 #include <linux/timer.h>
    42 #include <linux/timer.h>
    43 #include <linux/wait.h>
    43 #include <linux/wait.h>
    44 #include <linux/kthread.h>
    44 #include <linux/kthread.h>
    45 
    45 
    46 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
       
    47 #include <linux/semaphore.h>
       
    48 #else
       
    49 #include <asm/semaphore.h>
       
    50 #endif
       
    51 
       
    52 #include "device.h"
    46 #include "device.h"
    53 #include "domain.h"
    47 #include "domain.h"
    54 #include "ethernet.h"
    48 #include "ethernet.h"
    55 #include "fsm_master.h"
    49 #include "fsm_master.h"
    56 #include "cdev.h"
    50 #include "cdev.h"
    57 
    51 
       
    52 
    58 /*****************************************************************************/
    53 /*****************************************************************************/
    59 
    54 
    60 /** Convenience macro for printing master-specific information to syslog.
    55 /** Convenience macro for printing master-specific information to syslog.
    61  *
    56  *
    62  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    57  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    64  *
    59  *
    65  * \param master EtherCAT master
    60  * \param master EtherCAT master
    66  * \param fmt format string (like in printf())
    61  * \param fmt format string (like in printf())
    67  * \param args arguments (optional)
    62  * \param args arguments (optional)
    68  */
    63  */
       
    64 #ifdef USE_TRACE_PRINTK
       
    65 #define EC_MASTER_INFO(master, fmt, args...) \
       
    66     do { \
       
    67         __trace_printk(_THIS_IP_,"EtherCAT %u: " fmt, master->index, ##args); \
       
    68         printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args);   \
       
    69     } while (0)
       
    70 #else
    69 #define EC_MASTER_INFO(master, fmt, args...) \
    71 #define EC_MASTER_INFO(master, fmt, args...) \
    70     printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
    72     printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
       
    73 #endif
    71 
    74 
    72 /** Convenience macro for printing master-specific errors to syslog.
    75 /** Convenience macro for printing master-specific errors to syslog.
    73  *
    76  *
    74  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    77  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    75  * where INDEX is the master index.
    78  * where INDEX is the master index.
    76  *
    79  *
    77  * \param master EtherCAT master
    80  * \param master EtherCAT master
    78  * \param fmt format string (like in printf())
    81  * \param fmt format string (like in printf())
    79  * \param args arguments (optional)
    82  * \param args arguments (optional)
    80  */
    83  */
       
    84 #ifdef USE_TRACE_PRINTK
       
    85 #define EC_MASTER_ERR(master, fmt, args...) \
       
    86     do { \
       
    87         __trace_printk(_THIS_IP_,"EtherCAT ERROR %u: " fmt, master->index, ##args); \
       
    88         printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args); \
       
    89     } while (0)
       
    90 #else
    81 #define EC_MASTER_ERR(master, fmt, args...) \
    91 #define EC_MASTER_ERR(master, fmt, args...) \
    82     printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
    92     printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
       
    93 #endif
    83 
    94 
    84 /** Convenience macro for printing master-specific warnings to syslog.
    95 /** Convenience macro for printing master-specific warnings to syslog.
    85  *
    96  *
    86  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    97  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    87  * where INDEX is the master index.
    98  * where INDEX is the master index.
    88  *
    99  *
    89  * \param master EtherCAT master
   100  * \param master EtherCAT master
    90  * \param fmt format string (like in printf())
   101  * \param fmt format string (like in printf())
    91  * \param args arguments (optional)
   102  * \param args arguments (optional)
    92  */
   103  */
       
   104 #ifdef USE_TRACE_PRINTK
       
   105 #define EC_MASTER_WARN(master, fmt, args...) \
       
   106     do { \
       
   107         __trace_printk(_THIS_IP_,"EtherCAT WARNING %u: " fmt, master->index, ##args); \
       
   108         printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args);    \
       
   109     } while (0)
       
   110 #else
    93 #define EC_MASTER_WARN(master, fmt, args...) \
   111 #define EC_MASTER_WARN(master, fmt, args...) \
    94     printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
   112     printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
       
   113 #endif
    95 
   114 
    96 /** Convenience macro for printing master-specific debug messages to syslog.
   115 /** Convenience macro for printing master-specific debug messages to syslog.
    97  *
   116  *
    98  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
   117  * This will print the message in \a fmt with a prefixed "EtherCAT <INDEX>: ",
    99  * where INDEX is the master index.
   118  * where INDEX is the master index.
   100  *
   119  *
   101  * \param master EtherCAT master
   120  * \param master EtherCAT master
   102  * \param fmt format string (like in printf())
   121  * \param fmt format string (like in printf())
   103  * \param args arguments (optional)
   122  * \param args arguments (optional)
   104  */
   123  */
       
   124 #ifdef USE_TRACE_PRINTK
   105 #define EC_MASTER_DBG(master, level, fmt, args...) \
   125 #define EC_MASTER_DBG(master, level, fmt, args...) \
   106     do { \
   126     do { \
       
   127         __trace_printk(_THIS_IP_,"EtherCAT DEBUG%u %u: " fmt, \
       
   128             level,master->index, ##args); \
   107         if (master->debug_level >= level) { \
   129         if (master->debug_level >= level) { \
   108             printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
   130             printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
   109                     master->index, ##args); \
   131                     master->index, ##args); \
   110         } \
   132         } \
   111     } while (0)
   133     } while (0)
       
   134 #else
       
   135 #define EC_MASTER_DBG(master, level, fmt, args...) \
       
   136     do { \
       
   137         if (master->debug_level >= level) { \
       
   138             printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
       
   139                     master->index, ##args); \
       
   140         } \
       
   141     } while (0)
       
   142 #endif
   112 
   143 
   113 /*****************************************************************************/
   144 /*****************************************************************************/
   114 
   145 
   115 /** EtherCAT master phase.
   146 /** EtherCAT master phase.
   116  */
   147  */
   150     struct device *class_device; /**< Master class device. */
   181     struct device *class_device; /**< Master class device. */
   151 #else
   182 #else
   152     struct class_device *class_device; /**< Master class device. */
   183     struct class_device *class_device; /**< Master class device. */
   153 #endif
   184 #endif
   154 
   185 
   155     struct semaphore master_sem; /**< Master semaphore. */
   186     struct ec_mutex_t master_mutex; /**< Master mutex. */
   156 
   187 
   157     ec_device_t main_device; /**< EtherCAT main device. */
   188     ec_device_t main_device; /**< EtherCAT main device. */
   158     const uint8_t *main_mac; /**< MAC address of main device. */
   189     const uint8_t *main_mac; /**< MAC address of main device. */
   159     ec_device_t backup_device; /**< EtherCAT backup device. */
   190     ec_device_t backup_device; /**< EtherCAT backup device. */
   160     const uint8_t *backup_mac; /**< MAC address of backup device. */
   191     const uint8_t *backup_mac; /**< MAC address of backup device. */
   161     struct semaphore device_sem; /**< Device semaphore. */
   192     struct ec_mutex_t device_mutex; /**< Device mutex. */
   162 
   193 
   163     ec_fsm_master_t fsm; /**< Master state machine. */
   194     ec_fsm_master_t fsm; /**< Master state machine. */
   164     ec_datagram_t fsm_datagram; /**< Datagram used for state machines. */
   195     ec_datagram_t fsm_datagram; /**< Datagram used for state machines. */
       
   196     ec_mailbox_t fsm_mbox; /**< Mailbox used for state machines. */
   165     ec_master_phase_t phase; /**< Master phase. */
   197     ec_master_phase_t phase; /**< Master phase. */
   166     unsigned int active; /**< Master has been activated. */
   198     unsigned int active; /**< Master has been activated. */
   167     unsigned int config_changed; /**< The configuration changed. */
   199     unsigned int config_changed; /**< The configuration changed. */
   168     unsigned int injection_seq_fsm; /**< Datagram injection sequence number
   200     unsigned int injection_seq_fsm; /**< Datagram injection sequence number
   169                                       for the FSM side. */
   201                                       for the FSM side. */
   185     ec_datagram_t sync_datagram; /**< Datagram used for DC drift
   217     ec_datagram_t sync_datagram; /**< Datagram used for DC drift
   186                                    compensation. */
   218                                    compensation. */
   187     ec_datagram_t sync_mon_datagram; /**< Datagram used for DC synchronisation
   219     ec_datagram_t sync_mon_datagram; /**< Datagram used for DC synchronisation
   188                                        monitoring. */
   220                                        monitoring. */
   189     ec_slave_t *dc_ref_clock; /**< DC reference clock slave. */
   221     ec_slave_t *dc_ref_clock; /**< DC reference clock slave. */
   190     
   222 #ifdef EC_HAVE_CYCLES
       
   223     cycles_t dc_cycles_app_start_time; /** cycles at last ecrt_master_sync() call.*/
       
   224 #endif
       
   225     unsigned long dc_jiffies_app_start_time;/** jiffies at last
       
   226                                             ecrt_master_sync() call.*/
   191     unsigned int scan_busy; /**< Current scan state. */
   227     unsigned int scan_busy; /**< Current scan state. */
   192     unsigned int allow_scan; /**< \a True, if slave scanning is allowed. */
   228     unsigned int allow_scan; /**< \a True, if slave scanning is allowed. */
   193     struct semaphore scan_sem; /**< Semaphore protecting the \a scan_busy
   229     struct ec_mutex_t scan_mutex; /**< Mutex protecting the \a scan_busy
   194                                  variable and the \a allow_scan flag. */
   230                                  variable and the \a allow_scan flag. */
   195     wait_queue_head_t scan_queue; /**< Queue for processes that wait for
   231     wait_queue_head_t scan_queue; /**< Queue for processes that wait for
   196                                     slave scanning. */
   232                                     slave scanning. */
   197 
   233 
   198     unsigned int config_busy; /**< State of slave configuration. */
   234     unsigned int config_busy; /**< State of slave configuration. */
   199     unsigned int allow_config; /**< \a True, if slave configuration is
   235     unsigned int allow_config; /**< \a True, if slave configuration is
   200                                  allowed. */
   236                                  allowed. */
   201     struct semaphore config_sem; /**< Semaphore protecting the \a config_busy
   237     struct ec_mutex_t config_mutex; /**< Mutex protecting the \a config_busy
   202                                    variable and the allow_config flag. */
   238                                    variable and the allow_config flag. */
   203     wait_queue_head_t config_queue; /**< Queue for processes that wait for
   239     wait_queue_head_t config_queue; /**< Queue for processes that wait for
   204                                       slave configuration. */
   240                                       slave configuration. */
   205 
   241 
   206     struct list_head datagram_queue; /**< Datagram queue. */
   242     struct list_head datagram_queue; /**< Datagram queue. */
   207     uint8_t datagram_index; /**< Current datagram index. */
   243     uint8_t datagram_index; /**< Current datagram index. */
   208 
   244 
   209     struct list_head ext_datagram_queue; /**< Queue for non-application
   245     struct ec_mutex_t fsm_queue_mutex; /**< Mutex protecting the \a
   210                                            datagrams. */
   246                                       fsm_datagram_queue. */
   211     struct semaphore ext_queue_sem; /**< Semaphore protecting the \a
   247     struct list_head fsm_datagram_queue; /**< External Datagram queue. */
   212                                       ext_datagram_queue. */
   248 
   213 
       
   214     struct list_head external_datagram_queue; /**< External Datagram queue. */
       
   215     unsigned int send_interval; /**< Interval between calls to ecrt_master_send */
   249     unsigned int send_interval; /**< Interval between calls to ecrt_master_send */
   216     size_t max_queue_size; /**< Maximum size of datagram queue */
   250     size_t max_queue_size; /**< Maximum size of datagram queue */
   217 
   251 
   218     unsigned int debug_level; /**< Master debug level. */
   252     unsigned int debug_level; /**< Master debug level. */
   219     ec_stats_t stats; /**< Cyclic statistics. */
   253     ec_stats_t stats; /**< Cyclic statistics. */
   223 #ifdef EC_EOE
   257 #ifdef EC_EOE
   224     struct task_struct *eoe_thread; /**< EoE thread. */
   258     struct task_struct *eoe_thread; /**< EoE thread. */
   225     struct list_head eoe_handlers; /**< Ethernet over EtherCAT handlers. */
   259     struct list_head eoe_handlers; /**< Ethernet over EtherCAT handlers. */
   226 #endif
   260 #endif
   227 
   261 
   228     struct semaphore io_sem; /**< Semaphore used in \a IDLE phase. */
   262     struct ec_mutex_t io_mutex; /**< Mutex used in \a IDLE phase. */
   229 
   263 
   230     void (*send_cb)(void *); /**< Current send datagrams callback. */
   264     void (*fsm_queue_lock_cb)(void *); /**< FSM queue lock callback. */
   231     void (*receive_cb)(void *); /**< Current receive datagrams callback. */
   265     void (*fsm_queue_unlock_cb)(void *); /**< FSM queue unlock callback. */
   232     void *cb_data; /**< Current callback data. */
   266     void *fsm_queue_locking_data; /**< Data parameter of fsm queue locking callbacks. */
   233     void (*app_send_cb)(void *); /**< Application's send datagrams
   267     void (*app_fsm_queue_lock_cb)(void *); /**< App's FSM queue lock callback. */
   234                                           callback. */
   268     void (*app_fsm_queue_unlock_cb)(void *); /**< App's FSM queue unlock callback. */
   235     void (*app_receive_cb)(void *); /**< Application's receive datagrams
   269     void *app_fsm_queue_locking_data; /**< App's data parameter of fsm queue locking callbacks. */
   236                                       callback. */
       
   237     void *app_cb_data; /**< Application callback data. */
       
   238 
   270 
   239     struct list_head sii_requests; /**< SII write requests. */
   271     struct list_head sii_requests; /**< SII write requests. */
   240     wait_queue_head_t sii_queue; /**< Wait queue for SII
   272     wait_queue_head_t sii_queue; /**< Wait queue for SII
   241                                       write requests from user space. */
   273                                       write requests from user space. */
   242 
   274 
   258 int ec_master_enter_idle_phase(ec_master_t *);
   290 int ec_master_enter_idle_phase(ec_master_t *);
   259 void ec_master_leave_idle_phase(ec_master_t *);
   291 void ec_master_leave_idle_phase(ec_master_t *);
   260 int ec_master_enter_operation_phase(ec_master_t *);
   292 int ec_master_enter_operation_phase(ec_master_t *);
   261 void ec_master_leave_operation_phase(ec_master_t *);
   293 void ec_master_leave_operation_phase(ec_master_t *);
   262 
   294 
   263 #ifdef EC_EOE
       
   264 // EoE
       
   265 void ec_master_eoe_start(ec_master_t *);
       
   266 void ec_master_eoe_stop(ec_master_t *);
       
   267 #endif
       
   268 
       
   269 // datagram IO
   295 // datagram IO
   270 void ec_master_receive_datagrams(ec_master_t *, const uint8_t *, size_t);
   296 void ec_master_receive_datagrams(ec_master_t *, const uint8_t *, size_t);
   271 void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *);
   297 void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *);
   272 void ec_master_queue_datagram_ext(ec_master_t *, ec_datagram_t *);
   298 void ec_master_queue_request_fsm_datagram(ec_master_t *, ec_datagram_t *);
   273 void ec_master_queue_external_datagram(ec_master_t *, ec_datagram_t *);
   299 void ec_master_queue_fsm_datagram(ec_master_t *, ec_datagram_t *);
   274 void ec_master_inject_external_datagrams(ec_master_t *);
   300 void ec_master_inject_fsm_datagrams(ec_master_t *);
   275 
   301 
   276 // misc.
   302 // misc.
   277 void ec_master_set_send_interval(ec_master_t *, unsigned int);
   303 void ec_master_set_send_interval(ec_master_t *, unsigned int);
   278 void ec_master_attach_slave_configs(ec_master_t *);
   304 void ec_master_attach_slave_configs(ec_master_t *);
   279 ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
   305 ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
   306         uint16_t, uint32_t, uint32_t);
   332         uint16_t, uint32_t, uint32_t);
   307 
   333 
   308 void ec_master_calc_dc(ec_master_t *);
   334 void ec_master_calc_dc(ec_master_t *);
   309 void ec_master_request_op(ec_master_t *);
   335 void ec_master_request_op(ec_master_t *);
   310 
   336 
   311 void ec_master_internal_send_cb(void *);
   337 /*****************************************************************************/
   312 void ec_master_internal_receive_cb(void *);
   338 
   313 
   339 #endif
   314 /*****************************************************************************/
       
   315 
       
   316 #endif