LPC2: add LPC2378 example project.
[bertos.git] / examples / at91sam7 / at91sam7.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 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * \brief Simple BeRTOS test on AT91SAM7X-EK evaluation board.
39  *
40  * This short program shows you a simple demo of some BeRTOS feature:
41  *
42  * - Debug system
43  * - Timer interrupt
44  * - Serial
45  * - Cooperative BeRTOS Kernel
46  *
47  */
48
49 #include "cfg/cfg_ser.h"
50 #include <cfg/macros.h>
51
52 #include <kern/proc.h>
53 #include <kern/signal.h>
54
55 #include <cpu/detect.h>
56
57 #include <drv/timer.h>
58 #include <drv/sysirq_at91.h>
59 #include <drv/ser.h>
60
61 #include <io/arm.h>
62
63 Timer leds_timer;
64 Serial ser_fd;
65
66 enum
67 {
68         FORWARD,
69         BACKWARD,
70 };
71
72 int direction = FORWARD;
73
74 static void leds_init(void)
75 {
76         #if CPU_ARM_AT91SAM7X256
77                 /* Set PB19..22 connected to PIOB */
78                 PIOB_PER  = 0x780000;
79                 /* Set PB19..22 as output */
80                 PIOB_OER  = 0x780000;
81
82                 /* Set PB19..22 to 1 to turn off leds */
83                 PIOB_SODR  = 0x780000;
84
85                 /* turn first led on (PB19) */
86                 PIOB_CODR  = 0x80000;
87         #elif CPU_ARM_AT91SAM7S256
88                 /* Set PA0..3 connected to PIOA */
89                 PIOA_PER  = 0x0000001f;
90                 /* Set PA0..3 as output */
91                 PIOA_OER  = 0x0000001f;
92
93                 /* Set PA0..3 to 1 to turn off leds */
94                 PIOA_SODR  = 0x0000000f;
95                 /* turn first led on (PA0) */
96                 PIOA_CODR  = 0x00000001;
97         #endif
98 }
99
100 #if CPU_ARM_AT91SAM7X256
101         #define GET_PIO_STATUS()  (~PIOB_ODSR & 0x780000)
102         #define LAST_LED                        0x200000
103         #define FIRST_LED                       0x100000
104         #define SET_PIO_BITS                    PIOB_SODR
105         #define CLEAR_PIO_BITS                  PIOB_CODR
106         #define AT91SAM7_MSG      "BeRTOS is running on AT91SAM7X256..\n"
107 #elif CPU_ARM_AT91SAM7S256
108         #define GET_PIO_STATUS()  (~PIOA_ODSR & 0x0000000f)
109         #define LAST_LED                        0x00000004
110         #define FIRST_LED                       0x00000002
111         #define SET_PIO_BITS                    PIOA_SODR
112         #define CLEAR_PIO_BITS                  PIOA_CODR
113         #define AT91SAM7_MSG      "BeRTOS is running on AT91SAM7S256..\n"
114 #endif
115
116 /*
117  * Knight Rider leds effect..
118  */
119 static void leds_toggle(void)
120 {
121         uint32_t led_status = GET_PIO_STATUS();
122
123         // Turn on led in forward direction
124         if (direction == FORWARD)
125         {
126                 if(led_status == LAST_LED)
127                         direction = BACKWARD;
128
129                 SET_PIO_BITS = led_status;
130                 CLEAR_PIO_BITS = led_status << 1;
131         }
132         // Turn on led in backward direction
133         else if (direction == BACKWARD)
134         {
135                 if(led_status == FIRST_LED)
136                         direction = FORWARD;
137
138                 SET_PIO_BITS = led_status;
139                 CLEAR_PIO_BITS = led_status >> 1;
140         }
141
142         /* Wait for interval time */
143         timer_setDelay(&leds_timer, ms_to_ticks(100));
144         timer_add(&leds_timer);
145 }
146
147 int main(void)
148 {       char msg[]="BeRTOS, be fast be beatiful be realtime";
149
150
151         kdbg_init();
152         timer_init();
153         proc_init();
154         leds_init();
155
156         ASSERT(!IRQ_ENABLED());
157
158         /* Open the main communication port */
159         ser_init(&ser_fd, 0);
160         ser_setbaudrate(&ser_fd, 115200);
161         ser_setparity(&ser_fd, SER_PARITY_NONE);
162
163         IRQ_ENABLE;
164         ASSERT(IRQ_ENABLED());
165
166         /*
167          * Register timer and arm timer interupt.
168          */
169         timer_setSoftint(&leds_timer, (Hook)leds_toggle, 0);
170         timer_setDelay(&leds_timer, ms_to_ticks(100));
171         timer_add(&leds_timer);
172
173         /*
174          * Run process test.
175          */
176         if(!proc_testRun())
177                 kfile_printf(&ser_fd.fd, "ProcTest..ok!\n");
178         else
179                 kfile_printf(&ser_fd.fd, "ProcTest..FAIL!\n");
180         /*
181          * Run signal test.
182          */
183         if(!signal_testRun())
184                 kfile_printf(&ser_fd.fd, "SignalTest..ok!\n");
185         else
186                 kfile_printf(&ser_fd.fd, "SignalTest..FAIL!\n");
187
188         kputs(AT91SAM7_MSG);
189
190         // Main loop
191         for(;;)
192         {
193                 kfile_printf(&ser_fd.fd, "From serial 0: %s\r\n", msg);
194         }
195         return 0;
196 }