/*!
    \file  main.c
    \brief enet demo 

    \version 2017-02-10, V1.0.0, firmware for GD32F30x
    \version 2018-10-10, V1.1.0, firmware for GD32F30x
    \version 2018-12-25, V2.0.0, firmware for GD32F30x
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f30x.h"
#include "hd_eth.h"
#include "main.h"
#include "tcpip.h"

#include "hello_gigadevice.h"
#include "tcp_client.h"
#include "led.h"
#include "usart.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include "netconf.h"
#include "sys_mqtt.h"
#include "tcp_server.h"
#include "log.h"
#include "gd32_flash.h"
#include "timer.h"

#include "sys_http.h"
#include "data_task.h"
#include "mmodbus.h"

/*
*freertos优先级管理
*管理启动流程任务 init_task        	优先级 1
*freertos时间任务 prvTimerTask     	优先级 2
*上位机交互任务  	tcp_server        优先级 1
*可能会开启的任务 dhcp_task        	优先级 4
*mqtt通信任务			mqtt_publish_task 优先级 4
*以太网输入任务   ethernetif_input 	优先级 5
*数据处理任务     data_task         优先级 3
*/
#define INIT_TASK_PRIO   ( tskIDLE_PRIORITY + 1 )
#define DHCP_TASK_PRIO   ( tskIDLE_PRIORITY + 4 )
#define LED_TASK_PRIO    ( tskIDLE_PRIORITY + 2 )



 
void led_task(void * pvParameters); 
void init_task(void * pvParameters);
char  gatewayId[11];

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
		sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
	   /* configure 4 bits pre-emption priority */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
		gd_485_DE_pin_init();
		config_485_port(COM_485,115200,8,1,0);
		
		timer5_init();

    /* init task */
    xTaskCreate(init_task, "INIT",256, NULL, INIT_TASK_PRIO, NULL);
    /* start scheduler */
    vTaskStartScheduler();
    while(1)
		{
			
    }
		
}

/*!
    \brief      init task
    \param[in]  pvParameters not used
    \param[out] none
    \retval     none
*/
int fttime1, fttime2;
void init_task(void * pvParameters)
{
//		gd32_flash_test();
    /* configure ethernet (GPIOs, clocks, MAC, DMA) */ 
    enet_system_setup();
    /* initilaize the LwIP stack */
		lwip_stack_init();
		log_init();
    /* initilaize the tcp server: telnet 8000 */
//    hello_gigadevice_init();
    /* initilaize the tcp client: echo 1026 */
//    tcp_client_init();
    /* initilaize the udp: echo 1025 */
	 tcp_server_init();
#ifdef USE_DHCP
    /* start DHCP client */
    xTaskCreate(dhcp_task, "DHCP", 512, NULL, DHCP_TASK_PRIO, NULL);
#endif /* USE_DHCP */
		
    /* start toogle LED task every 250ms */
//    xTaskCreate(led_task, "LED", 512, NULL, LED_TASK_PRIO, NULL);
		http_getDemo();
		xTaskCreate(data_task, "DATA",1024, NULL, DATA_TASK_PRIO, NULL);
		mqtt_task_creat();
    for( ;; ){
        vTaskDelete(NULL);
    }
}

/*!
    \brief      led task
    \param[in]  pvParameters not used
    \param[out] none
    \retval     none
*/

void led_task(void * pvParameters)
{  
    for( ;; ){
        /* toggle LED3 each 250ms */
        gd_eval_led_toggle(LED_DEV);
        vTaskDelay(250);
    }
}



void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName )
{
	LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s 栈溢出\r\n",pcTaskName);
}
void vApplicationHeapOverflowHook(TaskHandle_t xTask, char *pcTaskName )
{
	LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s 堆溢出\r\n",pcTaskName);
}
void vApplicationMallocFailedHook()
{
		LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"malloc error");
}
