examples/dc_rtai/dc_rtai_sample.c
changeset 1500 ed1a733efbc5
parent 1466 362147819ca1
child 1508 60a116ed3897
equal deleted inserted replaced
1499:5461fce4a2ea 1500:ed1a733efbc5
    56 /*****************************************************************************/
    56 /*****************************************************************************/
    57 
    57 
    58 // EtherCAT
    58 // EtherCAT
    59 static ec_master_t *master = NULL;
    59 static ec_master_t *master = NULL;
    60 static ec_master_state_t master_state = {};
    60 static ec_master_state_t master_state = {};
    61 spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
       
    62 
    61 
    63 static ec_domain_t *domain1 = NULL;
    62 static ec_domain_t *domain1 = NULL;
    64 static ec_domain_state_t domain1_state = {};
    63 static ec_domain_state_t domain1_state = {};
    65 
    64 
    66 // RTAI
    65 // RTAI
   123 
   122 
   124 void check_domain1_state(void)
   123 void check_domain1_state(void)
   125 {
   124 {
   126     ec_domain_state_t ds;
   125     ec_domain_state_t ds;
   127 
   126 
   128     spin_lock(&master_lock);
   127     rt_sem_wait(&master_sem);
   129     ecrt_domain_state(domain1, &ds);
   128     ecrt_domain_state(domain1, &ds);
   130     spin_unlock(&master_lock);
   129     rt_sem_signal(&master_sem);
   131 
   130 
   132     if (ds.working_counter != domain1_state.working_counter)
   131     if (ds.working_counter != domain1_state.working_counter)
   133         printk(KERN_INFO PFX "Domain1: WC %u.\n", ds.working_counter);
   132         printk(KERN_INFO PFX "Domain1: WC %u.\n", ds.working_counter);
   134     if (ds.wc_state != domain1_state.wc_state)
   133     if (ds.wc_state != domain1_state.wc_state)
   135         printk(KERN_INFO PFX "Domain1: State %u.\n", ds.wc_state);
   134         printk(KERN_INFO PFX "Domain1: State %u.\n", ds.wc_state);
   141 
   140 
   142 void check_master_state(void)
   141 void check_master_state(void)
   143 {
   142 {
   144     ec_master_state_t ms;
   143     ec_master_state_t ms;
   145 
   144 
   146     spin_lock(&master_lock);
   145     rt_sem_wait(&master_sem);
   147     ecrt_master_state(master, &ms);
   146     ecrt_master_state(master, &ms);
   148     spin_unlock(&master_lock);
   147     rt_sem_signal(&master_sem);
   149 
   148 
   150     if (ms.slaves_responding != master_state.slaves_responding)
   149     if (ms.slaves_responding != master_state.slaves_responding)
   151         printk(KERN_INFO PFX "%u slave(s).\n", ms.slaves_responding);
   150         printk(KERN_INFO PFX "%u slave(s).\n", ms.slaves_responding);
   152     if (ms.al_states != master_state.al_states)
   151     if (ms.al_states != master_state.al_states)
   153         printk(KERN_INFO PFX "AL states: 0x%02X.\n", ms.al_states);
   152         printk(KERN_INFO PFX "AL states: 0x%02X.\n", ms.al_states);
   237     }
   236     }
   238 }
   237 }
   239 
   238 
   240 /*****************************************************************************/
   239 /*****************************************************************************/
   241 
   240 
   242 int request_lock(void *data)
   241 void send_callback(ec_master_t *master)
   243 {
   242 {
   244     // too close to the next real time cycle: deny access...
   243     // too close to the next real time cycle: deny access...
   245     if (get_cycles() - t_last_cycle > t_critical) return -1;
   244     if (get_cycles() - t_last_cycle <= t_critical) {
   246 
   245         rt_sem_wait(&master_sem);
   247     // allow access
   246         ecrt_master_send_ext(master);
   248     rt_sem_wait(&master_sem);
   247         rt_sem_signal(&master_sem);
   249     return 0;
   248     }
   250 }
   249 }
   251 
   250 
   252 /*****************************************************************************/
   251 /*****************************************************************************/
   253 
   252 
   254 void release_lock(void *data)
   253 void receive_callback(ec_master_t *master)
   255 {
   254 {
   256     rt_sem_signal(&master_sem);
   255     // too close to the next real time cycle: deny access...
       
   256     if (get_cycles() - t_last_cycle <= t_critical) {
       
   257         rt_sem_wait(&master_sem);
       
   258         ecrt_master_receive(master);
       
   259         rt_sem_signal(&master_sem);
       
   260     }
   257 }
   261 }
   258 
   262 
   259 /*****************************************************************************/
   263 /*****************************************************************************/
   260 
   264 
   261 int __init init_mod(void)
   265 int __init init_mod(void)
   275         ret = -EBUSY; 
   279         ret = -EBUSY; 
   276         printk(KERN_ERR PFX "Requesting master 0 failed!\n");
   280         printk(KERN_ERR PFX "Requesting master 0 failed!\n");
   277         goto out_return;
   281         goto out_return;
   278     }
   282     }
   279 
   283 
   280     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   284     ecrt_master_callbacks(master, send_callback, receive_callback);
   281 
   285 
   282     printk(KERN_INFO PFX "Registering domain...\n");
   286     printk(KERN_INFO PFX "Registering domain...\n");
   283     if (!(domain1 = ecrt_master_create_domain(master))) {
   287     if (!(domain1 = ecrt_master_create_domain(master))) {
   284         printk(KERN_ERR PFX "Domain creation failed!\n");
   288         printk(KERN_ERR PFX "Domain creation failed!\n");
   285         goto out_release_master;
   289         goto out_release_master;