examples/mini/mini.c
changeset 818 b6c87ae254c9
parent 814 a51f857b1b2d
child 820 f8e6f5966cce
equal deleted inserted replaced
817:118dea2fa505 818:b6c87ae254c9
    41 
    41 
    42 #define PFX "ec_mini: "
    42 #define PFX "ec_mini: "
    43 
    43 
    44 #define FREQUENCY 100
    44 #define FREQUENCY 100
    45 
    45 
    46 //#define KBUS
    46 #define CONFIGURE_MAPPING
    47 #define PDOS
       
    48 #define MAPPING
       
    49 #define EXTERNAL_MEMORY
    47 #define EXTERNAL_MEMORY
    50 
    48 
    51 /*****************************************************************************/
    49 /*****************************************************************************/
    52 
    50 
    53 static struct timer_list timer;
    51 static struct timer_list timer;
    54 
    52 
    55 // EtherCAT
    53 // EtherCAT
    56 static ec_master_t *master = NULL;
    54 static ec_master_t *master = NULL;
       
    55 static ec_master_state_t master_state = {};
       
    56 spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
       
    57 
    57 static ec_domain_t *domain1 = NULL;
    58 static ec_domain_t *domain1 = NULL;
    58 spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
    59 static ec_domain_state_t domain1_state = {};
    59 static ec_master_state_t master_state, old_state = {};
    60 
    60 
    61 #ifdef CONFIGURE_MAPPING
    61 // data fields
       
    62 #ifdef KBUS
       
    63 static void *r_inputs;
       
    64 static void *r_outputs;
       
    65 #endif
       
    66 
       
    67 #ifdef MAPPING
       
    68 const ec_pdo_entry_info_t el3162_channel1[] = {
    62 const ec_pdo_entry_info_t el3162_channel1[] = {
    69     {0x3101, 1,  8}, // status
    63     {0x3101, 1,  8}, // status
    70     {0x3101, 2, 16}  // value
    64     {0x3101, 2, 16}  // value
    71 };
    65 };
    72 
    66 
    93     {EC_DIR_OUTPUT, 0x1602, 1, &el2004_channels[2]},
    87     {EC_DIR_OUTPUT, 0x1602, 1, &el2004_channels[2]},
    94     {EC_DIR_OUTPUT, 0x1603, 1, &el2004_channels[3]},
    88     {EC_DIR_OUTPUT, 0x1603, 1, &el2004_channels[3]},
    95 };
    89 };
    96 #endif
    90 #endif
    97 
    91 
    98 #ifdef PDOS
       
    99 static uint8_t *pd; /**< Process data. */
    92 static uint8_t *pd; /**< Process data. */
   100 static unsigned int off_ana_in;
    93 static unsigned int off_ana_in;
   101 static unsigned int off_dig_out;
    94 static unsigned int off_dig_out;
   102 
    95 
   103 const static ec_pdo_entry_reg_t domain1_regs[] = {
    96 const static ec_pdo_entry_reg_t domain1_regs[] = {
   104     {0, 1, Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
    97     {0, 1, Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
   105     {0, 3, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
    98     {0, 3, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
   106     {}
    99     {}
   107 };
   100 };
   108 #endif
   101 
       
   102 /*****************************************************************************/
       
   103 
       
   104 void check_domain1_state(void)
       
   105 {
       
   106     ec_domain_state_t ds;
       
   107 
       
   108     ecrt_domain_state(domain1, &ds);
       
   109     if (ds.working_counter != domain1_state.working_counter)
       
   110         printk(KERN_INFO PFX "domain1 working_counter changed to %u.\n",
       
   111                 ds.working_counter);
       
   112 
       
   113     if (ds.wc_state != domain1_state.wc_state)
       
   114         printk(KERN_INFO PFX "domain1 wc_state changed to %u.\n",
       
   115                 ds.wc_state);
       
   116 
       
   117     domain1_state = ds;
       
   118 }
       
   119 
       
   120 /*****************************************************************************/
       
   121 
       
   122 void check_master_state(void)
       
   123 {
       
   124     ec_master_state_t ms;
       
   125 
       
   126     spin_lock(&master_lock);
       
   127     ecrt_master_state(master, &ms);
       
   128     spin_unlock(&master_lock);
       
   129 
       
   130     if (ms.bus_state != master_state.bus_state) {
       
   131         printk(KERN_INFO PFX "bus state changed to %i.\n", ms.bus_state);
       
   132     }
       
   133     if (ms.bus_tainted != master_state.bus_tainted) {
       
   134         printk(KERN_INFO PFX "tainted flag changed to %u.\n",
       
   135                 ms.bus_tainted);
       
   136     }
       
   137     if (ms.slaves_responding != master_state.slaves_responding) {
       
   138         printk(KERN_INFO PFX "slaves_responding changed to %u.\n",
       
   139                 ms.slaves_responding);
       
   140     }
       
   141 
       
   142     master_state = ms;
       
   143 }
   109 
   144 
   110 /*****************************************************************************/
   145 /*****************************************************************************/
   111 
   146 
   112 void run(unsigned long data)
   147 void run(unsigned long data)
   113 {
   148 {
   114     static unsigned int counter = 0;
   149     static unsigned int counter = 0;
   115     static unsigned int blink = 0;
   150     static unsigned int blink = 0;
   116 
   151 
   117     // receive
   152     // receive process data
   118     spin_lock(&master_lock);
   153     spin_lock(&master_lock);
   119     ecrt_master_receive(master);
   154     ecrt_master_receive(master);
   120     ecrt_domain_process(domain1);
   155     ecrt_domain_process(domain1);
   121     spin_unlock(&master_lock);
   156     spin_unlock(&master_lock);
   122 
   157 
   123     // process data
   158     // check process data state (optional)
   124     EC_WRITE_U8(pd + off_dig_out, blink ? 0x0F : 0x00);
   159     check_domain1_state();
   125 
   160 
   126     if (counter) {
   161     if (counter) {
   127         counter--;
   162         counter--;
   128     }
   163     } else { // do this at FREQUENCY
   129     else {
       
   130         counter = FREQUENCY;
   164         counter = FREQUENCY;
       
   165 
       
   166         // calculate new process data
   131         blink = !blink;
   167         blink = !blink;
   132 
   168 
   133         spin_lock(&master_lock);
   169         // check for master state (optional)
   134         ecrt_master_state(master, &master_state);
   170         check_master_state();
   135         spin_unlock(&master_lock);
   171     }
   136 
   172 
   137         if (master_state.bus_state != old_state.bus_state) {
   173     // write process data
   138             printk(KERN_INFO PFX "bus state changed to %i.\n",
   174     EC_WRITE_U8(pd + off_dig_out, blink ? 0x0F : 0x00);
   139                     master_state.bus_state);
   175 
   140         }
   176     // send process data
   141         if (master_state.bus_tainted != old_state.bus_tainted) {
       
   142             printk(KERN_INFO PFX "tainted flag changed to %u.\n",
       
   143                     master_state.bus_tainted);
       
   144         }
       
   145         if (master_state.slaves_responding !=
       
   146                 old_state.slaves_responding) {
       
   147             printk(KERN_INFO PFX "slaves_responding changed to %u.\n",
       
   148                     master_state.slaves_responding);
       
   149         }
       
   150        
       
   151         old_state = master_state;
       
   152     }
       
   153 
       
   154 #ifdef KBUS
       
   155     EC_WRITE_U8(r_outputs + 2, blink ? 0xFF : 0x00);
       
   156 #endif
       
   157 
       
   158     // send
       
   159     spin_lock(&master_lock);
   177     spin_lock(&master_lock);
   160     ecrt_domain_queue(domain1);
   178     ecrt_domain_queue(domain1);
   161     spin_unlock(&master_lock);
       
   162 
       
   163     spin_lock(&master_lock);
       
   164     ecrt_master_send(master);
   179     ecrt_master_send(master);
   165     spin_unlock(&master_lock);
   180     spin_unlock(&master_lock);
   166 
   181 
   167     // restart timer
   182     // restart timer
   168     timer.expires += HZ / FREQUENCY;
   183     timer.expires += HZ / FREQUENCY;
   186 
   201 
   187 /*****************************************************************************/
   202 /*****************************************************************************/
   188 
   203 
   189 int __init init_mini_module(void)
   204 int __init init_mini_module(void)
   190 {
   205 {
   191 #ifdef MAPPING
   206 #ifdef CONFIGURE_MAPPING
   192     ec_slave_config_t *sc;
   207     ec_slave_config_t *sc;
   193 #endif
   208 #endif
   194 #ifdef EXTERNAL_MEMORY
   209 #ifdef EXTERNAL_MEMORY
   195     unsigned int size;
   210     unsigned int size;
   196 #endif
   211 #endif
   208     if (!(domain1 = ecrt_master_create_domain(master))) {
   223     if (!(domain1 = ecrt_master_create_domain(master))) {
   209         printk(KERN_ERR PFX "Domain creation failed!\n");
   224         printk(KERN_ERR PFX "Domain creation failed!\n");
   210         goto out_release_master;
   225         goto out_release_master;
   211     }
   226     }
   212 
   227 
   213 #ifdef MAPPING
   228 #ifdef CONFIGURE_MAPPING
   214     printk(KERN_INFO PFX "Configuring Pdo mapping...\n");
   229     printk(KERN_INFO PFX "Configuring Pdo mapping...\n");
   215     if (!(sc = ecrt_master_slave_config(master, 0, 1, Beckhoff_EL3162))) {
   230     if (!(sc = ecrt_master_slave_config(master, 0, 1, Beckhoff_EL3162))) {
   216         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
   231         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
   217         goto out_release_master;
   232         goto out_release_master;
   218     }
   233     }
   232         goto out_release_master;
   247         goto out_release_master;
   233     }
   248     }
   234 #endif
   249 #endif
   235 
   250 
   236     printk(KERN_INFO PFX "Registering Pdo entries...\n");
   251     printk(KERN_INFO PFX "Registering Pdo entries...\n");
   237 #ifdef PDOS
       
   238     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
   252     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
   239         printk(KERN_ERR PFX "Pdo entry registration failed!\n");
   253         printk(KERN_ERR PFX "Pdo entry registration failed!\n");
   240         goto out_release_master;
   254         goto out_release_master;
   241     }
   255     }
   242 #endif
       
   243 
   256 
   244 #ifdef EXTERNAL_MEMORY
   257 #ifdef EXTERNAL_MEMORY
   245     if ((size = ecrt_domain_size(domain1))) {
   258     if ((size = ecrt_domain_size(domain1))) {
   246         if (!(pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
   259         if (!(pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
   247             printk(KERN_ERR PFX "Failed to allocate %u bytes of process data"
   260             printk(KERN_ERR PFX "Failed to allocate %u bytes of process data"