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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
//=========================================================================//
// //
// 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. //
// //
//=========================================================================//
#ifndef _BUSINTERFACE_H
#define _BUSINTERFACE_H
#include "types.h"
#include "errcode.h"
#include "wait.h"
#include "globals.h"
#include "interfconv.h"
#include <QDebug>
//Some useful flags
#define SPIMODE_CPHA 0x01 // clock phase/edge
#define SPIMODE_CPOL 0x02 // clock polarity
#define SPIMODE_MASK 0x03
#define xMODE_RDONLY 0x04
#define xMODE_WRONLY 0x08
//#define I2CMODE_MASK 0x10
enum
{
SPI_MODE_0 = (0 | 0),
SPI_MODE_1 = (0 | SPIMODE_CPHA),
SPI_MODE_2 = (SPIMODE_CPOL | 0),
SPI_MODE_3 = (SPIMODE_CPOL | SPIMODE_CPHA)
};
#define SCLTIMEOUT 900 // enable SCL check and timing (for slaves that hold down the SCL line to slow the transfer)
class BusInterface
{
public:
BusInterface()
: old_portno(-1),
usb_vp(0),
installed(-1),
cmd2cmd_delay(0),
shot_delay(5),
i2c_mode(false)
{
}
virtual int Open(int port) = 0;
virtual void Close() = 0;
virtual int TestOpen(int port)
{
qDebug() << Q_FUNC_INFO << "(" << port << ") IN";
int ret_val = TestSave(port);
TestRestore();
qDebug() << Q_FUNC_INFO << "=" << ret_val << " OUT";
return ret_val;
}
virtual int TestPort(int port)
{
qDebug() << Q_FUNC_INFO << "(" << port << ") IN";
return TestOpen(port);
}
virtual int TestSave(int port)
{
int ret_val;
qDebug() << Q_FUNC_INFO << "(" << port << ") IN";
old_portno = installed;
Close();
if ((ret_val = Open(port)) == OK)
{
}
qDebug() << Q_FUNC_INFO << "=" << ret_val << " OUT";
return ret_val;
}
virtual void TestRestore()
{
qDebug() << Q_FUNC_INFO << "IN *** Inst=" << installed;
if (IsInstalled())
{
Close();
}
if (old_portno >= 0)
{
Open(old_portno);
old_portno = -1;
}
qDebug() << Q_FUNC_INFO << "OUT";
}
virtual int SetPower(bool onoff)
{
return OK;
}
virtual void SetControlLine(int res = 1)
{
}
virtual void SetDataOut(int sda = 1) = 0;
virtual void SetInvDataOut(int sda = 1)
{
SetDataOut(!sda);
}
virtual void SetClock(int scl = 1) = 0;
virtual int GetDataIn() = 0;
virtual int GetClock() = 0;
virtual void SetClockData() = 0;
virtual void ClearClockData()
{ }
virtual int IsClockDataUP() = 0;
virtual int IsClockDataDOWN() = 0;
virtual bool CheckDataLines(int len = 1, int sda = -1, int scl = -1)
{
bool test = false;
if (len > 0)
{
do {
if (sda == 0 && scl == 0)
{
test = IsClockDataDOWN();
}
else if (sda > 0 && scl > 0)
{
test = IsClockDataUP();
}
else
{
bool test_sda = true, test_scl = true;
if (sda > 0)
{
test_sda = GetDataIn();
}
else if (sda == 0)
{
test_sda = !GetDataIn();
}
if (scl > 0)
{
test_scl = GetClock();
}
else if (scl == 0)
{
test_scl = !GetClock();
}
test = (test_sda && test_scl);
}
} while (test && --len > 0);
}
return test;
}
int GetCmd2CmdDelay() const
{
return cmd2cmd_delay;
}
void SetCmd2CmdDelay(int delay)
{
if (delay >= 0)
{
cmd2cmd_delay = delay;
}
}
bool IsInstalled() const
{
return (installed >= 0) ? true : false;
}
void SetUSBVidPid(VidPid vp)
{
usb_vp = vp;
}
VidPid GetUSBVid()
{
return usb_vp;
}
virtual void WaitMsec(unsigned int msec)
{
Flush();
w.WaitMsec(msec);
}
virtual void WaitUsec(unsigned int usec)
{
Flush();
w.WaitUsec(usec);
}
virtual void ShotDelay(int n = 1)
{
w.WaitUsec(shot_delay * n);
}
virtual int xferBit(int &err, int b, int mode = 0)
{
int ret = 0;
if (!i2c_mode) //(mode & I2CMODE_MASK) == 0)
{
switch (mode & SPIMODE_MASK)
{
case 3:
SetClock(0);
SetDataOut(b);
ShotDelay();
SetClock(1);
if ((mode & xMODE_WRONLY) == 0)
{
ret = GetDataIn();
}
ShotDelay();
break;
case 2:
SetDataOut(b);
ShotDelay();
SetClock(0);
if ((mode & xMODE_WRONLY) == 0)
{
ret = GetDataIn();
}
ShotDelay();
SetClock(1);
break;
case 1:
SetClock(1);
SetDataOut(b);
ShotDelay();
SetClock(0);
if ((mode & xMODE_WRONLY) == 0)
{
ret = GetDataIn();
}
ShotDelay();
break;
case 0:
default:
SetDataOut(b);
ShotDelay();
SetClock(1);
if ((mode & xMODE_WRONLY) == 0)
{
ret = GetDataIn();
}
ShotDelay();
SetClock(0);
break;
}
}
else
{
//I2CBus
SetDataOut(b); // SDA must be high to receive data (low dominant)
ShotDelay(); // tSU;DAT = 250 nsec (tLOW / 2 = 2 usec)
SetClock(1);
#ifdef SCLTIMEOUT
for (int k = SCLTIMEOUT; GetClock() == 0 && k > 0; k--)
{
WaitUsec(1);
}
if (GetClock() == 0)
{
return IICERR_SCLCONFLICT;
}
#endif
ShotDelay(); // tHIGH / 2 = 2 usec
if ((mode & xMODE_WRONLY) == 0)
{
ret = GetDataIn();
}
ShotDelay(); // tHIGH / 2 = 2 usec
SetClock(0);
ShotDelay(); // tHD;DATA = 300 nsec (tLOW / 2 = 2 usec)
}
err = OK;
return ret;
}
virtual uint8_t xferByte(int &err, uint8_t by, int mode = 0, int bpw = 8, bool lsb_first = false)
{
return (uint8_t)xferWord(err, by, mode, bpw, lsb_first);
}
virtual unsigned long xferWord(int &err, unsigned long word_out, int mode = 0, int bpw = 8, bool lsb_first = false)
{
uint32_t word_in = 0;
uint32_t bitmask;
err = OK;
if (!i2c_mode) //if ((mode & I2CMODE_MASK) == 0)
{
switch (mode & SPIMODE_MASK)
{
case 3:
case 2:
SetClock(1);
break;
case 1:
case 0:
default:
SetClock(0);
break;
}
}
if (lsb_first)
{
bitmask = 1;
}
else
{
bitmask = 1 << (bpw - 1);
}
for (int k = 0; k < bpw; k++)
{
if (xferBit(err, word_out & bitmask, mode))
{
word_in |= bitmask;
}
if (lsb_first)
{
bitmask <<= 1;
}
else
{
bitmask >>= 1;
}
}
SetDataOut(1);
return word_in;
}
virtual void SetDelay(int delay)
{
if (delay >= 0)
{
shot_delay = delay;
}
}
int GetDelay() const
{
return shot_delay;
}
void SetI2CMode(bool mode)
{
i2c_mode = mode;
}
bool GetI2CMode() const
{
return i2c_mode;
}
virtual void ConfigPins(int pinum_ctrl = -1, int pinum_datain = -1, int pinum_dataout = -1, int pinum_clock = -1, int pinum_clockin = -1, int pinum_poweron = -1, int pinum_enbus = -1, int pinnum_ctrlin = -1)
{
pins.ctrl = pinum_ctrl;
pins.datain = pinum_datain;
pins.dataout = pinum_dataout;
pins.clock = pinum_clock;
pins.ctrlin = pinnum_ctrlin;
pins.clockin = pinum_clockin;
pins.poweron = pinum_poweron;
pins.enbus = pinum_enbus;
}
virtual void ConfigPins(InterfPins p)
{
ConfigPins(p.ctrl, p.datain, p.dataout, p.clock, p.clockin, p.poweron, p.enbus, p.ctrlin);
}
protected:
void Install(int val)
{
installed = val;
}
void DeInstall()
{
installed = -1;
}
int GetInstalled() const
{
return installed;
}
virtual int Flush()
{
return OK;
}
int old_portno; // TestSave() save the status here
VidPid usb_vp;
Wait w;
InterfPins pins;
private:
int installed; // -1 --> not installed, >= 0 number if the installed port
int cmd2cmd_delay; // <> 0 if a delay between commands is needed
unsigned int shot_delay; //delay unit to perform bus timing
bool i2c_mode;
};
#endif
|