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