Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
do_best

Ajuda Converter programa Windows para MAC OS X

Recommended Posts

do_best

isto é programa C em windows

que faz bassicamente é

verificar se um device está ligado no windows, se estiver abre ligaçao

depois faz umas trocas de dados para verificar se é efectivamente o device certo

depois envia 1ficheiro que vai flashar o device

basicamente é isto

BOOL CFlashB1Dlg::SendThread(LPVOID lpParam)
{

CFlashB1Dlg *me = (CFlashB1Dlg*)lpParam;
unsigned char * ptr;
DWORD cbLen = 0;

DWORD dwFTStatus;
DWORD dwState = 0;
FT_HANDLE hFTHandle = NULL;
DWORD numDevs = 0;
DWORD dwBytesIO;
unsigned char ch;
int n;
DWORD dwTotalDataCRC = 0;


while(! (me->m_bExit) )
{
	TRACE("State %d\n",dwState);
	switch(dwState)
	{
	case 0:
		// check for file being selected...
		if(me->m_dwDataSize)
		{
			cbLen = me->m_dwDataSize;
			dwState = 1;
			ptr = me->m_lpData;
			me->m_staticStatus.SetWindowText(_T("Waiting for B1..."));
			me->m_progStatus.SetRange32(0, cbLen);
			me->m_progStatus.SetPos(0);

		}
		else
		{
			Sleep(1000);
		}
		break;
	case 1:
		// identify the presence of a device...

		if(hFTHandle)
		{
			// close any previously opened handle
			FT_Close(hFTHandle);
			hFTHandle = NULL;
		}
		if(FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY) == FT_OK)
		{


			if(numDevs > 0)
			{
				// got a device connected
				dwState = 2;
				hFTHandle = NULL;
			}
			else
			{
				// no device connected...
				Sleep(500);
			}
		}
		else
		{
			// failed getting device list...
			Sleep(50);
		}

		break;
	case 2:
		// identified a device, lets open the port...			
		for(n =0; n < 32; n++)
		{
			dwFTStatus = FT_Open(0,&hFTHandle);


			if(FT_OK == dwFTStatus)
			{
				// opened successfull
				dwState = 3;

				// prepare the port
				FT_SetBaudRate(hFTHandle, 115200);
				FT_SetDataCharacteristics (hFTHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
				FT_SetFlowControl(hFTHandle, FT_FLOW_NONE,0,0);
				break;
			}
			else
			{
				Sleep(100);
			}
		}
		break;
	case 3:
		// device is open lets check for start packet...
		dwFTStatus = FT_GetQueueStatus(hFTHandle, &dwBytesIO);
		if(FT_OK  != dwFTStatus)
		{
			// something is wrong...

			dwState = 1;
			continue;
		}
		else
		{

			if(dwBytesIO )
			{
				// got some data...
				if(FT_Read(hFTHandle,&ch, 1, &dwBytesIO) == FT_OK )
				{
					if(ch == '+')
					{
						ch = '#';
						if(FT_Purge(hFTHandle,FT_PURGE_RX) != FT_OK)
						{
							dwState = 1;
							continue;
						}
						for(n = 0; n < 4; n++)
						{
							if(FT_Write(hFTHandle,&ch,1, &dwBytesIO) != FT_OK)
							{
								dwState = 1;
								n = 5;
								continue;
							}
							Sleep(2);
						}
						continue;
					}
					if(ch == '*')
					{
						// 
						Sleep(10);
						// send the * to tell B1 to erase its memory
						if(FT_Write(hFTHandle,&ch,1, &dwBytesIO) != FT_OK)
						{
							// failed...
							dwState = 1;
							continue;
						}
						Sleep(10);
						me->m_staticStatus.SetWindowText(_T("Erasing memory..."));
						//me->m_progStatus.SetRange32(0, cbLen);
						//me->m_progStatus.SetPos(0);

						TRACE(_T("Erasing memory!\n"));
						dwTotalDataCRC = 0;
						for(DWORD n = 0; n < cbLen && dwState == 3;)
						{
							dwBytesIO=0;

							while(dwBytesIO == 0)
							{
								// wait for a request packet message... 
								if(FT_Read(hFTHandle,&ch, 1, &dwBytesIO) != FT_OK)
								{
									// reading error, 
									dwState = 1;
									dwBytesIO = 0;
									break;
								}
							}
							if(dwBytesIO)
							{
								if(ch == 0xaa)
								{
									// received a request packet message... 

									unsigned char chBytesToSend = 2*64;
									unsigned char crc;

									if((c-n) < chBytesToSend) chBytesToSend = (cbLen-n);
									me->m_staticStatus.SetWindowText(_T("Programing..."));
									me->m_progStatus.SetPos(n);
									TRACE("%d of %d sending %d\n", n, cbLen, chBytesToSend);
									// send the size of packet that will be sent...
									FT_Write(hFTHandle, &chBytesToSend,1, &dwBytesIO);
									Sleep(1);
									// send the actual packet...
									crc = 0;
									for(int k = 0; k < chBytesToSend; k++)
									{
										FT_Write(hFTHandle, ptr+n+k, 1, &dwBytesIO);
										crc ^= *(ptr+n+k);
										//Sleep(1);
										dwTotalDataCRC+=*(ptr+n+k);
									}

									FT_Write(hFTHandle, &crc, 1, &dwBytesIO);

									Sleep(1);

									// now wait for the acknowledge

									dwBytesIO=0;
									while(dwBytesIO == 0)
									{
										FT_Read(hFTHandle,&ch, 1, &dwBytesIO);
									}
									if(dwBytesIO)
									{
										// check what to do...
										if(ch == 0xbb)
										{
											// need a resend....
											TRACE("need a resend\n");
										}
										if(ch = 0xcc)
										{
											// packet is ok...
											// move forward...
											n += chBytesToSend;
										}
									}
								}
							}
						}
						// send the final 0 tell B1 we are done...
						ch = 0;
						FT_Write(hFTHandle, &ch, 1, &dwBytesIO);

						// FT_Close(hFTHandle);
						CString strMsg;
						strMsg.Format(_T("Complete.  CRC:%u"), dwTotalDataCRC);
						me->m_staticStatus.SetWindowText(strMsg);
						TRACE("Done Leaving\n");
						me->m_dwDataSize = 0;
						// reset 
						dwState = 0;
						//return 1;
					}
				}
				else
				{
					// an error occured during ready, reset the machine.
					dwState = 1;
				}
			}
			else
			{
				// no data in the queue, wait a little...
				Sleep(10);
			}
		}
	}

}
AfxMessageBox(_T("thread termination unexpected!"));
return 1;
}

