Skip to content

Commit dc444cb

Browse files
committed
Merge pull request #5 from parallaxinc/rewrite
Merge rewrite branch into mainline
2 parents e0ebd8d + 3506dbd commit dc444cb

31 files changed

+973
-831
lines changed

app/propman/main.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ QStringList devices = manager.listPorts();
3333
QCommandLineOption argList (QStringList() << "l" << "list", QObject::tr("List available devices"));
3434
QCommandLineOption argWrite (QStringList() << "w" << "write", QObject::tr("Write program to EEPROM"));
3535
QCommandLineOption argDevice (QStringList() << "d" << "device", QObject::tr("Device to program (default: first system device)"), "DEV");
36+
QCommandLineOption argBaud (QStringList() << "b" << "baud", QObject::tr("Baud rate for terminal (default: 115200)"), "BAUD");
3637
QCommandLineOption argPin (QStringList() << "p" << "pin", QObject::tr("Pin for GPIO reset"), "PIN");
3738
QCommandLineOption argTerm (QStringList() << "t" << "terminal",QObject::tr("Drop into terminal after download"));
3839
QCommandLineOption argIdentify (QStringList() << "i" << "identify",QObject::tr("Identify device connected at port"));
@@ -41,7 +42,6 @@ QCommandLineOption argClkMode (QStringList() << "clkmode", QObject::tr(
4142
QCommandLineOption argClkFreq (QStringList() << "clkfreq", QObject::tr("Change clock frequency before download"), "FREQ");
4243
QCommandLineOption argHighSpeed (QStringList() << "ultrafast", QObject::tr("Enable two-stage high-speed mode (experimental)"));
4344

44-
4545
int main(int argc, char *argv[])
4646
{
4747
QCoreApplication app(argc, argv);
@@ -61,13 +61,14 @@ int main(int argc, char *argv[])
6161
parser.addOption(argList);
6262
parser.addOption(argWrite);
6363
parser.addOption(argDevice);
64+
parser.addOption(argBaud);
6465
parser.addOption(argPin);
6566
parser.addOption(argTerm);
6667
parser.addOption(argIdentify);
6768
parser.addOption(argInfo);
6869
parser.addOption(argClkMode);
6970
parser.addOption(argClkFreq);
70-
parser.addOption(argHighSpeed);
71+
// parser.addOption(argHighSpeed);
7172

7273
parser.addPositionalArgument("file", QObject::tr("Binary file to download"), "FILE");
7374

@@ -130,6 +131,15 @@ void open_loader(QCommandLineParser &parser, QStringList devices)
130131
error("Device does not exist!");
131132
}
132133

134+
qint32 baudrate = 115200;
135+
if (!parser.value(argBaud).isEmpty())
136+
{
137+
bool ok;
138+
baudrate = parser.value(argBaud).toInt(&ok);
139+
if (baudrate <= 0 || baudrate > 1000000 || !ok)
140+
error(QString("Baud rate %1 is invalid!").arg(baudrate));
141+
}
142+
133143
if (parser.positionalArguments().isEmpty())
134144
{
135145
if (parser.isSet(argTerm))
@@ -168,6 +178,8 @@ void open_loader(QCommandLineParser &parser, QStringList devices)
168178
error("Image is invalid!");
169179

170180

181+
PropellerTerminal terminal(&manager, device, baudrate);
182+
171183
if (parser.isSet(argHighSpeed))
172184
{
173185
// if (!loader.highSpeedUpload(image, parser.isSet(argWrite)))
@@ -178,14 +190,15 @@ void open_loader(QCommandLineParser &parser, QStringList devices)
178190
QObject::connect (&loader, SIGNAL(statusChanged(const QString &)),
179191
&loader, SLOT(message(const QString &)));
180192

181-
if (!loader.upload(image, parser.isSet(argWrite),true,true))
193+
if (!loader.upload(image, parser.isSet(argWrite), true, true))
182194
exit(1);
195+
183196
QObject::disconnect (&loader, SIGNAL(statusChanged(const QString &)),
184197
&loader, SLOT(message(const QString &)));
185198
}
186199

187200
if (parser.isSet(argTerm))
188-
PropellerTerminal terminal(&manager, device);
201+
terminal.exec();
189202
}
190203

191204
void list()

app/propman/propman.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TARGET = propman
66
DESTDIR = $$TOP_PWD/bin/
77

88
CONFIG += console
9+
CONFIG += debug
910

1011
SOURCES += \
1112
main.cpp

src/device/include.pri

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SOURCES += \
2+
$$PWD/propellerdevice.cpp \
3+
$$PWD/gpio.cpp \
4+
5+
HEADERS += \
6+
$$PWD/propellerdevice.h \
7+
$$PWD/gpio.h \

src/device/propellerdevice.cpp

Lines changed: 125 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#include "propellerdevice.h"
22

3-
#include <QDebug>
43
#include <QSerialPortInfo>
54
#include <QTimer>
65
#include <QEventLoop>
76

87
#include "gpio.h"
98
#include "../util/logging.h"
109

11-
PropellerDevice::PropellerDevice(QObject * parent)
12-
: QSerialPort(parent)
10+
PropellerDevice::PropellerDevice()
11+
: Interface()
1312
{
14-
resource_error_count = 0;
13+
_resource_error_count = 0;
1514
_minimum_timeout = 400;
1615

17-
setSettingsRestoredOnClose(false);
16+
device.setSettingsRestoredOnClose(false);
17+
device.setBaudRate(115200);
1818

1919
_reset_defaults["ttyAMA"] = "gpio";
2020
_reset_defaults["ttyS"] = "dtr";
@@ -24,24 +24,36 @@ PropellerDevice::PropellerDevice(QObject * parent)
2424

2525
useDefaultReset();
2626

27-
connect(this,SIGNAL(error(QSerialPort::SerialPortError)),
28-
this, SLOT(handleError(QSerialPort::SerialPortError)));
27+
connect(&device, SIGNAL(error(QSerialPort::SerialPortError)),
28+
this, SLOT(handleError(QSerialPort::SerialPortError)));
29+
30+
connect(&device, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64)));
31+
connect(&device, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
32+
33+
connect(&device, SIGNAL(baudRateChanged(qint32, QSerialPort::Directions)),
34+
this, SIGNAL(baudRateChanged(qint32)));
2935
}
3036

31-
3237
PropellerDevice::~PropellerDevice()
3338
{
3439
close();
35-
}
3640

41+
disconnect(&device, SIGNAL(error(QSerialPort::SerialPortError)),
42+
this, SLOT(handleError(QSerialPort::SerialPortError)));
43+
44+
disconnect(&device, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64)));
45+
disconnect(&device, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
46+
47+
disconnect(&device, SIGNAL(baudRateChanged(qint32, QSerialPort::Directions)),
48+
this, SIGNAL(baudRateChanged(qint32)));
49+
}
3750

