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