examples/mini/mini.c
changeset 809 ec4ef8911824
parent 799 068a58b96965
child 814 a51f857b1b2d
equal deleted inserted replaced
808:1feddbd65608 809:ec4ef8911824
    44 #define FREQUENCY 100
    44 #define FREQUENCY 100
    45 
    45 
    46 //#define KBUS
    46 //#define KBUS
    47 #define PDOS
    47 #define PDOS
    48 #define MAPPING
    48 #define MAPPING
       
    49 #define EXTERNAL_MEMORY
    49 
    50 
    50 /*****************************************************************************/
    51 /*****************************************************************************/
    51 
    52 
    52 static struct timer_list timer;
    53 static struct timer_list timer;
    53 
    54 
    72 const ec_pdo_entry_info_t el3162_channel2[] = {
    73 const ec_pdo_entry_info_t el3162_channel2[] = {
    73     {0x3102, 1,  8}, // status
    74     {0x3102, 1,  8}, // status
    74     {0x3102, 2, 16}  // value
    75     {0x3102, 2, 16}  // value
    75 };
    76 };
    76 
    77 
    77 const ec_pdo_info_t mapping[] = {
    78 const ec_pdo_info_t el3162_mapping[] = {
    78     {EC_DIR_INPUT, 0x1A00, 2, el3162_channel1},
    79     {EC_DIR_INPUT, 0x1A00, 2, el3162_channel1},
    79     {EC_DIR_INPUT, 0x1A01, 2, el3162_channel2},
    80     {EC_DIR_INPUT, 0x1A01, 2, el3162_channel2},
    80 };
    81 };
       
    82 
       
    83 const ec_pdo_entry_info_t el2004_channels[] = {
       
    84     {0x3001, 1, 1}, // Value 1
       
    85     {0x3001, 2, 1}, // Value 2
       
    86     {0x3001, 3, 1}, // Value 3
       
    87     {0x3001, 4, 1}  // Value 4
       
    88 };
       
    89 
       
    90 const ec_pdo_info_t el2004_mapping[] = {
       
    91     {EC_DIR_OUTPUT, 0x1600, 1, &el2004_channels[0]},
       
    92     {EC_DIR_OUTPUT, 0x1601, 1, &el2004_channels[1]},
       
    93     {EC_DIR_OUTPUT, 0x1602, 1, &el2004_channels[2]},
       
    94     {EC_DIR_OUTPUT, 0x1603, 1, &el2004_channels[3]},
       
    95 };
    81 #endif
    96 #endif
    82 
    97 
    83 #ifdef PDOS
    98 #ifdef PDOS
    84 static uint8_t off_ana_in;
    99 static uint8_t *pd; /**< Process data. */
    85 //static uint8_t off_ana_out;
   100 static unsigned int off_ana_in;
       
   101 static unsigned int off_dig_out;
    86 
   102 
    87 const static ec_pdo_entry_reg_t domain1_regs[] = {
   103 const static ec_pdo_entry_reg_t domain1_regs[] = {
    88     {0, 1, Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
   104     {0, 1, Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
    89     //{0, 2, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
   105     {0, 3, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
    90     {}
   106     {}
    91 };
   107 };
    92 #endif
   108 #endif
    93 
   109 
    94 /*****************************************************************************/
   110 /*****************************************************************************/
   103     ecrt_master_receive(master);
   119     ecrt_master_receive(master);
   104     ecrt_domain_process(domain1);
   120     ecrt_domain_process(domain1);
   105     spin_unlock(&master_lock);
   121     spin_unlock(&master_lock);
   106 
   122 
   107     // process data
   123     // process data
   108     // k_pos = EC_READ_U32(r_ssi);
   124     EC_WRITE_U8(pd + off_dig_out, blink ? 0x0F : 0x00);
   109     //EC_WRITE_U8(r_dig_out, blink ? 0x0F : 0x00);
       
   110 
   125 
   111     if (counter) {
   126     if (counter) {
   112         counter--;
   127         counter--;
   113     }
   128     }
   114     else {
   129     else {
   174 int __init init_mini_module(void)
   189 int __init init_mini_module(void)
   175 {
   190 {
   176 #ifdef MAPPING
   191 #ifdef MAPPING
   177     ec_slave_config_t *sc;
   192     ec_slave_config_t *sc;
   178 #endif
   193 #endif
       
   194 #ifdef EXTERNAL_MEMORY
       
   195     unsigned int size;
       
   196 #endif
   179     
   197     
   180     printk(KERN_INFO PFX "Starting...\n");
   198     printk(KERN_INFO PFX "Starting...\n");
   181 
   199 
   182     if (!(master = ecrt_request_master(0))) {
   200     if (!(master = ecrt_request_master(0))) {
   183         printk(KERN_ERR PFX "Requesting master 0 failed!\n");
   201         printk(KERN_ERR PFX "Requesting master 0 failed!\n");
   197     if (!(sc = ecrt_master_slave_config(master, 0, 1, Beckhoff_EL3162))) {
   215     if (!(sc = ecrt_master_slave_config(master, 0, 1, Beckhoff_EL3162))) {
   198         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
   216         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
   199         goto out_release_master;
   217         goto out_release_master;
   200     }
   218     }
   201 
   219 
   202     if (ecrt_slave_config_mapping(sc, 2, mapping)) {
   220     if (ecrt_slave_config_mapping(sc, 2, el3162_mapping)) {
       
   221         printk(KERN_ERR PFX "Failed to configure Pdo mapping.\n");
       
   222         goto out_release_master;
       
   223     }
       
   224 
       
   225     if (!(sc = ecrt_master_slave_config(master, 0, 3, Beckhoff_EL2004))) {
       
   226         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
       
   227         goto out_release_master;
       
   228     }
       
   229 
       
   230     if (ecrt_slave_config_mapping(sc, 4, el2004_mapping)) {
   203         printk(KERN_ERR PFX "Failed to configure Pdo mapping.\n");
   231         printk(KERN_ERR PFX "Failed to configure Pdo mapping.\n");
   204         goto out_release_master;
   232         goto out_release_master;
   205     }
   233     }
   206 #endif
   234 #endif
   207 
   235 
   211         printk(KERN_ERR PFX "PDO entry registration failed!\n");
   239         printk(KERN_ERR PFX "PDO entry registration failed!\n");
   212         goto out_release_master;
   240         goto out_release_master;
   213     }
   241     }
   214 #endif
   242 #endif
   215 
   243 
       
   244 #ifdef EXTERNAL_MEMORY
       
   245     if ((size = ecrt_domain_size(domain1))) {
       
   246         if (!(pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
       
   247             printk(KERN_ERR PFX "Failed to allocate %u bytes of process data"
       
   248                     " memory!\n", size);
       
   249             goto out_release_master;
       
   250         }
       
   251         ecrt_domain_external_memory(domain1, pd);
       
   252     }
       
   253 #endif
       
   254 
   216     printk(KERN_INFO PFX "Activating master...\n");
   255     printk(KERN_INFO PFX "Activating master...\n");
   217     if (ecrt_master_activate(master)) {
   256     if (ecrt_master_activate(master)) {
   218         printk(KERN_ERR PFX "Failed to activate master!\n");
   257         printk(KERN_ERR PFX "Failed to activate master!\n");
   219         goto out_release_master;
   258 #ifdef EXTERNAL_MEMORY
   220     }
   259         goto out_free_process_data;
       
   260 #else
       
   261         goto out_release_master;
       
   262 #endif
       
   263     }
       
   264 
       
   265 #ifndef EXTERNAL_MEMORY
       
   266     // Get internal process data for domain
       
   267     pd = ecrt_domain_data(domain1);
       
   268 #endif
   221 
   269 
   222     printk(KERN_INFO PFX "Starting cyclic sample thread.\n");
   270     printk(KERN_INFO PFX "Starting cyclic sample thread.\n");
   223     init_timer(&timer);
   271     init_timer(&timer);
   224     timer.function = run;
   272     timer.function = run;
   225     timer.expires = jiffies + 10;
   273     timer.expires = jiffies + 10;
   226     add_timer(&timer);
   274     add_timer(&timer);
   227 
   275 
   228     printk(KERN_INFO PFX "Started.\n");
   276     printk(KERN_INFO PFX "Started.\n");
   229     return 0;
   277     return 0;
   230 
   278 
   231  out_release_master:
   279 #ifdef EXTERNAL_MEMORY
       
   280 out_free_process_data:
       
   281     kfree(pd);
       
   282 #endif
       
   283 out_release_master:
   232     printk(KERN_ERR PFX "Releasing master...\n");
   284     printk(KERN_ERR PFX "Releasing master...\n");
   233     ecrt_release_master(master);
   285     ecrt_release_master(master);
   234  out_return:
   286 out_return:
   235     printk(KERN_ERR PFX "Failed to load. Aborting.\n");
   287     printk(KERN_ERR PFX "Failed to load. Aborting.\n");
   236     return -1;
   288     return -1;
   237 }
   289 }
   238 
   290 
   239 /*****************************************************************************/
   291 /*****************************************************************************/
   241 void __exit cleanup_mini_module(void)
   293 void __exit cleanup_mini_module(void)
   242 {
   294 {
   243     printk(KERN_INFO PFX "Stopping...\n");
   295     printk(KERN_INFO PFX "Stopping...\n");
   244 
   296 
   245     del_timer_sync(&timer);
   297     del_timer_sync(&timer);
       
   298 
       
   299 #ifdef EXTERNAL_MEMORY
       
   300     kfree(pd);
       
   301 #endif
       
   302 
   246     printk(KERN_INFO PFX "Releasing master...\n");
   303     printk(KERN_INFO PFX "Releasing master...\n");
   247     ecrt_release_master(master);
   304     ecrt_release_master(master);
   248 
   305 
   249     printk(KERN_INFO PFX "Unloading.\n");
   306     printk(KERN_INFO PFX "Unloading.\n");
   250 }
   307 }