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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // A function to implement bubble sort
  17. void bubbleSort(clock_t arr[], size_t n) {
  18. size_t i, j;
  19. clock_t t;
  20. for (i =0; i <n-1; ++i)
  21. // Last i elements are already in place
  22. for (j = 0; j<n-i-1; ++j)
  23. if (arr[j] > arr[j+1]) {
  24. t = arr[j];
  25. arr[j] = arr[j+1];
  26. arr[j+1] = t;
  27. }
  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 (clock_t *t, size_t n) {
  48. bubbleSort (t, n);
  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 (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. }