#include "lorawan.h"
#include "USARTSetup.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "stm32f10x_dma.h"

uint8_t Recive_lorawanID_flag=0;  //接收LORAWAN ID号


uint16_t send_cont=0;   //数据发送次数
uint8_t  init_sent=0;   //初始化顺序完成次数
uint8_t lorawan_init_flag=0;//lorawan初始化完成标志
uint8_t lorawan_send_data_flag=0;////数据发送标志

extern  uint8_t lorawan_overtime_flag;
extern  uint16_t lorawan_overtime;
extern  uint8_t lorawan_send_finish_flag;       //数据发送完成标志
extern  uint8_t  lorawan_time_5sflag;                     //5秒时间标志
extern  uint16_t lorawan_time_5s;                         //5s时间,发送数据后，收不到发送完成标志，延时3s进入睡眠模式
extern  uint8_t lorawan_to_sleep;            //lorawan模块进入睡眠标志
/*int fputc(int ch, FILE *f)

{

USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等

while (!(USART1->SR & USART_FLAG_TXE));

return (ch);

}*/
/**************************
 函数名：void delay(unsigned int a)   
函数功能： 1ms延时函数
输入变量： a 代表几ms
输出变量： 无

***************************/
void LoraNode_Delay(uint32_t a)   //ms延时函数
{ 
		unsigned int i,j;  
		for(j=0;j<a;j++)
			{ 
		     for(i=0;i<1500;i++); 
			} 
} 
void LoraNode_GPIO_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct; 	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
	/* enable clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	/* gpio config */
	GPIO_InitStruct.GPIO_Pin 	= LORANODE_WAKE_PIN|LORANODE_MODE_PIN;
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_Out_PP;
	//GPIO_InitStruct.Pull 	= GPIO_NOPULL;
	GPIO_InitStruct.GPIO_Speed 	= GPIO_Speed_50MHz;	
	GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin 	= LORANODE_NRST_PIN;
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_Out_PP;
	//GPIO_InitStruct.Pull 	= GPIO_NOPULL;
	GPIO_InitStruct.GPIO_Speed 	= GPIO_Speed_50MHz;	
	GPIO_Init(GPIOA, &GPIO_InitStruct);

	GPIO_InitStruct.GPIO_Pin 	= LORANODE_STAT_PIN;
	//GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_IN_FLOATING;
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_IPD;   //下拉输入
	//GPIO_InitStruct.Pull 	= GPIO_NOPULL;
	GPIO_InitStruct.GPIO_Speed 	= GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin 	= LORANODE_BUSY_PIN;
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_IN_FLOATING;
	//GPIO_InitStruct.Pull 	= GPIO_NOPULL;
	GPIO_InitStruct.GPIO_Speed 	= GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);

}

void LoraNode_Init_Mode(LoraNode_Mode_T mode)
{
	

	//LoraNode_Wake_Sleep(MODE_WAKEUP);
	LoraNode_Reset();
	
	
}


//----------------------------------------------------------------------
//	@funtion:	set the lorawan module mode, wakeup or sleep mode
//	@paramters:	MODE_WAKEUP, or MODE_SLEEP
//	@return:	void
//-------------------------------------------------------------------
void LoraNode_Wake_Sleep(LoraNode_SleepMode_T mode)
{
	if (mode == MODE_WAKEUP)
	{		
		// wake signal, high for module wakeup, low for module sleep 	
		if (GPIO_ReadOutputDataBit(LORANODE_WAKE_GPIO_PORT,LORANODE_WAKE_PIN) != Bit_SET)
		{
			// wake module first, and wait 10 ms 
			LORANODE_WAKE_HIGH();			
			LoraNode_Delay(20);				
		}
	}
	
	if (mode == MODE_SLEEP)
	{
		if (GPIO_ReadOutputDataBit(LORANODE_WAKE_GPIO_PORT,LORANODE_WAKE_PIN) != Bit_RESET)
		{
			LORANODE_WAKE_LOW();			
		}
	}
}


static void LoraNode_Reset(void)
{
	LORANODE_NRST_LOW();	
	
	LoraNode_Delay(300);	//15ms
	
	LORANODE_NRST_HIGH();	
}


void transparent_send_data(uint8_t *DataBuf, uint32_t xmitLen)
{
	
	//----唤醒模块 ----
	LoraNode_Wake_Sleep(MODE_WAKEUP);

	LoraNode_Delay(100);

	
	USART2Send(DataBuf, xmitLen);
	send_cont++;
	lorawan_send_data_flag=1;      //数据发送标志
	lorawan_time_5s=0;             //发送数据后，开始计时
	lorawan_time_5sflag=0;         //5s定时晚上
	
	lorawan_overtime_flag=0;       //清除超时标志
	lorawan_overtime=2000;        //重新赋值超时寄存器,2s超时

}

//-----------------
//数据发送后，判断lorawan模块是否发送完成，完成就进入睡眠模式，
//收不到完成标志，为降低功耗，最多延时5s进入睡眠模式
//------------------
void lorawan_Sleep(void)  
{
	if(lorawan_send_data_flag != 1)
		return;
	//if((lorawan_send_finish_flag == 1) || (lorawan_time_5sflag == 1))
	if((lorawan_to_sleep == 1) && (lorawan_time_5sflag == 1))
	{
		lorawan_to_sleep=0;
		lorawan_send_finish_flag=0;
		LoraNode_Wake_Sleep(MODE_SLEEP);
		lorawan_send_data_flag=0; //进入睡眠后，清除发送标志
		lorawan_time_5sflag=0;    //进入睡眠后，清除5s标志
		lorawan_time_5s=0;        //进入睡眠后，清除5s倒计时寄存器
	}
}

/* Over-the-Air Activation */
void LoraNode_OTAA_Config(void)
{

	  LoraNode_Init_Mode(MODE_CMD);
 
}

