examples/msr/msr_sample.c
changeset 325 7833cf70c4f2
parent 246 0bf7c769de06
child 449 3caf8ff4d8a2
child 1732 1cc865ba17c2
equal deleted inserted replaced
324:9aa51cbdbfae 325:7833cf70c4f2
    46 #include <msr_time.h>
    46 #include <msr_time.h>
    47 #include "msr_param.h"
    47 #include "msr_param.h"
    48 
    48 
    49 // EtherCAT
    49 // EtherCAT
    50 #include "../../include/ecrt.h"
    50 #include "../../include/ecrt.h"
    51 
    51 #include "../../include/ecdb.h"
    52 #define ASYNC
       
    53 
    52 
    54 #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
    53 #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
    55 #define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
    54 #define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
    56 
    55 
    57 /*****************************************************************************/
    56 /*****************************************************************************/
    64 // EtherCAT
    63 // EtherCAT
    65 ec_master_t *master = NULL;
    64 ec_master_t *master = NULL;
    66 ec_domain_t *domain1 = NULL;
    65 ec_domain_t *domain1 = NULL;
    67 
    66 
    68 // raw process data
    67 // raw process data
    69 void *r_ssi;
    68 void *r_ana_out;
    70 void *r_ssi_st;
       
    71 
    69 
    72 // Channels
    70 // Channels
    73 uint32_t k_ssi;
    71 double k_ana_out;
    74 uint32_t k_ssi_st;
    72 
    75 
    73 ec_pdo_reg_t domain1_pdos[] = {
    76 ec_field_init_t domain1_fields[] = {
    74     {"3", Beckhoff_EL4132_Output1, &r_ana_out},
    77     {&r_ssi,    "0:3", "Beckhoff", "EL5001", "InputValue", 0},
       
    78     {&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status",     0},
       
    79     {}
    75     {}
    80 };
    76 };
    81 
    77 
    82 /*****************************************************************************/
    78 /*****************************************************************************/
    83 
    79 
    84 void msr_controller_run(void)
    80 void msr_controller_run(void)
    85 {
    81 {
    86     rt_sem_wait(&master_sem);
    82     rt_sem_wait(&master_sem);
    87 
    83 
    88 #ifdef ASYNC
    84     // receive
    89     // Empfangen
    85     ecrt_master_receive(master);
    90     ecrt_master_async_receive(master);
       
    91     ecrt_domain_process(domain1);
    86     ecrt_domain_process(domain1);
    92 #else
    87 
    93     // Senden und empfangen
    88     // Process data
    94     ecrt_domain_queue(domain1);
    89     EC_WRITE_S16(r_ana_out, k_ana_out / 10.0 * 0x7FFF);
       
    90 
       
    91     // Send
    95     ecrt_master_run(master);
    92     ecrt_master_run(master);
    96     ecrt_master_sync_io(master);
    93     ecrt_master_send(master);
    97     ecrt_domain_process(domain1);
       
    98 #endif
       
    99 
       
   100     // Prozessdaten verarbeiten
       
   101     k_ssi    = EC_READ_U32(r_ssi);
       
   102     k_ssi_st = EC_READ_U8 (r_ssi_st);
       
   103 
       
   104 #ifdef ASYNC
       
   105     // Senden
       
   106     ecrt_domain_queue(domain1);
       
   107     ecrt_master_run(master);
       
   108     ecrt_master_async_send(master);
       
   109 #endif
       
   110 
    94 
   111     rt_sem_signal(&master_sem);
    95     rt_sem_signal(&master_sem);
   112 
    96 
   113     msr_write_kanal_list();
    97     msr_write_kanal_list();
   114 }
    98 }
   126 
   110 
   127 /*****************************************************************************/
   111 /*****************************************************************************/
   128 
   112 
   129 int msr_reg(void)
   113 int msr_reg(void)
   130 {
   114 {
   131     msr_reg_kanal("/ssi_position", "", &k_ssi,    TUINT);
   115     msr_reg_kanal("/ana_out", "", &k_ana_out, TDBL);
   132     msr_reg_kanal("/ssi_status",   "", &k_ssi_st, TUINT);
       
   133     return 0;
   116     return 0;
   134 }
   117 }
   135 
   118 
   136 /*****************************************************************************/
   119 /*****************************************************************************/
   137 
   120 
   155 /*****************************************************************************/
   138 /*****************************************************************************/
   156 
   139 
   157 int __init init_mod(void)
   140 int __init init_mod(void)
   158 {
   141 {
   159     RTIME ticks;
   142     RTIME ticks;
   160 #if 0
       
   161     ec_slave_t *slave;
       
   162 #endif
       
   163 
   143 
   164     printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
   144     printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
   165 
   145 
   166     rt_sem_init(&master_sem, 1);
   146     rt_sem_init(&master_sem, 1);
   167     t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
   147     t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
   169     if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
   149     if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
   170         printk(KERN_ERR "Failed to initialize rtlib!\n");
   150         printk(KERN_ERR "Failed to initialize rtlib!\n");
   171         goto out_return;
   151         goto out_return;
   172     }
   152     }
   173 
   153 
   174     if ((master = ecrt_request_master(0)) == NULL) {
   154     if (!(master = ecrt_request_master(0))) {
   175         printk(KERN_ERR "Failed to request master 0!\n");
   155         printk(KERN_ERR "Failed to request master 0!\n");
   176         goto out_msr_cleanup;
   156         goto out_msr_cleanup;
   177     }
   157     }
   178 
   158 
   179     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   159     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   182     if (!(domain1 = ecrt_master_create_domain(master))) {
   162     if (!(domain1 = ecrt_master_create_domain(master))) {
   183         printk(KERN_ERR "Failed to create domains!\n");
   163         printk(KERN_ERR "Failed to create domains!\n");
   184         goto out_release_master;
   164         goto out_release_master;
   185     }
   165     }
   186 
   166 
   187     printk(KERN_INFO "Registering domain fields...\n");
   167     printk(KERN_INFO "Registering PDOs...\n");
   188     if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
   168     if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) {
   189         printk(KERN_ERR "Failed to register domain fields.\n");
   169         printk(KERN_ERR "Failed to register PDOs.\n");
   190         goto out_release_master;
   170         goto out_release_master;
   191     }
   171     }
   192 
   172 
   193     printk(KERN_INFO "Activating master...\n");
   173     printk(KERN_INFO "Activating master...\n");
   194     if (ecrt_master_activate(master)) {
   174     if (ecrt_master_activate(master)) {
   195         printk(KERN_ERR "Could not activate master!\n");
   175         printk(KERN_ERR "Could not activate master!\n");
   196         goto out_release_master;
   176         goto out_release_master;
   197     }
   177     }
   198 
   178 
   199 #if 0
   179     ecrt_master_prepare(master);
   200     if (ecrt_master_fetch_sdo_lists(master)) {
       
   201         printk(KERN_ERR "Failed to fetch SDO lists!\n");
       
   202         goto out_deactivate;
       
   203     }
       
   204     ecrt_master_print(master, 2);
       
   205 #else
       
   206     ecrt_master_print(master, 0);
       
   207 #endif
       
   208 
       
   209 #if 0
       
   210     if (!(slave = ecrt_master_get_slave(master, "0:3"))) {
       
   211         printk(KERN_ERR "Failed to get slave!\n");
       
   212         goto out_deactivate;
       
   213     }
       
   214 
       
   215     if (
       
   216         ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  1) || // disable frame error bit
       
   217         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  0) || // power failure bit
       
   218         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) || // inhibit time
       
   219         ecrt_slave_sdo_write_exp8(slave, 0x4061, 4,  0) || // test mode
       
   220         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  0) || // graycode
       
   221         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  5) || // 125kbaud
       
   222         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) || // single-turn
       
   223         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
       
   224         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
       
   225         ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 50) // inhibit time in us
       
   226         ) {
       
   227         printk(KERN_ERR "Failed to configure SSI slave!\n");
       
   228         goto out_deactivate;
       
   229     }
       
   230 #endif
       
   231 
       
   232 #if 0
       
   233     if (!(slave = ecrt_master_get_slave(master, "1:0"))) {
       
   234         printk(KERN_ERR "Failed to get slave!\n");
       
   235         goto out_deactivate;
       
   236     }
       
   237     if (ecrt_slave_write_alias(slave, 0x5678)) {
       
   238         printk(KERN_ERR "Failed to write alias!\n");
       
   239         goto out_deactivate;
       
   240     }
       
   241 #endif
       
   242 
       
   243 #ifdef ASYNC
       
   244     // Einmal senden und warten...
       
   245     ecrt_master_prepare_async_io(master);
       
   246 #endif
       
   247 
       
   248     if (ecrt_master_start_eoe(master)) {
       
   249         printk(KERN_ERR "Failed to start EoE processing!\n");
       
   250         goto out_deactivate;
       
   251     }
       
   252 
   180 
   253     printk("Starting cyclic sample thread...\n");
   181     printk("Starting cyclic sample thread...\n");
   254     ticks = start_rt_timer(nano2count(TIMERTICKS));
   182     ticks = start_rt_timer(nano2count(TIMERTICKS));
   255     if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) {
   183     if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) {
   256         printk(KERN_ERR "Failed to init RTAI task!\n");
   184         printk(KERN_ERR "Failed to init RTAI task!\n");
   266 
   194 
   267  out_stop_task:
   195  out_stop_task:
   268     rt_task_delete(&task);
   196     rt_task_delete(&task);
   269  out_stop_timer:
   197  out_stop_timer:
   270     stop_rt_timer();
   198     stop_rt_timer();
   271  out_deactivate:
       
   272     ecrt_master_deactivate(master);
   199     ecrt_master_deactivate(master);
   273  out_release_master:
   200  out_release_master:
   274     ecrt_release_master(master);
   201     ecrt_release_master(master);
   275  out_msr_cleanup:
   202  out_msr_cleanup:
   276     msr_rtlib_cleanup();
   203     msr_rtlib_cleanup();