//////////////////////////////////////////////////////////////////////
// SerialPro.cpp: implementation of the CSerialPro class.
//负责与串行编程器通讯，对编程器发令
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EPro.h"
#include "SerialPro.h"
#include "Pro.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSerialPro::CSerialPro()
{
	m_bCanUse=FALSE;
	m_bPort=FALSE;
	m_bThread=FALSE;
	m_hReadRsp=CreateEvent(NULL,FALSE,FALSE,NULL);
	m_hWriteRsp=CreateEvent(NULL,FALSE,FALSE,NULL);
}

CSerialPro::~CSerialPro()
{

}

//发送18个字节，第1个字节表示操作类型，第2个字节表示器件的FID，
//后16个字节是与该操作相关的数据
BOOL CSerialPro::SendData()
{
	if(!m_bPort)
	{
		m_pPro->Notify(PRO_INIT_SERIAL_NOT);
		return FALSE;
	}
	if(m_bCanUse)
	{
		TRACE("[0]%2x [1]%2x [2]%2x [3]%2x [4]%2x [5]%2x [6]%2x [7]%2x\n",TxdBuf[0],TxdBuf[1],TxdBuf[2],TxdBuf[3],TxdBuf[4],TxdBuf[5],TxdBuf[6],TxdBuf[7]);
		SendBlock(TxdBuf,18);
		m_bCanUse=FALSE;
		return TRUE;
	}
	else
	{
		m_pPro->Notify(PRO_INVALID);
		return FALSE;
	}
}

//接收完指定长度数据块后的处理
void CSerialPro::OnReceived(BYTE *pBuf, UINT nLength)
{
	static int n=0;
	n++;
	TRACE("%d-OnReceived %d BYTES\n",n,nLength);
	TRACE("[0]%2x [1]%2x [2]%2x [3]%2x [4]%2x [5]%2x [6]%2x [7]%2x\n",pBuf[0],pBuf[1],pBuf[2],pBuf[3],pBuf[4],pBuf[5],pBuf[6],pBuf[7]);
	if(nLength==18)
	{
		switch(pBuf[0])//是什么操作的回应
		{
		case 0://编程器就绪
			m_bCanUse=TRUE;
			m_pPro->Notify(PRO_AVAILABLE);
			return;
		case 1://编程器读入特征字
			m_bCanUse=TRUE;
			m_pPro->SignByte[0]=pBuf[2];
			m_pPro->SignByte[1]=pBuf[3];
			m_pPro->SignByte[2]=pBuf[4];
			m_pPro->CheckChip();//检查是否可以找到与特征字匹配的器件
			return;
		case 2://编程器插除器件
			m_bCanUse=TRUE;
			m_pPro->Notify(PRO_ERASE);
			return;
		case 3:
			//编程器等待写器件的回应，建立写ROM的线程
			TRACE("m_bThread=%d\n",m_bThread);
			if(m_bThread==FALSE?(AfxBeginThread(WriteRomThread,this)!=NULL):FALSE)
				return;
		case 4:
			//编程器等待读器件的回应，建立读ROM的线程
			TRACE("m_bThread=%d\n",m_bThread);
			if(m_bThread==FALSE?(AfxBeginThread(ReadRomThread,this)!=NULL):FALSE)
				return;
		case 5://编程器写锁定位完成
			m_bCanUse=TRUE;
			m_pPro->Notify(PRO_LOCK_BIT);
			return;
		case 6://读出器件的数据
		 if(m_bThread)
		 {
			SetEvent(m_hReadRsp);//用事件通知读线程
		 }
			return;
		case 0xff://编程器收到无效的命令
			m_bCanUse=TRUE;
			m_pPro->Notify(PRO_WORK_INVALID);
			return;
		default:
			m_bCanUse=TRUE;
			break;
		}
	}
	else if(nLength==1 && m_bThread)//如果只接收到1个字节，是否给写线程的 
	{
		//为提高通讯效率，编程器写完一块数据后回应一个字节
		//表示是否成功写入
		SetEvent(m_hWriteRsp);//用事件通知写线程
	}
	else//通讯出错了
	{
		m_pPro->Notify(PRO_RCV_ER);
		m_bCanUse=FALSE;
	}
}


