etherlab/plc_etherlab.c
changeset 2022 c2295d311402
child 2024 08d67a0af2c1
child 2025 87f428326f9b
equal deleted inserted replaced
-1:000000000000 2022:c2295d311402
       
     1 /*
       
     2  * Etherlab Asynchronous execution code
       
     3  *
       
     4  * */
       
     5 
       
     6 #include <rtdm/rtdm.h>
       
     7 #include <native/task.h>
       
     8 #include <native/timer.h>
       
     9 
       
    10 #include "ecrt.h"
       
    11 #include "ec_rtdm.h"
       
    12 
       
    13 #ifdef _WINDOWS_H
       
    14   #include "iec_types.h"
       
    15 #else
       
    16   #include "iec_std_lib.h"
       
    17 #endif
       
    18 
       
    19 // declaration of interface variables
       
    20 %(located_variables_declaration)s
       
    21 
       
    22 // Optional features
       
    23 #define CONFIGURE_PDOS  %(configure_pdos)d
       
    24 
       
    25 // process data
       
    26 static uint8_t *domain1_pd = NULL;
       
    27 %(used_pdo_entry_offset_variables_declaration)s
       
    28 
       
    29 const static ec_pdo_entry_reg_t domain1_regs[] = {
       
    30 %(used_pdo_entry_configuration)s
       
    31     {}
       
    32 };
       
    33 /*****************************************************************************/
       
    34 
       
    35 #if CONFIGURE_PDOS
       
    36 %(pdos_configuration_declaration)s
       
    37 #endif
       
    38 
       
    39 int rt_fd = -1;
       
    40 CstructMstrAttach MstrAttach;
       
    41 char rt_dev_file[64];
       
    42 long long wait_period_ns = 100000LL;
       
    43 
       
    44 // EtherCAT
       
    45 static ec_master_t *master = NULL;
       
    46 static ec_domain_t *domain1 = NULL;
       
    47 %(slaves_declaration)s
       
    48 
       
    49 /* Beremiz plugin functions */
       
    50 int __init_%(location)s(int argc,char **argv)
       
    51 {
       
    52 	int rtstatus;
       
    53 
       
    54 	MstrAttach.masterindex = %(master_number)d;
       
    55 
       
    56 	master = ecrt_request_master(MstrAttach.masterindex);
       
    57 	if (!master) return -1;
       
    58 
       
    59 	domain1 = ecrt_master_create_domain(master);
       
    60 	if (!domain1) return -1;
       
    61 
       
    62 #if CONFIGURE_PDOS
       
    63 	%(slaves_configuration)s
       
    64 #endif
       
    65 
       
    66     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
       
    67         fprintf(stderr, "PDO entry registration failed!\n");
       
    68         return -1;
       
    69     }
       
    70 
       
    71     sprintf(&rt_dev_file[0],"%%s%%u",EC_RTDM_DEV_FILE_NAME,0);
       
    72     rt_fd = rt_dev_open( &rt_dev_file[0], 0);
       
    73     if (rt_fd < 0) {
       
    74         printf("Can't open %%s\n", &rt_dev_file[0]);
       
    75         return -1;
       
    76     }
       
    77 
       
    78     // attach the master over rtdm driver
       
    79     MstrAttach.domainindex = ecrt_domain_index(domain1);
       
    80     rtstatus = ecrt_rtdm_master_attach(rt_fd, &MstrAttach);
       
    81     if (rtstatus < 0) {
       
    82         printf("Cannot attach to master over rtdm\n");
       
    83         return -1;
       
    84     }
       
    85 
       
    86     if (ecrt_master_activate(master))
       
    87         return -1;
       
    88 
       
    89     if (!(domain1_pd = ecrt_domain_data(domain1))) {
       
    90         fprintf(stderr, "domain1_pd:  0x%%.6lx\n", (unsigned long)domain1_pd);
       
    91         return -1;
       
    92     }
       
    93 
       
    94     fprintf(stdout, "Master %(master_number)d activated...\n");
       
    95     return 0;
       
    96 }
       
    97 
       
    98 void __cleanup_%(location)s(void)
       
    99 {
       
   100 	if (rt_fd >= 0) {
       
   101 		rt_dev_close(rt_fd);
       
   102 	}
       
   103 }
       
   104 
       
   105 void __retrieve_%(location)s(void)
       
   106 {
       
   107     // receive ethercat
       
   108     ecrt_rtdm_master_recieve(rt_fd);
       
   109     ecrt_rtdm_domain_process(rt_fd);
       
   110 
       
   111     rt_task_sleep(rt_timer_ns2tsc(wait_period_ns));
       
   112 
       
   113     // send process data
       
   114     ecrt_rtdm_domain_queque(rt_fd);
       
   115     ecrt_rtdm_master_send(rt_fd);
       
   116 
       
   117 %(retrieve_variables)s
       
   118 }
       
   119 
       
   120 void __publish_%(location)s(void)
       
   121 {
       
   122 %(publish_variables)s
       
   123 }