##// END OF EJS Templates
3.1.0.3...
paul -
r296:fb16b781e584 R3_plus draft
parent child
Show More
@@ -1,125 +1,125
1 1 TEMPLATE = app
2 2 # CONFIG += console v8 sim
3 3 # CONFIG options =
4 4 # verbose
5 5 # boot_messages
6 6 # debug_messages
7 7 # cpu_usage_report
8 8 # stack_report
9 9 # vhdl_dev
10 10 # debug_tch
11 11 # lpp_dpu_destid /!\ REMOVE BEFORE DELIVERY TO LESIA /!\
12 12 # debug_watchdog
13 13 CONFIG += console verbose lpp_dpu_destid cpu_usage_report
14 14 CONFIG -= qt
15 15
16 16 include(./sparc.pri)
17 17
18 18 INCLUDEPATH += /opt/rtems-4.10/sparc-rtems/leon3/lib/include
19 19
20 20 # flight software version
21 21 SWVERSION=-1-0
22 22 DEFINES += SW_VERSION_N1=3 # major
23 23 DEFINES += SW_VERSION_N2=1 # minor
24 24 DEFINES += SW_VERSION_N3=0 # patch
25 DEFINES += SW_VERSION_N4=2 # internal
25 DEFINES += SW_VERSION_N4=3 # internal
26 26
27 27 # <GCOV>
28 28 #QMAKE_CFLAGS_RELEASE += -fprofile-arcs -ftest-coverage
29 29 #LIBS += -lgcov /opt/GCOV/01A/lib/overload.o -lc
30 30 # </GCOV>
31 31
32 32 # <CHANGE BEFORE FLIGHT>
33 33 contains( CONFIG, lpp_dpu_destid ) {
34 34 DEFINES += LPP_DPU_DESTID
35 35 }
36 36 # </CHANGE BEFORE FLIGHT>
37 37
38 38 contains( CONFIG, debug_tch ) {
39 39 DEFINES += DEBUG_TCH
40 40 }
41 41 DEFINES += MSB_FIRST_TCH
42 42
43 43 contains( CONFIG, vhdl_dev ) {
44 44 DEFINES += VHDL_DEV
45 45 }
46 46
47 47 contains( CONFIG, verbose ) {
48 48 DEFINES += PRINT_MESSAGES_ON_CONSOLE
49 49 }
50 50
51 51 contains( CONFIG, debug_messages ) {
52 52 DEFINES += DEBUG_MESSAGES
53 53 }
54 54
55 55 contains( CONFIG, cpu_usage_report ) {
56 56 DEFINES += PRINT_TASK_STATISTICS
57 57 }
58 58
59 59 contains( CONFIG, stack_report ) {
60 60 DEFINES += PRINT_STACK_REPORT
61 61 }
62 62
63 63 contains( CONFIG, boot_messages ) {
64 64 DEFINES += BOOT_MESSAGES
65 65 }
66 66
67 67 contains( CONFIG, debug_watchdog ) {
68 68 DEFINES += DEBUG_WATCHDOG
69 69 }
70 70
71 71 #doxygen.target = doxygen
72 72 #doxygen.commands = doxygen ../doc/Doxyfile
73 73 #QMAKE_EXTRA_TARGETS += doxygen
74 74
75 75 TARGET = fsw
76 76
77 77 INCLUDEPATH += \
78 78 $${PWD}/../src \
79 79 $${PWD}/../header \
80 80 $${PWD}/../header/lfr_common_headers \
81 81 $${PWD}/../header/processing \
82 82 $${PWD}/../LFR_basic-parameters
83 83
84 84 SOURCES += \
85 85 ../src/wf_handler.c \
86 86 ../src/tc_handler.c \
87 87 ../src/fsw_misc.c \
88 88 ../src/fsw_init.c \
89 89 ../src/fsw_globals.c \
90 90 ../src/fsw_spacewire.c \
91 91 ../src/tc_load_dump_parameters.c \
92 92 ../src/tm_lfr_tc_exe.c \
93 93 ../src/tc_acceptance.c \
94 94 ../src/processing/fsw_processing.c \
95 95 ../src/processing/avf0_prc0.c \
96 96 ../src/processing/avf1_prc1.c \
97 97 ../src/processing/avf2_prc2.c \
98 98 ../src/lfr_cpu_usage_report.c \
99 99 ../LFR_basic-parameters/basic_parameters.c
100 100
101 101 HEADERS += \
102 102 ../header/wf_handler.h \
103 103 ../header/tc_handler.h \
104 104 ../header/grlib_regs.h \
105 105 ../header/fsw_misc.h \
106 106 ../header/fsw_init.h \
107 107 ../header/fsw_spacewire.h \
108 108 ../header/tc_load_dump_parameters.h \
109 109 ../header/tm_lfr_tc_exe.h \
110 110 ../header/tc_acceptance.h \
111 111 ../header/processing/fsw_processing.h \
112 112 ../header/processing/avf0_prc0.h \
113 113 ../header/processing/avf1_prc1.h \
114 114 ../header/processing/avf2_prc2.h \
115 115 ../header/fsw_params_wf_handler.h \
116 116 ../header/lfr_cpu_usage_report.h \
117 117 ../header/lfr_common_headers/ccsds_types.h \
118 118 ../header/lfr_common_headers/fsw_params.h \
119 119 ../header/lfr_common_headers/fsw_params_nb_bytes.h \
120 120 ../header/lfr_common_headers/fsw_params_processing.h \
121 121 ../header/lfr_common_headers/tm_byte_positions.h \
122 122 ../LFR_basic-parameters/basic_parameters.h \
123 123 ../LFR_basic-parameters/basic_parameters_params.h \
124 124 ../header/GscMemoryLPP.hpp
125 125
@@ -1,1599 +1,1599
1 1 /** Functions related to the SpaceWire interface.
2 2 *
3 3 * @file
4 4 * @author P. LEROY
5 5 *
6 6 * A group of functions to handle SpaceWire transmissions:
7 7 * - configuration of the SpaceWire link
8 8 * - SpaceWire related interruption requests processing
9 9 * - transmission of TeleMetry packets by a dedicated RTEMS task
10 10 * - reception of TeleCommands by a dedicated RTEMS task
11 11 *
12 12 */
13 13
14 14 #include "fsw_spacewire.h"
15 15
16 16 rtems_name semq_name;
17 17 rtems_id semq_id;
18 18
19 19 //*****************
20 20 // waveform headers
21 21 Header_TM_LFR_SCIENCE_CWF_t headerCWF;
22 22 Header_TM_LFR_SCIENCE_SWF_t headerSWF;
23 23 Header_TM_LFR_SCIENCE_ASM_t headerASM;
24 24
25 25 unsigned char previousTimecodeCtr = 0;
26 26 unsigned int *grspwPtr = (unsigned int *) (REGS_ADDR_GRSPW + APB_OFFSET_GRSPW_TIME_REGISTER);
27 27
28 28 //***********
29 29 // RTEMS TASK
30 30 rtems_task spiq_task(rtems_task_argument unused)
31 31 {
32 32 /** This RTEMS task is awaken by an rtems_event sent by the interruption subroutine of the SpaceWire driver.
33 33 *
34 34 * @param unused is the starting argument of the RTEMS task
35 35 *
36 36 */
37 37
38 38 rtems_event_set event_out;
39 39 rtems_status_code status;
40 40 int linkStatus;
41 41
42 42 BOOT_PRINTF("in SPIQ *** \n")
43 43
44 44 while(true){
45 45 rtems_event_receive(SPW_LINKERR_EVENT, RTEMS_WAIT, RTEMS_NO_TIMEOUT, &event_out); // wait for an SPW_LINKERR_EVENT
46 46 PRINTF("in SPIQ *** got SPW_LINKERR_EVENT\n")
47 47
48 48 // [0] SUSPEND RECV AND SEND TASKS
49 49 status = rtems_task_suspend( Task_id[ TASKID_RECV ] );
50 50 if ( status != RTEMS_SUCCESSFUL ) {
51 51 PRINTF("in SPIQ *** ERR suspending RECV Task\n")
52 52 }
53 53 status = rtems_task_suspend( Task_id[ TASKID_SEND ] );
54 54 if ( status != RTEMS_SUCCESSFUL ) {
55 55 PRINTF("in SPIQ *** ERR suspending SEND Task\n")
56 56 }
57 57
58 58 // [1] CHECK THE LINK
59 59 status = ioctl(fdSPW, SPACEWIRE_IOCTRL_GET_LINK_STATUS, &linkStatus); // get the link status (1)
60 60 if ( linkStatus != 5) {
61 61 PRINTF1("in SPIQ *** linkStatus %d, wait...\n", linkStatus)
62 62 status = rtems_task_wake_after( SY_LFR_DPU_CONNECT_TIMEOUT ); // wait SY_LFR_DPU_CONNECT_TIMEOUT 1000 ms
63 63 }
64 64
65 65 // [2] RECHECK THE LINK AFTER SY_LFR_DPU_CONNECT_TIMEOUT
66 66 status = ioctl(fdSPW, SPACEWIRE_IOCTRL_GET_LINK_STATUS, &linkStatus); // get the link status (2)
67 67 if ( linkStatus != 5 ) // [2.a] not in run state, reset the link
68 68 {
69 69 spacewire_read_statistics();
70 70 status = spacewire_several_connect_attemps( );
71 71 }
72 72 else // [2.b] in run state, start the link
73 73 {
74 74 status = spacewire_stop_and_start_link( fdSPW ); // start the link
75 75 if ( status != RTEMS_SUCCESSFUL)
76 76 {
77 77 PRINTF1("in SPIQ *** ERR spacewire_stop_and_start_link %d\n", status)
78 78 }
79 79 }
80 80
81 81 // [3] COMPLETE RECOVERY ACTION AFTER SY_LFR_DPU_CONNECT_ATTEMPTS
82 82 if ( status == RTEMS_SUCCESSFUL ) // [3.a] the link is in run state and has been started successfully
83 83 {
84 84 status = rtems_task_restart( Task_id[ TASKID_SEND ], 1 );
85 85 if ( status != RTEMS_SUCCESSFUL ) {
86 86 PRINTF("in SPIQ *** ERR resuming SEND Task\n")
87 87 }
88 88 status = rtems_task_restart( Task_id[ TASKID_RECV ], 1 );
89 89 if ( status != RTEMS_SUCCESSFUL ) {
90 90 PRINTF("in SPIQ *** ERR resuming RECV Task\n")
91 91 }
92 92 }
93 93 else // [3.b] the link is not in run state, go in STANDBY mode
94 94 {
95 95 status = enter_mode_standby();
96 96 if ( status != RTEMS_SUCCESSFUL )
97 97 {
98 98 PRINTF1("in SPIQ *** ERR enter_standby_mode *** code %d\n", status)
99 99 }
100 100 {
101 101 updateLFRCurrentMode( LFR_MODE_STANDBY );
102 102 }
103 103 // wake the LINK task up to wait for the link recovery
104 104 status = rtems_event_send ( Task_id[TASKID_LINK], RTEMS_EVENT_0 );
105 105 status = rtems_task_suspend( RTEMS_SELF );
106 106 }
107 107 }
108 108 }
109 109
110 110 rtems_task recv_task( rtems_task_argument unused )
111 111 {
112 112 /** This RTEMS task is dedicated to the reception of incoming TeleCommands.
113 113 *
114 114 * @param unused is the starting argument of the RTEMS task
115 115 *
116 116 * The RECV task blocks on a call to the read system call, waiting for incoming SpaceWire data. When unblocked:
117 117 * 1. It reads the incoming data.
118 118 * 2. Launches the acceptance procedure.
119 119 * 3. If the Telecommand is valid, sends it to a dedicated RTEMS message queue.
120 120 *
121 121 */
122 122
123 123 int len;
124 124 ccsdsTelecommandPacket_t currentTC;
125 125 unsigned char computed_CRC[ 2 ];
126 126 unsigned char currentTC_LEN_RCV[ 2 ];
127 127 unsigned char destinationID;
128 128 unsigned int estimatedPacketLength;
129 129 unsigned int parserCode;
130 130 rtems_status_code status;
131 131 rtems_id queue_recv_id;
132 132 rtems_id queue_send_id;
133 133
134 134 initLookUpTableForCRC(); // the table is used to compute Cyclic Redundancy Codes
135 135
136 136 status = get_message_queue_id_recv( &queue_recv_id );
137 137 if (status != RTEMS_SUCCESSFUL)
138 138 {
139 139 PRINTF1("in RECV *** ERR get_message_queue_id_recv %d\n", status)
140 140 }
141 141
142 142 status = get_message_queue_id_send( &queue_send_id );
143 143 if (status != RTEMS_SUCCESSFUL)
144 144 {
145 145 PRINTF1("in RECV *** ERR get_message_queue_id_send %d\n", status)
146 146 }
147 147
148 148 BOOT_PRINTF("in RECV *** \n")
149 149
150 150 while(1)
151 151 {
152 152 len = read( fdSPW, (char*) &currentTC, CCSDS_TC_PKT_MAX_SIZE ); // the call to read is blocking
153 153 if (len == -1){ // error during the read call
154 154 PRINTF1("in RECV *** last read call returned -1, ERRNO %d\n", errno)
155 155 }
156 156 else {
157 157 if ( (len+1) < CCSDS_TC_PKT_MIN_SIZE ) {
158 158 PRINTF("in RECV *** packet lenght too short\n")
159 159 }
160 160 else {
161 PRINTF1("incoming TC with len: %d\n", len);
161 // PRINTF1("incoming TC with len: %d\n", len);
162 162 estimatedPacketLength = (unsigned int) (len - CCSDS_TC_TM_PACKET_OFFSET - 3); // => -3 is for Prot ID, Reserved and User App bytes
163 163 currentTC_LEN_RCV[ 0 ] = (unsigned char) (estimatedPacketLength >> 8);
164 164 currentTC_LEN_RCV[ 1 ] = (unsigned char) (estimatedPacketLength );
165 165 // CHECK THE TC
166 166 parserCode = tc_parser( &currentTC, estimatedPacketLength, computed_CRC ) ;
167 167 if ( (parserCode == ILLEGAL_APID) || (parserCode == WRONG_LEN_PKT)
168 168 || (parserCode == INCOR_CHECKSUM) || (parserCode == ILL_TYPE)
169 169 || (parserCode == ILL_SUBTYPE) || (parserCode == WRONG_APP_DATA)
170 170 || (parserCode == WRONG_SRC_ID) )
171 171 { // send TM_LFR_TC_EXE_CORRUPTED
172 172 PRINTF1("TC corrupted received, with code: %d\n", parserCode);
173 173 if ( !( (currentTC.serviceType==TC_TYPE_TIME) && (currentTC.serviceSubType==TC_SUBTYPE_UPDT_TIME) )
174 174 &&
175 175 !( (currentTC.serviceType==TC_TYPE_GEN) && (currentTC.serviceSubType==TC_SUBTYPE_UPDT_INFO))
176 176 )
177 177 {
178 178 if ( parserCode == WRONG_SRC_ID )
179 179 {
180 180 destinationID = SID_TC_GROUND;
181 181 }
182 182 else
183 183 {
184 184 destinationID = currentTC.sourceID;
185 185 }
186 186 send_tm_lfr_tc_exe_corrupted( &currentTC, queue_send_id,
187 187 computed_CRC, currentTC_LEN_RCV,
188 188 destinationID );
189 189 }
190 190 }
191 191 else
192 192 { // send valid TC to the action launcher
193 193 status = rtems_message_queue_send( queue_recv_id, &currentTC,
194 194 estimatedPacketLength + CCSDS_TC_TM_PACKET_OFFSET + 3);
195 195 }
196 196 }
197 197 }
198 198
199 199 update_queue_max_count( queue_recv_id, &hk_lfr_q_rv_fifo_size_max );
200 200
201 201 }
202 202 }
203 203
204 204 rtems_task send_task( rtems_task_argument argument)
205 205 {
206 206 /** This RTEMS task is dedicated to the transmission of TeleMetry packets.
207 207 *
208 208 * @param unused is the starting argument of the RTEMS task
209 209 *
210 210 * The SEND task waits for a message to become available in the dedicated RTEMS queue. When a message arrives:
211 211 * - if the first byte is equal to CCSDS_DESTINATION_ID, the message is sent as is using the write system call.
212 212 * - if the first byte is not equal to CCSDS_DESTINATION_ID, the message is handled as a spw_ioctl_pkt_send. After
213 213 * analyzis, the packet is sent either using the write system call or using the ioctl call SPACEWIRE_IOCTRL_SEND, depending on the
214 214 * data it contains.
215 215 *
216 216 */
217 217
218 218 rtems_status_code status; // RTEMS status code
219 219 char incomingData[MSG_QUEUE_SIZE_SEND]; // incoming data buffer
220 220 ring_node *incomingRingNodePtr;
221 221 int ring_node_address;
222 222 char *charPtr;
223 223 spw_ioctl_pkt_send *spw_ioctl_send;
224 224 size_t size; // size of the incoming TC packet
225 225 rtems_id queue_send_id;
226 226 unsigned int sid;
227 227 unsigned char sidAsUnsignedChar;
228 228 unsigned char type;
229 229
230 230 incomingRingNodePtr = NULL;
231 231 ring_node_address = 0;
232 232 charPtr = (char *) &ring_node_address;
233 233 sid = 0;
234 234 sidAsUnsignedChar = 0;
235 235
236 236 init_header_cwf( &headerCWF );
237 237 init_header_swf( &headerSWF );
238 238 init_header_asm( &headerASM );
239 239
240 240 status = get_message_queue_id_send( &queue_send_id );
241 241 if (status != RTEMS_SUCCESSFUL)
242 242 {
243 243 PRINTF1("in HOUS *** ERR get_message_queue_id_send %d\n", status)
244 244 }
245 245
246 246 BOOT_PRINTF("in SEND *** \n")
247 247
248 248 while(1)
249 249 {
250 250 status = rtems_message_queue_receive( queue_send_id, incomingData, &size,
251 251 RTEMS_WAIT, RTEMS_NO_TIMEOUT );
252 252
253 253 if (status!=RTEMS_SUCCESSFUL)
254 254 {
255 255 PRINTF1("in SEND *** (1) ERR = %d\n", status)
256 256 }
257 257 else
258 258 {
259 259 if ( size == sizeof(ring_node*) )
260 260 {
261 261 charPtr[0] = incomingData[0];
262 262 charPtr[1] = incomingData[1];
263 263 charPtr[2] = incomingData[2];
264 264 charPtr[3] = incomingData[3];
265 265 incomingRingNodePtr = (ring_node*) ring_node_address;
266 266 sid = incomingRingNodePtr->sid;
267 267 if ( (sid==SID_NORM_CWF_LONG_F3)
268 268 || (sid==SID_BURST_CWF_F2 )
269 269 || (sid==SID_SBM1_CWF_F1 )
270 270 || (sid==SID_SBM2_CWF_F2 ))
271 271 {
272 272 spw_send_waveform_CWF( incomingRingNodePtr, &headerCWF );
273 273 }
274 274 else if ( (sid==SID_NORM_SWF_F0) || (sid== SID_NORM_SWF_F1) || (sid==SID_NORM_SWF_F2) )
275 275 {
276 276 spw_send_waveform_SWF( incomingRingNodePtr, &headerSWF );
277 277 }
278 278 else if ( (sid==SID_NORM_CWF_F3) )
279 279 {
280 280 spw_send_waveform_CWF3_light( incomingRingNodePtr, &headerCWF );
281 281 }
282 282 else if (sid==SID_NORM_ASM_F0)
283 283 {
284 284 spw_send_asm_f0( incomingRingNodePtr, &headerASM );
285 285 }
286 286 else if (sid==SID_NORM_ASM_F1)
287 287 {
288 288 spw_send_asm_f1( incomingRingNodePtr, &headerASM );
289 289 }
290 290 else if (sid==SID_NORM_ASM_F2)
291 291 {
292 292 spw_send_asm_f2( incomingRingNodePtr, &headerASM );
293 293 }
294 294 else if ( sid==TM_CODE_K_DUMP )
295 295 {
296 296 spw_send_k_dump( incomingRingNodePtr );
297 297 }
298 298 else
299 299 {
300 300 PRINTF1("unexpected sid = %d\n", sid);
301 301 }
302 302 }
303 303 else if ( incomingData[0] == CCSDS_DESTINATION_ID ) // the incoming message is a ccsds packet
304 304 {
305 305 sidAsUnsignedChar = (unsigned char) incomingData[ PACKET_POS_PA_LFR_SID_PKT ];
306 306 sid = sidAsUnsignedChar;
307 307 type = (unsigned char) incomingData[ PACKET_POS_SERVICE_TYPE ];
308 308 if (type == TM_TYPE_LFR_SCIENCE) // this is a BP packet, all other types are handled differently
309 309 // SET THE SEQUENCE_CNT PARAMETER IN CASE OF BP0 OR BP1 PACKETS
310 310 {
311 311 increment_seq_counter_source_id( (unsigned char*) &incomingData[ PACKET_POS_SEQUENCE_CNT ], sid );
312 312 }
313 313
314 314 status = write( fdSPW, incomingData, size );
315 315 if (status == -1){
316 316 PRINTF2("in SEND *** (2.a) ERRNO = %d, size = %d\n", errno, size)
317 317 }
318 318 }
319 319 else // the incoming message is a spw_ioctl_pkt_send structure
320 320 {
321 321 spw_ioctl_send = (spw_ioctl_pkt_send*) incomingData;
322 322 status = ioctl( fdSPW, SPACEWIRE_IOCTRL_SEND, spw_ioctl_send );
323 323 if (status == -1){
324 324 PRINTF2("in SEND *** (2.b) ERRNO = %d, RTEMS = %d\n", errno, status)
325 325 }
326 326 }
327 327 }
328 328
329 329 update_queue_max_count( queue_send_id, &hk_lfr_q_sd_fifo_size_max );
330 330
331 331 }
332 332 }
333 333
334 334 rtems_task link_task( rtems_task_argument argument )
335 335 {
336 336 rtems_event_set event_out;
337 337 rtems_status_code status;
338 338 int linkStatus;
339 339
340 340 BOOT_PRINTF("in LINK ***\n")
341 341
342 342 while(1)
343 343 {
344 344 // wait for an RTEMS_EVENT
345 345 rtems_event_receive( RTEMS_EVENT_0,
346 346 RTEMS_WAIT | RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, &event_out);
347 347 PRINTF("in LINK *** wait for the link\n")
348 348 status = ioctl(fdSPW, SPACEWIRE_IOCTRL_GET_LINK_STATUS, &linkStatus); // get the link status
349 349 while( linkStatus != 5) // wait for the link
350 350 {
351 351 status = rtems_task_wake_after( 10 ); // monitor the link each 100ms
352 352 status = ioctl(fdSPW, SPACEWIRE_IOCTRL_GET_LINK_STATUS, &linkStatus); // get the link status
353 353 watchdog_reload();
354 354 }
355 355
356 356 spacewire_read_statistics();
357 357 status = spacewire_stop_and_start_link( fdSPW );
358 358
359 359 if (status != RTEMS_SUCCESSFUL)
360 360 {
361 361 PRINTF1("in LINK *** ERR link not started %d\n", status)
362 362 }
363 363 else
364 364 {
365 365 PRINTF("in LINK *** OK link started\n")
366 366 }
367 367
368 368 // restart the SPIQ task
369 369 status = rtems_task_restart( Task_id[TASKID_SPIQ], 1 );
370 370 if ( status != RTEMS_SUCCESSFUL ) {
371 371 PRINTF("in SPIQ *** ERR restarting SPIQ Task\n")
372 372 }
373 373
374 374 // restart RECV and SEND
375 375 status = rtems_task_restart( Task_id[ TASKID_SEND ], 1 );
376 376 if ( status != RTEMS_SUCCESSFUL ) {
377 377 PRINTF("in SPIQ *** ERR restarting SEND Task\n")
378 378 }
379 379 status = rtems_task_restart( Task_id[ TASKID_RECV ], 1 );
380 380 if ( status != RTEMS_SUCCESSFUL ) {
381 381 PRINTF("in SPIQ *** ERR restarting RECV Task\n")
382 382 }
383 383 }
384 384 }
385 385
386 386 //****************
387 387 // OTHER FUNCTIONS
388 388 int spacewire_open_link( void ) // by default, the driver resets the core: [SPW_CTRL_WRITE(pDev, SPW_CTRL_RESET);]
389 389 {
390 390 /** This function opens the SpaceWire link.
391 391 *
392 392 * @return a valid file descriptor in case of success, -1 in case of a failure
393 393 *
394 394 */
395 395 rtems_status_code status;
396 396
397 397 fdSPW = open(GRSPW_DEVICE_NAME, O_RDWR); // open the device. the open call resets the hardware
398 398 if ( fdSPW < 0 ) {
399 399 PRINTF1("ERR *** in configure_spw_link *** error opening "GRSPW_DEVICE_NAME" with ERR %d\n", errno)
400 400 }
401 401 else
402 402 {
403 403 status = RTEMS_SUCCESSFUL;
404 404 }
405 405
406 406 return status;
407 407 }
408 408
409 409 int spacewire_start_link( int fd )
410 410 {
411 411 rtems_status_code status;
412 412
413 413 status = ioctl( fd, SPACEWIRE_IOCTRL_START, -1); // returns successfuly if the link is started
414 414 // -1 default hardcoded driver timeout
415 415
416 416 return status;
417 417 }
418 418
419 419 int spacewire_stop_and_start_link( int fd )
420 420 {
421 421 rtems_status_code status;
422 422
423 423 status = ioctl( fd, SPACEWIRE_IOCTRL_STOP); // start fails if link pDev->running != 0
424 424 status = ioctl( fd, SPACEWIRE_IOCTRL_START, -1); // returns successfuly if the link is started
425 425 // -1 default hardcoded driver timeout
426 426
427 427 return status;
428 428 }
429 429
430 430 int spacewire_configure_link( int fd )
431 431 {
432 432 /** This function configures the SpaceWire link.
433 433 *
434 434 * @return GR-RTEMS-DRIVER directive status codes:
435 435 * - 22 EINVAL - Null pointer or an out of range value was given as the argument.
436 436 * - 16 EBUSY - Only used for SEND. Returned when no descriptors are avialble in non-blocking mode.
437 437 * - 88 ENOSYS - Returned for SET_DESTKEY if RMAP command handler is not available or if a non-implemented call is used.
438 438 * - 116 ETIMEDOUT - REturned for SET_PACKET_SIZE and START if the link could not be brought up.
439 439 * - 12 ENOMEM - Returned for SET_PACKETSIZE if it was unable to allocate the new buffers.
440 440 * - 5 EIO - Error when writing to grswp hardware registers.
441 441 * - 2 ENOENT - No such file or directory
442 442 */
443 443
444 444 rtems_status_code status;
445 445
446 446 spacewire_set_NP(1, REGS_ADDR_GRSPW); // [N]o [P]ort force
447 447 spacewire_set_RE(1, REGS_ADDR_GRSPW); // [R]MAP [E]nable, the dedicated call seems to break the no port force configuration
448 448
449 449 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_RXBLOCK, 1); // sets the blocking mode for reception
450 450 if (status!=RTEMS_SUCCESSFUL) {
451 451 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_RXBLOCK\n")
452 452 }
453 453 //
454 454 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_EVENT_ID, Task_id[TASKID_SPIQ]); // sets the task ID to which an event is sent when a
455 455 if (status!=RTEMS_SUCCESSFUL) {
456 456 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_EVENT_ID\n") // link-error interrupt occurs
457 457 }
458 458 //
459 459 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_DISABLE_ERR, 0); // automatic link-disabling due to link-error interrupts
460 460 if (status!=RTEMS_SUCCESSFUL) {
461 461 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_DISABLE_ERR\n")
462 462 }
463 463 //
464 464 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_LINK_ERR_IRQ, 1); // sets the link-error interrupt bit
465 465 if (status!=RTEMS_SUCCESSFUL) {
466 466 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_LINK_ERR_IRQ\n")
467 467 }
468 468 //
469 469 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_TXBLOCK, 1); // transmission blocks
470 470 if (status!=RTEMS_SUCCESSFUL) {
471 471 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_TXBLOCK\n")
472 472 }
473 473 //
474 474 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_TXBLOCK_ON_FULL, 1); // transmission blocks when no transmission descriptor is available
475 475 if (status!=RTEMS_SUCCESSFUL) {
476 476 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_TXBLOCK_ON_FULL\n")
477 477 }
478 478 //
479 479 status = ioctl(fd, SPACEWIRE_IOCTRL_SET_TCODE_CTRL, 0x0909); // [Time Rx : Time Tx : Link error : Tick-out IRQ]
480 480 if (status!=RTEMS_SUCCESSFUL) {
481 481 PRINTF("in SPIQ *** Error SPACEWIRE_IOCTRL_SET_TCODE_CTRL,\n")
482 482 }
483 483
484 484 return status;
485 485 }
486 486
487 487 int spacewire_several_connect_attemps( void )
488 488 {
489 489 /** This function is executed by the SPIQ rtems_task wehn it has been awaken by an interruption raised by the SpaceWire driver.
490 490 *
491 491 * @return RTEMS directive status code:
492 492 * - RTEMS_UNSATISFIED is returned is the link is not in the running state after 10 s.
493 493 * - RTEMS_SUCCESSFUL is returned if the link is up before the timeout.
494 494 *
495 495 */
496 496
497 497 rtems_status_code status_spw;
498 498 rtems_status_code status;
499 499 int i;
500 500
501 501 for ( i=0; i<SY_LFR_DPU_CONNECT_ATTEMPT; i++ )
502 502 {
503 503 PRINTF1("in spacewire_reset_link *** link recovery, try %d\n", i);
504 504
505 505 // CLOSING THE DRIVER AT THIS POINT WILL MAKE THE SEND TASK BLOCK THE SYSTEM
506 506
507 507 status = rtems_task_wake_after( SY_LFR_DPU_CONNECT_TIMEOUT ); // wait SY_LFR_DPU_CONNECT_TIMEOUT 1000 ms
508 508
509 509 status_spw = spacewire_stop_and_start_link( fdSPW );
510 510
511 511 if ( status_spw != RTEMS_SUCCESSFUL )
512 512 {
513 513 PRINTF1("in spacewire_reset_link *** ERR spacewire_start_link code %d\n", status_spw)
514 514 }
515 515
516 516 if ( status_spw == RTEMS_SUCCESSFUL)
517 517 {
518 518 break;
519 519 }
520 520 }
521 521
522 522 return status_spw;
523 523 }
524 524
525 525 void spacewire_set_NP( unsigned char val, unsigned int regAddr ) // [N]o [P]ort force
526 526 {
527 527 /** This function sets the [N]o [P]ort force bit of the GRSPW control register.
528 528 *
529 529 * @param val is the value, 0 or 1, used to set the value of the NP bit.
530 530 * @param regAddr is the address of the GRSPW control register.
531 531 *
532 532 * NP is the bit 20 of the GRSPW control register.
533 533 *
534 534 */
535 535
536 536 unsigned int *spwptr = (unsigned int*) regAddr;
537 537
538 538 if (val == 1) {
539 539 *spwptr = *spwptr | 0x00100000; // [NP] set the No port force bit
540 540 }
541 541 if (val== 0) {
542 542 *spwptr = *spwptr & 0xffdfffff;
543 543 }
544 544 }
545 545
546 546 void spacewire_set_RE( unsigned char val, unsigned int regAddr ) // [R]MAP [E]nable
547 547 {
548 548 /** This function sets the [R]MAP [E]nable bit of the GRSPW control register.
549 549 *
550 550 * @param val is the value, 0 or 1, used to set the value of the RE bit.
551 551 * @param regAddr is the address of the GRSPW control register.
552 552 *
553 553 * RE is the bit 16 of the GRSPW control register.
554 554 *
555 555 */
556 556
557 557 unsigned int *spwptr = (unsigned int*) regAddr;
558 558
559 559 if (val == 1)
560 560 {
561 561 *spwptr = *spwptr | 0x00010000; // [RE] set the RMAP Enable bit
562 562 }
563 563 if (val== 0)
564 564 {
565 565 *spwptr = *spwptr & 0xfffdffff;
566 566 }
567 567 }
568 568
569 569 void spacewire_read_statistics( void )
570 570 {
571 571 /** This function reads the SpaceWire statistics from the grspw RTEMS driver.
572 572 *
573 573 * @param void
574 574 *
575 575 * @return void
576 576 *
577 577 * Once they are read, the counters are stored in a global variable used during the building of the
578 578 * HK packets.
579 579 *
580 580 */
581 581
582 582 rtems_status_code status;
583 583 spw_stats current;
584 584
585 585 spacewire_get_last_error();
586 586
587 587 // read the current statistics
588 588 status = ioctl( fdSPW, SPACEWIRE_IOCTRL_GET_STATISTICS, &current );
589 589
590 590 // clear the counters
591 591 status = ioctl( fdSPW, SPACEWIRE_IOCTRL_CLR_STATISTICS );
592 592
593 593 // typedef struct {
594 594 // unsigned int tx_link_err; // NOT IN HK
595 595 // unsigned int rx_rmap_header_crc_err; // NOT IN HK
596 596 // unsigned int rx_rmap_data_crc_err; // NOT IN HK
597 597 // unsigned int rx_eep_err;
598 598 // unsigned int rx_truncated;
599 599 // unsigned int parity_err;
600 600 // unsigned int escape_err;
601 601 // unsigned int credit_err;
602 602 // unsigned int write_sync_err;
603 603 // unsigned int disconnect_err;
604 604 // unsigned int early_ep;
605 605 // unsigned int invalid_address;
606 606 // unsigned int packets_sent;
607 607 // unsigned int packets_received;
608 608 // } spw_stats;
609 609
610 610 // rx_eep_err
611 611 grspw_stats.rx_eep_err = grspw_stats.rx_eep_err + current.rx_eep_err;
612 612 // rx_truncated
613 613 grspw_stats.rx_truncated = grspw_stats.rx_truncated + current.rx_truncated;
614 614 // parity_err
615 615 grspw_stats.parity_err = grspw_stats.parity_err + current.parity_err;
616 616 // escape_err
617 617 grspw_stats.escape_err = grspw_stats.escape_err + current.escape_err;
618 618 // credit_err
619 619 grspw_stats.credit_err = grspw_stats.credit_err + current.credit_err;
620 620 // write_sync_err
621 621 grspw_stats.write_sync_err = grspw_stats.write_sync_err + current.write_sync_err;
622 622 // disconnect_err
623 623 grspw_stats.disconnect_err = grspw_stats.disconnect_err + current.disconnect_err;
624 624 // early_ep
625 625 grspw_stats.early_ep = grspw_stats.early_ep + current.early_ep;
626 626 // invalid_address
627 627 grspw_stats.invalid_address = grspw_stats.invalid_address + current.invalid_address;
628 628 // packets_sent
629 629 grspw_stats.packets_sent = grspw_stats.packets_sent + current.packets_sent;
630 630 // packets_received
631 631 grspw_stats.packets_received= grspw_stats.packets_received + current.packets_received;
632 632
633 633 }
634 634
635 635 void spacewire_get_last_error( void )
636 636 {
637 637 static spw_stats previous;
638 638 spw_stats current;
639 639 rtems_status_code status;
640 640
641 641 unsigned int hk_lfr_last_er_rid;
642 642 unsigned char hk_lfr_last_er_code;
643 643 int coarseTime;
644 644 int fineTime;
645 645 unsigned char update_hk_lfr_last_er;
646 646
647 647 update_hk_lfr_last_er = 0;
648 648
649 649 status = ioctl( fdSPW, SPACEWIRE_IOCTRL_GET_STATISTICS, &current );
650 650
651 651 // get current time
652 652 coarseTime = time_management_regs->coarse_time;
653 653 fineTime = time_management_regs->fine_time;
654 654
655 655 // typedef struct {
656 656 // unsigned int tx_link_err; // NOT IN HK
657 657 // unsigned int rx_rmap_header_crc_err; // NOT IN HK
658 658 // unsigned int rx_rmap_data_crc_err; // NOT IN HK
659 659 // unsigned int rx_eep_err;
660 660 // unsigned int rx_truncated;
661 661 // unsigned int parity_err;
662 662 // unsigned int escape_err;
663 663 // unsigned int credit_err;
664 664 // unsigned int write_sync_err;
665 665 // unsigned int disconnect_err;
666 666 // unsigned int early_ep;
667 667 // unsigned int invalid_address;
668 668 // unsigned int packets_sent;
669 669 // unsigned int packets_received;
670 670 // } spw_stats;
671 671
672 672 // tx_link_err *** no code associated to this field
673 673 // rx_rmap_header_crc_err *** LE *** in HK
674 674 if (previous.rx_rmap_header_crc_err != current.rx_rmap_header_crc_err)
675 675 {
676 676 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
677 677 hk_lfr_last_er_code = CODE_HEADER_CRC;
678 678 update_hk_lfr_last_er = 1;
679 679 }
680 680 // rx_rmap_data_crc_err *** LE *** NOT IN HK
681 681 if (previous.rx_rmap_data_crc_err != current.rx_rmap_data_crc_err)
682 682 {
683 683 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
684 684 hk_lfr_last_er_code = CODE_DATA_CRC;
685 685 update_hk_lfr_last_er = 1;
686 686 }
687 687 // rx_eep_err
688 688 if (previous.rx_eep_err != current.rx_eep_err)
689 689 {
690 690 hk_lfr_last_er_rid = RID_ME_LFR_DPU_SPW;
691 691 hk_lfr_last_er_code = CODE_EEP;
692 692 update_hk_lfr_last_er = 1;
693 693 }
694 694 // rx_truncated
695 695 if (previous.rx_truncated != current.rx_truncated)
696 696 {
697 697 hk_lfr_last_er_rid = RID_ME_LFR_DPU_SPW;
698 698 hk_lfr_last_er_code = CODE_RX_TOO_BIG;
699 699 update_hk_lfr_last_er = 1;
700 700 }
701 701 // parity_err
702 702 if (previous.parity_err != current.parity_err)
703 703 {
704 704 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
705 705 hk_lfr_last_er_code = CODE_PARITY;
706 706 update_hk_lfr_last_er = 1;
707 707 }
708 708 // escape_err
709 709 if (previous.parity_err != current.parity_err)
710 710 {
711 711 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
712 712 hk_lfr_last_er_code = CODE_ESCAPE;
713 713 update_hk_lfr_last_er = 1;
714 714 }
715 715 // credit_err
716 716 if (previous.credit_err != current.credit_err)
717 717 {
718 718 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
719 719 hk_lfr_last_er_code = CODE_CREDIT;
720 720 update_hk_lfr_last_er = 1;
721 721 }
722 722 // write_sync_err
723 723 if (previous.write_sync_err != current.write_sync_err)
724 724 {
725 725 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
726 726 hk_lfr_last_er_code = CODE_WRITE_SYNC;
727 727 update_hk_lfr_last_er = 1;
728 728 }
729 729 // disconnect_err
730 730 if (previous.disconnect_err != current.disconnect_err)
731 731 {
732 732 hk_lfr_last_er_rid = RID_LE_LFR_DPU_SPW;
733 733 hk_lfr_last_er_code = CODE_DISCONNECT;
734 734 update_hk_lfr_last_er = 1;
735 735 }
736 736 // early_ep
737 737 if (previous.early_ep != current.early_ep)
738 738 {
739 739 hk_lfr_last_er_rid = RID_ME_LFR_DPU_SPW;
740 740 hk_lfr_last_er_code = CODE_EARLY_EOP_EEP;
741 741 update_hk_lfr_last_er = 1;
742 742 }
743 743 // invalid_address
744 744 if (previous.invalid_address != current.invalid_address)
745 745 {
746 746 hk_lfr_last_er_rid = RID_ME_LFR_DPU_SPW;
747 747 hk_lfr_last_er_code = CODE_INVALID_ADDRESS;
748 748 update_hk_lfr_last_er = 1;
749 749 }
750 750
751 751 // if a field has changed, update the hk_last_er fields
752 752 if (update_hk_lfr_last_er == 1)
753 753 {
754 754 update_hk_lfr_last_er_fields( hk_lfr_last_er_rid, hk_lfr_last_er_code );
755 755 }
756 756
757 757 previous = current;
758 758 }
759 759
760 760 void update_hk_lfr_last_er_fields(unsigned int rid, unsigned char code)
761 761 {
762 762 unsigned char *coarseTimePtr;
763 763 unsigned char *fineTimePtr;
764 764
765 765 coarseTimePtr = (unsigned char*) &time_management_regs->coarse_time;
766 766 fineTimePtr = (unsigned char*) &time_management_regs->fine_time;
767 767
768 768 housekeeping_packet.hk_lfr_last_er_rid[0] = (unsigned char) ((rid & 0xff00) >> 8 );
769 769 housekeeping_packet.hk_lfr_last_er_rid[1] = (unsigned char) (rid & 0x00ff);
770 770 housekeeping_packet.hk_lfr_last_er_code = code;
771 771 housekeeping_packet.hk_lfr_last_er_time[0] = coarseTimePtr[0];
772 772 housekeeping_packet.hk_lfr_last_er_time[1] = coarseTimePtr[1];
773 773 housekeeping_packet.hk_lfr_last_er_time[2] = coarseTimePtr[2];
774 774 housekeeping_packet.hk_lfr_last_er_time[3] = coarseTimePtr[3];
775 775 housekeeping_packet.hk_lfr_last_er_time[4] = fineTimePtr[2];
776 776 housekeeping_packet.hk_lfr_last_er_time[5] = fineTimePtr[3];
777 777 }
778 778
779