main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <stdio.h> // NULL
  19. #ifdef ENABLE_MESSENGER
  20. #include "app/messenger.h"
  21. #endif
  22. #include "audio.h"
  23. #include "board.h"
  24. #include "misc.h"
  25. #include "radio.h"
  26. #include "settings.h"
  27. #include "version.h"
  28. #include "app/app.h"
  29. #include "app/dtmf.h"
  30. #include "bsp/dp32g030/gpio.h"
  31. #include "bsp/dp32g030/syscon.h"
  32. #include "driver/backlight.h"
  33. #include "driver/bk4819.h"
  34. #include "driver/gpio.h"
  35. #include "driver/system.h"
  36. #include "driver/systick.h"
  37. #ifdef ENABLE_UART
  38. #include "driver/uart.h"
  39. #endif
  40. #include "helper/battery.h"
  41. #include "helper/boot.h"
  42. #include "ui/lock.h"
  43. #include "ui/welcome.h"
  44. #include "ui/menu.h"
  45. void _putchar(__attribute__((unused)) char c)
  46. {
  47. #ifdef ENABLE_UART
  48. UART_Send((uint8_t *)&c, 1);
  49. #endif
  50. }
  51. void Main(void)
  52. {
  53. // Enable clock gating of blocks we need
  54. SYSCON_DEV_CLK_GATE = 0
  55. | SYSCON_DEV_CLK_GATE_GPIOA_BITS_ENABLE
  56. | SYSCON_DEV_CLK_GATE_GPIOB_BITS_ENABLE
  57. | SYSCON_DEV_CLK_GATE_GPIOC_BITS_ENABLE
  58. | SYSCON_DEV_CLK_GATE_UART1_BITS_ENABLE
  59. | SYSCON_DEV_CLK_GATE_SPI0_BITS_ENABLE
  60. | SYSCON_DEV_CLK_GATE_SARADC_BITS_ENABLE
  61. | SYSCON_DEV_CLK_GATE_CRC_BITS_ENABLE
  62. | SYSCON_DEV_CLK_GATE_AES_BITS_ENABLE
  63. | SYSCON_DEV_CLK_GATE_PWM_PLUS0_BITS_ENABLE;
  64. SYSTICK_Init();
  65. BOARD_Init();
  66. boot_counter_10ms = 250; // 2.5 sec
  67. #ifdef ENABLE_UART
  68. UART_Init();
  69. UART_Send(UART_Version, strlen(UART_Version));
  70. #endif
  71. // Not implementing authentic device checks
  72. memset(gDTMF_String, '-', sizeof(gDTMF_String));
  73. gDTMF_String[sizeof(gDTMF_String) - 1] = 0;
  74. BK4819_Init();
  75. BOARD_ADC_GetBatteryInfo(&gBatteryCurrentVoltage, &gBatteryCurrent);
  76. SETTINGS_InitEEPROM();
  77. #ifdef ENABLE_CONTRAST
  78. ST7565_SetContrast(gEeprom.LCD_CONTRAST);
  79. #endif
  80. SETTINGS_WriteBuildOptions();
  81. SETTINGS_LoadCalibration();
  82. RADIO_ConfigureChannel(0, VFO_CONFIGURE_RELOAD);
  83. RADIO_ConfigureChannel(1, VFO_CONFIGURE_RELOAD);
  84. RADIO_SelectVfos();
  85. RADIO_SetupRegisters(true);
  86. for (unsigned int i = 0; i < ARRAY_SIZE(gBatteryVoltages); i++)
  87. BOARD_ADC_GetBatteryInfo(&gBatteryVoltages[i], &gBatteryCurrent);
  88. BATTERY_GetReadings(false);
  89. #ifdef ENABLE_MESSENGER
  90. MSG_Init();
  91. #endif
  92. const BOOT_Mode_t BootMode = BOOT_GetMode();
  93. if (BootMode == BOOT_MODE_F_LOCK)
  94. {
  95. gF_LOCK = true; // flag to say include the hidden menu items
  96. }
  97. // count the number of menu items
  98. gMenuListCount = 0;
  99. while (MenuList[gMenuListCount].name[0] != '\0') {
  100. if(!gF_LOCK && MenuList[gMenuListCount].menu_id == FIRST_HIDDEN_MENU_ITEM)
  101. break;
  102. gMenuListCount++;
  103. }
  104. // wait for user to release all butts before moving on
  105. if (!GPIO_CheckBit(&GPIOC->DATA, GPIOC_PIN_PTT) ||
  106. KEYBOARD_Poll() != KEY_INVALID ||
  107. #ifdef ENABLE_PMR_MODE
  108. (BootMode != BOOT_MODE_NORMAL && BootMode != BOOT_MODE_PMR)
  109. #else
  110. BootMode != BOOT_MODE_NORMAL
  111. #endif
  112. )
  113. { // keys are pressed
  114. UI_DisplayReleaseKeys();
  115. BACKLIGHT_TurnOn();
  116. // 500ms
  117. for (int i = 0; i < 50;)
  118. {
  119. i = (GPIO_CheckBit(&GPIOC->DATA, GPIOC_PIN_PTT) && KEYBOARD_Poll() == KEY_INVALID) ? i + 1 : 0;
  120. SYSTEM_DelayMs(10);
  121. }
  122. gKeyReading0 = KEY_INVALID;
  123. gKeyReading1 = KEY_INVALID;
  124. gDebounceCounter = 0;
  125. }
  126. if (!gChargingWithTypeC && gBatteryDisplayLevel == 0)
  127. {
  128. FUNCTION_Select(FUNCTION_POWER_SAVE);
  129. if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) // backlight is not set to be always on
  130. BACKLIGHT_TurnOff(); // turn the backlight OFF
  131. else
  132. BACKLIGHT_TurnOn(); // turn the backlight ON
  133. gReducedService = true;
  134. }
  135. else
  136. {
  137. UI_DisplayWelcome();
  138. BACKLIGHT_TurnOn();
  139. if (gEeprom.POWER_ON_DISPLAY_MODE != POWER_ON_DISPLAY_MODE_NONE)
  140. { // 2.55 second boot-up screen
  141. while (boot_counter_10ms > 0)
  142. {
  143. if (KEYBOARD_Poll() != KEY_INVALID)
  144. { // halt boot beeps
  145. boot_counter_10ms = 0;
  146. break;
  147. }
  148. #ifdef ENABLE_BOOT_BEEPS
  149. if ((boot_counter_10ms % 25) == 0)
  150. AUDIO_PlayBeep(BEEP_880HZ_40MS_OPTIONAL);
  151. #endif
  152. }
  153. }
  154. #ifdef ENABLE_PWRON_PASSWORD
  155. if (gEeprom.POWER_ON_PASSWORD < 1000000)
  156. {
  157. bIsInLockScreen = true;
  158. UI_DisplayLock();
  159. bIsInLockScreen = false;
  160. }
  161. #endif
  162. BOOT_ProcessMode(BootMode);
  163. GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_VOICE_0);
  164. gUpdateStatus = true;
  165. #ifdef ENABLE_VOICE
  166. {
  167. uint8_t Channel;
  168. AUDIO_SetVoiceID(0, VOICE_ID_WELCOME);
  169. Channel = gEeprom.ScreenChannel[gEeprom.TX_VFO];
  170. if (IS_MR_CHANNEL(Channel))
  171. {
  172. AUDIO_SetVoiceID(1, VOICE_ID_CHANNEL_MODE);
  173. AUDIO_SetDigitVoice(2, Channel + 1);
  174. }
  175. else if (IS_FREQ_CHANNEL(Channel))
  176. AUDIO_SetVoiceID(1, VOICE_ID_FREQUENCY_MODE);
  177. AUDIO_PlaySingleVoice(0);
  178. }
  179. #endif
  180. #ifdef ENABLE_NOAA
  181. RADIO_ConfigureNOAA();
  182. #endif
  183. }
  184. while (true) {
  185. APP_Update();
  186. if (gNextTimeslice) {
  187. APP_TimeSlice10ms();
  188. if (gNextTimeslice_500ms) {
  189. APP_TimeSlice500ms();
  190. }
  191. }
  192. }
  193. }