etherlab/plc_etherlab.c
changeset 2108 6bcfd50d5a47
parent 2107 6f9aa410a997
child 2116 2b1980a038b1
equal deleted inserted replaced
2107:6f9aa410a997 2108:6bcfd50d5a47
     6 #include <rtdm/rtdm.h>
     6 #include <rtdm/rtdm.h>
     7 #include <native/task.h>
     7 #include <native/task.h>
     8 #include <native/timer.h>
     8 #include <native/timer.h>
     9 
     9 
    10 #include "ecrt.h"
    10 #include "ecrt.h"
    11 #include "ec_rtdm.h"
       
    12 
    11 
    13 #include "beremiz.h"
    12 #include "beremiz.h"
    14 #include "iec_types_all.h"
    13 #include "iec_types_all.h"
    15 
    14 
    16 // declaration of interface variables
    15 // declaration of interface variables
    26 };
    25 };
    27 /*****************************************************************************/
    26 /*****************************************************************************/
    28 
    27 
    29 %(pdos_configuration_declaration)s
    28 %(pdos_configuration_declaration)s
    30 
    29 
    31 int rt_fd = -1;
       
    32 CstructMstrAttach MstrAttach;
       
    33 char rt_dev_file[64];
       
    34 long long wait_period_ns = 100000LL;
    30 long long wait_period_ns = 100000LL;
    35 
    31 
    36 // EtherCAT
    32 // EtherCAT
    37 static ec_master_t *master = NULL;
    33 static ec_master_t *master = NULL;
    38 static ec_domain_t *domain1 = NULL;
    34 static ec_domain_t *domain1 = NULL;
    48 /* Beremiz plugin functions */
    44 /* Beremiz plugin functions */
    49 int __init_%(location)s(int argc,char **argv)
    45 int __init_%(location)s(int argc,char **argv)
    50 {
    46 {
    51     uint32_t abort_code;
    47     uint32_t abort_code;
    52     size_t result_size;
    48     size_t result_size;
    53     int rtstatus;
       
    54     
    49     
    55 	MstrAttach.masterindex = %(master_number)d;
    50 	master = ecrt_request_master(%(master_number)d);
    56 
       
    57 	master = ecrt_request_master(MstrAttach.masterindex);
       
    58 	if (!master) return -1;
    51 	if (!master) return -1;
    59 
    52 
    60 	domain1 = ecrt_master_create_domain(master);
    53 	domain1 = ecrt_master_create_domain(master);
    61 	if (!domain1) return -1;
    54 	if (!domain1) return -1;
    62 
    55 
    74 %(slaves_initialization)s
    67 %(slaves_initialization)s
    75 
    68 
    76     // extracting default value for not mapped entry in output PDOs
    69     // extracting default value for not mapped entry in output PDOs
    77 %(slaves_output_pdos_default_values_extraction)s
    70 %(slaves_output_pdos_default_values_extraction)s
    78 
    71 
    79     sprintf(&rt_dev_file[0],"%%s%%u",EC_RTDM_DEV_FILE_NAME,0);
       
    80     rt_fd = rt_dev_open( &rt_dev_file[0], 0);
       
    81     if (rt_fd < 0) {
       
    82         SLOGF(LOG_CRITICAL, "Can't open %%s\n", &rt_dev_file[0]);
       
    83         return -1;
       
    84     }
       
    85 
       
    86     // attach the master over rtdm driver
       
    87     MstrAttach.domainindex = ecrt_domain_index(domain1);
       
    88     rtstatus = ecrt_rtdm_master_attach(rt_fd, &MstrAttach);
       
    89     if (rtstatus < 0) {
       
    90         SLOGF(LOG_CRITICAL, "Cannot attach to master over rtdm\n");
       
    91         return -1;
       
    92     }
       
    93 
       
    94     if (ecrt_master_activate(master))
    72     if (ecrt_master_activate(master))
    95         return -1;
    73         return -1;
    96 
    74 
    97     if (!(domain1_pd = ecrt_domain_data(domain1))) {
    75     if (!(domain1_pd = ecrt_domain_data(domain1))) {
    98         SLOGF(LOG_CRITICAL, "domain1_pd:  0x%%.6lx\n", (unsigned long)domain1_pd);
    76         SLOGF(LOG_CRITICAL, "domain1_pd:  0x%%.6lx\n", (unsigned long)domain1_pd);
   106     return 0;
    84     return 0;
   107 }
    85 }
   108 
    86 
   109 void __cleanup_%(location)s(void)
    87 void __cleanup_%(location)s(void)
   110 {
    88 {
   111 	if (rt_fd >= 0) {
       
   112 		rt_dev_close(rt_fd);
       
   113 	}
       
   114 	//release master
    89 	//release master
   115 	ecrt_release_master(master);
    90 	ecrt_release_master(master);
   116     first_sent = 0;
    91     first_sent = 0;
   117 }
    92 }
   118 
    93 
   119 void __retrieve_%(location)s(void)
    94 void __retrieve_%(location)s(void)
   120 {
    95 {
   121     // receive ethercat
    96     // receive ethercat
   122     if(first_sent){
    97     if(first_sent){
   123         ecrt_rtdm_master_recieve(rt_fd);
    98         ecrt_master_receive(master);
   124         ecrt_rtdm_domain_process(rt_fd);
    99         ecrt_domain_process(domain1);
   125 %(retrieve_variables)s
   100 %(retrieve_variables)s
   126     }
   101     }
   127 
   102 
   128 }
   103 }
   129 
   104 
   134 static inline RTIME max(RTIME a,RTIME b){return a>b?a:b;}
   109 static inline RTIME max(RTIME a,RTIME b){return a>b?a:b;}
   135 
   110 
   136 void __publish_%(location)s(void)
   111 void __publish_%(location)s(void)
   137 {
   112 {
   138 %(publish_variables)s
   113 %(publish_variables)s
   139     ecrt_rtdm_domain_queque(rt_fd);
   114     ecrt_domain_queue(domain1);
   140     {
   115     {
   141         RTIME current_time = rt_timer_read();
   116         RTIME current_time = rt_timer_read();
   142         // Limit spining max 1/5 of common_ticktime
   117         // Limit spining max 1/5 of common_ticktime
   143         RTIME maxdeadline = current_time + (common_ticktime__ / 5);
   118         RTIME maxdeadline = current_time + (common_ticktime__ / 5);
   144         RTIME deadline = _last_occur ? 
   119         RTIME deadline = _last_occur ? 
   162         if( _max_jitter * 10 < common_ticktime__ && _current_lag < _max_jitter){
   137         if( _max_jitter * 10 < common_ticktime__ && _current_lag < _max_jitter){
   163             //Consuming security margin ?
   138             //Consuming security margin ?
   164             _last_occur = current_time; //Drift forward
   139             _last_occur = current_time; //Drift forward
   165         }
   140         }
   166     }
   141     }
   167     ecrt_rtdm_master_send(rt_fd);
   142     ecrt_master_send(master);
   168     first_sent = 1;
   143     first_sent = 1;
   169 }
   144 }