Update to new kernel config: kernel if OFF by default now.
[bertos.git] / bertos / kern / proc_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Test kernel process.
34  *
35  * \version $Id$
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * $test$: cp bertos/cfg/cfg_proc.h $cfgdir/
39  * $test$: echo  "#undef CONFIG_KERN" >> $cfgdir/cfg_proc.h
40  * $test$: echo "#define CONFIG_KERN 1" >> $cfgdir/cfg_proc.h
41  * $test$: cp bertos/cfg/cfg_monitor.h $cfgdir/
42  * $test$: echo  "#undef CONFIG_KERN_MONITOR" >> $cfgdir/cfg_monitor.h
43  * $test$: echo "#define CONFIG_KERN_MONITOR 1" >> $cfgdir/cfg_monitor.h
44  * $test$: cp bertos/cfg/cfg_signal.h $cfgdir/
45  * $test$: echo  "#undef CONFIG_KERN_SIGNALS" >> $cfgdir/cfg_signal.h
46  * $test$: echo "#define CONFIG_KERN_SIGNALS 1" >> $cfgdir/cfg_signal.h
47  */
48
49 #include <kern/proc.h>
50 #include <kern/irq.h>
51 #include <kern/monitor.h>
52
53 #include <drv/timer.h>
54 #include <cfg/test.h>
55
56
57 // Global settings for the test.
58 #define DELAY                           5
59
60 // Settings for the test process.
61 //Process 1
62 #define INC_PROC_T1                     1
63 #define DELAY_PROC_T1   INC_PROC_T1*DELAY
64 //Process 2
65 #define INC_PROC_T2                     3
66 #define DELAY_PROC_T2   INC_PROC_T2*DELAY
67 //Process 3
68 #define INC_PROC_T3                     5
69 #define DELAY_PROC_T3   INC_PROC_T3*DELAY
70 //Process 4
71 #define INC_PROC_T4                     7
72 #define DELAY_PROC_T4   INC_PROC_T4*DELAY
73 //Process 5
74 #define INC_PROC_T5                    11
75 #define DELAY_PROC_T5   INC_PROC_T5*DELAY
76 //Process 6
77 #define INC_PROC_T6                    13
78 #define DELAY_PROC_T6   INC_PROC_T6*DELAY
79 //Process 7
80 #define INC_PROC_T7                    17
81 #define DELAY_PROC_T7   INC_PROC_T7*DELAY
82 //Process 8
83 #define INC_PROC_T8                    19
84 #define DELAY_PROC_T8   INC_PROC_T8*DELAY
85
86 //Global count for each process.
87 unsigned int t1_count = 0;
88 unsigned int t2_count = 0;
89 unsigned int t3_count = 0;
90 unsigned int t4_count = 0;
91 unsigned int t5_count = 0;
92 unsigned int t6_count = 0;
93 unsigned int t7_count = 0;
94 unsigned int t8_count = 0;
95
96 /*
97  * These macros generate the code needed to create the test process functions.
98  */
99 #define PROC_TEST(num) static void proc_test##num(void) \
100 { \
101         for (int i = 0; i < INC_PROC_T##num; ++i) \
102         { \
103                 t##num##_count++; \
104                 kprintf("> Process[%d]: count[%d]\n", num, t##num##_count); \
105                 timer_delay(DELAY_PROC_T##num); \
106         } \
107 } \
108
109 #define PROC_TEST_STACK(num)  static cpu_stack_t proc_test##num##_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
110 #define PROC_TEST_INIT(num)   proc_new(proc_test##num, NULL, sizeof(proc_test##num##_stack), proc_test##num##_stack);
111
112 // Define process
113 PROC_TEST(1)
114 PROC_TEST(2)
115 PROC_TEST(3)
116 PROC_TEST(4)
117 PROC_TEST(5)
118 PROC_TEST(6)
119 PROC_TEST(7)
120 PROC_TEST(8)
121
122 // Define process stacks for test.
123 PROC_TEST_STACK(1)
124 PROC_TEST_STACK(2)
125 PROC_TEST_STACK(3)
126 PROC_TEST_STACK(4)
127 PROC_TEST_STACK(5)
128 PROC_TEST_STACK(6)
129 PROC_TEST_STACK(7)
130 PROC_TEST_STACK(8)
131
132
133 /**
134  * Process scheduling test
135  */
136 int proc_testRun(void)
137 {
138         kprintf("Run Process test..\n");
139
140         //Init the process tests
141         PROC_TEST_INIT(1)
142         PROC_TEST_INIT(2)
143         PROC_TEST_INIT(3)
144         PROC_TEST_INIT(4)
145         PROC_TEST_INIT(5)
146         PROC_TEST_INIT(6)
147         PROC_TEST_INIT(7)
148         PROC_TEST_INIT(8)
149         kputs("> Main: Processes created\n");
150
151         for (int i = 0; i < 30; ++i)
152         {
153                 kputs("> Main\n");
154                 timer_delay(93);
155                 monitor_report();
156         }
157
158         if( t1_count == INC_PROC_T1 &&
159                 t2_count == INC_PROC_T2 &&
160                 t3_count == INC_PROC_T3 &&
161                 t4_count == INC_PROC_T4 &&
162                 t5_count == INC_PROC_T5 &&
163                 t6_count == INC_PROC_T6 &&
164                 t7_count == INC_PROC_T7 &&
165                 t8_count == INC_PROC_T8)
166         {
167                 kputs("> Main: process test finished..ok!\n");
168                 return 0;
169         }
170
171         kputs("> Main: process test..fail!\n");
172         return -1;
173 }
174
175
176 int proc_testSetup(void)
177 {
178         kdbg_init();
179
180         #if CONFIG_KERN_PREEMPT
181                 kprintf("Init Interrupt (preempt mode)..");
182                 irq_init();
183                 kprintf("Done.\n");
184         #endif
185
186         kprintf("Init Timer..");
187         timer_init();
188         kprintf("Done.\n");
189
190         kprintf("Init Process..");
191         proc_init();
192         kprintf("Done.\n");
193
194         return 0;
195 }
196
197 int proc_testTearDown(void)
198 {
199         kputs("TearDown Process test.\n");
200         return 0;
201 }
202
203 TEST_MAIN(proc);