Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: ego.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-09-02 09:37:41 $
26
 ------------
27
**/
28
 
29
/*
30
 * Copyright (C) 2000 Paolo Gai and Giorgio Buttazzo
31
 *
32
 * This program is free software; you can redistribute it and/or modify
33
 * it under the terms of the GNU General Public License as published by
34
 * the Free Software Foundation; either version 2 of the License, or
35
 * (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45
 *
46
 */
47
 
48
/****************************************************************/
49
/*      PERIODIC PROCESS TEST                                   */
50
/****************************************************************/
51
 
52
#include <kernel/kern.h>
53
#include <drivers/glib.h>
54
#include <drivers/keyb.h>
55
 
56
#include <semaphore.h>
57
 
58
#define X0      10
59
 
60
/* task periods */
61
#define PERIOD_T1 100000
62
#define PERIOD_T2 200000
63
#define PERIOD_T3 300000
64
 
65
/* X position of the text printed by each task */
66
int     y[3] = {100, 180, 260};
67
 
68
/* text printed by each task */
69
char    talk[3][50] = { "I am ego1 and I print a character every 100 ms",
70
                        "I am ego2 and I print a character every 200 ms",
71
                        "I am ego3 and I print a character every 300 ms"};
72
 
73
/* A semaphore used to access Video Cards in mutual exclusion */
74
sem_t   mutex;
75
 
76
/***************************************************************/
77
 
78
TASK    ego(void *arg)
79
{
80
int     i = (int)arg;
81
int     leng;
82
char    s[2];
83
int     x;
84
int     j = 0;
85
 
86
        /* compute the length of the string to print */
87
        leng = 0;
88
        while (talk[i][leng] != 0) leng++;
89
 
90
        x = X0;
91
        s[1] = 0;
92
        task_endcycle();
93
 
94
        while (1) {
95
                s[0] = talk[i][j];
96
                sem_wait(&mutex);
97
                /* grx_text("TEST", 100,100,12,0); */
98
                grx_text(s,x,y[i],12+i,0);
99
                sem_post(&mutex);
100
                x += 8;
101
                if (++j == leng) {
102
                        j = 0;
103
                        x = X0;
104
                        y[i] += 8;
105
                        if (y[i]>340) y[i]=100;
106
                }
107
                task_endcycle();
108
        }
109
}
110
 
111
 
112
/****************************************************************/
113
 
114
/* This is the exception handler. It is called when an exception
115
   is raised.
116
   It exits from the graphical mode, then it prints a message and
117
   shutdown the kernel using sys_abort()
118
*/
119
 
120
void demo_exc_handler(int signo, siginfo_t *info, void *extra)
121
{
122
  struct timespec t;
123
 
124
  grx_close();
125
 
126
  /* Default action for an kern exception is  */
127
  kern_cli();
128
  ll_gettime(TIME_EXACT, &t),
129
  kern_printf("\nS.Ha.R.K. Exception raised!!!"
130
              "\nTime (s:ns)     :%ld:%ld"
131
              "\nException number:%d (numbers in include/bits/errno.h)"
132
              "\nPID             :%d\n",
133
              t.tv_sec, t.tv_nsec, info->si_value.sival_int,
134
              info->si_task);
135
  sys_abort(1);
136
}
137
 
138
/******************************************************************/
139
 
140
/* This function is called when Alt-X is pressed.
141
   It simply shutdown the system using sys_end.
142
   Note that the byebye() function is called only if we exit from
143
   the system using sys_end()!!!!
144
*/
145
void my_end(KEY_EVT* e)
146
{
147
        sys_end();
148
}
149
 
150
/******************************************************************/
151
 
152
/* This function is called when the system exit correctly after Alt-X.
153
   It exits from the graphic mode and then it prints a small greeting.
154
   Note that:
155
   - The function calls grx_exit, so it must be registered using
156
     RUNLEVEL_BEFORE_EXIT (RUNLEVEL_AFTER_EXIT does not work because
157
     at that point the kernel is already returned in real mode!!!)
158
   - When an exception is raised, the exception handler is called.
159
     Since the exception handler already exits from the graphic mode,
160
     this funcion has not to be called. For this reason:
161
     . we registered byebye using the flag NO_AT_ABORT
162
     . the exception handler exits using sys_abort; in that way byebye is
163
       NOT called
164
*/
165
 
