Skip to content

Commit

Permalink
Added support for USB
Browse files Browse the repository at this point in the history
  • Loading branch information
rasgo-cc committed Sep 28, 2015
1 parent 302b6b1 commit d99b9bf
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 75 deletions.
88 changes: 58 additions & 30 deletions efm32loader.cpp
Expand Up @@ -11,20 +11,26 @@
EFM32Loader::EFM32Loader(QObject *parent) :
QObject(parent)
{
m_serialPort = new QSerialPort(this);
m_xmodem = new XMODEM(m_serialPort, this);
_serialPort = new QSerialPort(this);
_xmodem = new XMODEM(_serialPort, this);
_bootEnablePolarity = true;
connect(m_xmodem, SIGNAL(output(QString)), this, SIGNAL(output(QString)));
_transport = TransportUART;
connect(_xmodem, SIGNAL(output(QString)), this, SIGNAL(output(QString)));
}

void EFM32Loader::setBootEnablePolarity(bool high)
{
_bootEnablePolarity = high;
}

void EFM32Loader::setTransport(Transport transport)
{
_transport = transport;
}

bool EFM32Loader::open(const QString &portName)
{
QSerialPort *sp = m_serialPort;
QSerialPort *sp = _serialPort;

if(!sp->isOpen())
{
Expand Down Expand Up @@ -58,22 +64,38 @@ bool EFM32Loader::open(const QString &portName)
void EFM32Loader::close()
{
emit output(tr("Disconnected"));
m_serialPort->close();
_serialPort->close();
}

bool EFM32Loader::detect()
{
char byteBuf;
bool detected;

m_serialPort->readAll();
enterBoot();

byteBuf = 'U';
m_serialPort->write(&byteBuf, 1);
bool detected = waitForChipID();

m_serialPort->readAll();
exitBoot();
if(_transport == TransportUART)
{
byteBuf = 'U';
int tries = 3;
QEventLoop eventLoop;
QTimer timer;
timer.setInterval(10);
timer.setSingleShot(false);
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer.start();
while(tries--)
{
_serialPort->write(&byteBuf, 1);
eventLoop.exec();
}
timer.stop();
detected = waitForChipID();
}
else
{
byteBuf = 'i';
_serialPort->write(&byteBuf, 1);
detected = waitForChipID();
}

return detected;
}
Expand All @@ -97,7 +119,7 @@ bool EFM32Loader::upload(const QString &filePath)
elapsedTimer.start();

// Autobaud sync
byteBuf = 'U';
/*byteBuf = 'U';
int tries = 3;
QEventLoop eventLoop;
QTimer timer;
Expand All @@ -107,27 +129,33 @@ bool EFM32Loader::upload(const QString &filePath)
timer.start();
while(tries--)
{
m_serialPort->write(&byteBuf, 1);
_serialPort->write(&byteBuf, 1);
eventLoop.exec();
}
if(!waitForChipID())
{
exitBoot();
return false;
}*/

if(!detect())
{
exitBoot();
return false;
}

// Enter upload mode (XMODEM)
byteBuf = 'u';
m_serialPort->write(&byteBuf, 1);
_serialPort->write(&byteBuf, 1);
waitForReady();

emit output("Uploading...");

// Send file through XMODEM-CRC protocol
success = m_xmodem->sendFile(filePath);
success = _xmodem->sendFile(filePath);
emit output(QString().sprintf("Elapsed time: %.3f seconds", (double)elapsedTimer.elapsed()/1000.0));
m_serialPort->readAll();
_serialPort->readAll();
exitBoot();
emit output(tr("Done"));
return success;
Expand All @@ -144,9 +172,9 @@ void EFM32Loader::enterBoot()
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));

if(_bootEnablePolarity == true)
m_serialPort->setDataTerminalReady(false);
_serialPort->setDataTerminalReady(false);
else
m_serialPort->setDataTerminalReady(true);
_serialPort->setDataTerminalReady(true);
timer.start(10);
eventLoop.exec();

Expand All @@ -161,9 +189,9 @@ void EFM32Loader::exitBoot()
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));

if(_bootEnablePolarity == true)
m_serialPort->setDataTerminalReady(true);
_serialPort->setDataTerminalReady(true);
else
m_serialPort->setDataTerminalReady(false);
_serialPort->setDataTerminalReady(false);
timer.start(10);
eventLoop.exec();
reset();
Expand All @@ -176,10 +204,10 @@ void EFM32Loader::reset()
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));

