/**
* @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 -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - 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 *********************************/