3. Analyzing the Key Points of a Sample Code

This section covers the fundamental code structure found in every demonstration application included in Dialog’s SDK. All applications are implemented as FreeRTOS tasks that are created under the system_init() routine.

3.1. The Key Points of the Main Function

The main() function is the basic routine of every source code and is responsible for the following actions:

  1. To create the system_init() task which is responsible for initializing the system. In this task all application tasks are being created as well.

  2. To trigger the FreeRTOS scheduler which is responsible for controlling and executing all the RTOS tasks, according to their priorities.

3.2. The Key Points of the System Initialization Function

  1. System clocks should be initialised according to applications’ needs. The system clock, low power clock source, clock divider for the AMBA peripheral bus clock (APB) and AMBA high speed bus (AHB) should be set. The low power clock (lp) used is selected via dg_configUSE_LP_CLK declared in config/custom_config_xxx.h file. Valid values of this macro are:

  1. LP_CLK_RCX used for selecting the internal RC oscillator.

  2. LP_CLK_32768 used for selecting the external crystal XTAL32K

  3. LP_LCK_32000 used for clocking the device using digital clocks generated either by another MCU or digital clock generator.

By default, all the code examples found in Dialog’s SDK make use of the external crystal XTAL32K as LP clock

/*
 * 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 XTAL32M has settled and the PLL maybe
 * 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();

If XTAL32K is selected as low power clock, the system is prevented from entering sleep for approx. 8 seconds after a HW reset (cold boot). This delay ensures the external crystal is settled before the device utilizes it. After the passage of that delay, the device can enter sleep mode. Note that sysclk_LP cannot be used as clock source while the device is active (system clock).

  1. The next step is the initialization of the peripherals used by the target application. User should add their own functionality in prvSetupHardware() routine.

  2. Select the preferred sleep as well as wakeup mode.

/*
 * Upon a wakeup cycle, wait for the XTAL32M crystal to settle.
 * BLE, USB and UART blocks require the XTAL32M to be up and
 * running to work properly.
 */
pm_set_wakeup_mode(true);

/* Set the desired sleep mode. */
pm_set_sleep_mode(pm_mode_extended_sleep);

/* Set the desired wakeup mode. */
pm_set_sys_wakeup_mode(pm_sys_wakeup_mode_fast);
'Import sleep'

Figure 14 Possible Device’s States

  1. After all system configurations is done, the application tasks should be declared.

Note

The last code line in system_init(), OS_TASK_DELETE(OS_GET_CURRENT_TASK()), is used for deleting the task itself. This is done in order to release valuable system resources. After all, the initialization of the system only needs to be done once at beginning of device’s start.