2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_os.h>
|
2022-04-21 13:15:19 +00:00
|
|
|
#include <furi_hal_clock.h>
|
2022-05-06 13:37:10 +00:00
|
|
|
#include <furi_hal_console.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_power.h>
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
#include <furi_hal_gpio.h>
|
|
|
|
#include <furi_hal_resources.h>
|
2022-04-21 13:15:19 +00:00
|
|
|
#include <furi_hal_idle_timer.h>
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
|
2021-09-10 02:19:02 +00:00
|
|
|
#include <stm32wbxx_ll_cortex.h>
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "FuriHalOs"
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
#define FURI_HAL_IDLE_TIMER_CLK_HZ 32768
|
|
|
|
#define FURI_HAL_OS_TICK_HZ configTICK_RATE_HZ
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
#define FURI_HAL_OS_IDLE_CNT_TO_TICKS(x) (((x)*FURI_HAL_OS_TICK_HZ) / FURI_HAL_IDLE_TIMER_CLK_HZ)
|
|
|
|
#define FURI_HAL_OS_TICKS_TO_IDLE_CNT(x) (((x)*FURI_HAL_IDLE_TIMER_CLK_HZ) / FURI_HAL_OS_TICK_HZ)
|
2022-04-21 13:15:19 +00:00
|
|
|
|
|
|
|
#define FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH (FURI_HAL_OS_IDLE_CNT_TO_TICKS(FURI_HAL_IDLE_TIMER_MAX))
|
|
|
|
#define FURI_HAL_OS_MAX_SLEEP (FURI_HAL_IDLE_TIMER_TICK_PER_EPOCH - 1)
|
2021-09-10 02:19:02 +00:00
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
#define FURI_HAL_OS_NVIC_IS_PENDING() (NVIC->ISPR[0] || NVIC->ISPR[1])
|
|
|
|
#define FURI_HAL_OS_EXTI_LINE_0_31 0
|
|
|
|
#define FURI_HAL_OS_EXTI_LINE_32_63 1
|
|
|
|
|
|
|
|
// Arbitrary (but small) number for better tick consistency
|
|
|
|
#define FURI_HAL_OS_EXTRA_CNT 3
|
|
|
|
|
2023-04-13 14:47:38 +00:00
|
|
|
#ifndef FURI_HAL_OS_DEBUG_AWAKE_GPIO
|
|
|
|
#define FURI_HAL_OS_DEBUG_AWAKE_GPIO (&gpio_ext_pa7)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_HAL_OS_DEBUG_TICK_GPIO
|
|
|
|
#define FURI_HAL_OS_DEBUG_TICK_GPIO (&gpio_ext_pa6)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_HAL_OS_DEBUG_SECOND_GPIO
|
|
|
|
#define FURI_HAL_OS_DEBUG_SECOND_GPIO (&gpio_ext_pa4)
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 02:19:02 +00:00
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
#include <stm32wbxx_ll_gpio.h>
|
|
|
|
|
|
|
|
void furi_hal_os_timer_callback() {
|
2023-04-13 14:47:38 +00:00
|
|
|
furi_hal_gpio_write(
|
|
|
|
FURI_HAL_OS_DEBUG_SECOND_GPIO, !furi_hal_gpio_read(FURI_HAL_OS_DEBUG_SECOND_GPIO));
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern void xPortSysTickHandler();
|
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
static volatile uint32_t furi_hal_os_skew;
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
void furi_hal_os_init() {
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_idle_timer_init();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
2023-04-13 14:47:38 +00:00
|
|
|
furi_hal_gpio_init_simple(FURI_HAL_OS_DEBUG_AWAKE_GPIO, GpioModeOutputPushPull);
|
|
|
|
furi_hal_gpio_init_simple(FURI_HAL_OS_DEBUG_TICK_GPIO, GpioModeOutputPushPull);
|
|
|
|
furi_hal_gpio_init_simple(FURI_HAL_OS_DEBUG_SECOND_GPIO, GpioModeOutputPushPull);
|
|
|
|
furi_hal_gpio_write(FURI_HAL_OS_DEBUG_AWAKE_GPIO, 1);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriTimer* second_timer =
|
|
|
|
furi_timer_alloc(furi_hal_os_timer_callback, FuriTimerTypePeriodic, NULL);
|
|
|
|
furi_timer_start(second_timer, FURI_HAL_OS_TICK_HZ);
|
2021-09-10 02:19:02 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
void furi_hal_os_tick() {
|
|
|
|
if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
2022-01-05 16:10:18 +00:00
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
2023-04-13 14:47:38 +00:00
|
|
|
furi_hal_gpio_write(
|
|
|
|
FURI_HAL_OS_DEBUG_TICK_GPIO, !furi_hal_gpio_read(FURI_HAL_OS_DEBUG_TICK_GPIO));
|
2022-01-05 16:10:18 +00:00
|
|
|
#endif
|
2022-04-21 13:15:19 +00:00
|
|
|
xPortSysTickHandler();
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
// Find out the IRQ number while debugging
|
|
|
|
static void furi_hal_os_nvic_dbg_trap() {
|
|
|
|
for(int32_t i = WWDG_IRQn; i <= DMAMUX1_OVR_IRQn; i++) {
|
|
|
|
if(NVIC_GetPendingIRQ(i)) {
|
|
|
|
(void)i;
|
|
|
|
// Break here
|
|
|
|
__NOP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out the EXTI line number while debugging
|
|
|
|
static void furi_hal_os_exti_dbg_trap(uint32_t exti, uint32_t val) {
|
|
|
|
for(uint32_t i = 0; val; val >>= 1U, ++i) {
|
|
|
|
if(val & 1U) {
|
|
|
|
(void)exti;
|
|
|
|
(void)i;
|
|
|
|
// Break here
|
|
|
|
__NOP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline bool furi_hal_os_is_pending_irq() {
|
|
|
|
if(FURI_HAL_OS_NVIC_IS_PENDING()) {
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
furi_hal_os_nvic_dbg_trap();
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t exti_lines_active;
|
|
|
|
if((exti_lines_active = LL_EXTI_ReadFlag_0_31(LL_EXTI_LINE_ALL_0_31))) {
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
furi_hal_os_exti_dbg_trap(FURI_HAL_OS_EXTI_LINE_0_31, exti_lines_active);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
} else if((exti_lines_active = LL_EXTI_ReadFlag_32_63(LL_EXTI_LINE_ALL_32_63))) {
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
|
|
|
furi_hal_os_exti_dbg_trap(FURI_HAL_OS_EXTI_LINE_32_63, exti_lines_active);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:19:02 +00:00
|
|
|
static inline uint32_t furi_hal_os_sleep(TickType_t expected_idle_ticks) {
|
|
|
|
// Stop ticks
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_clock_suspend_tick();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Start wakeup timer
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_idle_timer_start(FURI_HAL_OS_TICKS_TO_IDLE_CNT(expected_idle_ticks));
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
2023-04-13 14:47:38 +00:00
|
|
|
furi_hal_gpio_write(FURI_HAL_OS_DEBUG_AWAKE_GPIO, 0);
|
2021-09-10 02:19:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Go to sleep mode
|
|
|
|
furi_hal_power_sleep();
|
|
|
|
|
|
|
|
#ifdef FURI_HAL_OS_DEBUG
|
2023-04-13 14:47:38 +00:00
|
|
|
furi_hal_gpio_write(FURI_HAL_OS_DEBUG_AWAKE_GPIO, 1);
|
2021-09-10 02:19:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Calculate how much time we spent in the sleep
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
uint32_t after_cnt = furi_hal_idle_timer_get_cnt() + furi_hal_os_skew + FURI_HAL_OS_EXTRA_CNT;
|
2022-04-21 13:15:19 +00:00
|
|
|
uint32_t after_tick = FURI_HAL_OS_IDLE_CNT_TO_TICKS(after_cnt);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
furi_hal_os_skew = after_cnt - FURI_HAL_OS_TICKS_TO_IDLE_CNT(after_tick);
|
2021-09-10 02:19:02 +00:00
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
bool cmpm = LL_LPTIM_IsActiveFlag_CMPM(FURI_HAL_IDLE_TIMER);
|
|
|
|
bool arrm = LL_LPTIM_IsActiveFlag_ARRM(FURI_HAL_IDLE_TIMER);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(cmpm && arrm) after_tick += expected_idle_ticks;
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Prepare tick timer for new round
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_idle_timer_reset();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Resume ticks
|
2022-04-21 13:15:19 +00:00
|
|
|
furi_hal_clock_resume_tick();
|
2021-09-10 02:19:02 +00:00
|
|
|
return after_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) {
|
|
|
|
if(!furi_hal_power_sleep_available()) {
|
|
|
|
__WFI();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:15:19 +00:00
|
|
|
// Limit amount of ticks to maximum that timer can count
|
2022-01-05 16:10:18 +00:00
|
|
|
if(expected_idle_ticks > FURI_HAL_OS_MAX_SLEEP) {
|
2021-09-10 02:19:02 +00:00
|
|
|
expected_idle_ticks = FURI_HAL_OS_MAX_SLEEP;
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
// Stop IRQ handling, no one should disturb us till we finish
|
2022-03-29 17:37:23 +00:00
|
|
|
__disable_irq();
|
2021-09-10 02:19:02 +00:00
|
|
|
|
|
|
|
// Confirm OS that sleep is still possible
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
if(eTaskConfirmSleepModeStatus() == eAbortSleep || furi_hal_os_is_pending_irq()) {
|
2022-03-29 17:37:23 +00:00
|
|
|
__enable_irq();
|
2021-09-10 02:19:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep and track how much ticks we spent sleeping
|
|
|
|
uint32_t completed_ticks = furi_hal_os_sleep(expected_idle_ticks);
|
|
|
|
// Notify system about time spent in sleep
|
2022-01-05 16:10:18 +00:00
|
|
|
if(completed_ticks > 0) {
|
2022-04-21 13:15:19 +00:00
|
|
|
vTaskStepTick(MIN(completed_ticks, expected_idle_ticks));
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reenable IRQ
|
2022-03-29 17:37:23 +00:00
|
|
|
__enable_irq();
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
void vApplicationStackOverflowHook(TaskHandle_t xTask, char* pcTaskName) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(xTask);
|
|
|
|
furi_hal_console_puts("\r\n\r\n stack overflow in ");
|
|
|
|
furi_hal_console_puts(pcTaskName);
|
|
|
|
furi_hal_console_puts("\r\n\r\n");
|
2021-12-31 17:32:49 +00:00
|
|
|
furi_crash("StackOverflow");
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|