Morse Micro IoT SDK  2.9.7
hooks.c
1/*
2 * Copyright 2021-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#include "FreeRTOS.h"
8#include "task.h"
9#include "queue.h"
10
11#include "mmhal.h"
12
13void vApplicationMallocFailedHook(void)
14{
15 /* This hook function that will be called if configUSE_MALLOC_FAILED_HOOK is set to 1 and
16 * a call to pvPortMalloc() fails. */
17 configABORT();
18}
19
20void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName)
21{
22 (void)pcTaskName;
23 (void)pxTask;
24
25 /* This hook function will be called if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2
26 * and a stack overflow is detected. */
27 configABORT();
28}
29
30void vApplicationIdleHook(void)
31{
32 /* This hook function will be called on each iteration of the idle task if
33 * configUSE_IDLE_HOOK is set to 1. */
34}
35
36void vApplicationTickHook(void)
37{
38 /* This hook function will be called on each tick interrupt (from interrupt context)
39 * if configUSE_TICK_HOOK is set to 1. */
40}
41
42#if (configUSE_TICKLESS_IDLE == 1)
43
52void vPortSuppressTicksAndSleep(uint32_t expected_idle_time_ms)
53{
54 enum mmhal_sleep_state sleep_state;
55 uint32_t elapsed_ms = 0;
56
57 while (elapsed_ms < expected_idle_time_ms)
58 {
59 sleep_state = mmhal_sleep_prepare(expected_idle_time_ms - elapsed_ms);
60 if (sleep_state == MMHAL_SLEEP_DISABLED)
61 {
62 return;
63 }
64
65 /* If a context switch is pending or a task is waiting for the scheduler
66 * to be unsuspended then abandon the low power entry. */
67 if (eTaskConfirmSleepModeStatus() == eAbortSleep)
68 {
69 mmhal_sleep_abort(sleep_state);
70 return;
71 }
72
73 uint32_t slept_ms = mmhal_sleep(sleep_state, expected_idle_time_ms - elapsed_ms);
74 elapsed_ms += slept_ms;
75
76 if (sleep_state == MMHAL_SLEEP_DEEP)
77 {
78 /* Step the FreeRTOS tick to account for any tick periods that elapsed. */
79 vTaskStepTick(slept_ms);
80 }
81
82 /* If a context switch is pending or a task is waiting for the scheduler
83 * to be unsuspended then abandon the low power entry. */
84 if (eTaskConfirmSleepModeStatus() == eAbortSleep)
85 {
86 mmhal_sleep_abort(sleep_state);
87 return;
88 }
89
91 }
92}
93
94#endif
uint32_t mmhal_sleep(enum mmhal_sleep_state sleep_state, uint32_t expected_idle_time_ms)
Function to enter MCU sleep.
mmhal_sleep_state
Enumeration of MCU sleep state.
Definition: mmhal.h:59
void mmhal_sleep_abort(enum mmhal_sleep_state sleep_state)
Function to abort the MCU sleep state.
void mmhal_sleep_cleanup(void)
Function to cleanup on exit from the MCU sleep state.
enum mmhal_sleep_state mmhal_sleep_prepare(uint32_t expected_idle_time_ms)
Function to prepare MCU to enter sleep.
@ MMHAL_SLEEP_DISABLED
Disable MCU sleep.
Definition: mmhal.h:60
@ MMHAL_SLEEP_DEEP
MCU can enter deep sleep.
Definition: mmhal.h:62