Add at91sam7x bertos example.
[bertos.git] / examples / at91sam7x / at91sam7x.c
1 /**\r
2  * \file\r
3  * <!--\r
4  * This file is part of BeRTOS.\r
5  *\r
6  * Bertos is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * This program is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program; if not, write to the Free Software\r
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
19  *\r
20  * As a special exception, you may use this file as part of a free software\r
21  * library without restriction.  Specifically, if other files instantiate\r
22  * templates or use macros or inline functions from this file, or you compile\r
23  * this file and link it with other files to produce an executable, this\r
24  * file does not by itself cause the resulting executable to be covered by\r
25  * the GNU General Public License.  This exception does not however\r
26  * invalidate any other reasons why the executable file might be covered by\r
27  * the GNU General Public License.\r
28  *\r
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)\r
30  *\r
31  * -->\r
32  *\r
33  * \version $Id: at91sam7s.c 2663 2009-04-24 16:30:11Z asterix $\r
34  *\r
35  * \author Francesco Sacchi <batt@develer.com>\r
36  * \author Daniele Basile <asterix@develer.com>\r
37  *\r
38  * \brief Simple BeRTOS test on AT91SAM7X-EK evaluation board.\r
39  * \r
40  * This short program shows you a simple demo of some BeRTOS feature:\r
41  * \r
42  * - Debug system\r
43  * - Timer interrupt\r
44  * - Serial\r
45  * - Cooperative BeRTOS Kernel\r
46  * \r
47  */\r
48 \r
49 #include "cfg/cfg_ser.h"\r
50 #include <cfg/macros.h>\r
51 \r
52 #include <kern/proc.h>\r
53 \r
54 #include <drv/timer.h>\r
55 #include <drv/sysirq_at91.h>\r
56 #include <drv/ser.h>\r
57 \r
58 #include <io/arm.h>\r
59 \r
60 Timer leds_timer;\r
61 Serial ser_fd;\r
62 int roll = 0;\r
63 \r
64 /*\r
65  * Supercar leds effect..\r
66  */\r
67 static void leds_toggle(void)\r
68 {\r
69         uint32_t a = (~PIOB_ODSR & 0x780000);\r
70 \r
71         // Turn on led in forward direction\r
72         if (roll == 1)\r
73         {\r
74                 if(a == 0x200000)\r
75                         roll = 2;\r
76 \r
77                 PIOB_SODR = a;\r
78                 PIOB_CODR = a << 1;\r
79         }\r
80         // Turn on led in backward direction\r
81         else if (roll == 2)\r
82         {\r
83                 if(a == 0x100000)\r
84                         roll = 1;\r
85 \r
86                 PIOB_SODR = a;\r
87                 PIOB_CODR = a >> 1;\r
88         }\r
89         // Start to turn on first led\r
90         else\r
91         {\r
92                 PIOB_SODR  = 0x780000;\r
93                 /* turn first led on */\r
94                 PIOB_CODR  = 0x80000;\r
95                 roll = 1;\r
96         }\r
97 \r
98         /* Wait for interval time */\r
99         timer_setDelay(&leds_timer, ms_to_ticks(100));\r
100         timer_add(&leds_timer);\r
101 }\r
102 \r
103 int main(void)\r
104 {       char msg[]="BeRTOS, be fast be beatiful be realtime";\r
105 \r
106 \r
107         kdbg_init();\r
108         timer_init();\r
109         proc_init();\r
110 \r
111         ASSERT(!IRQ_ENABLED());\r
112 \r
113         /* Open the main communication port */\r
114         ser_init(&ser_fd, 0);\r
115         ser_setbaudrate(&ser_fd, 115200);\r
116         ser_setparity(&ser_fd, SER_PARITY_NONE);\r
117 \r
118         IRQ_ENABLE;\r
119         ASSERT(IRQ_ENABLED());\r
120 \r
121         /* Disable all pullups */\r
122         PIOB_PUDR = 0xffffffff;\r
123         /* Set PB0..3 connected to PIOA */\r
124         PIOB_PER  = 0x780000;\r
125         /* Set PB0..3 as output */\r
126         PIOB_OER  = 0x780000;\r
127         /* Disable multidrive on all pins */\r
128         PIOB_MDDR = 0xffffffff;\r
129 \r
130         /* Set PA0..3 to 1 to turn off leds */\r
131         PIOB_SODR  = 0x780000;\r
132         \r
133         /* turn first led on */\r
134         PIOB_CODR  = 0x80000;\r
135 \r
136         /*\r
137          * Register timer and arm timer interupt.\r
138          */\r
139         timer_setSoftint(&leds_timer, (Hook)leds_toggle, 0);\r
140         timer_setDelay(&leds_timer, ms_to_ticks(100));\r
141         timer_add(&leds_timer);\r
142 \r
143         /*\r
144          * Run process test.\r
145          */\r
146         if(!proc_testRun())\r
147                 kfile_printf(&ser_fd.fd, "ProcTest..ok!\n");\r
148         else\r
149                 kfile_printf(&ser_fd.fd, "ProcTest..FAIL!\n");\r
150 \r
151         // Main loop\r
152         for(;;)\r
153         {\r
154                 kfile_printf(&ser_fd.fd, "From serial 0: %s\r\n", msg);\r
155         }\r
156         return 0;\r
157 }\r