Added software timers

Accessory state machine framework functional
This commit is contained in:
2025-06-18 17:53:00 -05:00
parent aaa7f0dc29
commit 658cedfa3b
18 changed files with 614 additions and 580 deletions

View File

@@ -50,6 +50,7 @@
#include "System/system.h"
#include "io.h"
#include "soft_timer.h"
extern volatile uint8_t BC_Duty_Cycle;
@@ -58,7 +59,7 @@ extern uint8_t Power_Level,Safe_Mode;
extern uint8_t Port_State[];
extern uint8_t old_freq, frequency, frq_chg_tmr,Cur_Mode;
extern uint8_t Bat_Type,Bcast_Pwr_Level;
extern uint16_t Port_timer, Taps_adjust_timer,Sys_Chk_tmr,Shut_down_tmr;
extern uint16_t Port_timer, Taps_adjust_timer,Shut_down_tmr;
extern uint8_t Port_changed_flag;
extern uint16_t Low_Bat_timer,Estop_timer;
extern uint32_t what_val1, what_val2;
@@ -78,7 +79,7 @@ ClampData_t clampData;
void setSafeMode(bool safe)
{
sys.safeMode = safe;
Select_Estop((safe ? ON : OFF));
Select_Estop((safe ? ISOLATED : CONNECTED));
if (safe)
{
@@ -97,52 +98,50 @@ bool isUsbConnected(void)
return GPIO_READ(PIN_VBUS);
}
int main(void)
static char tmpString[32];
static bool _firstInit = true;
static void init(void)
{
bool prevUsbConnected = false;
sys.usbConnected = false;
if (_firstInit)
{
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
// indicate no slope calulated since slope will always be positive
clampData.slope = -1.0f;
/* attach 12 MHz clock to SPI3 */
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM3);
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
/* reset FLEXCOMM for SPI */
RESET_PeripheralReset(kFC3_RST_SHIFT_RSTn);
/* attach 12 MHz clock to SPI3 */
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM3);
/* reset FLEXCOMM for SPI */
RESET_PeripheralReset(kFC3_RST_SHIFT_RSTn);
BOARD_InitPins();
BOARD_BootClockPLL150M();
CLOCK_SetupFROClocking(96000000U); // Set up high frequency FRO output for USB
POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY); /*Turn on USB Phy */
BOARD_InitPins();
BOARD_BootClockPLL150M();
CLOCK_SetupFROClocking(96000000U); // Set up high frequency FRO output for USB
POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY); /*Turn on USB Phy */
_firstInit = false;
}
Power_ON_OFF(ON); // Enable_Psu(); Ensure Power supply stays switched on.
Select_Estop(ON); // Ensure output is ISOLATED from connections
Select_Estop(ISOLATED); // Ensure output is ISOLATED from connections
Init_vars();
Init_peripherals();
timer_init();
SPI_Init();
LCD_Init();
HWF_Init();
Init_sys();
FREQ_Init();
Init_PSU_Pot(); // initialize pot.
//###################
//Check_For_Usb(); // Set everything safe if so
KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup
Init_Ports(); // Ensure Ports are set to safe mode
BL_ReadInfo();
EE_LoadData(); //Read saved data
//###################
Display_Splash(); //Display splash screen
Delay_Ticks(100); // execute short delay
@@ -151,27 +150,16 @@ int main(void)
KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup
// Read_Tx_Ports(); // Read output ports and determine what is connected
// Select_Amplifier(); // Select correct amplifier according to frequency
//Init_Mode(); //
Init_Mode(); //
//init_PWM();
// PWM_Setup(32768, BC_Duty_Cycle);//freqArray[frequency].frequency1
// PWM_Setup(29430, BC_Duty_Cycle);
PWM_Setup(15890, 0); // switches off PWM
Delay_Ticks(100); // execute short delay
Safety_Check(); // Check all voltages are safe to continue in DC.
//Safety_Check(); // Check all voltages are safe to continue in DC.
Check_Bat_Id(); // Check for Alkaline or Lithium and battery insertion error.
what_val1=0;
what_val2=0;
Cur_Mode = PORT2_A;
init_flag = 0;
init_flag = 0;
if ((sys.adc.V_ID2 > 3.0) && (sys.adc.V_ID1 > 3.0))
@@ -181,25 +169,24 @@ int main(void)
init_flag = 1;
}
Disconnect(ACCY_PORT_2);
//Disconnect(ACCY_PORT_2);
Delay_Ticks(30);
Check_For_Clamp_On_Pwr_Up();
//Check_For_Clamp_On_Pwr_Up();
Select_Output_Port();
//Select_Output_Port();
Safety_Check(); // Second time J.I.C Check all voltages are safe to continue in DC.
//Safety_Check(); // Second time J.I.C Check all voltages are safe to continue in DC.
Normal_Bypass_Chk();
//Normal_Bypass_Chk();
old_freq = DUMMY_FRQ; //force a frequency on initialization
frq_chg_tmr = 0;
Update_Frequency();
// Select_Bypass(OFF); // 3/14/24
//Update_Frequency();
// HACK
Select_Estop(OFF); // Restore output.
Select_Estop(ISOLATED); // Restore output.
Init_Amplitude();
@@ -207,8 +194,22 @@ int main(void)
Menu_init();
// Broadcast accessory is always connected
ACCY_Connect(&sys.ports[ACCY_PORT_INDUCTION], ID_BROADCAST);
ACCY_setActive(&sys.ports[ACCY_PORT_INDUCTION], CHANNEL_A);
sys.guiMode = GUI_MODE_NORMAL;
}
int main(void)
{
bool prevUsbConnected = false;
sys.usbConnected = false;
// indicate no slope calulated since slope will always be positive
clampData.slope = -1.0f;
init();
while(1)
{
@@ -236,7 +237,10 @@ int main(void)
if (prevUsbConnected)
{
//setSafeMode(false);
Normal_Init(); // USB is unplugged so reinitialize
//Normal_Init(); // USB is unplugged so reinitialize
//init();
// check that things are safe before exiting safe mode
setSafeMode(false);
prevUsbConnected = false;
}
@@ -246,12 +250,45 @@ int main(void)
KEY_Update();
Tx_TimeOut(); // Check main transmitter timer
//Tx_TimeOut(); // Check main transmitter timer
// TODO: replace with system time
if (stimer_fired(sys.hSysCheckTimer))
{
#if 0
System_Check(); // Check all system functions
Chk_Gain();
if(catch_up_flag)
{
Delay_Ticks(10);
Request_Current_Change(); // Request_Current_Change();
catch_up_flag = false;
}
#endif
stimer_clearFired(sys.hSysCheckTimer);
}
// one second tasks
if (stimer_fired(sys.hOneSecondTimer))
{
if (sys.usbConnected)
{
// send diagnostics
//sprintf(tmpString, "Hello!");
//USB_SendString(tmpString);
}
stimer_clearFired(sys.hOneSecondTimer);
}
// 10Hz
if(tickCount++ % 10 == 0)
{
// check for accessories
ACCY_service();
uint32_t pressed = 0;
if (sys.guiMode == GUI_MODE_MENU)
@@ -261,7 +298,7 @@ int main(void)
else
{
pressed = KEY_GetPressed();
#if 0
if (sys.safeMode)
{
// only allow power off while in safe mode
@@ -270,6 +307,7 @@ int main(void)
pressed = MENU_ID_NONE;
}
}
#endif
switch(pressed)
{
@@ -287,13 +325,17 @@ int main(void)
case FRQ_KEY:
{
freq_key_process();
//freq_key_process();
break;
}
case MODE_KEY:
{
mode_menu();
//mode_menu();
// test accessory state machine
ACCY_next();
break;
}
#if 0
@@ -329,7 +371,7 @@ int main(void)
Display_Update();
}
#if 0
switch (Task)
{
@@ -351,9 +393,10 @@ int main(void)
break;
}
#endif
#if !DDS_PLL_TEST
#if 0
if(old_freq != frequency && frq_chg_tmr == 0 && (!frq_update_flag))
{
Update_Frequency();
@@ -373,19 +416,9 @@ int main(void)
Init_Pwr_Level_One(); // Set power out level 1
}
if (Sys_Chk_tmr == 0)
{
System_Check(); // Check all system functions
Chk_Gain();
if(catch_up_flag)
{
Delay_Ticks(10);
Request_Current_Change(); // Request_Current_Change();
catch_up_flag = false;
}
}
if((!Check_For_Clamp_New()))
{
@@ -410,8 +443,8 @@ int main(void)
if (Taps_adjust_timer == 0)
Check_Taps(); // Check for optimum Taps
#endif
#endif
}