summaryrefslogtreecommitdiff
path: root/led/complex1/periodic.c
blob: b57e0f0c9e3f09ba1ef7b4bed6c816fde0ed9f59 (plain)
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
/*
 *  $Id$
 */

#if defined(__rtems__)
#include <rtems.h>
#include <stdio.h>
#include "periodic.h"

#define MAX_PERIODIC 100

rtems_id PeriodicThreads_Ids[100];
rtems_id PeriodicThreads_Periods[100];

bool PeriodicThreads_initialized = FALSE;
bool PeriodicThreads_running = FALSE;

unsigned int PeriodicThreads_Count = 10;

int factorial( int i );

rtems_task PeriodicThreads_Body(
  rtems_task_argument index
)
{
  /* int i; */
  int t = index + 1;

  fprintf( stderr, "Periodic task %d starting\n", t );
  /* this doesn't seem to dirty the stack :( */
  factorial(t * 100);
  while (1) {
    rtems_rate_monotonic_period( PeriodicThreads_Periods[index], t * 2 );
    /* for (i=0 ; i<(100000 * t) ; i++ ) ;  */
    rtems_task_wake_after( t * 2 );
  }
}

/* this doesn't seem to dirty the stack :( */
int factorial( int i )
{
  if ( i == 1 ) {
    return 1;
  }
  return i * (i - 1);
}

void PeriodicThreads_Initialize(void)
{
  int i;
  rtems_status_code s;
  rtems_name name;
  char buf[6];

  if ( PeriodicThreads_initialized )
    return;
  PeriodicThreads_running = FALSE;

  for( i=0 ; i<PeriodicThreads_Count ; i++ ) {
    sprintf( buf, "PE%02x", i );
    name = rtems_build_name( buf[0], buf[1], buf[2], buf[3] );
    s = rtems_rate_monotonic_create( name, &PeriodicThreads_Periods[i] );
    if ( s ) {
      fprintf( stderr, "PeriodicThreads -- period create failed\n" );
      return;
    }

    sprintf( buf, "TA%02x", i );
    name = rtems_build_name( buf[0], buf[1], buf[2], buf[3] );
    s = rtems_task_create( name, 2, RTEMS_MINIMUM_STACK_SIZE * 4,
       RTEMS_DEFAULT_MODES, RTEMS_DEFAULT_ATTRIBUTES, &PeriodicThreads_Ids[i] );
    if ( s ) {
      fprintf( stderr, "PeriodicThreads -- task create failed\n" );
      return;
    }

    s = rtems_task_start( PeriodicThreads_Ids[i], PeriodicThreads_Body, i );
    if ( s ) {
      fprintf( stderr, "PeriodicThreads -- task start failed\n" );
      return;
    }
  } 

  PeriodicThreads_running = TRUE;
  PeriodicThreads_initialized = TRUE;
}

void PeriodicThreads_Start(void)
{
  PeriodicThreads_Initialize();
}

void PeriodicThreads_Stop(void)
{
  rtems_status_code s;
  int i;

  if ( !PeriodicThreads_initialized )
    return;

  fprintf( stderr, "Stopping Periodic Threads\n" );
  for ( i=0 ; i<PeriodicThreads_Count ; i++ ) {
    s = rtems_rate_monotonic_delete( PeriodicThreads_Periods[i] );
    if ( s ) {
      fprintf( stderr, "PeriodicThreads -- period delete failed\n" );
      return;
    }

    s = rtems_task_delete( PeriodicThreads_Ids[i] );
    if ( s ) {
      fprintf( stderr, "PeriodicThreads -- task delete failed\n" );
      return;
    }
  }
  PeriodicThreads_running = FALSE;
  PeriodicThreads_initialized = FALSE;
}

#endif