
#include "main.h"
#include "sram.h"
#include "nandflash.h"
#include "usart.h"
#include "malloc.h"
#include "demo_nand_fatfs.h"
#include "cjson.h" 
#include "delay.h"
#include "string.h"
#include "led.h"
#include "log.h"
#include "task.h"
#include "myfile.h"





//设置任务优先级
#define START_TASK_PRIO      	10 //开始任务的优先级设置为最低
//设置任务堆栈大小
#define START_STK_SIZE  	    128
//任务堆栈
OS_STK START_TASK_STK[START_STK_SIZE];
//任务函数
void start_task(void *pdata);
	   
//LED任务
//设置任务优先级
#define LED_TASK_PRIO       	9
//设置任务堆栈大小
#define LED_STK_SIZE  	    	64
//任务堆栈
OS_STK LED_TASK_STK[LED_STK_SIZE];
//任务函数
void led_task(void *pdata);
 
 //网络状态灯任务
#define NET_STATUS_LED_TASK_PRIO       	8
//设置任务堆栈大小
#define NET_STATUS_LED_STK_SIZE  	    	64
//任务堆栈
OS_STK NET_STATUS_LED_TASK_STK[NET_STATUS_LED_STK_SIZE];
//任务函数
void net_status_led_task(void *pdata);

OS_STK DATA_TASK_STK[DATA_STK_SIZE];

void NVIC_Configuration(void);

