5. Code Overview

5.1. Header Files

In main.c file, add the following header files:

#include "hw_pdc.h"
#include "hw_wkup.h"
#include "hw_sys.h"

Add the following function prototypes after line : static OS_TASK xHandle:

/* LED D1 status */
__RETAINED bool LED_state = 0;
__RETAINED bool button_state = 0;

5.2. Application Task

In main.c file, replace the static OS_TASK_FUNCTION(prvTemplateTask, pvParameters) apllication task with the following code:

static OS_TASK_FUNCTION(prvTemplateTask, pvParameters)
{
        OS_TICK_TIME xNextWakeTime;
        static uint32_t test_counter=0;

        /* Initialise xNextWakeTime - this only needs to be done once. */
        xNextWakeTime = OS_GET_TICK_COUNT();

        for ( ;; ) {
                /* Place this task in the blocked state until it is time to run again.
                The block time is specified in ticks, the constant used converts ticks
                to ms.  While in the Blocked state this task will not consume any CPU
                time. */
                xNextWakeTime += mainCOUNTER_FREQUENCY_MS;
                OS_DELAY_UNTIL(xNextWakeTime);
                test_counter++;

                if (test_counter % (1000 / OS_TICKS_2_MS(mainCOUNTER_FREQUENCY_MS)) == 0) {
#if defined CONFIG_RETARGET
                        printf("#");
                        fflush(stdout);
#endif
                        /* Enable the COM power domain before handling any GPIO pin */
                        hw_sys_pd_com_enable();

                        hw_gpio_configure_pin(LED1_PORT, LED1_PIN, LED1_MODE, LED1_FUNC, LED_state && button_state);
                        hw_gpio_pad_latch_enable (LED1_PORT, LED1_PIN);
                        hw_gpio_pad_latch_disable(LED1_PORT, LED1_PIN); /* Lock pin status */

                        /* Disable the COM power domain after handling the GPIO pins */
                        hw_sys_pd_com_disable();
                        LED_state ^= 1;

                }
        }
}

5.3. Wake-Up Timer Code

In main.c file and after static OS_TASK_FUNCTION(prvTemplateTask, pvParameters), add the following code used for handling external events via the wake-up controller:

/* WKUP KEY interrupt handler */
static void wkup_cb(void)
{

        /* Clear the WKUP interrupt flag!!! */
        hw_wkup_reset_key_interrupt();


        /*
        * Avoid using printf() within ISR context!!! It may crash your code.
        * Instead, use notifications to notify a task to perform an action!!
        */

        /* Toggle the led status */
        button_state ^= 1;

}


/* Initialize the WKUP controller */
static void wkup_init(void)
{
        /********************************* Custom wake up settings ************************************/
        wkup_config pin_wkup_conf = {
                .debounce = 10,                                                 /* key debounce time 10 mS */
                .pin_wkup_state[HW_GPIO_PORT_0] = (1 << KEY1_PIN),              /* Set GPIO P0_10 (=K1) to wake up */
                .pin_gpio_state[HW_GPIO_PORT_0] = 0,
                .pin_trigger[HW_GPIO_PORT_0] = (0 << KEY1_PIN),
                .gpio_sense[HW_GPIO_PORT_0] = (1 << KEY1_PIN),
        };
        /********************************************************************************************/

        /* Initialize the WKUP controller */
        hw_wkup_init(&pin_wkup_conf);

        /*
        * Enable interrupts produced by the KEY block of the wakeup controller (debounce
        * circuitry) and register a callback function to hit following a KEY event.
        */
        hw_wkup_register_key_interrupt(wkup_cb, 1);

        /* Enable interrupts of WKUP controller */
        hw_wkup_enable_key_irq();
}

5.4. Hardware Initialization

In main.c file, replace the prvSetupHardware() routine with the following code used for configuring the pins utilized by the wake-up controller.

/**
* @brief Hardware Initialization
*/
static void prvSetupHardware( void )
{
        /*
        * The IRQ produced by the KEY sub block of the wakeup controller (debounced
        * IO IRQ) is multiplexed with other trigger sources (VBUS IRQ, SYS2CMAC IRQ,
        * JTAG present) in a single PDC peripheral trigger ID
        * (HW_PDC_PERIPH_TRIG_ID_COMBO).
        */
#if !defined(CONFIG_USE_BLE) && (!dg_configUSE_SYS_CHARGER)

        uint32_t pdc_wkup_combo_id = hw_pdc_add_entry(HW_PDC_LUT_ENTRY_VAL(
                                                        HW_PDC_TRIG_SELECT_PERIPHERAL,
                                                        HW_PDC_PERIPH_TRIG_ID_COMBO,
                                                        HW_PDC_MASTER_CM33, 0));
        OS_ASSERT(pdc_wkup_combo_id != HW_PDC_INVALID_LUT_INDEX);

        /*  */
        hw_pdc_set_pending(pdc_wkup_combo_id);
        hw_pdc_acknowledge(pdc_wkup_combo_id);
#endif

        wkup_init();

        /* Init hardware */
        pm_system_init(periph_init);


        /* Enable the COM power domain before handling any GPIO pin */
        hw_sys_pd_com_enable();

        /* Configure the KEY1 push button on Pro DevKit */
        HW_GPIO_SET_PIN_FUNCTION(KEY1);
        HW_GPIO_PAD_LATCH_ENABLE(KEY1);

        /* Lock the mode of the target GPIO pin */
        HW_GPIO_PAD_LATCH_DISABLE(KEY1);

        /* Disable the COM power domain after handling the GPIO pins */
        hw_sys_pd_com_disable();
}