master/master.c
changeset 2024 96e2ae6cce95
parent 2022 393e7aef6706
child 2028 55854f070c4a
equal deleted inserted replaced
2023:da133ec36c3a 2024:96e2ae6cce95
   189     master->eoe_thread = NULL;
   189     master->eoe_thread = NULL;
   190     INIT_LIST_HEAD(&master->eoe_handlers);
   190     INIT_LIST_HEAD(&master->eoe_handlers);
   191 #endif
   191 #endif
   192 
   192 
   193     sema_init(&master->io_sem, 1);
   193     sema_init(&master->io_sem, 1);
   194     master->send_cb = NULL;
   194     master->fsm_queue_lock_cb = NULL;
   195     master->receive_cb = NULL;
   195     master->fsm_queue_unlock_cb = NULL;
   196     master->cb_data = NULL;
   196     master->fsm_queue_locking_data = NULL;
   197     master->app_send_cb = NULL;
   197     master->app_fsm_queue_lock_cb = NULL;
   198     master->app_receive_cb = NULL;
   198     master->app_fsm_queue_unlock_cb = NULL;
   199     master->app_cb_data = NULL;
   199     master->app_fsm_queue_locking_data = NULL;
   200 
   200 
   201     INIT_LIST_HEAD(&master->sii_requests);
   201     INIT_LIST_HEAD(&master->sii_requests);
   202     init_waitqueue_head(&master->sii_queue);
   202     init_waitqueue_head(&master->sii_queue);
   203 
   203 
   204     INIT_LIST_HEAD(&master->reg_requests);
   204     INIT_LIST_HEAD(&master->reg_requests);
   453     up(&master->master_sem);
   453     up(&master->master_sem);
   454 }
   454 }
   455 
   455 
   456 /*****************************************************************************/
   456 /*****************************************************************************/
   457 
   457 
   458 /** Internal sending callback.
       
   459  */
       
   460 void ec_master_internal_send_cb(
       
   461         void *cb_data /**< Callback data. */
       
   462         )
       
   463 {
       
   464     ec_master_t *master = (ec_master_t *) cb_data;
       
   465     down(&master->io_sem);
       
   466     ecrt_master_send_ext(master);
       
   467     up(&master->io_sem);
       
   468 }
       
   469 
       
   470 /*****************************************************************************/
       
   471 
       
   472 /** Internal receiving callback.
       
   473  */
       
   474 void ec_master_internal_receive_cb(
       
   475         void *cb_data /**< Callback data. */
       
   476         )
       
   477 {
       
   478     ec_master_t *master = (ec_master_t *) cb_data;
       
   479     down(&master->io_sem);
       
   480     ecrt_master_receive(master);
       
   481     up(&master->io_sem);
       
   482 }
       
   483 
       
   484 /*****************************************************************************/
       
   485 
       
   486 /** Starts the master thread.
   458 /** Starts the master thread.
   487  *
   459  *
   488  * \retval  0 Success.
   460  * \retval  0 Success.
   489  * \retval <0 Error code.
   461  * \retval <0 Error code.
   490  */
   462  */
   546 {
   518 {
   547     int ret;
   519     int ret;
   548 
   520 
   549     EC_MASTER_DBG(master, 1, "ORPHANED -> IDLE.\n");
   521     EC_MASTER_DBG(master, 1, "ORPHANED -> IDLE.\n");
   550 
   522 
   551     master->send_cb = ec_master_internal_send_cb;
   523     master->fsm_queue_lock_cb = NULL;
   552     master->receive_cb = ec_master_internal_receive_cb;
   524     master->fsm_queue_unlock_cb = NULL;
   553     master->cb_data = master;
   525     master->fsm_queue_locking_data = NULL;
   554 
   526 
   555     master->phase = EC_IDLE;
   527     master->phase = EC_IDLE;
   556     ret = ec_master_thread_start(master, ec_master_idle_thread,
   528     ret = ec_master_thread_start(master, ec_master_idle_thread,
   557             "EtherCAT-IDLE");
   529             "EtherCAT-IDLE");
   558     if (ret)
   530     if (ret)
   645             ec_slave_request_state(eoe->slave, EC_SLAVE_STATE_OP);
   617             ec_slave_request_state(eoe->slave, EC_SLAVE_STATE_OP);
   646     }
   618     }
   647 #endif
   619 #endif
   648 
   620 
   649     master->phase = EC_OPERATION;
   621     master->phase = EC_OPERATION;
   650     master->app_send_cb = NULL;
   622     master->app_fsm_queue_lock_cb = NULL;
   651     master->app_receive_cb = NULL;
   623     master->app_fsm_queue_unlock_cb = NULL;
   652     master->app_cb_data = NULL;
   624     master->app_fsm_queue_locking_data = NULL;
   653     return ret;
   625     return ret;
   654     
   626     
   655 out_allow:
   627 out_allow:
   656     master->allow_scan = 1;
   628     master->allow_scan = 1;
   657     master->allow_config = 1;
   629     master->allow_config = 1;
   686         )
   658         )
   687 {
   659 {
   688     ec_datagram_t *datagram, *n;
   660     ec_datagram_t *datagram, *n;
   689     size_t queue_size = 0;
   661     size_t queue_size = 0;
   690 
   662 
       
   663     if (master->fsm_queue_lock_cb)
       
   664         master->fsm_queue_lock_cb(master->fsm_queue_locking_data);
   691     down(&master->fsm_queue_sem);
   665     down(&master->fsm_queue_sem);
   692     if (list_empty(&master->fsm_datagram_queue)) {
   666     if (list_empty(&master->fsm_datagram_queue)) {
   693         up(&master->fsm_queue_sem);
   667         up(&master->fsm_queue_sem);
       
   668         if (master->fsm_queue_unlock_cb)
       
   669             master->fsm_queue_unlock_cb(master->fsm_queue_locking_data);
   694         return;
   670         return;
   695     }
   671     }
   696     list_for_each_entry(datagram, &master->datagram_queue, queue) {
   672     list_for_each_entry(datagram, &master->datagram_queue, queue) {
   697         queue_size += datagram->data_size;
   673         queue_size += datagram->data_size;
   698     }
   674     }
   759 #endif
   735 #endif
   760             }
   736             }
   761         }
   737         }
   762     }
   738     }
   763     up(&master->fsm_queue_sem);
   739     up(&master->fsm_queue_sem);
       
   740     if (master->fsm_queue_unlock_cb)
       
   741         master->fsm_queue_unlock_cb(master->fsm_queue_locking_data);
   764 }
   742 }
   765 
   743 
   766 /*****************************************************************************/
   744 /*****************************************************************************/
   767 
   745 
   768 /** Sets the expected interval between calls to ecrt_master_send
   746 /** Sets the expected interval between calls to ecrt_master_send
   801         ec_datagram_t *datagram /**< datagram */
   779         ec_datagram_t *datagram /**< datagram */
   802         )
   780         )
   803 {
   781 {
   804     ec_datagram_t *queued_datagram;
   782     ec_datagram_t *queued_datagram;
   805 
   783 
       
   784     if (master->fsm_queue_lock_cb)
       
   785         master->fsm_queue_lock_cb(master->fsm_queue_locking_data);
   806     down(&master->fsm_queue_sem);
   786     down(&master->fsm_queue_sem);
   807 
   787 
   808     // check, if the datagram is already queued
   788     // check, if the datagram is already queued
   809     list_for_each_entry(queued_datagram, &master->fsm_datagram_queue,
   789     list_for_each_entry(queued_datagram, &master->fsm_datagram_queue,
   810             queue) {
   790             queue) {
   811         if (queued_datagram == datagram) {
   791         if (queued_datagram == datagram) {
   812             datagram->state = EC_DATAGRAM_QUEUED;
   792             datagram->state = EC_DATAGRAM_QUEUED;
   813             up(&master->fsm_queue_sem);
   793             up(&master->fsm_queue_sem);
       
   794             if (master->fsm_queue_unlock_cb)
       
   795                 master->fsm_queue_unlock_cb(master->fsm_queue_locking_data);
   814             return;
   796             return;
   815         }
   797         }
   816     }
   798     }
   817 
   799 
   818 #if DEBUG_INJECT
   800 #if DEBUG_INJECT
   826     datagram->cycles_sent = get_cycles();
   808     datagram->cycles_sent = get_cycles();
   827 #endif
   809 #endif
   828     datagram->jiffies_sent = jiffies;
   810     datagram->jiffies_sent = jiffies;
   829 
   811 
   830     up(&master->fsm_queue_sem);
   812     up(&master->fsm_queue_sem);
       
   813     if (master->fsm_queue_unlock_cb)
       
   814         master->fsm_queue_unlock_cb(master->fsm_queue_locking_data);
   831 }
   815 }
   832 
   816 
   833 /*****************************************************************************/
   817 /*****************************************************************************/
   834 
   818 
   835 /** Places a datagram in the datagram queue.
   819 /** Places a datagram in the datagram queue.
  1941     EC_MASTER_DBG(master, 1, "FSM datagram is %p.\n", &master->fsm_datagram);
  1925     EC_MASTER_DBG(master, 1, "FSM datagram is %p.\n", &master->fsm_datagram);
  1942 
  1926 
  1943     master->injection_seq_fsm = 0;
  1927     master->injection_seq_fsm = 0;
  1944     master->injection_seq_rt = 0;
  1928     master->injection_seq_rt = 0;
  1945 
  1929 
  1946     master->send_cb = master->app_send_cb;
  1930     master->fsm_queue_lock_cb = master->app_fsm_queue_lock_cb;
  1947     master->receive_cb = master->app_receive_cb;
  1931     master->fsm_queue_unlock_cb = master->app_fsm_queue_unlock_cb;
  1948     master->cb_data = master->app_cb_data;
  1932     master->fsm_queue_locking_data = master->app_fsm_queue_locking_data;
  1949     
  1933     
  1950     ret = ec_master_thread_start(master, ec_master_operation_thread,
  1934     ret = ec_master_thread_start(master, ec_master_operation_thread,
  1951                 "EtherCAT-OP");
  1935                 "EtherCAT-OP");
  1952     if (ret < 0) {
  1936     if (ret < 0) {
  1953         EC_MASTER_ERR(master, "Failed to start master thread!\n");
  1937         EC_MASTER_ERR(master, "Failed to start master thread!\n");
  1980         return;
  1964         return;
  1981     }
  1965     }
  1982 
  1966 
  1983     ec_master_thread_stop(master);
  1967     ec_master_thread_stop(master);
  1984     
  1968     
  1985     master->send_cb = ec_master_internal_send_cb;
  1969     master->fsm_queue_lock_cb = NULL;
  1986     master->receive_cb = ec_master_internal_receive_cb;
  1970     master->fsm_queue_unlock_cb= NULL;
  1987     master->cb_data = master;
  1971     master->fsm_queue_locking_data = NULL;
  1988     
  1972     
  1989     ec_master_clear_config(master);
  1973     ec_master_clear_config(master);
  1990 
  1974 
  1991     for (slave = master->slaves;
  1975     for (slave = master->slaves;
  1992             slave < master->slaves + master->slave_count;
  1976             slave < master->slaves + master->slave_count;
  2094             }
  2078             }
  2095         }
  2079         }
  2096     }
  2080     }
  2097 }
  2081 }
  2098 
  2082 
  2099 /*****************************************************************************/
       
  2100 
       
  2101 void ecrt_master_send_ext(ec_master_t *master)
       
  2102 {
       
  2103     ecrt_master_send(master);
       
  2104 }
       
  2105 
  2083 
  2106 /*****************************************************************************/
  2084 /*****************************************************************************/
  2107 
  2085 
  2108 /** Same as ecrt_master_slave_config(), but with ERR_PTR() return value.
  2086 /** Same as ecrt_master_slave_config(), but with ERR_PTR() return value.
  2109  */
  2087  */
  2224 }
  2202 }
  2225 
  2203 
  2226 /*****************************************************************************/
  2204 /*****************************************************************************/
  2227 
  2205 
  2228 void ecrt_master_callbacks(ec_master_t *master,
  2206 void ecrt_master_callbacks(ec_master_t *master,
  2229         void (*send_cb)(void *), void (*receive_cb)(void *), void *cb_data)
  2207                            void (*lock_cb)(void *), void (*unlock_cb)(void *),
  2230 {
  2208                            void *cb_data)
  2231     EC_MASTER_DBG(master, 1, "ecrt_master_callbacks(master = 0x%p,"
  2209 {
  2232             " send_cb = 0x%p, receive_cb = 0x%p, cb_data = 0x%p)\n",
  2210     EC_MASTER_DBG(master, 1,"ecrt_master_callbacks(master = %p, "
  2233             master, send_cb, receive_cb, cb_data);
  2211                             "lock_cb = %p, unlock_cb = %p, cb_data = %p)\n",
  2234 
  2212                             master, lock_cb, unlock_cb, cb_data);
  2235     master->app_send_cb = send_cb;
  2213 
  2236     master->app_receive_cb = receive_cb;
  2214     master->app_fsm_queue_lock_cb = lock_cb;
  2237     master->app_cb_data = cb_data;
  2215     master->app_fsm_queue_unlock_cb = unlock_cb;
  2238 }
  2216     master->app_fsm_queue_locking_data = cb_data;
       
  2217 }
       
  2218 
  2239 
  2219 
  2240 /*****************************************************************************/
  2220 /*****************************************************************************/
  2241 
  2221 
  2242 void ecrt_master_state(const ec_master_t *master, ec_master_state_t *state)
  2222 void ecrt_master_state(const ec_master_t *master, ec_master_state_t *state)
  2243 {
  2223 {
  2480 
  2460 
  2481 EXPORT_SYMBOL(ecrt_master_create_domain);
  2461 EXPORT_SYMBOL(ecrt_master_create_domain);
  2482 EXPORT_SYMBOL(ecrt_master_activate);
  2462 EXPORT_SYMBOL(ecrt_master_activate);
  2483 EXPORT_SYMBOL(ecrt_master_deactivate);
  2463 EXPORT_SYMBOL(ecrt_master_deactivate);
  2484 EXPORT_SYMBOL(ecrt_master_send);
  2464 EXPORT_SYMBOL(ecrt_master_send);
  2485 EXPORT_SYMBOL(ecrt_master_send_ext);
       
  2486 EXPORT_SYMBOL(ecrt_master_receive);
  2465 EXPORT_SYMBOL(ecrt_master_receive);
  2487 EXPORT_SYMBOL(ecrt_master_callbacks);
  2466 EXPORT_SYMBOL(ecrt_master_callbacks);
  2488 EXPORT_SYMBOL(ecrt_master);
  2467 EXPORT_SYMBOL(ecrt_master);
  2489 EXPORT_SYMBOL(ecrt_master_get_slave);
  2468 EXPORT_SYMBOL(ecrt_master_get_slave);
  2490 EXPORT_SYMBOL(ecrt_master_slave_config);
  2469 EXPORT_SYMBOL(ecrt_master_slave_config);