/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.1
 * Date               : 2020/08/06
 * Description        : Peripheral slave application main function and task system initialization
 *********************************************************************************
 * 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.
 *******************************************************************************/

/******************************************************************************/
/* Header file contains */
#include "define.h"
#include "gattprofile.h"
#include "peripheral.h"
#include "user_config.h"
#include "tools.h"
#include "dip_sw.h"
#include "ble_action.h"
#include "user_elect.h"
#include "elect_sw.h"
#include "user_nfc.h"
#include "user_can.h"
#include "user_server.h"
#include "user_ws2812b.h"
#include "user_led.h"

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

uint8_t MacAddr[6] = {0};

/* Used for app judgment file effectiveness */
const uint32_t Address = 0xFFFFFFFF;

__attribute__((aligned(4))) uint32_t Image_Flag __attribute__((section(".ImageFlag"))) = (uint32_t)&Address;

/*********************************************************************
 * @fn      Main_Circulation
 *
 * @brief   Main loop
 *
 * @return  none
 */
__attribute__((section(".highcode")))
__attribute__((noinline))
void Main_Circulation(void)
{
    while(1)
    {
	      IWDG_ReloadCounter();	//Feed dog
        TMOS_SystemProcess();
    }
}

static void func_init(void)
{
  set_download_ble_data_func(download_ble_data_callback);
  set_download_ble_file_func(download_ble_file_callback);
  set_upload_ble_data_func(ble_data_send);
  set_ble_connect_evtfunc(ble_connect_act);
}

static void task_init(void)
{
  vUser_server_init();
  vBle_action_init();
  vUser_can_init();
  vDipsw_init();
  vElectsw_init();
  elect_task_init();
  vUser_nfc_init();
  vUser_ws2812b_init(USER_WS2812B_COUNT_DEFAULT);
  led_task_init();
}

static void task_start(void)
{
  vUser_ws2812b_task_start();
  dipsw_task_start();
  elect_task_start();
  electsw_task_start();
  vUser_nfc_task_start();
  vUser_server_task_start();
  led_task_start();
}

/*********************************************************************
 * @fn      main
 *
 * @brief   Main function
 *
 * @return  none
 */
int main(void)
{
//    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                //优先级分组4，所有位均用于抢占优先级
    SystemCoreClockUpdate();
    Delay_Init();
#ifdef DEBUG
    USART_Printf_Init(115200);
#endif
    PRINT("%s\nSystemClk:%d\r\n", VER_LIB, SystemCoreClock);
    PRINT( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
    rng_init(0);
    user_config_init();
    func_init();
    WCHBLE_Init();
    HAL_Init();
    GAPRole_PeripheralInit();
    Peripheral_Init();
    task_init();
    task_start();
    // set_led_indication(DEV_LED, WS2812B_NORMAL_RGB, LED_WORK_INVERT, 5000, 2500, 0);
    Main_Circulation();
}

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