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