m_serialPort->setRequestToSend(true);
_serialPort->setRequestToSend(true);
timer.start(100);
eventLoop.exec();
m_serialPort->setRequestToSend(false);
_serialPort->setRequestToSend(false);
timer.start(100);
eventLoop.exec();
}
Expand All @@ -196,7 +224,7 @@ bool EFM32Loader::waitForChipID()
}
else
{
QString line = QString(m_serialPort->readLine());
QString line = QString(_serialPort->readLine());
if(line.contains("ChipID"))
{
emit output(tr("Bootloader detected"));
Expand All @@ -217,7 +245,7 @@ bool EFM32Loader::waitForReady()
}
else
{
QString line = QString(m_serialPort->readLine());
QString line = QString(_serialPort->readLine());
if(line.contains("C"))
{
emit output(tr("Ready"));
Expand All @@ -235,16 +263,16 @@ bool EFM32Loader::waitForData(int timeout)
timer.setInterval(timeout);
timer.setSingleShot(true);

connect(m_serialPort, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
connect(_serialPort, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));

if(m_serialPort->bytesAvailable() > 0)
if(_serialPort->bytesAvailable() > 0)
return true;
else
{
timer.start();
eventLoop.exec();
return (m_serialPort->bytesAvailable() > 0);
return (_serialPort->bytesAvailable() > 0);
}
}

Expand Down
16 changes: 12 additions & 4 deletions efm32loader.h
Expand Up @@ -29,31 +29,39 @@ class EFM32Loader : public QObject
{
Q_OBJECT
public:
enum Transport
{
TransportUART,
TransportUSB
};

explicit EFM32Loader(QObject *parent = 0);

QSerialPort *serialPort() { return m_serialPort; }
QSerialPort *serialPort() { return _serialPort; }
void setBootEnablePolarity(bool high);
void setTransport(Transport transport);

signals:
void output(QString);

public slots:
bool open(const QString &portName);
void close();
bool detect();
bool upload(const QString &filePath);

private:
QSerialPort *m_serialPort;
XMODEM *m_xmodem;
QSerialPort *_serialPort;
XMODEM *_xmodem;
bool _bootEnablePolarity;
Transport _transport;

bool waitForChipID();
bool waitForReady();
bool waitForData(int timeout);

void enterBoot();
void exitBoot();
bool detect();
void reset();
};

Expand Down
14 changes: 7 additions & 7 deletions helpdialog.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>351</width>
<height>178</height>
<width>417</width>
<height>217</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -23,7 +23,7 @@
</sizepolicy>
</property>
<property name="text">
<string>Connections:</string>
<string>UART Connections:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
Expand Down Expand Up @@ -68,10 +68,10 @@
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;efm32_loader call also be used as a command-line application. Usage:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;efm32_loader may also be used as a command-line application.&lt;br/&gt;Usage depends on chosen transport type.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
Expand All @@ -83,10 +83,10 @@
</font>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;efm32_loader &amp;lt;port_name&amp;gt; &amp;lt;bin_file&amp;gt; &amp;lt;boot_pol&amp;gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;UART:&lt;/span&gt; efm32_loader &amp;lt;port_name&amp;gt; &amp;lt;bin_file&amp;gt; uart &amp;lt;boot_pol&amp;gt;&lt;br/&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;USB:&lt;/span&gt; efm32_loader &amp;lt;port_name&amp;gt; &amp;lt;bin_file&amp;gt; usb&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
Expand Down
18 changes: 12 additions & 6 deletions mainwindow.cpp
Expand Up @@ -34,9 +34,9 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->buttonHelp, SIGNAL(clicked()), this, SLOT(slotHelp()));
connect(ui->buttonReload, SIGNAL(clicked()), this, SLOT(slotReloadSerialPorts()));
connect(ui->buttonBrowse, SIGNAL(clicked()), this, SLOT(slotBrowse()));
connect(ui->buttonDetect, SIGNAL(clicked()), this, SLOT(slotDetect()));
connect(ui->buttonUpload, SIGNAL(clicked()), this, SLOT(slotUpload()));
connect(ui->buttonConnect, SIGNAL(clicked()), this, SLOT(slotConnect()));
connect(ui->comboTransport, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTransport()));

connect(ui->lineASCII, SIGNAL(returnPressed()), this, SLOT(slotSendASCII()));

Expand Down Expand Up @@ -115,11 +115,14 @@ void MainWindow::slotConnect()
updateInterface();
}

void MainWindow::slotDetect()
void MainWindow::slotTransport()
{
disconnect(serialPort, SIGNAL(readyRead()), this, SLOT(slotDataReady()));
loader->detect();
connect(serialPort, SIGNAL(readyRead()), this, SLOT(slotDataReady()));
bool transportIsUart = ui->comboTransport->currentText() == "UART";
if(transportIsUart)
loader->setTransport(EFM32Loader::TransportUART);
else
loader->setTransport(EFM32Loader::TransportUSB);
updateInterface();
}

void MainWindow::slotUpload()
Expand Down Expand Up @@ -150,7 +153,6 @@ void MainWindow::slotDataReady()
void MainWindow::updateInterface()
{
ui->buttonConnect->setDisabled(ui->comboPort->count() == 0);
ui->buttonDetect->setEnabled(m_connected);
ui->buttonUpload->setEnabled(m_connected);

if(m_connected)
Expand All @@ -159,6 +161,10 @@ void MainWindow::updateInterface()
ui->buttonConnect->setText(tr("Connect"));

ui->comboPort->setEnabled(!m_connected);

bool transportIsUart = ui->comboTransport->currentText() == "UART";
ui->labelBootEn->setVisible(transportIsUart);
ui->comboBootEnPol->setVisible(transportIsUart);
}

#endif //EFM32_LOADER_GUI
2 changes: 1 addition & 1 deletion mainwindow.h
Expand Up @@ -42,9 +42,9 @@ private slots:
void slotHelp();
void slotReloadSerialPorts();
void slotBrowse();
void slotDetect();
void slotUpload();
void slotConnect();
void slotTransport();
void slotSendASCII();
void slotDataReady();
void log(const QString &text);
Expand Down

0 comments on commit d99b9bf

Please sign in to comment.