2025-05-14 12:57:39 -05:00
|
|
|
/*
|
|
|
|
|
* main.c
|
|
|
|
|
*
|
|
|
|
|
* Created on: May 20, 2022
|
|
|
|
|
* Author: Keith.Lloyd
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "fsl_spi.h"
|
|
|
|
|
#include "pin_mux.h"
|
|
|
|
|
#include "board.h"
|
|
|
|
|
#include "fsl_debug_console.h"
|
|
|
|
|
#include "fsl_power.h"
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include "Fonts\fontLibrary.h"
|
|
|
|
|
#include "Graphics\graphicsLibrary.h"
|
|
|
|
|
#include "spi.h"
|
|
|
|
|
#include "lcd.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include "frq.h"
|
|
|
|
|
#include "pro_key.h"
|
|
|
|
|
#include "menu1.h"
|
|
|
|
|
#include "mode.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
#include "menu.h"
|
|
|
|
|
#include "sys_chk.h"
|
|
|
|
|
#include "amps.h"
|
|
|
|
|
#include "display.h"
|
|
|
|
|
#include "ports.h"
|
|
|
|
|
#include "adc.h"
|
|
|
|
|
#include "battery.h"
|
|
|
|
|
#include "mode.h"
|
|
|
|
|
#include "init.h"
|
|
|
|
|
#include "taps.h"
|
|
|
|
|
#include "pwm.h"
|
|
|
|
|
#include "safety_key.h"
|
|
|
|
|
#include "usbComms.h"
|
|
|
|
|
#include "flashUpdate.h"
|
|
|
|
|
#include "bootloader.h"
|
|
|
|
|
#include "eeprom.h"
|
|
|
|
|
#include "sys_chk.h"
|
|
|
|
|
#include "pwr_level.h"
|
|
|
|
|
#include "Graphics/icons.h"
|
|
|
|
|
#include "hwFixes.h"
|
|
|
|
|
//temp
|
|
|
|
|
#include "fsl_sctimer.h"
|
|
|
|
|
|
2025-05-16 07:56:18 -05:00
|
|
|
#include "System/system.h"
|
|
|
|
|
#include "io.h"
|
|
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
extern volatile uint8_t BC_Duty_Cycle;
|
|
|
|
|
|
|
|
|
|
uint8_t Task,Tx_Time_Out_Flag,i,Test_Mode;
|
2025-06-11 10:55:00 -05:00
|
|
|
extern uint8_t Power_Level,Safe_Mode;
|
2025-05-14 12:57:39 -05:00
|
|
|
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 uint8_t Port_changed_flag;
|
|
|
|
|
extern uint16_t Low_Bat_timer,Estop_timer;
|
|
|
|
|
extern uint32_t what_val1, what_val2;
|
|
|
|
|
extern uint32_t new_freq;
|
|
|
|
|
uint8_t init_flag,catch_up_flag;
|
|
|
|
|
uint8_t Selected = false;
|
|
|
|
|
extern uint8_t frq_update_flag,Ports_Cleared_Flag;
|
|
|
|
|
extern HARDWARE_FIX_t hwf;
|
|
|
|
|
extern uint8_t tempString[40]; // Todo move
|
|
|
|
|
|
2025-05-16 07:56:18 -05:00
|
|
|
extern SYSTEM_DATA_t sys;
|
2025-06-11 10:55:00 -05:00
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
#define DDS_PLL_TEST 0
|
|
|
|
|
|
|
|
|
|
ClampData_t clampData;
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
2025-06-11 10:55:00 -05:00
|
|
|
bool prevUsbConnected = false;
|
|
|
|
|
sys.usbConnected = false;
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
// indicate no slope calulated since slope will always be positive
|
|
|
|
|
clampData.slope = -1.0f;
|
|
|
|
|
|
|
|
|
|
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
|
|
|
|
|
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
Power_ON_OFF(ON); // Enable_Psu(); Ensure Power supply stays switched on.
|
|
|
|
|
Select_Estop(ON); // Ensure output is ISOLATED from connections
|
|
|
|
|
|
|
|
|
|
Init_vars();
|
|
|
|
|
|
|
|
|
|
Init_peripherals();
|
|
|
|
|
HWF_Init();
|
|
|
|
|
Init_sys();
|
|
|
|
|
|
|
|
|
|
//###################
|
2025-06-11 10:55:00 -05:00
|
|
|
//Check_For_Usb(); // Set everything safe if so
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup
|
|
|
|
|
|
|
|
|
|
BL_ReadInfo();
|
|
|
|
|
EE_LoadData(); //Read saved data
|
|
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
//###################
|
|
|
|
|
|
|
|
|
|
Display_Splash(); //Display splash screen
|
|
|
|
|
Delay_Ticks(100); // execute short delay
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
ADC_SysInit();
|
2025-05-14 12:57:39 -05:00
|
|
|
ACCY_Init();
|
|
|
|
|
|
|
|
|
|
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_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.
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
if ((sys.adc.V_ID2 > 3.0) && (sys.adc.V_ID1 > 3.0))
|
|
|
|
|
{
|
2025-05-14 12:57:39 -05:00
|
|
|
Cur_Mode = BROADCAST;
|
|
|
|
|
Port_changed_flag= 1;
|
|
|
|
|
init_flag = 1;
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
Disconnect(ACCY_PORT_2);
|
2025-05-14 12:57:39 -05:00
|
|
|
Delay_Ticks(30);
|
|
|
|
|
|
|
|
|
|
Check_For_Clamp_On_Pwr_Up();
|
|
|
|
|
|
|
|
|
|
Select_Output_Port();
|
|
|
|
|
|
|
|
|
|
Safety_Check(); // Second time J.I.C Check all voltages are safe to continue in DC.
|
|
|
|
|
|
|
|
|
|
Normal_Bypass_Chk();
|
|
|
|
|
|
|
|
|
|
old_freq = DUMMY_FRQ; //force a frequency on initialization
|
|
|
|
|
frq_chg_tmr = 0;
|
|
|
|
|
|
|
|
|
|
Update_Frequency();
|
|
|
|
|
// Select_Bypass(OFF); // 3/14/24
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
// HACK
|
2025-05-14 12:57:39 -05:00
|
|
|
Select_Estop(OFF); // Restore output.
|
|
|
|
|
|
|
|
|
|
Init_Amplitude();
|
|
|
|
|
|
|
|
|
|
USB_Init();
|
|
|
|
|
|
2025-05-16 07:56:18 -05:00
|
|
|
Menu_init();
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
|
2025-05-16 07:56:18 -05:00
|
|
|
sys.guiMode = GUI_MODE_NORMAL;
|
|
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
static uint32_t tickCount = 0;
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
sys.usbConnected = isUsbConnected();
|
|
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
USB_Update(); //Update USB here so we're outside the ISR
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
if (sys.usbConnected)
|
|
|
|
|
{
|
|
|
|
|
// USB connected
|
|
|
|
|
if (!prevUsbConnected)
|
2025-05-14 12:57:39 -05:00
|
|
|
{
|
2025-06-11 10:55:00 -05:00
|
|
|
setSafeMode(true);
|
2025-05-14 12:57:39 -05:00
|
|
|
}
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
Display_USB();
|
|
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
else
|
2025-06-11 10:55:00 -05:00
|
|
|
{
|
|
|
|
|
// USB disconnected
|
|
|
|
|
if (prevUsbConnected)
|
2025-05-14 12:57:39 -05:00
|
|
|
{
|
2025-06-11 10:55:00 -05:00
|
|
|
//setSafeMode(false);
|
2025-05-14 12:57:39 -05:00
|
|
|
Normal_Init(); // USB is unplugged so reinitialize
|
2025-06-11 10:55:00 -05:00
|
|
|
setSafeMode(false);
|
|
|
|
|
prevUsbConnected = false;
|
2025-05-14 12:57:39 -05:00
|
|
|
}
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
Delay_Ticks(1); //10mS delay
|
|
|
|
|
|
2025-05-16 07:56:18 -05:00
|
|
|
KEY_Update();
|
|
|
|
|
|
2025-05-29 13:12:29 -05:00
|
|
|
Tx_TimeOut(); // Check main transmitter timer
|
2025-05-16 07:56:18 -05:00
|
|
|
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
// TODO: replace with system time
|
|
|
|
|
if(tickCount++ % 10 == 0)
|
2025-05-14 12:57:39 -05:00
|
|
|
{
|
2025-05-16 17:11:59 -05:00
|
|
|
uint32_t pressed = 0;
|
|
|
|
|
|
|
|
|
|
if (sys.guiMode == GUI_MODE_MENU)
|
|
|
|
|
{
|
|
|
|
|
Menu_service();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pressed = KEY_GetPressed();
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if (sys.safeMode)
|
|
|
|
|
{
|
|
|
|
|
// only allow power off while in safe mode
|
|
|
|
|
if (pressed != KEY_POWER)
|
|
|
|
|
{
|
|
|
|
|
pressed = MENU_ID_NONE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 17:11:59 -05:00
|
|
|
switch(pressed)
|
|
|
|
|
{
|
2025-06-11 10:55:00 -05:00
|
|
|
case KEY_POWER:
|
|
|
|
|
{
|
|
|
|
|
Power_Down();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 17:11:59 -05:00
|
|
|
case MENU_KEY:
|
|
|
|
|
{
|
|
|
|
|
Menu_mainMenu();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
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
|
2025-05-16 17:11:59 -05:00
|
|
|
}
|
2025-06-11 10:55:00 -05:00
|
|
|
|
2025-05-16 17:11:59 -05:00
|
|
|
Display_Update();
|
|
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (Task)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
case PRIMARY_TASK:
|
|
|
|
|
pro_key(pressed); // process primary keys front 6 first menu
|
|
|
|
|
break;
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
case BAT_INSERTION_ERROR:
|
|
|
|
|
if(Shut_down_tmr == 0); //Display_Bat_Error();
|
|
|
|
|
Power_Down(); // Stores last settings and powers off
|
|
|
|
|
break;
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
case LOW_BATTERY_TASK:
|
|
|
|
|
if(Low_Bat_timer == 0)
|
|
|
|
|
Task = PWR_OFF_TASK;
|
|
|
|
|
break;
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
#if !DDS_PLL_TEST
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if(old_freq != frequency && frq_chg_tmr == 0 && (!frq_update_flag))
|
|
|
|
|
{
|
|
|
|
|
Update_Frequency();
|
|
|
|
|
//Port_changed_flag = true;
|
2025-05-14 12:57:39 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if(Bat_Type == BAT_ERROR)
|
|
|
|
|
Task = BAT_INSERTION_ERROR;
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if (Port_timer == 0)
|
|
|
|
|
Read_Tx_Ports(); // check for whats plugged in at the ports every 100mS.
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if(Port_changed_flag)
|
|
|
|
|
{
|
|
|
|
|
Select_Output_Port();
|
|
|
|
|
Update_Frequency();
|
|
|
|
|
Init_Pwr_Level_One(); // Set power out level 1
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
|
|
|
|
if (Sys_Chk_tmr == 0)
|
|
|
|
|
{
|
|
|
|
|
System_Check(); // Check all system functions
|
|
|
|
|
Chk_Gain();
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if(catch_up_flag)
|
2025-05-14 12:57:39 -05:00
|
|
|
{
|
2025-06-11 10:55:00 -05:00
|
|
|
Delay_Ticks(10);
|
|
|
|
|
Request_Current_Change(); // Request_Current_Change();
|
|
|
|
|
catch_up_flag = false;
|
2025-05-14 12:57:39 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if((!Check_For_Clamp_New()))
|
|
|
|
|
{
|
|
|
|
|
Check_For_Open_Circuit(); // DC mode only
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
EXPANDER_SET(BYPASS_ON, true);
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
//Port_State[MID_SR] |= BYPASS_ON; // Set relay to By Pass protection cap
|
|
|
|
|
//SPI0_SendBytes(Port_State, 3, EXPANDER); // Send_Update_Port();
|
|
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
if (Check_For_Clamp())
|
|
|
|
|
{
|
|
|
|
|
doClampPower();
|
2025-05-14 12:57:39 -05:00
|
|
|
}
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
//Send_Wireless_Data(); // Send system update to Receiver.
|
|
|
|
|
|
|
|
|
|
if (Taps_adjust_timer == 0)
|
|
|
|
|
Check_Taps(); // Check for optimum Taps
|
|
|
|
|
#endif
|
2025-05-14 12:57:39 -05:00
|
|
|
|
2025-05-29 13:12:29 -05:00
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prevUsbConnected = sys.usbConnected;
|
2025-05-29 13:12:29 -05:00
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
|
|
|
|
|
} // while (1)
|
2025-05-14 12:57:39 -05:00
|
|
|
|
|
|
|
|
|
2025-06-11 10:55:00 -05:00
|
|
|
}
|
2025-05-14 12:57:39 -05:00
|
|
|
|