FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
eeprom-programmer/eeprom_programmer_PC/src/memorycomm.cpp at main · feer9/eeprom-programmer · GitHub
feer9
/
eeprom-programmer
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
eeprom-programmer
/
eeprom_programmer_PC
/
src
/
memorycomm.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
387 lines (328 loc) · 8.87 KB
Breadcrumbs
eeprom-programmer
/
eeprom_programmer_PC
/
src
/
memorycomm.cpp
Copy path
File metadata and controls
387 lines (328 loc) · 8.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* EEPROM-Programmer - Read and write EEPROM memories.
* Copyright (C) 2022 Fernando Coda <fcoda@pm.me>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#
include
"
memorycomm.h
"
MemoryComm::MemoryComm
(
int
&argc,
char
**argv,
FILE
* outStream)
: QCoreApplication(argc, argv)
, m_buffer()
, m_pkg(
pkgdata_t
({
CMD_NONE
,m_buffer}))
, m_standardOutput(outStream)
, m_serialPortWriter(&m_serialPort, outStream)
, m_serialPortReader(&m_serialPort, outStream)
{
//
m_pkg.cmd = CMD_NONE;
//
m_pkg.data = &m_buffer;
setSignals
();
}
void
MemoryComm::setSignals
()
{
connect
(&m_serialPortReader, &SerialPortReader::packageReady,
this
, &MemoryComm::handlePackageReceived);
connect
(&m_serialPortReader, &SerialPortReader::timeout,
this
, &MemoryComm::handleRxTimedOut);
connect
(&m_serialPortWriter, &SerialPortWriter::packageSent,
this
, &MemoryComm::handlePackageSent);
connect
( qApp, &QCoreApplication::aboutToQuit,
this
, &MemoryComm::handleAppQuit);
}
void
MemoryComm::setSerialPortOptions
(SerialPortOptions& op)
{
m_serialPort.
setPortName
(op.
name
);
m_serialPort.
setBaudRate
(op.
baudrate
);
m_serialPort.
setDataBits
(op.
databits
);
m_serialPort.
setParity
(op.
parity
);
m_serialPort.
setStopBits
(op.
stopbits
);
m_serialPort.
setFlowControl
(op.
flowcontrol
);
}
MemoryComm::~MemoryComm
()
{
qDebug
() <<
"
MemoryComm destructor.
"
;
if
(m_serialPort.
isOpen
()) {
qDebug
() <<
"
SerialPort is still open.
"
;
m_serialPort.
close
();
}
}
void
MemoryComm::handleAppQuit
()
{
qDebug
() <<
"
MemoryComm::HandleAppQuit()
"
;
if
(m_serialPort.
isOpen
()) {
qDebug
() <<
"
SerialPort connected. Sending CMD_DISCONNECT...
"
;
sendCommand
(
CMD_DISCONNECT
);
while
(m_serialPortWriter.
busy
() ==
true
)
if
(m_serialPort.
waitForBytesWritten
(
100
) ==
false
)
break
;
//
TODO: this doesn't seems to return on time on linux.
//
Nor does the signal bytesWritten get emmited.
m_serialPort.
close
();
}
else
{
qDebug
() <<
"
SerialPort not connected.
"
;
}
}
#
ifdef
_WIN32
bool
MemoryComm::handleSignal
(
int
signal)
{
qDebug
() <<
"
Handling signal
"
<< signal;
if
(signal &
DEFAULT_SIGNALS
) {
QTimer::singleShot
(
0
, qApp,
SLOT
(
quit
()));
//
The thread is going to stop soon, so don't propagate this signal further
return
true
;
}
else
{
//
Let the signal propagate as though we had not been there
return
false
;
}
}
#
endif
void
MemoryComm::clearBuffers
() {
m_serialPortReader.
clearBuffer
();
m_buffer.
clear
();
}
bool
MemoryComm::sendCommand
(commands_e cmd,
uint8_t
data) {
return
sendCommand
(cmd,
QByteArray
(
1
,
char
(data)));
}
bool
MemoryComm::sendCommand
(commands_e cmd) {
return
sendCommand
(cmd,
QByteArray
());
}
//
return true on success, false otherwise
bool
MemoryComm::sendCommand
(commands_e cmd,
const
QByteArray& data) {
qDebug
() << Qt::endl <<
"
about to send command
"
<<
EEPROM::getCommandName
(cmd) <<
"
with
"
<< data.
size
() <<
"
bytes of data.
"
;
m_lastTxCmd = cmd;
qint64 ret = m_serialPortWriter.
send
(cmd, data);
bool
success = ret != -
1
;
if
(success) {
setRxTimeout
(cmd);
}
else
{
qDebug
() <<
"
Error sending command
"
<<
EEPROM::getCommandName
(cmd);
}
return
success;
}
bool
MemoryComm::readMem
() {
m_buffer.
clear
();
m_operation =
OP_RX
;
m_commState =
COMM_READMEM_WAIT_OK
;
return
sendCommand
(
CMD_READMEM
, m_memtype);
}
bool
MemoryComm::writeMem
(
const
QByteArray& memBuffer) {
m_operation =
OP_TX
;
m_commState =
COMM_WRITEMEM_WAIT_OK
;
m_memindex =
0
;
m_memBuffer = memBuffer;
//
CHECK HOW THIS WORKS
m_buffer.
clear
();
m_serialPortReader.
clearBuffer
();
m_serialPort.
clear
(QSerialPort::Input);
return
sendCommand
(
CMD_WRITEMEM
, m_memtype);
}
bool
MemoryComm::sendMemoryBlock
() {
int
memidx = m_memindex;
m_memindex +=
PKG_DATA_MAX
;
return
sendCommand
(
CMD_MEMDATA
, m_memBuffer.
mid
(memidx,
PKG_DATA_MAX
));
}
bool
MemoryComm::sendCommand_memid
() {
return
sendCommand
(
CMD_MEMID
, m_memtype);
}
bool
MemoryComm::sendCommand_ping
(
void
) {
return
sendCommand
(
CMD_PING
);
}
void
MemoryComm::handleRxCrcError
()
{
//
see m_lastRxCmd and m_lastTxCmd
}
void
MemoryComm::reconnect
(
void
) {
/*
m_serialPort.close();
if (!m_serialPort.open(QIODevice::ReadWrite)) {
m_standardOutput << "Unable to open serial port. Aborting." << Qt::endl;
QCoreApplication::exit(1);
}
*/
}
void
MemoryComm::handleRxTimedOut
() {
//
chequear si no estaba esperando nada no le des bola a este timeout
if
(m_operation ==
OP_RX
) {
m_standardOutput <<
"
Reading from memory timed out.
"
<< Qt::endl;
reconnect
();
}
else
if
(m_operation ==
OP_TX
) {
m_standardOutput <<
"
Writing to memory timed out.
"
<< Qt::endl;
reconnect
();
}
else
{
m_standardOutput <<
"
RX timed out.
"
<< Qt::endl;
}
m_operation =
OP_NONE
;
}
void
MemoryComm::errorReceived
(
package_t
*pkg)
{
if
(pkg->
cmd
==
CMD_ERR
) {
m_standardOutput <<
"
ReadMem: uC error:
"
<<
EEPROM::getErrorMsg
(
errorcode_e
(pkg->
data
[
0
]))
<< Qt::endl;
}
m_commState =
COMM_IDLE
;
m_operation =
OP_NONE
;
//
forward error to application
packageReady
(pkg);
}
void
MemoryComm::handlePackageReceived
(
package_t
*pkg)
{
qDebug
() <<
"
Received command
"
<<
EEPROM::getCommandName
(pkg->
cmd
);
if
(!
CRC16::check
(pkg))
{
handleRxCrcError
();
}
else
{
switch
(m_commState)
{
case
COMM_IDLE
:
//
not in transfer
//
just forward package to application
packageReady
(pkg);
break
;
case
COMM_READMEM_WAIT_OK
:
//
readmem sent, waiting confirmation
if
(pkg->
cmd
==
CMD_OK
) {
sendCommand
(
CMD_READNEXT
);
m_commState =
COMM_READMEM_WAIT_DATA
;
}
else
{
errorReceived
(pkg);
}
break
;
case
COMM_READMEM_WAIT_DATA
:
if
(pkg->
cmd
==
CMD_MEMDATA
)
{
m_buffer.
append
((
char
*)(pkg->
data
), pkg->
datalen
);
qDebug
(
"
Received %lld bytes out of %d
"
, m_buffer.
size
(), m_memsize);
if
(m_buffer.
size
() < m_memsize) {
sendCommand
(
CMD_TXRX_ACK
);
m_pending.
append
(
CMD_READNEXT
);
}
else
{
m_commState =
COMM_IDLE
;
m_operation =
OP_NONE
;
sendCommand
(
CMD_TXRX_DONE
);
pkg->
data
= (
uint8_t
*)(m_buffer.
data
());
pkg->
datalen
= m_buffer.
size
();
packageReady
(pkg);
}
}
else
{
errorReceived
(pkg);
}
break
;
case
COMM_WRITEMEM_WAIT_OK
:
if
(pkg->
cmd
==
CMD_OK
) {
sendMemoryBlock
();
m_commState =
COMM_WRITEMEM_WAIT_ACK
;
}
else
{
errorReceived
(pkg);
}
break
;
case
COMM_WRITEMEM_WAIT_ACK
:
qDebug
(
"
Sent %d bytes out of %d
"
, m_memindex, m_memsize);
if
(pkg->
cmd
==
CMD_TXRX_ACK
) {
if
(m_memindex >= m_memsize) {
//
uC is doing some unintelligent thing
setPackageError
(pkg,
ERROR_MEMIDX
);
errorReceived
(pkg);
}
else
{
if
(!
sendMemoryBlock
()) {
setPackageError
(pkg,
ERROR_COMM
);
errorReceived
(pkg);
}
}
}
else
if
(pkg->
cmd
==
CMD_TXRX_DONE
&& m_memindex == m_memsize) {
m_commState =
COMM_IDLE
;
m_operation =
OP_NONE
;
packageReady
(pkg);
}
else
{
errorReceived
(pkg);
}
break
;
}
}
m_lastRxCmd = pkg->
cmd
;
}
//
TODO: split in methods.
void
MemoryComm::setPackageError
(
package_t
*pkg, errorcode_e err)
{
pkg->
cmd
=
CMD_ERR
;
pkg->
datalen
=
1
;
m_buffer[
0
] = err;
pkg->
data
= (
uint8_t
*)(m_buffer.
data
());
}
void
MemoryComm::packageReady
(
package_t
*pkg)
{
m_pkg.
cmd
= pkg->
cmd
;
QByteArray
data
((
char
*)(pkg->
data
), pkg->
datalen
);
m_pkg.
data
= data;
handleXfer
(&m_pkg);
}
void
MemoryComm::handlePackageSent
(commands_e cmd)
{
qDebug
() <<
"
Finished sending command:
"
<<
EEPROM::getCommandName
(cmd);
if
(!m_pending.
isEmpty
()) {
uint8_t
command = m_pending[
0
];
m_pending.
remove
(
0
,
1
);
sendCommand
(
commands_e
(command));
//
TODO: this doesn't account for command data
}
}
//
when we send <cmd>, expect an answer in X time
void
MemoryComm::setRxTimeout
(commands_e cmd)
{
switch
(cmd)
{
case
CMD_NONE
:
case
CMD_STARTXFER
:
case
CMD_ENDXFER
:
case
CMD_IDLE
:
break
;
case
CMD_INIT
:
m_serialPortReader.
startRxTimeout
(
2000
);
break
;
case
CMD_PING
:
case
CMD_MEMID
:
case
CMD_DATA
:
m_serialPortReader.
startRxTimeout
(
200
);
break
;
case
CMD_DISCONNECT
:
case
CMD_OK
:
case
CMD_ERR
:
case
CMD_TXRX_DONE
:
m_serialPortReader.
stopRxTimeout
();
break
;
case
CMD_TXRX_ACK
:
case
CMD_TXRX_ERR
:
case
CMD_READNEXT
:
case
CMD_MEMDATA
:
case
CMD_INFO
:
m_serialPortReader.
startRxTimeout
(
1500
);
break
;
case
CMD_READMEM
:
case
CMD_WRITEMEM
:
m_serialPortReader.
startRxTimeout
(
7000
);
break
;
//
TODO: check all this timing thing
}
}
Back
|
FazBrowse Home
|
New Git URL