time_measure.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "bsp/bsp_driver.h"
  2. #include "os/os_task.h"
  3. #include "libs/time_measure.h"
  4. #define COUNT_2_US(c) (c/(SYSTEM_CLOCK/1000000))
  5. u32 time_delta_us(u32 count, u32 *p_update) {
  6. u32 now = task_ticks_abs();
  7. u32 delta = now - count;
  8. if (now < count) { //wrap
  9. delta = 0xFFFFFFFF - count + now + 1;
  10. }
  11. if (p_update) {
  12. *p_update = now;
  13. }
  14. return COUNT_2_US(delta);
  15. }
  16. void time_measure_start(measure_time_t *m){
  17. m->intval_time = time_delta_us(m->intval_count, &m->intval_count);
  18. m->exec_count = task_ticks_abs();
  19. if (m->first) {
  20. m->first = false;
  21. return;
  22. }
  23. if (m->intval_time > m->intval_max_time+5) {
  24. m->intval_time_h_error ++;
  25. m->intval_hi_err = m->intval_time;
  26. }
  27. if (m->intval_time < m->intval_max_time-5) {
  28. m->intval_time_l_error ++;
  29. m->intval_low_err = m->intval_time;
  30. }
  31. }
  32. void time_measure_end(measure_time_t *m) {
  33. if (m->first) {
  34. return;
  35. }
  36. m->exec_time = time_delta_us(m->exec_count, NULL);
  37. if (m->exec_time > m->exec_max_time) {
  38. m->exec_time_error ++;
  39. if (m->exec_time > m->exec_max_error_time) {
  40. m->exec_max_error_time = m->exec_time;
  41. }
  42. }
  43. }