e até momento para MAC OS X

 int main (int argc, const char * argv[]) 
{
 int n;
 unsigned ch = 0;
 unsigned char buf[128*1024];

// just in case of make eay stuff - wrong --> char dir = "/b1firmware.b1b";
     printf("Started out app\n");
 for (n=0; n<1; n++)
 { 
	 // open file to send
	 int fd  = open("/b1firmware.b1b",O_RDONLY );    
    if( fd < 0) 
     {       
	  printf("Can't open the file\n");       

     }
	 else 
	  {
		  unsigned char chFileHeader[64];
		  printf("ficheiro aberto \n");
		  lseek(fd, 0, SEEK_SET);
		  if(read(fd, chFileHeader, sizeof(chFileHeader)) == sizeof(chFileHeader))
		  {
			  int iBytesRead =
			  read(fd, buf, sizeof(buf));
			  printf("read %d bytes into buf\n",iBytesRead);

			  // validate CRC
			  unsigned char ourcrc[4];
			  memset(ourcrc,0,sizeof(ourcrc));

			  for (int n= 0; n < iBytesRead; n++) 
			  {
				  ourcrc[n % 4] ^= buf[n];
			  }

			  if(memcmp(ourcrc, chFileHeader+0x3c,4) == 0)
			  {
				  printf("file is ok.\n");




				  // got some data...
				  if(read(fd ,&ch, 1) == 0 )
				  {
					  printf("1");
					  if(ch == '+')
					  {
						  ch = '#';

está a metade agora dificil vem ajudas?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.