/* * 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 #include #include "Fonts\fontLibrary.h" #include "Graphics\graphicsLibrary.h" #include "spi.h" #include "lcd.h" #include "main.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" #include "System/system.h" #include "io.h" #include "soft_timer.h" #include "driver.h" extern volatile uint8_t BC_Duty_Cycle; uint8_t Task,Tx_Time_Out_Flag,i,Test_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; 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; 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 extern SYSTEM_DATA_t sys; #define DDS_PLL_TEST 0 ClampData_t clampData; static char tmpString[32]; static bool _firstInit = true; static void init(void) { if (_firstInit) { /* 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 */ system_init(); _firstInit = false; } driver_enablePower(true); driver_isolateOutput(true); timer_init(); SPI_Init(); LCD_Init(); HWF_Init(); driver_init(); ADC_SysInit(); ACCY_Init(); io_expanderClearAll(false); io_backlightOn(true, NO_UPDATE); io_connectAmpAB(true, NO_UPDATE); io_update(); BL_ReadInfo(); EE_LoadData(); //Read saved data Display_Splash(); //Display splash screen delayms(1000); KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup driver_setDuty(0); // switches off PWM Delay_Ticks(100); // execute short delay //Safety_Check(); // Check all voltages are safe to continue in DC. battery_checkId(); // Check for Alkaline or Lithium and battery insertion error. USB_Init(); 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) { static uint32_t tickCount = 0; sys.usbConnected = isUsbConnected(); sys.status[USB_CONNECTED] = isUsbConnected(); USB_Update(); //Update USB here so we're outside the ISR if (sys.usbConnected) { // USB connected if (!prevUsbConnected) { driver_setSafe(true); //setSafeMode(true); } } else { // USB disconnected if (prevUsbConnected) { //setSafeMode(false); //Normal_Init(); // USB is unplugged so reinitialize //init(); // check that things are safe before exiting safe mode //setSafeMode(false); driver_setSafe(false); prevUsbConnected = false; } } //delayms(10); //10mS delay if (stimer_fired(sys.h100HzTimer)) { KEY_Update(); stimer_clearFired(sys.h100HzTimer); } // 1Hz tasks if (stimer_fired(sys.h1HzTimer)) { // check for power off timeout if (sys.usbConnected) { // send diagnostics //sprintf(tmpString, "Hello!"); //USB_SendString(tmpString); } stimer_clearFired(sys.h1HzTimer); } // 10Hz Tasks if (stimer_fired(sys.h10HzTimer)) { stimer_clearFired(sys.h10HzTimer); // check for accessories ACCY_service(); driver_service(); uint32_t pressed = 0; if (sys.guiMode == GUI_MODE_MENU) { Menu_service(); } else { pressed = KEY_GetPressed(); #if 0 if (sys.safeMode) { // only allow power off while in safe mode if (pressed != KEY_POWER) { pressed = MENU_ID_NONE; } } #endif switch(pressed) { case KEY_POWER: { Power_Down(); break; } case MENU_KEY: { Menu_mainMenu(); break; } case FRQ_KEY: { //freq_key_process(); break; } case MODE_KEY: { ACCY_next(); //io_setOutputPort(ACCY_PORT_2, CHANNEL_B); break; } case PWR_UP_KEY: { driver_powerUp(); break; } case PWR_DN_KEY: { driver_powerDown(); break; } } Display_Update(); } } prevUsbConnected = sys.usbConnected; } // while (1) }