master/master.c
changeset 330 b984763cecc2
parent 326 ddb48b173680
child 331 17f58fe99511
equal deleted inserted replaced
329:d004349777fc 330:b984763cecc2
   947         slave->coupler_subindex = coupler_subindex;
   947         slave->coupler_subindex = coupler_subindex;
   948         coupler_subindex++;
   948         coupler_subindex++;
   949     }
   949     }
   950 }
   950 }
   951 
   951 
       
   952 /*****************************************************************************/
       
   953 
       
   954 /**
       
   955    Measures the time, a frame is on the bus.
       
   956 */
       
   957 
       
   958 void ec_master_measure_bus_time(ec_master_t *master)
       
   959 {
       
   960     ec_datagram_t datagram;
       
   961     cycles_t t_start, t_end, t_timeout;
       
   962     uint32_t times[100], sum, min, max, i;
       
   963 
       
   964     ec_datagram_init(&datagram);
       
   965 
       
   966     if (ec_datagram_brd(&datagram, 0x130, 2)) {
       
   967         EC_ERR("Failed to allocate datagram for bus time measuring.\n");
       
   968         ec_datagram_clear(&datagram);
       
   969         return;
       
   970     }
       
   971 
       
   972     t_timeout = (cycles_t) EC_IO_TIMEOUT * (cpu_khz / 1000);
       
   973 
       
   974     sum = 0;
       
   975     min = 0xFFFFFFFF;
       
   976     max = 0;
       
   977 
       
   978     for (i = 0; i < 100; i++) {
       
   979         ec_master_queue_datagram(master, &datagram);
       
   980         ecrt_master_send(master);
       
   981         t_start = get_cycles();
       
   982 
       
   983         while (1) { // active waiting
       
   984             ec_device_call_isr(master->device);
       
   985             t_end = get_cycles(); // take current time
       
   986 
       
   987             if (datagram.state == EC_DATAGRAM_RECEIVED) {
       
   988                 break;
       
   989             }
       
   990             else if (datagram.state == EC_DATAGRAM_ERROR) {
       
   991                 EC_WARN("Failed to measure bus time.\n");
       
   992                 goto error;
       
   993             }
       
   994             else if (t_end - t_start >= t_timeout) {
       
   995                 EC_WARN("Timeout while measuring bus time.\n");
       
   996                 goto error;
       
   997             }
       
   998         }
       
   999 
       
  1000         times[i] = (unsigned int) (t_end - t_start) * 1000 / cpu_khz;
       
  1001         sum += times[i];
       
  1002         if (times[i] > max) max = times[i];
       
  1003         if (times[i] < min) min = times[i];
       
  1004     }
       
  1005 
       
  1006     EC_INFO("Bus time is (min/avg/max) %u/%u.%u/%u us.\n",
       
  1007             min, sum / 100, sum % 100, max);
       
  1008 
       
  1009   error:
       
  1010     // Dequeue and free datagram
       
  1011     list_del(&datagram.queue);
       
  1012     ec_datagram_clear(&datagram);
       
  1013 }
       
  1014 
   952 /******************************************************************************
  1015 /******************************************************************************
   953  *  Realtime interface
  1016  *  Realtime interface
   954  *****************************************************************************/
  1017  *****************************************************************************/
   955 
  1018 
   956 /**
  1019 /**