interrupts.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "stm32f4xx_hal.h"
  2. #include <stdio.h>
  3. #include "led_blink.h"
  4. #include "interrupts.h"
  5. #include "receiver.h"
  6. #include "sw_fifo_1.h"
  7. #include "sw_fifo_2.h"
  8. #include "queue.h"
  9. // Externals
  10. extern TIM_HandleTypeDef htim3;
  11. // Internals
  12. void doTimingStuff();
  13. int logData[LOG_SIZE];
  14. int logCounter = 0;
  15. #define SAMPLES_TO_SWITCH 5
  16. void ASKreceiver() {
  17. static unsigned char currValue = 0;
  18. static int samples = 0;
  19. static int totSamples = 0;
  20. if( receiverEnabled == 0 ) return; // Exit if the receiver's not been started
  21. // Sample the pin value
  22. unsigned char value = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0); // PIN PA0 on Nucleo board
  23. totSamples+=10;
  24. if( value != currValue ) {
  25. samples++;
  26. }
  27. else {
  28. samples--;
  29. }
  30. if( samples >= SAMPLES_TO_SWITCH ) {
  31. samples = 0;
  32. currValue = !currValue;
  33. logData[logCounter++] = totSamples;
  34. if( logCounter == LOG_SIZE ) {
  35. logCounter=0;
  36. }
  37. rcvEnQueue(totSamples);
  38. totSamples = 0;
  39. }
  40. if( samples < 0 ) samples = 0;
  41. }
  42. void ASKreceiver_old() {
  43. static unsigned char currValue = 0;
  44. static unsigned short samples = 0;
  45. static unsigned short newSamples = 0;
  46. if( receiverEnabled == 0 ) return; // Exit if the receiver's not been started
  47. // Sample the pin value
  48. unsigned char value = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0); // PIN PA0 on Nucleo board
  49. if( value == currValue ) {
  50. samples++;
  51. samples+=newSamples;
  52. newSamples=0;
  53. }
  54. else {
  55. newSamples++;
  56. }
  57. if( newSamples == 5 ) {
  58. logData[logCounter++] = samples * 10;
  59. if( logCounter == LOG_SIZE ) {
  60. logCounter=0;
  61. }
  62. rcvEnQueue(samples * 10);
  63. samples = newSamples;
  64. newSamples = 0;
  65. currValue = value;
  66. }
  67. }
  68. //=================================== TIM2 ===================================
  69. // This function is called every 10uS (clk/100/10)
  70. // This interrupt handles the sampling of the 433MHz receiver module.
  71. // Sends the filtered data to a receive queue.
  72. // This interrupt has the highest prio in the system
  73. //============================================================================
  74. void handle_TIM2_interrupt() {
  75. doTimingStuff();
  76. ASKreceiver();
  77. }
  78. //=========================== doTimingStuff ==================================
  79. // This function handles updating of all time-keeping timers in the system.
  80. // Remember to turn of the sys clock and tim3
  81. //============================================================================
  82. unsigned long mSTick = 0;
  83. unsigned long long uSTickX10 = 0;
  84. #pragma optimize=no_cse
  85. void doTimingStuff() {
  86. static int mSCounter = 0;
  87. uSTickX10++;
  88. if( (++mSCounter) == 100 ) {
  89. mSCounter = 0;
  90. mSTick++;
  91. }
  92. }
  93. // Function returns number of uS since startup
  94. // Returns an unsigned long long
  95. unsigned long long micros() {
  96. return (uSTickX10*10);
  97. }
  98. #pragma optimize=no_cse
  99. void delayMicroseconds(unsigned long long delay)
  100. {
  101. unsigned long long endtime = micros() + delay;
  102. while( micros() < endtime);
  103. }
  104. extern unsigned long millis() {
  105. return mSTick;
  106. }
  107. #pragma optimize=no_cse
  108. extern void delay(unsigned long delay) {
  109. unsigned long endtime = mSTick + delay;
  110. while( mSTick < endtime);
  111. }
  112. //=================================== TIM3 ===================================
  113. // This function is called every 1mS (clk/100/1000)
  114. // This timer/interrupt is responsible for the time keeping in the
  115. // micron() function.
  116. //============================================================================
  117. unsigned long tim3mSTick = 0;
  118. void handle_TIM3_interrupt() {
  119. tim3mSTick++;
  120. }
  121. /*
  122. // Function returns number of uS since startup
  123. // Returns an unsigned long long
  124. unsigned long long micros() {
  125. volatile static unsigned long long mS=0,uS=0;
  126. __HAL_TIM_DISABLE_IT(&htim3, TIM_IT_UPDATE);
  127. uS = TIM3->CNT; // 1uS clock tick
  128. mS = tim3mSTick;
  129. __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);
  130. return ((mS * 1000ULL) + uS);
  131. }
  132. void delayMicroseconds(unsigned int delay)
  133. {
  134. volatile unsigned long long endtime = micros() + ((unsigned long long)delay);
  135. while( micros() < endtime);
  136. }
  137. */
  138. //================================== SysTick =================================
  139. // This function is called every 1mS
  140. //============================================================================
  141. void handle_SysTick_interrupt() {
  142. const int LED_BLINK_START_VALUE = 200;
  143. static int i=200;
  144. if( (--i) == 0 ) {
  145. i= LED_BLINK_START_VALUE;
  146. led_interrupt();
  147. }
  148. receiver();
  149. }
  150. //================================== RTC =====================================
  151. // This function is called every 1 minutes
  152. //============================================================================
  153. int newMinuteEvent = 0;
  154. void handle_RTC_interrupt() {
  155. newMinuteEvent++;
  156. }
  157. //================================== USART1 ==================================
  158. //============================================================================
  159. void handle_USART1_interrupt() {
  160. uart1Interrupt();
  161. }
  162. //================================== USART2 ==================================
  163. //============================================================================
  164. void handle_USART2_interrupt() {
  165. uart2Interrupt();
  166. }