/**
	************************************************************
	************************************************************
	************************************************************
	*	ļ 	m6312.c
	*
	*	ߣ 		ż
	*
	*	ڣ 		2017-10-20
	*
	*	汾 		V1.0
	*
	*	˵ 		M6312
	*
	*	޸ļ¼	
	************************************************************
	************************************************************
	************************************************************
**/

//Ƭͷļ
#include "stm32f10x.h"

//豸
#include "m6312.h"

//Ӳ
#include "delay.h"
#include "usart.h"

//C
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


unsigned char m6312_buf[512];
unsigned short m6312_cnt = 0, m6312_cntPre = 0;


//==========================================================
//	ƣ	M6312_Clear
//
//	ܣ	ջ
//
//	ڲ	
//
//	ز	
//
//	˵		
//==========================================================
void M6312_Clear(void)
{

	memset(m6312_buf, 0, sizeof(m6312_buf));
	m6312_cnt = 0;

}

//==========================================================
//	ƣ	M6312_WaitRecive
//
//	ܣ	ȴ
//
//	ڲ	
//
//	ز	REV_OK-		REV_WAIT-ճʱδ
//
//	˵		ѭüǷ
//==========================================================
_Bool M6312_WaitRecive(void)
{

	if(m6312_cnt == 0) 							//ռΪ0 ˵ûдڽУֱ
		return REV_WAIT;
		
	if(m6312_cnt == m6312_cntPre)				//һεֵͬ˵
	{
		m6312_cnt = 0;							//0ռ
			
		return REV_OK;							//ؽɱ־
	}
		
	m6312_cntPre = m6312_cnt;					//Ϊͬ
	
	return REV_WAIT;							//ؽδɱ־

}

//==========================================================
//	ƣ	M6312_SendCmd
//
//	ܣ	
//
//	ڲ	cmd
//				resҪķָ
//
//	ز	0-ɹ	1-ʧ
//
//	˵		
//==========================================================
_Bool M6312_SendCmd(char *cmd, char *res)
{
	
	unsigned char timeOut = 200;

	Usart_SendString(USART2, (unsigned char *)cmd, strlen((const char *)cmd));
	
	while(timeOut--)
	{
		if(M6312_WaitRecive() == REV_OK)							//յ
		{
			if(strstr((const char *)m6312_buf, res) != NULL)		//ؼ
			{
				M6312_Clear();										//ջ
				
				return 0;
			}
		}
		
		DelayXms(10);
	}
	
	return 1;

}

//==========================================================
//	ƣ	M6312_SendData
//
//	ܣ	
//
//	ڲ	data
//				len
//
//	ز	
//
//	˵		
//==========================================================
void M6312_SendData(unsigned char *data, unsigned short len)
{

	char cmdBuf[32];
	
	M6312_Clear();								//սջ
	sprintf(cmdBuf, "AT+IPSEND=%d\r\n", len);	//
	if(!M6312_SendCmd(cmdBuf, ">"))				//յ>ʱԷ
	{
		Usart_SendString(USART2, data, len);	//豸
	}

}

//==========================================================
//	ƣ	M6312_GetIPD
//
//	ܣ	ȡƽ̨ص
//
//	ڲ	timeOutȴʱ(10ms)
//
//	ز	ƽ̨صԭʼ
//
//	˵		ͬ豸صĸʽͬҪȥ
//==========================================================
unsigned char *M6312_GetIPD(unsigned short timeOut)
{

	char *ptrIPD;
	
	do
	{
		if(M6312_WaitRecive() == REV_OK)								//
		{
			ptrIPD = strstr((char *)m6312_buf, "IPDATA:");				//IPDATAͷ
			if(ptrIPD == NULL)											//ûҵIPDATAͷӳ٣Ҫȴһᣬᳬ趨ʱ
			{
				//UsartPrintf(USART_DEBUG, "\"IPD\" not found\r\n");
			}
			else
			{
				ptrIPD = strchr(ptrIPD, '\n');							//ҵ'\n'
				if(ptrIPD != NULL)
				{
					ptrIPD++;
					return (unsigned char *)(ptrIPD);
				}
				else
					return NULL;
			}
		}
		
		DelayXms(10);													//ʱȴ
	} while(timeOut--);
	
	return NULL;														//ʱδҵؿָ

}

//==========================================================
//	ƣ	M6312_Init
//
//	ܣ	ʼM6312
//
//	ڲ	
//
//	ز	
//
//	˵		
//==========================================================
void M6312_Init(void)
{
	
	GPIO_InitTypeDef GPIO_Initure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);

	//M6312λ
	GPIO_Initure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Initure.GPIO_Pin = GPIO_Pin_15;				//GPIOC15-λ
	GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_Initure);
	
	//M6312Դ
	GPIO_Initure.GPIO_Pin = GPIO_Pin_4;					//GPIOC4-PowerKey
	GPIO_Init(GPIOC, &GPIO_Initure);
	
	//M6312״̬
	GPIO_Initure.GPIO_Mode = GPIO_Mode_IPD;
	GPIO_Initure.GPIO_Pin = GPIO_Pin_5;					//GPIOC5-status
	GPIO_Init(GPIOC, &GPIO_Initure);
	
	GPIO_WriteBit(GPIOC, GPIO_Pin_4, Bit_SET);			//ϵ
	
	while(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5))
		DelayXms(10);
	
	GPIO_WriteBit(GPIOC, GPIO_Pin_15, Bit_SET);			//λ
	DelayXms(250);
	GPIO_WriteBit(GPIOC, GPIO_Pin_15, Bit_RESET);
	DelayXms(500);
	
	M6312_Clear();

	UsartPrintf(USART_DEBUG, "1. AT\r\n");
	while(M6312_SendCmd("AT\r\n", "OK"))
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "2. AT+CPIN?\r\n");
	while(M6312_SendCmd("AT+CPIN?\r\n", "+CPIN: READY"))		//ȷSIMPINREADYʾɹ
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "3. AT+CREG?\r\n");
	while(M6312_SendCmd("AT+CREG?\r\n", "1,1"))
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "4. AT+CGREG?\r\n");
	while(M6312_SendCmd("AT+CGREG?\r\n","OK"))					//ע״̬
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "5. AT+CGACT=1,1\r\n");
	while(M6312_SendCmd("AT+CGACT=1,1\r\n","OK"))				//
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "6. AT+CGATT=1\r\n");
	while(M6312_SendCmd("AT+CGATT=1\r\n","OK"))					//GPRSҵ
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "7. AT+CMMUX=0\r\n");
	while(M6312_SendCmd("AT+CMMUX=0\r\n","OK"))					//ΪӣȻƽ̨IP
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "8. AT+CMHEAD=1\r\n");
	while(M6312_SendCmd("AT+CMHEAD=1\r\n","OK"))				//ʾIPͷ
		DelayXms(500);
	
	UsartPrintf(USART_DEBUG, "9. M6312 Init OK\r\n");

}

//==========================================================
//	ƣ	USART2_IRQHandler
//
//	ܣ	2շж
//
//	ڲ	
//
//	ز	
//
//	˵		
//==========================================================
void USART2_IRQHandler(void)
{

	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //ж
	{
		if(m6312_cnt >= sizeof(m6312_buf))	m6312_cnt = 0; //ֹڱˢ
		m6312_buf[m6312_cnt++] = USART2->DR;
		
		USART_ClearFlag(USART2, USART_FLAG_RXNE);
	}

}