3851
void PropellerDevice::setPortName(const QString & name)
3952
{
40-
QSerialPort::setPortName(name);
53+
device.setPortName(name);
4154
useDefaultReset();
4255
}
4356

44-
4557
void PropellerDevice::handleError(QSerialPort::SerialPortError e)
4658
{
4759
switch (e)
@@ -53,10 +65,9 @@ void PropellerDevice::handleError(QSerialPort::SerialPortError e)
5365
case QSerialPort::PermissionError: // 2
5466
case QSerialPort::NotOpenError: // 13
5567
case QSerialPort::UnsupportedOperationError: // 10
56-
clearError();
68+
device.clearError();
5769
break;
5870
case QSerialPort::TimeoutError: // 12
59-
emit finished();
6071
break;
6172
case QSerialPort::ParityError: // 4
6273
case QSerialPort::FramingError: // 5
@@ -65,12 +76,11 @@ void PropellerDevice::handleError(QSerialPort::SerialPortError e)
6576
case QSerialPort::ReadError: // 8
6677
case QSerialPort::UnknownError: // 11
6778
case QSerialPort::ResourceError: // SUPER IMPORTANT // 9
68-
resource_error_count++;
69-
if (resource_error_count > 1)
79+
_resource_error_count++;
80+
if (_resource_error_count > 1)
7081
{
7182
close();
72-
emit finished();
73-
sendError(QString("'%1' (error %2)").arg(errorString()).arg(e));
83+
emit sendError(QString("'%1' (error %2)").arg(device.errorString()).arg(e));
7484
}
7585
break;
7686
default:
@@ -83,32 +93,28 @@ void PropellerDevice::handleError(QSerialPort::SerialPortError e)
8393
*/
8494
bool PropellerDevice::open()
8595
{
86-
resource_error_count = 0;
96+
_resource_error_count = 0;
8797

88-
if (!QSerialPort::open(QSerialPort::ReadWrite))
98+
if (!device.open(QSerialPort::ReadWrite))
8999
{
90-
qCDebug(pdevice) << "Reattempting device open:" << portName();
91-
92-
QTimer wait;
93-
QEventLoop loop;
94-
connect(&wait, SIGNAL(timeout()), &loop, SLOT(quit()));
95-
wait.start(1000);
96-
loop.exec();
97-
disconnect(&wait, SIGNAL(timeout()), &loop, SLOT(quit()));
98-
99-
if (!QSerialPort::open(QSerialPort::ReadWrite))
100+
close();
101+
if (!device.open(QSerialPort::ReadWrite))
100102
{
101103
qCDebug(pdevice) << "Failed to open device:" << portName();
102104
return false;
103105
}
104106
}
105107

106108
reset();
107-
setBaudRate(115200);
108109

109110
return true;
110111
}
111112

113+
void PropellerDevice::close()
114+
{
115+
device.close();
116+
}
117+
112118
/**
113119
Return the minimum timeout for downloading to the Propeller.
114120
@@ -141,17 +147,17 @@ void PropellerDevice::setMinimumTimeout(quint32 milliseconds)
141147
safety_factor defaults to 10, and is divided by 10, allowing <1 safety factors.
142148
*/
143149

144-
quint32 PropellerDevice::calculateTimeout(quint32 bytes, quint32 safety_factor)
150+
quint32 PropellerDevice::calculateTimeout(quint32 bytes)
145151
{
146-
return bytes * (dataBits() + stopBits()) * safety_factor / 10
152+
return bytes * (device.dataBits() + device.stopBits()) * 25 / 10
147153
* 1000 / baudRate() + minimumTimeout();
148154
}
149155

150156
/**
151157
Set the reset strategy for your device.
152158
*/
153159

154-
void PropellerDevice::useReset(const QString & name, int pin)
160+
void PropellerDevice::useReset(QString name, int pin)
155161
{
156162
if (_reset_defaults.contains(name))
157163
{
@@ -208,13 +214,29 @@ bool PropellerDevice::reset()
208214
{
209215
if (_reset == "rts")
210216
{
211-
setRequestToSend(true);
212-
setRequestToSend(false);
217+
device.setRequestToSend(true);
218+
219+
QTimer wait;
220+
QEventLoop loop;
221+
connect(&wait, SIGNAL(timeout()), &loop, SLOT(quit()));
222+
wait.start(20);
223+
loop.exec();
224+
disconnect(&wait, SIGNAL(timeout()), &loop, SLOT(quit()));
225+
226+
device.setRequestToSend(false);
213227
}
214228
else if (_reset == "dtr")
215229
{
216-
setDataTerminalReady(true);
217-
setDataTerminalReady(false);
230+
device.setDataTerminalReady(true);
231+
232+
QTimer wait;
233+
QEventLoop loop;
234+
connect(&wait, SIGNAL(timeout()), &loop, SLOT(quit()));
235+
wait.start(20);
236+
loop.exec();
237+
disconnect(&wait, SIGNAL(timeout()), &loop, SLOT(quit()));
238+
239+
device.setDataTerminalReady(false);
218240
}
219241
else
220242
{
@@ -223,7 +245,6 @@ bool PropellerDevice::reset()
223245
}
224246

225247
clear();
226-
readAll(); // clear doesn't appear to actually do anything
227248

228249
return true;
229250
}
@@ -252,3 +273,69 @@ quint32 PropellerDevice::resetPeriod()
252273
{
253274
return 80;
254275
}
276+
277+
278+
bool PropellerDevice::isOpen()
279+
{
280+
if (!device.isOpen())
281+
{
282+
if (!open())
283+
{
284+
close();
285+
return open();
286+
}
287+
}
288+
return true;
289+
}
290+
291+
bool PropellerDevice::clear()
292+
{
293+
device.clear();
294+
device.readAll(); // clear doesn't appear to actually do anything
295+
return true;
296+
}
297+
298+
bool PropellerDevice::setBaudRate(quint32 baudRate)
299+
{
300+
return device.setBaudRate(baudRate);
301+
}
302+
303+
QString PropellerDevice::portName()
304+
{
305+
return device.portName();
306+
}
307+
308+
quint32 PropellerDevice::baudRate()
309+
{
310+
return device.baudRate();
311+
}
312+
313+
qint64 PropellerDevice::bytesToWrite()
314+
{
315+
return device.bytesToWrite();
316+
}
317+
318+
qint64 PropellerDevice::bytesAvailable()
319+
{
320+
return device.bytesAvailable();
321+
}
322+
323+
QByteArray PropellerDevice::read(qint64 maxSize)
324+
{
325+
return device.read(maxSize);
326+
}
327+
328+
QByteArray PropellerDevice::readAll()
329+
{
330+
return device.readAll();
331+
}
332+
333+
bool PropellerDevice::putChar(char c)
334+
{
335+
return device.putChar(c);
336+
}
337+
qint64 PropellerDevice::write(QByteArray ba)
338+
{
339+
return device.write(ba);
340+
}
341+

0 commit comments

Comments
 (0)