📄 parpgm.cpp
字号:
/**************************************************************************
Project: WinAVRIDE Class: Parallel Programmer
Copyright (C) 2005 Philipp Schober
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
History
19.Feb 2005 - First Release (V1.0)
****************************************************************************/
#include "ParPgm.h"
#include "io.h"
#include <vcl.h>
//---------------------------------------------------------------------------
bool __fastcall TParPgm::Init (void)
{
static bool runonce = true;
if (LoadIODLL ())
{
ShowMessage ("Unable to load io.dll");
return false;
}
PortOut (parport, 0);
EnableBuffer (false);
SetReset (false);
if (runonce) GetDelayCalib ();
runonce = false;
return true;
}
//---------------------------------------------------------------------------
__fastcall TParPgm::TParPgm (void)
{
parport = 0x378;
delay_calib = 1000;
SCKLen = 100;
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::SetParPort (short int port)
{
parport = port;
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::EnableBuffer (bool state)
{
unsigned char s;
s = PortIn (parport);
if (state)
{
s &= ~buffer;
s |= vcc;
PortOut (parport, s);
ClrPortBit (parport + 2, 1); // Needed for Altera Byteblaster
}
else
{
s |= buffer;
s &= ~vcc;
PortOut (parport, s);
SetPortBit (parport + 2, 1); // Needed for Altera Byteblaster
}
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::SetReset (bool state)
{
if (state)
{
ClrPortBit (parport, resetbit);
Sleep (100);
}
else SetPortBit (parport, resetbit);
}
//---------------------------------------------------------------------------
unsigned char __fastcall TParPgm::CommByte (unsigned char tx)
{
unsigned char rx = 0;
int i;
for (i = 0;i < 8;i++)
{
rx <<= 1;
if (GetPortBit (parport + 1, miso)) rx |= 0x01;
if (tx & 0x80) SetPortBit (parport, mosi);
else ClrPortBit (parport, mosi);
DoWait ();
SetPortBit (parport, sck);
DoWait ();
ClrPortBit (parport, sck);
DoWait ();
tx <<= 1;
}
return rx;
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::CommBlock (unsigned char *tx, unsigned char *rx)
{
int i;
for (i = 0;i < 4;i++)
{
*(rx++) = CommByte (*(tx++));
}
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::EnablePgm (void)
{
unsigned char rx[4], tx[4];
int i;
ClrPortBit (parport, sck);
SetReset (false);
Sleep (1);
SetReset (true);
Sleep (20);
for (i = 0;i < 32;i++)
{
tx[0] = 0xAC;
tx[1] = 0x53;
CommBlock (tx, rx);
if (rx[2] == 0x53) break;
else
{
DoWait ();
SetPortBit (parport, sck);
DoWait ();
ClrPortBit (parport, sck);
DoWait ();
}
}
if (rx[2] != 0x53) return false;
Sleep (100);
return true;
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::GetSignature (unsigned char *sig)
{
unsigned char rx[4], tx[4];
int i;
for (i = 0;i < 3;i++)
{
tx[0] = 0x30;
tx[1] = 0;
tx[2] = i;
tx[3] = 0x1e;
CommBlock (tx, rx);
*(sig++) = rx[3];
}
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::DoWait (void)
{
int i, maxcnt;
maxcnt = delay_calib * SCKLen;
for (i = 0;i < maxcnt;i++)
{
if (i > maxcnt) break; // Waste Time
}
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::delayus (int t)
{
int i, maxcnt;
maxcnt = delay_calib * t;
for (i = 0;i < maxcnt;i++)
{
if (i > maxcnt) break; // Waste Time
}
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::ChipErase (void)
{
unsigned char rx[4], tx[4];
tx[0] = 0xAC;
tx[1] = 0x80;
CommBlock (tx, rx);
if (rx[2] != 0x80) return false;
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::LoadPage (unsigned char *data, int adr)
{
unsigned char rx[4], tx[4];
tx[0] = 0x40; // Low Byte
tx[1] = (unsigned char)((adr / 256) & 0xff);
tx[2] = (unsigned char)(adr & 0xff);
tx[3] = *(data++);
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
tx[0] = 0x48; // High Byte
tx[1] = (unsigned char)((adr / 256) & 0xff);
tx[2] = (unsigned char)(adr & 0xff);
tx[3] = *data;
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::WritePage (int adr)
{
unsigned char rx[4], tx[4];
tx[0] = 0x4C; // Write Page
tx[1] = (char)((adr / 256) & 0xff);
tx[2] = (char)(adr & 0xff);
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::WriteByte (unsigned char data, int adr, bool hi)
{
unsigned char rx[4], tx[4];
if (hi) tx[0] = 0x48; // High Byte
else tx[0] = 0x40; // Low Byte
tx[1] = (unsigned char)((adr / 256) & 0xff);
tx[2] = (unsigned char)(adr & 0xff);
tx[3] = data;
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
return true;
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::GetDelayCalib (void)
{
DWORD start, stop;
int i, maxcnt = 1000;
do
{
maxcnt *= 10;
start = GetTickCount ();
for (i = 0;i < maxcnt;i++)
{
if (i > maxcnt) break; // Waste Time
}
stop = GetTickCount ();
} while ((stop - start) < 50);
delay_calib = maxcnt / ((stop - start) * 1000); // Cycles needed for 1us
}
//---------------------------------------------------------------------------
void __fastcall TParPgm::SetSCKLen (int len)
{
SCKLen = len;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::ReadFlash (int adr, unsigned char *data)
{
unsigned char rx[4], tx[4];
tx[0] = 0x20; // Read Low Byte
tx[1] = (char)((adr / 256) & 0xff);
tx[2] = (char)(adr & 0xff);
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
*(data++) = rx[3];
tx[0] = 0x28; // Read High Byte
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
*data = rx[3];
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::WriteEEPROM (int adr, unsigned char data)
{
unsigned char rx[4], tx[4];
tx[0] = 0xC0; // Write EEPROM
tx[1] = (char)((adr / 256) & 0xff);
tx[2] = (char)(adr & 0xff);
tx[3] = data;
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::ReadEEPROM (int adr, unsigned char *data)
{
unsigned char rx[4], tx[4];
tx[0] = 0xA0; // Read EEPROM
tx[1] = (char)((adr / 256) & 0xff);
tx[2] = (char)(adr & 0xff);
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
*data = rx[3];
return true;
}
//---------------------------------------------------------------------------
bool __fastcall TParPgm::ReadCalibrationByte (int num, unsigned char *byte)
{
unsigned char rx[4], tx[4];
tx[0] = 0x38; // Read Calibration Byte
tx[1] = 0;
tx[2] = (unsigned char)num;
tx[3] = 0;
CommBlock (tx, rx);
if (tx[0] != rx[1]) return false;
*byte = rx[3];
return true;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -