1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
//=========================================================================//
// //
// PonyProg - Serial Device Programmer //
// //
// Copyright (C) 1997-2021 Claudio Lanconelli //
// //
// http://ponyprog.sourceforge.net //
// //
//-------------------------------------------------------------------------//
// //
// 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 version2 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 (see LICENSE); if not, write to the //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
//=========================================================================//
#include "types.h"
#include "at250bus.h"
#include "errcode.h"
#include <QDebug>
#include <QtCore>
At250Bus::At250Bus(BusInterface *ptr)
: SPIBus(ptr),
WriteEnable(0x06),
WriteDisable(0x04),
ReadStatus(0x05),
WriteStatus(0x01),
ReadData(0x03),
WriteData(0x02),
NotReadyFlag(1),
WenFlag(2),
BPFlags(8 + 4),
loop_timeout(5000)
{
}
void At250Bus::EndCycle()
{
//WaitUsec(1);
setNCS();
ShotDelay(2);
clearNCS();
ShotDelay();
}
int At250Bus::ReadEEPByte(int addr)
{
int rv;
SendDataByte(ReadData | (((addr >> 8) & 1) << 3));
SendDataByte(addr);
ShotDelay();
rv = RecDataByte();
EndCycle();
return rv;
}
void At250Bus::WriteEEPByte(int addr, int data)
{
SendDataByte(WriteData | (((addr >> 8) & 1) << 3));
SendDataByte(addr);
SendDataByte(data);
EndCycle();
}
int At250Bus::ReadEEPStatus()
{
int rv;
SendDataByte(ReadStatus);
rv = RecDataByte();
EndCycle();
return rv;
}
int At250Bus::WriteEEPStatus(int data)
{
SendDataByte(WriteEnable);
EndCycle();
SendDataByte(WriteStatus);
SendDataByte(data);
EndCycle();
return 0;
}
int At250Bus::WaitEndOfWrite(int timeout) // 07/08/99
{
if (timeout <= 0)
{
timeout = loop_timeout;
}
int k;
for (k = timeout; k > 0 && (ReadEEPStatus() & NotReadyFlag); k--)
{
WaitUsec(50);
}
return (k != 0);
}
int At250Bus::Reset(void)
{
qDebug() << Q_FUNC_INFO;
SPIBus::Reset();
ShotDelay();
//SendDataByte(WriteEnable);
//EndCycle();
return OK;
}
long At250Bus::Read(int addr, uint8_t *data, long length, int page_size)
{
qDebug() << Q_FUNC_INFO << "(" << (hex) << addr << ", " << data << ", " << (dec) << length << ")";
long len;
ReadStart();
for (len = 0; len < length; len++)
{
*data++ = (uint8_t)ReadEEPByte(addr++);
if ((len % 10) == 0)
{
if (ReadProgress(len * 100 / length))
{
break;
}
}
}
WaitMsec(1); //Flush
ReadEnd();
qDebug() << Q_FUNC_INFO << "=" << len;
return len;
}
long At250Bus::Write(int addr, uint8_t const *data, long length, int page_size)
{
long len;
WriteStart();
WriteEEPStatus(0);
// 07/08/99 *** bug fix suggested by Atmel Product engineer
if (!WaitEndOfWrite())
{
return 0;
}
for (len = 0; len < length; len++)
{
SendDataByte(WriteEnable);
EndCycle();
WriteEEPByte(addr++, *data++);
if (!WaitEndOfWrite())
{
return 0; //Must return 0, because > 0 (and != length) means "Abort by user"
}
if ((len & 1))
{
if (WriteProgress(len * 100 / length))
{
break;
}
}
}
WaitMsec(1); //Flush
WriteEnd();
return len;
}
|