166
void byebye(void *arg)
167
{
168
  grx_close();
169
  kern_printf("Bye Bye!\n");
170
}
171
 
172
/****************************** MAIN ******************************/
173
 
174
int main(int argc, char **argv)
175
{
176
  PID             pid1, pid2, pid3;
177
  KEY_EVT         emerg;
178
  HARD_TASK_MODEL m1, m2, m3;
179
  struct sigaction action;
180
 
181
        /* Init the standard S.Ha.R.K. exception handler */
182
        action.sa_flags = SA_SIGINFO;            /* Set the signal action */
183
        action.sa_sigaction = demo_exc_handler;
184
        action.sa_handler = 0;
185
        sigfillset(&action.sa_mask); /* we block all the other signals... */
186
 
187
        if (sigaction(SIGHEXC, &action, NULL) == -1) {  /* set the signal */
188
          perror("Error initializing signals...");
189
          sys_end();
190
        }
191
 
192
        /* Set the closing function */
193
        sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT|NO_AT_ABORT);
194
 
195
        /* Initializes the semaphore */
196
        sem_init(&mutex,0,1);
197
 
198
        /* graphic card Initialization */
199
        if (grx_init() < 1) {
200
           sys_abort(1);
201
        }
202
 
203
        if (grx_open(640, 480, 8) < 0) {
204
            kern_printf("GRX Err\n");
205
            sys_abort(1);
206
        }
207
        kern_printf("Video card ok!\n");
208
 
209
        /* set the keyboard handler to exit correctly */
210
        emerg.ascii = 'x';
211
        emerg.scan = KEY_X;
212
        emerg.flag = ALTL_BIT;
213
        keyb_hook(emerg,my_end);
214
 
215
        emerg.ascii = 'x';
216
        emerg.scan = KEY_X;
217
        emerg.flag = ALTR_BIT;
218
        keyb_hook(emerg,my_end);
219
 
220
        /* a small banner */
221
        grx_text("EGO Test",8,8,WHITE,0);
222
        grx_text("Press Alt-X to exit",8,16,WHITE,0);
223
 
224
        /* ego1 creation */
225
        hard_task_default_model(m1);
226
        hard_task_def_ctrl_jet (m1);
227
        hard_task_def_arg      (m1, (void *)0);
228
        hard_task_def_wcet     (m1, 5000);
229
        hard_task_def_mit      (m1, PERIOD_T1);
230
        hard_task_def_group    (m1,1);
231
        pid1 = task_create("ego1", ego, &m1, NULL);
232
        if (pid1 == NIL) {
233
          grx_close();
234
          perror("Could not create task <ego1>");
235
          sys_abort(1);
236
        }
237
 
238
        /* ego2 creation */
239
        hard_task_default_model(m2);
240
        hard_task_def_ctrl_jet (m2);
241
        hard_task_def_arg      (m2, (void *)1);
242
        hard_task_def_wcet     (m2, 5000);
243
        hard_task_def_mit      (m2, PERIOD_T2);
244
        hard_task_def_group    (m2,1);
245
        pid2 = task_create("ego2", ego, &m2, NULL);
246
        if (pid2 == NIL) {
247
          grx_close();
248
          perror("Could not create task <ego2>");
249
          sys_abort(1);
250
        }
251
 
252
        /* ego3 creation */
253
        hard_task_default_model(m3);
254
        hard_task_def_ctrl_jet (m3);
255
        hard_task_def_arg      (m3, (void *)2);
256
        hard_task_def_wcet     (m3, 5000);
257
        hard_task_def_mit      (m3, PERIOD_T3);
258
        hard_task_def_group    (m3,1);
259
        pid3 = task_create("ego3", ego, &m3, NULL);
260
        if (pid3 == NIL) {
261
          grx_close();
262
          perror("Could not create task <ego3>");
263
          sys_abort(1);
264
        }
265
 
266
        /* and finally we activate the three threads... */
267
        group_activate(1);
268
 
269
        /*
270
           now the task main ends, but the system does not shutdown because
271
           there are the three task ego1, ego2, and ego3 running.
272
 
273
           The demo will finish if a Alt-X key is pressed.
274
        */
275
 
276
        return 0;
277
}
278
 
279
/****************************************************************/