/******************************************************************************
 * @file     	cmsis_armcc.h
 * @brief     任务初始化注册框架
 * @version   V5.0.4
 * @copyright    
 ******************************************************************************/
#include "task_register.h"


task_array_t tcp_server_task[W5500_MODBUS_NUM];// __attribute__((section (".CCM_RAM")));
//OS_STK tcp_server_stk[W5500_MODBUS_NUM][300];
extern const UintArgFunCall tcp_server_poll[W5500_MODBUS_NUM];
const uint8_t tcp_server_task_name[W5500_MODBUS_NUM][14] = 
{
   "tcp_server1", "tcp_server2",  "tcp_server3",   "tcp_server4",  "tcp_server5",  "tcp_server6",   "tcp_server7",   "tcp_server8"
};

void poll_1ms_task_init(void);
void poll_10ms_task_init(void);


bool is_first_run = false;
static int a = 0;
void task_1ms_handler(uint32_t base_time)
{  
    if(is_first_run == true)
    {
        a++;
    }
	KIT_DEBUG_PRINTF("cur_hv init start \r\n");
	bms_poll_cur_hv(base_time);
}

void task_can1_handler(uint32_t base_time)
{
   protocol_can_poll(base_time, INTER_CAN_PORT);
   protocol_can_poll(base_time, EXTER_CAN_PORT);
}

SN_BMS_INIT_CALL user_task_call;
void task_10ms_handler(uint32_t base_time)
{  
//	static uint32_t cout10ms = 0;
	//cout10ms++;
	bms_poll_signal(base_time);

#ifdef LIB_CREAT
	user_task_call(base_time);
#endif	
}

void bms_poll_bmu_handler(uint32_t base_time)
{
	bms_poll_bmu(base_time);
}

void task_100ms_handler(uint32_t base_time)
{ 
	static uint32_t cout100ms = 0;
	cout100ms++;
    bms_poll_iso(base_time);
    //bms_poll_adc(base_time);
    bms_poll_statistic(base_time);
	bms_poll_run_status(base_time); 
    bms_poll_sop(base_time);
    bms_poll_soh(base_time);
    is_first_run = true;
}


GLOBAL_TASK_VARS_CCM(poll_1ms_task,		4,  10,   	5000,    400, poll_1ms_task_init,		task_1ms_handler,			NULL);
GLOBAL_TASK_VARS_CCM(poll_100ms_task,	1,  100,   	2000,    400, NULL,               		task_100ms_handler,			NULL);

GLOBAL_TASK_VARS_CCM(poll_10ms_task,	3,  100,   	2000,    400, poll_10ms_task_init,   	task_10ms_handler,			NULL);
GLOBAL_TASK_VARS_CCM(ord_ctrl_task,	    2,  100,   	2000,    400, NULL,					 	task_dido_ctrl_handle,		NULL);

GLOBAL_TASK_VARS(modbus_rtu_task1,		6,  50,   	5000,    400, NULL,           			modbus_rtu_inter_handler,	NULL);
GLOBAL_TASK_VARS_CCM(modbus_rtu_task2,	7,  50,   	5000,    400, NULL,           			modbus_rtu_extern_handler,	NULL);
GLOBAL_TASK_VARS_CCM(modbus_rtu_task3,	8,  50,   	5000,    400, NULL,           			modbus_rtu_externII_handler,	NULL);

GLOBAL_TASK_VARS_CCM(ord_comm_can_task, 5,  100,   	5000,    400, NULL,       	 			task_can1_handler,     			NULL);
GLOBAL_TASK_VARS_CCM(bms_poll_bmu_task, 9,  100,   	5000,    400, bms_bmu_init,  			bms_poll_bmu_handler,   		NULL);

#define BMU_FINISH_BIT      (0u)
#define SIGNAL_FINISH_BIT   (1u)

