/** \file
 *  \brief This file provides the entry point for the application running on the Cortex-M4 core.
 *
 *  ## CapSense
 *  To tune the CapSense buttons, do the following:
 *     1. Define `TUNE_CAPSENSE` below.
 *     2. Rebuild the project, and load it on to your board.
 *     3. Right-click on the `CapSense` component on the "CapSense" schematic page in `TopDesign.cysch`, and choose "Launch Tuner".
 *     4. Follow the instructions in [AN85951 - PSoC® 4 and PSoC® 6 MCU CapSense® Design Guide](https://www.cypress.com/documentation/application-notes/an85951-psoc-4-and-psoc-6-mcu-capsense-design-guide) to complete the tuning.
 *
 */


/* Include Files */
#include "KTag.h"


// See the instructions at the top of this file for how to tune CapSense--this is only part of what you need.
//#define TUNE_CAPSENSE


/* This section is used to verify an application signature 
   For sha256 verification, set the number of elements in the array to 64, and
   in bootload_common.ld set __cy_boot_signature_size = 256.
*/
CY_SECTION(".cy_app_signature") __USED static const uint32_t cy_bootload_appSignature[64];


#ifndef TUNE_CAPSENSE
int main()
{
    CONFIG_InitTasks();
    
    /* Enable global interrupts. */
    __enable_irq();
    
    COMM_InitIPCMessages();
    
    CONFIG_RunTasks();
}
#endif // TUNE_CAPSENSE


void vApplicationIdleHook(void)
{
    CyDelay(500);
}


void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
    /* Halt the CPU */
    CY_ASSERT(0);
}


void vApplicationMallocFailedHook( void )
{
	/* Halt the CPU */
    CY_ASSERT(0);
}


#ifdef TUNE_CAPSENSE
int main()
{
    uint8 header[] = {0x0Du, 0x0Au};
    uint8 tail[] = {0x00u, 0xFFu, 0xFFu};

    __enable_irq(); /* Enable global interrupts. */
    
    UART_Console_Start(); /* Start UART SCB Component */
    CapSense_Start(); /* Initialize Component */
    CapSense_ScanAllWidgets(); /* Scan all widgets */
    
    for(;;)
    {
        /* Do this only when a scan is done */
        if(CapSense_NOT_BUSY == CapSense_IsBusy())
        {
            CapSense_ProcessAllWidgets(); /* Process all widgets */

            /* Send packet header */
            UART_Console_PutArrayBlocking((uint8 *)(&header), sizeof(header));
            /* Send packet with CapSense data */
            UART_Console_PutArrayBlocking((uint8 *)(&CapSense_dsRam), sizeof(CapSense_dsRam));
            /* Send packet tail */
            UART_Console_PutArrayBlocking((uint8 *)(&tail), sizeof(tail));

            CapSense_ScanAllWidgets(); /* Start next scan */
        }
    }
}
#endif // TUNE_CAPSENSE

void vApplicationGetIdleTaskMemory(StaticTask_t** ppxIdleTaskTCBBuffer, StackType_t** ppxIdleTaskStackBuffer, uint32_t* pulIdleTaskStackSize)
{
    static StaticTask_t xIdleTaskTCB;
    static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE];

    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
    *ppxIdleTaskStackBuffer = uxIdleTaskStack;
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

void vApplicationGetTimerTaskMemory(StaticTask_t** ppxTimerTaskTCBBuffer, StackType_t** ppxTimerTaskStackBuffer, uint32_t* pulTimerTaskStackSize)
{
    static StaticTask_t xTimerTaskTCB;
    static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH];

    *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
    *ppxTimerTaskStackBuffer = uxTimerTaskStack;
    *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}