add a script for xbee tests
[protos/xbee-avr.git] / main.c
1 /*
2  * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /* fuses:
29  * avrdude -p atmega1284p -P usb -c avrispmkii -U lfuse:w:0xff:m -U hfuse:w:0x91:m -U efuse:w:0xff:m
30  * -> it failed but I answered y, then make reset and it was ok
31  */
32
33 #include <aversive.h>
34 #include <aversive/queue.h>
35 #include <aversive/endian.h>
36 #include <aversive/wait.h>
37 #include <aversive/error.h>
38
39 #include <uart.h>
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdint.h>
44 #include <inttypes.h>
45 #include <stdlib.h>
46 #include <stdarg.h>
47 #include <errno.h>
48 #include <ctype.h>
49
50 #include <parse.h>
51 #include <rdline.h>
52 #include <timer.h>
53 #include <i2c.h>
54
55 #include "../fpv-common/i2c_commands.h"
56 #include "eeprom_config.h"
57 #include "beep.h"
58 #include "xbee_user.h"
59 #include "i2c_protocol.h"
60 #include "main.h"
61
62 struct xbeeboard xbeeboard;
63 volatile uint32_t global_ms;
64
65 /* global xbee device */
66 struct xbee_dev *xbee_dev;
67
68 void bootloader(void)
69 {
70 #define BOOTLOADER_ADDR 0x3f000
71         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
72                 printf_P(PSTR("Bootloader is not present\r\n"));
73                 return;
74         }
75         cli();
76         /* ... very specific :( */
77         TIMSK0 = 0;
78         TIMSK1 = 0;
79         TIMSK2 = 0;
80         TIMSK3 = 0;
81         EIMSK = 0;
82         UCSR0B = 0;
83         UCSR1B = 0;
84         SPCR = 0;
85         TWCR = 0;
86         ACSR = 0;
87         ADCSRA = 0;
88
89         /* XXX */
90         /* __asm__ __volatile__ ("ldi r31,0xf8\n"); */
91         /* __asm__ __volatile__ ("ldi r30,0x00\n"); */
92         /* __asm__ __volatile__ ("eijmp\n"); */
93 }
94
95 /* return time in milliseconds on unsigned 16 bits */
96 uint16_t get_time_ms(void)
97 {
98         uint16_t ms;
99         uint8_t flags;
100         IRQ_LOCK(flags);
101         ms = global_ms;
102         IRQ_UNLOCK(flags);
103         return ms;
104 }
105
106 static void main_timer_interrupt(void)
107 {
108         static uint16_t cycles;
109         static uint8_t cpt = 0;
110         static uint8_t stack = 0;
111
112         cpt++;
113
114         /* LED blink */
115         if (global_ms & 0x80)
116                 LED1_ON();
117         else
118                 LED1_OFF();
119
120         if (cpt & beep_mask)
121                 BUZZER_ON();
122         else
123                 BUZZER_OFF();
124
125         if ((cpt & 0x03) != 0)
126                 return;
127
128         /* the following code is only called one interrupt among 4: every 682us
129          * (at 12 Mhz) = 8192 cycles */
130         cycles += 8192;
131         if (cycles >= 12000) {
132                 cycles -= 12000;
133                 global_ms ++;
134         }
135
136         /* called  */
137         if (stack++ == 0)
138                 LED2_ON();
139         sei();
140         if ((cpt & 0x3) == 0)
141                 callout_manage(&xbeeboard.intr_cm);
142         cli();
143         if (--stack == 0)
144                 LED2_OFF();
145 }
146
147 int main(void)
148 {
149         FILE *xbee_file;
150         int8_t err;
151         struct xbee_dev dev;
152
153         DDRA = 0x07 /* LEDs */ | 0x10 /* buzzer */;
154
155         uart_init();
156         uart_register_rx_event(CMDLINE_UART, emergency);
157
158         fdevopen(cmdline_dev_send, cmdline_dev_recv);
159         xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv);
160         timer_init();
161         timer0_register_OV_intr(main_timer_interrupt);
162
163         callout_mgr_init(&xbeeboard.intr_cm, get_time_ms);
164
165         cmdline_init();
166         /* LOGS */
167         error_register_emerg(mylog);
168         error_register_error(mylog);
169         error_register_warning(mylog);
170         error_register_notice(mylog);
171         error_register_debug(mylog);
172
173         /* I2C */
174         i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
175         i2c_protocol_init();
176         i2c_register_recv_event(i2c_recvevent);
177         i2c_register_send_event(i2c_sendevent);
178
179         spi_servo_init();
180         beep_init();
181
182         /* initialize libxbee */
183         err = xbee_init();
184         if (err < 0)
185                 return -1;
186
187         xbee_dev = &dev;
188
189         /* open xbee device */
190         if (xbee_open(xbee_dev, xbee_file) < 0)
191                 return -1;
192
193         /* register default channel with a callback */
194         if (xbee_register_channel(xbee_dev, XBEE_DEFAULT_CHANNEL,
195                                   xbeeapp_rx, NULL) < 0) {
196                 fprintf(stderr, "cannot register default channel\n");
197                 return -1;
198         }
199
200         xbeeapp_init();
201
202         rc_proto_init();
203
204         sei();
205
206         eeprom_load_config();
207
208         printf_P(PSTR("\r\n"));
209         rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
210
211         xbee_mainloop();
212         return 0;
213 }