examples/xenomai_posix/main.c
branchstable-1.5
changeset 2433 3bdd7a747fae
child 2705 c6e3c6d66af0
equal deleted inserted replaced
2432:f4313f5aba88 2433:3bdd7a747fae
       
     1 /******************************************************************************
       
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C)      2011  IgH Andreas Stewering-Bone
       
     6  *                     2012  Florian Pose <fp@igh-essen.com>
       
     7  *
       
     8  *  This file is part of the IgH EtherCAT master
       
     9  *
       
    10  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
       
    11  *  modify it under the terms of the GNU General Public License version 2, as
       
    12  *  published by the Free Software Foundation.
       
    13  *
       
    14  *  The IgH EtherCAT master is distributed in the hope that it will be useful,
       
    15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    17  *  Public License for more details.
       
    18  *
       
    19  *  You should have received a copy of the GNU General Public License along
       
    20  *  with the IgH EtherCAT master. If not, see <http://www.gnu.org/licenses/>.
       
    21  *
       
    22  *  ---
       
    23  *
       
    24  *  The license mentioned above concerns the source code only. Using the
       
    25  *  EtherCAT technology and brand is only permitted in compliance with the
       
    26  *  industrial property and similar rights of Beckhoff Automation GmbH.
       
    27  *
       
    28  *****************************************************************************/
       
    29 
       
    30 #include <errno.h>
       
    31 #include <mqueue.h>
       
    32 #include <signal.h>
       
    33 #include <pthread.h>
       
    34 #include <stdio.h>
       
    35 #include <stdlib.h>
       
    36 #include <string.h>
       
    37 #include <unistd.h>
       
    38 #include <limits.h>
       
    39 #include <sys/ioctl.h>
       
    40 #include <sys/mman.h>
       
    41 #include <time.h>
       
    42 
       
    43 #include <rtdm/rtdm.h>
       
    44 #include <rtdk.h>
       
    45 
       
    46 #include "ecrt.h"
       
    47 
       
    48 #define NSEC_PER_SEC 1000000000
       
    49 
       
    50 static unsigned int cycle_us = 1000; /* 1 ms */
       
    51 
       
    52 static pthread_t cyclic_thread;
       
    53 
       
    54 static int run = 1;
       
    55 
       
    56 /****************************************************************************/
       
    57 
       
    58 // EtherCAT
       
    59 static ec_master_t *master = NULL;
       
    60 static ec_master_state_t master_state = {};
       
    61 
       
    62 static ec_domain_t *domain1 = NULL;
       
    63 static ec_domain_state_t domain1_state = {};
       
    64 
       
    65 static uint8_t *domain1_pd = NULL;
       
    66 
       
    67 static ec_slave_config_t *sc_dig_out_01 = NULL;
       
    68 
       
    69 /****************************************************************************/
       
    70 
       
    71 // process data
       
    72 
       
    73 #define BusCoupler01_Pos  0, 0
       
    74 #define DigOutSlave01_Pos 0, 1
       
    75 
       
    76 #define Beckhoff_EK1100 0x00000002, 0x044c2c52
       
    77 #define Beckhoff_EL2004 0x00000002, 0x07d43052
       
    78 
       
    79 // offsets for PDO entries
       
    80 static unsigned int off_dig_out0 = 0;
       
    81 
       
    82 // process data
       
    83 
       
    84 const static ec_pdo_entry_reg_t domain1_regs[] = {
       
    85    {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7000, 0x01, &off_dig_out0, NULL},
       
    86    {}
       
    87 };
       
    88 
       
    89 /****************************************************************************/
       
    90 
       
    91 /* Slave 1, "EL2004"
       
    92  * Vendor ID:       0x00000002
       
    93  * Product code:    0x07d43052
       
    94  * Revision number: 0x00100000
       
    95  */
       
    96 
       
    97 ec_pdo_entry_info_t slave_1_pdo_entries[] = {
       
    98    {0x7000, 0x01, 1}, /* Output */
       
    99    {0x7010, 0x01, 1}, /* Output */
       
   100    {0x7020, 0x01, 1}, /* Output */
       
   101    {0x7030, 0x01, 1}, /* Output */
       
   102 };
       
   103 
       
   104 ec_pdo_info_t slave_1_pdos[] = {
       
   105    {0x1600, 1, slave_1_pdo_entries + 0}, /* Channel 1 */
       
   106    {0x1601, 1, slave_1_pdo_entries + 1}, /* Channel 2 */
       
   107    {0x1602, 1, slave_1_pdo_entries + 2}, /* Channel 3 */
       
   108    {0x1603, 1, slave_1_pdo_entries + 3}, /* Channel 4 */
       
   109 };
       
   110 
       
   111 ec_sync_info_t slave_1_syncs[] = {
       
   112    {0, EC_DIR_OUTPUT, 4, slave_1_pdos + 0, EC_WD_ENABLE},
       
   113    {0xff}
       
   114 };
       
   115 
       
   116 /*****************************************************************************
       
   117  * Realtime task
       
   118  ****************************************************************************/
       
   119 
       
   120 void rt_check_domain_state(void)
       
   121 {
       
   122     ec_domain_state_t ds = {};
       
   123 
       
   124 	ecrt_domain_state(domain1, &ds);
       
   125 
       
   126     if (ds.working_counter != domain1_state.working_counter) {
       
   127         rt_printf("Domain1: WC %u.\n", ds.working_counter);
       
   128     }
       
   129 
       
   130     if (ds.wc_state != domain1_state.wc_state) {
       
   131         rt_printf("Domain1: State %u.\n", ds.wc_state);
       
   132     }
       
   133 
       
   134     domain1_state = ds;
       
   135 }
       
   136 
       
   137 /****************************************************************************/
       
   138 
       
   139 void rt_check_master_state(void)
       
   140 {
       
   141     ec_master_state_t ms;
       
   142 
       
   143 	ecrt_master_state(master, &ms);
       
   144 
       
   145     if (ms.slaves_responding != master_state.slaves_responding) {
       
   146         rt_printf("%u slave(s).\n", ms.slaves_responding);
       
   147     }
       
   148 
       
   149     if (ms.al_states != master_state.al_states) {
       
   150         rt_printf("AL states: 0x%02X.\n", ms.al_states);
       
   151     }
       
   152 
       
   153     if (ms.link_up != master_state.link_up) {
       
   154         rt_printf("Link is %s.\n", ms.link_up ? "up" : "down");
       
   155     }
       
   156 
       
   157     master_state = ms;
       
   158 }
       
   159 
       
   160 /****************************************************************************/
       
   161 
       
   162 void *my_thread(void *arg)
       
   163 {
       
   164     struct timespec next_period;
       
   165     int cycle_counter = 0;
       
   166 	unsigned int blink = 0;
       
   167 
       
   168     clock_gettime(CLOCK_REALTIME, &next_period);
       
   169 
       
   170     while (run) {
       
   171         next_period.tv_nsec += cycle_us * 1000;
       
   172         while (next_period.tv_nsec >= NSEC_PER_SEC) {
       
   173 			next_period.tv_nsec -= NSEC_PER_SEC;
       
   174 			next_period.tv_sec++;
       
   175 		}
       
   176 
       
   177         clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_period, NULL);
       
   178 
       
   179         cycle_counter++;
       
   180 
       
   181         // receive EtherCAT
       
   182         ecrt_master_receive(master);
       
   183         ecrt_domain_process(domain1);
       
   184 
       
   185         rt_check_domain_state();
       
   186 
       
   187         if (!(cycle_counter % 1000)) {
       
   188 			rt_check_master_state();
       
   189 		}
       
   190 
       
   191         if (!(cycle_counter % 200)) {
       
   192 			blink = !blink;
       
   193 		}
       
   194 
       
   195         EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0x0 : 0x0F);
       
   196 
       
   197         // send process data
       
   198         ecrt_domain_queue(domain1);
       
   199         ecrt_master_send(master);
       
   200     }
       
   201 
       
   202     return NULL;
       
   203 }
       
   204 
       
   205 /****************************************************************************
       
   206  * Signal handler
       
   207  ***************************************************************************/
       
   208 
       
   209 void signal_handler(int sig)
       
   210 {
       
   211     run = 0;
       
   212 }
       
   213 
       
   214 /****************************************************************************
       
   215  * Main function
       
   216  ***************************************************************************/
       
   217 
       
   218 int main(int argc, char *argv[])
       
   219 {
       
   220     ec_slave_config_t *sc;
       
   221     int ret;
       
   222 
       
   223     signal(SIGTERM, signal_handler);
       
   224     signal(SIGINT, signal_handler);
       
   225 
       
   226     mlockall(MCL_CURRENT | MCL_FUTURE);
       
   227 
       
   228     printf("Requesting master...\n");
       
   229     master = ecrt_request_master(0);
       
   230     if (!master) {
       
   231         return -1;
       
   232     }
       
   233 
       
   234     domain1 = ecrt_master_create_domain(master);
       
   235     if (!domain1) {
       
   236         return -1;
       
   237     }
       
   238 
       
   239     printf("Creating slave configurations...\n");
       
   240 
       
   241     // Create configuration for bus coupler
       
   242     sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
       
   243     if (!sc) {
       
   244         return -1;
       
   245     }
       
   246 
       
   247     sc_dig_out_01 =
       
   248         ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2004);
       
   249     if (!sc_dig_out_01) {
       
   250         fprintf(stderr, "Failed to get slave configuration.\n");
       
   251         return -1;
       
   252     }
       
   253 
       
   254     if (ecrt_slave_config_pdos(sc_dig_out_01, EC_END, slave_1_syncs)) {
       
   255         fprintf(stderr, "Failed to configure PDOs.\n");
       
   256         return -1;
       
   257     }
       
   258 
       
   259     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
       
   260         fprintf(stderr, "PDO entry registration failed!\n");
       
   261         return -1;
       
   262     }
       
   263 
       
   264     printf("Activating master...\n");
       
   265     if (ecrt_master_activate(master)) {
       
   266         return -1;
       
   267     }
       
   268 
       
   269     if (!(domain1_pd = ecrt_domain_data(domain1))) {
       
   270         fprintf(stderr, "Failed to get domain data pointer.\n");
       
   271         return -1;
       
   272     }
       
   273 
       
   274     /* Create cyclic RT-thread */
       
   275     struct sched_param param = { .sched_priority = 82 };
       
   276 
       
   277     pthread_attr_t thattr;
       
   278     pthread_attr_init(&thattr);
       
   279     pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
       
   280     pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED);
       
   281     pthread_attr_setschedpolicy(&thattr, SCHED_FIFO);
       
   282     pthread_setschedparam(cyclic_thread, SCHED_FIFO, &param);
       
   283 
       
   284     ret = pthread_create(&cyclic_thread, &thattr, &my_thread, NULL);
       
   285     if (ret) {
       
   286         fprintf(stderr, "%s: pthread_create cyclic task failed\n",
       
   287                 strerror(-ret));
       
   288 		return 1;
       
   289     }
       
   290 
       
   291 	while (run) {
       
   292 		sched_yield();
       
   293 	}
       
   294 
       
   295     pthread_join(cyclic_thread, NULL);
       
   296 
       
   297     printf("End of Program\n");
       
   298     ecrt_release_master(master);
       
   299 
       
   300     return 0;
       
   301 }
       
   302 
       
   303 /****************************************************************************/