Lots of refactoring

This commit is contained in:
2025-06-11 10:55:00 -05:00
parent a437bcf8b5
commit aaa7f0dc29
35 changed files with 1165 additions and 809 deletions

View File

@@ -25,7 +25,6 @@
#include "mode.h"
#include "utils.h"
#include "timer.h"
#include "over_ride.h"
#include "menu.h"
#include "sys_chk.h"
#include "amps.h"
@@ -55,7 +54,7 @@
extern volatile uint8_t BC_Duty_Cycle;
uint8_t Task,Tx_Time_Out_Flag,i,Test_Mode;
extern uint8_t Power_Level,Over_Voltage_Flag,Safe_Mode;
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;
@@ -63,7 +62,6 @@ extern uint16_t Port_timer, Taps_adjust_timer,Sys_Chk_tmr,Shut_down_tmr;
extern uint8_t Port_changed_flag;
extern uint16_t Low_Bat_timer,Estop_timer;
extern uint32_t what_val1, what_val2;
extern ADC_t adc;
extern uint32_t new_freq;
uint8_t init_flag,catch_up_flag;
uint8_t Selected = false;
@@ -72,14 +70,37 @@ extern HARDWARE_FIX_t hwf;
extern uint8_t tempString[40]; // Todo move
extern SYSTEM_DATA_t sys;
#define BYPASS_USB_SAFETY 0 //Use for debugging
#define DDS_PLL_TEST 0
ClampData_t clampData;
void setSafeMode(bool safe)
{
sys.safeMode = safe;
Select_Estop((safe ? ON : OFF));
if (safe)
{
//clear shift registers to known state
Port_State[MID_SR] = USB_STATE_MID; //
Port_State[BOTTOM_SR] = USB_STATE_BOT; //
Port_State[TOP_SR] = USB_STATE_TOP;
SPI0_SendBytes(Port_State, 3, EXPANDER); // Update shift register
Delay_Ticks(1);
Send_Ctrl_Word(SLP_CTRL_WRD2, SIGNAL); // Switch signal off
}
}
bool isUsbConnected(void)
{
return GPIO_READ(PIN_VBUS);
}
int main(void)
{
bool prevUsbConnected = false;
sys.usbConnected = false;
// indicate no slope calulated since slope will always be positive
clampData.slope = -1.0f;
@@ -99,6 +120,8 @@ int main(void)
POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY); /*Turn on USB Phy */
Power_ON_OFF(ON); // Enable_Psu(); Ensure Power supply stays switched on.
Select_Estop(ON); // Ensure output is ISOLATED from connections
@@ -109,42 +132,21 @@ int main(void)
Init_sys();
//###################
Check_For_Usb(); // Set everything safe if so
//Check_For_Usb(); // Set everything safe if so
KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup
// Display_Splash(); //Display splash screen
// Delay_Ticks(100); // execute short delay
BL_ReadInfo();
EE_LoadData(); //Read saved data
#if !BYPASS_USB_SAFETY
if(GPIO_PinRead(GPIO,1,6))
{
USB_Init();
Display_Splash(); //Display splash screen
Delay_Ticks(100); // execute short delay
}
while(GPIO_PinRead(GPIO,1,6))
{
LCD_Clear();
Display_USB(); //Display USB icon
LCD_Update();
Delay_Ticks(1); // execute short delay
safe_key(); // process primary keys as safety menu
USB_Update();
}
#endif
//###################
Display_Splash(); //Display splash screen
Delay_Ticks(100); // execute short delay
ADC_Init2();
ADC_SysInit();
ACCY_Init();
KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup
@@ -170,21 +172,16 @@ int main(void)
Cur_Mode = PORT2_A;
init_flag = 0;
for( i=0; i < 15; i++)
{
Delay_Ticks(10);
// ADC_Update();
// Read_Tx_Ports(); // check for whats plugged in at the ports every 100mS.
}
if ((adc.V_ID2 > 3.0) && (adc.V_ID1 > 3.0))
{
if ((sys.adc.V_ID2 > 3.0) && (sys.adc.V_ID1 > 3.0))
{
Cur_Mode = BROADCAST;
Port_changed_flag= 1;
init_flag = 1;
}
}
Disconnect(2);
Disconnect(ACCY_PORT_2);
Delay_Ticks(30);
Check_For_Clamp_On_Pwr_Up();
@@ -201,6 +198,7 @@ int main(void)
Update_Frequency();
// Select_Bypass(OFF); // 3/14/24
// HACK
Select_Estop(OFF); // Restore output.
Init_Amplitude();
@@ -209,47 +207,40 @@ int main(void)
Menu_init();
sys.guiMode = GUI_MODE_NORMAL;
#if DDS_PLL_TEST
//# Insert special test code here.
uint32_t tmp;
All_Amps_Off();
// Set OBPTEN
tmp = Calc_Freq(3500090);
Load_Frq_Gen(SINGLE,tmp,0); // # update the frequency generators
//# End Test code
#endif
while(1)
{
static uint32_t tickCount = 0;
sys.usbConnected = isUsbConnected();
USB_Update(); //Update USB here so we're outside the ISR
#if !BYPASS_USB_SAFETY
//#####################
Check_For_Usb();
if(GPIO_READ(PIN_VBUS))
if (sys.usbConnected)
{
// USB connected
if (!prevUsbConnected)
{
Task = USB_SAFE_TASK; // process primary keys as safety menu
safe_key(); // process primary keys as safety menu
LCD_Clear();
Display_USB(); //Display USB icon
LCD_Update();
setSafeMode(true);
}
Display_USB();
}
else
if(!GPIO_READ(PIN_VBUS) && Ports_Cleared_Flag)
{
// USB disconnected
if (prevUsbConnected)
{
//setSafeMode(false);
Normal_Init(); // USB is unplugged so reinitialize
setSafeMode(false);
prevUsbConnected = false;
}
//###############################
#endif
}
Delay_Ticks(1); //10mS delay
@@ -258,13 +249,9 @@ uint32_t tmp;
Tx_TimeOut(); // Check main transmitter timer
#if !BYPASS_USB_SAFETY
if((tickCount++ % 10 == 0) && !GPIO_READ(PIN_VBUS)) //every 10 ticks = 100mS AND !USB
#else
if(tickCount++ % 10 == 0) //every 10 ticks = 100mS
#endif
// TODO: replace with system time
if(tickCount++ % 10 == 0)
{
uint32_t pressed = 0;
if (sys.guiMode == GUI_MODE_MENU)
@@ -275,16 +262,70 @@ uint32_t tmp;
{
pressed = KEY_GetPressed();
switch(pressed)
if (sys.safeMode)
{
case MENU_KEY:
// only allow power off while in safe mode
if (pressed != KEY_POWER)
{
Menu_mainMenu();
break;
pressed = MENU_ID_NONE;
}
}
switch(pressed)
{
case KEY_POWER:
{
Power_Down();
break;
}
case MENU_KEY:
{
Menu_mainMenu();
break;
}
case FRQ_KEY:
{
freq_key_process();
break;
}
case MODE_KEY:
{
mode_menu();
break;
}
#if 0
case PWR_UP_KEY:
{
if(Key_Lock_Out_tmr == 0)
{
Vchktmr = LOCK_OUT_DELAY;
inc_pwr();//increment the power output
Key_Lock_Out_tmr = KEY_LOCK_TIME;
Suspend_Step_Chk = false;
step_count = 0;
}
}
break;
case PWR_DN_KEY:
{
if(Key_Lock_Out_tmr == 0)
{
Vchktmr = LOCK_OUT_DELAY;
dec_pwr();// decrement the power output
Key_Lock_Out_tmr = KEY_LOCK_TIME;
Suspend_Step_Chk = false;
step_count = 0;
}
break;
}
#endif
}
Display_Update();
}
@@ -292,117 +333,94 @@ uint32_t tmp;
switch (Task)
{
case SAFETY_TASK:
safe_key(); // process primary keys as safety menu
break;
case USB_SAFE_TASK:
safe_key(); // process primary keys as safety menu
break;
case FATAL_ERROR_TASK:
safe_key(); // process primary keys as safety menu
case PRIMARY_TASK:
pro_key(pressed); // process primary keys front 6 first menu
break;
case PRIMARY_TASK:
pro_key(pressed); // process primary keys front 6 first menu
break;
case MENU_TASK: // Allows user to select options
menu_key();
Task = PRIMARY_TASK;
break;
case MODE_TASK: // Selects between DC || BC
mode_menu();
Task = PRIMARY_TASK;
break;
case PWR_OFF_TASK:
Power_Down(); // Stores last settings and powers off
break;
case OVER_RIDE_TASK: // Allows user to Over ride safe settings
Over_Ride(); // schedule Over Ride Menu
KEY_ClearKeyPresses();
break;
case BAT_INSERTION_ERROR:
if(Shut_down_tmr == 0); //Display_Bat_Error();
Power_Down(); // Stores last settings and powers off
break;
case BAT_INSERTION_ERROR:
if(Shut_down_tmr == 0); //Display_Bat_Error();
Power_Down(); // Stores last settings and powers off
break;
case LOW_BATTERY_TASK:
if(Low_Bat_timer == 0)
Task = PWR_OFF_TASK;
break;
case LOW_BATTERY_TASK:
if(Low_Bat_timer == 0)
Task = PWR_OFF_TASK;
break;
}
#if !DDS_PLL_TEST
if(old_freq != frequency && frq_chg_tmr == 0 && (!frq_update_flag))
if(old_freq != frequency && frq_chg_tmr == 0 && (!frq_update_flag))
{
Update_Frequency();
//Port_changed_flag = true;
}
if(Bat_Type == BAT_ERROR)
Task = BAT_INSERTION_ERROR;
if (Port_timer == 0)
Read_Tx_Ports(); // check for whats plugged in at the ports every 100mS.
if(Port_changed_flag)
{
Select_Output_Port();
Update_Frequency();
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)
{
Update_Frequency();
//Port_changed_flag = true;
Delay_Ticks(10);
Request_Current_Change(); // Request_Current_Change();
catch_up_flag = false;
}
if(Bat_Type == BAT_ERROR)
Task = BAT_INSERTION_ERROR;
if (Port_timer == 0)
Read_Tx_Ports(); // check for whats plugged in at the ports every 100mS.
if(Port_changed_flag)
{
Select_Output_Port();
Update_Frequency();
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()))
{
Check_For_Open_Circuit(); // DC mode only
}
else
{
Port_State[MID_SR] |= BYPASS_ON; // Set relay to By Pass protection cap
SPI0_SendBytes(Port_State, 3, EXPANDER); // Send_Update_Port();
}
if (Check_For_Clamp())
{
doClampPower();
}
//Send_Wireless_Data(); // Send system update to Receiver.
if (Taps_adjust_timer == 0)
Check_Taps(); // Check for optimum Taps
#endif
// Display_Update();
}
if((!Check_For_Clamp_New()))
{
Check_For_Open_Circuit(); // DC mode only
}
else
{
EXPANDER_SET(BYPASS_ON, true);
//Port_State[MID_SR] |= BYPASS_ON; // Set relay to By Pass protection cap
//SPI0_SendBytes(Port_State, 3, EXPANDER); // Send_Update_Port();
}
if (Check_For_Clamp())
{
doClampPower();
}
//Send_Wireless_Data(); // Send system update to Receiver.
if (Taps_adjust_timer == 0)
Check_Taps(); // Check for optimum Taps
#endif
}
}
prevUsbConnected = sys.usbConnected;
} // while (1)
}
}