diff --git a/app/app.c b/app/app.c index 9e44243..604d73f 100644 --- a/app/app.c +++ b/app/app.c @@ -194,8 +194,8 @@ static void HandleIncoming(void) gFoundCTCSS = false; } - if (g_CDCSS_Lost && gCDCSSCodeType == CDCSS_POSITIVE_CODE - && (gCurrentCodeType == CODE_TYPE_DIGITAL || gCurrentCodeType == CODE_TYPE_REVERSE_DIGITAL)) + if (g_CDCSS_Lost && gCDCSSCodeType == CDCSS_POSITIVE_CODE + && (gCurrentCodeType == CODE_TYPE_DIGITAL || gCurrentCodeType == CODE_TYPE_REVERSE_DIGITAL)) { gFoundCDCSS = false; } @@ -936,17 +936,17 @@ void APP_Update(void) gEeprom.BATTERY_SAVE == 0 || gScanStateDir != SCAN_OFF || gCssBackgroundScan || - gScreenToDisplay != DISPLAY_MAIN + gScreenToDisplay != DISPLAY_MAIN #ifdef ENABLE_FMRADIO || gFmRadioMode #endif -#ifdef ENABLE_DTMF_CALLING +#ifdef ENABLE_DTMF_CALLING || gDTMF_CallState != DTMF_CALL_STATE_NONE #endif ){ gBatterySaveCountdown_10ms = battery_save_count_10ms; } - else + else #ifdef ENABLE_NOAA if ((!IS_NOAA_CHANNEL(gEeprom.ScreenChannel[0]) && !IS_NOAA_CHANNEL(gEeprom.ScreenChannel[1])) || !gIsNoaaMode) #endif @@ -965,12 +965,12 @@ void APP_Update(void) } - if (gPowerSaveCountdownExpired && gCurrentFunction == FUNCTION_POWER_SAVE -#ifdef ENABLE_VOICE + if (gPowerSaveCountdownExpired && gCurrentFunction == FUNCTION_POWER_SAVE +#ifdef ENABLE_VOICE && gVoiceWriteIndex == 0 -#endif +#endif ) - { + { static bool goToSleep; // wake up, enable RX then go back to sleep if (gRxIdleMode) @@ -1025,13 +1025,13 @@ void APP_Update(void) static void CheckKeys(void) { - if (0 + if (0 #ifdef ENABLE_DTMF_CALLING - || gSetting_KILLED -#endif -#ifdef ENABLE_AIRCOPY + || gSetting_KILLED +#endif +#ifdef ENABLE_AIRCOPY || (gScreenToDisplay == DISPLAY_AIRCOPY && gAircopyState != AIRCOPY_READY) -#endif +#endif ) return; @@ -1074,7 +1074,7 @@ static void CheckKeys(void) boot_counter_10ms = 0; // cancel boot screen/beeps if any key pressed if (gKeyReading0 != Key) // new key pressed - { + { if (gKeyReading0 != KEY_INVALID && Key != KEY_INVALID) ProcessKey(gKeyReading1, false, gKeyBeingHeld); // key pressed without releasing previous key @@ -1087,7 +1087,7 @@ static void CheckKeys(void) gDebounceCounter++; if (gDebounceCounter == key_debounce_10ms) // debounced new key pressed - { + { if (Key == KEY_INVALID) //all non PTT keys released { if (gKeyReading1 != KEY_INVALID) // some button was pressed before @@ -1110,7 +1110,7 @@ static void CheckKeys(void) return; if (gDebounceCounter == key_repeat_delay_10ms) //initial key repeat with longer delay - { + { if (Key != KEY_PTT) { gKeyBeingHeld = true; @@ -1118,7 +1118,7 @@ static void CheckKeys(void) } } else //subsequent fast key repeats - { + { if (Key == KEY_UP || Key == KEY_DOWN) // fast key repeats for up/down buttons { gKeyBeingHeld = true; @@ -1135,6 +1135,7 @@ static void CheckKeys(void) void APP_TimeSlice10ms(void) { + gNextTimeslice = false; gFlashLightBlinkCounter++; #ifdef ENABLE_BOOT_BEEPS @@ -1278,9 +1279,9 @@ void APP_TimeSlice10ms(void) } #endif - + SCANNER_TimeSlice10ms(); - + #ifdef ENABLE_AIRCOPY if (gScreenToDisplay == DISPLAY_AIRCOPY && gAircopyState == AIRCOPY_TRANSFER && gAirCopyIsSendMode == 1) { @@ -1317,6 +1318,7 @@ void cancelUserInputModes(void) // this is called once every 500ms void APP_TimeSlice500ms(void) { + gNextTimeslice_500ms = false; bool exit_menu = false; // Skipped authentic device check @@ -1421,7 +1423,7 @@ void APP_TimeSlice500ms(void) BATTERY_GetReadings(true); } } - + // regular display updates (once every 2 sec) - if need be if ((gBatteryCheckCounter & 3) == 0) { @@ -1433,23 +1435,22 @@ void APP_TimeSlice500ms(void) #endif } - #ifdef ENABLE_FMRADIO - if ((gFM_ScanState == FM_SCAN_OFF || gAskToSave) && !gCssBackgroundScan) - #else - if (!gCssBackgroundScan) - #endif - { - #ifdef ENABLE_AIRCOPY - if (gScanStateDir == SCAN_OFF && gScreenToDisplay != DISPLAY_AIRCOPY && !SCANNER_IsScanning()) - #else - if (gScanStateDir == SCAN_OFF && !SCANNER_IsScanning()) - #endif - { - if (gEeprom.AUTO_KEYPAD_LOCK && gKeyLockCountdown > 0 && !gDTMF_InputMode && gScreenToDisplay != DISPLAY_MENU) - { - if (--gKeyLockCountdown == 0) + if (!gCssBackgroundScan +#ifdef ENABLE_FMRADIO + && (gFM_ScanState == FM_SCAN_OFF || gAskToSave) +#endif + ) { + if (gScanStateDir == SCAN_OFF && !SCANNER_IsScanning() +#ifdef ENABLE_AIRCOPY + && gScreenToDisplay != DISPLAY_AIRCOPY +#endif + ) { + if (gEeprom.AUTO_KEYPAD_LOCK && gKeyLockCountdown > 0 && !gDTMF_InputMode && gScreenToDisplay != DISPLAY_MENU) { + if (--gKeyLockCountdown == 0) { gEeprom.KEY_LOCK = true; // lock the keyboard - gUpdateStatus = true; // lock symbol needs showing + gUpdateStatus = true; // lock symbol needs showing + } + } if (exit_menu) @@ -1485,25 +1486,25 @@ void APP_TimeSlice500ms(void) gUpdateStatus = true; gUpdateDisplay = true; - { - GUI_DisplayType_t disp = DISPLAY_INVALID; + GUI_DisplayType_t disp = DISPLAY_INVALID; - #ifdef ENABLE_FMRADIO - if (gFmRadioMode && ! FUNCTION_IsRx()) { - disp = DISPLAY_FM; - } - #endif - - if (disp == DISPLAY_INVALID) - { -#ifndef ENABLE_NO_CODE_SCAN_TIMEOUT - if (!SCANNER_IsScanning()) +#ifdef ENABLE_FMRADIO + if (gFmRadioMode && ! FUNCTION_IsRx()) { + disp = DISPLAY_FM; + } #endif - disp = DISPLAY_MAIN; - } - if (disp != DISPLAY_INVALID) - GUI_SelectNextDisplay(disp); + if (disp == DISPLAY_INVALID) { +#ifndef ENABLE_NO_CODE_SCAN_TIMEOUT + if (!SCANNER_IsScanning()) +#endif + { + disp = DISPLAY_MAIN; + } + } + + if (disp != DISPLAY_INVALID) { + GUI_SelectNextDisplay(disp); } } } @@ -1520,7 +1521,7 @@ void APP_TimeSlice500ms(void) FM_Start(); GUI_SelectNextDisplay(DISPLAY_FM); } -#endif +#endif } } @@ -1651,7 +1652,7 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) !(Key == KEY_PTT && !(gSetting_backlight_on_tx_rx & BACKLIGHT_ON_TR_TX)) // not in the backlight menu && !(gScreenToDisplay == DISPLAY_MENU && ( m == MENU_ABR || m == MENU_ABR_MAX || m == MENU_ABR_MIN)) - ) + ) { BACKLIGHT_TurnOn(); } @@ -1669,7 +1670,7 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) // cancel user input cancelUserInputModes(); - + if (gMonitor) ACTION_Monitor(); //turn off the monitor #ifdef ENABLE_SCAN_RANGES @@ -1707,7 +1708,7 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) gUpdateDisplay = true; AUDIO_PlayBeep(BEEP_1KHZ_60MS_OPTIONAL); return; - } + } if (Key == KEY_F) { // function/key-lock key @@ -1781,100 +1782,98 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) gUpdateStatus = true; } - if (!bFlag) - { - if (gCurrentFunction == FUNCTION_TRANSMIT) - { // transmitting + if (bFlag) { + goto Skip; + } + if (gCurrentFunction == FUNCTION_TRANSMIT) { #if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) - if (gAlarmState == ALARM_STATE_OFF) -#endif - { - char Code; - - if (Key == KEY_PTT) - { - GENERIC_Key_PTT(bKeyPressed); - goto Skip; - } - - if (Key == KEY_SIDE2) - { // transmit 1750Hz tone - Code = 0xFE; - } - else - { - Code = DTMF_GetCharacter(Key - KEY_0); - if (Code == 0xFF) - goto Skip; - - // transmit DTMF keys - } - - if (!bKeyPressed || bKeyHeld) - { - if (!bKeyPressed) - { - AUDIO_AudioPathOff(); - - gEnableSpeaker = false; - - BK4819_ExitDTMF_TX(false); - - if (gCurrentVfo->SCRAMBLING_TYPE == 0 || !gSetting_ScrambleEnable) - BK4819_DisableScramble(); - else - BK4819_EnableScramble(gCurrentVfo->SCRAMBLING_TYPE - 1); - } - } - else - { - if (gEeprom.DTMF_SIDE_TONE) - { // user will here the DTMF tones in speaker - AUDIO_AudioPathOn(); - gEnableSpeaker = true; - } - - BK4819_DisableScramble(); - - if (Code == 0xFE) - BK4819_TransmitTone(gEeprom.DTMF_SIDE_TONE, 1750); - else - BK4819_PlayDTMFEx(gEeprom.DTMF_SIDE_TONE, Code); - } - } -#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) - else if ((!bKeyHeld && bKeyPressed) || (gAlarmState == ALARM_STATE_TX1750 && bKeyHeld && !bKeyPressed)) - { - ALARM_Off(); - - if (gEeprom.REPEATER_TAIL_TONE_ELIMINATION == 0) - FUNCTION_Select(FUNCTION_FOREGROUND); - else - gRTTECountdown = gEeprom.REPEATER_TAIL_TONE_ELIMINATION * 10; - - if (Key == KEY_PTT) - gPttWasPressed = true; - else - if (!bKeyHeld) - gPttWasReleased = true; - } -#endif - } - else if (Key != KEY_SIDE1 && Key != KEY_SIDE2 && gScreenToDisplay != DISPLAY_INVALID) { - ProcessKeysFunctions[gScreenToDisplay](Key, bKeyPressed, bKeyHeld); - } - else -#ifdef ENABLE_AIRCOPY - if (!SCANNER_IsScanning() && gScreenToDisplay != DISPLAY_AIRCOPY) -#else - if (!SCANNER_IsScanning()) + if (gAlarmState == ALARM_STATE_OFF) #endif { - ACTION_Handle(Key, bKeyPressed, bKeyHeld); + char Code; + + if (Key == KEY_PTT) + { + GENERIC_Key_PTT(bKeyPressed); + goto Skip; + } + + if (Key == KEY_SIDE2) + { // transmit 1750Hz tone + Code = 0xFE; + } + else + { + + Code = DTMF_GetCharacter(Key - KEY_0); + if (Code == 0xFF) + goto Skip; + + // transmit DTMF keys + } + + if (!bKeyPressed || bKeyHeld) + { + if (!bKeyPressed) + { + AUDIO_AudioPathOff(); + + gEnableSpeaker = false; + + BK4819_ExitDTMF_TX(false); + + if (gCurrentVfo->SCRAMBLING_TYPE == 0 || !gSetting_ScrambleEnable) + BK4819_DisableScramble(); + else + BK4819_EnableScramble(gCurrentVfo->SCRAMBLING_TYPE - 1); + } + } + else + { + if (gEeprom.DTMF_SIDE_TONE) + { // user will here the DTMF tones in speaker + AUDIO_AudioPathOn(); + gEnableSpeaker = true; + } + + BK4819_DisableScramble(); + + if (Code == 0xFE) + BK4819_TransmitTone(gEeprom.DTMF_SIDE_TONE, 1750); + else + BK4819_PlayDTMFEx(gEeprom.DTMF_SIDE_TONE, Code); + } } - else if (!bKeyHeld && bKeyPressed) - gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; +#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) + else if ((!bKeyHeld && bKeyPressed) || (gAlarmState == ALARM_STATE_TX1750 && bKeyHeld && !bKeyPressed)) { + ALARM_Off(); + + if (gEeprom.REPEATER_TAIL_TONE_ELIMINATION == 0) + FUNCTION_Select(FUNCTION_FOREGROUND); + else + gRTTECountdown = gEeprom.REPEATER_TAIL_TONE_ELIMINATION * 10; + + if (Key == KEY_PTT) + gPttWasPressed = true; + else + if (!bKeyHeld) + gPttWasReleased = true; + } +#endif + } + else if (Key != KEY_SIDE1 && Key != KEY_SIDE2 && gScreenToDisplay != DISPLAY_INVALID) { + ProcessKeysFunctions[gScreenToDisplay](Key, bKeyPressed, bKeyHeld); + } + else if (!SCANNER_IsScanning() +#ifdef ENABLE_AIRCOPY + && gScreenToDisplay != DISPLAY_AIRCOPY +#endif + ) { + ACTION_Handle(Key, bKeyPressed, bKeyHeld); + } + else if (!bKeyHeld && bKeyPressed) { + gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; } Skip: @@ -1904,24 +1903,24 @@ Skip: gUpdateStatus = true; } - #ifdef ENABLE_FMRADIO - if (gRequestSaveFM) - { - if (!bKeyHeld) - SETTINGS_SaveFM(); - else - gFlagSaveFM = true; - gRequestSaveFM = false; - } - #endif +#ifdef ENABLE_FMRADIO + if (gRequestSaveFM) + { + gRequestSaveFM = false; + if (!bKeyHeld) + SETTINGS_SaveFM(); + else + gFlagSaveFM = true; + } +#endif if (gRequestSaveVFO) { + gRequestSaveVFO = false; if (!bKeyHeld) SETTINGS_SaveVfoIndices(); else gFlagSaveVfo = true; - gRequestSaveVFO = false; } if (gRequestSaveChannel > 0) // TODO: remove the gRequestSaveChannel, why use global variable for that?? diff --git a/driver/bk4819.c b/driver/bk4819.c index bc01a7f..f7ac28a 100644 --- a/driver/bk4819.c +++ b/driver/bk4819.c @@ -16,13 +16,15 @@ #include // NULL -#include "audio.h" +#include "../audio.h" +#include "../bsp/dp32g030/gpio.h" +#include "../bsp/dp32g030/portcon.h" + #include "bk4819.h" -#include "bsp/dp32g030/gpio.h" -#include "bsp/dp32g030/portcon.h" -#include "driver/gpio.h" -#include "driver/system.h" -#include "driver/systick.h" +#include "gpio.h" +#include "system.h" +#include "systick.h" + #ifndef ARRAY_SIZE #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) @@ -127,7 +129,6 @@ static uint16_t BK4819_ReadU16(void) PORTCON_PORTC_IE = (PORTCON_PORTC_IE & ~PORTCON_PORTC_IE_C2_MASK) | PORTCON_PORTC_IE_C2_BITS_ENABLE; GPIOC->DIR = (GPIOC->DIR & ~GPIO_DIR_2_MASK) | GPIO_DIR_2_BITS_INPUT; SYSTICK_DelayUs(1); - Value = 0; for (i = 0; i < 16; i++) { diff --git a/driver/keyboard.c b/driver/keyboard.c index 62882df..0a39b94 100644 --- a/driver/keyboard.c +++ b/driver/keyboard.c @@ -117,19 +117,13 @@ KEY_Code_t KEYBOARD_Poll(void) GPIOA->DATA &= keyboard[j].set_to_zero_mask; // Read all 4 GPIO pins at once .. with de-noise, max of 8 sample loops - for (i = 0, k = 0, reg = 0; i < 3 && k < 8; i++, k++) - { - uint16_t reg2; - + for (i = 0, k = 0, reg = 0; i < 3 && k < 8; i++, k++) { SYSTICK_DelayUs(1); - - reg2 = GPIOA->DATA; - if (reg != reg2) - { // noise - reg = reg2; - i = 0; - } + uint16_t reg2 = GPIOA->DATA; + i *= reg == reg2; + reg = reg2; } + if (i < 3) break; // noise is too bad diff --git a/driver/system.c b/driver/system.c index abcfb06..813ddec 100644 --- a/driver/system.c +++ b/driver/system.c @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "bsp/dp32g030/pmu.h" -#include "bsp/dp32g030/syscon.h" -#include "driver/system.h" -#include "driver/systick.h" +#include "../bsp/dp32g030/pmu.h" +#include "../bsp/dp32g030/syscon.h" +#include "system.h" +#include "systick.h" void SYSTEM_DelayMs(uint32_t Delay) { diff --git a/driver/systick.c b/driver/systick.c index 5971d74..c7cbed3 100644 --- a/driver/systick.c +++ b/driver/systick.c @@ -15,8 +15,8 @@ */ #include "ARMCM0.h" -#include "driver/systick.h" -#include "misc.h" +#include "systick.h" +#include "../misc.h" // 0x20000324 static uint32_t gTickMultiplier; @@ -29,17 +29,21 @@ void SYSTICK_Init(void) void SYSTICK_DelayUs(uint32_t Delay) { - const uint32_t ticks = Delay * gTickMultiplier; - uint32_t i = 0; - uint32_t Start = SysTick->LOAD; - uint32_t Previous = SysTick->VAL; + const uint32_t ticks = Delay * gTickMultiplier; + uint32_t elapsed_ticks = 0; + uint32_t Start = SysTick->LOAD; + uint32_t Previous = SysTick->VAL; do { uint32_t Current; - uint32_t Delta; - while ((Current = SysTick->VAL) == Previous) {} - Delta = (Current < Previous) ? -Current : Start - Current; - i += Delta + Previous; - Previous = Current; - } while (i < ticks); -} + do { + Current = SysTick->VAL; + } while (Current == Previous); + + uint32_t Delta = ((Current < Previous) ? - Current : Start - Current); + + elapsed_ticks += Delta + Previous; + + Previous = Current; + } while (elapsed_ticks < ticks); +} diff --git a/main.c b/main.c index d35a32c..aa0366a 100644 --- a/main.c +++ b/main.c @@ -14,18 +14,26 @@ * limitations under the License. */ +#include #include #include // NULL #ifdef ENABLE_AM_FIX #include "am_fix.h" #endif + +#include "audio.h" +#include "board.h" +#include "misc.h" +#include "radio.h" +#include "settings.h" +#include "version.h" + #include "app/app.h" #include "app/dtmf.h" -#include "audio.h" #include "bsp/dp32g030/gpio.h" #include "bsp/dp32g030/syscon.h" -#include "board.h" + #include "driver/backlight.h" #include "driver/bk4819.h" #include "driver/gpio.h" @@ -34,16 +42,13 @@ #ifdef ENABLE_UART #include "driver/uart.h" #endif + #include "helper/battery.h" #include "helper/boot.h" -#include "misc.h" -#include "radio.h" -#include "settings.h" + #include "ui/lock.h" #include "ui/welcome.h" #include "ui/menu.h" -#include "version.h" - void _putchar(__attribute__((unused)) char c) { @@ -67,6 +72,7 @@ void Main(void) | SYSCON_DEV_CLK_GATE_AES_BITS_ENABLE | SYSCON_DEV_CLK_GATE_PWM_PLUS0_BITS_ENABLE; + SYSTICK_Init(); BOARD_Init(); @@ -102,9 +108,9 @@ void Main(void) BATTERY_GetReadings(false); - #ifdef ENABLE_AM_FIX - AM_fix_init(); - #endif +#ifdef ENABLE_AM_FIX + AM_fix_init(); +#endif const BOOT_Mode_t BootMode = BOOT_GetMode(); @@ -211,24 +217,18 @@ void Main(void) #ifdef ENABLE_NOAA RADIO_ConfigureNOAA(); #endif - - // ****************** } - while (1) - { + while (true) { APP_Update(); - if (gNextTimeslice) - { - APP_TimeSlice10ms(); - gNextTimeslice = false; - } + if (gNextTimeslice) { - if (gNextTimeslice_500ms) - { - APP_TimeSlice500ms(); - gNextTimeslice_500ms = false; + APP_TimeSlice10ms(); + + if (gNextTimeslice_500ms) { + APP_TimeSlice500ms(); + } } } }