HW_TIMER_ATY.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 - 2025 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. * - 1_02_240410 > ATY
  25. * -# add multy addr and channel
  26. * -# add lock
  27. * - Undone
  28. ********************************************************************************
  29. */
  30. #ifndef __HW_TIMER_ATY_C
  31. #define __HW_TIMER_ATY_C
  32. #include "HW_TIMER_ATY.h"
  33. /******************************* For user *************************************/
  34. /******************************************************************************/
  35. /**
  36. * @brief define at user file and put at main while
  37. * @note long time process is not recommended
  38. */
  39. void UserTimerLoopProcess(struct HW_TIMER_ATY_Dev* dev)
  40. {
  41. if(dev->timerInitFlag == 0){
  42. dev->timerInitFlag = 1;
  43. dev->timerInit();
  44. }
  45. dev->timerLoopProcess_User();
  46. }
  47. /**
  48. * @brief 1ms timer loop cycle
  49. * @note put at 1ms timer IT callback function
  50. */
  51. void UserTimerLoop_Cycle1ms(struct HW_TIMER_ATY_Dev* dev)
  52. {
  53. dev->msN++;
  54. if(dev->ms10_f == 0)
  55. dev->ms10++;
  56. if(dev->ms100_f == 0)
  57. dev->ms100++;
  58. if(dev->s1_f == 0)
  59. dev->s1++;
  60. if(dev->s2_f == 0)
  61. dev->s2++;
  62. if(dev->s10_f == 0)
  63. dev->s10++;
  64. /* ATY: 1ms unit **********************************************************/
  65. dev->ms1_f = 1;
  66. /* ATY: 10ms unit *********************************************************/
  67. if(dev->ms10 >= 10){
  68. dev->ms10_f = 1;
  69. dev->ms10 = 0;
  70. }
  71. /* ATY: 100ms unit ********************************************************/
  72. if(dev->ms100 >= 100){
  73. dev->ms100_f = 1;
  74. dev->ms100 = 0;
  75. }
  76. /* ATY: 1s unit ***********************************************************/
  77. if(dev->s1 >= 1000){
  78. dev->s1_f = 1;
  79. dev->s1 = 0;
  80. }
  81. /* ATY: 2s unit ***********************************************************/
  82. if(dev->s2 >= 2000){
  83. dev->s2_f = 1;
  84. dev->s2 = 0;
  85. }
  86. /* ATY: 10s unit **********************************************************/
  87. if(dev->s10 >= 10000){
  88. dev->s10_f = 1;
  89. dev->s10 = 0;
  90. }
  91. }
  92. /******************************* For user *************************************/
  93. #if defined (__STC51_ATY)
  94. /**
  95. * @brief 1 us delay with for @ 24MHz about 1.04us
  96. */
  97. __WEAK_ATY void Delay1Us(void)
  98. {
  99. // uint8_t i = 2; // for 12MHz
  100. uint8_t i = 6; // for 24MHz
  101. while(--i);
  102. }
  103. /**
  104. * @brief us delay
  105. * @param us us
  106. */
  107. __WEAK_ATY void DelayUs(uint32_t us)
  108. {
  109. // uint32_t i = 2 + 4 * us; // for 12MHz
  110. uint32_t i = 6 + 8 * us; // for 12MHz
  111. // while(--i);
  112. }
  113. /**
  114. * @brief ms delay
  115. * @param ms ms
  116. */
  117. __WEAK_ATY void DelayMs(uint32_t ms)
  118. {
  119. while(ms--)
  120. DelayUs(1000);
  121. }
  122. #if TIMERC_N == 0
  123. /**
  124. * @brief cycle timer init
  125. * @note put at main init
  126. */
  127. void TimerLoopInit(void) //1ms@24.000MHz
  128. {
  129. AUXR |= 0x80; // timer clock 1T mode
  130. TMOD &= 0xF0; // set timer mode
  131. TL0 = 0x40; // set timer origin value
  132. TH0 = 0xA2; // set timer origin value
  133. TF0 = 0; // clear TF0 flag
  134. TR0 = 1; // start timer 0
  135. ET0 = 1; // enable timer IT
  136. EA = 1; // enable all IT
  137. }
  138. /**
  139. * @brief cycle timer IT callback function
  140. */
  141. void TM0_Isr(void) INTERRUPT(1)
  142. {
  143. UserTimerLoop_Cycle1ms();
  144. }
  145. #elif TIMERC_N == 1
  146. /**
  147. * @brief cycle timer init
  148. * @note put at main init
  149. */
  150. void TimerLoopInit(void) //1ms@24.000MHz
  151. {
  152. AUXR |= 0x40; // timer clock 1T mode
  153. TMOD &= 0x0F; // set timer mode
  154. TL1 = 0x40; // set timer origin value
  155. TH1 = 0xA2; // set timer origin value
  156. TF1 = 0; // clear TF1 flag
  157. TR1 = 1; // start timer 1
  158. ET1 = 1; // enable timer IT
  159. EA = 1; // enable all IT
  160. }
  161. /**
  162. * @brief cycle timer IT callback function
  163. */
  164. void TM1_Isr(void) INTERRUPT(3)
  165. {
  166. UserTimerLoop_Cycle1ms();
  167. }
  168. #elif TIMERC_N == 2
  169. void TM2_Isr(void) INTERRUPT(12)
  170. {
  171. UserTimerLoop_Cycle1ms();
  172. }
  173. #elif TIMERC_N == 3
  174. void TM3_Isr(void) INTERRUPT(19)
  175. {
  176. UserTimerLoop_Cycle1ms();
  177. }
  178. #elif TIMERC_N == 4
  179. void TM4_Isr(void) INTERRUPT(20)
  180. {
  181. UserTimerLoop_Cycle1ms();
  182. }
  183. #endif /* TIMERC_N */
  184. #elif defined(__STM32_HAL_ATY1)
  185. #include "tim.h"
  186. /**
  187. * @brief us delay with soft cycle
  188. * @param us us
  189. * @note @ 8MHz(PCLK1 = 36MHz && Others = 72MHz), about 0.9722us
  190. */
  191. __WEAK_ATY void Delay1Us(void)
  192. {
  193. uint8_t i = 12;
  194. while(--i);
  195. }
  196. /**
  197. * @brief us delay
  198. * @param us us
  199. */
  200. __WEAK_ATY void DelayUs(uint32_t us)
  201. {
  202. uint32_t i = 18 * us;
  203. while(--i);
  204. }
  205. /**
  206. * @brief ms delay
  207. * @param ms ms
  208. * @note long time with cycle is not recommended
  209. */
  210. __WEAK_ATY void DelayMs(uint32_t ms)
  211. {
  212. while(ms--)
  213. DelayUs(1000);
  214. // HAL_Delay(ms); // HAL_Delay not good for little time
  215. }
  216. #endif /* PLATFORM */
  217. /******************************************************************************/
  218. #if 0
  219. uint8_t it = 0;
  220. void DelayTest(void)
  221. {
  222. it = 0;
  223. it++;
  224. Delay1Us();
  225. it++;
  226. Delay1Us();
  227. it++;
  228. DelayUs(2);
  229. it++;
  230. DelayUs(2);
  231. it++;
  232. DelayUs(10);
  233. it++;
  234. DelayUs(10);
  235. it++;
  236. DelayUs(100);
  237. it++;
  238. DelayUs(100);
  239. it++;
  240. DelayMs(1);
  241. it++;
  242. DelayMs(1);
  243. it++;
  244. DelayMs(2);
  245. it++;
  246. DelayMs(2);
  247. it++;
  248. DelayMs(10);
  249. it++;
  250. DelayMs(10);
  251. it++;
  252. DelayMs(100);
  253. it++;
  254. DelayMs(100);
  255. #ifdef __STM32_HAL_ATY
  256. it++;
  257. HAL_Delay(100);
  258. it++;
  259. HAL_Delay(100);
  260. #endif /* __STM32_HAL_ATY */
  261. it++;
  262. DelaySec(1);
  263. it++;
  264. DelaySec(1);
  265. it++;
  266. it = 0;
  267. }
  268. #endif /* 0 */
  269. #endif /* __HW_TIMER_ATY_C */
  270. /************************************ etc *************************************/
  271. /* init */
  272. // // timer loop
  273. // void TimerLoopProcess_User_4(void);
  274. // void TimerInit_4()
  275. // {
  276. // HAL_TIM_Base_Start_IT(&htim4);
  277. // }
  278. // struct HW_TIMER_ATY_Dev HW_TIMER_ATY_Dev_4 = {
  279. // .msN = 0,
  280. // .ms10 = 0,
  281. // .ms100 = 0,
  282. // .s1 = 0,
  283. // .ms1_f = 0,
  284. // .ms10_f = 0,
  285. // .ms100_f = 0,
  286. // .s1_f = 0,
  287. // .timerInitFlag = 0,
  288. // .timerInit = TimerInit_4,
  289. // .timerLoopProcess_User = TimerLoopProcess_User_4,
  290. // .lock = _ATY_UNLOCKED,
  291. // .debugEnable = 0,
  292. // .LOG = printf
  293. // };
  294. // void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
  295. // {
  296. // if(htim == &htim4)
  297. // {
  298. // UserTimerLoop_Cycle1ms(&HW_TIMER_ATY_Dev_4);
  299. // }
  300. // }
  301. /* use */
  302. // void TimerLoopProcess_User_4(void)
  303. // {
  304. // if(HW_TIMER_ATY_Dev_4.ms1_f == 1)
  305. // {
  306. // HW_TIMER_ATY_Dev_4.ms1_f = 0;
  307. // }
  308. // if(HW_TIMER_ATY_Dev_4.ms100_f == 1)
  309. // {
  310. // HW_TIMER_ATY_Dev_4.ms100_f = 0;
  311. // SysLedBlink(&LED_ATY_t_1);
  312. // UpdateMbRegsFromFloat(mbG, &MODBUS_S_LOW_ATY_Dev_1);
  313. // }
  314. // if(HW_TIMER_ATY_Dev_4.msN >= 10000){
  315. // HW_TIMER_ATY_Dev_4.msN = 0;
  316. // }
  317. // if(HW_TIMER_ATY_Dev_4.s1_f == 1)
  318. // {
  319. // HW_TIMER_ATY_Dev_4.s1_f = 0;
  320. // }
  321. // }
  322. /******************************************************************************/
  323. /******************************** End Of File *********************************/