summaryrefslogtreecommitdiffstats
path: root/src/progs/pickit2_bootloader/base/pickit2_bootloader.cpp
blob: 7620a9b5f58b9e5f9af0c204493d0617c1d22fdb (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
67
68
69
70
71
72
73
/***************************************************************************
 *   Copyright (C) 2007 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   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 "pickit2_bootloader.h"

//-----------------------------------------------------------------------------
Pickit2Bootloader::Hardware::Hardware(::Programmer::Base &base)
  : Bootloader::Hardware(base, new Pickit2::USBPort(base))
{}

bool Pickit2Bootloader::Hardware::internalConnectHardware()
{
  if ( !openPort() ) return false;
  ::Programmer::Mode mode;
  VersionData version;
  if ( !port().getMode(version, mode) ) return false;
  if ( mode!=::Programmer::BootloadMode ) {
    log(Log::LineType::Information, i18n("Trying to enter bootloader mode..."));
    if ( !port().resetFirmwareDevice(::Programmer::BootloadMode) ) return false;
    for (uint i=0;; i++) {
      port().close();
      Port::usleep(500000); // ??
      if ( !openPort() ) continue;
      if ( !port().getMode(version, mode) ) continue;
      if ( i==4 || mode!=::Programmer::BootloadMode ) {
        log(Log::LineType::Error, i18n("Could not detect device in bootloader mode."));
        return false;
      }
    }
  }
  log(Log::LineType::Information, i18n("Bootloader version %1 detected.").tqarg(version.pretty()));
  if ( version.majorNum()!=2 ) {
    log(Log::LineType::Error, i18n("Only bootloader version 2.x is supported."));
    return false;
  }
  return true;
}

bool Pickit2Bootloader::Hardware::write(Pic::MemoryRangeType type, const Device::Array &data)
{
  Q_ASSERT( data.count()==device().nbWords(type) );
  Q_ASSERT( type==Pic::MemoryRangeType::Code );
  // check that there is nothing in bootloader reserved area
  for (uint i=0; i<data.count(); i++) {
    if ( i>=0x1000 && i<0x3FF0 ) continue;
    if ( data[i]==device().mask(Pic::MemoryRangeType::Code) ) continue;
    uint address = device().addressIncrement(Pic::MemoryRangeType::Code) * i;
    log(Log::LineType::Warning, "  " + i18n("Code is present in bootloader reserved area (at address %1). It will be ignored.").tqarg(toHexLabel(address, device().nbCharsAddress())));
    break;
  }
  return port().writeFirmwareCodeMemory(data, _base.progressMonitor());
}

bool Pickit2Bootloader::Hardware::read(Pic::MemoryRangeType type, Device::Array &data, const ::Programmer::VerifyData *vdata)
{
  Q_ASSERT( type==Pic::MemoryRangeType::Code );
  data.resize(device().nbWords(type));
  Device::Array varray;
  if (vdata) varray = static_cast<const Pic::Memory &>(vdata->memory).arrayForWriting(Pic::MemoryRangeType::Code);
  return port().readFirmwareCodeMemory(data, vdata ? &varray : 0, _base.progressMonitor());
}

//-----------------------------------------------------------------------------
bool Pickit2Bootloader::DeviceSpecific::doEraseRange(Pic::MemoryRangeType type)
{
  Q_ASSERT( type==Pic::MemoryRangeType::Code );
  return static_cast<Hardware &>(hardware()).port().eraseFirmwareCodeMemory();
}