summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/sp35/priinv.c
blob: 417462418a21425f678c6a29d81e5290e8822a7e (plain) (blame)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 *  Simple test program to demonstrate priority inversion when blocking
 *
   -Ulf Ivraeus ------------- --- -- -  -   -
 *  Saab Ericsson Space AB
 *  phone:  +46 (0)31 735 4542
 *  e-mail: ulf.ivraeus@space.se
 *  ----------------------------- --- -- -  -   -
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

/********************************************************************

  Semaphore Ids:
    Sync Mutex Id   = 0x18010009
    Local Mutex Id  = 0x1801000a
    Remote Mutex Id = 0x1801000b

  Task Ids:
    TaMedium[0] Id  = 0x08010002
    TaMedium[1] Id  = 0x08010003
    TaMedium[2] Id  = 0x08010004
    TaHigh Id       = 0x08010005
    TaLow Id        = 0x08010006
    TaHwSim Id      = 0x08010007

********************************************************************/

/********************************************************************/
/* define this to use the RTEMS 4.5 scheme for object names */
/* #define TEST_ON_RTEMS_45 */

/* define this to print the Id of the calling task */
/* #define TEST_ON_TASK_ID */

/* define this if you are (1) on a ERC32 and (2) want a SW ISR trigger */
#if defined(__sparc__)
/* #define TEST_USE_ISR */
#endif

/* define this if you want the test to exit */
#define TEST_EXIT_AFTER_ITERATIONS 10
/********************************************************************/

#define CONFIGURE_INIT

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <bsp.h>
#include <stdio.h>
#include "tmacros.h"

#include <rtems/score/threadimpl.h>

const char rtems_test_name[] = "SP 35";

#if defined(TEST_EXIT_AFTER_ITERATIONS)
volatile uint32_t Iterations = 0;
#endif

const char *CallerName(void);

/* Task entry point prototypes */
rtems_task Init(rtems_task_argument ignored);
rtems_task Medium_Exec(rtems_task_argument TaskArg);
rtems_task Low_Exec(rtems_task_argument TaskArg);
rtems_task High_Exec(rtems_task_argument TaskArg);
rtems_task LocalHwSim_Exec(rtems_task_argument TaskArg);

/* ISR that signals that HW has completed */
rtems_isr  LocalHwIsr(/*in*/ rtems_vector_number   Vector);

/* Test driver functions */
void AccessLocalHw(void);
void AccessRemoteHw(void);

const char *CallerName(void)
{
  static char buffer[32];
  Thread_Control *executing = _Thread_Get_executing();
#if defined(TEST_PRINT_TASK_ID)
  sprintf( buffer, "0x%08x -- %d",
      rtems_task_self(), _Thread_Get_priority( executing ) );
#else
  volatile union {
    uint32_t u;
    unsigned char c[4];
  } TempName;

  #if defined(TEST_ON_RTEMS_45)
    TempName.u = *(uint32_t *)executing->Object.name;
  #else
    TempName.u = (uint32_t) executing->Object.name.name_u32;
  #endif
  sprintf( buffer, "%c%c%c%c -- %" PRIdPriority_Control,
      TempName.c[0], TempName.c[1], TempName.c[2], TempName.c[3],
      _Thread_Get_priority( executing )
  );
#endif
  return buffer;
}

#define NofMediumTask_C 3

/* RTEMS identifiers */
rtems_id  TaMedium[NofMediumTask_C]; /* Medium-prio tasks accessing */
                                     /*    the common local HW */
rtems_id  TaHwSim;                   /* HW simulator */
rtems_id  TaLow;                     /* Low-prio task accessing common */
                                     /*    remote HW */
rtems_id  TaHigh;                    /* High-prio task accessing common */
                                     /*    remote HW */


rtems_id  LocalHwSync_S;             /* Syncrhonize task to local HW */
rtems_id  LocalHwAccess_R;           /* Execlusive access to the local HW */
rtems_id  RemoteHwAccess_R;          /* Execlusive access to the remote HW */


