Cypress: Add entering deep sleep in MCUBootApp, code improvements

- improved code in MCUBootApp
- added possibility to enter deep sleep mode after mcuboot app execution
- minor refactoring

Signed-off-by: dmiv <dmiv@cypress.com>
Signed-off-by: Roman Okhrimenko <roman.okhrimenko@cypress.com>
Signed-off-by: Taras Boretskyy <taras.boretskyy@cypress.com>
diff --git a/boot/cypress/MCUBootApp/main.c b/boot/cypress/MCUBootApp/main.c
index 6fcc81f..eb0ee11 100644
--- a/boot/cypress/MCUBootApp/main.c
+++ b/boot/cypress/MCUBootApp/main.c
@@ -41,6 +41,55 @@
 #define CYBSP_UART_HW SCB5
 #define CYBSP_UART_IRQ scb_5_interrupt_IRQn
 
+#ifdef CY_BOOT_USE_EXTERNAL_FLASH
+/* Choose SMIF slot number (slave select).
+ * Acceptable values are:
+ * 0 - SMIF disabled (no external memory);
+ * 1, 2, 3 or 4 - slave select line memory module is connected to.
+ */
+uint32_t smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
+#endif
+
+extern cy_stc_scb_uart_context_t CYBSP_UART_context;
+
+/* Parameter structures for callback function */
+static cy_stc_syspm_callback_params_t deep_sleep_clbk_params = 
+{
+    CYBSP_UART_HW,
+    &CYBSP_UART_context
+};
+
+static cy_stc_syspm_callback_params_t deep_sleep_sysclk_pm_clbk_param =
+{
+    NULL,
+    NULL
+};
+
+/* Callback structure */
+cy_stc_syspm_callback_t uart_deep_sleep = 
+{
+    &Cy_SCB_UART_DeepSleepCallback, 
+    CY_SYSPM_DEEPSLEEP,
+    CY_SYSPM_SKIP_BEFORE_TRANSITION ,
+    &deep_sleep_clbk_params,
+    NULL,
+    NULL,
+    0
+};
+
+cy_stc_syspm_callback_t clk_deep_sleep = 
+{
+    &Cy_SysClk_DeepSleepCallback, 
+    CY_SYSPM_DEEPSLEEP,
+    CY_SYSPM_SKIP_BEFORE_TRANSITION ,
+    &deep_sleep_sysclk_pm_clbk_param,
+    NULL,
+    NULL,
+    0
+};
+
+void hw_deinit(void);
+
 static void do_boot(struct boot_rsp *rsp)
 {
     uint32_t app_addr = 0;
@@ -51,26 +100,36 @@
     BOOT_LOG_INF("Start Address: 0x%08lx", app_addr);
     Cy_SysLib_Delay(100);
 
-    Cy_SysEnableCM4(app_addr);
+    hw_deinit();
 
-    while (1)
-    {
-        __WFI() ;
-    }
+    Cy_SysEnableCM4(app_addr);
 }
 
 int main(void)
 {
     struct boot_rsp rsp;
+    cy_rslt_t result = CY_RSLT_TYPE_ERROR;
 
     init_cycfg_clocks();
     init_cycfg_peripherals();
     init_cycfg_pins();
+
+    /* register callback funtions that manage peripherals before going to deep sleep */
+    if (!Cy_SysPm_RegisterCallback(&uart_deep_sleep) || 
+        !Cy_SysPm_RegisterCallback(&clk_deep_sleep))
+    {
+        CY_ASSERT(0);
+    }
     /* enable interrupts */
     __enable_irq();
 
     /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */
-    cy_retarget_io_pdl_init(115200u);
+    result = cy_retarget_io_pdl_init(115200u);
+
+    if (result != CY_RSLT_SUCCESS)
+    {
+        CY_ASSERT(0);
+    }
 
     BOOT_LOG_INF("MCUBoot Bootloader Started");
 
@@ -82,13 +141,8 @@
      * available on PSoC062-2M in case of external
      * memory usage */
     #define MCUBOOT_MAX_IMG_SECTORS 4096
-    int smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
-    /* Acceptable values are:
-    * 0 - SMIF disabled (no external memory);
-    * 1, 2, 3 or 4 - slave select line memory module is connected to.
-    */
     rc = qspi_init_sfdp(smif_id);
-    if(rc == CY_SMIF_SUCCESS)
+    if (rc == CY_SMIF_SUCCESS)
     {
         BOOT_LOG_INF("External Memory initialized w/ SFDP.");
     }
@@ -96,14 +150,35 @@
     {
         BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc);
     }
-    if(0 == rc)
+    if (0 == rc)
 #endif
     {
-        if (boot_go(&rsp) == 0) {
+        if (boot_go(&rsp) == 0)
+        {
             BOOT_LOG_INF("User Application validated successfully");
             do_boot(&rsp);
-        } else
-            BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images") ;
+        }
+        else
+        {
+            BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images");
+        }
     }
+
+    while (1)
+    {
+        Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
+    }
+
     return 0;
 }
+
+void hw_deinit(void)
+{
+    cy_retarget_io_pdl_deinit();
+    Cy_GPIO_Port_Deinit(CYBSP_UART_RX_PORT);
+    Cy_GPIO_Port_Deinit(CYBSP_UART_TX_PORT);
+
+#ifdef CY_BOOT_USE_EXTERNAL_FLASH
+    qspi_deinit(smif_id);
+#endif
+}