HW_TIMER_ATY.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * @file HW_TIMER_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of timer definition for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220602 > ATY
  23. * -# Preliminary version, first Release
  24. * - Undone
  25. ********************************************************************************
  26. */
  27. #ifndef __HW_TIMER_ATY_C
  28. #define __HW_TIMER_ATY_C
  29. #include "HW_TIMER_ATY.h"
  30. /******************************* For user *************************************/
  31. #if defined(__STC51_ATY)
  32. /**
  33. * @brief 1 us delay with for @ 24MHz about 1.04us
  34. */
  35. __WEAK_ATY void Delay1Us(void)
  36. {
  37. // uint8_t i = 2; // for 12MHz
  38. uint8_t i = 6; // for 24MHz
  39. while(--i);
  40. }
  41. /**
  42. * @brief us delay
  43. * @param us us
  44. */
  45. __WEAK_ATY void DelayUs(uint32_t us)
  46. {
  47. // uint32_t i = 2 + 4 * us; // for 12MHz
  48. uint32_t i = 6 + 8 * us; // for 12MHz
  49. // while(--i);
  50. }
  51. /**
  52. * @brief ms delay
  53. * @param ms ms
  54. */
  55. __WEAK_ATY void DelayMs(uint32_t ms)
  56. {
  57. while(ms--)
  58. DelayUs(1000);
  59. }
  60. #if TIMERC_N == 0
  61. /**
  62. * @brief cycle timer init
  63. * @note put at main init
  64. */
  65. void TimerLoopInit(void) //1ms@24.000MHz
  66. {
  67. AUXR |= 0x80; // timer clock 1T mode
  68. TMOD &= 0xF0; // set timer mode
  69. TL0 = 0x40; // set timer origin value
  70. TH0 = 0xA2; // set timer origin value
  71. TF0 = 0; // clear TF0 flag
  72. TR0 = 1; // start timer 0
  73. ET0 = 1; // enable timer IT
  74. EA = 1; // enable all IT
  75. }
  76. /**
  77. * @brief cycle timer IT callback function
  78. */
  79. void TM0_Isr(void) INTERRUPT(1)
  80. {
  81. UserTimerLoop_Cycle1ms();
  82. }
  83. #elif TIMERC_N == 1
  84. /**
  85. * @brief cycle timer init
  86. * @note put at main init
  87. */
  88. void TimerLoopInit(void) //1ms@24.000MHz
  89. {
  90. AUXR |= 0x40; // timer clock 1T mode
  91. TMOD &= 0x0F; // set timer mode
  92. TL1 = 0x40; // set timer origin value
  93. TH1 = 0xA2; // set timer origin value
  94. TF1 = 0; // clear TF1 flag
  95. TR1 = 1; // start timer 1
  96. ET1 = 1; // enable timer IT
  97. EA = 1; // enable all IT
  98. }
  99. /**
  100. * @brief cycle timer IT callback function
  101. */
  102. void TM1_Isr(void) INTERRUPT(3)
  103. {
  104. UserTimerLoop_Cycle1ms();
  105. }
  106. #elif TIMERC_N == 2
  107. void TM2_Isr(void) INTERRUPT(12)
  108. {
  109. UserTimerLoop_Cycle1ms();
  110. }
  111. #elif TIMERC_N == 3
  112. void TM3_Isr(void) INTERRUPT(19)
  113. {
  114. UserTimerLoop_Cycle1ms();
  115. }
  116. #elif TIMERC_N == 4
  117. void TM4_Isr(void) INTERRUPT(20)
  118. {
  119. UserTimerLoop_Cycle1ms();
  120. }
  121. #endif /* TIMERC_N */
  122. #elif defined(__STM32_HAL_ATY)
  123. #include "tim.h"
  124. /**
  125. * @brief us delay with soft cycle
  126. * @param us us
  127. * @note @ 8MHz(PCLK1 = 36MHz && Others = 72MHz), about 0.9722us
  128. */
  129. __WEAK_ATY void Delay1Us(void)
  130. {
  131. uint8_t i = 12;
  132. while(--i);
  133. }
  134. /**
  135. * @brief us delay
  136. * @param us us
  137. */
  138. __WEAK_ATY void DelayUs(uint32_t us)
  139. {
  140. uint32_t i = 18 * us;
  141. while(--i);
  142. }
  143. /**
  144. * @brief ms delay
  145. * @param ms ms
  146. * @note long time with cycle is not recommended
  147. */
  148. __WEAK_ATY void DelayMs(uint32_t ms)
  149. {
  150. while(ms--)
  151. DelayUs(1000);
  152. // HAL_Delay(ms); // HAL_Delay not good for little time
  153. }
  154. /**
  155. * @brief cycle timer IT callback function
  156. * @note HAL_TIM_Base_Start_IT(&TIMERC_N); start IT at main init
  157. */
  158. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
  159. {
  160. if(htim == &TIMERC_N)
  161. {
  162. UserTimerLoop_Cycle1ms();
  163. }
  164. }
  165. #endif /* PLATFORM */
  166. /******************************************************************************/
  167. struct _timeBaseCounter_ATY timeCounterUnit_ATY = {0};
  168. /**
  169. * @brief sec delay
  170. * @param sec sec
  171. */
  172. __WEAK_ATY void DelaySec(uint32_t sec)
  173. {
  174. while(sec--)
  175. DelayMs(1000);
  176. }
  177. #if 0
  178. uint8_t it = 0;
  179. void DelayTest(void)
  180. {
  181. it = 0;
  182. it++;
  183. Delay1Us();
  184. it++;
  185. Delay1Us();
  186. it++;
  187. DelayUs(2);
  188. it++;
  189. DelayUs(2);
  190. it++;
  191. DelayUs(10);
  192. it++;
  193. DelayUs(10);
  194. it++;
  195. DelayUs(100);
  196. it++;
  197. DelayUs(100);
  198. it++;
  199. DelayMs(1);
  200. it++;
  201. DelayMs(1);
  202. it++;
  203. DelayMs(2);
  204. it++;
  205. DelayMs(2);
  206. it++;
  207. DelayMs(10);
  208. it++;
  209. DelayMs(10);
  210. it++;
  211. DelayMs(100);
  212. it++;
  213. DelayMs(100);
  214. #ifdef __STM32_HAL_ATY
  215. it++;
  216. HAL_Delay(100);
  217. it++;
  218. HAL_Delay(100);
  219. #endif /* __STM32_HAL_ATY */
  220. it++;
  221. DelaySec(1);
  222. it++;
  223. DelaySec(1);
  224. it++;
  225. it = 0;
  226. }
  227. #endif /* 0 */
  228. #ifdef __TIMER_CYCLE_ATY
  229. /**
  230. * @brief define at user file and put at main while
  231. * @note long time process is not recommended
  232. */
  233. __WEAK_ATY void UserTimerLoopProcess(void)
  234. {
  235. TimerLoopProcess_User();
  236. }
  237. #ifndef __STC51_ATY
  238. __WEAK_ATY void TimerLoopProcess_User(void)
  239. {
  240. // if(timeCounterUnit_ATY.ms1_f == 1)
  241. // {
  242. // timeCounterUnit_ATY.ms1_f = 0;
  243. // }
  244. if(timeCounterUnit_ATY.ms100_f == 1)
  245. {
  246. timeCounterUnit_ATY.ms100_f = 0;
  247. ledBlinkType = (2);
  248. // HAL_IWDG_Refresh(&hiwdg);
  249. }
  250. // if(timeCounterUnit_ATY.s2_f == 1)
  251. // {
  252. // timeCounterUnit_ATY.s2_f = 0;
  253. // }
  254. }
  255. #endif /* __STC51_ATY */
  256. /**
  257. * @brief 1ms timer loop cycle
  258. */
  259. void UserTimerLoop_Cycle1ms(void)
  260. {
  261. // put at 1ms timer IT callback function
  262. timeCounterUnit_ATY.msN++;
  263. if(timeCounterUnit_ATY.ms10_f == 0)
  264. timeCounterUnit_ATY.ms10++;
  265. if(timeCounterUnit_ATY.ms100_f == 0)
  266. timeCounterUnit_ATY.ms100++;
  267. if(timeCounterUnit_ATY.s1_f == 0)
  268. timeCounterUnit_ATY.s1++;
  269. if(timeCounterUnit_ATY.s2_f == 0)
  270. timeCounterUnit_ATY.s2++;
  271. if(timeCounterUnit_ATY.s10_f == 0)
  272. timeCounterUnit_ATY.s10++;
  273. /* ATY: 1ms unit **********************************************************/
  274. timeCounterUnit_ATY.ms1_f = 1;
  275. /* ATY: 10ms unit *********************************************************/
  276. if(timeCounterUnit_ATY.ms10 >= 10)
  277. {
  278. timeCounterUnit_ATY.ms10_f = 1;
  279. timeCounterUnit_ATY.ms10 = 0;
  280. }
  281. /* ATY: 100ms unit ********************************************************/
  282. if(timeCounterUnit_ATY.ms100 >= 100)
  283. {
  284. timeCounterUnit_ATY.ms100_f = 1;
  285. timeCounterUnit_ATY.ms100 = 0;
  286. // ledBlinkType = (2);
  287. }
  288. /* ATY: 1s unit ***********************************************************/
  289. if(timeCounterUnit_ATY.s1 >= 1000)
  290. {
  291. timeCounterUnit_ATY.s1_f = 1;
  292. timeCounterUnit_ATY.s1 = 0;
  293. }
  294. /* ATY: 2s unit ***********************************************************/
  295. if(timeCounterUnit_ATY.s2 >= 2000)
  296. {
  297. timeCounterUnit_ATY.s2_f = 1;
  298. timeCounterUnit_ATY.s2 = 0;
  299. }
  300. /* ATY: 10s unit **********************************************************/
  301. if(timeCounterUnit_ATY.s10 >= 10000)
  302. {
  303. timeCounterUnit_ATY.s10_f = 1;
  304. timeCounterUnit_ATY.s10 = 0;
  305. }
  306. }
  307. #endif /* __TIMER_CYCLE_ATY */
  308. #endif /* __HW_TIMER_ATY_C */
  309. /******************************** End Of File *********************************/