diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index f068d2b..4827031 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -15,7 +15,7 @@ - + diff --git a/.vscode/settings.json b/.vscode/settings.json index bcd4aa6..098ea5a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,9 @@ "io.h": "c", "spi.h": "c", "system.h": "c", - "pwr_level.h": "c" + "pwr_level.h": "c", + "fsl_ctimer.h": "c", + "soft_timer.h": "c", + "eeprom.h": "c" } } \ No newline at end of file diff --git a/source/System/system.c b/source/System/system.c index 65270a7..f1dc8d8 100644 --- a/source/System/system.c +++ b/source/System/system.c @@ -63,3 +63,14 @@ uint32_t SYS_GetLanguage(void) return sys.language; } +SYSTEM_DATA_t* system_getSys(void) +{ + return &sys; +} + +// monitor for overvoltage, power, current, etc. +// call from the main timer at 100Hz +void system_monitor(void) +{ + +} diff --git a/source/System/system.h b/source/System/system.h index 7415489..8f5bb62 100644 --- a/source/System/system.h +++ b/source/System/system.h @@ -82,6 +82,8 @@ typedef struct uint8_t currentMode; ACCESSORY_t ports[NUM_PORTS]; + ACCESSORY_t *activeAccessory; + ACCESSORY_t *nextAccessory; float32_t maxPowerLimit; @@ -89,12 +91,18 @@ typedef struct bool status[NUM_STATUS_FLAGS]; + int hSysCheckTimer; + int hOneSecondTimer; + char tmpString[64]; } SYSTEM_DATA_t; void SYS_LoadFactoryDefaults(void); uint32_t SYS_GetLanguage(void); +SYSTEM_DATA_t* system_getSys(void); + +void system_monitor(void); #endif /* SYSTEM_SYSTEM_H_ */ diff --git a/source/USB/virtual_com.c b/source/USB/virtual_com.c index eea1f2a..a6a4014 100644 --- a/source/USB/virtual_com.c +++ b/source/USB/virtual_com.c @@ -41,6 +41,7 @@ extern uint8_t USB_EnterLowpowerMode(void); //Application Includes #include "flashUpdate.h" #include "usbComms.h" +#include "System/system.h" /******************************************************************************* * Definitions @@ -633,7 +634,17 @@ void USB_SendString(uint8_t * str) #if 1 //Send data //Wait for USB ready - while(!((1 == s_cdcVcom.attach) && (1 == s_cdcVcom.startTransactions))); //TODO: make this not block forever + // TODO: Need to timeout here + SYSTEM_DATA_t *sys = system_getSys(); + uint32_t timeout = sys->systemTime + 100; + while(!((1 == s_cdcVcom.attach) && (1 == s_cdcVcom.startTransactions))) + { + if (sys->systemTime > timeout) + { + txDataSize = 0; + return; + } + } uint32_t size = txDataSize; txDataSize = 0; @@ -642,7 +653,6 @@ void USB_SendString(uint8_t * str) if (error != kStatus_USB_Success) { /* Failure to send Data Handling code here */ - usb.sendErrorCount++; } diff --git a/source/display.c b/source/display.c index 8a3a79d..f91eed7 100644 --- a/source/display.c +++ b/source/display.c @@ -73,7 +73,7 @@ void Display_Bcast(void) } void Display_USB(void) { - GL_DrawMonoBitmap(usbIconSmall, LCD_X_MID-16, LCD_Y_MID, LCD_DRAW_SET); + GL_DrawMonoBitmap(usbIconSmall, LCD_X_MIN + 30, LCD_Y_MAX - usbIconSmall[1], LCD_DRAW_SET); } @@ -454,186 +454,79 @@ void Display_Wireless(uint8_t wireless) void Display_Mode(uint8_t Con_Mode1) { -uint8_t What; // What to display + SYSTEM_DATA_t * sys = system_getSys(); + ACCESSORY_t *active = sys->activeAccessory; - - switch(Con_Mode1) // Where to draw + if (active != NULL) { - case BROADCAST: - { - GL_DrawMonoBitmap(inductionIcon, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - break; - } - - case PORT1_A: // ACtually PORT 2 - What = ACCY_GetConnectedAccessory(1); - // What = ID_TX_DUAL_DIRECT; // Test - if(What == ID_CLAMP || What == ID_CLAMP2) - GL_DrawMonoBitmap(clampIcon2, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); - if(What == ID_TX_SINGLE_DIRECT) - GL_DrawMonoBitmap(directConnectIcon5, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - if(What == ID_TX_DUAL_DIRECT) - GL_DrawMonoBitmap(directConnectIcon3, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - - if (What == ID_TX_DUAL_DIRECT) - FL_DrawString("1A", LCD_X_MAX-40, LCD_Y_MIN+5, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); - else - FL_DrawString("1", LCD_X_MAX-40, LCD_Y_MIN, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); - - break; - - case PORT1_B: //can only be lower half of Dual -// GL_DrawMonoBitmap(box_unchecked, LCD_X_MAX-44, LCD_Y_MIN + 24, LCD_DRAW_SET); -// GL_DrawRectangle(189, 0, 239, 49, 1, LCD_DRAW_SET); -// GL_DrawRectangle(179, 0, 239, 59, 1, LCD_DRAW_SET); - GL_DrawMonoBitmap(directConnectIcon3, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - FL_DrawString("1B", LCD_X_MAX-40, LCD_Y_MIN+29, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); - - break; - - case PORT2_A: - What = ACCY_GetConnectedAccessory(2); - - if(What == ID_CLAMP || What == ID_CLAMP2) - GL_DrawMonoBitmap(clampIcon2, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); - if(What == ID_TX_SINGLE_DIRECT) - GL_DrawMonoBitmap(directConnectIcon5, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - - if(What == ID_TX_DUAL_DIRECT) - GL_DrawMonoBitmap(directConnectIcon3, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - - if (What == ID_TX_DUAL_DIRECT) - FL_DrawString("2A", LCD_X_MAX-40, LCD_Y_MIN+5, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); - else - FL_DrawString("2", LCD_X_MAX-40, LCD_Y_MIN, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); - - break; - case PORT2_B: //can only be lower half of Dual - GL_DrawMonoBitmap(directConnectIcon3, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); // Always draw - FL_DrawString("2B", LCD_X_MAX-40, LCD_Y_MIN+29, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); - - break; - } -} - - - - -#if 0 -void Display_Connection(CON_MODE_t Con_Output1, CON_MODE_t Con_Output2) -{ - - switch(Con_Output1) - { - case A1: - FL_DrawString("A1", LCD_X_MAX -48, LCD_Y_MIN , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - case A2: - FL_DrawString("A2", LCD_X_MAX-48, LCD_Y_MIN , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - case B1: - FL_DrawString("B1", LCD_X_MAX-48, LCD_Y_MIN , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - case B2: - FL_DrawString("B2", LCD_X_MAX-48, LCD_Y_MIN , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - } - - switch(Con_Output2) + switch (active->connected) { - case A1: - FL_DrawString("A1", LCD_X_MAX -48, LCD_Y_MIN+48 , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - case A2: - FL_DrawString("A2", LCD_X_MAX-48, LCD_Y_MIN+48 , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - case B1: - FL_DrawString("B1", LCD_X_MAX-48, LCD_Y_MIN+48 , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; - case B2: - FL_DrawString("B2", LCD_X_MAX-48, LCD_Y_MIN+48 , font14Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - break; + case ID_BROADCAST: + { + GL_DrawMonoBitmap(inductionIcon, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); + break; + } + + case ID_TX_SINGLE_DIRECT: + { + GL_DrawMonoBitmap(directConnectIcon5, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); + sprintf(sys->tmpString, "%d", active->portId); + FL_DrawString(sys->tmpString, LCD_X_MAX-40, LCD_Y_MIN, font12Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); + break; + } + + case ID_TX_DUAL_DIRECT: + { + GL_DrawMonoBitmap(directConnectIcon3, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); + break; + } + + case ID_CLAMP: + { + GL_DrawMonoBitmap(clampIcon2, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); + break; + } + + case ID_CLAMP2: + { + GL_DrawMonoBitmap(clampIcon2, LCD_X_MAX-60,LCD_Y_MIN + 2, LCD_DRAW_SET); + FL_DrawString("V2", LCD_X_MAX-40, LCD_Y_MIN+30, font10Bold, LCD_DRAW_SET, FL_ALIGN_CENTER); + break; + } + + } } + return; + + } -#endif void Display_Level(PWR_MODE_t Level) { -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); - // Print all bars + GL_DrawRectangle( 70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); + GL_DrawRectangle( 94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); + GL_DrawRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); + GL_DrawRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); switch(Level) { - case LEVEL0: -// GL_DrawMonoBitmap(power0, LCD_X_MID-60, LCD_Y_MID-64, LCD_DRAW_SET); - GL_DrawRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first + case LEVEL4: + GL_DrawFilledRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, LCD_DRAW_SET); -// GL_DrawFilledRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, LCD_DRAW_SET); //first + case LEVEL3: + GL_DrawFilledRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, LCD_DRAW_SET); - // GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); //second -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); //third -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); //fourth + case LEVEL2: + GL_DrawFilledRectangle( 94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, LCD_DRAW_SET); - break; - - case LEVEL1: -// GL_DrawMonoBitmap(power1, LCD_X_MID-60, LCD_Y_MID-64, LCD_DRAW_SET); -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); - GL_DrawRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - - GL_DrawFilledRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - break; - - case LEVEL2: -// GL_DrawMonoBitmap(power2, LCD_X_MID-60, LCD_Y_MID-64, LCD_DRAW_SET); -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); - GL_DrawRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - - GL_DrawFilledRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - GL_DrawFilledRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - break; - - case LEVEL3: -// GL_DrawMonoBitmap(power3, LCD_X_MID-60,LCD_Y_MID-64, LCD_DRAW_SET); -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); - GL_DrawRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - - GL_DrawFilledRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - GL_DrawFilledRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - GL_DrawFilledRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1,LCD_DRAW_SET); //first - - break; - - default: -// GL_DrawMonoBitmap(power4, LCD_X_MID-60, LCD_Y_MID-64, LCD_DRAW_SET); -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); - GL_DrawRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - GL_DrawRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, 2, LCD_DRAW_SET); //first - - GL_DrawFilledRectangle(70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - GL_DrawFilledRectangle(94, LCD_Y_MIN+15, 114, LCD_Y_MIN + 1, LCD_DRAW_SET); //first - GL_DrawFilledRectangle(118, LCD_Y_MIN+15, 138, LCD_Y_MIN + 1,LCD_DRAW_SET); //first - GL_DrawFilledRectangle(142, LCD_Y_MIN+15, 162, LCD_Y_MIN + 1, LCD_DRAW_SET); //first + case LEVEL1: + GL_DrawFilledRectangle( 70, LCD_Y_MIN+15, 90, LCD_Y_MIN + 1, LCD_DRAW_SET); } } @@ -766,7 +659,7 @@ void Display_Normal(void) Display_Frequency(frequency); if(Test_Mode) - FL_DrawString("Test", LCD_X_MIN, LCD_Y_MIN, font16Bold, LCD_DRAW_XOR, FL_ALIGN_LEFT); + FL_DrawString("BETA", LCD_X_MIN, LCD_Y_MIN-3, font16Bold, LCD_DRAW_XOR, FL_ALIGN_LEFT); } void Display_Flash_Bat(void) @@ -1009,69 +902,3 @@ void Display_OnScreen_Diagnostics(void) // FL_DrawString(tempString, 0, 80, font10Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); } - -//if(GPIO_PinRead(GPIO,1,6)) -// GL_DrawMonoBitmap(usbIconSmall, LCD_X_MID, 30, LCD_DRAW_SET); - -// if (countx >= 25) -// countx = 0; -// else -// countx++; -// -// if (!GPIO_PinRead(GPIO, 0, 31)) -// { -// FL_DrawString("Key0", 12, 80, font18Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); -// } - - -// tunaX += tunaXStep; -// if(tunaX > tunaXMax) -// { -// tunaX = 0; -// } - - - -// char a[] = "29KHz"; -// char *ptr1 = a; - -// char *frqptr[26] = {" 98Hz","128Hz","263Hz","440Hz","512Hz","560Hz","577Hz","640Hz","815Hz","870Hz", -// "940Hz","1.02KHz","1.17KHz","3.14KHz","4.09KHz","6.00KHz","8.01KHz","9.82KHz","29.4KHz", -// "32.7KHz","44.6KHz","65.5KHz","83.0KHz","131KHz","200KHz","480KHz"}; - - -// GL_DrawLine(1, 1, 33, 33, 3, LCD_DRAW_SET); -// GL_DrawFilledRectangle(160, 0, 200, 40, LCD_DRAW_SET); - -// FL_DrawString("Over Voltage!", tunaX, 80, font18Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - -// GL_DrawMonoBitmap(anchor, 160, 20, LCD_DRAW_SET); - -// GL_DrawFilledRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, LCD_DRAW_XOR); - -// GL_DrawRectangle(00, LCD_Y_MAX-40, 40, LCD_Y_MAX, 1, LCD_DRAW_SET); -// GL_DrawRectangle(50, LCD_Y_MAX-30, 80, LCD_Y_MAX, 1, LCD_DRAW_SET); - - // FL_DrawString("8 KHz", LCD_X_MAX, LCD_Y_MAX - FL_GetFontHeight(font18Bold), font18Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); -//# FL_DrawString( ptr1, LCD_X_MAX, LCD_Y_MAX - FL_GetFontHeight(font18Bold), font18Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); -// FL_DrawString( frqptr[countx], LCD_X_MAX, LCD_Y_MAX - FL_GetFontHeight(font18Bold), font18Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); -// FL_DrawString( frqptr[countx], LCD_X_MAX, LCD_Y_MAX - 22, font18Bold, LCD_DRAW_SET, FL_ALIGN_RIGHT); -// GL_DrawRectangle(0, 0, LCD_X_MAX, LCD_Y_MAX, 1, LCD_DRAW_SET); // Outer Border - - - - - - - -// sprintf(tempString, "1: %.2f", sys.adc.V_ID1); -// FL_DrawString( tempString, 0, 0, font12Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); -// sprintf(tempString, "2: %.2f", sys.adc.V_ID2); -// FL_DrawString( tempString, 0, 15, font12Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); -// sprintf(tempString, "%d", Power_Level); // Display number -// FL_DrawString(tempString, 120, 00, font18Bold, LCD_DRAW_SET, FL_ALIGN_LEFT); - -// Display_Backlight(1); -//SetBacklightPower(1); - - diff --git a/source/frq.c b/source/frq.c index a713f48..738bfbb 100644 --- a/source/frq.c +++ b/source/frq.c @@ -29,7 +29,6 @@ extern uint8_t Taps_Flag; extern uint32_t new_freq; extern uint8_t frequency,Cur_Mode,LD_Flag; extern uint8_t Dds_Pot_Val[2]; -extern uint16_t Sys_Chk_tmr; uint8_t Tx_mode, frq_chg_tmr,old_freq; FREQDATA_t freq; @@ -941,7 +940,8 @@ uint8_t count; { frequency = Next_Frequency(frequency); // increment the frequency new_freq = freqArray[frequency].frequency1; - Sys_Chk_tmr= DELAY_500MS; // Allow system settling before checking measurements + //Sys_Chk_tmr= DELAY_500MS; // Allow system settling before checking measurements + // catch_up_flag = true; } diff --git a/source/init.c b/source/init.c index 80523f4..1c9609e 100644 --- a/source/init.c +++ b/source/init.c @@ -36,6 +36,7 @@ #include "battery.h" #include "keys.h" #include "System/system.h" +#include "soft_timer.h" extern uint8_t OverVolts_Flag,catch_up_flag; // global OverVoltsFlag extern MODE_REC_t mode_Array[MODE_MAX_NUM]; @@ -96,8 +97,12 @@ void Init_vars() } } + stimer_init(); + + sys.hSysCheckTimer = stimer_start(1000, TIMER_MODE_PERIODIC, NULL, NULL); + sys.hOneSecondTimer = stimer_start(1000, TIMER_MODE_PERIODIC, NULL, NULL); + Estop_timer = 0; - Sys_Chk_tmr = 0; Vchktmr = 0; Key_Lock_Out_tmr = 0; step_count = 0; @@ -274,7 +279,7 @@ void Normal_Init(void) 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(); @@ -343,7 +348,7 @@ void Normal_Init(void) // Select_Bypass(OFF); // HACK - Select_Estop(OFF); // Ensure output is ISOLATED from connections + Select_Estop(CONNECTED); // Ensure output is ISOLATED from connections Init_Amplitude(); } diff --git a/source/main.c b/source/main.c index 7751b30..d4714ac 100644 --- a/source/main.c +++ b/source/main.c @@ -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 } diff --git a/source/ports.c b/source/ports.c index 51b3000..cb83e59 100644 --- a/source/ports.c +++ b/source/ports.c @@ -30,6 +30,7 @@ #include "pwm.h" #include "pwr_level.h" #include "System/system.h" +#include "usbComms.h" ACCESSORY_t accy1; ACCESSORY_t accy2; @@ -54,43 +55,112 @@ static int handleBroadcast(ACCESSORY_t *accy) switch (accy->state) { case PORT_STATE_INIT: + { + if (accy->initState) + { + USB_SendString("Broadcast initializing..."); + accy->stateTimer = sys.systemTime + 2000; + } + + if (sys.systemTime >= accy->stateTimer) + { + accy->state = PORT_STATE_RUNNING; + } break; + } case PORT_STATE_DEINIT: - break; + { + if (accy->initState) + { + USB_SendString("Broadcast deinitializing..."); + accy->stateTimer = sys.systemTime + 2000; + } + if (sys.systemTime >= accy->stateTimer) + { + accy->state = PORT_STATE_STANDBY; + USB_SendString("Broadcast in standby!"); + } + + break; + } + + case PORT_STATE_RUNNING: + { + if (accy->initState) + { + USB_SendString("Broadcast running!"); + } + break; + } default: return -1; } + return 0; } static int handleClamp(ACCESSORY_t *accy) { + switch (accy->state) { case PORT_STATE_INIT: + { + if (accy->initState) + { + + } break; + } case PORT_STATE_DEINIT: + accy->state = PORT_STATE_STANDBY; break; default: return -1; } + return 0; } -static int handleDirect(ACCESSORY_t *accy) +static int handleDirectConnect(ACCESSORY_t *accy) { switch (accy->state) { case PORT_STATE_INIT: + { + if (accy->initState) + { + accy->stateTimer = sys.systemTime + 2000; + + USB_SendString("Direct connect initializing..."); + } + + if (sys.systemTime >= accy->stateTimer) + { + accy->state = PORT_STATE_RUNNING; + } break; + } case PORT_STATE_DEINIT: + accy->state = PORT_STATE_STANDBY; break; + case PORT_STATE_STANDBY: + break; + + case PORT_STATE_RUNNING: + { + if (accy->initState) + { + USB_SendString("Direct connect running!"); + } + break; + } default: return -1; } @@ -101,44 +171,184 @@ static int initAccessory(ACCESSORY_t *accy) { accy->state = PORT_STATE_INIT; accy->initState = true; - accy->handler(accy); + //accy->handler(accy); } static int deinitAccessory(ACCESSORY_t *accy) { - accy->state = PORT_STATE_DEINIT; - accy->initState = true; - accy->handler(accy); + if (accy->isConnected) + { + accy->state = PORT_STATE_DEINIT; + accy->initState = true; + } + //accy->handler(accy); } -void ACCY_Connect(ACCESSORY_t *accy, ACCY_ID_t id) +void ACCY_service(void) +{ + ACCY_Update(); + + ACCESSORY_t *active = sys.activeAccessory; + + // if there is an accessory waiting to be switched, check that the current active is in standby before initializing + if (active != NULL) + { + if (active->isConnected) + { + // service the current accessory + PortState_t prevState = active->state; + active->handler(active); + active->initState = (prevState != active->state); + } + else + { + // active accessory was disconnected + // fall back to induction for now + ACCY_setActive(&sys.ports[ACCY_PORT_INDUCTION], CHANNEL_A); + } + + + if ((sys.nextAccessory) != NULL) + { + // wait until current accessory has been deinitialized before switching + if (active->state == PORT_STATE_STANDBY) + { + sys.activeAccessory = sys.nextAccessory; + sys.nextAccessory = NULL; + initAccessory(active); + } + } + } + + +} + +void ACCY_next(void) +{ + // cycle through accessories and channels to find the next available + AccessoryPortId_t currentPort = sys.activeAccessory->portId; + AccyChannelId_t currentChannel = sys.activeAccessory->activeChannel; + + + AccessoryPortId_t port = currentPort; + AccyChannelId_t channel = currentChannel + 1; + + + do + { + + if (channel >= NUM_CHANNELS) + { + port++; + + if (port >= NUM_PORTS) + { + port = 0; + } + + channel = CHANNEL_A; + } + + AccessoryChannel_t *ch = &sys.ports[port].channels[channel]; + + if (ch->connected) + { + ACCY_setActive(&sys.ports[port], channel); + break; + } + + channel++; + + } while ((port != currentPort) || (channel != currentChannel)); + + +} + +void ACCY_setActive(ACCESSORY_t *accy, AccyChannelId_t channel) +{ + accy->activeChannel = channel; + // deinitialize current accessory + if (sys.activeAccessory != NULL) + { + deinitAccessory(sys.activeAccessory); + + // initialize the accessory to switch when complete + sys.nextAccessory = accy; + + } + else + { + // nothing active, switch now + sys.activeAccessory = accy; + initAccessory(sys.activeAccessory); + } + + + +} + +void ACCY_Disconnect(ACCESSORY_t *accy) +{ + + sprintf(sys.tmpString, "Accessory disconnected: %d", accy->connected); + USB_SendString(sys.tmpString); + + accy->connected = ID_NONE; + accy->isConnected = false; + + accy->handler = NULL; + + accy->state = PORT_STATE_STANDBY; + + accy->channels[CHANNEL_A].id = CHANNEL_A; + accy->channels[CHANNEL_A].connected = false; + + accy->channels[CHANNEL_B].id = CHANNEL_B; + accy->channels[CHANNEL_B].connected = false; + + +} + + +// setup the accessory as connected but do not initialize it until set as active +bool ACCY_Connect(ACCESSORY_t *accy, ACCY_ID_t id) { accy->connected = id; accy->isConnected = true; - accy->state = PORT_STATE_INIT; + accy->state = PORT_STATE_STANDBY; - accy->channels[0] = 0; - accy->channels[1] = 0; + accy->channels[CHANNEL_A].id = CHANNEL_A; + accy->channels[CHANNEL_A].connected = false; + + accy->channels[CHANNEL_B].id = CHANNEL_B; + accy->channels[CHANNEL_B].connected = false; switch (id) { - case ID_TX_SINGLE_DIRECT: case ID_TX_DUAL_DIRECT: { - accy->handler = handleDirect; + accy->channels[CHANNEL_B].connected = true; + // intentional fall through + } + case ID_TX_SINGLE_DIRECT: + { + accy->channels[CHANNEL_A].connected = true; + accy->handler = handleDirectConnect; break; } case ID_CLAMP: case ID_CLAMP2: { + accy->channels[CHANNEL_A].connected = true; accy->handler = handleClamp; break; } case ID_BROADCAST: { + accy->channels[CHANNEL_A].connected = true; accy->handler = handleBroadcast; break; } @@ -150,6 +360,11 @@ void ACCY_Connect(ACCESSORY_t *accy, ACCY_ID_t id) accy->isConnected = false; } } + + sprintf(sys.tmpString, "New accessory connected: %d", accy->connected); + USB_SendString(sys.tmpString); + + return accy->isConnected; } static ACCY_ID_t ReadAccessory(uint8_t port) @@ -237,7 +452,7 @@ void Disconnect(uint8_t port) // called when you disconnect //Configure frequency to make sure things are setup correctly // Tx_ConfigureFrequency(); } -void ACCY_Init(void) +void ACCY_Init(void) { for (int i=0; iconnected == ID_NONE) //Nothing connected. Look for accessory to connect + if(!accy->isConnected) //Nothing connected. Look for accessory to connect { - if((detected == accy->lastDetected) && (detected != ID_NONE)) //If detected same as last time and not ID_NONE { if(++accy->consecutiveScans == AC_NUM_SCANS_TO_CONNECT) //Connect on 3rd consecutive scan of same accessory { - ACCY_Connect(accy, detected); + // we found a newly connected accessory. switch to it. + if (ACCY_Connect(accy, detected)) + { + ACCY_setActive(accy, CHANNEL_A); + } } } else //If different than last scan @@ -288,88 +508,17 @@ void ACCY_Update(void) accy->lastDetected = detected; //remember what was scanned last time accy->consecutiveScans = 0; } - } else if (detected != accy->connected) //If connected and detected reads different, disconnect { - Disconnect(i); + ACCY_Disconnect(accy); } } } //Call @ 10Hz to detect, connect, and disconnect accessories -void ACCY_Update1(void) -{ - //read ID voltage and determine connected accessory *for each port* - //Require same accessory detected 3 consecutive updates (to de-glitch) - - //Read accessory voltage and identify accessory - - ACCY_ID_t detected = ReadAccessory(1); - - if(accy1.connected == ID_NONE) //Nothing connected. Look for accessory to connect - { - - if((detected == accy1.lastDetected) && (detected != ID_NONE)) //If detected same as last time and not ID_NONE - { - - if(++accy1.consecutiveScans == AC_NUM_SCANS_TO_CONNECT) //Connect on 3rd consecutive scan of same accessory - { - ACCY_Connect1(accy1.lastDetected); //CONNECT - } - } - else //If different than last scan - { - accy1.lastDetected = detected; //remember what was scanned last time - accy1.consecutiveScans = 0; - } - - } - else if (detected != accy1.connected) //If connected and detected reads different, disconnect - { - Disconnect(ACCY_PORT_1); - } - -} -//Call @ 10Hz to detect, connect, and disconnect accessories -void ACCY_Update2(void) -{ - //read ID voltage and determine connected accessory *for each port* - - //Require same accessory detected 3 consecutive updates (to de-glitch) - - //Read accessory voltage and identify accessory - - ACCY_ID_t detected = ReadAccessory(2); - - whatever = ReadAccessory(2); - - if(accy2.connected == ID_NONE) //Nothing connected. Look for accessory to connect - { - - if((detected == accy2.lastDetected) && (detected != ID_NONE)) //If detected same as last time and not ID_NONE - { - - if(++accy2.consecutiveScans == AC_NUM_SCANS_TO_CONNECT) //Connect on 3rd consecutive scan of same accessory - { - ACCY_Connect2(accy2.lastDetected); //CONNECT - } - } - else //If different than last scan - { - accy2.lastDetected = detected; //remember what was scanned last time - accy2.consecutiveScans = 0; - } - - } - else if (detected != accy2.connected) //If connected and detected reads different, disconnect - { - Disconnect(ACCY_PORT_2); - } - -} /* * Connect accessory */ @@ -390,83 +539,10 @@ bool ACCY_isKnown(ACCY_ID_t id) return known; } -void ACCY_Connect1(ACCY_ID_t id) -{ - accy1.connected = id; - accy1.isConnected = true; - - switch(id) - { - case ID_TX_SINGLE_DIRECT: - Port_changed_flag = true; //connect it - break; - case ID_TX_DUAL_DIRECT: - Port_changed_flag = true; //connect it - break; - case ID_CLAMP: - Port_changed_flag = true; //connect it - break; - case ID_CLAMP2: - Port_changed_flag = true; //connect it - break; - - - default: - accy1.connected = ID_NONE; - accy1.isConnected = false; - } - - - //Configure frequency to make sure things are setup correctly -// Tx_ConfigureFrequency(); - -} -void ACCY_Connect2(ACCY_ID_t id) -{ - accy2.connected = id; - accy2.isConnected = true; - - switch(id) - { - case ID_TX_SINGLE_DIRECT: - Port_changed_flag = true;//connect it - break; - case ID_TX_DUAL_DIRECT: - Port_changed_flag = true;//connect it - break; - case ID_CLAMP: - Port_changed_flag = true;//connect it - break; - case ID_CLAMP2: - Port_changed_flag = true;//connect it - break; - - default: - accy2.connected = ID_NONE; - accy2.isConnected = false; - } - - - //Configure frequency to make sure things are setup correctly -// Tx_ConfigureFrequency(); - -} -bool ACCY_IsConnected1(uint8_t port) -{ - if(port == 1) - return accy1.isConnected; - else - return accy2.isConnected; - -} ACCY_ID_t ACCY_GetConnectedAccessory(uint8_t port) { - if(port == 1) - return accy1.connected; - else - return accy2.connected; - + return ID_NONE; } ACCY_ID_t ACCY_GetActive(void) @@ -492,64 +568,6 @@ void Read_Tx_Ports(void) // check for whats plugged in at the ports every 100mS. ACCY_Update(); - //ACCY_Update1(); - - if((mode_Array[PORT1_A].Selected == false) && (mode_Array[PORT1_B].Selected == false) && (accy1.isConnected)) //new bit here - Cur_Mode = PORT1_A; // Something is connected to PORT1 - - switch(accy1.connected) // Find out what it is - { - case ID_TX_DUAL_DIRECT: //Transmitter accessory - mode_Array[PORT1_A].Selected = accy1.isConnected; - mode_Array[PORT1_B].Selected = accy1.isConnected; - break; - - case ID_TX_SINGLE_DIRECT: - mode_Array[PORT1_A].Selected = accy1.isConnected; - break; - - case ID_CLAMP: - mode_Array[PORT1_A].Selected = accy1.isConnected; - break; - - case ID_CLAMP2: - mode_Array[PORT1_A].Selected = accy1.isConnected; - break; - - default: //empty - mode_Array[PORT1_A].Selected = accy1.isConnected; - mode_Array[PORT1_B].Selected = accy1.isConnected; - } - - - //ACCY_Update2(); - if((mode_Array[PORT2_A].Selected == false) && (mode_Array[PORT2_B].Selected == false) && (accy2.isConnected)) //new bit here - Cur_Mode = PORT2_A; - - switch(accy2.connected) - { - case ID_TX_DUAL_DIRECT: //Transmitter accessory - mode_Array[PORT2_A].Selected = accy2.isConnected; - mode_Array[PORT2_B].Selected = accy2.isConnected; - break; - - case ID_TX_SINGLE_DIRECT: - mode_Array[PORT2_A].Selected = accy2.isConnected; - break; - - case ID_CLAMP: - mode_Array[PORT2_A].Selected = accy2.isConnected; - break; - case ID_CLAMP2: - mode_Array[PORT2_A].Selected = accy2.isConnected; - break; - - default: //empty - mode_Array[PORT2_A].Selected = accy2.isConnected; - mode_Array[PORT2_B].Selected = accy2.isConnected; - - - } } @@ -573,7 +591,7 @@ void Select_Output_Port(void) // which DC Out port, to switch on } // HACK - Select_Estop(OFF); // Ensure output is not ISOLATED from connections + Select_Estop(CONNECTED); // Ensure output is not ISOLATED from connections switch (Cur_Mode) { @@ -602,7 +620,7 @@ void Select_Output_Port(void) // which DC Out port, to switch on else { // Assumes broadcast mode has just been selected Disable_DC(); // Shut down Direct connect circuitry - Select_Estop(ON); // Ensure output is ISOLATED from connections + Select_Estop(ISOLATED); // Ensure output is ISOLATED from connections Tx_ConfigureFrequency(); // Select correct frequency Enable_BC(); // Enable BCAST circuitry using either Minimum or previously selected freq diff --git a/source/ports.h b/source/ports.h index 162be8b..d7a46ce 100644 --- a/source/ports.h +++ b/source/ports.h @@ -32,13 +32,15 @@ typedef enum { PORT_STATE_DEINIT = -1, PORT_STATE_INIT = 0, + PORT_STATE_STANDBY = 1, + PORT_STATE_RUNNING = 2, } PortState_t; typedef enum { - ACCY_PORT_INDUCTION = -1, - ACCY_PORT_1 = 0, - ACCY_PORT_2 = 1, + ACCY_PORT_INDUCTION = 0, + ACCY_PORT_1 = 1, + ACCY_PORT_2 = 2, NUM_PORTS } AccessoryPortId_t; @@ -67,6 +69,12 @@ struct ACCESSORY_s { AccessoryChannel_t channels[NUM_CHANNELS]; + AccyChannelId_t activeChannel; + + uint32_t stateTimer; + + AccessoryPortId_t portId; + }; #define DELAY_100MS 10 @@ -94,6 +102,7 @@ typedef enum { void ACCY_Init(void); bool ACCY_isKnown(ACCY_ID_t id); +void ACCY_Update(void); void ACCY_Update1(void); void ACCY_Update2(void); void Read_Tx_Ports(void); // check for whats plugged in at the ports. @@ -113,4 +122,11 @@ uint32_t Search_Frequency(uint32_t pattern); bool Is_Clamp_Detected(void); void Cycled_Freq_Change_BC(uint8_t value); void Reset_Power_Gain(void); + + +void ACCY_service(void); + +bool ACCY_Connect(ACCESSORY_t *accy, ACCY_ID_t id); +void ACCY_setActive(ACCESSORY_t *accy, AccyChannelId_t channel); +void ACCY_next(void); #endif /* PORTS_H_ */ diff --git a/source/pro_key.c b/source/pro_key.c index cc5b06f..b5e8096 100644 --- a/source/pro_key.c +++ b/source/pro_key.c @@ -75,29 +75,9 @@ void pro_key(uint32_t pressed) break; - // case MENU_KEY: - // Task = MENU_TASK; // display the menus - // break; - - case MODE_KEY: // End Primary Key -// Task = MODE_TASK; // Display the Modes - - #if 0 //Use mode key to toggle current gain - if(Port_State[MID_SR] & I_GAIN) - { - Port_State[MID_SR] &= ~I_GAIN; - } - else - { - Port_State[MID_SR] |= I_GAIN; - } - SPI0_SendBytes(Port_State, 3, EXPANDER); - //Sys_Chk_tmr = DELAY_1S; -#else Task = MODE_TASK; // Display the Modes -#endif break; diff --git a/source/pwr_level.c b/source/pwr_level.c index 2296737..6ed58db 100644 --- a/source/pwr_level.c +++ b/source/pwr_level.c @@ -92,7 +92,7 @@ uint8_t Dds_Pot_Val[2]; // 2 byte Data for SPI extern uint8_t Cur_Mode,Taps_Flag, Bat_Type; extern volatile uint8_t BC_Duty_Cycle; extern volatile bool PWM_Update_Flag; -extern uint16_t Sys_Chk_tmr,Taps_adjust_timer; +extern uint16_t Taps_adjust_timer; extern uint8_t Bat_Type; REGULATE_t reg; @@ -413,7 +413,7 @@ void Request_Current_Change(void) SPI0_SendBytes(Dds_Pot_Val, 2, AMPLITUDE); - Sys_Chk_tmr= DELAY_500MS; // Allow system settling before checking measurements + //Sys_Chk_tmr= DELAY_500MS; // Allow system settling before checking measurements Taps_Flag = false; Taps_adjust_timer = DELAY_2S; catch_up_flag = true; diff --git a/source/soft_timer.c b/source/soft_timer.c new file mode 100644 index 0000000..60a1700 --- /dev/null +++ b/source/soft_timer.c @@ -0,0 +1,90 @@ +#include "soft_timer.h" + +static soft_timer_t timers[MAX_TIMERS]; + +void stimer_init(void) { + for (int i = 0; i < MAX_TIMERS; ++i) + { + timers[i].active = false; + timers[i].inUse = false; + } +} + +void stimer_end(int timer_id) +{ + if (timer_id >= 0 && timer_id < MAX_TIMERS) + { + timers[timer_id].active = false; + timers[timer_id].inUse = false; + } +} + +int stimer_start(uint32_t timeout_ticks, timer_mode_t mode, timer_callback_t cb, void* ctx) { + for (int i = 0; i < MAX_TIMERS; ++i) { + if (!timers[i].inUse) { + timers[i].timeout_ticks = timeout_ticks; + timers[i].remaining_ticks = timeout_ticks; + timers[i].mode = mode; + timers[i].callback = cb; + timers[i].callback_context = ctx; + timers[i].inUse = true; + timers[i].active = true; + timers[i].fired = false; + return i; + } + } + return -1; // No free timer slot +} + +void stimer_stop(int timer_id) { + if (timer_id >= 0 && timer_id < MAX_TIMERS) + { + timers[timer_id].active = false; + } +} + +bool stimer_is_active(int timer_id) { + return timer_id >= 0 && timer_id < MAX_TIMERS && timers[timer_id].active; +} + +bool stimer_fired(int timer_id) +{ + if (timer_id >= 0 && timer_id < MAX_TIMERS) { + return timers[timer_id].fired; + } + return false; +} + +bool stimer_clearFired(int timer_id) +{ + if (timer_id >= 0 && timer_id < MAX_TIMERS) + { + timers[timer_id].fired = false; + } +} + +void stimer_update(void) { + for (int i = 0; i < MAX_TIMERS; ++i) { + if (timers[i].active && timers[i].remaining_ticks > 0) { + timers[i].remaining_ticks--; + if (timers[i].remaining_ticks == 0) + { + timers[i].fired = true; + + if (timers[i].callback) + { + timers[i].callback(timers[i].callback_context); + } + + if (timers[i].mode == TIMER_MODE_PERIODIC) + { + timers[i].remaining_ticks = timers[i].timeout_ticks; + } + else + { + timers[i].active = false; + } + } + } + } +} diff --git a/source/soft_timer.h b/source/soft_timer.h new file mode 100644 index 0000000..9daba8c --- /dev/null +++ b/source/soft_timer.h @@ -0,0 +1,36 @@ +#ifndef SOFT_TIMER_H +#define SOFT_TIMER_H + +#include +#include + +#define MAX_TIMERS 8 + +typedef enum { + TIMER_MODE_ONE_SHOT, + TIMER_MODE_PERIODIC +} timer_mode_t; + +typedef void (*timer_callback_t)(void* context); + +typedef struct { + uint32_t timeout_ticks; + uint32_t remaining_ticks; + bool active; + bool inUse; + bool fired; + timer_mode_t mode; + timer_callback_t callback; + void* callback_context; +} soft_timer_t; + +void stimer_init(void); +int stimer_start(uint32_t timeout_ticks, timer_mode_t mode, timer_callback_t cb, void* ctx); +void stimer_stop(int timer_id); +void stimer_end(int timer_id); +bool stimer_fired(int timer_id); +bool stimer_clearFired(int timer_id); +void stimer_update(void); // Should be called from timer ISR +bool stimer_is_active(int timer_id); + +#endif diff --git a/source/sys_chk.c b/source/sys_chk.c index c8ef4fe..f084955 100644 --- a/source/sys_chk.c +++ b/source/sys_chk.c @@ -142,7 +142,7 @@ void Check_For_Open_Circuit(void) if (Compare_Voltage(sys.adc.V_CHK, MAX_OP_VOLTS)) //Potentially fatal voltage { - Select_Estop(ON); // Fault condition disable output + Select_Estop(ISOLATED); // Fault condition disable output Select_Bypass(OFF); // Force into blocked state. Task = SAFETY_TASK; } diff --git a/source/timer.c b/source/timer.c index 7611846..9ce8695 100644 --- a/source/timer.c +++ b/source/timer.c @@ -22,6 +22,7 @@ #include "utils.h" #include "usbComms.h" #include "System/system.h" +#include "soft_timer.h" /******************************************************************************* * Definitions @@ -58,7 +59,7 @@ ctimer_config_t config; 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; +extern uint16_t Port_timer, Taps_adjust_timer,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}; @@ -72,26 +73,26 @@ uint32_t systemTime = 0; // 10mS ticks #define TIMER_SCALE 10 -void ctimer_match0_callback(uint32_t flags) // ISR every 10mS +void ctimer_match0_callback(uint32_t flags) // ISR every 1mS { 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 + sys.systemTime++; // increment system time by 1mS - Check_Live_Voltage(); + stimer_update(); - if ((systemTime % TIMER_SCALE != 0)) + + + //Check_Live_Voltage(); + + if ((systemTime % TIMER_SCALE != 0)) // 10ms timer { return; } + system_monitor(); + if (Sys_Timer > 0) Sys_Timer--; @@ -122,8 +123,6 @@ void ctimer_match0_callback(uint32_t flags) // ISR every 10mS 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--; @@ -136,7 +135,7 @@ void ctimer_match0_callback(uint32_t flags) // ISR every 10mS { ADC_Update(); } -// GPIO_PinWrite(GPIO, 0, 4, 0); // TODO Whats' this for ?? Probably left over debug of timer + } /*! diff --git a/source/utils.c b/source/utils.c index 7ade76e..5782c5b 100644 --- a/source/utils.c +++ b/source/utils.c @@ -33,7 +33,6 @@ //#define ON 1 -uint16_t Sys_Chk_tmr; extern uint8_t Port_State[]; extern uint16_t Estop_timer; extern uint8_t Over_Voltage_Flag, LD_Flag; @@ -248,7 +247,7 @@ void Check_Live_Voltage() { if (Compare_Voltage(sys.adc.V_CHK, MAX_OP_VOLTS)) //Potentially fatal voltage { - Select_Estop(ON); // Fault condition disable output + Select_Estop(ISOLATED); // Fault condition disable output Select_Bypass(OFF); // Force into blocked state. // Over_Voltage_Flag = true; // Estop_timer = DELAY_5S; @@ -327,7 +326,7 @@ float32_t Get_Power_Limit(void) } - +// if over power, scale amplitude void Adjust_Output_Power(float32_t New_Power_Limit) { if (Watts_Filt > New_Power_Limit) @@ -355,7 +354,7 @@ void Check_PSU_Short(void) // if output less than 10% of expected PSU voltage { if ((sys.adc.V_PSU * 1.2) < Convert_Pot2_Volts(Psu_Pot_Val[1])) { - Select_Estop(ON); // Disconnect from external threats + Select_Estop(ISOLATED); // Disconnect from external threats Power_Level = 0; // set demand to minimum to prevent damage // Update_Amp_Pot(); @@ -376,7 +375,7 @@ void Check_PSU_Short(void) // if output less than 10% of expected PSU voltage void Estop_Mode(void) { - Select_Estop(ON); + Select_Estop(ISOLATED); Task = ESTOP_TASK; } @@ -459,7 +458,6 @@ void Chk_Gain(void) // SPI0_SendBytes(Port_State, 3, EXPANDER); } - Sys_Chk_tmr = DELAY_1S; }