/* The following variable triggers simulated HW activity */
volatile bool StartHw = false;

rtems_task Medium_Exec(rtems_task_argument TaskArg)
{
  printf("Medium_Exec (%" PRIdrtems_task_argument ") begins...\n", TaskArg);

  rtems_task_wake_after(50);

  while(1) {
    AccessLocalHw();
  }

  /* JRS - task does not get here */

  printf("Medium_Exec (%" PRIdrtems_task_argument ") ends...\n", TaskArg);
  while(1) {
    rtems_task_wake_after(10000);
  }
}

rtems_task High_Exec(rtems_task_argument TaskArg)
{
  printf("High_Exec (%" PRIdrtems_task_argument ") begins...\n", TaskArg);

  /* Delay more than the Low-prio task so that Remote HW access resource is
   * taken before call to AccesRemoteHw.
   */
  rtems_task_wake_after(250);

  while(1) {
    AccessRemoteHw();
  }

  /* JRS - task does not get here */

  printf("High_Exec (%" PRIdrtems_task_argument ") ends...\n", TaskArg);
  while(1) {
    rtems_task_wake_after(10000);
  }
  /* task does not get here */
}

rtems_task Low_Exec(rtems_task_argument TaskArg)
{
  printf("Low_Exec (%" PRIdrtems_task_argument ") begins...\n", TaskArg);

  /* Delay less than High-prio task so that we take the remote HW access
   * resource before it does it. However, delay longer than the mid-prio
   * tasks so that we know for certain that they have taken control over
   * the local HW access.
   */
  rtems_task_wake_after(100);

  while(1) {
    AccessRemoteHw();
  }

  /* JRS - task does not get here */

  printf("Low_Exec (%" PRIdrtems_task_argument ") ends...\n", TaskArg);
  while(1) {
    rtems_task_wake_after(10000);
  }
}


rtems_task LocalHwSim_Exec(rtems_task_argument TaskArg)
{
#if 0
  int ISRCount = 0;
#endif
  printf("LocalHwSim_Exec begins...\n");

  while(1) {
    if (StartHw) {
      /* A test task has activated the HW, wait for a while and then
       * generate an interrupt
       */
      rtems_task_wake_after(100);

      StartHw = FALSE;
#if defined(TEST_USE_ISR)
      __asm__ volatile ("ta 5");
      __asm__ volatile ("nop");
      __asm__ volatile ("nop");
#else
      LocalHwIsr( 0x85 );
#endif
    }

  }

  printf("LocalHwSim_Exec ends...\n");
  while(1) {
    rtems_task_wake_after(10000);
  }
}


rtems_isr  LocalHwIsr(/*in*/ rtems_vector_number   Vector)
{
  rtems_status_code status;

  /* Signal synchroniztion semaphore to invoke waiting task */
  status = rtems_semaphore_release(LocalHwSync_S);
  if (status != RTEMS_SUCCESSFUL) {
    printf( "LocalHwISR release %d\n", status );
    while(1); /* Error */
  }

  return;
}

