/* USER CODE BEGIN Header */
/* you can remove the copyright */

/*
 *  Copyright 2020-2023 Yuntu Microelectronics Co., Ltd.
 *  All rights reserved.
 * 
 *  SPDX-License-Identifier: BSD-3-Clause
 * 
 * @file main.c
 * @brief 
 * 
 */

/* USER CODE END Header */
#include "sdk_project_config.h"
/* Includes ------------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define FLASH_INST         (0U)
#define TEST_PFLASH_0_ADDR (0x207D000U)
#define TEST_PFLASH_1_ADDR (0x02100000U)
#define TEST_DFLASH_ADDR   (0x06000000U)
#define TEST_PFLASH_0_LEN  (4 * 1024U)
#define TEST_PFLASH_1_LEN  (6 * 1024U)
#define TEST_DFLASH_LEN    (0x400U)
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
volatile bool flashError = false;
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
volatile uint8_t lptmr_flag = 0U;
uint8_t boot_info = 0U;
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
status_t Fls_ReadSwap(uint8_t * DestinationAddressPtr)
{
    status_t status = STATUS_SUCCESS;
    *DestinationAddressPtr = (EFM->STS & EFM_STS_BOOT_INFO_MASK) >> EFM_STS_BOOT_INFO_SHIFT;
    return status;
}

void lpTMR0_IRQHandler(void)
{
    lpTMR_DRV_ClearCompareFlag(0);
    lptmr_flag = 1;
}
/* USER CODE END 0 */


/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */
    status_t status = STATUS_SUCCESS;
    /* USER CODE END 1 */
    Board_Init();
    /* USER CODE BEGIN 2 */
    Fls_ReadSwap(&boot_info);
    if(boot_info == 0U)
    {
        PRINTF("APP is locating PFLASH0\r\n");
        GPIOE -> PCOR = (1U << 16);
        GPIOA -> PSOR = (1U << 20);
    }
    else
    {
        PRINTF("APP is locating PFLASH1\r\n");
        GPIOE -> PSOR = (1U << 16);
        GPIOA -> PCOR = (1U << 20);
    }
    INT_SYS_EnableIRQ(lpTMR0_IRQn);
    lpTMR_DRV_StartCounter(0);
    
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        if(lptmr_flag == 1U)
        {
            lptmr_flag = 0U;
            GPIOA -> PSOR = (1U << 18);
            FLASH_DRV_BootSwap(0);
            GPIOA -> PCOR = (1U << 18);
            SystemSoftwareReset();
        }
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    CLOCK_SYS_Init(g_clockManConfigsArr,CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr,CLOCK_MANAGER_CALLBACK_CNT);
    if(STATUS_SUCCESS != CLOCK_SYS_UpdateConfiguration(CLOCK_MANAGER_ACTIVE_INDEX,CLOCK_MANAGER_POLICY_AGREEMENT))
    {
        /* USER CODE BEGIN ERROR_HANDLER 1 */
        SystemSoftwareReset();
        /* USER CODE END ERROR_HANDLER 1 */
    }
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0,g_pin_mux_InitConfigArr0);
    UTILITY_PRINT_Init();
    FLASH_DRV_Init(0,&flash_config0,&flash_config0_State);
    FLASH_ECC_Fault_Init(&flash_ecc_fault_config0,&flash_ecc_fault_config0_State);
    lpTMR_DRV_Init(0,&LPTMR_Config,false);
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
