Good morning, I’m trying to create a simple application equal to the “user” example provided with the EtherCAT master documentation (the code is attached at the end of this message), but in a kernel version. I’ve adopted the basic environment of the “mini”example but i cannot include the “/usr/include” directory (where lies the time.h header, useful for the clock_gettime function). Could you give me any hints in order to solve this problem?
Thank you for your help. Tommaso #include <linux/version.h> #include <linux/module.h> #include <linux/timer.h> #include <linux/interrupt.h> #include <linux/err.h> //#include <linux/time.h> #include <time.h> #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) #include <linux/slab.h> #endif #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) #include <linux/semaphore.h> #else #include <asm/semaphore.h> #endif #include "/usr/local/etherlab/src/ethercat-1.5.2/include/ecrt.h" // EtherCAT realtime interface /*****************************************************************************/ // Module parameters #define FREQUENCY 1000 // Optional features #define EXTERNAL_MEMORY 1 #define CLOCK_TO_USE CLOCK_REALTIME #define MEASURE_TIMING /****************************************************************************/ #define NSEC_PER_SEC (1000000000L) #define PERIOD_NS (NSEC_PER_SEC / FREQUENCY) #define DIFF_NS(A, B) (((B).tv_sec - (A).tv_sec) * NSEC_PER_SEC + (B).tv_nsec - (A).tv_nsec) #define TIMESPEC2NS(T) ((uint64_t) (T).tv_sec * NSEC_PER_SEC + (T).tv_nsec) #define PFX "ec_mini: " /*****************************************************************************/ // EtherCAT static ec_master_t *master = NULL; static ec_master_state_t master_state = {}; struct semaphore master_sem; static ec_domain_t *domain1 = NULL; static ec_domain_state_t domain1_state = {}; // Timer static struct timer_list timer; /*****************************************************************************/ // process data static uint8_t *domain1_pd; // process data memory #define BusCouplerPos 0, 0 #define DigOutSlavePos 0, 1 #define AnaOutSlavePos 0, 6 #define Beckhoff_EK1100 0x00000002, 0x044c2c52 #define Beckhoff_EL2004 0x00000002, 0x07d43052 #define Beckhoff_EL4002 0x00000002, 0x0fa23052 // offsets for PDO entries static unsigned int off_ana_out; static unsigned int off_dig_out; /*const static ec_pdo_entry_reg_t domain1_regs[] = { {AnaOutSlavePos, Beckhoff_EL4002, 0x7000, 1, &off_ana_out}, {DigOutSlavePos, Beckhoff_EL2004, 0x7000, 1, &off_dig_out}, {} };*/ static unsigned int counter = 0; static unsigned int blink = 0; static unsigned int sync_ref_counter = 0; const struct timespec cycletime = {0, PERIOD_NS}; /*****************************************************************************/ // Analog out ------------------------- ec_pdo_entry_info_t el4002_pdo_entries[] = { {0x7000, 0x01, 16}, /* Analog output */ {0x7010, 0x01, 16}, /* Analog output */ }; ec_pdo_info_t el4002_pdos[] = { {0x1600, 1, el4002_pdo_entries + 0}, /* RxPDO-Map OutputsCh.1 */ {0x1601, 1, el4002_pdo_entries + 1}, /* RxPDO-Map OutputsCh.2 */ }; ec_sync_info_t el4002_syncs[] = { {2, EC_DIR_OUTPUT, 2, el4002_pdos + 0, EC_WD_DISABLE}, {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE}, {0xff} }; // Digital out ------------------------ ec_pdo_entry_info_t el2004_channels[] = { {0x7000, 0x01, 1}, /* Output */ {0x7010, 0x01, 1}, /* Output */ {0x7020, 0x01, 1}, /* Output */ {0x7030, 0x01, 1}, /* Output */ }; ec_pdo_info_t el2004_pdos[] = { {0x1600, 1, &el2004_channels[0]}, {0x1601, 1, &el2004_channels[1]}, {0x1602, 1, &el2004_channels[2]}, {0x1603, 1, &el2004_channels[3]} }; ec_sync_info_t el2004_syncs[] = { {0, EC_DIR_OUTPUT, 4, el2004_pdos + 0, EC_WD_ENABLE}, {1, EC_DIR_INPUT}, {0xff} }; /*****************************************************************************/ struct timespec timespec_add(struct timespec time1, struct timespec time2) { struct timespec result; if ((time1.tv_nsec + time2.tv_nsec) >= NSEC_PER_SEC) { result.tv_sec = time1.tv_sec + time2.tv_sec + 1; result.tv_nsec = time1.tv_nsec + time2.tv_nsec - NSEC_PER_SEC; } else { result.tv_sec = time1.tv_sec + time2.tv_sec; result.tv_nsec = time1.tv_nsec + time2.tv_nsec; } return result; } /*****************************************************************************/ void check_domain1_state(void) { ec_domain_state_t ds; down(&master_sem); ecrt_domain_state(domain1, &ds); up(&master_sem); if (ds.working_counter != domain1_state.working_counter) printk(KERN_INFO PFX "Domain1: WC %u.\n", ds.working_counter); if (ds.wc_state != domain1_state.wc_state) printk(KERN_INFO PFX "Domain1: State %u.\n", ds.wc_state); domain1_state = ds; } /*****************************************************************************/ void check_master_state(void) { ec_master_state_t ms; down(&master_sem); ecrt_master_state(master, &ms); up(&master_sem); if (ms.slaves_responding != master_state.slaves_responding) printk(KERN_INFO PFX "%u slave(s).\n", ms.slaves_responding); if (ms.al_states != master_state.al_states) printk(KERN_INFO PFX "AL states: 0x%02X.\n", ms.al_states); if (ms.link_up != master_state.link_up) printk(KERN_INFO PFX "Link is %s.\n", ms.link_up ? "up" : "down"); master_state = ms; } /*****************************************************************************/ void cyclic_task(unsigned long data) { struct timespec wakeupTime, time; #ifdef MEASURE_TIMING struct timespec startTime, endTime, lastStartTime = {}; uint32_t period_ns = 0, exec_ns = 0, latency_ns = 0, latency_min_ns = 0, latency_max_ns = 0, period_min_ns = 0, period_max_ns = 0, exec_min_ns = 0, exec_max_ns = 0; // get current time from the clock set in "CLOCK_TO_USE" and put it into wakeupTime clock_gettime(CLOCK_TO_USE, &startTime); latency_ns = DIFF_NS(wakeupTime, startTime); period_ns = DIFF_NS(lastStartTime, startTime); exec_ns = DIFF_NS(lastStartTime, endTime); lastStartTime = startTime; if (latency_ns > latency_max_ns) { latency_max_ns = latency_ns; } if (latency_ns < latency_min_ns) { latency_min_ns = latency_ns; } if (period_ns > period_max_ns) { period_max_ns = period_ns; } if (period_ns < period_min_ns) { period_min_ns = period_ns; } if (exec_ns > exec_max_ns) { exec_max_ns = exec_ns; } if (exec_ns < exec_min_ns) { exec_min_ns = exec_ns; } #endif // receive process data down(&master_sem); ecrt_master_receive(master); ecrt_domain_process(domain1); up(&master_sem); // check process data state (optional) check_domain1_state(); if (counter) { counter--; } else { counter = FREQUENCY; // check for master state (optional) check_master_state(); #ifdef MEASURE_TIMING // output timing stats printk(KERN_INFO PFX "period %10u ... %10u\n", period_min_ns, period_max_ns); printk(KERN_INFO PFX "exec %10u ... %10u\n", exec_min_ns, exec_max_ns); printk(KERN_INFO PFX "latency %10u ... %10u\n", latency_min_ns, latency_max_ns); period_max_ns = 0; period_min_ns = 0xffffffff; exec_max_ns = 0; exec_min_ns = 0xffffffff; latency_max_ns = 0; latency_min_ns = 0xffffffff; #endif // calculate new process data blink = !blink; } // write process data EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x06 : 0x09); // write application time to master clock_gettime(CLOCK_TO_USE, &time); ecrt_master_application_time(master, TIMESPEC2NS(time)); if (sync_ref_counter) { sync_ref_counter--; } else { sync_ref_counter = 1; // sync every cycle ecrt_master_sync_reference_clock(master); } ecrt_master_sync_slave_clocks(master); // send process data down(&master_sem); ecrt_domain_queue(domain1); ecrt_master_send(master); up(&master_sem); #ifdef MEASURE_TIMING clock_gettime(CLOCK_TO_USE, &endTime); #endif // restart timer timer.expires += HZ/FREQUENCY; add_timer(&timer); #ifdef MEASURE_TIMING clock_gettime(CLOCK_TO_USE, &wakeupTime); #endif } /*****************************************************************************/ void send_callback(void *cb_data) { ec_master_t *m = (ec_master_t *) cb_data; down(&master_sem); ecrt_master_send_ext(m); up(&master_sem); } /*****************************************************************************/ void receive_callback(void *cb_data) { ec_master_t *m = (ec_master_t *) cb_data; down(&master_sem); ecrt_master_receive(m); up(&master_sem); } /*****************************************************************************/ int __init init_mini_module(void) { int ret = -1; ec_slave_config_t *sc; #if EXTERNAL_MEMORY unsigned int size; #endif printk(KERN_INFO PFX "Starting...\n"); master = ecrt_request_master(0); if (!master) { ret = -EBUSY; printk(KERN_ERR PFX "Requesting master 0 failed.\n"); goto out_return; } sema_init(&master_sem, 1); ecrt_master_callbacks(master, send_callback, receive_callback, master); printk(KERN_INFO PFX "Registering domain...\n"); if (!(domain1 = ecrt_master_create_domain(master))) { printk(KERN_ERR PFX "Domain creation failed!\n"); goto out_release_master; } // Create configuration for bus coupler sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); if (!sc) goto out_release_master; //Create configuration for digital output if (!(sc = ecrt_master_slave_config(master, DigOutSlavePos, Beckhoff_EL2004))) { printk(KERN_ERR PFX "Failed to get slave configuration.\n"); goto out_release_master; } off_dig_out = ecrt_slave_config_reg_pdo_entry(sc, 0x7000, 1, domain1, NULL); if (off_dig_out < 0) goto out_release_master; ecrt_slave_config_dc(sc, 0x0300, PERIOD_NS, 4400000, 0, 0); if (!(sc = ecrt_master_slave_config(master, AnaOutSlavePos, Beckhoff_EL4002))) { printk(KERN_ERR PFX "Failed to get slave configuration.\n"); goto out_release_master; } off_ana_out = ecrt_slave_config_reg_pdo_entry(sc, 0x7000, 1, domain1, NULL); if (off_dig_out < 0) goto out_release_master; // configure SYNC signals for this slave ecrt_slave_config_dc(sc, 0x0700, PERIOD_NS, 4400000, 0, 0); /* if (!(sc = ecrt_master_slave_config(master, AnaOutSlavePos, Beckhoff_EL4002))) { printk(KERN_ERR PFX "Failed to get slave configuration.\n"); goto out_release_master; } if (ecrt_slave_config_pdos(sc, EC_END, el4002_syncs)) { printk(KERN_ERR PFX "Failed to configure PDOs.\n"); goto out_release_master; } ecrt_slave_config_dc(sc, 0x0700, PERIOD_NS, 4400000, 0, 0); if (!(sc = ecrt_master_slave_config(master, DigOutSlavePos, Beckhoff_EL2004))) { printk(KERN_ERR PFX "Failed to get slave configuration.\n"); goto out_release_master; } if (ecrt_slave_config_pdos(sc, EC_END, el2004_syncs)) { printk(KERN_ERR PFX "Failed to configure PDOs.\n"); goto out_release_master; } ecrt_slave_config_dc(sc, 0x0300, PERIOD_NS, 4400000, 0, 0); printk(KERN_INFO PFX "Registering PDO entries...\n"); if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) { printk(KERN_ERR PFX "PDO entry registration failed!\n"); goto out_release_master; } */ #if EXTERNAL_MEMORY if ((size = ecrt_domain_size(domain1))) { if (!(domain1_pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) { printk(KERN_ERR PFX "Failed to allocate %u bytes of process data memory!\n", size); goto out_release_master; } ecrt_domain_external_memory(domain1, domain1_pd); } #endif printk(KERN_INFO PFX "Activating master...\n"); if (ecrt_master_activate(master)) { printk(KERN_ERR PFX "Failed to activate master!\n"); #if EXTERNAL_MEMORY goto out_free_process_data; #else goto out_release_master; #endif } #if !EXTERNAL_MEMORY // Get internal process data for domain domain1_pd = ecrt_domain_data(domain1); #endif printk(KERN_INFO PFX "Starting cyclic sample thread.\n"); init_timer(&timer); timer.function = cyclic_task; timer.expires = jiffies + 10; add_timer(&timer); printk(KERN_INFO PFX "Started.\n"); return 0; #if EXTERNAL_MEMORY out_free_process_data: kfree(domain1_pd); #endif out_release_master: printk(KERN_ERR PFX "Releasing master...\n"); ecrt_release_master(master); out_return: printk(KERN_ERR PFX "Failed to load. Aborting.\n"); return ret; } /*****************************************************************************/ void __exit cleanup_mini_module(void) { printk(KERN_INFO PFX "Stopping...\n"); del_timer_sync(&timer); #if EXTERNAL_MEMORY kfree(domain1_pd); #endif printk(KERN_INFO PFX "Releasing master...\n"); ecrt_release_master(master); printk(KERN_INFO PFX "Unloading.\n"); } /*****************************************************************************/ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Florian Pose <f...@igh-essen.com>"); MODULE_DESCRIPTION("EtherCAT minimal test environment"); module_init(init_mini_module); module_exit(cleanup_mini_module); /*****************************************************************************/
_______________________________________________ etherlab-users mailing list etherlab-users@etherlab.org http://lists.etherlab.org/mailman/listinfo/etherlab-users