examples/user/main.c
changeset 1258 900f1124e8f8
parent 1255 38b7e05b20c1
child 1259 5f9d1abbee71
equal deleted inserted replaced
1257:9844ac126275 1258:900f1124e8f8
    44 
    44 
    45 #include "ecrt.h"
    45 #include "ecrt.h"
    46 
    46 
    47 /****************************************************************************/
    47 /****************************************************************************/
    48 
    48 
       
    49 // Application parameters
       
    50 #define PRIORITY 1
       
    51 
    49 // Optional features
    52 // Optional features
    50 #define CONFIGURE_PDOS  1
    53 #define CONFIGURE_PDOS  1
    51 #define EL3152_ALT_PDOS 0
    54 
    52 #define SDO_ACCESS      0
    55 /****************************************************************************/
    53 #define VOE_ACCESS      0
    56 
    54 
    57 // EtherCAT
    55 #define PRIORITY 1
    58 static ec_master_t *master = NULL;
       
    59 
       
    60 static ec_domain_t *domain1 = NULL;
       
    61 
       
    62 static ec_slave_config_t *sc_ana_in = NULL;
       
    63 
       
    64 // Timer
       
    65 static unsigned int sig_alarms = 0;
       
    66 static unsigned int user_alarms = 0;
       
    67 
       
    68 /****************************************************************************/
       
    69 
       
    70 // process data
       
    71 static uint8_t *domain1_pd = NULL;
    56 
    72 
    57 #define BusCouplerPos  0, 0
    73 #define BusCouplerPos  0, 0
    58 #define AnaOutSlavePos 0, 1
    74 #define AnaOutSlavePos 0, 1
    59 #define AnaInSlavePos  0, 2
    75 #define AnaInSlavePos  0, 2
    60 #define DigOutSlavePos 0, 3
    76 #define DigOutSlavePos 0, 3
    62 #define Beckhoff_EK1100 0x00000002, 0x044C2C52
    78 #define Beckhoff_EK1100 0x00000002, 0x044C2C52
    63 #define Beckhoff_EL2004 0x00000002, 0x07D43052
    79 #define Beckhoff_EL2004 0x00000002, 0x07D43052
    64 #define Beckhoff_EL3152 0x00000002, 0x0c503052
    80 #define Beckhoff_EL3152 0x00000002, 0x0c503052
    65 #define Beckhoff_EL4102 0x00000002, 0x10063052
    81 #define Beckhoff_EL4102 0x00000002, 0x10063052
    66 
    82 
    67 /****************************************************************************/
       
    68 
       
    69 static unsigned int sig_alarms = 0;
       
    70 static unsigned int user_alarms = 0;
       
    71 
       
    72 // offsets for Pdo entries
    83 // offsets for Pdo entries
    73 static unsigned int off_ana_in;
    84 static unsigned int off_ana_in_status;
       
    85 static unsigned int off_ana_in_value;
    74 static unsigned int off_ana_out;
    86 static unsigned int off_ana_out;
    75 static unsigned int off_dig_out;
    87 static unsigned int off_dig_out;
    76 
    88 
    77 const static ec_pdo_entry_reg_t domain1_regs[] = {
    89 const static ec_pdo_entry_reg_t domain1_regs[] = {
    78 #if EL3152_ALT_PDOS
    90     {AnaInSlavePos,  Beckhoff_EL3152, 0x3101, 1, &off_ana_in_status},
    79     {AnaInSlavePos,  Beckhoff_EL3152, 0x6401, 1, &off_ana_in},
    91     {AnaInSlavePos,  Beckhoff_EL3152, 0x3101, 2, &off_ana_in_value},
    80 #else
       
    81     {AnaInSlavePos,  Beckhoff_EL3152, 0x3101, 2, &off_ana_in},
       
    82 #endif
       
    83 	{AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
    92 	{AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
    84 	{DigOutSlavePos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
    93 	{DigOutSlavePos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
    85 	{}
    94 	{}
    86 };
    95 };
    87 
       
    88 static ec_slave_config_t *sc_ana_in = NULL;
       
    89 
    96 
    90 /*****************************************************************************/
    97 /*****************************************************************************/
    91 
    98 
    92 #if CONFIGURE_PDOS
    99 #if CONFIGURE_PDOS
    93 
   100 
   100     {0x3102, 2, 16}, // channel 2 value
   107     {0x3102, 2, 16}, // channel 2 value
   101     {0x6401, 1, 16}, // channel 1 value (alt.)
   108     {0x6401, 1, 16}, // channel 1 value (alt.)
   102     {0x6401, 2, 16}  // channel 2 value (alt.)
   109     {0x6401, 2, 16}  // channel 2 value (alt.)
   103 };
   110 };
   104 
   111 
   105 #if EL3152_ALT_PDOS
       
   106 static ec_pdo_info_t el3152_pdos[] = {
       
   107     {0x1A10, 2, el3152_pdo_entries + 4},
       
   108 };
       
   109 
       
   110 static ec_sync_info_t el3152_syncs[] = {
       
   111     {2, EC_DIR_OUTPUT},
       
   112     {3, EC_DIR_INPUT, 1, el3152_pdos},
       
   113     {0xff}
       
   114 };
       
   115 #else
       
   116 static ec_pdo_info_t el3152_pdos[] = {
   112 static ec_pdo_info_t el3152_pdos[] = {
   117     {0x1A00, 2, el3152_pdo_entries},
   113     {0x1A00, 2, el3152_pdo_entries},
   118     {0x1A01, 2, el3152_pdo_entries + 2}
   114     {0x1A01, 2, el3152_pdo_entries + 2}
   119 };
   115 };
   120 
   116 
   121 static ec_sync_info_t el3152_syncs[] = {
   117 static ec_sync_info_t el3152_syncs[] = {
   122     {2, EC_DIR_OUTPUT},
   118     {2, EC_DIR_OUTPUT},
   123     {3, EC_DIR_INPUT, 2, el3152_pdos},
   119     {3, EC_DIR_INPUT, 2, el3152_pdos},
   124     {0xff}
   120     {0xff}
   125 };
   121 };
   126 #endif
       
   127 
   122 
   128 // Analog out -------------------------
   123 // Analog out -------------------------
   129 
   124 
   130 static ec_pdo_entry_info_t el4102_pdo_entries[] = {
   125 static ec_pdo_entry_info_t el4102_pdo_entries[] = {
   131     {0x3001, 1, 16}, // channel 1 value
   126     {0x3001, 1, 16}, // channel 1 value
   166 };
   161 };
   167 #endif
   162 #endif
   168 
   163 
   169 /****************************************************************************/
   164 /****************************************************************************/
   170 
   165 
       
   166 void cyclic_task()
       
   167 {
       
   168     ecrt_master_receive(master);
       
   169     ecrt_domain_process(domain1);
       
   170 
       
   171 #if 0
       
   172 #if EL3152_ALT_PDOS
       
   173     printf("AnaIn: value %u\n",
       
   174             EC_READ_U16(domain1_pd + off_ana_in_value));
       
   175 #else
       
   176     printf("AnaIn: state %u value %u\n",
       
   177             EC_READ_U8(domain1_pd + off_ana_in_status),
       
   178             EC_READ_U16(domain1_pd + off_ana_in_value));
       
   179 #endif
       
   180 #endif
       
   181 
       
   182     ecrt_domain_queue(domain1);
       
   183     ecrt_master_send(master);
       
   184 }
       
   185 
       
   186 /****************************************************************************/
       
   187 
   171 void signal_handler(int signum) {
   188 void signal_handler(int signum) {
   172     switch (signum) {
   189     switch (signum) {
   173         case SIGALRM:
   190         case SIGALRM:
   174             sig_alarms++;
   191             sig_alarms++;
   175             break;
   192             break;
   178 
   195 
   179 /****************************************************************************/
   196 /****************************************************************************/
   180 
   197 
   181 int main(int argc, char **argv)
   198 int main(int argc, char **argv)
   182 {
   199 {
   183 	ec_master_t *master;
       
   184 	ec_domain_t *domain1;
       
   185 	ec_slave_config_t *sc;
   200 	ec_slave_config_t *sc;
   186     struct sigaction sa;
   201     struct sigaction sa;
   187     struct itimerval tv;
   202     struct itimerval tv;
   188     
   203     
   189     master = ecrt_request_master(0);
   204     master = ecrt_request_master(0);
   241     }
   256     }
   242 
   257 
   243     printf("Activating master...\n");
   258     printf("Activating master...\n");
   244     if (ecrt_master_activate(master))
   259     if (ecrt_master_activate(master))
   245         return -1;
   260         return -1;
       
   261 
       
   262     if (!(domain1_pd = ecrt_domain_data(domain1))) {
       
   263         return -1;
       
   264     }
   246 
   265 
   247 #if PRIORITY
   266 #if PRIORITY
   248     pid_t pid = getpid();
   267     pid_t pid = getpid();
   249     if (setpriority(PRIO_PROCESS, pid, -19))
   268     if (setpriority(PRIO_PROCESS, pid, -19))
   250         fprintf(stderr, "Warning: Failed to set priority: %s\n",
   269         fprintf(stderr, "Warning: Failed to set priority: %s\n",
   269         return 1;
   288         return 1;
   270     }
   289     }
   271 
   290 
   272     printf("Started.\n");
   291     printf("Started.\n");
   273 	while (1) {
   292 	while (1) {
   274         sleep(1);
   293         sleep(1); // always interrupted by SIGALRM
   275 
   294 
   276 #if 0
   295 #if 0
   277         struct timeval t;
   296         struct timeval t;
   278         gettimeofday(&t, NULL);
   297         gettimeofday(&t, NULL);
   279         printf("%u.%06u\n", t.tv_sec, t.tv_usec);
   298         printf("%u.%06u\n", t.tv_sec, t.tv_usec);
   280 #endif
   299 #endif
   281 
   300 
   282         while (sig_alarms != user_alarms) {
   301         while (sig_alarms != user_alarms) {
   283             ecrt_master_receive(master);
   302             cyclic_task();
   284             ecrt_master_send(master);
       
   285 
       
   286             user_alarms++;
   303             user_alarms++;
   287         }
   304         }
   288 	}
   305 	}
   289 
   306 
   290 	return 0;
   307 	return 0;