BOOL CSerialPro::InitPort(CPro* pPro, UINT nPort, UINT nBaud)
{
	m_pPro=pPro;
	if(m_bPort)
	{
		StopMonitoring();//如果以前占用了串口，先关闭监听串口
	}
	//初始化串口通讯
	m_bPort=CSerialPort::InitPort((CWnd*)pPro->m_pDlg,nPort,nBaud,'N');
	if(m_bPort)
	{
		SetExpRcv(18);//设置期望的数据块长度
		StartMonitoring();//监听串口
		m_pPro->Notify(PRO_INIT_SERIAL_SEC);
		TRACE("CSerialPort::InitPort-PASS\n");
	}
	else
	{
		m_pPro->Notify(PRO_INIT_SERIAL_ER);
		TRACE("CSerialPort::InitPort-ERROR\n");
	}
	return m_bPort;
}

void CSerialPro::DetectPro(BYTE FID)
{
	TRACE("DetectPro(%d)\n",FID);
	TxdBuf[0]=0;
	TxdBuf[1]=FID;
	m_bCanUse=TRUE;
	SendData();
}

void CSerialPro::ReadSign(BYTE FID)
{
	TRACE("ReadSign(%d)\n",FID);
	TxdBuf[0]=1;
	TxdBuf[1]=FID;
	SendData();
}

void CSerialPro::Erase(BYTE FID)
{
	TRACE("Erase(%d)\n",FID);
	TxdBuf[0]=2;
	TxdBuf[1]=FID;
	SendData();	
}

//发送开始写器件的命令，让编程器做好准备
void CSerialPro::BeginWrite(BYTE FID)
{
	TRACE("BeginWrite(%d)\n",FID);
	TxdBuf[0]=3;
	TxdBuf[1]=FID;
	SendData();
}

//发送开始读器件的命令，让编程器做好准备
void CSerialPro::BeginRead(BYTE FID)
{
	TRACE("BeginWrite(%d)\n",FID);
	TxdBuf[0]=4;
	TxdBuf[1]=FID;
	SendData();
}

void CSerialPro::LockBit(BYTE FID,int nBit)
{//写锁定位，nBit为锁定位数
	TRACE("LockBit(%d,%d)\n",FID,nBit);
	TxdBuf[0]=5;
	TxdBuf[1]=FID;
	TxdBuf[2]=(BYTE)nBit;
	SendData();
}

//写器件的线程
UINT CSerialPro::WriteRomThread(LPVOID pParam)
{
	TRACE("WriteRomThread(LPVOID pParam)\n");
	CSerialPro* pSerialPro=(CSerialPro*)pParam;
	pSerialPro->m_bThread=TRUE;
	CPro* pPro=pSerialPro->m_pPro;
	pPro->Notify(PRO_WRITE_BEGIN);
	pPro->m_nAddr=0;
	pPro->m_nDataLength=pPro->GetMinLength(pPro->m_pWriteBuf,0x10000);
	//得到缓冲中最小数据块长度，跳过写0xff
	if(pPro->m_nDataLength==0)//如果数据块长度为0，直接退出
	{
		TRACE("WriteRomThread(...) Over\n");
		pSerialPro->m_bThread=FALSE;
		pSerialPro->TxdBuf[0]=0;//通知编程器写结束
		pSerialPro->SendBlock(pSerialPro->TxdBuf,18);
		pPro->Notify(PRO_WRITE_OVER);
		return 0;
	}
	pSerialPro->SetExpRcv(1);//设置只要接收一个字节
	Sleep(5);
	ResetEvent(pSerialPro->m_hWriteRsp);//事件复位
	while(pPro->m_nAddr<pPro->m_nDataLength)
	{//只要ROM地址指针小于数据块长度，就继续写
		TRACE("pPro->m_nAddr:%04x pPro->m_nDataLength:%04x\n",pPro->m_nAddr,pPro->m_nDataLength);
		pSerialPro->TxdBuf[0]=0x07;//表示继续写
		memcpy(&pSerialPro->TxdBuf[2],&pPro->m_pWriteBuf[pPro->m_nAddr],0x10);
		//给发送缓冲填充要写的数据
		pSerialPro->SendBlock(pSerialPro->TxdBuf,18);//发送
		TRACE("WaitForSingleObject(...)\n");
		if(WaitForSingleObject(pSerialPro->m_hWriteRsp,1000)==WAIT_TIMEOUT)
		{//等待编程器回应超时
			TRACE("WaitForSingleObject(...) TimeOut\n");
			pSerialPro->SetExpRcv(18);
			pPro->Notify(PRO_RCV_ER);
			pSerialPro->m_bThread=FALSE;
			pSerialPro->m_bCanUse=TRUE;
			return -1;
			
		}
		TRACE("WaitForSingleObject(...) OVER\n");
		if(pSerialPro->RxdBuf[0]==0x01)
		{//写数据成功
			pPro->m_nAddr+=0x10;
			pPro->Notify(PRO_WRITE_BLOCK);
		}
		else if(pSerialPro->RxdBuf[0]==0xff)
		{//写数据失败
			TRACE("WriteRomThread(...) ERROR\n");
			pSerialPro->SetExpRcv(18);
			pSerialPro->TxdBuf[0]=0;
			pSerialPro->SendBlock(pSerialPro->TxdBuf,18);
			pPro->Notify(PRO_WRITE_ER);
			pSerialPro->m_bThread=FALSE;
			pSerialPro->m_bCanUse=TRUE;
			return -1;
		}
		else//通讯出错
		{
			TRACE("WriteRomThread(...) COM ER\n");
			pSerialPro->SetExpRcv(18);
			pPro->Notify(PRO_RCV_ER);
			pSerialPro->m_bThread=FALSE;
			pSerialPro->m_bCanUse=TRUE;
			return -1;
		}
	}
	pSerialPro->SetExpRcv(18);//还原
	pSerialPro->TxdBuf[0]=0;//通知编程器写结束
	pSerialPro->SendBlock(pSerialPro->TxdBuf,18);
	pPro->Notify(PRO_WRITE_OVER);
	TRACE("WriteRomThread(...) SEC\n");
	pSerialPro->m_bThread=FALSE;
	pSerialPro->m_bCanUse=TRUE;
	return 0;
}

