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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
//=========================================================================//
// //
// PonyProg - Serial Device Programmer //
// //
// Copyright (C) 1997-2025 Claudio Lanconelli //
// //
// https://github.com/lancos/ponyprog //
// //
//-------------------------------------------------------------------------//
// //
// 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 "pic125xx.h" // Header file
#include "errcode.h"
#include "eeptypes.h"
#include <QDebug>
#include "e2awinfo.h"
#define CONFIG_SIZE ( 8 * sizeof(uint16_t) )
Pic125xx::Pic125xx(e2AppWinInfo *wininfo, BusIO *busp)
: Device(wininfo, busp, 1 /*BANK_SIZE*/)
{
config_word = 0xffff;
}
Pic125xx::~Pic125xx()
{
}
int Pic125xx::CodeProtectAdjust(uint16_t &config, int read)
{
config = ~config & 0x0fff;
return OK;
}
int Pic125xx::SecurityRead(uint32_t &bits)
{
int rv = GetBus()->ReadConfig(config_word);
if (rv == OK)
{
uint16_t config = config_word;
CodeProtectAdjust(config, 1);
bits = config;
}
return rv;
}
int Pic125xx::SecurityWrite(uint32_t bits)
{
uint16_t config = (uint16_t)bits;
CodeProtectAdjust(config, 0);
config_word = config;
return GetBus()->WriteConfig(config_word);
}
int Pic125xx::Probe(int probe_size)
{
return 1;
}
int Pic125xx::Read(int probe, int type)
{
int rv = Probe(probe || GetNoOfBank() == 0);
if (rv > 0)
{
if (type & CONFIG_TYPE)
{
// read the config locations
// this must be the FIRST operation (just after reset)
uint32_t f;
SecurityRead(f);
GetAWInfo()->SetLockBits(f);
}
else
{
//Skip configuration word
GetBus()->IncAddress(1);
}
if (type & PROG_TYPE)
{
rv = ReadProg();
}
}
return rv;
}
int Pic125xx::Write(int probe, int type)
{
int rv = Probe(probe || GetNoOfBank() == 0);
if (rv > 0)
{
if (type & PROG_TYPE)
{
//Skip configuration word
GetBus()->IncAddress(1);
rv = WriteProg();
}
if (rv > 0 && (type & CONFIG_TYPE))
{
// write the config locations
// this must be the FIRST operation (just after reset)
uint32_t f;
GetBus()->Reset();
f = GetAWInfo()->GetLockBits();
SecurityWrite(f);
}
}
else if (rv == 0)
{
rv = E2ERR_WRITEFAILED;
}
return rv;
}
int Pic125xx::Verify(int type)
{
GetBus()->Reset();
if (GetNoOfBank() == 0)
{
return BADPARAM;
}
int rval = -1;
if (GetSize() >= GetSplitted())
{
unsigned char *localbuf = new unsigned char[GetSize()];
int v_prog = OK, v_config = OK;
if (type & CONFIG_TYPE)
{
uint32_t f;
SecurityRead(f);
qDebug() << "Pic125xx::Verify() ** " << f << " <-> " << (unsigned long)GetAWInfo()->GetLockBits();
if (GetAWInfo()->GetLockBits() == f)
{
v_config = OK;
}
else
{
v_config = 1;
}
}
else
{
//Skip configuration word
GetBus()->IncAddress(1);
}
if (type & PROG_TYPE)
{
v_prog = VerifyProg(localbuf);
}
rval = (v_prog == OK && v_config == OK) ? 1 : 0;
delete[] localbuf;
}
return rval;
}
int Pic125xx::VerifyProg(unsigned char *localbuf)
{
int rval = -1;
int size = GetSplitted();
int base = 0;
//Verify only programmed bytes (to save time in big devices)
// long v_len = size - 2; //Don't verify RC Calibration location
long v_len = size;
if (GetBus()->GetLastProgrammedAddress() > 0 && GetBus()->GetLastProgrammedAddress() < size - 1)
{
v_len = GetBus()->GetLastProgrammedAddress() + 2;
GetBus()->ClearLastProgrammedAddress(); //reset last_programmed_addr, so next verify not preceeded by write verify all the flash
}
//Set blank locations to default 0xFF (erased)
memset(localbuf, 0xFF, size);
// read the current flash content and store it in localbuf
rval = GetBus()->Read(0, localbuf, v_len);
if (rval != v_len)
{
if (rval > 0)
{
rval = OP_ABORTED;
}
}
else
{
rval = GetBus()->CompareMultiWord(GetBufPtr() + base, localbuf + base, v_len, 0) == 0 ? OK : 1;
}
return rval;
}
/**
//Write Flash program memory (don't program RC calibration value)
int Pic125xx::WriteProg()
{
int rv;
int size = GetSplitted()-2; //Don't program last location (RC calibration value)
int base = 0;
rv = GetBus()->Write(0, GetBufPtr()+base, size);
if ( rv != size )
{
if (rv > 0)
rv = OP_ABORTED;
}
return rv;
}
**/
|