int main(void){
	delay_init();
	NVIC_Configuration();
	my_mem_init(SRAMEX);
	my_mem_init(SRAMIN);
  USART_232_config();
	LED_GPIO_Config();
	NET_STATUS_LED_Config();
	USART_485_config();

/*
	char *lora_test_json=mymalloc(SRAMEX,9*1024);
	lora_test_json="{\"bandwidth\":250,\"codeRate\":4700,\"commandTopic\":\"/device/297877563234/command\",\"dataRate\":7,\"dataSource\":2,\"deviceId\":\"297877563234\",\"host\":\"36.134.23.11\",\"inboundTime\":1000,\"messageTopic\":\"/device/297877563234\",\"node_1\":{\"device_1\":{\"sensorList\":[{\"identifier\":\"electric_fr\",\"deviceID645\":\"297800000001\",\"identifier645\":41943042},{\"identifier\":\"pActiveEnergy\",\"deviceID645\":\"297800000001\",\"identifier645\":65536},{\"identifier\":\"rActiveEnergy\",\"deviceID645\":\"297800000001\",\"identifier645\":131072},{\"identifier\":\"activePower\",\"deviceID645\":\"297800000001\",\"identifier645\":33751040},{\"identifier\":\"electric_fra\",\"deviceID645\":\"297800000001\",\"identifier645\":33751296},{\"identifier\":\"electric_frb\",\"deviceID645\":\"297800000001\",\"identifier645\":33751552},{\"identifier\":\"electric_frc\",\"deviceID645\":\"297800000001\",\"identifier645\":33751808},{\"identifier\":\"Ua\",\"deviceID645\":\"297800000001\",\"identifier645\":33620224},{\"identifier\":\"Ub\",\"deviceID645\":\"297800000001\",\"identifier645\":33620480},{\"identifier\":\"Uc\",\"deviceID645\":\"297800000001\",\"identifier645\":33620736},{\"identifier\":\"Ia\",\"deviceID645\":\"297800000001\",\"identifier645\":33685760},{\"identifier\":\"Ib\",\"deviceID645\":\"297800000001\",\"identifier645\":33686016},{\"identifier\":\"Ic\",\"deviceID645\":\"297800000001\",\"identifier645\":33686272},{\"identifier\":\"reactivePower\",\"deviceID645\":\"297800000001\",\"identifier645\":33816576},{\"identifier\":\"electric_pqa\",\"deviceID645\":\"297800000001\",\"identifier645\":33619968},{\"identifier\":\"electric_pqb\",\"deviceID645\":\"297800000001\",\"identifier645\":33685504},{\"identifier\":\"electric_pqc\",\"deviceID645\":\"297800000001\",\"identifier645\":33751040},{\"identifier\":\"powerFactor\",\"deviceID645\":\"297800000001\",\"identifier645\":33947648},{\"identifier\":\"electric_pfa\",\"deviceID645\":\"297800000001\",\"identifier645\":33947904},{\"identifier\":\"electric_pfb\",\"deviceID645\":\"297800000001\",\"identifier645\":33948160},{\"identifier\":\"electric_pfc\",\"deviceID645\":\"297800000001\",\"identifier645\":33948416},{\"identifier\":\"innerTemp\",\"deviceID645\":\"297800000001\",\"identifier645\":41943047},{\"identifier\":\"lastMonthEnergy\",\"deviceID645\":\"297800000001\",\"identifier645\":65537},{\"identifier\":\"lastTwoMonthEnergy\",\"deviceID645\":\"297800000001\",\"identifier645\":65538},{\"identifier\":\"totalPostMaximum\",\"deviceID645\":\"297800000001\",\"identifier645\":16842752},{\"identifier\":\"lastPostMaximum\",\"deviceID645\":\"297800000001\",\"identifier645\":16842753}],\"commandList\":[],\"deviceId\":\"297800000001\",\"RFFrequency\":1,\"SignalBw\":9,\"SpreadingFactor\":7,\"ErrorCoding\":1,\"baudrate\":9600,\"dataBit\":8,\"stopBit\":1,\"checkBit\":1,\"protocol\":2},\"device_2\":{\"sensorList\":[{\"identifier\":\"electric_fr\",\"deviceID645\":\"297800000002\",\"identifier645\":41943042},{\"identifier\":\"activePower\",\"deviceID645\":\"297800000002\",\"identifier645\":33751040},{\"identifier\":\"electric_fra\",\"deviceID645\":\"297800000002\",\"identifier645\":33751296},{\"identifier\":\"electric_frb\",\"deviceID645\":\"297800000002\",\"identifier645\":33751552},{\"identifier\":\"electric_frc\",\"deviceID645\":\"297800000002\",\"identifier645\":33751808},{\"identifier\":\"Ua\",\"deviceID645\":\"297800000002\",\"identifier645\":33620224},{\"identifier\":\"Ub\",\"deviceID645\":\"297800000002\",\"identifier645\":33620480},{\"identifier\":\"Uc\",\"deviceID645\":\"297800000002\",\"identifier645\":33620736},{\"identifier\":\"Ia\",\"deviceID645\":\"297800000002\",\"identifier645\":33685760},{\"identifier\":\"Ib\",\"deviceID645\":\"297800000002\",\"identifier645\":33686016},{\"identifier\":\"Ic\",\"deviceID645\":\"297800000002\",\"identifier645\":33686272},{\"identifier\":\"reactivePower\",\"deviceID645\":\"297800000002\",\"identifier645\":33816576},{\"identifier\":\"electric_pqa\",\"deviceID645\":\"297800000002\",\"identifier645\":33619968},{\"identifier\":\"electric_pqb\",\"deviceID645\":\"297800000002\",\"identifier645\":33685504},{\"identifier\":\"electric_pqc\",\"deviceID645\":\"297800000002\",\"identifier645\":33751040},{\"identifier\":\"powerFactor\",\"deviceID645\":\"297800000002\",\"identifier645\":33947648},{\"identifier\":\"electric_pfa\",\"deviceID645\":\"297800000002\",\"identifier645\":33947904},{\"identifier\":\"electric_pfb\",\"deviceID645\":\"297800000002\",\"identifier645\":33948160},{\"identifier\":\"electric_pfc\",\"deviceID645\":\"297800000002\",\"identifier645\":33948416},{\"identifier\":\"innerTemp\",\"deviceID645\":\"297800000002\",\"identifier645\":41943047},{\"identifier\":\"lastMonthEnergy\",\"deviceID645\":\"297800000002\",\"identifier645\":65537},{\"identifier\":\"lastTwoMonthEnergy\",\"deviceID645\":\"297800000002\",\"identifier645\":65538},{\"identifier\":\"totalPostMaximum\",\"deviceID645\":\"297800000002\",\"identifier645\":16842752},{\"identifier\":\"lastPostMaximum\",\"deviceID645\":\"297800000002\",\"identifier645\":16842753}],\"commandList\":[],\"deviceId\":\"297800000002\",\"RFFrequency\":1,\"SignalBw\":9,\"SpreadingFactor\":7,\"ErrorCoding\":1,\"baudrate\":9600,\"dataBit\":8,\"stopBit\":1,\"checkBit\":1,\"protocol\":2},\"nodeId\":\"1F6E\",\"RFFrequency\":1,\"SignalBw\":9,\"SpreadingFactor\":7,\"ErrorCoding\":1,\"baudrate\":9600,\"dataBit\":8,\"stopBit\":1,\"checkBit\":1,\"protocol\":2},\"node_2\":{\"device_1\":{\"sensorList\":[{\"registerByteNum\":4,\"identifier\":\"WaterConsumption\",\"rFunctionCode\":3,\"registerAddress\":0,\"slaveAddress\":4,\"bigLittleFormat\":1,\"precise\":2,\"dataType\":1},{\"registerByteNum\":1,\"identifier\":\"WaterOutletSwitch\",\"rFunctionCode\":1,\"registerAddress\":0,\"slaveAddress\":4,\"bigLittleFormat\":1,\"precise\":0,\"dataType\":1}],\"commandList\":[{\"registerByteNum\":1,\"identifier\":\"WaterOutletSwitch\",\"registerAddress\":0,\"slaveAddress\":4,\"bigLittleFormat\":1,\"wFunctionCode\":5,\"dataType\":1}],\"deviceId\":\"lfklio59\",\"RFFrequency\":1,\"SignalBw\":9,\"SpreadingFactor\":7,\"ErrorCoding\":1,\"baudrate\":9600,\"dataBit\":8,\"stopBit\":1,\"checkBit\":1,\"protocol\":3},\"device_2\":{\"sensorList\":[{\"registerByteNum\":4,\"identifier\":\"WaterConsumption\",\"rFunctionCode\":3,\"registerAddress\":0,\"slaveAddress\":5,\"bigLittleFormat\":1,\"precise\":2,\"dataType\":1},{\"registerByteNum\":1,\"identifier\":\"WaterOutletSwitch\",\"rFunctionCode\":1,\"registerAddress\":0,\"slaveAddress\":5,\"bigLittleFormat\":1,\"precise\":0,\"dataType\":1}],\"commandList\":[{\"registerByteNum\":1,\"identifier\":\"WaterOutletSwitch\",\"registerAddress\":0,\"slaveAddress\":5,\"bigLittleFormat\":1,\"wFunctionCode\":5,\"dataType\":1}],\"deviceId\":\"tncecx2m\",\"RFFrequency\":1,\"SignalBw\":9,\"SpreadingFactor\":7,\"ErrorCoding\":1,\"baudrate\":9600,\"dataBit\":8,\"stopBit\":1,\"checkBit\":1,\"protocol\":3},\"nodeId\":\"2A73\",\"RFFrequency\":1,\"SignalBw\":9,\"SpreadingFactor\":7,\"ErrorCoding\":1,\"baudrate\":9600,\"dataBit\":8,\"stopBit\":1,\"checkBit\":1},\"pollTime\":1000,\"port\":1883,\"power\":22,\"preambleLen\":0,\"version645\":1}";
	write_file("lora_json.txt",lora_test_json,strlen(lora_test_json));
	myfree(SRAMEX,lora_test_json);
*/	
	//ucos初始化
	OSInit();
	OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
	OSStart(); 
	
  /* Infinite loop */
  while (1)
  {
		
  }
	
}
void start_task(void *pdata)
{
	OS_CPU_SR cpu_sr;
	pdata = pdata ;
	OSStatInit();  			//初始化统计任务
	OS_ENTER_CRITICAL();  	//关中断
#if LWIP_DHCP
	lwip_comm_dhcp_creat(); //创建DHCP任务
	OSTimeDlyHMSM(0,0,2,500);
#endif  
	OSTaskCreate(led_task,(void*)0,(OS_STK*)&LED_TASK_STK[LED_STK_SIZE-1],LED_TASK_PRIO);//创建LED任务
	OSTaskCreate(net_status_led_task,(void*)0,(OS_STK*)&NET_STATUS_LED_TASK_STK[NET_STATUS_LED_STK_SIZE-1],NET_STATUS_LED_TASK_PRIO);//创建LED任务
	
	OSTaskCreate(data_task,(void*)0,(OS_STK*)&DATA_TASK_STK[DATA_STK_SIZE-1],DATA_TASK_PRIO);
	
	OSTaskSuspend(OS_PRIO_SELF); 	//挂起start_task任务
	OS_EXIT_CRITICAL();  			//开中断
	
}
//测试任务
void led_task(void *pdata)
{
	OS_CPU_SR cpu_sr;
	pdata = pdata ;
	while(1)
	{
		LED(ON);	
		OSTimeDlyHMSM(0,0,0,500); 
		LED(OFF);
		OSTimeDlyHMSM(0,0,0,500);
	}
}
void net_status_led_task(void *pdata)
{
	OS_CPU_SR cpu_sr;
	pdata = pdata ;
	while(1)
  {
		NET_STATUS_LED(ON);
		OSTimeDlyHMSM(0,0,0,500);
		NET_STATUS_LED(OFF);
		OSTimeDlyHMSM(0,0,0,500);
	}
	
}



void bsp_Idle(void)
{
	/* --- 喂狗 */

	/* --- 让CPU进入休眠，由Systick定时中断唤醒或者其他中断唤醒 */
}
 /**
  * @brief  配置嵌套向量中断控制器NVIC分组
  * @param  无
  * @retval 无
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* 嵌套向量中断控制器组选择 */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  
}
