comparison src/cs/riviera/rvt/rvt_task.c @ 0:b6a5e36de839

src/cs: initial import from Magnetite
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 15 Jul 2018 04:39:26 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b6a5e36de839
1 /**************************************************************************
2 *
3 * rvt_task.c
4 *
5 * This contains the core of the Trace task.
6 *
7 * (C) Texas Instruments, all rights reserved
8 *
9 * Version number : 0.1
10 *
11 * History : 0.1 (7/5/2000) - Created
12 *
13 * Date : 7/5/2000
14 *
15 * Author : Guido Pagana
16 *
17 * Update : Pascal Puel
18 * : David Lamy-Charrier (changes for Riviera 1.6)
19 *
20 ***************************************************************************/
21
22
23 #include "nucleus.h"
24
25 #include "rv/general.h"
26 #include "rv/rv_general.h"
27 #include "rvf/rvf_api.h"
28 #include "rvt/rvt_gen.h"
29 #include "rvt/rvt_def_i.h"
30 #include "rvt/rvt_env.h"
31 #include "rvt/rvt_env_i.h"
32 #include "rvm/rvm_use_id_list.h"
33
34 #include "uart/serialswitch.h"
35
36 #ifndef _WINDOWS
37 #include "config/swconfig.cfg"
38 #endif
39
40 #include <string.h>
41
42 extern NU_HISR TI_rcv_HISR;
43
44 /* Time (in milliseconds) between two consecutive 'System Time' messages */
45 #ifdef PSEUDO_MODEM_USB
46 #define RVT_ALIVE_POLLING_TIME (RVF_MS_TO_TICKS (10000))
47 #elif defined(PSEUDO_MODEM_KEEPALIVE)
48 #define RVT_ALIVE_POLLING_TIME (RVF_MS_TO_TICKS (5000))
49 #else
50 #define RVT_ALIVE_POLLING_TIME (RVF_MS_TO_TICKS (20000))
51 #endif
52
53 #define RVT_WAIT_FOR_HEADER (1)
54 #define RVT_WAIT_FOR_DATA (2)
55
56 #ifdef PSEUDO_MODEM_KEEPALIVE
57 extern void rvt_keepalive_register(void);
58 extern void rvt_keepalive_process(void);
59 #endif
60
61 /********************************************************************************/
62 /* */
63 /* Function Name: rvt_task_core */
64 /* */
65 /* Purpose: Core of Trace task. */
66 /* */
67 /* Input Parameters: */
68 /* None. */
69 /* */
70 /* Output Parameters: */
71 /* None. */
72 /* */
73 /* Global Parameters: */
74 /* None. */
75 /* */
76 /* Note: */
77 /* None. */
78 /* */
79 /********************************************************************************/
80 T_RVM_RETURN rvt_task_core (void)
81 {
82 UINT16 event = 0;
83 UINT32 nb_bytes_sent = 0;
84
85 #ifdef PSEUDO_MODEM_KEEPALIVE
86 rvt_keepalive_register();
87 #endif
88
89 #ifdef FRAMING_PROTOCOL
90
91 // Request for the level of filtering, as well as the 32-bit
92 // mask related to the software entities to be monitored.
93 {
94 UINT8 trace_level_request[] = {RVM_INVALID_USE_ID, 0, 0, 0, 0, (RV_TRACE_LEVEL_ERROR - 1), 0, 0, 0, 0};
95
96 // Note that the level is defined as invalid
97 trace_level_request[0] = (char) rv_trace_user_id;
98
99 // Transmit an 'empty' message
100 nb_bytes_sent = 0;
101 while (nb_bytes_sent < sizeof (trace_level_request))
102 {
103 nb_bytes_sent += SER_tr_WriteNBytes (SER_LAYER_1,
104 trace_level_request + nb_bytes_sent,
105 sizeof (trace_level_request) - nb_bytes_sent);
106 }
107 }
108
109 // Start the 'Alive Polling Timer'
110 #if (OP_WCP == 0)
111 rvf_start_timer (RVF_TIMER_0,
112 RVT_ALIVE_POLLING_TIME,
113 TRUE);
114 #endif
115
116 for (;
117 ;
118 )
119 {
120 // Infinite wait on 'Trace Task' mailbox or timer events
121 event = rvf_wait ((1 << RVT_TRACE_MAILBOX) | (RVF_TIMER_0_EVT_MASK),
122 0);
123
124 // Check for some messages lost
125 if (((rvt_lost_msg_cpt.bit_mask).count >= RVT_MAX_LOST_TRACE_MSG) && (p_rvt_lost_msg))
126 {
127 INT8 count = 0;
128 UINT8 lost_msg_length = RVT_HDR_LENGTH + RVT_LOST_MSG_LENGTH;
129 UINT32 lost_msg_cpt = rvt_lost_msg_cpt.overall_value;
130
131 // Append with the number of messages lost
132 rvt_lost_msg_cpt.overall_value = 0;
133 for (count = 0;
134 count < RVT_HEX_VALUE_LENGTH;
135 count++)
136 {
137 p_rvt_lost_msg[lost_msg_length + count] = Num2Char[(UINT8) ((lost_msg_cpt << (count << 2)) >> 28)];
138 }
139 lost_msg_length += RVT_HEX_VALUE_LENGTH;
140
141 // Send message to the UART with byte stuffing
142 nb_bytes_sent = 0;
143 while (nb_bytes_sent < lost_msg_length)
144 {
145 nb_bytes_sent += SER_tr_WriteNBytes (SER_LAYER_1,
146 (UINT8 *) p_rvt_lost_msg + nb_bytes_sent,
147 lost_msg_length - nb_bytes_sent);
148 }
149
150 } // End of if ((rvt_lost_msg_cpt.bit_mask).count >= RVT_MAX_LOST_TRACE_MSG)
151
152 if (event & EVENT_MASK (RVT_TRACE_MAILBOX))
153 {
154 T_RV_HDR *msg = NULL;
155
156 // Read the message from the mailbox
157 if ((msg = (T_RV_HDR *) rvf_read_mbox (RVT_TRACE_MAILBOX)) != NULL)
158 {
159 if (msg->msg_id == RVT_TRACE_RQST_ID)
160 {
161 UINT8 msg_format = 0;
162 UINT32 msg_length = 0;
163
164 // Get the length
165 msg_length = ((T_RVT_TRACE_RQST *) msg)->msg_length;
166
167 // Get the format
168 msg_format = ((T_RVT_TRACE_RQST *) msg)->format;
169
170 // Copy the 'User ID'
171 ((UINT8 *) msg + RVT_HEADER_SIZE - 1)[0] = ((T_RVT_TRACE_RQST *) msg)->user_id;
172 msg_length++;
173
174 switch (msg_format)
175 {
176 case RVT_ASCII_FORMAT:
177 {
178 // Send message to the UART without byte stuffing
179 nb_bytes_sent = 0;
180 while (nb_bytes_sent < msg_length)
181 {
182 nb_bytes_sent += SER_tr_EncapsulateNChars (SER_LAYER_1,
183 (char *) msg + RVT_HEADER_SIZE - 1 + nb_bytes_sent,
184 msg_length - nb_bytes_sent);
185 }
186 break;
187 }
188 case RVT_BINARY_FORMAT:
189 {
190 // Send message to the UART with byte stuffing
191 nb_bytes_sent = 0;
192 while (nb_bytes_sent < msg_length)
193 {
194 nb_bytes_sent += SER_tr_WriteNBytes (SER_LAYER_1,
195 (UINT8 *) msg + RVT_HEADER_SIZE - 1 + nb_bytes_sent,
196 msg_length - nb_bytes_sent);
197 }
198 break;
199 }
200 default:
201 {
202 // Increment the number of messages lost.
203 // Is the buffer corrupted?
204 (rvt_lost_msg_cpt.bit_mask).count++;
205 (rvt_lost_msg_cpt.bit_mask).unknown_format = 1;
206 break;
207 }
208 }
209
210 // Deallocate the buffer
211 rvf_free_buf (msg);
212
213 } // End of if (msg->msg_id == RVT_TRACE_RQST_ID)
214
215 else
216 {
217 // Increment the number of messages lost. Is the buffer
218 // corrupted?
219 (rvt_lost_msg_cpt.bit_mask).count++;
220 (rvt_lost_msg_cpt.bit_mask).unknown_request = 1;
221 }
222
223 } // End of if (msg != NULL)
224
225 else
226 {
227 // Increment the number of messages lost. Is the buffer
228 // corrupted?
229 (rvt_lost_msg_cpt.bit_mask).count++;
230 (rvt_lost_msg_cpt.bit_mask).message_empty = 1;
231 }
232
233 } // End of if (event & EVENT_MASK (RVT_TRACE_MAILBOX))
234
235 if ((event & (RVF_TIMER_0_EVT_MASK)) && (p_rvt_sys_time))
236 {
237 UINT8 count = 0;
238 UINT8 sys_time_length = RVT_HDR_LENGTH + RVT_SYS_TIME_LENGTH;
239 UINT32 current_time = rvf_get_tick_count ();
240
241 // Append with the system time
242 for (count = 0;
243 count < RVT_HEX_VALUE_LENGTH;
244 count++)
245 {
246 p_rvt_sys_time[sys_time_length + count] = Num2Char[(UINT8) ((current_time << (count << 2)) >> 28)];
247 }
248 sys_time_length += RVT_HEX_VALUE_LENGTH;
249
250 // Send message to the UART with byte stuffing
251 nb_bytes_sent = 0;
252 while (nb_bytes_sent < sys_time_length)
253 {
254 nb_bytes_sent += SER_tr_WriteNBytes (SER_LAYER_1,
255 (UINT8 *) p_rvt_sys_time + nb_bytes_sent,
256 sys_time_length - nb_bytes_sent);
257 }
258
259 #ifdef PSEUDO_MODEM_KEEPALIVE
260 rvt_keepalive_process();
261 #endif
262
263 } // End of if (event & (RVF_TIMER_0_EVT_MASK))
264 }
265 #else
266
267 // Start the 'Alive Polling Timer'
268 #if (OP_WCP == 0)
269 rvf_start_timer (RVF_TIMER_0,
270 RVT_ALIVE_POLLING_TIME,
271 TRUE);
272 #endif
273
274 for (;
275 ;
276 )
277 {
278 // Infinite wait on 'Trace Task' mailbox or timer events
279 event = rvf_wait ((1 << RVT_TRACE_MAILBOX) | (RVF_TIMER_0_EVT_MASK),
280 0);
281
282 // Check for some messages lost
283 if (((rvt_lost_msg_cpt.bit_mask).count >= RVT_MAX_LOST_TRACE_MSG) && (p_rvt_lost_msg))
284 {
285 INT8 count = 0;
286 UINT8 lost_msg_length = RVT_LOST_MSG_LENGTH;
287 UINT32 lost_msg_cpt = rvt_lost_msg_cpt.overall_value;
288
289 // Append with the number of messages lost
290 rvt_lost_msg_cpt.overall_value = 0;
291 for (count = 0;
292 count < RVT_HEX_VALUE_LENGTH;
293 count ++)
294 {
295 p_rvt_lost_msg[lost_msg_length + count] = Num2Char[(UINT8) ((lost_msg_cpt << (count << 2)) >> 28)];
296 }
297 rvt_lost_msg_length += RVT_HEX_VALUE_LENGTH;
298
299 // Append with the '\n' and '\r' characters for the hyper terminal
300 p_rvt_lost_msg[lost_msg_length++] = '\n';
301 p_rvt_lost_msg[lost_msg_length++] = '\r';
302
303 // Send the message to the UART without byte stuffing
304 nb_bytes_sent = 0;
305 while (nb_bytes_sent < lost_msg_length)
306 {
307 nb_bytes_sent += SER_tr_WriteNChars (SER_LAYER_1,
308 (UINT8 *) p_rvt_lost_msg + nb_bytes_sent,
309 lost_msg_length - nb_bytes_sent);
310 }
311
312 } // End of if ((rvt_lost_msg_cpt.bit_mask).count >= RVT_MAX_LOST_TRACE_MSG)
313
314 if (event & EVENT_MASK (RVT_TRACE_MAILBOX))
315 {
316 T_RV_HDR *msg = NULL;
317
318 // Read the message from the mailbox
319 if ((msg = (T_RV_HDR *) rvf_read_mbox (RVT_TRACE_MAILBOX)) != NULL)
320 {
321 if (msg->msg_id == RVT_TRACE_RQST_ID)
322 {
323 UINT32 msg_length = 0;
324
325 // Get the length
326 msg_length = ((T_RVT_TRACE_RQST *) msg)->msg_length;
327
328 // Send message to the UART without byte stuffing
329 nb_bytes_sent = 0;
330 while (nb_bytes_sent < msg_length)
331 {
332 nb_bytes_sent += SER_tr_WriteNChars (SER_LAYER_1,
333 msg + RVT_HEADER_SIZE + nb_bytes_sent,
334 msg_length - nb_bytes_sent);
335 }
336
337 // Append with the '\n' and '\r' characters for the hyper terminal
338 msg_length = 0;
339 msg[msg_length++] = '\n';
340 msg[msg_length++] = '\r';
341
342 // Send message to the UART without byte stuffing
343 nb_bytes_sent = 0;
344 while (nb_bytes_sent < msg_length)
345 {
346 nb_bytes_sent += SER_tr_WriteNChars (SER_LAYER_1,
347 msg + nb_bytes_sent,
348 msg_length - nb_bytes_sent);
349 }
350
351 // Deallocate the buffer
352 rvf_free_buf (msg);
353
354 } // End of if (msg->msg_id == RVT_TRACE_RQST_ID)
355
356 else
357 {
358 // Increment the number of messages lost. Is the buffer
359 // corrupted?
360 (rvt_lost_msg_cpt.bit_mask).count++;
361 (rvt_lost_msg_cpt.bit_mask).unknown_request = 1;
362 }
363
364 } // End of if (msg != NULL)
365
366 else
367 {
368 // Increment the number of messages lost. Is the buffer
369 // corrupted?
370 (rvt_lost_msg_cpt.bit_mask).count++;
371 (rvt_lost_msg_cpt.bit_mask).message_empty = 1;
372 }
373
374 } // End of if (event & EVENT_MASK (RVT_TRACE_MAILBOX))
375
376 if ((event & (RVF_TIMER_0_EVT_MASK)) && (p_rvt_sys_time))
377 {
378 UINT8 count = 0;
379 UINT8 sys_time_length = RVT_SYS_TIME_LENGTH;
380 UINT32 current_time = rvf_get_tick_count ();
381
382 // Append with the system time
383 for (count = 0;
384 count < RVT_HEX_VALUE_LENGTH;
385 count++)
386 {
387 p_rvt_sys_time[sys_time_length + count] = Num2Char[(UINT8) ((current_time << (count << 2)) >> 28)];
388 }
389 sys_time_length += RVT_HEX_VALUE_LENGTH;
390
391 // Append with the '\n' and '\r' characters for the hyper terminal
392 p_rvt_sys_time[sys_time_length++] = '\n';
393 p_rvt_sys_time[sys_time_length++] = '\r';
394
395 // Send message to the UART without byte stuffing
396 nb_bytes_sent = 0;
397 while (nb_bytes_sent < sys_time_length)
398 {
399 nb_bytes_sent += SER_tr_WriteNChars (SER_LAYER_1,
400 (UINT8 *) p_rvt_sys_time + nb_bytes_sent,
401 sys_time_length - nb_bytes_sent);
402 }
403
404 } // End of if (event & (RVF_TIMER_0_EVT_MASK))
405 }
406 #endif
407 }
408
409
410 /********************************************************************************/
411 /* */
412 /* Function Name: rvt_RX_process */
413 /* */
414 /* Purpose: This function is called when characters are received */
415 /* on the serial port on receive HISR. */
416 /* */
417 /* Input Parameters: */
418 /* None. */
419 /* */
420 /* Output Parameters: */
421 /* None. */
422 /* */
423 /* Global Parameters: */
424 /* None. */
425 /* */
426 /* Note: */
427 /* None. */
428 /* */
429 /********************************************************************************/
430 void rvt_RX_process (void)
431 {
432 UINT32 bytesRead;
433 static UINT8 inBuffer[255];
434
435 #ifdef FRAMING_PROTOCOL
436 BOOL eof = 0;
437 static UINT8 rcv_state = RVT_WAIT_FOR_HEADER;
438 static UINT32 total_bytesRead = 0;
439 static RVT_CALLBACK_FUNC rx_callback_func = NULL;
440
441 // Get all bytes from the UART RX FIFO
442 for (;
443 ;
444 )
445 {
446 // Read and destuff the UART RX FIFO and fill inBuffer with received
447 // bytes
448 bytesRead = SER_tr_ReadNBytes (SER_LAYER_1,
449 (char *) (inBuffer + total_bytesRead),
450 sizeof (inBuffer) - total_bytesRead,
451 &eof);
452
453 // Check for the header. Hence, get the sendee
454 if ((rcv_state == RVT_WAIT_FOR_HEADER) && \
455 (bytesRead) && \
456 !(total_bytesRead))
457 {
458 if ((inBuffer[0] >= RVT_RV_HEADER) &&
459 (inBuffer[0] <= RVT_KEEPALIVE_HEADER))
460 {
461 UINT8 idtab = 0;
462
463 // Search for the ID in the table
464 for (idtab = 0;
465 rvt_user_db[idtab].user_id != RVT_INVALID_HEADER;
466 idtab++)
467 {
468 if (rvt_user_db[idtab].user_id == inBuffer[0])
469 {
470 rx_callback_func = rvt_user_db[idtab].rx_callback_func;
471 break;
472 }
473 }
474 rcv_state = RVT_WAIT_FOR_DATA;
475 }
476 }
477
478 // Update the total number of bytes read, regarding the current frame
479 total_bytesRead += bytesRead;
480
481 // Call the corresponding callback function when a complete message is
482 // received (eof odd)
483 if (eof & 0x01)
484 {
485 // Invoke the callback function
486 if (rx_callback_func != NULL)
487 {
488 rx_callback_func ((T_RVT_BUFFER) (inBuffer + 1),
489 total_bytesRead - 1);
490 rx_callback_func = NULL;
491 }
492
493 // Wait for the next frame to come
494 rcv_state = RVT_WAIT_FOR_HEADER;
495 total_bytesRead = 0;
496 }
497
498 // inBuffer may be full due to some synchro lost problems
499 else if (total_bytesRead == sizeof (inBuffer))
500 {
501 // If still waiting for the header, discard received characters and
502 // reset static variables for the next frame to come
503 if (rcv_state == RVT_WAIT_FOR_HEADER)
504 {
505 total_bytesRead = 0;
506 break;
507 }
508
509 // Just discard characters received as payload
510 total_bytesRead = sizeof (inBuffer[0]);
511 }
512
513 // Proceed with the next concatenated frame whether more bytes left
514 if (eof > 1)
515 {
516 continue;
517 }
518 break;
519 }
520 #else
521
522 // Read the UART RX FIFO and fill inBuffer with received bytes
523 bytesRead = SER_tr_ReadNChars (SER_LAYER_1,
524 (char *) inBuffer,
525 sizeof (inBuffer));
526
527 // Invoke the Testmode callback function : this is the only one able,
528 // for the moment, to send an external command !! WARNING : This
529 // should be the same name than the one already registered in
530 // create_RVtasks.c.
531 tm_receive ((T_RVT_BUFFER) inBuffer,
532 bytesRead);
533 #endif
534 }
535
536
537
538 /********************************************************************************/
539 /* */
540 /* Function Name: rvt_activate_RX_HISR */
541 /* */
542 /* Purpose: This function is called when an RX interrupt occurs. */
543 /* */
544 /* Input Parameters: */
545 /* None. */
546 /* */
547 /* Output Parameters: */
548 /* None. */
549 /* */
550 /* Global Parameters: */
551 /* None. */
552 /* */
553 /* Note: */
554 /* None. */
555 /* */
556 /********************************************************************************/
557 void rvt_activate_RX_HISR (void)
558 {
559 NU_Activate_HISR (&TI_rcv_HISR);
560 }