/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.1
 * Date               : 2020/08/06
 * Description        : 外设从机应用主函数及任务系统初始化
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/******************************************************************************/
/* 头文件包含 */
#include "CONFIG.h"
#include "HAL.h"
#include "gattprofile.h"
#include "peripheral.h"
#include "bsp_bmp390.h"
#include "bsp_ml307r.h"
#include "bsp_uart.h"
#include "bsp_adc.h"
#include "bsp_motor.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_iwdg.h"
#include "log.h"

#undef  LOG_ENABLE
#define LOG_ENABLE  1

#define SYSTICK_INTERVAL (1)
#define PB3_PIN     GPIO_Pin_3
uint8_t TxBuff[] = "This is a uart3 tx example\r\n";

/*********************************************************************
 * GLOBAL TYPEDEFS
 */
__attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];

#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
#endif

/*********************************************************************
 * @fn      Main_Circulation
 *
 * @brief   主循环
 *
 * @return  none
 */
__HIGH_CODE
__attribute__((noinline))
void Main_Circulation()
{
    logDebug("Main_Circulation\n");
    while(1)
    {
        TMOS_SystemProcess();
        KEY_ProcessLoop();
        MultiTimerYield();
    }
}

/*********************************************************************
 * @fn      main
 *
 * @brief   主函数
 *
 * @return  none
 */
int main(void)
{
#if(defined(DCDC_ENABLE)) && (DCDC_ENABLE == TRUE)
    PWR_DCDCCfg(ENABLE);
#endif
    HSECFG_Capacitance(HSECap_18p);
    SetSysClock(CLK_SOURCE_HSE_PLL_62_4MHz);
    // SysTick_Config( GetSysClock() / 1000 * SYSTICK_INTERVAL);  //设定嘀嗒时间1ms
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
    GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PD);
    GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PD);
#endif

#ifdef DEBUG
    BSP_UART3_Init();

    logDebug("%s", TxBuff);
#endif
    logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
    logDebug("%s\n", VER_LIB);

    CH58x_BLEInit();
    logDebug("BLE init ok\n");
    HAL_Init();
    logDebug("HAL init ok\n");
    GAPRole_PeripheralInit();
    logDebug("GAP init ok\n");
    Peripheral_Init();
    logDebug("Peripheral init ok\n");
    MultiTimerInstall((PlatformTicksFunction_t)BSP_Get_Tick);
    BSP_KEY_Init(app_task_handler);

    BSP_MOTOR_Init();
    VALVE_OPEN();

    BSP_LED_Init();

    BSP_VBAT_Init();
    logDebug("VBAT init ok\n");

    // BSP_Ml307r_Init();
    // logDebug("BSP_M1307r ok\n");
   
    ShowRestartReason();
    IWDG_Init(5000);
    logDebug("IWDG init ok\n");

    BSP_PRESS_Init();
    logDebug("BSP init ok\n");

    Function_Check();

    logDebug("BSP_Get_Tick1 %d\n",BSP_Get_Tick());
    DelayMs(1000);
    logDebug("BSP_Get_Tick2 %d\n",BSP_Get_Tick());
    
    Main_Circulation();
    logDebug("Main_Circulation\n");
}

/******************************** endfile @ main ******************************/
