#include "common.hpp"

void vApplicationMallocFailedHook(void)
{
    /* Called if a call to pvPortMalloc() fails because there is insufficient
       free memory available in the FreeRTOS heap.  pvPortMalloc() is called
       internally by FreeRTOS API functions that create tasks, queues, software
       timers, and semaphores.  The size of the FreeRTOS heap is set by the
       configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */

    /* Force an assert. */
    configASSERT((volatile void *)NULL);
}

/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName)
{
    (void)pcTaskName;
    (void)pxTask;

    /* Run time stack overflow checking is performed if
       configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
       function is called if a stack overflow is detected. */

    /* Force an assert. */
    configASSERT((volatile void *)NULL);
}

void vApplicationTickHook(void)
{
#if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0
    {
        /* The full demo includes a software timer demo/test that requires
           prodding periodically from the tick interrupt. */
        /*完整的演示包括软件定时器演示/测试，需要
           从滴答声中断中周期性地发出提示 */
#if (mainENABLE_TIMER_DEMO == 1)
        vTimerPeriodicISRTests();
#endif

        /* Call the periodic queue overwrite from ISR demo. */
        /*调用ISR演示中的周期性队列覆盖 */
#if (mainENABLE_QUEUE_OVERWRITE == 1)
        vQueueOverwritePeriodicISRDemo();
#endif

        /* Call the periodic event group from ISR demo. */
        /*从ISR演示中调用定期事件组 */
#if (mainENABLE_EVENT_GROUP == 1)
        vPeriodicEventGroupsProcessing();
#endif

        /* Call the code that uses a mutex from an ISR. */
        /*从ISR调用使用互斥的代码 */
#if (mainENABLE_INTERRUPT_SEMAPHORE == 1)
        vInterruptSemaphorePeriodicTest();
#endif

        /* Call the code that 'gives' a task notification from an ISR. */
        /*调用从ISR“发出”任务通知的代码 */
#if (mainENABLE_TASK_NOTIFY == 1)
        xNotifyTaskFromISR();
#endif
    }
#endif
}

void _printTaskStackHighWaterMark(const char *task_name)
{
    TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
    if (currentTask != NULL) {
        printf("%s TASK STACK HIGH WATER MARK: %ld\n", task_name, uxTaskGetStackHighWaterMark(currentTask));
    } else {
        printf("FAILED TO GET CURRENT TASK HANDLE.\n");
    }
}
