examples/rtai/rtai_sample.c
branchstable-1.2
changeset 1739 5fcbd29151d2
parent 1732 1cc865ba17c2
child 1744 7bc131b92039
equal deleted inserted replaced
1738:bc89e3fba1a5 1739:5fcbd29151d2
    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 4000
    57 #define INHIBIT_TIME 20
    51 #define INHIBIT_TIME 20
    58 
    52 
    59 #define TIMERTICKS (1000000000 / FREQUENCY)
    53 #define TIMERTICKS (1000000000 / FREQUENCY)
    60 
    54 
    61 /*****************************************************************************/
    55 /*****************************************************************************/
    68 // EtherCAT
    62 // EtherCAT
    69 ec_master_t *master = NULL;
    63 ec_master_t *master = NULL;
    70 ec_domain_t *domain1 = NULL;
    64 ec_domain_t *domain1 = NULL;
    71 
    65 
    72 // data fields
    66 // data fields
    73 void *r_ana_out;
    67 void *r_dig_out;
    74 
       
    75 // channels
       
    76 uint32_t k_pos;
       
    77 
    68 
    78 ec_pdo_reg_t domain1_pdos[] = {
    69 ec_pdo_reg_t domain1_pdos[] = {
    79     {"2", Beckhoff_EL4132_Output1, &r_ana_out},
    70     {"2", Beckhoff_EL2004_Outputs, &r_dig_out},
    80     {}
    71     {}
    81 };
    72 };
    82 
    73 
    83 /*****************************************************************************/
    74 /*****************************************************************************/
    84 
    75 
    85 void run(long data)
    76 void run(long data)
    86 {
    77 {
    87     while (1)
    78     static unsigned int blink = 0;
    88     {
    79     static unsigned int counter = 0;
       
    80 
       
    81     while (1) {
    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         EC_WRITE_U8(r_dig_out, blink ? 0x0F : 0x00);
    97 
    91 
       
    92         rt_sem_wait(&master_sem);
       
    93         ecrt_domain_queue(domain1);
    98         ecrt_master_run(master);
    94         ecrt_master_run(master);
    99         ecrt_master_send(master);
    95         ecrt_master_send(master);
   100 
       
   101         rt_sem_signal(&master_sem);
    96         rt_sem_signal(&master_sem);
       
    97 		
       
    98         if (counter) {
       
    99             counter--;
       
   100         }
       
   101         else {
       
   102             counter = FREQUENCY;
       
   103             blink = !blink;
       
   104         }
       
   105 
   102         rt_task_wait_period();
   106         rt_task_wait_period();
   103     }
   107     }
   104 }
   108 }
   105 
   109 
   106 /*****************************************************************************/
   110 /*****************************************************************************/
   107 
   111 
   108 int request_lock(void *data)
   112 int request_lock(void *data)
   109 {
   113 {
   110     // too close to the next RT cycle: deny access...
   114     // too close to the next real time cycle: deny access...
   111     if (get_cycles() - t_last_cycle > t_critical) return -1;
   115     if (get_cycles() - t_last_cycle > t_critical) return -1;
   112 
   116 
   113     // allow access
   117     // allow access
   114     rt_sem_wait(&master_sem);
   118     rt_sem_wait(&master_sem);
   115     return 0;
   119     return 0;
   137     if (!(master = ecrt_request_master(0))) {
   141     if (!(master = ecrt_request_master(0))) {
   138         printk(KERN_ERR "Requesting master 0 failed!\n");
   142         printk(KERN_ERR "Requesting master 0 failed!\n");
   139         goto out_return;
   143         goto out_return;
   140     }
   144     }
   141 
   145 
   142 
       
   143     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   146     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   144 
   147 
   145     printk(KERN_INFO "Registering domain...\n");
   148     printk(KERN_INFO "Creating domain...\n");
   146     if (!(domain1 = ecrt_master_create_domain(master))) {
   149     if (!(domain1 = ecrt_master_create_domain(master))) {
   147         printk(KERN_ERR "Domain creation failed!\n");
   150         printk(KERN_ERR "Domain creation failed!\n");
   148         goto out_release_master;
   151         goto out_release_master;
   149     }
   152     }
   150 
   153 
   157     printk(KERN_INFO "Activating master...\n");
   160     printk(KERN_INFO "Activating master...\n");
   158     if (ecrt_master_activate(master)) {
   161     if (ecrt_master_activate(master)) {
   159         printk(KERN_ERR "Failed to activate master!\n");
   162         printk(KERN_ERR "Failed to activate master!\n");
   160         goto out_release_master;
   163         goto out_release_master;
   161     }
   164     }
   162 
       
   163     ecrt_master_prepare(master);
       
   164 
   165 
   165     printk("Starting cyclic sample thread...\n");
   166     printk("Starting cyclic sample thread...\n");
   166     requested_ticks = nano2count(TIMERTICKS);
   167     requested_ticks = nano2count(TIMERTICKS);
   167     tick_period = start_rt_timer(requested_ticks);
   168     tick_period = start_rt_timer(requested_ticks);
   168     printk(KERN_INFO "RT timer started with %i/%i ticks.\n",
   169     printk(KERN_INFO "RT timer started with %i/%i ticks.\n",
   184 
   185 
   185  out_stop_task:
   186  out_stop_task:
   186     rt_task_delete(&task);
   187     rt_task_delete(&task);
   187  out_stop_timer:
   188  out_stop_timer:
   188     stop_rt_timer();
   189     stop_rt_timer();
   189     ecrt_master_deactivate(master);
       
   190  out_release_master:
   190  out_release_master:
   191     ecrt_release_master(master);
   191     ecrt_release_master(master);
   192  out_return:
   192  out_return:
   193     rt_sem_delete(&master_sem);
   193     rt_sem_delete(&master_sem);
   194     return -1;
   194     return -1;
   208     printk(KERN_INFO "=== EtherCAT RTAI sample module stopped. ===\n");
   208     printk(KERN_INFO "=== EtherCAT RTAI sample module stopped. ===\n");
   209 }
   209 }
   210 
   210 
   211 /*****************************************************************************/
   211 /*****************************************************************************/
   212 
   212 
       
   213 MODULE_LICENSE("GPL");
       
   214 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
       
   215 MODULE_DESCRIPTION("EtherCAT RTAI sample module");
       
   216 
   213 module_init(init_mod);
   217 module_init(init_mod);
   214 module_exit(cleanup_mod);
   218 module_exit(cleanup_mod);
   215 
   219 
   216 /*****************************************************************************/
   220 /*****************************************************************************/
   217