107 */ |
107 */ |
108 void ec_master_init_static(void) |
108 void ec_master_init_static(void) |
109 { |
109 { |
110 #ifdef EC_HAVE_CYCLES |
110 #ifdef EC_HAVE_CYCLES |
111 timeout_cycles = (cycles_t) EC_IO_TIMEOUT /* us */ * (cpu_khz / 1000); |
111 timeout_cycles = (cycles_t) EC_IO_TIMEOUT /* us */ * (cpu_khz / 1000); |
112 ext_injection_timeout_cycles = (cycles_t) EC_SDO_INJECTION_TIMEOUT /* us */ * (cpu_khz / 1000); |
112 ext_injection_timeout_cycles = |
|
113 (cycles_t) EC_SDO_INJECTION_TIMEOUT /* us */ * (cpu_khz / 1000); |
113 #else |
114 #else |
114 // one jiffy may always elapse between time measurement |
115 // one jiffy may always elapse between time measurement |
115 timeout_jiffies = max(EC_IO_TIMEOUT * HZ / 1000000, 1); |
116 timeout_jiffies = max(EC_IO_TIMEOUT * HZ / 1000000, 1); |
116 ext_injection_timeout_jiffies = max(EC_SDO_INJECTION_TIMEOUT * HZ / 1000000, 1); |
117 ext_injection_timeout_jiffies = |
|
118 max(EC_SDO_INJECTION_TIMEOUT * HZ / 1000000, 1); |
117 #endif |
119 #endif |
118 } |
120 } |
119 |
121 |
120 /*****************************************************************************/ |
122 /*****************************************************************************/ |
121 |
123 |
138 master->index = index; |
140 master->index = index; |
139 master->reserved = 0; |
141 master->reserved = 0; |
140 |
142 |
141 sema_init(&master->master_sem, 1); |
143 sema_init(&master->master_sem, 1); |
142 |
144 |
143 master->main_mac = main_mac; |
145 master->macs[EC_DEVICE_MAIN] = main_mac; |
144 master->backup_mac = backup_mac; |
146 master->macs[EC_DEVICE_BACKUP] = backup_mac; |
145 ec_master_clear_device_stats(master); |
147 ec_master_clear_device_stats(master); |
146 |
148 |
147 sema_init(&master->device_sem, 1); |
149 sema_init(&master->device_sem, 1); |
148 |
150 |
149 master->phase = EC_ORPHANED; |
151 master->phase = EC_ORPHANED; |
209 |
211 |
210 INIT_LIST_HEAD(&master->reg_requests); |
212 INIT_LIST_HEAD(&master->reg_requests); |
211 init_waitqueue_head(&master->reg_queue); |
213 init_waitqueue_head(&master->reg_queue); |
212 |
214 |
213 // init devices |
215 // init devices |
214 ret = ec_device_init(&master->main_device, master); |
216 ret = ec_device_init(&master->devices[EC_DEVICE_MAIN], master); |
215 if (ret < 0) |
217 if (ret < 0) |
216 goto out_return; |
218 goto out_return; |
217 |
219 |
218 ret = ec_device_init(&master->backup_device, master); |
220 ret = ec_device_init(&master->devices[EC_DEVICE_BACKUP], master); |
219 if (ret < 0) |
221 if (ret < 0) |
220 goto out_clear_main; |
222 goto out_clear_main; |
221 |
223 |
222 // init state machine datagram |
224 // init state machine datagram |
223 ec_datagram_init(&master->fsm_datagram); |
225 ec_datagram_init(&master->fsm_datagram); |
232 // create state machine object |
234 // create state machine object |
233 ec_fsm_master_init(&master->fsm, master, &master->fsm_datagram); |
235 ec_fsm_master_init(&master->fsm, master, &master->fsm_datagram); |
234 |
236 |
235 // init reference sync datagram |
237 // init reference sync datagram |
236 ec_datagram_init(&master->ref_sync_datagram); |
238 ec_datagram_init(&master->ref_sync_datagram); |
237 snprintf(master->ref_sync_datagram.name, EC_DATAGRAM_NAME_SIZE, "refsync"); |
239 snprintf(master->ref_sync_datagram.name, EC_DATAGRAM_NAME_SIZE, |
|
240 "refsync"); |
238 ret = ec_datagram_apwr(&master->ref_sync_datagram, 0, 0x0910, 8); |
241 ret = ec_datagram_apwr(&master->ref_sync_datagram, 0, 0x0910, 8); |
239 if (ret < 0) { |
242 if (ret < 0) { |
240 ec_datagram_clear(&master->ref_sync_datagram); |
243 ec_datagram_clear(&master->ref_sync_datagram); |
241 EC_MASTER_ERR(master, "Failed to allocate reference" |
244 EC_MASTER_ERR(master, "Failed to allocate reference" |
242 " synchronisation datagram.\n"); |
245 " synchronisation datagram.\n"); |
254 goto out_clear_ref_sync; |
257 goto out_clear_ref_sync; |
255 } |
258 } |
256 |
259 |
257 // init sync monitor datagram |
260 // init sync monitor datagram |
258 ec_datagram_init(&master->sync_mon_datagram); |
261 ec_datagram_init(&master->sync_mon_datagram); |
259 snprintf(master->sync_mon_datagram.name, EC_DATAGRAM_NAME_SIZE, "syncmon"); |
262 snprintf(master->sync_mon_datagram.name, EC_DATAGRAM_NAME_SIZE, |
|
263 "syncmon"); |
260 ret = ec_datagram_brd(&master->sync_mon_datagram, 0x092c, 4); |
264 ret = ec_datagram_brd(&master->sync_mon_datagram, 0x092c, 4); |
261 if (ret < 0) { |
265 if (ret < 0) { |
262 ec_datagram_clear(&master->sync_mon_datagram); |
266 ec_datagram_clear(&master->sync_mon_datagram); |
263 EC_MASTER_ERR(master, "Failed to allocate sync" |
267 EC_MASTER_ERR(master, "Failed to allocate sync" |
264 " monitoring datagram.\n"); |
268 " monitoring datagram.\n"); |
342 ec_datagram_clear(&master->sync_mon_datagram); |
346 ec_datagram_clear(&master->sync_mon_datagram); |
343 ec_datagram_clear(&master->sync_datagram); |
347 ec_datagram_clear(&master->sync_datagram); |
344 ec_datagram_clear(&master->ref_sync_datagram); |
348 ec_datagram_clear(&master->ref_sync_datagram); |
345 ec_fsm_master_clear(&master->fsm); |
349 ec_fsm_master_clear(&master->fsm); |
346 ec_datagram_clear(&master->fsm_datagram); |
350 ec_datagram_clear(&master->fsm_datagram); |
347 ec_device_clear(&master->backup_device); |
351 ec_device_clear(&master->devices[EC_DEVICE_BACKUP]); |
348 ec_device_clear(&master->main_device); |
352 ec_device_clear(&master->devices[EC_DEVICE_MAIN]); |
349 } |
353 } |
350 |
354 |
351 /*****************************************************************************/ |
355 /*****************************************************************************/ |
352 |
356 |
353 #ifdef EC_EOE |
357 #ifdef EC_EOE |
626 up(&master->scan_sem); |
632 up(&master->scan_sem); |
627 } else { |
633 } else { |
628 up(&master->scan_sem); |
634 up(&master->scan_sem); |
629 |
635 |
630 // wait for slave scan to complete |
636 // wait for slave scan to complete |
631 ret = wait_event_interruptible(master->scan_queue, !master->scan_busy); |
637 ret = wait_event_interruptible(master->scan_queue, |
|
638 !master->scan_busy); |
632 if (ret) { |
639 if (ret) { |
633 EC_MASTER_INFO(master, "Waiting for slave scan" |
640 EC_MASTER_INFO(master, "Waiting for slave scan" |
634 " interrupted by signal.\n"); |
641 " interrupted by signal.\n"); |
635 goto out_allow; |
642 goto out_allow; |
636 } |
643 } |
906 |
913 |
907 EC_MASTER_DBG(master, 2, "ec_master_send_datagrams\n"); |
914 EC_MASTER_DBG(master, 2, "ec_master_send_datagrams\n"); |
908 |
915 |
909 do { |
916 do { |
910 // fetch pointer to transmit socket buffer |
917 // fetch pointer to transmit socket buffer |
911 frame_data = ec_device_tx_data(&master->main_device); |
918 frame_data = ec_device_tx_data(&master->devices[EC_DEVICE_MAIN]); |
912 cur_data = frame_data + EC_FRAME_HEADER_SIZE; |
919 cur_data = frame_data + EC_FRAME_HEADER_SIZE; |
913 follows_word = NULL; |
920 follows_word = NULL; |
914 more_datagrams_waiting = 0; |
921 more_datagrams_waiting = 0; |
915 |
922 |
916 // fill current frame with datagrams |
923 // fill current frame with datagrams |
931 EC_MASTER_DBG(master, 2, "adding datagram 0x%02X\n", |
938 EC_MASTER_DBG(master, 2, "adding datagram 0x%02X\n", |
932 datagram->index); |
939 datagram->index); |
933 |
940 |
934 // set "datagram following" flag in previous frame |
941 // set "datagram following" flag in previous frame |
935 if (follows_word) |
942 if (follows_word) |
936 EC_WRITE_U16(follows_word, EC_READ_U16(follows_word) | 0x8000); |
943 EC_WRITE_U16(follows_word, |
|
944 EC_READ_U16(follows_word) | 0x8000); |
937 |
945 |
938 // EtherCAT datagram header |
946 // EtherCAT datagram header |
939 EC_WRITE_U8 (cur_data, datagram->type); |
947 EC_WRITE_U8 (cur_data, datagram->type); |
940 EC_WRITE_U8 (cur_data + 1, datagram->index); |
948 EC_WRITE_U8 (cur_data + 1, datagram->index); |
941 memcpy(cur_data + 2, datagram->address, EC_ADDR_LEN); |
949 memcpy(cur_data + 2, datagram->address, EC_ADDR_LEN); |
958 break; |
966 break; |
959 } |
967 } |
960 |
968 |
961 // EtherCAT frame header |
969 // EtherCAT frame header |
962 EC_WRITE_U16(frame_data, ((cur_data - frame_data |
970 EC_WRITE_U16(frame_data, ((cur_data - frame_data |
963 - EC_FRAME_HEADER_SIZE) & 0x7FF) | 0x1000); |
971 - EC_FRAME_HEADER_SIZE) & 0x7FF) | 0x1000); |
964 |
972 |
965 // pad frame |
973 // pad frame |
966 while (cur_data - frame_data < ETH_ZLEN - ETH_HLEN) |
974 while (cur_data - frame_data < ETH_ZLEN - ETH_HLEN) |
967 EC_WRITE_U8(cur_data++, 0x00); |
975 EC_WRITE_U8(cur_data++, 0x00); |
968 |
976 |
969 EC_MASTER_DBG(master, 2, "frame size: %zu\n", cur_data - frame_data); |
977 EC_MASTER_DBG(master, 2, "frame size: %zu\n", cur_data - frame_data); |
970 |
978 |
971 // send frame |
979 // send frame |
972 ec_device_send(&master->main_device, cur_data - frame_data); |
980 ec_device_send(&master->devices[EC_DEVICE_MAIN], |
|
981 cur_data - frame_data); |
973 #ifdef EC_HAVE_CYCLES |
982 #ifdef EC_HAVE_CYCLES |
974 cycles_sent = get_cycles(); |
983 cycles_sent = get_cycles(); |
975 #endif |
984 #endif |
976 jiffies_sent = jiffies; |
985 jiffies_sent = jiffies; |
977 |
986 |
1090 EC_MASTER_DBG(master, 0, "UNMATCHED datagram:\n"); |
1099 EC_MASTER_DBG(master, 0, "UNMATCHED datagram:\n"); |
1091 ec_print_data(cur_data - EC_DATAGRAM_HEADER_SIZE, |
1100 ec_print_data(cur_data - EC_DATAGRAM_HEADER_SIZE, |
1092 EC_DATAGRAM_HEADER_SIZE + data_size |
1101 EC_DATAGRAM_HEADER_SIZE + data_size |
1093 + EC_DATAGRAM_FOOTER_SIZE); |
1102 + EC_DATAGRAM_FOOTER_SIZE); |
1094 #ifdef EC_DEBUG_RING |
1103 #ifdef EC_DEBUG_RING |
1095 ec_device_debug_ring_print(&master->main_device); |
1104 ec_device_debug_ring_print(&master->devices[EC_DEVICE_MAIN]); |
1096 #endif |
1105 #endif |
1097 } |
1106 } |
1098 |
1107 |
1099 cur_data += data_size + EC_DATAGRAM_FOOTER_SIZE; |
1108 cur_data += data_size + EC_DATAGRAM_FOOTER_SIZE; |
1100 continue; |
1109 continue; |
1115 cur_data += EC_DATAGRAM_FOOTER_SIZE; |
1124 cur_data += EC_DATAGRAM_FOOTER_SIZE; |
1116 |
1125 |
1117 // dequeue the received datagram |
1126 // dequeue the received datagram |
1118 datagram->state = EC_DATAGRAM_RECEIVED; |
1127 datagram->state = EC_DATAGRAM_RECEIVED; |
1119 #ifdef EC_HAVE_CYCLES |
1128 #ifdef EC_HAVE_CYCLES |
1120 datagram->cycles_received = master->main_device.cycles_poll; |
1129 datagram->cycles_received = |
1121 #endif |
1130 master->devices[EC_DEVICE_MAIN].cycles_poll; |
1122 datagram->jiffies_received = master->main_device.jiffies_poll; |
1131 #endif |
|
1132 datagram->jiffies_received = |
|
1133 master->devices[EC_DEVICE_MAIN].jiffies_poll; |
1123 list_del_init(&datagram->queue); |
1134 list_del_init(&datagram->queue); |
1124 } |
1135 } |
1125 } |
1136 } |
1126 |
1137 |
1127 /*****************************************************************************/ |
1138 /*****************************************************************************/ |
1228 s->last_tx_count = s->tx_count; |
1239 s->last_tx_count = s->tx_count; |
1229 s->last_rx_count = s->rx_count; |
1240 s->last_rx_count = s->rx_count; |
1230 s->last_tx_bytes = s->tx_bytes; |
1241 s->last_tx_bytes = s->tx_bytes; |
1231 s->last_rx_bytes = s->rx_bytes; |
1242 s->last_rx_bytes = s->rx_bytes; |
1232 |
1243 |
1233 ec_device_update_stats(&master->main_device); |
1244 ec_device_update_stats(&master->devices[EC_DEVICE_MAIN]); |
1234 ec_device_update_stats(&master->backup_device); |
1245 ec_device_update_stats(&master->devices[EC_DEVICE_BACKUP]); |
1235 |
1246 |
1236 s->jiffies = jiffies; |
1247 s->jiffies = jiffies; |
1237 } |
1248 } |
1238 |
1249 |
1239 /*****************************************************************************/ |
1250 /*****************************************************************************/ |
1354 if (fsm_exec) { |
1365 if (fsm_exec) { |
1355 ec_master_queue_datagram(master, &master->fsm_datagram); |
1366 ec_master_queue_datagram(master, &master->fsm_datagram); |
1356 } |
1367 } |
1357 ec_master_inject_external_datagrams(master); |
1368 ec_master_inject_external_datagrams(master); |
1358 ecrt_master_send(master); |
1369 ecrt_master_send(master); |
1359 sent_bytes = master->main_device.tx_skb[ |
1370 sent_bytes = master->devices[EC_DEVICE_MAIN].tx_skb[ |
1360 master->main_device.tx_ring_index]->len; |
1371 master->devices[EC_DEVICE_MAIN].tx_ring_index]->len; |
1361 up(&master->io_sem); |
1372 up(&master->io_sem); |
1362 |
1373 |
1363 if (ec_fsm_master_idle(&master->fsm)) { |
1374 if (ec_fsm_master_idle(&master->fsm)) { |
1364 #ifdef EC_USE_HRTIMER |
1375 #ifdef EC_USE_HRTIMER |
1365 ec_master_nanosleep(master->send_interval * 1000); |
1376 ec_master_nanosleep(master->send_interval * 1000); |
1998 unsigned int index; |
2009 unsigned int index; |
1999 |
2010 |
2000 EC_MASTER_DBG(master, 1, "ecrt_master_create_domain(master = 0x%p)\n", |
2011 EC_MASTER_DBG(master, 1, "ecrt_master_create_domain(master = 0x%p)\n", |
2001 master); |
2012 master); |
2002 |
2013 |
2003 if (!(domain = (ec_domain_t *) kmalloc(sizeof(ec_domain_t), GFP_KERNEL))) { |
2014 if (!(domain = |
|
2015 (ec_domain_t *) kmalloc(sizeof(ec_domain_t), GFP_KERNEL))) { |
2004 EC_MASTER_ERR(master, "Error allocating domain memory!\n"); |
2016 EC_MASTER_ERR(master, "Error allocating domain memory!\n"); |
2005 return ERR_PTR(-ENOMEM); |
2017 return ERR_PTR(-ENOMEM); |
2006 } |
2018 } |
2007 |
2019 |
2008 down(&master->master_sem); |
2020 down(&master->master_sem); |
2184 ec_master_queue_datagram(master, &master->fsm_datagram); |
2196 ec_master_queue_datagram(master, &master->fsm_datagram); |
2185 master->injection_seq_rt = master->injection_seq_fsm; |
2197 master->injection_seq_rt = master->injection_seq_fsm; |
2186 } |
2198 } |
2187 ec_master_inject_external_datagrams(master); |
2199 ec_master_inject_external_datagrams(master); |
2188 |
2200 |
2189 if (unlikely(!master->main_device.link_state)) { |
2201 if (unlikely(!master->devices[EC_DEVICE_MAIN].link_state)) { |
2190 // link is down, no datagram can be sent |
2202 // link is down, no datagram can be sent |
2191 list_for_each_entry_safe(datagram, n, &master->datagram_queue, queue) { |
2203 list_for_each_entry_safe(datagram, n, |
|
2204 &master->datagram_queue, queue) { |
2192 datagram->state = EC_DATAGRAM_ERROR; |
2205 datagram->state = EC_DATAGRAM_ERROR; |
2193 list_del_init(&datagram->queue); |
2206 list_del_init(&datagram->queue); |
2194 } |
2207 } |
2195 |
2208 |
2196 // query link state |
2209 // query link state |
2197 ec_device_poll(&master->main_device); |
2210 ec_device_poll(&master->devices[EC_DEVICE_MAIN]); |
2198 |
2211 |
2199 // clear frame statistics |
2212 // clear frame statistics |
2200 ec_device_clear_stats(&master->main_device); |
2213 ec_device_clear_stats(&master->devices[EC_DEVICE_MAIN]); |
2201 return; |
2214 return; |
2202 } |
2215 } |
2203 |
2216 |
2204 // send frames |
2217 // send frames |
2205 ec_master_send_datagrams(master); |
2218 ec_master_send_datagrams(master); |
2210 void ecrt_master_receive(ec_master_t *master) |
2223 void ecrt_master_receive(ec_master_t *master) |
2211 { |
2224 { |
2212 ec_datagram_t *datagram, *next; |
2225 ec_datagram_t *datagram, *next; |
2213 |
2226 |
2214 // receive datagrams |
2227 // receive datagrams |
2215 ec_device_poll(&master->main_device); |
2228 ec_device_poll(&master->devices[EC_DEVICE_MAIN]); |
2216 if (master->backup_device.dev) { |
2229 if (master->devices[EC_DEVICE_BACKUP].dev) { |
2217 ec_device_poll(&master->backup_device); |
2230 ec_device_poll(&master->devices[EC_DEVICE_BACKUP]); |
2218 } |
2231 } |
2219 ec_master_update_device_stats(master); |
2232 ec_master_update_device_stats(master); |
2220 |
2233 |
2221 // dequeue all datagrams that timed out |
2234 // dequeue all datagrams that timed out |
2222 list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) { |
2235 list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) { |
2223 if (datagram->state != EC_DATAGRAM_SENT) continue; |
2236 if (datagram->state != EC_DATAGRAM_SENT) continue; |
2224 |
2237 |
2225 #ifdef EC_HAVE_CYCLES |
2238 #ifdef EC_HAVE_CYCLES |
2226 if (master->main_device.cycles_poll - datagram->cycles_sent |
2239 if (master->devices[EC_DEVICE_MAIN].cycles_poll - |
2227 > timeout_cycles) { |
2240 datagram->cycles_sent > timeout_cycles) { |
2228 #else |
2241 #else |
2229 if (master->main_device.jiffies_poll - datagram->jiffies_sent |
2242 if (master->devices[EC_DEVICE_MAIN].jiffies_poll - |
2230 > timeout_jiffies) { |
2243 datagram->jiffies_sent > timeout_jiffies) { |
2231 #endif |
2244 #endif |
2232 list_del_init(&datagram->queue); |
2245 list_del_init(&datagram->queue); |
2233 datagram->state = EC_DATAGRAM_TIMED_OUT; |
2246 datagram->state = EC_DATAGRAM_TIMED_OUT; |
2234 master->stats.timeouts++; |
2247 master->stats.timeouts++; |
2235 ec_master_output_stats(master); |
2248 ec_master_output_stats(master); |
2236 |
2249 |
2237 if (unlikely(master->debug_level > 0)) { |
2250 if (unlikely(master->debug_level > 0)) { |
2238 unsigned int time_us; |
2251 unsigned int time_us; |
2239 #ifdef EC_HAVE_CYCLES |
2252 #ifdef EC_HAVE_CYCLES |
2240 time_us = (unsigned int) (master->main_device.cycles_poll - |
2253 time_us = (unsigned int) |
|
2254 (master->devices[EC_DEVICE_MAIN].cycles_poll - |
2241 datagram->cycles_sent) * 1000 / cpu_khz; |
2255 datagram->cycles_sent) * 1000 / cpu_khz; |
2242 #else |
2256 #else |
2243 time_us = (unsigned int) ((master->main_device.jiffies_poll - |
2257 time_us = (unsigned int) |
|
2258 ((master->devices[EC_DEVICE_MAIN].jiffies_poll - |
2244 datagram->jiffies_sent) * 1000000 / HZ); |
2259 datagram->jiffies_sent) * 1000000 / HZ); |
2245 #endif |
2260 #endif |
2246 EC_MASTER_DBG(master, 0, "TIMED OUT datagram %p," |
2261 EC_MASTER_DBG(master, 0, "TIMED OUT datagram %p," |
2247 " index %02X waited %u us.\n", |
2262 " index %02X waited %u us.\n", |
2248 datagram, datagram->index, time_us); |
2263 datagram, datagram->index, time_us); |
2343 { |
2358 { |
2344 EC_MASTER_DBG(master, 1, "ecrt_master(master = 0x%p," |
2359 EC_MASTER_DBG(master, 1, "ecrt_master(master = 0x%p," |
2345 " master_info = 0x%p)\n", master, master_info); |
2360 " master_info = 0x%p)\n", master, master_info); |
2346 |
2361 |
2347 master_info->slave_count = master->slave_count; |
2362 master_info->slave_count = master->slave_count; |
2348 master_info->link_up = master->main_device.link_state; |
2363 master_info->link_up = master->devices[EC_DEVICE_MAIN].link_state; |
2349 master_info->scan_busy = master->scan_busy; |
2364 master_info->scan_busy = master->scan_busy; |
2350 master_info->app_time = master->app_time; |
2365 master_info->app_time = master->app_time; |
2351 return 0; |
2366 return 0; |
2352 } |
2367 } |
2353 |
2368 |
2404 |
2419 |
2405 void ecrt_master_state(const ec_master_t *master, ec_master_state_t *state) |
2420 void ecrt_master_state(const ec_master_t *master, ec_master_state_t *state) |
2406 { |
2421 { |
2407 state->slaves_responding = master->fsm.slaves_responding; |
2422 state->slaves_responding = master->fsm.slaves_responding; |
2408 state->al_states = master->fsm.slave_states; |
2423 state->al_states = master->fsm.slave_states; |
2409 state->link_up = master->main_device.link_state; |
2424 state->link_up = master->devices[EC_DEVICE_MAIN].link_state; |
2410 } |
2425 } |
2411 |
2426 |
2412 /*****************************************************************************/ |
2427 /*****************************************************************************/ |
2413 |
2428 |
2414 void ecrt_master_application_time(ec_master_t *master, uint64_t app_time) |
2429 void ecrt_master_application_time(ec_master_t *master, uint64_t app_time) |