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