flipperzero-firmware_official_dev 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. commit 37fb330b362dba3b01b7c6ca6a200564211dc1b7
  2. Author: Georgii Surkov <37121527+gsurkov@users.noreply.github.com>
  3. Date: Thu Apr 13 17:47:38 2023 +0300
  4. [FL-3226] Deep Sleep Idle (#2569)
  5. * Improve RNG error handling
  6. * Sync RTC shadow registers on Stop mode exit
  7. * Implement working STOP2 mode
  8. * Fix formatting
  9. * FuriHal: disable SWD pins if debug is disabled
  10. * Power: cleanup battery info view, handle zero current report from gauge
  11. * Fbt: add command line argument for extra global defines
  12. * FuriHal: cleanup debug defines in power and os, drop deep_insomnia counter.
  13. * Add a setting to disable deep sleep
  14. * Clean up furi_hal_power
  15. * FuriHal,FapLoader,Debug: implement debug in stop mode, workaround resume in stop
  16. * FuriHal: document OS and power subsystems debugging
  17. * Furi: enable debug interface on crash
  18. ---------
  19. Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
  20. diff --git a/applications/main/fap_loader/fap_loader_app.c b/applications/main/fap_loader/fap_loader_app.c
  21. index f5c7af024..7af5244ae 100644
  22. --- a/applications/main/fap_loader/fap_loader_app.c
  23. +++ b/applications/main/fap_loader/fap_loader_app.c
  24. @@ -1,6 +1,7 @@
  25. #include "fap_loader_app.h"
  26. #include <furi.h>
  27. +#include <furi_hal_debug.h>
  28. #include <assets_icons.h>
  29. #include <gui/gui.h>
  30. @@ -23,8 +24,6 @@ struct FapLoader {
  31. Loading* loading;
  32. };
  33. -volatile bool fap_loader_debug_active = false;
  34. -
  35. bool fap_loader_load_name_and_icon(
  36. FuriString* path,
  37. Storage* storage,
  38. @@ -111,7 +110,7 @@ static bool fap_loader_run_selected_app(FapLoader* loader) {
  39. FuriThread* thread = flipper_application_spawn(loader->app, NULL);
  40. /* This flag is set by the debugger - to break on app start */
  41. - if(fap_loader_debug_active) {
  42. + if(furi_hal_debug_is_gdb_session_active()) {
  43. FURI_LOG_W(TAG, "Triggering BP for debugger");
  44. /* After hitting this, you can set breakpoints in your .fap's code
  45. * Note that you have to toggle breakpoints that were set before */
  46. diff --git a/applications/settings/power_settings_app/views/battery_info.c b/applications/settings/power_settings_app/views/battery_info.c
  47. index 7394fd3c5..0956cae4f 100644
  48. --- a/applications/settings/power_settings_app/views/battery_info.c
  49. +++ b/applications/settings/power_settings_app/views/battery_info.c
  50. @@ -4,8 +4,8 @@
  51. #include <assets_icons.h>
  52. #include <locale/locale.h>
  53. -#define LOW_CHARGE_THRESHOLD 10
  54. -#define HIGH_DRAIN_CURRENT_THRESHOLD 100
  55. +#define LOW_CHARGE_THRESHOLD (10)
  56. +#define HIGH_DRAIN_CURRENT_THRESHOLD (-100)
  57. struct BatteryInfo {
  58. View* view;
  59. @@ -25,14 +25,13 @@ static void draw_battery(Canvas* canvas, BatteryInfoModel* data, int x, int y) {
  60. char header[20] = {};
  61. char value[20] = {};
  62. - int32_t drain_current = data->gauge_current * (-1000);
  63. - uint32_t charge_current = data->gauge_current * 1000;
  64. + int32_t current = 1000.0f * data->gauge_current;
  65. // Draw battery
  66. canvas_draw_icon(canvas, x, y, &I_BatteryBody_52x28);
  67. - if(charge_current > 0) {
  68. + if(current > 0) {
  69. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceCharging_29x14);
  70. - } else if(drain_current > HIGH_DRAIN_CURRENT_THRESHOLD) {
  71. + } else if(current < HIGH_DRAIN_CURRENT_THRESHOLD) {
  72. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceConfused_29x14);
  73. } else if(data->charge < LOW_CHARGE_THRESHOLD) {
  74. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceNopower_29x14);
  75. @@ -44,7 +43,7 @@ static void draw_battery(Canvas* canvas, BatteryInfoModel* data, int x, int y) {
  76. elements_bubble(canvas, 53, 0, 71, 39);
  77. // Set text
  78. - if(charge_current > 0) {
  79. + if(current > 0) {
  80. snprintf(emote, sizeof(emote), "%s", "Yummy!");
  81. snprintf(header, sizeof(header), "%s", "Charging at");
  82. snprintf(
  83. @@ -53,34 +52,36 @@ static void draw_battery(Canvas* canvas, BatteryInfoModel* data, int x, int y) {
  84. "%lu.%luV %lumA",
  85. (uint32_t)(data->vbus_voltage),
  86. (uint32_t)(data->vbus_voltage * 10) % 10,
  87. - charge_current);
  88. - } else if(drain_current > 0) {
  89. + current);
  90. + } else if(current < 0) {
  91. snprintf(
  92. emote,
  93. sizeof(emote),
  94. "%s",
  95. - drain_current > HIGH_DRAIN_CURRENT_THRESHOLD ? "Oh no!" : "Om-nom-nom!");
  96. + current < HIGH_DRAIN_CURRENT_THRESHOLD ? "Oh no!" : "Om-nom-nom!");
  97. snprintf(header, sizeof(header), "%s", "Consumption is");
  98. snprintf(
  99. value,
  100. sizeof(value),
  101. "%ld %s",
  102. - drain_current,
  103. - drain_current > HIGH_DRAIN_CURRENT_THRESHOLD ? "mA!" : "mA");
  104. - } else if(drain_current != 0) {
  105. - snprintf(header, 20, "...");
  106. - } else if(data->charge_voltage_limit < 4.2) {
  107. - // Non-default battery charging limit, mention it
  108. - snprintf(emote, sizeof(emote), "Charged!");
  109. - snprintf(header, sizeof(header), "Limited to");
  110. - snprintf(
  111. - value,
  112. - sizeof(value),
  113. - "%lu.%luV",
  114. - (uint32_t)(data->charge_voltage_limit),
  115. - (uint32_t)(data->charge_voltage_limit * 10) % 10);
  116. + ABS(current),
  117. + current < HIGH_DRAIN_CURRENT_THRESHOLD ? "mA!" : "mA");
  118. + } else if(data->vbus_voltage > 0) {
  119. + if(data->charge_voltage_limit < 4.2) {
  120. + // Non-default battery charging limit, mention it
  121. + snprintf(emote, sizeof(emote), "Charged!");
  122. + snprintf(header, sizeof(header), "Limited to");
  123. + snprintf(
  124. + value,
  125. + sizeof(value),
  126. + "%lu.%luV",
  127. + (uint32_t)(data->charge_voltage_limit),
  128. + (uint32_t)(data->charge_voltage_limit * 10) % 10);
  129. + } else {
  130. + snprintf(header, sizeof(header), "Charged!");
  131. + }
  132. } else {
  133. - snprintf(header, sizeof(header), "Charged!");
  134. + snprintf(header, sizeof(header), "Napping...");
  135. }
  136. canvas_draw_str_aligned(canvas, 92, y + 3, AlignCenter, AlignCenter, emote);
  137. diff --git a/applications/settings/system/system_settings.c b/applications/settings/system/system_settings.c
  138. index cb74d7a83..597710a53 100644
  139. --- a/applications/settings/system/system_settings.c
  140. +++ b/applications/settings/system/system_settings.c
  141. @@ -141,6 +141,21 @@ static void hand_orient_changed(VariableItem* item) {
  142. loader_update_menu();
  143. }
  144. +const char* const sleep_method[] = {
  145. + "Default",
  146. + "Legacy",
  147. +};
  148. +
  149. +static void sleep_method_changed(VariableItem* item) {
  150. + uint8_t index = variable_item_get_current_value_index(item);
  151. + variable_item_set_current_value_text(item, sleep_method[index]);
  152. + if(index) {
  153. + furi_hal_rtc_set_flag(FuriHalRtcFlagLegacySleep);
  154. + } else {
  155. + furi_hal_rtc_reset_flag(FuriHalRtcFlagLegacySleep);
  156. + }
  157. +}
  158. +
  159. static uint32_t system_settings_exit(void* context) {
  160. UNUSED(context);
  161. return VIEW_NONE;
  162. @@ -218,6 +233,12 @@ SystemSettings* system_settings_alloc() {
  163. variable_item_set_current_value_index(item, value_index);
  164. variable_item_set_current_value_text(item, heap_trace_mode_text[value_index]);
  165. + item = variable_item_list_add(
  166. + app->var_item_list, "Sleep Method", COUNT_OF(sleep_method), sleep_method_changed, app);
  167. + value_index = furi_hal_rtc_is_flag_set(FuriHalRtcFlagLegacySleep) ? 1 : 0;
  168. + variable_item_set_current_value_index(item, value_index);
  169. + variable_item_set_current_value_text(item, sleep_method[value_index]);
  170. +
  171. view_set_previous_callback(
  172. variable_item_list_get_view(app->var_item_list), system_settings_exit);
  173. view_dispatcher_add_view(
  174. diff --git a/debug/flipperapps.py b/debug/flipperapps.py
  175. index 1dc5ebd04..90582c1e4 100644
  176. --- a/debug/flipperapps.py
  177. +++ b/debug/flipperapps.py
  178. @@ -135,6 +135,7 @@ class FlipperAppStateHelper:
  179. self.app_list_ptr = None
  180. self.app_list_entry_type = None
  181. self._current_apps: list[AppState] = []
  182. + self.set_debug_mode(True)
  183. def _walk_app_list(self, list_head):
  184. while list_head:
  185. @@ -195,7 +196,7 @@ class FlipperAppStateHelper:
  186. self.set_debug_mode(False)
  187. def set_debug_mode(self, mode: bool) -> None:
  188. - gdb.execute(f"set variable fap_loader_debug_active = {int(mode)}")
  189. + gdb.execute(f"set variable furi_hal_debug_gdb_session_active = {int(mode)}")
  190. # Init additional 'fap-set-debug-elf-root' command and set up hooks
  191. diff --git a/documentation/FuriHalDebuging.md b/documentation/FuriHalDebuging.md
  192. new file mode 100644
  193. index 000000000..8ff770163
  194. --- /dev/null
  195. +++ b/documentation/FuriHalDebuging.md
  196. @@ -0,0 +1,26 @@
  197. +# Furi HAL Debugging
  198. +
  199. +Some Furi subsystem got additional debugging features that can be enabled by adding additional defines to firmware compilation.
  200. +Usually they are used for low level tracing and profiling or signal redirection/duplication.
  201. +
  202. +
  203. +## FuriHalOs
  204. +
  205. +`--extra-define=FURI_HAL_OS_DEBUG` enables tick, tick suppression, idle and time flow.
  206. +
  207. +There are 3 signals that will be exposed to external GPIO pins:
  208. +
  209. +- `AWAKE` - `PA7` - High when system is busy with computations, low when sleeping. Can be used to track transitions to sleep mode.
  210. +- `TICK` - `PA6` - Flipped on system tick, only flips when no tick suppression in progress. Can be used to track tick skew and abnormal task scheduling.
  211. +- `SECOND` - `PA4` - Flipped each second. Can be used for tracing RT issue: time flow disturbance means system doesn't conforms Hard RT.
  212. +
  213. +
  214. +
  215. +## FuriHalPower
  216. +
  217. +`--extra-define=FURI_HAL_POWER_DEBUG` enables power subsystem mode transitions tracing.
  218. +
  219. +There are 2 signals that will be exposed to external GPIO pins:
  220. +
  221. +- `WFI` - `PB2` - Light sleep (wait for interrupt) used. Basically this is lightest and most non-breaking things power save mode. All function and debug should work correctly in this mode.
  222. +- `STOP` - `PC3` - STOP mode used. Platform deep sleep mode. Extremely fragile mode where most of the silicon is disabled or in unusable state. Debugging MCU in this mode is nearly impossible.
  223. \ No newline at end of file
  224. diff --git a/documentation/fbt.md b/documentation/fbt.md
  225. index 14d63e9ce..23b2e2b55 100644
  226. --- a/documentation/fbt.md
  227. +++ b/documentation/fbt.md
  228. @@ -105,6 +105,7 @@ To run cleanup (think of `make clean`) for specified targets, add the `-c` optio
  229. - `--options optionfile.py` (default value `fbt_options.py`) - load a file with multiple configuration values
  230. - `--extra-int-apps=app1,app2,appN` - force listed apps to be built as internal with the `firmware` target
  231. - `--extra-ext-apps=app1,app2,appN` - force listed apps to be built as external with the `firmware_extapps` target
  232. +- `--extra-define=A --extra-define=B=C ` - extra global defines that will be passed to the C/C++ compiler, can be specified multiple times
  233. - `--proxy-env=VAR1,VAR2` - additional environment variables to expose to subprocesses spawned by `fbt`. By default, `fbt` sanitizes the execution environment and doesn't forward all inherited environment variables. You can find the list of variables that are always forwarded in the `environ.scons` file.
  234. ## Configuration
  235. diff --git a/firmware/targets/f18/api_symbols.csv b/firmware/targets/f18/api_symbols.csv
  236. index 5d5b7bbcf..493f59634 100644
  237. --- a/firmware/targets/f18/api_symbols.csv
  238. +++ b/firmware/targets/f18/api_symbols.csv
  239. @@ -1,5 +1,5 @@
  240. entry,status,name,type,params
  241. -Version,+,21.0,,
  242. +Version,+,22.0,,
  243. Header,+,applications/services/bt/bt_service/bt.h,,
  244. Header,+,applications/services/cli/cli.h,,
  245. Header,+,applications/services/cli/cli_vcp.h,,
  246. @@ -901,6 +901,7 @@ Function,+,furi_hal_crypto_verify_enclave,_Bool,"uint8_t*, uint8_t*"
  247. Function,+,furi_hal_crypto_verify_key,_Bool,uint8_t
  248. Function,+,furi_hal_debug_disable,void,
  249. Function,+,furi_hal_debug_enable,void,
  250. +Function,+,furi_hal_debug_is_gdb_session_active,_Bool,
  251. Function,-,furi_hal_deinit_early,void,
  252. Function,-,furi_hal_flash_erase,void,uint8_t
  253. Function,-,furi_hal_flash_get_base,size_t,
  254. @@ -983,7 +984,6 @@ Function,-,furi_hal_os_init,void,
  255. Function,+,furi_hal_os_tick,void,
  256. Function,+,furi_hal_power_check_otg_status,void,
  257. Function,+,furi_hal_power_debug_get,void,"PropertyValueCallback, void*"
  258. -Function,+,furi_hal_power_deep_sleep_available,_Bool,
  259. Function,+,furi_hal_power_disable_external_3_3v,void,
  260. Function,+,furi_hal_power_disable_otg,void,
  261. Function,+,furi_hal_power_enable_external_3_3v,void,
  262. @@ -1059,6 +1059,7 @@ Function,+,furi_hal_rtc_set_locale_units,void,FuriHalRtcLocaleUnits
  263. Function,+,furi_hal_rtc_set_log_level,void,uint8_t
  264. Function,+,furi_hal_rtc_set_pin_fails,void,uint32_t
  265. Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
  266. +Function,+,furi_hal_rtc_sync_shadow,void,
  267. Function,+,furi_hal_rtc_validate_datetime,_Bool,FuriHalRtcDateTime*
  268. Function,+,furi_hal_speaker_acquire,_Bool,uint32_t
  269. Function,-,furi_hal_speaker_deinit,void,
  270. @@ -2149,6 +2150,8 @@ Variable,+,gpio_ext_pd0,const GpioPin,
  271. Variable,+,gpio_ext_pe4,const GpioPin,
  272. Variable,+,gpio_i2c_power_scl,const GpioPin,
  273. Variable,+,gpio_i2c_power_sda,const GpioPin,
  274. +Variable,+,gpio_ibutton,const GpioPin,
  275. +Variable,+,gpio_periph_power,const GpioPin,
  276. Variable,+,gpio_pins,const GpioPinRecord[],
  277. Variable,+,gpio_pins_count,const size_t,
  278. Variable,+,gpio_sdcard_cd,const GpioPin,
  279. @@ -2157,11 +2160,13 @@ Variable,+,gpio_speaker,const GpioPin,
  280. Variable,+,gpio_spi_d_miso,const GpioPin,
  281. Variable,+,gpio_spi_d_mosi,const GpioPin,
  282. Variable,+,gpio_spi_d_sck,const GpioPin,
  283. +Variable,+,gpio_swclk,const GpioPin,
  284. +Variable,+,gpio_swdio,const GpioPin,
  285. Variable,+,gpio_usart_rx,const GpioPin,
  286. Variable,+,gpio_usart_tx,const GpioPin,
  287. Variable,+,gpio_usb_dm,const GpioPin,
  288. Variable,+,gpio_usb_dp,const GpioPin,
  289. -Variable,+,gpio_ibutton,const GpioPin,
  290. +Variable,+,gpio_vibro,const GpioPin,
  291. Variable,+,input_pins,const InputPin[],
  292. Variable,+,input_pins_count,const size_t,
  293. Variable,+,message_blink_set_color_blue,const NotificationMessage,
  294. @@ -2309,7 +2314,6 @@ Variable,+,message_red_255,const NotificationMessage,
  295. Variable,+,message_sound_off,const NotificationMessage,
  296. Variable,+,message_vibro_off,const NotificationMessage,
  297. Variable,+,message_vibro_on,const NotificationMessage,
  298. -Variable,+,gpio_periph_power,const GpioPin,
  299. Variable,+,sequence_audiovisual_alert,const NotificationSequence,
  300. Variable,+,sequence_blink_blue_10,const NotificationSequence,
  301. Variable,+,sequence_blink_blue_100,const NotificationSequence,
  302. @@ -2364,4 +2368,3 @@ Variable,+,usb_cdc_single,FuriHalUsbInterface,
  303. Variable,+,usb_hid,FuriHalUsbInterface,
  304. Variable,+,usb_hid_u2f,FuriHalUsbInterface,
  305. Variable,+,usbd_devfs,const usbd_driver,
  306. -Variable,+,gpio_vibro,const GpioPin,
  307. diff --git a/firmware/targets/f18/furi_hal/furi_hal_resources.c b/firmware/targets/f18/furi_hal/furi_hal_resources.c
  308. index 4a7015d13..6db483dbc 100644
  309. --- a/firmware/targets/f18/furi_hal/furi_hal_resources.c
  310. +++ b/firmware/targets/f18/furi_hal/furi_hal_resources.c
  311. @@ -6,6 +6,9 @@
  312. #define TAG "FuriHalResources"
  313. +const GpioPin gpio_swdio = {.port = GPIOA, .pin = LL_GPIO_PIN_13};
  314. +const GpioPin gpio_swclk = {.port = GPIOA, .pin = LL_GPIO_PIN_14};
  315. +
  316. const GpioPin gpio_vibro = {.port = GPIOA, .pin = LL_GPIO_PIN_8};
  317. const GpioPin gpio_ibutton = {.port = GPIOB, .pin = LL_GPIO_PIN_14};
  318. diff --git a/firmware/targets/f18/furi_hal/furi_hal_resources.h b/firmware/targets/f18/furi_hal/furi_hal_resources.h
  319. index 3c4708d15..7d2caab36 100644
  320. --- a/firmware/targets/f18/furi_hal/furi_hal_resources.h
  321. +++ b/firmware/targets/f18/furi_hal/furi_hal_resources.h
  322. @@ -50,6 +50,9 @@ extern const size_t input_pins_count;
  323. extern const GpioPinRecord gpio_pins[];
  324. extern const size_t gpio_pins_count;
  325. +extern const GpioPin gpio_swdio;
  326. +extern const GpioPin gpio_swclk;
  327. +
  328. extern const GpioPin gpio_vibro;
  329. extern const GpioPin gpio_ibutton;
  330. diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv
  331. index 89984352a..3ef57813b 100644
  332. --- a/firmware/targets/f7/api_symbols.csv
  333. +++ b/firmware/targets/f7/api_symbols.csv
  334. @@ -1,5 +1,5 @@
  335. entry,status,name,type,params
  336. -Version,+,21.0,,
  337. +Version,+,22.0,,
  338. Header,+,applications/services/bt/bt_service/bt.h,,
  339. Header,+,applications/services/cli/cli.h,,
  340. Header,+,applications/services/cli/cli_vcp.h,,
  341. @@ -1082,6 +1082,7 @@ Function,+,furi_hal_crypto_verify_enclave,_Bool,"uint8_t*, uint8_t*"
  342. Function,+,furi_hal_crypto_verify_key,_Bool,uint8_t
  343. Function,+,furi_hal_debug_disable,void,
  344. Function,+,furi_hal_debug_enable,void,
  345. +Function,+,furi_hal_debug_is_gdb_session_active,_Bool,
  346. Function,-,furi_hal_deinit_early,void,
  347. Function,-,furi_hal_flash_erase,void,uint8_t
  348. Function,-,furi_hal_flash_get_base,size_t,
  349. @@ -1212,7 +1213,6 @@ Function,-,furi_hal_os_init,void,
  350. Function,+,furi_hal_os_tick,void,
  351. Function,+,furi_hal_power_check_otg_status,void,
  352. Function,+,furi_hal_power_debug_get,void,"PropertyValueCallback, void*"
  353. -Function,+,furi_hal_power_deep_sleep_available,_Bool,
  354. Function,+,furi_hal_power_disable_external_3_3v,void,
  355. Function,+,furi_hal_power_disable_otg,void,
  356. Function,+,furi_hal_power_enable_external_3_3v,void,
  357. @@ -1313,6 +1313,7 @@ Function,+,furi_hal_rtc_set_locale_units,void,FuriHalRtcLocaleUnits
  358. Function,+,furi_hal_rtc_set_log_level,void,uint8_t
  359. Function,+,furi_hal_rtc_set_pin_fails,void,uint32_t
  360. Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
  361. +Function,+,furi_hal_rtc_sync_shadow,void,
  362. Function,+,furi_hal_rtc_validate_datetime,_Bool,FuriHalRtcDateTime*
  363. Function,+,furi_hal_speaker_acquire,_Bool,uint32_t
  364. Function,-,furi_hal_speaker_deinit,void,
  365. @@ -3076,10 +3077,12 @@ Variable,+,gpio_ext_pc1,const GpioPin,
  366. Variable,+,gpio_ext_pc3,const GpioPin,
  367. Variable,+,gpio_i2c_power_scl,const GpioPin,
  368. Variable,+,gpio_i2c_power_sda,const GpioPin,
  369. +Variable,+,gpio_ibutton,const GpioPin,
  370. Variable,+,gpio_infrared_rx,const GpioPin,
  371. Variable,+,gpio_infrared_tx,const GpioPin,
  372. Variable,+,gpio_nfc_cs,const GpioPin,
  373. Variable,+,gpio_nfc_irq_rfid_pull,const GpioPin,
  374. +Variable,+,gpio_periph_power,const GpioPin,
  375. Variable,+,gpio_pins,const GpioPinRecord[],
  376. Variable,+,gpio_pins_count,const size_t,
  377. Variable,+,gpio_rf_sw_0,const GpioPin,
  378. @@ -3096,11 +3099,13 @@ Variable,+,gpio_spi_r_miso,const GpioPin,
  379. Variable,+,gpio_spi_r_mosi,const GpioPin,
  380. Variable,+,gpio_spi_r_sck,const GpioPin,
  381. Variable,+,gpio_subghz_cs,const GpioPin,
  382. +Variable,+,gpio_swclk,const GpioPin,
  383. +Variable,+,gpio_swdio,const GpioPin,
  384. Variable,+,gpio_usart_rx,const GpioPin,
  385. Variable,+,gpio_usart_tx,const GpioPin,
  386. Variable,+,gpio_usb_dm,const GpioPin,
  387. Variable,+,gpio_usb_dp,const GpioPin,
  388. -Variable,+,gpio_ibutton,const GpioPin,
  389. +Variable,+,gpio_vibro,const GpioPin,
  390. Variable,+,input_pins,const InputPin[],
  391. Variable,+,input_pins_count,const size_t,
  392. Variable,+,lfrfid_protocols,const ProtocolBase*[],
  393. @@ -3249,7 +3254,6 @@ Variable,+,message_red_255,const NotificationMessage,
  394. Variable,+,message_sound_off,const NotificationMessage,
  395. Variable,+,message_vibro_off,const NotificationMessage,
  396. Variable,+,message_vibro_on,const NotificationMessage,
  397. -Variable,+,gpio_periph_power,const GpioPin,
  398. Variable,+,sequence_audiovisual_alert,const NotificationSequence,
  399. Variable,+,sequence_blink_blue_10,const NotificationSequence,
  400. Variable,+,sequence_blink_blue_100,const NotificationSequence,
  401. @@ -3307,4 +3311,3 @@ Variable,+,usb_cdc_single,FuriHalUsbInterface,
  402. Variable,+,usb_hid,FuriHalUsbInterface,
  403. Variable,+,usb_hid_u2f,FuriHalUsbInterface,
  404. Variable,+,usbd_devfs,const usbd_driver,
  405. -Variable,+,gpio_vibro,const GpioPin,
  406. diff --git a/firmware/targets/f7/ble_glue/ble_glue.c b/firmware/targets/f7/ble_glue/ble_glue.c
  407. index 83562c73e..a2f2f1a94 100644
  408. --- a/firmware/targets/f7/ble_glue/ble_glue.c
  409. +++ b/firmware/targets/f7/ble_glue/ble_glue.c
  410. @@ -58,12 +58,6 @@ void ble_glue_init() {
  411. ble_glue = malloc(sizeof(BleGlue));
  412. ble_glue->status = BleGlueStatusStartup;
  413. - // Configure the system Power Mode
  414. - // Select HSI as system clock source after Wake Up from Stop mode
  415. - LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  416. - /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  417. - LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  418. -
  419. #ifdef BLE_GLUE_DEBUG
  420. APPD_Init();
  421. #endif
  422. diff --git a/firmware/targets/f7/furi_hal/furi_hal_clock.c b/firmware/targets/f7/furi_hal/furi_hal_clock.c
  423. index cf19451ec..d85524ce4 100644
  424. --- a/firmware/targets/f7/furi_hal/furi_hal_clock.c
  425. +++ b/firmware/targets/f7/furi_hal/furi_hal_clock.c
  426. @@ -63,6 +63,10 @@ void furi_hal_clock_init() {
  427. LL_RCC_HSI_Enable();
  428. while(!HS_CLOCK_IS_READY())
  429. ;
  430. + /* Select HSI as system clock source after Wake Up from Stop mode
  431. + * Must be set before enabling CSS */
  432. + LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  433. +
  434. LL_RCC_HSE_EnableCSS();
  435. /* LSE and LSI1 configuration and activation */
  436. @@ -215,11 +219,14 @@ void furi_hal_clock_switch_to_hsi() {
  437. void furi_hal_clock_switch_to_pll() {
  438. LL_RCC_HSE_Enable();
  439. LL_RCC_PLL_Enable();
  440. + LL_RCC_PLLSAI1_Enable();
  441. while(!LL_RCC_HSE_IsReady())
  442. ;
  443. while(!LL_RCC_PLL_IsReady())
  444. ;
  445. + while(!LL_RCC_PLLSAI1_IsReady())
  446. + ;
  447. LL_FLASH_SetLatency(LL_FLASH_LATENCY_3);
  448. @@ -296,4 +303,4 @@ void furi_hal_clock_mco_disable() {
  449. LL_RCC_MSI_Disable();
  450. while(LL_RCC_MSI_IsReady() != 0)
  451. ;
  452. -}
  453. \ No newline at end of file
  454. +}
  455. diff --git a/firmware/targets/f7/furi_hal/furi_hal_debug.c b/firmware/targets/f7/furi_hal/furi_hal_debug.c
  456. index 3b5dfe622..3dc03ea69 100644
  457. --- a/firmware/targets/f7/furi_hal/furi_hal_debug.c
  458. +++ b/firmware/targets/f7/furi_hal/furi_hal_debug.c
  459. @@ -3,12 +3,26 @@
  460. #include <stm32wbxx_ll_exti.h>
  461. #include <stm32wbxx_ll_system.h>
  462. +#include <furi_hal_gpio.h>
  463. +#include <furi_hal_resources.h>
  464. +
  465. +volatile bool furi_hal_debug_gdb_session_active = false;
  466. +
  467. void furi_hal_debug_enable() {
  468. // Low power mode debug
  469. LL_DBGMCU_EnableDBGSleepMode();
  470. LL_DBGMCU_EnableDBGStopMode();
  471. LL_DBGMCU_EnableDBGStandbyMode();
  472. LL_EXTI_EnableIT_32_63(LL_EXTI_LINE_48);
  473. + // SWD GPIO
  474. + furi_hal_gpio_init_ex(
  475. + &gpio_swdio,
  476. + GpioModeAltFunctionPushPull,
  477. + GpioPullUp,
  478. + GpioSpeedVeryHigh,
  479. + GpioAltFn0JTMS_SWDIO);
  480. + furi_hal_gpio_init_ex(
  481. + &gpio_swclk, GpioModeAltFunctionPushPull, GpioPullDown, GpioSpeedLow, GpioAltFn0JTCK_SWCLK);
  482. }
  483. void furi_hal_debug_disable() {
  484. @@ -17,4 +31,11 @@ void furi_hal_debug_disable() {
  485. LL_DBGMCU_DisableDBGStopMode();
  486. LL_DBGMCU_DisableDBGStandbyMode();
  487. LL_EXTI_DisableIT_32_63(LL_EXTI_LINE_48);
  488. + // SWD GPIO
  489. + furi_hal_gpio_init_simple(&gpio_swdio, GpioModeAnalog);
  490. + furi_hal_gpio_init_simple(&gpio_swclk, GpioModeAnalog);
  491. }
  492. +
  493. +bool furi_hal_debug_is_gdb_session_active() {
  494. + return furi_hal_debug_gdb_session_active;
  495. +}
  496. \ No newline at end of file
  497. diff --git a/firmware/targets/f7/furi_hal/furi_hal_os.c b/firmware/targets/f7/furi_hal/furi_hal_os.c
  498. index ee9743e62..3fc1fbea8 100644
  499. --- a/firmware/targets/f7/furi_hal/furi_hal_os.c
  500. +++ b/firmware/targets/f7/furi_hal/furi_hal_os.c
  501. @@ -28,11 +28,24 @@
  502. // Arbitrary (but small) number for better tick consistency
  503. #define FURI_HAL_OS_EXTRA_CNT 3
  504. +#ifndef FURI_HAL_OS_DEBUG_AWAKE_GPIO
  505. +#define FURI_HAL_OS_DEBUG_AWAKE_GPIO (&gpio_ext_pa7)
  506. +#endif
  507. +
  508. +#ifndef FURI_HAL_OS_DEBUG_TICK_GPIO
  509. +#define FURI_HAL_OS_DEBUG_TICK_GPIO (&gpio_ext_pa6)
  510. +#endif
  511. +
  512. +#ifndef FURI_HAL_OS_DEBUG_SECOND_GPIO
  513. +#define FURI_HAL_OS_DEBUG_SECOND_GPIO (&gpio_ext_pa4)
  514. +#endif
  515. +
  516. #ifdef FURI_HAL_OS_DEBUG
  517. #include <stm32wbxx_ll_gpio.h>
  518. void furi_hal_os_timer_callback() {
  519. - furi_hal_gpio_write(&gpio_ext_pa4, !furi_hal_gpio_read(&gpio_ext_pa4));
  520. + furi_hal_gpio_write(
  521. + FURI_HAL_OS_DEBUG_SECOND_GPIO, !furi_hal_gpio_read(FURI_HAL_OS_DEBUG_SECOND_GPIO));
  522. }
  523. #endif
  524. @@ -44,9 +57,11 @@ void furi_hal_os_init() {
  525. furi_hal_idle_timer_init();
  526. #ifdef FURI_HAL_OS_DEBUG
  527. - furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeOutputPushPull);
  528. - furi_hal_gpio_init_simple(&gpio_ext_pa6, GpioModeOutputPushPull);
  529. - furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  530. + furi_hal_gpio_init_simple(FURI_HAL_OS_DEBUG_AWAKE_GPIO, GpioModeOutputPushPull);
  531. + furi_hal_gpio_init_simple(FURI_HAL_OS_DEBUG_TICK_GPIO, GpioModeOutputPushPull);
  532. + furi_hal_gpio_init_simple(FURI_HAL_OS_DEBUG_SECOND_GPIO, GpioModeOutputPushPull);
  533. + furi_hal_gpio_write(FURI_HAL_OS_DEBUG_AWAKE_GPIO, 1);
  534. +
  535. FuriTimer* second_timer =
  536. furi_timer_alloc(furi_hal_os_timer_callback, FuriTimerTypePeriodic, NULL);
  537. furi_timer_start(second_timer, FURI_HAL_OS_TICK_HZ);
  538. @@ -58,7 +73,8 @@ void furi_hal_os_init() {
  539. void furi_hal_os_tick() {
  540. if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  541. #ifdef FURI_HAL_OS_DEBUG
  542. - furi_hal_gpio_write(&gpio_ext_pa6, !furi_hal_gpio_read(&gpio_ext_pa6));
  543. + furi_hal_gpio_write(
  544. + FURI_HAL_OS_DEBUG_TICK_GPIO, !furi_hal_gpio_read(FURI_HAL_OS_DEBUG_TICK_GPIO));
  545. #endif
  546. xPortSysTickHandler();
  547. }
  548. @@ -121,14 +137,14 @@ static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
  549. furi_hal_idle_timer_start(FURI_HAL_OS_TICKS_TO_IDLE_CNT(expected_idle_ticks));
  550. #ifdef FURI_HAL_OS_DEBUG
  551. - furi_hal_gpio_write(&gpio_ext_pa7, 0);
  552. + furi_hal_gpio_write(FURI_HAL_OS_DEBUG_AWAKE_GPIO, 0);
  553. #endif
  554. // Go to sleep mode
  555. furi_hal_power_sleep();
  556. #ifdef FURI_HAL_OS_DEBUG
  557. - furi_hal_gpio_write(&gpio_ext_pa7, 1);
  558. + furi_hal_gpio_write(FURI_HAL_OS_DEBUG_AWAKE_GPIO, 1);
  559. #endif
  560. // Calculate how much time we spent in the sleep
  561. diff --git a/firmware/targets/f7/furi_hal/furi_hal_power.c b/firmware/targets/f7/furi_hal/furi_hal_power.c
  562. index fd601ec7e..9a87cef15 100644
  563. --- a/firmware/targets/f7/furi_hal/furi_hal_power.c
  564. +++ b/firmware/targets/f7/furi_hal/furi_hal_power.c
  565. @@ -4,6 +4,8 @@
  566. #include <furi_hal_vibro.h>
  567. #include <furi_hal_resources.h>
  568. #include <furi_hal_uart.h>
  569. +#include <furi_hal_rtc.h>
  570. +#include <furi_hal_debug.h>
  571. #include <stm32wbxx_ll_rcc.h>
  572. #include <stm32wbxx_ll_pwr.h>
  573. @@ -19,15 +21,16 @@
  574. #define TAG "FuriHalPower"
  575. -#ifdef FURI_HAL_POWER_DEEP_SLEEP_ENABLED
  576. -#define FURI_HAL_POWER_DEEP_INSOMNIA 0
  577. -#else
  578. -#define FURI_HAL_POWER_DEEP_INSOMNIA 1
  579. +#ifndef FURI_HAL_POWER_DEBUG_WFI_GPIO
  580. +#define FURI_HAL_POWER_DEBUG_WFI_GPIO (&gpio_ext_pb2)
  581. +#endif
  582. +
  583. +#ifndef FURI_HAL_POWER_DEBUG_STOP_GPIO
  584. +#define FURI_HAL_POWER_DEBUG_STOP_GPIO (&gpio_ext_pc3)
  585. #endif
  586. typedef struct {
  587. volatile uint8_t insomnia;
  588. - volatile uint8_t deep_insomnia;
  589. volatile uint8_t suppress_charge;
  590. uint8_t gauge_initialized;
  591. @@ -36,7 +39,6 @@ typedef struct {
  592. static volatile FuriHalPower furi_hal_power = {
  593. .insomnia = 0,
  594. - .deep_insomnia = FURI_HAL_POWER_DEEP_INSOMNIA,
  595. .suppress_charge = 0,
  596. };
  597. @@ -79,19 +81,23 @@ const ParamCEDV cedv = {
  598. };
  599. void furi_hal_power_init() {
  600. +#ifdef FURI_HAL_POWER_DEBUG
  601. + furi_hal_gpio_init_simple(FURI_HAL_POWER_DEBUG_WFI_GPIO, GpioModeOutputPushPull);
  602. + furi_hal_gpio_init_simple(FURI_HAL_POWER_DEBUG_STOP_GPIO, GpioModeOutputPushPull);
  603. + furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 0);
  604. + furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 0);
  605. +#endif
  606. +
  607. LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
  608. LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
  609. + LL_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
  610. + LL_C2_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
  611. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  612. bq27220_init(&furi_hal_i2c_handle_power, &cedv);
  613. bq25896_init(&furi_hal_i2c_handle_power);
  614. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  615. -#ifdef FURI_HAL_OS_DEBUG
  616. - furi_hal_gpio_init_simple(&gpio_ext_pb2, GpioModeOutputPushPull);
  617. - furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
  618. -#endif
  619. -
  620. FURI_LOG_I(TAG, "Init OK");
  621. }
  622. @@ -140,11 +146,12 @@ bool furi_hal_power_sleep_available() {
  623. return furi_hal_power.insomnia == 0;
  624. }
  625. -bool furi_hal_power_deep_sleep_available() {
  626. - return furi_hal_bt_is_alive() && furi_hal_power.deep_insomnia == 0;
  627. +static inline bool furi_hal_power_deep_sleep_available() {
  628. + return furi_hal_bt_is_alive() && !furi_hal_rtc_is_flag_set(FuriHalRtcFlagLegacySleep) &&
  629. + !furi_hal_debug_is_gdb_session_active();
  630. }
  631. -void furi_hal_power_light_sleep() {
  632. +static inline void furi_hal_power_light_sleep() {
  633. __WFI();
  634. }
  635. @@ -152,17 +159,15 @@ static inline void furi_hal_power_suspend_aux_periphs() {
  636. // Disable USART
  637. furi_hal_uart_suspend(FuriHalUartIdUSART1);
  638. furi_hal_uart_suspend(FuriHalUartIdLPUART1);
  639. - // TODO: Disable USB
  640. }
  641. static inline void furi_hal_power_resume_aux_periphs() {
  642. // Re-enable USART
  643. furi_hal_uart_resume(FuriHalUartIdUSART1);
  644. furi_hal_uart_resume(FuriHalUartIdLPUART1);
  645. - // TODO: Re-enable USB
  646. }
  647. -void furi_hal_power_deep_sleep() {
  648. +static inline void furi_hal_power_deep_sleep() {
  649. furi_hal_power_suspend_aux_periphs();
  650. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  651. @@ -187,8 +192,6 @@ void furi_hal_power_deep_sleep() {
  652. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  653. // Prepare deep sleep
  654. - LL_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
  655. - LL_C2_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
  656. LL_LPM_EnableDeepSleep();
  657. #if defined(__CC_ARM)
  658. @@ -200,13 +203,6 @@ void furi_hal_power_deep_sleep() {
  659. LL_LPM_EnableSleep();
  660. - // Make sure that values differ to prevent disaster on wfi
  661. - LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);
  662. - LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  663. -
  664. - LL_PWR_ClearFlag_C1STOP_C1STB();
  665. - LL_PWR_ClearFlag_C2STOP_C2STB();
  666. -
  667. /* Release ENTRY_STOP_MODE semaphore */
  668. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  669. @@ -220,28 +216,25 @@ void furi_hal_power_deep_sleep() {
  670. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  671. furi_hal_power_resume_aux_periphs();
  672. + furi_hal_rtc_sync_shadow();
  673. }
  674. void furi_hal_power_sleep() {
  675. if(furi_hal_power_deep_sleep_available()) {
  676. -#ifdef FURI_HAL_OS_DEBUG
  677. - furi_hal_gpio_write(&gpio_ext_pc3, 1);
  678. +#ifdef FURI_HAL_POWER_DEBUG
  679. + furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 1);
  680. #endif
  681. -
  682. furi_hal_power_deep_sleep();
  683. -
  684. -#ifdef FURI_HAL_OS_DEBUG
  685. - furi_hal_gpio_write(&gpio_ext_pc3, 0);
  686. +#ifdef FURI_HAL_POWER_DEBUG
  687. + furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 0);
  688. #endif
  689. } else {
  690. -#ifdef FURI_HAL_OS_DEBUG
  691. - furi_hal_gpio_write(&gpio_ext_pb2, 1);
  692. +#ifdef FURI_HAL_POWER_DEBUG
  693. + furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 1);
  694. #endif
  695. -
  696. furi_hal_power_light_sleep();
  697. -
  698. -#ifdef FURI_HAL_OS_DEBUG
  699. - furi_hal_gpio_write(&gpio_ext_pb2, 0);
  700. +#ifdef FURI_HAL_POWER_DEBUG
  701. + furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 0);
  702. #endif
  703. }
  704. }
  705. diff --git a/firmware/targets/f7/furi_hal/furi_hal_random.c b/firmware/targets/f7/furi_hal/furi_hal_random.c
  706. index f36407cc1..d3461c4d1 100644
  707. --- a/firmware/targets/f7/furi_hal/furi_hal_random.c
  708. +++ b/firmware/targets/f7/furi_hal/furi_hal_random.c
  709. @@ -9,19 +9,35 @@
  710. #define TAG "FuriHalRandom"
  711. +static uint32_t furi_hal_random_read_rng() {
  712. + while(LL_RNG_IsActiveFlag_CECS(RNG) && LL_RNG_IsActiveFlag_SECS(RNG) &&
  713. + !LL_RNG_IsActiveFlag_DRDY(RNG)) {
  714. + /* Error handling as described in RM0434, pg. 582-583 */
  715. + if(LL_RNG_IsActiveFlag_CECS(RNG)) {
  716. + /* Clock error occurred */
  717. + LL_RNG_ClearFlag_CEIS(RNG);
  718. + }
  719. +
  720. + if(LL_RNG_IsActiveFlag_SECS(RNG)) {
  721. + /* Noise source error occurred */
  722. + LL_RNG_ClearFlag_SEIS(RNG);
  723. +
  724. + for(uint32_t i = 0; i < 12; ++i) {
  725. + const volatile uint32_t discard = LL_RNG_ReadRandData32(RNG);
  726. + UNUSED(discard);
  727. + }
  728. + }
  729. + }
  730. +
  731. + return LL_RNG_ReadRandData32(RNG);
  732. +}
  733. +
  734. uint32_t furi_hal_random_get() {
  735. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RNG_SEMID))
  736. ;
  737. LL_RNG_Enable(RNG);
  738. - while(!LL_RNG_IsActiveFlag_DRDY(RNG))
  739. - ;
  740. -
  741. - if((LL_RNG_IsActiveFlag_CECS(RNG)) || (LL_RNG_IsActiveFlag_SECS(RNG))) {
  742. - furi_crash("TRNG error");
  743. - }
  744. -
  745. - uint32_t random_val = LL_RNG_ReadRandData32(RNG);
  746. + const uint32_t random_val = furi_hal_random_read_rng();
  747. LL_RNG_Disable(RNG);
  748. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RNG_SEMID, 0);
  749. @@ -35,15 +51,7 @@ void furi_hal_random_fill_buf(uint8_t* buf, uint32_t len) {
  750. LL_RNG_Enable(RNG);
  751. for(uint32_t i = 0; i < len; i += 4) {
  752. - while(!LL_RNG_IsActiveFlag_DRDY(RNG))
  753. - ;
  754. -
  755. - if((LL_RNG_IsActiveFlag_CECS(RNG)) || (LL_RNG_IsActiveFlag_SECS(RNG))) {
  756. - furi_crash("TRNG error");
  757. - }
  758. -
  759. - uint32_t random_val = LL_RNG_ReadRandData32(RNG);
  760. -
  761. + const uint32_t random_val = furi_hal_random_read_rng();
  762. uint8_t len_cur = ((i + 4) < len) ? (4) : (len - i);
  763. memcpy(&buf[i], &random_val, len_cur);
  764. }
  765. diff --git a/firmware/targets/f7/furi_hal/furi_hal_resources.c b/firmware/targets/f7/furi_hal/furi_hal_resources.c
  766. index 912912b4a..abfd977e5 100644
  767. --- a/firmware/targets/f7/furi_hal/furi_hal_resources.c
  768. +++ b/firmware/targets/f7/furi_hal/furi_hal_resources.c
  769. @@ -6,6 +6,9 @@
  770. #define TAG "FuriHalResources"
  771. +const GpioPin gpio_swdio = {.port = GPIOA, .pin = LL_GPIO_PIN_13};
  772. +const GpioPin gpio_swclk = {.port = GPIOA, .pin = LL_GPIO_PIN_14};
  773. +
  774. const GpioPin gpio_vibro = {.port = VIBRO_GPIO_Port, .pin = VIBRO_Pin};
  775. const GpioPin gpio_ibutton = {.port = iBTN_GPIO_Port, .pin = iBTN_Pin};
  776. diff --git a/firmware/targets/f7/furi_hal/furi_hal_resources.h b/firmware/targets/f7/furi_hal/furi_hal_resources.h
  777. index f29917300..6e585c518 100644
  778. --- a/firmware/targets/f7/furi_hal/furi_hal_resources.h
  779. +++ b/firmware/targets/f7/furi_hal/furi_hal_resources.h
  780. @@ -50,6 +50,9 @@ extern const size_t input_pins_count;
  781. extern const GpioPinRecord gpio_pins[];
  782. extern const size_t gpio_pins_count;
  783. +extern const GpioPin gpio_swdio;
  784. +extern const GpioPin gpio_swclk;
  785. +
  786. extern const GpioPin gpio_vibro;
  787. extern const GpioPin gpio_ibutton;
  788. diff --git a/firmware/targets/f7/furi_hal/furi_hal_rtc.c b/firmware/targets/f7/furi_hal/furi_hal_rtc.c
  789. index 84e7fe395..7bd45c35d 100644
  790. --- a/firmware/targets/f7/furi_hal/furi_hal_rtc.c
  791. +++ b/firmware/targets/f7/furi_hal/furi_hal_rtc.c
  792. @@ -165,6 +165,14 @@ void furi_hal_rtc_init() {
  793. FURI_LOG_I(TAG, "Init OK");
  794. }
  795. +void furi_hal_rtc_sync_shadow() {
  796. + if(!LL_RTC_IsShadowRegBypassEnabled(RTC)) {
  797. + LL_RTC_ClearFlag_RS(RTC);
  798. + while(!LL_RTC_IsActiveFlag_RS(RTC)) {
  799. + };
  800. + }
  801. +}
  802. +
  803. uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
  804. return LL_RTC_BAK_GetRegister(RTC, reg);
  805. }
  806. @@ -312,12 +320,7 @@ void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
  807. /* Exit Initialization mode */
  808. LL_RTC_DisableInitMode(RTC);
  809. - /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  810. - if(!LL_RTC_IsShadowRegBypassEnabled(RTC)) {
  811. - LL_RTC_ClearFlag_RS(RTC);
  812. - while(!LL_RTC_IsActiveFlag_RS(RTC)) {
  813. - };
  814. - }
  815. + furi_hal_rtc_sync_shadow();
  816. /* Enable write protection */
  817. LL_RTC_EnableWriteProtection(RTC);
  818. diff --git a/firmware/targets/furi_hal_include/furi_hal_debug.h b/firmware/targets/furi_hal_include/furi_hal_debug.h
  819. index 88397bbba..befbb4f40 100644
  820. --- a/firmware/targets/furi_hal_include/furi_hal_debug.h
  821. +++ b/firmware/targets/furi_hal_include/furi_hal_debug.h
  822. @@ -18,6 +18,9 @@ void furi_hal_debug_enable();
  823. /** Disable MCU debug */
  824. void furi_hal_debug_disable();
  825. +/** Check if GDB debug session is active */
  826. +bool furi_hal_debug_is_gdb_session_active();
  827. +
  828. #ifdef __cplusplus
  829. }
  830. #endif
  831. diff --git a/firmware/targets/furi_hal_include/furi_hal_power.h b/firmware/targets/furi_hal_include/furi_hal_power.h
  832. index 462e20e41..00182fa28 100644
  833. --- a/firmware/targets/furi_hal_include/furi_hal_power.h
  834. +++ b/firmware/targets/furi_hal_include/furi_hal_power.h
  835. @@ -58,12 +58,6 @@ void furi_hal_power_insomnia_exit();
  836. */
  837. bool furi_hal_power_sleep_available();
  838. -/** Check if deep sleep availble
  839. - *
  840. - * @return true if available
  841. - */
  842. -bool furi_hal_power_deep_sleep_available();
  843. -
  844. /** Go to sleep
  845. */
  846. void furi_hal_power_sleep();
  847. diff --git a/firmware/targets/furi_hal_include/furi_hal_rtc.h b/firmware/targets/furi_hal_include/furi_hal_rtc.h
  848. index b16b04a68..0d9f46f01 100644
  849. --- a/firmware/targets/furi_hal_include/furi_hal_rtc.h
  850. +++ b/firmware/targets/furi_hal_include/furi_hal_rtc.h
  851. @@ -30,6 +30,7 @@ typedef enum {
  852. FuriHalRtcFlagLock = (1 << 2),
  853. FuriHalRtcFlagC2Update = (1 << 3),
  854. FuriHalRtcFlagHandOrient = (1 << 4),
  855. + FuriHalRtcFlagLegacySleep = (1 << 5),
  856. } FuriHalRtcFlag;
  857. typedef enum {
  858. @@ -85,6 +86,9 @@ void furi_hal_rtc_deinit_early();
  859. /** Initialize RTC subsystem */
  860. void furi_hal_rtc_init();
  861. +/** Force sync shadow registers */
  862. +void furi_hal_rtc_sync_shadow();
  863. +
  864. /** Get RTC register content
  865. *
  866. * @param[in] reg The register identifier
  867. diff --git a/furi/core/check.c b/furi/core/check.c
  868. index 910527cee..64f9f72f1 100644
  869. --- a/furi/core/check.c
  870. +++ b/furi/core/check.c
  871. @@ -5,6 +5,7 @@
  872. #include <furi_hal_console.h>
  873. #include <furi_hal_power.h>
  874. #include <furi_hal_rtc.h>
  875. +#include <furi_hal_debug.h>
  876. #include <stdio.h>
  877. #include <FreeRTOS.h>
  878. @@ -117,6 +118,8 @@ FURI_NORETURN void __furi_crash() {
  879. if(debug) {
  880. furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
  881. furi_hal_console_puts("\033[0m\r\n");
  882. + furi_hal_debug_enable();
  883. +
  884. RESTORE_REGISTERS_AND_HALT_MCU(true);
  885. } else {
  886. furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
  887. diff --git a/furi/core/core_defines.h b/furi/core/core_defines.h
  888. index 03a364abd..830bb191c 100644
  889. --- a/furi/core/core_defines.h
  890. +++ b/furi/core/core_defines.h
  891. @@ -24,6 +24,10 @@ extern "C" {
  892. })
  893. #endif
  894. +#ifndef ABS
  895. +#define ABS(a) ({ (a) < 0 ? -(a) : (a); })
  896. +#endif
  897. +
  898. #ifndef ROUND_UP_TO
  899. #define ROUND_UP_TO(a, b) \
  900. ({ \
  901. diff --git a/site_scons/cc.scons b/site_scons/cc.scons
  902. index 1eb6a3376..55ab72ba6 100644
  903. --- a/site_scons/cc.scons
  904. +++ b/site_scons/cc.scons
  905. @@ -36,6 +36,7 @@ ENV.AppendUnique(
  906. ],
  907. CPPDEFINES=[
  908. "_GNU_SOURCE",
  909. + *GetOption("extra_defines"),
  910. ],
  911. LINKFLAGS=[
  912. "-mcpu=cortex-m4",
  913. diff --git a/site_scons/commandline.scons b/site_scons/commandline.scons
  914. index d832a466e..2e9486627 100644
  915. --- a/site_scons/commandline.scons
  916. +++ b/site_scons/commandline.scons
  917. @@ -26,6 +26,14 @@ AddOption(
  918. help="List of applications to add to firmware's built-ins. Also see FIRMWARE_APP_SET and FIRMWARE_APPS",
  919. )
  920. +AddOption(
  921. + "--extra-define",
  922. + action="append",
  923. + dest="extra_defines",
  924. + default=[],
  925. + help="Extra global define that will be passed to C/C++ compiler, can be specified multiple times",
  926. +)
  927. +
  928. AddOption(
  929. "--extra-ext-apps",
  930. action="store",