dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
tl_dfu.h
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #ifndef TL_DFU_H
28 #define TL_DFU_H
29 
30 #include <rawhid/hidapi/hidapi.h>
31 #include <rawhid/usbsignalfilter.h>
32 #include <QDebug>
33 #include <QFile>
34 #include <QThread>
35 #include <QTimer>
36 #include "bl_messages.h"
37 
38 using namespace std;
39 #define BUF_LEN 64
40 #define BL_CAP_EXTENSION_MAGIC 0x3456
41 
42 namespace tl_dfu {
43 
44 enum Status {
45  DFUidle, // 0
46  uploading, // 1
52  idle, // 7
56  CRC_Fail, // 11
57  failed_jump, // 12
58  abort, // 13
60 };
61 
62 struct device
63 {
64  quint16 ID;
65  quint32 FW_CRC;
66  quint8 BL_Version;
68  quint32 SizeOfCode;
69  bool Readable;
70  bool Writable;
71  QVector<quint32> PartitionSizes;
72  int HW_Rev;
73  bool CapExt;
74 };
75 
76 class DFUObject : public QThread
77 {
78  Q_OBJECT
79 
80  typedef struct messagePackets
81  {
82  quint32 numberOfPackets;
84  int pad;
85  } messagePackets;
86 
87  typedef struct statusReport
88  {
89  quint32 additional;
91  } statusReport;
92 
93 public:
94  static quint32 CRCFromQBArray(QByteArray array, quint32 Size);
95  DFUObject();
96  ~DFUObject();
97 
98  // Service commands:
99  int JumpToApp(bool);
100  int ResetDevice(void);
101  bool OpenBootloaderComs(USBPortInfo port);
102  void CloseBootloaderComs();
103 
104  // Partition operations:
105  bool UploadPartitionThreaded(QByteArray &sourceArray, dfu_partition_label partition, int size);
106  bool DownloadPartitionThreaded(QByteArray *firmwareArray, dfu_partition_label partition,
107  int size);
108  bool WipePartition(dfu_partition_label partition);
109  QByteArray DownloadDescriptionAsByteArray(int const &numberOfChars);
110 
111 public slots:
112  device findCapabilities();
113  QString partitionStringFromLabel(dfu_partition_label label);
114 
115 signals:
116  void downloadFinished(bool);
117  void uploadFinished(tl_dfu::Status);
118  void operationProgress(QString status, int progress);
119 
120 private:
121  bool DownloadPartition(QByteArray *fw, qint32 const &numberOfBytes,
122  const dfu_partition_label &partition);
123  tl_dfu::Status UploadPartition(QByteArray &sfile, dfu_partition_label partition);
124 
125  // Helper functions:
126  QString StatusToString(tl_dfu::Status const &status);
127  static quint32 CRC32WideFast(quint32 Crc, quint32 Size, quint32 *Buffer);
128  void CopyWords(char *source, char *destination, int count);
129  messagePackets CalculatePadding(quint32 numberOfBytes);
130 
131  // Service commands:
132  bool EnterDFU();
133  statusReport StatusRequest();
134  bool EndOperation();
135  int AbortOperation(void);
136 
137  // USB coms:
138  int SendData(bl_messages);
139  int ReceiveData(bl_messages &data, int timeoutMS = 10000);
140  hid_device *m_hidHandle;
141 
142  bool StartUpload(qint32 const &numberOfBytes, const dfu_partition_label &label, quint32 crc);
143  bool UploadData(qint32 const &numberOfPackets, QByteArray &data);
144 
145  typedef struct ThreadJobStruc
146  {
147  enum Actions { Download, Upload };
148  qint32 requestSize;
150  QByteArray *requestStorage;
151  quint32 partition_size;
153  } ThreadJobStruc;
154  ThreadJobStruc threadJob;
155 
156 protected:
157  void run(); // Executes the upload or download operations
158 };
159 }
160 
162 
163 #endif // TL_DFU_H
quint8 BL_Version
Definition: tl_dfu.h:66
int SizeOfDesc
Definition: tl_dfu.h:67
quint32 SizeOfCode
Definition: tl_dfu.h:68
dfu_partition_label requestTransferType
Definition: tl_dfu.h:149
quint32 FW_CRC
Definition: tl_dfu.h:65
bool CapExt
Definition: tl_dfu.h:73
bool Readable
Definition: tl_dfu.h:69
DataFields data
dfu_partition_label
Definition: bl_messages.h:96
Status
Definition: tl_dfu.h:44
int HW_Rev
Definition: tl_dfu.h:72
bool Writable
Definition: tl_dfu.h:70
QVector< quint32 > PartitionSizes
Definition: tl_dfu.h:71
Q_DECLARE_METATYPE(Core::Internal::MenuActionContainer *) using namespace Core
quint16 ID
Definition: tl_dfu.h:64
function crc