/* 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 */
#include "fls_drv_bin.h"
/* USER CODE END Includes */

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

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define PRINTF_UART (2) /* UART instance for PRINTF */
#define EFM_CUS_NVR_ADDR        (0x10030000U)
/* USER CODE END PD */

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

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint32_t cus_nvr[FEATURE_EFM_NVR_ARRAY_SECTOR_SIZE >> 2] = { 0 };
uint32_t cus_readnvr[FEATURE_EFM_NVR_ARRAY_SECTOR_SIZE >> 2] = { 0 };
#define EFM_DEBUGGER_TAG_OFFSET (0x00U)
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* 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 */

    status = FLASH_DRV_Init();
    if (status != STATUS_SUCCESS)
    {
        return status;
    }
    
    cus_nvr[EFM_DEBUGGER_TAG_OFFSET >> 2] = 0x11111111;
    cus_nvr[(EFM_DEBUGGER_TAG_OFFSET >> 2) + 1] = 0x11111111;
    EFM->CUS_KEY = 0x4DFF32;
    status = FLASH_DRV_EraseNVR(EFM_CUS_NVR_ADDR);
    status = FLASH_DRV_ReadNVR(EFM_CUS_NVR_ADDR, FEATURE_EFM_NVR_ARRAY_SECTOR_SIZE, cus_readnvr);
    status |= FLASH_DRV_ProgramNVR(EFM_CUS_NVR_ADDR, FEATURE_EFM_NVR_ARRAY_SECTOR_SIZE, cus_nvr);
    status = FLASH_DRV_ReadNVR(EFM_CUS_NVR_ADDR, FEATURE_EFM_NVR_ARRAY_SECTOR_SIZE, cus_readnvr);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        if (status != STATUS_SUCCESS)
        {
            break;
        }
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
    }
    return status;
    /* 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);
}

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