@@ -55,6 +55,20 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_start()
55
55
#endif
56
56
}
57
57
58
+ /* Serial.write with care of the overflow */
59
+ uint8_t serialWrite (uint8_t data)
60
+ {
61
+ uint8_t result;
62
+ if (Serial.availableForWrite ())
63
+ {
64
+ Serial.write (data);
65
+ result = 0 ;
66
+ } else {
67
+ result = 0 ;
68
+ }
69
+ return result;
70
+ }
71
+
58
72
/* *
59
73
* Trace ends (close the file for instance). This event is sent by
60
74
* ShutdownOS() system call
@@ -65,6 +79,48 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_close()
65
79
66
80
}
67
81
82
+ /* *
83
+ * Overflow. there are too many trace messages and the trace subsystem
84
+ * cannot handle all this stuff.
85
+ * we just flush the fifo and send a trace overflow message. There will
86
+ * be lost trace messages.
87
+ * NOTE: ***** the system should be able to send 5 bytes at minimum *****
88
+ * (the TX serial line fifo should be > 5 bytes).
89
+ *
90
+ */
91
+ FUNC (void , OS_CODE) tpl_trace_overflow()
92
+ {
93
+ const tpl_tick ts=tpl_trace_get_timestamp ();
94
+ tpl_trace_start ();
95
+ # if TRACE_FORMAT == TRACE_FORMAT_SERIAL
96
+ // there was an overflow, first flush the serial line
97
+ // we do not generate any overflow here of course
98
+ // to prevent recursive calls.
99
+ //
100
+ // in Arduino, we cannot empty the Serial line
101
+ // we have to wait for the end of transmission...
102
+ // however, at last one message is not completed.
103
+ Serial.flush ();
104
+ /* TTT 00000 (Type) */
105
+ uint8_t byte = OVERFLOW << 5 ;
106
+ uint8_t chksum = byte;
107
+ Serial.write (byte);
108
+
109
+ byte = ts >> 8 ;
110
+ chksum += byte;
111
+ Serial.write (byte);
112
+
113
+ byte = ts & 0xff ;
114
+ chksum += byte;
115
+ Serial.write (byte);
116
+
117
+ Serial.write (0 ); // no data
118
+ Serial.write (chksum);
119
+ # else
120
+ # error "unsupported trace mode: TRACE_FORMAT"
121
+ # endif /* TRACE_FORMAT == TRACE_FORMAT_SERIAL */
122
+ }
123
+
68
124
/* *
69
125
* trace the execution of a task or ISR
70
126
* ** Function defined in os/tpl_trace.h **
@@ -80,21 +136,23 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_proc_change_state(
80
136
/* TTT 00 SSS (Type, State) */
81
137
uint8_t byte = PROC_TYPE<<5 | target_state;
82
138
uint8_t chksum = byte;
83
- Serial.write (byte);
139
+ uint8_t overflow = 0 ;
140
+ overflow |= serialWrite (byte);
84
141
85
142
byte = ts >> 8 ;
86
143
chksum += byte;
87
- Serial. write (byte);
144
+ overflow |= serialWrite (byte);
88
145
89
146
byte = ts & 0xff ;
90
147
chksum += byte;
91
- Serial. write (byte);
148
+ overflow |= serialWrite (byte);
92
149
93
150
byte = proc_id & 0xff ;
94
151
chksum += byte;
95
- Serial. write (byte);
152
+ overflow |= serialWrite (byte);
96
153
97
- Serial.write (chksum);
154
+ overflow |= serialWrite (chksum);
155
+ if (overflow) tpl_trace_overflow ();
98
156
# else
99
157
# error "unsupported trace mode: TRACE_FORMAT"
100
158
#endif
@@ -116,21 +174,23 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_res_change_state(
116
174
/* TTT 00 00S (Type, State) */
117
175
uint8_t byte = (RES_TYPE<<5 ) | (target_state & 1 );
118
176
uint8_t chksum = byte;
119
- Serial.write (byte);
177
+ uint8_t overflow = 0 ;
178
+ overflow |= serialWrite (byte);
120
179
121
180
byte = ts >> 8 ;
122
181
chksum += byte;
123
- Serial. write (byte);
182
+ overflow |= serialWrite (byte);
124
183
125
184
byte = ts & 0xff ;
126
185
chksum += byte;
127
- Serial. write (byte);
186
+ overflow |= serialWrite (byte);
128
187
129
188
byte = res_id & 0xff ;
130
189
chksum += byte;
131
- Serial. write (byte);
190
+ overflow |= serialWrite (byte);
132
191
133
- Serial.write (chksum);
192
+ overflow |= serialWrite (chksum);
193
+ if (overflow) tpl_trace_overflow ();
134
194
# else
135
195
# error "unsupported trace mode: TRACE_FORMAT"
136
196
#endif
@@ -151,21 +211,23 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_time_obj_change_state(
151
211
/* TTT K0 SSS (Type, Kind, State) */
152
212
uint8_t byte = TIMEOBJ_TYPE<<5 | TIMEOBJ_CHANGE_STATE_KIND <<4 | target_state;
153
213
uint8_t chksum = byte;
154
- Serial.write (byte);
214
+ uint8_t overflow = 0 ;
215
+ overflow |= serialWrite (byte);
155
216
156
217
byte = ts >> 8 ;
157
218
chksum += byte;
158
- Serial. write (byte);
219
+ overflow |= serialWrite (byte);
159
220
160
221
byte = ts & 0xff ;
161
222
chksum += byte;
162
- Serial. write (byte);
223
+ overflow |= serialWrite (byte);
163
224
164
225
byte = timeobj_id & 0xff ;
165
226
chksum += byte;
166
- Serial. write (byte);
227
+ overflow |= serialWrite (byte);
167
228
168
- Serial.write (chksum);
229
+ overflow |= serialWrite (chksum);
230
+ if (overflow) tpl_trace_overflow ();
169
231
# else
170
232
# error "unsupported trace mode: TRACE_FORMAT"
171
233
#endif
@@ -185,21 +247,23 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_time_obj_expire(
185
247
/* TTT K0000 (Type, Kind) */
186
248
uint8_t byte = TIMEOBJ_TYPE<<5 | TIMEOBJ_EXPIRE_KIND <<4 ;
187
249
uint8_t chksum = byte;
188
- Serial.write (byte);
250
+ uint8_t overflow = 0 ;
251
+ overflow |= serialWrite (byte);
189
252
190
253
byte = ts >> 8 ;
191
254
chksum += byte;
192
- Serial. write (byte);
255
+ overflow |= serialWrite (byte);
193
256
194
257
byte = ts & 0xff ;
195
258
chksum += byte;
196
- Serial. write (byte);
259
+ overflow |= serialWrite (byte);
197
260
198
261
byte = timeobj_id & 0xff ;
199
262
chksum += byte;
200
- Serial. write (byte);
263
+ overflow |= serialWrite (byte);
201
264
202
- Serial.write (chksum);
265
+ overflow |= serialWrite (chksum);
266
+ if (overflow) tpl_trace_overflow ();
203
267
# else
204
268
# error "unsupported trace mode: TRACE_FORMAT"
205
269
#endif
@@ -221,22 +285,24 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_event_set(
221
285
/* TTT EEEEE (Type, Event) */
222
286
uint8_t byte = EVENT_TYPE<<5 | (event & 0x1F );
223
287
uint8_t chksum = byte;
224
- Serial.write (byte);
288
+ uint8_t overflow = 0 ;
289
+ overflow |= serialWrite (byte);
225
290
226
291
byte = ts >> 8 ;
227
292
chksum += byte;
228
- Serial. write (byte);
293
+ overflow |= serialWrite (byte);
229
294
230
295
byte = ts & 0xff ;
231
296
chksum += byte;
232
- Serial. write (byte);
297
+ overflow |= serialWrite (byte);
233
298
234
299
// TODO: limited to 128 tasks
235
300
byte = (EVENT_SET_KIND << 7 ) | (task_target_id & 0x7f );
236
301
chksum += byte;
237
- Serial. write (byte);
302
+ overflow |= serialWrite (byte);
238
303
239
- Serial.write (chksum);
304
+ overflow |= serialWrite (chksum);
305
+ if (overflow) tpl_trace_overflow ();
240
306
# else
241
307
# error "unsupported trace mode: TRACE_FORMAT"
242
308
#endif
@@ -257,21 +323,23 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_event_reset(
257
323
/* TTT EEEEE (Type, Event) */
258
324
uint8_t byte = EVENT_TYPE<<5 | (event & 0x1F );
259
325
uint8_t chksum = byte;
260
- Serial.write (byte);
326
+ uint8_t overflow = 0 ;
327
+ overflow |= serialWrite (byte);
261
328
262
329
byte = ts >> 8 ;
263
330
chksum += byte;
264
- Serial. write (byte);
331
+ overflow |= serialWrite (byte);
265
332
266
333
byte = ts & 0xff ;
267
334
chksum += byte;
268
- Serial. write (byte);
335
+ overflow |= serialWrite (byte);
269
336
270
337
byte = EVENT_RESET_KIND << 7 ;
271
338
chksum += byte;
272
- Serial. write (byte);
339
+ overflow |= serialWrite (byte);
273
340
274
- Serial.write (chksum);
341
+ overflow |= serialWrite (chksum);
342
+ if (overflow) tpl_trace_overflow ();
275
343
# else
276
344
# error "unsupported trace mode: TRACE_FORMAT"
277
345
#endif
@@ -293,21 +361,23 @@ extern "C" FUNC(void, OS_CODE) tpl_trace_msg_send(
293
361
/* TTT MMMMM (Type, Message) */
294
362
uint8_t byte = MESSAGE_TYPE << 5 | (mess_id & 0x1F );
295
363
uint8_t chksum = byte;
296
- Serial.write (byte);
364
+ uint8_t overflow = 0 ;
365
+ overflow |= serialWrite (byte);
297
366
298
367
byte = ts >> 8 ;
299
368
chksum += byte;
300
- Serial. write (byte);
369
+ overflow |= serialWrite (byte);
301
370
302
371
byte = ts & 0xff ;
303
372
chksum += byte;
304
- Serial. write (byte);
373
+ overflow |= serialWrite (byte);
305
374
306
375
byte = (uint8_t )is_zero_message;
307
376
chksum += byte;
308
- Serial. write (byte);
377
+ overflow |= serialWrite (byte);
309
378
310
- Serial.write (chksum);
379
+ overflow |= serialWrite (chksum);
380
+ if (overflow) tpl_trace_overflow ();
311
381
# else
312
382
# error "unsupported trace mode: TRACE_FORMAT"
313
383
# endif /* TRACE_FORMAT == TRACE_FORMAT_SERIAL */
@@ -326,21 +396,23 @@ FUNC(void, OS_CODE) tpl_trace_msg_receive(
326
396
/* TTT MMMMM (Type, Message) */
327
397
uint8_t byte = MESSAGE_TYPE << 5 | (mess_id & 0x1F );
328
398
uint8_t chksum = byte;
329
- Serial.write (byte);
399
+ uint8_t overflow = 0 ;
400
+ overflow |= serialWrite (byte);
330
401
331
402
byte = ts >> 8 ;
332
403
chksum += byte;
333
- Serial. write (byte);
404
+ overflow |= serialWrite (byte);
334
405
335
406
byte = ts & 0xff ;
336
407
chksum += byte;
337
- Serial. write (byte);
408
+ overflow |= serialWrite (byte);
338
409
339
410
byte = (uint8_t )MESSAGE_RECEIVE_KIND;
340
411
chksum += byte;
341
- Serial. write (byte);
412
+ overflow |= serialWrite (byte);
342
413
343
- Serial.write (chksum);
414
+ overflow |= serialWrite (chksum);
415
+ if (overflow) tpl_trace_overflow ();
344
416
# else
345
417
# error "unsupported trace mode: TRACE_FORMAT"
346
418
# endif /* TRACE_FORMAT == TRACE_FORMAT_SERIAL */
0 commit comments