245 lines
5.2 KiB
C
245 lines
5.2 KiB
C
/*
|
|
* timer.c
|
|
*
|
|
* Created on: Jun 29, 2022
|
|
* Author: Keith.Lloyd
|
|
*/
|
|
|
|
/*******************************************************************************
|
|
* Includes
|
|
******************************************************************************/
|
|
|
|
|
|
#include "pin_mux.h"
|
|
#include "board.h"
|
|
#include "fsl_ctimer.h"
|
|
#include "timer.h"
|
|
#include "keys.h"
|
|
#include <stdbool.h>
|
|
#include "main.h"
|
|
#include "ports.h"
|
|
#include "adc.h"
|
|
#include "utils.h"
|
|
#include "usbComms.h"
|
|
#include "System/system.h"
|
|
|
|
/*******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
#define CTIMER CTIMER0 /* Timer 0 */
|
|
#define CTIMER_MAT0_OUT kCTIMER_Match_0 /* Match output 3 */
|
|
|
|
#define CTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
void ctimer_match0_callback(uint32_t flags);
|
|
|
|
|
|
/* Array of function pointers for callback for each channel */
|
|
ctimer_callback_t ctimer_callback_table[] = {ctimer_match0_callback};
|
|
|
|
/*******************************************************************************
|
|
* Variables
|
|
******************************************************************************/
|
|
|
|
TIMER_t tmr;
|
|
|
|
uint8_t * autoShutdownStrings[] = {"1 HOUR", "2 HOURS", "NEVER"}; //MUST match AUTO_SHUTDOWN_t
|
|
|
|
/* Match Configuration for Channel 0 */
|
|
static ctimer_match_config_t matchConfig0;
|
|
ctimer_config_t config;
|
|
|
|
//static volatile uint32_t Sys_Timer,Tx_timer;
|
|
uint32_t Sys_Timer,Tx_timer;
|
|
|
|
extern uint8_t Task, Tx_Time_Out_Flag, frq_chg_tmr;
|
|
extern uint16_t Port_timer, Taps_adjust_timer,Sys_Chk_tmr,Display_warning_tmr;
|
|
uint16_t Low_Bat_timer,Estop_timer,Shut_down_tmr,Vchktmr, Over_Voltage_tmr,PSU_Check_tmr;
|
|
uint16_t Key_Lock_Out_tmr,ByPass_tmr;
|
|
uint32_t TX_TIME[4] = {TIME_1HR,TIME_2HR,TIME_0HR,TIME_0HR};
|
|
|
|
extern SYSTEM_DATA_t sys;
|
|
uint32_t systemTime = 0; // 10mS ticks
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
|
|
|
|
#define TIMER_SCALE 10
|
|
void ctimer_match0_callback(uint32_t flags) // ISR every 10mS
|
|
{
|
|
int timerScaler = 0;
|
|
static bool Test_Flg = false;
|
|
if(Test_Flg)
|
|
GPIO_PinWrite(GPIO, 0, 4, 1); //TODO remove
|
|
else
|
|
GPIO_PinWrite(GPIO, 0, 4, 0); //TODO remove
|
|
|
|
Test_Flg ^=1;
|
|
|
|
systemTime++; // increment system time by 10mS
|
|
|
|
Check_Live_Voltage();
|
|
|
|
if ((systemTime % TIMER_SCALE != 0))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Sys_Timer > 0)
|
|
Sys_Timer--;
|
|
|
|
if (frq_chg_tmr > 0)
|
|
frq_chg_tmr--;
|
|
|
|
if (Low_Bat_timer > 0)
|
|
Low_Bat_timer--;
|
|
|
|
if(Shut_down_tmr > 0)
|
|
Shut_down_tmr--;
|
|
if(Vchktmr > 0)
|
|
Vchktmr--;
|
|
|
|
if(Over_Voltage_tmr > 0)
|
|
Over_Voltage_tmr--;
|
|
|
|
if(PSU_Check_tmr > 0)
|
|
PSU_Check_tmr--;
|
|
|
|
if(Key_Lock_Out_tmr > 0)
|
|
Key_Lock_Out_tmr--;
|
|
|
|
|
|
if( Port_timer > 0)
|
|
Port_timer--; // Port checking timer
|
|
if( Taps_adjust_timer > 0)
|
|
Taps_adjust_timer--; // Port checking timer
|
|
if(Estop_timer > 0)
|
|
Estop_timer--;
|
|
if(Sys_Chk_tmr > 0)
|
|
Sys_Chk_tmr--;
|
|
|
|
if(ByPass_tmr > 0)
|
|
ByPass_tmr--;
|
|
|
|
if(Display_warning_tmr > 0)
|
|
Display_warning_tmr--;
|
|
|
|
|
|
if(sys.adc.measure)
|
|
{
|
|
ADC_Update();
|
|
}
|
|
// GPIO_PinWrite(GPIO, 0, 4, 0); // TODO Whats' this for ?? Probably left over debug of timer
|
|
}
|
|
|
|
/*!
|
|
* @brief Main function
|
|
*/
|
|
void timer_init(void)
|
|
{
|
|
uint8_t i;
|
|
|
|
/* Enable the asynchronous bridge */
|
|
SYSCON->ASYNCAPBCTRL = 1;
|
|
|
|
/* Use 12 MHz clock for some of the Ctimers */
|
|
CLOCK_AttachClk(kFRO12M_to_ASYNC_APB);
|
|
|
|
|
|
CTIMER_GetDefaultConfig(&config);
|
|
|
|
CTIMER_Init(CTIMER, &config);
|
|
|
|
/* Configuration 0 */
|
|
matchConfig0.enableCounterReset = true;
|
|
matchConfig0.enableCounterStop = false;
|
|
matchConfig0.matchValue = 150000;//150MHz clock 10mS period
|
|
matchConfig0.enableInterrupt = true;
|
|
|
|
CTIMER_RegisterCallBack(CTIMER, &ctimer_callback_table[0], kCTIMER_MultipleCallback);
|
|
CTIMER_SetupMatch(CTIMER, CTIMER_MAT0_OUT, &matchConfig0);
|
|
|
|
CTIMER_StartTimer(CTIMER);
|
|
tmr.timerInitialized = true;
|
|
|
|
i = tmr.autoShutdown;
|
|
Tx_timer = TX_TIME[i]; // reload the timer
|
|
}
|
|
|
|
|
|
void Delay_Ticks(uint32_t Delay_Interval)
|
|
{
|
|
Sys_Timer = Delay_Interval; // load timer with multiples of 10mS
|
|
|
|
while (Sys_Timer > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tx_TimeOut(void)
|
|
{
|
|
if (tmr.autoShutdown != SD_NEVER)
|
|
{
|
|
if (Tx_timer > 0)
|
|
{
|
|
Tx_timer--;
|
|
if (Tx_timer == 0)
|
|
Task = PWR_OFF_TASK; // schedule a PWR Down
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Cycle through auto shutdown options
|
|
void tmr_ChangeAutoSDTimer(void)
|
|
{
|
|
unsigned int i;
|
|
|
|
i = tmr.autoShutdown;
|
|
|
|
if(++tmr.autoShutdown >= SD_NUM)
|
|
{
|
|
tmr.autoShutdown = (AUTO_SHUTDOWN_t)0;
|
|
}
|
|
|
|
Tx_timer = TX_TIME[tmr.autoShutdown]; // reload the timer
|
|
}
|
|
|
|
|
|
|
|
uint8_t * tmr_GetAutoSDTimerString(void)
|
|
{
|
|
return autoShutdownStrings[tmr.autoShutdown];
|
|
}
|
|
|
|
/* Start CTIMER0
|
|
* Does nothing if timer not initialized
|
|
*/
|
|
void TMR_Start(void)
|
|
{
|
|
if(tmr.timerInitialized)
|
|
{
|
|
CTIMER_StartTimer(CTIMER);
|
|
}
|
|
}
|
|
|
|
/* Stop CTIMER0
|
|
* Does nothing if timer not initialized
|
|
*/
|
|
void TMR_Stop(void)
|
|
{
|
|
if(tmr.timerInitialized)
|
|
{
|
|
CTIMER_StopTimer(CTIMER);
|
|
}
|
|
}
|