Move sam7x makefile to sam7 proj dir.
[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 <cpu/detect.h>\r
55 \r
56 #include <drv/timer.h>\r
57 #include <drv/sysirq_at91.h>\r
58 #include <drv/ser.h>\r
59 \r
60 #include <io/arm.h>\r
61 \r
62 Timer leds_timer;\r
63 Serial ser_fd;\r
64 \r
65 enum\r
66 {\r
67         FORWARD,\r
68         BACKWARD,\r
69 };\r
70 \r
71 int direction = FORWARD;\r
72 \r
73 static void leds_init(void)\r
74 {\r
75         #if CPU_ARM_AT91SAM7X256\r
76                 /* Set PB19..22 connected to PIOB */\r
77                 PIOB_PER  = 0x780000;\r
78                 /* Set PB19..22 as output */\r
79                 PIOB_OER  = 0x780000;\r
80 \r
81                 /* Set PB19..22 to 1 to turn off leds */\r
82                 PIOB_SODR  = 0x780000;\r
83 \r
84                 /* turn first led on (PB19) */\r
85                 PIOB_CODR  = 0x80000;\r
86         #elif CPU_ARM_AT91SAM7S256\r
87                 /* Set PA0..3 connected to PIOA */\r
88                 PIOA_PER  = 0x0000001f;\r
89                 /* Set PA0..3 as output */\r
90                 PIOA_OER  = 0x0000001f;\r
91 \r
92                 /* Set PA0..3 to 1 to turn off leds */\r
93                 PIOA_SODR  = 0x0000000f;\r
94                 /* turn first led on (PA0) */\r
95                 PIOA_CODR  = 0x00000001;\r
96         #endif\r
97 }\r
98 \r
99 #if CPU_ARM_AT91SAM7X256\r
100         #define GET_PIO_STATUS()  (~PIOB_ODSR & 0x780000)\r
101         #define LAST_LED                        0x200000\r
102         #define FIRST_LED                       0x100000\r
103         #define SET_PIO_BITS                    PIOB_SODR\r
104         #define CLEAR_PIO_BITS                  PIOB_CODR\r
105         #define AT91SAM7_MSG      "BeRTOS is run on AT91SAM7X256..\n"\r
106 #elif CPU_ARM_AT91SAM7S256\r
107         #define GET_PIO_STATUS()  (~PIOA_ODSR & 0x0000000f)\r
108         #define LAST_LED                        0x00000004\r
109         #define FIRST_LED                       0x00000002\r
110         #define SET_PIO_BITS                    PIOA_SODR\r
111         #define CLEAR_PIO_BITS                  PIOA_CODR\r
112         #define AT91SAM7_MSG      "BeRTOS is run on AT91SAM7S256..\n"\r
113 #endif\r
114 \r
115 /*\r
116  * Knight Rider leds effect..\r
117  */\r
118 static void leds_toggle(void)\r
119 {\r
120         uint32_t led_status = GET_PIO_STATUS();\r
121 \r
122         // Turn on led in forward direction\r
123         if (direction == FORWARD)\r
124         {\r
125                 if(led_status == LAST_LED)\r
126                         direction = BACKWARD;\r
127 \r
128                 SET_PIO_BITS = led_status;\r
129                 CLEAR_PIO_BITS = led_status << 1;\r
130         }\r
131         // Turn on led in backward direction\r
132         else if (direction == BACKWARD)\r
133         {\r
134                 if(led_status == FIRST_LED)\r
135                         direction = FORWARD;\r
136 \r
137                 SET_PIO_BITS = led_status;\r
138                 CLEAR_PIO_BITS = led_status >> 1;\r
139         }\r
140 \r
141         /* Wait for interval time */\r
142         timer_setDelay(&leds_timer, ms_to_ticks(100));\r
143         timer_add(&leds_timer);\r
144 }\r
145 \r
146 int main(void)\r
147 {       char msg[]="BeRTOS, be fast be beatiful be realtime";\r
148 \r
149 \r
150         kdbg_init();\r
151         timer_init();\r
152         proc_init();\r
153         leds_init();\r
154 \r
155         ASSERT(!IRQ_ENABLED());\r
156 \r
157         /* Open the main communication port */\r
158         ser_init(&ser_fd, 0);\r
159         ser_setbaudrate(&ser_fd, 115200);\r
160         ser_setparity(&ser_fd, SER_PARITY_NONE);\r
161 \r
162         IRQ_ENABLE;\r
163         ASSERT(IRQ_ENABLED());\r
164 \r
165         /*\r
166          * Register timer and arm timer interupt.\r
167          */\r
168         timer_setSoftint(&leds_timer, (Hook)leds_toggle, 0);\r
169         timer_setDelay(&leds_timer, ms_to_ticks(100));\r
170         timer_add(&leds_timer);\r
171 \r
172         /*\r
173          * Run process test.\r
174          */\r
175         if(!proc_testRun())\r
176                 kfile_printf(&ser_fd.fd, "ProcTest..ok!\n");\r
177         else\r
178                 kfile_printf(&ser_fd.fd, "ProcTest..FAIL!\n");\r
179 \r
180 \r
181         kputs(AT91SAM7_MSG);\r
182 \r
183         // Main loop\r
184         for(;;)\r
185         {\r
186                 kfile_printf(&ser_fd.fd, "From serial 0: %s\r\n", msg);\r
187         }\r
188         return 0;\r
189 }\r