examples/rtai/rtai_sample.c
branchstable-1.1
changeset 1716 9440f4ff25c7
parent 1715 e675450f2174
child 449 3caf8ff4d8a2
equal deleted inserted replaced
1715:e675450f2174 1716:9440f4ff25c7
    52 
    52 
    53 /*****************************************************************************/
    53 /*****************************************************************************/
    54 
    54 
    55 // RTAI task frequency in Hz
    55 // RTAI task frequency in Hz
    56 #define FREQUENCY 10000
    56 #define FREQUENCY 10000
       
    57 #define INHIBIT_TIME 20
       
    58 
    57 #define TIMERTICKS (1000000000 / FREQUENCY)
    59 #define TIMERTICKS (1000000000 / FREQUENCY)
    58 
    60 
    59 /*****************************************************************************/
    61 /*****************************************************************************/
    60 
    62 
    61 // RTAI
    63 // RTAI
    62 RT_TASK task;
    64 RT_TASK task;
    63 SEM master_sem;
    65 SEM master_sem;
    64 cycles_t t_last_start = 0, t_critical;
    66 cycles_t t_last_cycle = 0, t_critical;
    65 
    67 
    66 // EtherCAT
    68 // EtherCAT
    67 ec_master_t *master = NULL;
    69 ec_master_t *master = NULL;
    68 ec_domain_t *domain1 = NULL;
    70 ec_domain_t *domain1 = NULL;
    69 
    71 
    82 
    84 
    83 void run(long data)
    85 void run(long data)
    84 {
    86 {
    85     while (1)
    87     while (1)
    86     {
    88     {
    87         t_last_start = get_cycles();
    89         t_last_cycle = get_cycles();
    88         rt_sem_wait(&master_sem);
    90         rt_sem_wait(&master_sem);
    89 
    91 
    90         ecrt_master_receive(master);
    92         ecrt_master_receive(master);
    91         ecrt_domain_process(domain1);
    93         ecrt_domain_process(domain1);
    92 
    94 
   104 /*****************************************************************************/
   106 /*****************************************************************************/
   105 
   107 
   106 int request_lock(void *data)
   108 int request_lock(void *data)
   107 {
   109 {
   108     // too close to the next RT cycle: deny access...
   110     // too close to the next RT cycle: deny access...
   109     if (get_cycles() - t_last_start > t_critical) return -1;
   111     if (get_cycles() - t_last_cycle > t_critical) return -1;
   110 
   112 
   111     // allow access
   113     // allow access
   112     rt_sem_wait(&master_sem);
   114     rt_sem_wait(&master_sem);
   113     return 0;
   115     return 0;
   114 }
   116 }
   128 
   130 
   129     printk(KERN_INFO "=== Starting EtherCAT RTAI sample module... ===\n");
   131     printk(KERN_INFO "=== Starting EtherCAT RTAI sample module... ===\n");
   130 
   132 
   131     rt_sem_init(&master_sem, 1);
   133     rt_sem_init(&master_sem, 1);
   132 
   134 
   133     t_critical = cpu_khz * 800 / FREQUENCY; // ticks for 80%
   135     t_critical = cpu_khz * 1000 / FREQUENCY - cpu_khz * INHIBIT_TIME / 1000;
   134 
   136 
   135     if ((master = ecrt_request_master(0)) == NULL) {
   137     if (!(master = ecrt_request_master(0))) {
   136         printk(KERN_ERR "Requesting master 0 failed!\n");
   138         printk(KERN_ERR "Requesting master 0 failed!\n");
   137         goto out_return;
   139         goto out_return;
   138     }
   140     }
       
   141 
   139 
   142 
   140     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   143     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   141 
   144 
   142     printk(KERN_INFO "Registering domain...\n");
   145     printk(KERN_INFO "Registering domain...\n");
   143     if (!(domain1 = ecrt_master_create_domain(master))) {
   146     if (!(domain1 = ecrt_master_create_domain(master))) {