Release version 2.5.1.
[bertos.git] / 2.5 / 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 #if CONFIG_USE_HP_TIMER
51 #include <drv/ser.h>
52 static Serial out;
53 #endif
54
55 #include <kern/proc.h>
56
57 #define PROC_STACK_SIZE    KERN_MINSTACKSIZE
58
59 static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
60 static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
61
62 static Process *hp_proc, *lp_proc, *main_proc;
63 #if CONFIG_USE_HP_TIMER
64 static hptime_t start, end;
65 #endif
66
67 static void NORETURN hp_process(void)
68 {
69         while (1)
70         {
71                 sig_wait(SIG_USER0);
72                 #if CONFIG_USE_LED
73                         LED_ON();
74                 #endif
75                 #if CONFIG_USE_HP_TIMER
76                         end = timer_hw_hpread();
77                 #endif
78                 sig_send(main_proc, SIG_USER0);
79         }
80 }
81
82 static void NORETURN lp_process(void)
83 {
84         while (1)
85         {
86                 sig_wait(SIG_USER0);
87                 #if CONFIG_USE_LED
88                         LED_ON();
89                         LED_OFF();
90                 #endif
91                 #if CONFIG_USE_HP_TIMER
92                         start = timer_hw_hpread();
93                 #endif
94                 sig_send(hp_proc, SIG_USER0);
95         }
96 }
97
98
99 void NORETURN context_switch(void)
100 {
101         IRQ_ENABLE;
102         timer_init();
103         proc_init();
104
105         #if CONFIG_USE_HP_TIMER
106                 ser_init(&out, CONFIG_CTX_DEBUG_PORT);
107                 ser_setbaudrate(&out, CONFIG_CTX_DEBUG_BAUDRATE);
108         #endif
109
110         #if CONFIG_USE_LED
111                 LED_INIT();
112         #endif
113
114         proc_forbid();
115         hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
116         lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
117         main_proc = proc_current();
118         proc_setPri(hp_proc, 2);
119         proc_setPri(lp_proc, 1);
120         proc_permit();
121
122         while (1)
123         {
124                 timer_delay(100);
125
126                 sig_send(lp_proc, SIG_USER0);
127                 sig_wait(SIG_USER0);
128
129                 #if CONFIG_USE_HP_TIMER
130                         kfile_printf(&out.fd,
131                                 "Switch: %lu.%lu usec\n\r",
132                                 hptime_to_us((end - start)),
133                                 hptime_to_us((end - start) * 1000) % 1000);
134                 #endif
135         }
136 }