main.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <gfatimer.h>
  7. #define _NANOSECS_PER_SEC 1000000000
  8. static long long _TimeSpecDiffNs(const struct timespec *pts1, const struct timespec *pts2)
  9. {
  10. long long ns1 = (long long)pts1->tv_sec * _NANOSECS_PER_SEC + pts1->tv_nsec;
  11. long long ns2 = (long long)pts2->tv_sec * _NANOSECS_PER_SEC + pts2->tv_nsec;
  12. return ns2 - ns1;
  13. }
  14. int main(int argc, char **argv)
  15. {
  16. long long elps1, elps2;
  17. int tnum1 = 3, tnum2 = 14, nPulse, nOldPulse = 0;
  18. unsigned long tw1 = 1, tw2 = 1;
  19. struct timespec tsRes, tsStart, tsEnd1, tsEnd2;
  20. memset(&tsEnd1, 0, sizeof(tsEnd1));
  21. memset(&tsEnd2, 0, sizeof(tsEnd2));
  22. if(!GfaTimerInit(32, GTR_25ms))
  23. {
  24. printf("GfaTimerInit failed!\n");
  25. return 1;
  26. }
  27. tf_set(tnum1);
  28. tf_set(tnum2);
  29. tw_set(tnum1, 15000);
  30. tw_set(tnum2, 10000);
  31. clock_gettime(CLOCK_MONOTONIC, &tsStart);
  32. do
  33. {
  34. nPulse = cp_test(GTCP_750ms);
  35. if(nOldPulse != nPulse)
  36. {
  37. nOldPulse = nPulse;
  38. printf("Pulse 750ms %d\n", nPulse);
  39. }
  40. if(tw1)
  41. {
  42. if(!(tw1 = tw_read(tnum1)))
  43. {
  44. clock_gettime(CLOCK_MONOTONIC, &tsEnd1);
  45. printf("T%d expired.\n", tnum1);
  46. }
  47. }
  48. if(tw2)
  49. {
  50. if(!(tw2 = tw_read(tnum2)))
  51. {
  52. clock_gettime(CLOCK_MONOTONIC, &tsEnd2);
  53. printf("T%d expired.\n", tnum2);
  54. }
  55. }
  56. usleep(25000);
  57. // printf("T%d: %3lu, T%d: %3lu\n", tnum1, tw1, tnum2, tw2);
  58. }
  59. while(tw1 || tw2);
  60. elps1 = _TimeSpecDiffNs(&tsStart, &tsEnd1);
  61. elps2 = _TimeSpecDiffNs(&tsStart, &tsEnd2);
  62. printf("\nT%d effective: %lld ms.\n", tnum1, elps1 / 1000000);
  63. printf("T%d effective: %lld ms.\n", tnum2, elps2 / 1000000);
  64. return 0;
  65. }