| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- /**
- * @file HW_TIMER_ATY.c
- *
- * @param Project DEVICE_GENERAL_ATY_LIB
- *
- * @author ATY
- *
- * @copyright
- * - Copyright 2017 - 2023 MZ-ATY
- * - This code follows:
- * - MZ-ATY Various Contents Joint Statement -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - Your use will be deemed to have accepted the terms of this statement.
- *
- * @brief Familiar functions of timer definition for all embedded device
- *
- * @version
- * - 1_01_220602 > ATY
- * -# Preliminary version, first Release
- * - 1_02_240410 > ATY
- * -# add multy addr and channel
- * -# add lock
- * - Undone
- ********************************************************************************
- */
- #ifndef __HW_TIMER_ATY_C
- #define __HW_TIMER_ATY_C
- #include "HW_TIMER_ATY.h"
- /******************************* For user *************************************/
- /******************************************************************************/
- /**
- * @brief define at user file and put at main while
- * @note long time process is not recommended
- */
- void UserTimerLoopProcess(struct HW_TIMER_ATY_Dev* dev)
- {
- if(dev->timerInitFlag == 0){
- dev->timerInitFlag = 1;
- dev->timerInit();
- }
- dev->timerLoopProcess_User();
- }
- /**
- * @brief 1ms timer loop cycle
- * @note put at 1ms timer IT callback function
- */
- void UserTimerLoop_Cycle1ms(struct HW_TIMER_ATY_Dev* dev)
- {
- dev->msN++;
- if(dev->ms10_f == 0)
- dev->ms10++;
- if(dev->ms100_f == 0)
- dev->ms100++;
- if(dev->s1_f == 0)
- dev->s1++;
- if(dev->s2_f == 0)
- dev->s2++;
- if(dev->s10_f == 0)
- dev->s10++;
- /* ATY: 1ms unit **********************************************************/
- dev->ms1_f = 1;
- /* ATY: 10ms unit *********************************************************/
- if(dev->ms10 >= 10){
- dev->ms10_f = 1;
- dev->ms10 = 0;
- }
- /* ATY: 100ms unit ********************************************************/
- if(dev->ms100 >= 100){
- dev->ms100_f = 1;
- dev->ms100 = 0;
- }
- /* ATY: 1s unit ***********************************************************/
- if(dev->s1 >= 1000){
- dev->s1_f = 1;
- dev->s1 = 0;
- }
- /* ATY: 2s unit ***********************************************************/
- if(dev->s2 >= 2000){
- dev->s2_f = 1;
- dev->s2 = 0;
- }
- /* ATY: 10s unit **********************************************************/
- if(dev->s10 >= 10000){
- dev->s10_f = 1;
- dev->s10 = 0;
- }
- }
- /******************************* For user *************************************/
- #if defined (__STC51_ATY)
- /**
- * @brief 1 us delay with for @ 24MHz about 1.04us
- */
- __WEAK_ATY void Delay1Us(void)
- {
- // uint8_t i = 2; // for 12MHz
- uint8_t i = 6; // for 24MHz
- while(--i);
- }
- /**
- * @brief us delay
- * @param us us
- */
- __WEAK_ATY void DelayUs(uint32_t us)
- {
- // uint32_t i = 2 + 4 * us; // for 12MHz
- uint32_t i = 6 + 8 * us; // for 12MHz
- // while(--i);
- }
- /**
- * @brief ms delay
- * @param ms ms
- */
- __WEAK_ATY void DelayMs(uint32_t ms)
- {
- while(ms--)
- DelayUs(1000);
- }
- #if TIMERC_N == 0
- /**
- * @brief cycle timer init
- * @note put at main init
- */
- void TimerLoopInit(void) //1ms@24.000MHz
- {
- AUXR |= 0x80; // timer clock 1T mode
- TMOD &= 0xF0; // set timer mode
- TL0 = 0x40; // set timer origin value
- TH0 = 0xA2; // set timer origin value
- TF0 = 0; // clear TF0 flag
- TR0 = 1; // start timer 0
- ET0 = 1; // enable timer IT
- EA = 1; // enable all IT
- }
- /**
- * @brief cycle timer IT callback function
- */
- void TM0_Isr(void) INTERRUPT(1)
- {
- UserTimerLoop_Cycle1ms();
- }
- #elif TIMERC_N == 1
- /**
- * @brief cycle timer init
- * @note put at main init
- */
- void TimerLoopInit(void) //1ms@24.000MHz
- {
- AUXR |= 0x40; // timer clock 1T mode
- TMOD &= 0x0F; // set timer mode
- TL1 = 0x40; // set timer origin value
- TH1 = 0xA2; // set timer origin value
- TF1 = 0; // clear TF1 flag
- TR1 = 1; // start timer 1
- ET1 = 1; // enable timer IT
- EA = 1; // enable all IT
- }
- /**
- * @brief cycle timer IT callback function
- */
- void TM1_Isr(void) INTERRUPT(3)
- {
- UserTimerLoop_Cycle1ms();
- }
- #elif TIMERC_N == 2
- void TM2_Isr(void) INTERRUPT(12)
- {
- UserTimerLoop_Cycle1ms();
- }
- #elif TIMERC_N == 3
- void TM3_Isr(void) INTERRUPT(19)
- {
- UserTimerLoop_Cycle1ms();
- }
- #elif TIMERC_N == 4
- void TM4_Isr(void) INTERRUPT(20)
- {
- UserTimerLoop_Cycle1ms();
- }
- #endif /* TIMERC_N */
- #elif defined(__STM32_HAL_ATY1)
- #include "tim.h"
- /**
- * @brief us delay with soft cycle
- * @param us us
- * @note @ 8MHz(PCLK1 = 36MHz && Others = 72MHz), about 0.9722us
- */
- __WEAK_ATY void Delay1Us(void)
- {
- uint8_t i = 12;
- while(--i);
- }
- /**
- * @brief us delay
- * @param us us
- */
- __WEAK_ATY void DelayUs(uint32_t us)
- {
- uint32_t i = 18 * us;
- while(--i);
- }
- /**
- * @brief ms delay
- * @param ms ms
- * @note long time with cycle is not recommended
- */
- __WEAK_ATY void DelayMs(uint32_t ms)
- {
- while(ms--)
- DelayUs(1000);
- // HAL_Delay(ms); // HAL_Delay not good for little time
- }
- #endif /* PLATFORM */
- /******************************************************************************/
- #if 0
- uint8_t it = 0;
- void DelayTest(void)
- {
- it = 0;
- it++;
- Delay1Us();
- it++;
- Delay1Us();
- it++;
- DelayUs(2);
- it++;
- DelayUs(2);
- it++;
- DelayUs(10);
- it++;
- DelayUs(10);
- it++;
- DelayUs(100);
- it++;
- DelayUs(100);
- it++;
- DelayMs(1);
- it++;
- DelayMs(1);
- it++;
- DelayMs(2);
- it++;
- DelayMs(2);
- it++;
- DelayMs(10);
- it++;
- DelayMs(10);
- it++;
- DelayMs(100);
- it++;
- DelayMs(100);
- #ifdef __STM32_HAL_ATY
- it++;
- HAL_Delay(100);
- it++;
- HAL_Delay(100);
- #endif /* __STM32_HAL_ATY */
- it++;
- DelaySec(1);
- it++;
- DelaySec(1);
- it++;
- it = 0;
- }
- #endif /* 0 */
- #endif /* __HW_TIMER_ATY_C */
- /************************************ etc *************************************/
- /* init */
- // // timer loop
- // void TimerLoopProcess_User_4(void);
- // void TimerInit_4()
- // {
- // HAL_TIM_Base_Start_IT(&htim4);
- // }
- // struct HW_TIMER_ATY_Dev HW_TIMER_ATY_Dev_4 = {
- // .msN = 0,
- // .ms10 = 0,
- // .ms100 = 0,
- // .s1 = 0,
- // .ms1_f = 0,
- // .ms10_f = 0,
- // .ms100_f = 0,
- // .s1_f = 0,
- // .timerInitFlag = 0,
- // .timerInit = TimerInit_4,
- // .timerLoopProcess_User = TimerLoopProcess_User_4,
- // .lock = _ATY_UNLOCKED,
- // .debugEnable = 0,
- // .LOG = printf
- // };
- // void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
- // {
- // if(htim == &htim4)
- // {
- // UserTimerLoop_Cycle1ms(&HW_TIMER_ATY_Dev_4);
- // }
- // }
- /* use */
- // void TimerLoopProcess_User_4(void)
- // {
- // if(HW_TIMER_ATY_Dev_4.ms1_f == 1)
- // {
- // HW_TIMER_ATY_Dev_4.ms1_f = 0;
- // }
- // if(HW_TIMER_ATY_Dev_4.ms100_f == 1)
- // {
- // HW_TIMER_ATY_Dev_4.ms100_f = 0;
- // SysLedBlink(&LED_ATY_t_1);
- // UpdateMbRegsFromFloat(mbG, &MODBUS_S_LOW_ATY_Dev_1);
- // }
- // if(HW_TIMER_ATY_Dev_4.msN >= 10000){
- // HW_TIMER_ATY_Dev_4.msN = 0;
- // }
- // if(HW_TIMER_ATY_Dev_4.s1_f == 1)
- // {
- // HW_TIMER_ATY_Dev_4.s1_f = 0;
- // }
- // }
- /******************************************************************************/
- /******************************** End Of File *********************************/
|