uint8_t is_data_ok = 0;
extern const DoLogic doLogicArray[kDoType_End];
extern const DoLogic bmsCircuitCtrl[kDoCtrlStrategy_End];
void poll_1ms_task_init(void)
{ 
#if (OS_TASK_STAT_EN > 0)
	OSStatInit();
#endif

 #ifdef CPU_CFG_INT_DIS_MEAS_EN
    CPU_IntDisMeasMaxCurReset();
#endif

	bms_init_gpio();
    bms_init_eeprom();
    //bms_init_adc();
    bms_init_tag();
    bms_init_run();
	bms_init_comm();
    bms_init_di_do(bmsCircuitCtrl, kDoCtrlStrategy_End, doLogicArray);
    bsp_create_task(&poll_10ms_task, (uint8_t *)"10ms");
      
    KIT_DEBUG_PRINTF("cur_hv init start \r\n");
	
	//debug here  is no more run 
    bms_init_cur_hv();
	
    KIT_DEBUG_PRINTF("cur_hv init ok \r\n");

    while(KIT_GET_BIT_32(is_data_ok, BMU_FINISH_BIT) == 0)
    {
        bsp_task_delay_ms(10);
    }
    //耗时初始化 ≈300ms 确保信号正常    
    KIT_DEBUG_PRINTF("signal init start \r\n");

    bms_poll_statistic(0);
	
    bms_init_signal();
    //刷新信号策略 确保例如常闭等需要立即闭合do输出
    task_dido_ctrl_handle(1000);
    KIT_DEBUG_PRINTF("signal init ok \r\n");
    KIT_SET_BIT_MASK_32(is_data_ok, SIGNAL_FINISH_BIT);
	bsp_create_task(&ord_ctrl_task, (uint8_t *)"ctrl");
}

void poll_10ms_task_init(void)
{

	uint8_t i = 0;
    bms_init_fdb();
    //耗时初始化 等待从机上电并初始化完成 ≈500ms，且请求两轮数据 ≈1300ms(最大)
    KIT_DEBUG_PRINTF("bmu init start \r\n");
	//bms_init_chain_bmu();
    KIT_DEBUG_PRINTF("bmu init ok \r\n");
    bms_poll_statistic(0);
    //SOC初始化需要统计数据
    bms_init_soc();
    //掉电中断在SOC初始化完成后打开防止存入未初始化值
	drv_misc_cfg_pwr_off_interrupt(APP_CFG_INT_PRIO_PWR_OFF, kPwrOffVolt_2_9, bms_pwr_off_save);
    
    KIT_SET_BIT_MASK_32(is_data_ok, BMU_FINISH_BIT);
    //while(KIT_GET_BIT_32(is_data_ok, SIGNAL_FINISH_BIT) == 0)
    {
        bsp_task_delay_ms(10);
    }
    KIT_DEBUG_PRINTF("data init ok \r\n");

    protocol_comm_init();

    bms_init_sop();
    bms_init_iso();
    //bsp_create_task(&poll_100ms_task, (uint8_t *)"100ms");
	bsp_create_task(&bms_poll_bmu_task, (uint8_t *)"100ms");
    bsp_create_task(&ord_comm_can_task, (uint8_t *)"100ms");
	bsp_create_task(&modbus_rtu_task1, (uint8_t *)"inter_modbus");
    bsp_create_task(&modbus_rtu_task2, (uint8_t *)"exter1_modbus");
	bsp_create_task(&modbus_rtu_task3, (uint8_t *)"exter2_modbus");
	
	/*
	bms_w5500_net_protocol();
    for(i = 0; i < W5500_MODBUS_NUM; i++)
    {
        tcp_server_task[i].task_id = i + 10;
        tcp_server_task[i].stk_size = 300;
        tcp_server_task[i].stk_array = tcp_server_stk[i];
        tcp_server_task[i].interval = 100;
        tcp_server_task[i].timeout = 1000;
        tcp_server_task[i].handler_func = tcp_server_poll[i];
        bsp_create_task(&tcp_server_task[i], (uint8_t *)&tcp_server_task_name[i][0]);
    }
	*/
}


/*****************************************************************************
 * @brief       ucos系统中断回调
 * @param[in]   void: 无
 * @return      void
 *****************************************************************************/
void ucos_irq_handler(void)
{
    OSIntEnter();
    kit_time_beat();
    bsp_task_beat_wdog();
    OSIntExit();
}
