summaryrefslogtreecommitdiffstats
path: root/src/progs/icd2/base/icd2_serial.cpp
blob: 5a254624fb7a55377a5cc3f8df740e2525a3fbb0 (plain)
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
/***************************************************************************
 *   Copyright (C) 2005 Nicolas Hadacek <[email protected]>                  *
 *   Copyright (C) 2002-2003 Stephen Landamore <[email protected]>     *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
#include "icd2_serial.h"

#include "common/global/global.h"
#include "common/common/misc.h"

//-----------------------------------------------------------------------------
Icd2::SerialPort::SerialPort(const TQString &device, Log::Base &log)
  : Port::Serial(device, NeedDrain | NeedFlush, log)
{}

bool Icd2::SerialPort::open(Speed speed)
{
  if ( !Port::Serial::open() ) return false;
  return setMode(NoInputFlag, ByteSize8 | EnableReceiver | HardwareFlowControl, speed, 0);
}

//-----------------------------------------------------------------------------
Icd2::SerialHardware::SerialHardware(::Programmer::Base &base, const TQString &portDevice)
  : Hardware(base, new SerialPort(portDevice, base))
{}

bool Icd2::SerialHardware::internalConnect(const TQString &mode)
{
  if ( !static_cast<SerialPort *>(_port)->open(Port::Serial::S19200) ) return false;
  if ( !reset() ) return false;
  if ( !_port->send("Z", 1) ) return false;
  TQString s;
  if ( !_port->receive(4, s) ) return false;
  if ( !reset() ) return false;
  TQByteArray a = toAscii(mode);
  if ( !_port->send(a.data(), a.count()) ) return false;
  if ( !_port->receive(1, s) ) return false;
  if ( s.upper()!=mode ) {
    log(Log::LineType::Error, i18n("Failed to set port mode to '%1'.").arg(mode));
    return false;
  }
  //log(Log::Debug, "set fast speed");
  //if ( !setFastSpeed() ) return false;
  return true;
}

bool Icd2::SerialHardware::reset()
{
  static_cast<Port::Serial *>(_port)->setPinOn(Port::Serial::DTR, false, Port::PositiveLogic); // Trigger DTR to reset icd2
  Port::msleep(10);
  static_cast<Port::Serial *>(_port)->setPinOn(Port::Serial::DTR, true, Port::PositiveLogic); // remove reset
  Port::msleep(10);
  return true;
}

bool Icd2::SerialHardware::setFastSpeed()
{
  if ( !command("4D", 0) ) return false; // go faster
  static_cast<SerialPort *>(_port)->open(Port::Serial::S57600);
  Port::msleep(100); // ...we do need to delay here
  return !hasError();
}