systick.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "stm32f0xx_hal.h"
  2. #include "systick.h"
  3. //#include "string.h"
  4. //#include "led.h"
  5. //#include "buttons.h"
  6. //#include <stdint.h>
  7. #include <stdbool.h>
  8. typedef struct
  9. {
  10. TTimerHandler Handler;
  11. uint16_t Countdown;
  12. uint16_t Reload;
  13. bool Run; // Запущено
  14. bool Fired; // Сработало
  15. } THandlers;
  16. volatile uint32_t TimingDelay;
  17. volatile uint8_t StopDelayFlag = 0;
  18. static THandlers Handlers[TIMER_HANDLERS];
  19. static uint32_t TimerFrequency;
  20. static int TimerCount;
  21. static uint8_t counter100hz = 0;
  22. void IWDG_Feed(void);
  23. //
  24. void SysTick_Handler(void)
  25. {
  26. //IWDG_Feed();
  27. HAL_IncTick();
  28. TimingDelay_Decrement();
  29. counter100hz++;
  30. if(counter100hz == 10)
  31. {
  32. counter100hz = 0;
  33. //LED_Blink();
  34. //BUTTON_CommonHandler();
  35. }
  36. for (int i = 0; i < TIMER_HANDLERS; i++)
  37. {
  38. if(Handlers[i].Run)
  39. {
  40. if(--Handlers[i].Countdown == 0)
  41. {
  42. Handlers[i].Countdown = Handlers[i].Reload;
  43. // Запишем, что сработало
  44. Handlers[i].Fired = true;
  45. }
  46. }
  47. }
  48. }
  49. // Запуск таймера с заданной частотой
  50. void timer_Init(uint32_t Frequency)
  51. {
  52. #if 0
  53. if(!Frequency) return;
  54. // Сбросим параметры
  55. TimerCount = 0;
  56. TimerFrequency = Frequency;
  57. memset(&Handlers[0], 0, sizeof(Handlers));
  58. // Настройка частоты
  59. SysTick_Config(clock_GetSYS() / Frequency);
  60. // Разрешение прерывания
  61. // NVIC_EnableIRQ(SysTick_IRQn);
  62. #endif
  63. }
  64. // Добавить функцию в список вызова. Handler будет вызываться с заданной частотой
  65. void timer_AddFunction(uint16_t Frequency, TTimerHandler Handler)
  66. {
  67. for (int i = 0; i < TIMER_HANDLERS; i++)
  68. {
  69. // Найдем пустой слот
  70. if(!Handlers[i].Handler)
  71. {
  72. // Обработчик, частота опроса
  73. Handlers[i].Run = true;
  74. Handlers[i].Fired = false;
  75. Handlers[i].Handler = Handler;
  76. //Handlers[i].Reload = (TimerFrequency / Frequency);
  77. Handlers[i].Reload = Frequency;
  78. Handlers[i].Countdown = Handlers[i].Reload;
  79. TimerCount++;
  80. return;
  81. }
  82. }
  83. }
  84. // Изменить частоту таймера
  85. void timer_ChangeFrequency(TTimerHandler Handler, uint16_t Frequency)
  86. {
  87. for (int i = 0; i < TIMER_HANDLERS; i++)
  88. {
  89. if(Handlers[i].Handler == Handler)
  90. {
  91. Handlers[i].Reload = (TimerFrequency / Frequency);
  92. Handlers[i].Countdown = Handlers[i].Reload;
  93. break;
  94. }
  95. }
  96. }
  97. // Включить таймер
  98. void timer_Resume(TTimerHandler Handler)
  99. {
  100. for (int i = 0; i < TIMER_HANDLERS; i++)
  101. {
  102. if(Handlers[i].Handler == Handler)
  103. {
  104. Handlers[i].Run = true;
  105. break;
  106. }
  107. }
  108. }
  109. // Перезапустить таймер
  110. void timer_Restart(TTimerHandler Handler)
  111. {
  112. for (int i = 0; i < TIMER_HANDLERS; i++)
  113. {
  114. if(Handlers[i].Handler == Handler)
  115. {
  116. Handlers[i].Run = true;
  117. Handlers[i].Countdown = Handlers[i].Reload;
  118. break;
  119. }
  120. }
  121. }
  122. //
  123. void timer_RestartAtOnce(TTimerHandler Handler)
  124. {
  125. for (int i = 0; i < TIMER_HANDLERS; i++)
  126. {
  127. if(Handlers[i].Handler == Handler)
  128. {
  129. Handlers[i].Run = true;
  130. Handlers[i].Countdown = 1;
  131. break;
  132. }
  133. }
  134. }
  135. // Остановить таймер
  136. void timer_Stop(TTimerHandler Handler)
  137. {
  138. for (int i = 0; i < TIMER_HANDLERS; i++)
  139. {
  140. if(Handlers[i].Handler == Handler)
  141. {
  142. Handlers[i].Run = false;
  143. Handlers[i].Fired = false;
  144. break;
  145. }
  146. }
  147. }
  148. //
  149. void timer_Main(void)
  150. {
  151. for(int i = 0; i < TIMER_HANDLERS; i++)
  152. {
  153. // Если сраотало - вызовем
  154. if(Handlers[i].Fired)
  155. {
  156. Handlers[i].Fired = false;
  157. Handlers[i].Handler();
  158. }
  159. }
  160. }
  161. // -----------------------------------------------------------------------------
  162. // Вспомогательные функции
  163. // -----------------------------------------------------------------------------
  164. uint32_t clock_GetSYS(void)
  165. {
  166. return SystemCoreClock;
  167. }
  168. // -----------------------------------------------------------------------------
  169. /*
  170. void Delay_ms(uint32_t nTime)
  171. {
  172. TimingDelay = nTime;
  173. while(TimingDelay);
  174. }
  175. */
  176. //
  177. void SYSTICK_SetDelayFlag()
  178. {
  179. StopDelayFlag = 1;
  180. }
  181. //
  182. uint8_t Delay_ms(uint32_t nTime)
  183. {
  184. TimingDelay = nTime;
  185. while(TimingDelay);
  186. if (StopDelayFlag) {
  187. StopDelayFlag = 0;
  188. return 1;
  189. }
  190. else {
  191. return 0;
  192. }
  193. }
  194. // -----------------------------------------------------------------------------
  195. void TimingDelay_Decrement(void)
  196. {
  197. if (TimingDelay)
  198. TimingDelay--;
  199. }
  200. //******************************** (C) POTEK ***********************************