void AccessLocalHw(void)
{
  rtems_status_code     Sts;

  printf("  AccessLocalHw called by %s\n", CallerName());

  /* Obtain exclusive access to local HW, Start HW, Wait for completion,
   * Release access
   */

  Sts = rtems_semaphore_obtain(LocalHwAccess_R, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  directive_failed( Sts, "rtems_semaphore_obtain(LocalHwAccess_R...)" );

  StartHw = TRUE;

  Sts = rtems_semaphore_obtain(LocalHwSync_S, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  directive_failed( Sts, "rtems_semaphore_obtain(LocalHwAccess_R...)" );

  Sts = rtems_semaphore_release(LocalHwAccess_R);
  directive_failed( Sts, "rtems_semaphore_release(LocalHwAccess_R)" );

  printf("  AccessLocalHw returns to %s\n", CallerName());
  #if defined(TEST_EXIT_AFTER_ITERATIONS)
    if ( ++Iterations == 10 ) {
      TEST_END();
      exit(0);
    }
  #endif
  return;
}

void AccessRemoteHw(void)
{
  rtems_status_code     Sts;

  printf("AccessRemoteHw called by %s\n", CallerName());

  /* Obtain exclusive access to remote HW, Start HW, Wait for completion,
   * Release access
   */

  Sts = rtems_semaphore_obtain(RemoteHwAccess_R, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  directive_failed( Sts, "rtems_semaphore_obtain(RemoteHwAccess_R...)" );

  /* Carry out the remote access via the Local HW interface */
  printf("AccessRemoteHw access local %s\n", CallerName());
  AccessLocalHw();

  Sts = rtems_semaphore_release(RemoteHwAccess_R);
  directive_failed( Sts, "rtems_semaphore_release(RemoreHwAccess_R" );

  printf("AccessRemoteHw returns to %s\n", CallerName());
  return;
}

/*************************************************************************/
/**********************        INITIALIZATION        *********************/
/*************************************************************************/

/* The Init operation (the Init-task) */
rtems_task Init(rtems_task_argument ignored)
{
  rtems_status_code status;
#if defined(TEST_USE_ISR)
  rtems_isr_entry   DummyIsr;
#endif
  int i;

  TEST_BEGIN();

  /* Create synchronisation semaphore for LocalHwIsr -> Test Tasks */
  status = rtems_semaphore_create(
    rtems_build_name ('S', 'Y', 'N', 'C'),           /* name */
    0,                                               /* initial count = 0 */
    RTEMS_LOCAL                   |
    RTEMS_SIMPLE_BINARY_SEMAPHORE |
    RTEMS_NO_INHERIT_PRIORITY     |
    RTEMS_NO_PRIORITY_CEILING     |
    RTEMS_FIFO,
    0,
    &LocalHwSync_S);                                 /* *id */
  directive_failed( status, "rtems_semaphore_create (SYNC)" );

  printf( "Sync Mutex Id = 0x%08" PRIxrtems_id "\n", LocalHwSync_S );

  /* Create resource semaphore for exclusive access to the local HW */
  status = rtems_semaphore_create(
    rtems_build_name ('R', 'E', 'S', '1'), /* name             */
    1,                                     /* count            */
    RTEMS_PRIORITY         |
    RTEMS_BINARY_SEMAPHORE |
    RTEMS_INHERIT_PRIORITY |
    RTEMS_LOCAL,                           /* attribute_set    */
    1, /* insignificant */                 /* priority_ceiling */
    &LocalHwAccess_R);                     /* *id              */
  directive_failed( status, "rtems_semaphore_create (RES1)" );

  printf( "Local Mutex Id = 0x%08" PRIxrtems_id "\n", LocalHwAccess_R );

  /* Create resource semaphore for exclusive access to the remote HW */
  status = rtems_semaphore_create(
    rtems_build_name ('R', 'E', 'S', '2'), /* name             */
    1,                                     /* count            */
    RTEMS_PRIORITY         |
    RTEMS_BINARY_SEMAPHORE |
    RTEMS_INHERIT_PRIORITY |
    RTEMS_LOCAL,                           /* attribute_set    */
    1, /* insignificant */                 /* priority_ceiling */
    &RemoteHwAccess_R);                    /* *id              */
  directive_failed( status, "rtems_semaphore_create (RES2)" );

  printf( "Remote Mutex Id = 0x%08" PRIxrtems_id "\n", RemoteHwAccess_R );

#if defined(TEST_USE_ISR)
  /* Install ISR for HW/SW synchronization, use ta 0x85 which is synchronous */
  status = rtems_interrupt_catch(LocalHwIsr, 0x85 + 0x100, &DummyIsr);
  directive_failed( status, "rtems_interrupt_catch" );
#endif


  printf("Ending Init-task\n");
  /* Create and start all tasks in the test */

  /* -- Medium-prio Test Tasks --- */
  for (i = 0; i < NofMediumTask_C; i++) {
#define MEDIUM_PRIORITY ((RTEMS_MAXIMUM_PRIORITY / 2u) + 1u)
    status = rtems_task_create(
      rtems_build_name('M','E','D','0'+i),               /* Name */
      MEDIUM_PRIORITY,                                   /* Priority */
      RTEMS_MINIMUM_STACK_SIZE*2,                        /* Stack size (8KB) */
      RTEMS_DEFAULT_MODES | RTEMS_NO_ASR,                /* Mode */
      RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,   /* Attributes */
      &TaMedium[i]);                                     /* Assigned ID */
    directive_failed( status, "rtems_task_create (MEDn)" );

    printf( "TaMedium[%d] Id = 0x%08" PRIxrtems_id "\n", i, TaMedium[i] );
    status = rtems_task_start(
      TaMedium[i],
      Medium_Exec,
      (rtems_task_argument) i
    );
    directive_failed( status, "rtems_task_start (MEDn)" );
  }

  /* -- High-prio Test Task --- */
#define HIGH_PRIORITY ((RTEMS_MAXIMUM_PRIORITY / 2u))
  status = rtems_task_create(
    rtems_build_name('H','I','G','H'),                 /* Name */
    HIGH_PRIORITY,                                     /* Priority */
    RTEMS_MINIMUM_STACK_SIZE*2,                        /* Stack size (8KB) */
    RTEMS_DEFAULT_MODES | RTEMS_NO_ASR,                /* Mode */
    RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,   /* Attributes */
    &TaHigh);                                          /* Assigned ID */
  directive_failed( status, "rtems_task_create (HIGH)" );

  printf( "TaHigh Id = 0x%08" PRIxrtems_id "\n", TaHigh );
  status = rtems_task_start(TaHigh, High_Exec, 0);
  directive_failed( status, "rtems_task_start (HIGH)" );

  /* -- Low-prio Test Task --- */
#define LOW_PRIORITY (RTEMS_MAXIMUM_PRIORITY - 1u)
  status = rtems_task_create(
    rtems_build_name('L','O','W',' '),                 /* Name */
    LOW_PRIORITY,                                      /* Priority */
    RTEMS_MINIMUM_STACK_SIZE*2,                        /* Stack size (8KB) */
    RTEMS_DEFAULT_MODES | RTEMS_NO_ASR,                /* Mode */
    RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,   /* Attributes */
    &TaLow);                                           /* Assigned ID */
  directive_failed( status, "rtems_task_create (LOW)" );

  printf( "TaLow Id = 0x%08" PRIxrtems_id "\n", TaLow );
  status = rtems_task_start(TaLow, Low_Exec, 0);
  directive_failed( status, "rtems_task_start (LOW)" );

  /* -- HW Simulator Task --- */
#define HWTASK_PRIORITY (RTEMS_MAXIMUM_PRIORITY - 2u)
  status = rtems_task_create(
    rtems_build_name('H','W','S','M'),                 /* Name */
    HWTASK_PRIORITY,                                   /* Priority */
    RTEMS_MINIMUM_STACK_SIZE*2,                        /* Stack size (8KB) */
    RTEMS_DEFAULT_MODES | RTEMS_NO_ASR,                /* Mode */
    RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,   /* Attributes */
    &TaHwSim);                                         /* Assigned ID */
  directive_failed( status, "rtems_task_create (HWSM)" );

  printf( "TaHwSim Id = 0x%08" PRIxrtems_id "\n", TaHwSim );

  status = rtems_task_start(TaHwSim, LocalHwSim_Exec, 0);
  directive_failed( status, "rtems_task_start (HWSM)" );

  /* Destroy the Init task (and let the ready tasks start running) */
  rtems_task_delete(RTEMS_SELF);
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_EXTRA_TASK_STACKS (RTEMS_MINIMUM_STACK_SIZE * 3)

#define CONFIGURE_MAXIMUM_TASKS 10
#define CONFIGURE_MAXIMUM_SEMAPHORES 10

#include <rtems/confdefs.h>

/* end of file */