time_measure.c 1009 B

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