//读器件的线程
UINT CSerialPro::ReadRomThread(LPVOID pParam)
{
	TRACE("ReadRomThread(LPVOID pParam)\n");
	CSerialPro* pSerialPro=(CSerialPro*)pParam;
	pSerialPro->m_bThread=TRUE;
	CPro* pPro=pSerialPro->m_pPro;
	pPro->Notify(PRO_READ_BEGIN);
	BYTE RespByte=1;
	BOOL bReWait=FALSE;
	pPro->m_nAddr=0;
	if(!pPro->m_bCheck)//如果不是效验，则读整个ROM
		pPro->m_nDataLength=pPro->GetCurChip()->RomSize*1024;
	if(pPro->m_nDataLength==0)
	{
		pPro->Notify(PRO_READ_OVER);
		TRACE("ReadRomThread(...) Over\n");
		pSerialPro->m_bThread=FALSE;
		return 0;
	}
	ResetEvent(pSerialPro->m_hReadRsp);//事件复位
	while(pPro->m_nAddr<pPro->m_nDataLength)
	{
		TRACE("pPro->m_nAddr:%04x pPro->m_nDataLength:%04x\n",pPro->m_nAddr,pPro->m_nDataLength);
		pSerialPro->SendBlock(&RespByte,1);//发一个字节给编程器
		TRACE("WaitForSingleObject(...)\n");
		if(WaitForSingleObject(pSerialPro->m_hReadRsp,500)==WAIT_TIMEOUT)
		{//等待回应超时
			if(bReWait)//如果是重发都失败
			{
				RespByte=0;
				pSerialPro->SendBlock(&RespByte,1);
				pPro->Notify(PRO_RCV_ER);
				pSerialPro->m_bThread=FALSE;
				pSerialPro->m_bCanUse=TRUE;
				return -1;
			}
			else//尝试要求重发
			{
				RespByte=0xff;
				bReWait=TRUE;
				continue;
			}
		}
		bReWait=FALSE;
		TRACE("WaitForSingleObject(...) OVER\n");
		memcpy(&pPro->m_pReadBuf[pPro->m_nAddr],&pSerialPro->RxdBuf[2],0x10);
		//收到数据后，填充到读缓冲
		pPro->m_nAddr+=0x10;
		RespByte=0x01;//表示继续读
		pPro->Notify(PRO_READ_BLOCK);
	}
	RespByte=0;//表示读结束
	pSerialPro->SendBlock(&RespByte,1);
	pPro->Notify(PRO_READ_OVER);
	pSerialPro->m_bThread=FALSE;
	pSerialPro->m_bCanUse=TRUE;
	TRACE("ReadRomThread(...) Over\n");
	return 0;
}


