examples/rtai/rtai_sample.c
changeset 512 24292123d174
parent 509 87baea61d0c7
child 516 f703e33ee395
equal deleted inserted replaced
511:73fb3c35e53d 512:24292123d174
    44 #include "../../include/ecrt.h"
    44 #include "../../include/ecrt.h"
    45 #include "../../include/ecdb.h"
    45 #include "../../include/ecdb.h"
    46 
    46 
    47 /*****************************************************************************/
    47 /*****************************************************************************/
    48 
    48 
    49 MODULE_LICENSE("GPL");
       
    50 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
       
    51 MODULE_DESCRIPTION("EtherCAT RTAI sample module");
       
    52 
       
    53 /*****************************************************************************/
       
    54 
       
    55 // RTAI task frequency in Hz
    49 // RTAI task frequency in Hz
    56 #define FREQUENCY 10000
    50 #define FREQUENCY 10000
    57 #define INHIBIT_TIME 20
    51 #define INHIBIT_TIME 20
    58 
    52 
    59 #define TIMERTICKS (1000000000 / FREQUENCY)
    53 #define TIMERTICKS (1000000000 / FREQUENCY)
    82 
    76 
    83 /*****************************************************************************/
    77 /*****************************************************************************/
    84 
    78 
    85 void run(long data)
    79 void run(long data)
    86 {
    80 {
    87     while (1)
    81     while (1) {
    88     {
       
    89         t_last_cycle = get_cycles();
    82         t_last_cycle = get_cycles();
       
    83 
    90         rt_sem_wait(&master_sem);
    84         rt_sem_wait(&master_sem);
    91 
       
    92         ecrt_master_receive(master);
    85         ecrt_master_receive(master);
    93         ecrt_domain_process(domain1);
    86         ecrt_domain_process(domain1);
       
    87         rt_sem_signal(&master_sem);
    94 
    88 
    95         // process data
    89         // process data
    96         //k_pos = EC_READ_U32(r_ssi_input);
    90         //k_pos = EC_READ_U32(r_ssi_input);
    97 
    91 
       
    92         rt_sem_wait(&master_sem);
    98         ecrt_domain_queue(domain1);
    93         ecrt_domain_queue(domain1);
    99         ecrt_master_run(master);
    94         ecrt_master_run(master);
   100         ecrt_master_send(master);
    95         ecrt_master_send(master);
   101 
       
   102         rt_sem_signal(&master_sem);
    96         rt_sem_signal(&master_sem);
       
    97 
   103         rt_task_wait_period();
    98         rt_task_wait_period();
   104     }
    99     }
   105 }
   100 }
   106 
   101 
   107 /*****************************************************************************/
   102 /*****************************************************************************/
   108 
   103 
   109 int request_lock(void *data)
   104 int request_lock(void *data)
   110 {
   105 {
   111     // too close to the next RT cycle: deny access...
   106     // too close to the next real time cycle: deny access...
   112     if (get_cycles() - t_last_cycle > t_critical) return -1;
   107     if (get_cycles() - t_last_cycle > t_critical) return -1;
   113 
   108 
   114     // allow access
   109     // allow access
   115     rt_sem_wait(&master_sem);
   110     rt_sem_wait(&master_sem);
   116     return 0;
   111     return 0;
   140         goto out_return;
   135         goto out_return;
   141     }
   136     }
   142 
   137 
   143     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   138     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   144 
   139 
   145     printk(KERN_INFO "Registering domain...\n");
   140     printk(KERN_INFO "Creating domain...\n");
   146     if (!(domain1 = ecrt_master_create_domain(master))) {
   141     if (!(domain1 = ecrt_master_create_domain(master))) {
   147         printk(KERN_ERR "Domain creation failed!\n");
   142         printk(KERN_ERR "Domain creation failed!\n");
   148         goto out_release_master;
   143         goto out_release_master;
   149     }
   144     }
   150 
   145 
   205     printk(KERN_INFO "=== EtherCAT RTAI sample module stopped. ===\n");
   200     printk(KERN_INFO "=== EtherCAT RTAI sample module stopped. ===\n");
   206 }
   201 }
   207 
   202 
   208 /*****************************************************************************/
   203 /*****************************************************************************/
   209 
   204 
       
   205 MODULE_LICENSE("GPL");
       
   206 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
       
   207 MODULE_DESCRIPTION("EtherCAT RTAI sample module");
       
   208 
   210 module_init(init_mod);
   209 module_init(init_mod);
   211 module_exit(cleanup_mod);
   210 module_exit(cleanup_mod);
   212 
   211 
   213 /*****************************************************************************/
   212 /*****************************************************************************/
   214