92d9d9dcd8e58894a906448554251959bd9b1006
[bertos.git] / bertos / benchmark / context_switch.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Context switch benchmark
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  * \author Daniele Basiele <asterix@develer.com>
37  */
38
39 #include "context_switch.h"
40
41 #include "hw/hw_led.h"
42
43 #include "cfg/cfg_context_switch.h"
44 #include <cfg/debug.h>
45
46 #include <cpu/irq.h>
47 #include <cpu/power.h>
48
49 #include <drv/timer.h>
50 #include <drv/ser.h>
51
52 #include <kern/proc.h>
53
54 #define PROC_STACK_SIZE    1024
55
56 static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
57 static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
58
59 static Process *hp_proc, *lp_proc, *main_proc;
60 Serial out;
61
62 #if CONFIG_USE_HP_TIMER
63
64 static hptime_t start, end;
65
66 #endif
67
68 static void NORETURN hp_process(void)
69 {
70         proc_setPri(hp_proc, 2);
71
72         while (1)
73         {
74                 sig_wait(SIG_USER0);
75                 #if CONFIG_USE_HP_TIMER
76                         end = timer_clock_hp();
77                 #endif
78                 #if CONFIG_USE_LED
79                         LED_ON();
80                 #endif
81                 timer_delay(100);
82                 sig_send(main_proc, SIG_USER1);
83         }
84 }
85
86 static void NORETURN lp_process(void)
87 {
88         proc_setPri(lp_proc, 1);
89
90         while (1)
91         {
92                 sig_wait(SIG_USER1);
93                 #if CONFIG_USE_HP_TIMER
94                         start = timer_clock_hp();
95                 #endif
96                 #if CONFIG_USE_LED
97                         LED_ON();
98                         LED_OFF();
99                 #endif
100
101                 sig_send(hp_proc, SIG_USER0);
102         }
103 }
104
105
106 void NORETURN context_switch(void)
107 {
108         IRQ_ENABLE;
109         timer_init();
110         proc_init();
111
112         ser_init(&out, CONFIG_CTX_DEBUG_PORT);
113         ser_setbaudrate(&out, CONFIG_CTX_DEBUG_BAUDRATE);
114
115         #if CONFIG_USE_LED
116                 LED_INIT();
117         #endif
118
119         hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
120         lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
121         main_proc = proc_current();
122
123         while (1)
124         {
125                 #if CONFIG_USE_HP_TIMER
126                         kfile_printf(&out.fd, "Switch: %lu.%lu usec\n",
127                                                         hptime_to_us((end - start)),
128                                                         hptime_to_us((end - start) * 1000) % 1000);
129                 #endif
130
131                 sig_send(lp_proc, SIG_USER1);
132                 sig_wait(SIG_USER1);
133         }
134
135 }