5. Code Overview¶
This section contains the code blocks needed to successfully execute this tutorial. Please watch the following video.
Presentation on Preparing SmartSnippets Studio for this Tutorial.
5.1. Header Files¶
In main.c
, add the following header file:
/* For the Wakeup Timer */
#include "hw_wkup.h"
5.2. System Init Code¶
In main.c
, replace system_init()
with the following code:
static void system_init( void *pvParameters )
{
OS_TASK task_h = NULL;
#if defined CONFIG_RETARGET
extern void retarget_init(void);
#endif
/* Prepare clocks. Note: cm_cpu_clk_set() and cm_sys_clk_set() can only be called from a
* task since they will suspend the task until the XTAL16M has settled and the PLL maybe
* is locked.
*/
cm_sys_clk_init(sysclk_XTAL16M);
cm_apb_set_clock_divider(apb_div1);
cm_ahb_set_clock_divider(ahb_div1);
cm_lp_clk_init();
/* Prepare the hardware to run this demo. */
prvSetupHardware();
/* init resources */
resource_init();
#if defined CONFIG_RETARGET
retarget_init();
#endif
/* Set the desired sleep mode. */
pm_set_wakeup_mode(true);
pm_set_sleep_mode(pm_mode_extended_sleep);
/* Start main task here (text menu available via UART1 to control application) */
OS_TASK_CREATE( "Template", /* The text name assigned to the task; for
debug only, not used by the kernel. */
prvTemplateTask, /* The function that implements the task. */
NULL, /* The parameter passed to the task. */
200 * OS_STACK_WORD_SIZE, /* The number of bytes to allocate to the
stack of the task. */
mainTEMPLATE_TASK_PRIORITY, /* The priority assigned to the task. */
task_h ); /* The task handle. */
OS_ASSERT(task_h);
/* The work of the SysInit task is done */
OS_TASK_DELETE( xHandle );
}
5.3. Wake-Up Timer Code¶
In main.c
, after system_init()
, add the following code for handling external events via the wake-up controller:
PRIVILEGED_DATA volatile bool pin_status_flag = 0;
/* Callback function to be called after an interrupt is generated, that is, when event counter reaches configured value. */
void wkup_cb(void)
{
/* This function must be called by any user-specified interrupt callback, to clear the interrupt flag. */
hw_wkup_reset_interrupt();
/* Toggle the LED D2 on the Pro DevKit */
hw_gpio_toggle(HW_GPIO_PORT_1, HW_GPIO_PIN_5);
/* Toggle the value of the flag indicating the status of the LED before entering sleep */
pin_status_flag ^= 1;
}
/* Function which makes all the necessary initializations for the wake-up controller. */
static void init_wkup(void)
{
/* This function must be called first and is responsible for the initialization of the hardware block */
hw_wkup_init(NULL);
/*
* Configure the pin(s) that can trigger the device to wake-up while in sleep mode. The last input parameter
* determines the triggering edge of the pulse (event)
*/
hw_wkup_configure_pin(HW_GPIO_PORT_1, HW_GPIO_PIN_6, true, HW_WKUP_PIN_STATE_LOW);
/*
* This function defines a delay between the time a trigger event will be presented and the time the controller
* will take this event into consideration.
* Setting debounce time to 0 disables hardware debouncing. Maximum debounce time is 63 ms.
*/
hw_wkup_set_debounce_time(10);
#if dg_configBLACK_ORCA_IC_REV == BLACK_ORCA_IC_REV_A // Check if the chip is either DA14680 or 81
/*
* Set threshold for event counter. Interrupt is generated after the event counter reaches the configured value.
* This function is only supported in DA14680/1 chips.
*/
hw_wkup_set_counter_threshold(3);
#endif
/* Register interrupt handler. */
hw_wkup_register_interrupt(wkup_cb, 1);
}
Note
If the variable named pin_status_flag
is not used, LED D2 on Pro DevKit will lose its state after
entering sleep. The default state for all pins when entering sleep is input pull-down.
5.4. Hardware Initialization¶
In main.c
, replace both periph_init()
and prvSetupHardware()
with the following code to configure pins
after a power-up/wake-up cycle. Please note that every time the system enters sleep, it loses all its pin
configurations.
/**
* @brief Initialize the peripherals domain after power-up.
*
*/
static void periph_init(void)
{
# if dg_configBLACK_ORCA_MB_REV == BLACK_ORCA_MB_REV_D
# define UART_TX_PORT HW_GPIO_PORT_1
# define UART_TX_PIN HW_GPIO_PIN_3
# define UART_RX_PORT HW_GPIO_PORT_2
# define UART_RX_PIN HW_GPIO_PIN_3
# else
# error "Unknown value for dg_configBLACK_ORCA_MB_REV!"
# endif
hw_gpio_set_pin_function(UART_TX_PORT, UART_TX_PIN, HW_GPIO_MODE_OUTPUT,
HW_GPIO_FUNC_UART_TX);
hw_gpio_set_pin_function(UART_RX_PORT, UART_RX_PIN, HW_GPIO_MODE_INPUT,
HW_GPIO_FUNC_UART_RX);
/* Configure pin PIN_5 as a GPIO with output functionality.*/
hw_gpio_configure_pin(HW_GPIO_PORT_1, HW_GPIO_PIN_5, HW_GPIO_MODE_OUTPUT, HW_GPIO_FUNC_GPIO, pin_status_flag);
}
/**
* @brief Hardware Initialization
*/
static void prvSetupHardware( void )
{
/* Init hardware */
pm_system_init(periph_init);
init_wkup();
}