From f910b7d057c225e76445ce6907c0b99b30ffd2cd Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Sun, 10 Dec 2023 14:43:06 +0100 Subject: [PATCH] DownloadThread: add real dc-download implementation. --- src/download_thread.cpp | 101 +++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/src/download_thread.cpp b/src/download_thread.cpp index a05bf1a..a5f0e9e 100644 --- a/src/download_thread.cpp +++ b/src/download_thread.cpp @@ -99,57 +99,63 @@ DownloadThread::~DownloadThread() { // bootloader is really not running anymore. */ void DownloadThread::run() { -// download thread running in ca-master sends the dc-file down to firmware -#if 0 - Update::DownloadResult Update::dc_downloadBinary(QByteArray const &b) const { - int const nBlocks = (((b.size())%64)==0) ? (b.size()/64) : (b.size()/64)+1; - - // fill lst block of data to be sent with 0xFF - QByteArray ba = b.leftJustified(nBlocks*64, (char)(0xFF)); - - qInfo() << "total number of bytes to send to dc" << ba.size(); - qInfo() << "total number of blocks to send to dc" << nBlocks; - - int bNum = 0; - DownloadResult res = DownloadResult::OK; - fprintf(stderr, "\n64-byte block %04d ", bNum); - while (res != DownloadResult::ERROR && bNum < nBlocks) { - if ((res = sendNextAddress(bNum)) != DownloadResult::ERROR) { - if ((res = sendNextDataBlock(ba, bNum)) != DownloadResult::ERROR) { - bNum += 1; - fprintf(stderr, "."); - if ((bNum % 80) == 0) { - fprintf(stderr, "\n64-byte block %04d ", bNum); - } - } - } - } - fprintf(stderr, "\nlast 64-byte block %04d\n", bNum); - - int const rest = ba.size() % 64; - int const offset = ba.size() - rest; - char const *startAddress = ba.constData() + offset; - - if (rest > 0) { - // SHOULD NEVER HAPPEN !!! - uint8_t local[66]; - memset(local, 0xFF, sizeof(local)); - memcpy(local, startAddress, rest); - qCritical() << "ERROR SEND REMAINING" << rest << "BYTES"; - m_hw->bl_sendDataBlock(64, local); - } - - m_hw->bl_sendLastBlock(); - qInfo() << "last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK()); - return res; - } - -#endif + // download thread running in ca-master sends the dc-file down to firmware + // TODO: send the json files as well m_hw->dcDownloadRequestAck(); - qCritical() << QDateTime::currentDateTime().toString(Qt::ISODate) + "DOWNLOAD THREAD STARTED"; + qCritical() << "DownloadThread::run(): DOWNLOAD THREAD STARTED"; + // load binary device controller file into memory + QByteArray ba = loadBinaryDCFile(m_hw->dcDownloadFileName()); + if (ba.size() > 0) { + uint16_t const totalBlocks = (((ba.size())%64)==0) ? (ba.size()/64) : (ba.size()/64)+1; + m_hw->dcDownloadSetTotalBlockNumber(totalBlocks); + + // fill last block of data to be sent with 0xFF + ba = ba.leftJustified(totalBlocks*64, (char)(0xFF)); + + resetDeviceController(); + if (startBootloader()) { + + qCritical() << "DownloadThread::run(): TOTAL NUMBER OF BYTES TO SEND TO DC" << ba.size(); + qCritical() << "DownloadThread::run(): TOTAL NUMBER OF BLOCKS" << totalBlocks; + + int currentBlock = 0; + DownloadResult res = DownloadResult::OK; + qCritical() << "64-byte block " << currentBlock; + while (res != DownloadResult::ERROR && currentBlock < totalBlocks) { + if ((res = sendNextAddress(currentBlock)) != DownloadResult::ERROR) { + if ((res = sendNextDataBlock(ba, currentBlock)) != DownloadResult::ERROR) { + m_hw->dcDownloadSetCurrentBlockNumber(currentBlock); + currentBlock += 1; + } + } + } + + qCritical() << "DownloadThread::run(): last 64-byte block %04d" << currentBlock; + + int const rest = ba.size() % 64; + int const offset = ba.size() - rest; + char const *startAddress = ba.constData() + offset; + + if (rest > 0) { + // SHOULD NEVER HAPPEN !!! + uint8_t local[66]; + memset(local, 0xFF, sizeof(local)); + memcpy(local, startAddress, rest); + qCritical() << "DownloadThread::run(): ERROR SEND REMAINING" << rest << "BYTES"; + m_hw->bl_sendDataBlock(64, local); + } else { + m_hw->bl_sendLastBlock(); + m_hw->dcDownloadSetCurrentBlockNumber(currentBlock); + } + qCritical() << "DownloadThread::run(): last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK()); + } + stopBootloader(); // there is no harm in stopping the bootloader even + } // if it was not started at all + +#if 0 // test code: uint16_t const totalBlocks = 100; m_hw->dcDownloadSetTotalBlockNumber(totalBlocks); @@ -158,6 +164,7 @@ void DownloadThread::run() { m_hw->dcDownloadSetCurrentBlockNumber(currentBlock); QThread::msleep(100); } +#endif m_hw->dcDownloadSetRunning(false); m_hw->dcDownloadSetFinished(true);