Microprocessor and peripheral 2 assignments for AUTH
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 lines
3.4 KiB

  1. /*!
  2. * \file
  3. * main.c
  4. * \brief
  5. * Main application file
  6. *
  7. * Created on: May 23, 2020
  8. * Author: Christos Choutouridis AEM: 8997
  9. * email : <cchoutou@ece.auth.gr>
  10. */
  11. #include "assign2_impl.h"
  12. /*
  13. * Global data
  14. */
  15. stats_t stats;
  16. /*!
  17. * Compare functionality for qsort
  18. * \param a left hand site
  19. * \param b right hand site
  20. * \return stdlib requirements
  21. * \arg -1 a<b
  22. * \arg 0 a==b
  23. * \arg 1 a>b
  24. */
  25. static int cmpfunc (const void * a, const void * b) {
  26. fp_data_t v = *(fp_data_t*)a - *(fp_data_t*)b;
  27. return (v < 0) ? -1 : (v > 0) ? 1 : 0;
  28. }
  29. /*!
  30. * Calculates and return the average of an array of measurements
  31. * \param t Pointer to measurements
  32. * \param n Size of measurements array
  33. * \return The average
  34. */
  35. fp_data_t average (const clock_t *t, size_t n) {
  36. fp_data_t ret =0;
  37. for (size_t i=0 ; i<n ; ++i)
  38. ret += t[i];
  39. return ret / n;
  40. }
  41. /*!
  42. * Calculates and return the median of an array of measurements
  43. * \param t Pointer to measurements
  44. * \param n Size of measurements array
  45. * \return The average
  46. */
  47. fp_data_t median (const clock_t *t, size_t n) {
  48. qsort ((void*)t, n, sizeof(t[0]), cmpfunc);
  49. return (n % 2) ? t[n/2] : (t[n/2] + t[n/2 -1]) /2;
  50. }
  51. /*!
  52. * Calculates and return the std. deviation of an array of measurements
  53. * \param t Pointer to measurements
  54. * \param n Size of measurements array
  55. * \return The average
  56. */
  57. fp_data_t std_deviation (const clock_t* t, size_t n) {
  58. fp_data_t av = average (t, n);
  59. fp_data_t s =0;
  60. for (size_t i=0 ; i<n ; ++i) {
  61. s += (t[i]-av)*(t[i]-av);
  62. }
  63. return sqrt (s/n);
  64. }
  65. /*!
  66. * Leading edge trigger experiment
  67. * \param out Pointer to array to store the measurements
  68. * \param n Number of measurements
  69. */
  70. void leading (clock_t *out, size_t n) {
  71. srand(0);
  72. rand();
  73. LED (OFF);
  74. for (size_t i =0 ; i<n ; ++i) {
  75. clock_t t1, t2;
  76. HAL_Delay(rand() % (MAX_WAIT_TIME + 1));
  77. LED(ON);
  78. t1 = clock ();
  79. while (BTN())
  80. ;
  81. t2 = clock ();
  82. LED (OFF);
  83. out[i] = CPUtime2msec(_CLOCK_DIFF(t2, t1));
  84. }
  85. }
  86. /*!
  87. * Trailing edge trigger experiment
  88. * \param out Pointer to array to store the measurements
  89. * \param n Number of measurements
  90. */
  91. void trailing (clock_t *out, size_t n) {
  92. srand(0);
  93. rand();
  94. LED (ON);
  95. for (size_t i =0 ; i<n ; ++i) {
  96. clock_t t1, t2;
  97. HAL_Delay(rand() % (MAX_WAIT_TIME + 1));
  98. LED(OFF);
  99. t1 = clock ();
  100. while (BTN())
  101. ;
  102. t2 = clock ();
  103. LED (ON);
  104. out[i] = CPUtime2msec(_CLOCK_DIFF(t2, t1));
  105. }
  106. }
  107. /*
  108. * Main
  109. */
  110. int main(void) {
  111. clock_t times[MEASUREMENTS];
  112. LLD_Init (SYSTICK_FREQ); // Initialize the board
  113. // Experiment
  114. #if MODE == MODE_LEADING_EDGE
  115. leading (times, MEASUREMENTS);
  116. #elif MODE == MODE_TRAILING_EDGE
  117. trailing (times, MEASUREMENTS);
  118. #endif
  119. // Get statistical data
  120. stats.average = average ((const clock_t*)times, MEASUREMENTS);
  121. stats.median = median ((const clock_t*)times, MEASUREMENTS);
  122. stats.std_dev = std_deviation((const clock_t*)times, MEASUREMENTS);
  123. // Flash 10Hz to indicate the end of experiment
  124. while (1) {
  125. HAL_Delay(msec2CPUtime(50));
  126. LED (ON);
  127. HAL_Delay(msec2CPUtime(50));
  128. LED (OFF);
  129. }
  130. }