time_measure.c 993 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/120)
  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. m->exec_time = time_delta_us(m->exec_count, NULL);
  34. if (m->exec_time > m->exec_max_time) {
  35. m->exec_time_error ++;
  36. m->exec_max_error_time = m->exec_time;
  37. }
  38. }