introduce sd_log for logging on the sdcard
[protos/imu.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 <i2cm_sw.h>
54
55 #include "eeprom_config.h"
56 #include "gps_venus.h"
57 #include "sd_log.h"
58 #include "main.h"
59
60 struct imuboard imuboard;
61 volatile uint32_t global_ms;
62
63 /* global xbee device */
64 struct xbee_dev *xbee_dev;
65
66 void bootloader(void)
67 {
68 #define BOOTLOADER_ADDR 0x3f000
69         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
70                 printf_P(PSTR("Bootloader is not present\r\n"));
71                 return;
72         }
73         cli();
74         /* ... very specific :( */
75         TIMSK0 = 0;
76         TIMSK1 = 0;
77         TIMSK2 = 0;
78         TIMSK3 = 0;
79         EIMSK = 0;
80         UCSR0B = 0;
81         UCSR1B = 0;
82         SPCR = 0;
83         TWCR = 0;
84         ACSR = 0;
85         ADCSRA = 0;
86
87         /* XXX */
88         /* __asm__ __volatile__ ("ldi r31,0xf8\n"); */
89         /* __asm__ __volatile__ ("ldi r30,0x00\n"); */
90         /* __asm__ __volatile__ ("eijmp\n"); */
91 }
92
93 /* return time in milliseconds on unsigned 16 bits */
94 uint16_t get_time_ms(void)
95 {
96         uint16_t ms;
97         uint8_t flags;
98         IRQ_LOCK(flags);
99         ms = global_ms;
100         IRQ_UNLOCK(flags);
101         return ms;
102 }
103
104 static void main_timer_interrupt(void)
105 {
106         static uint16_t cycles;
107         static uint8_t stack = 0;
108         static uint8_t cpt = 0;
109         cpt++;
110
111         /* LED blink */
112         if (global_ms & 0x80)
113                 LED1_ON();
114         else
115                 LED1_OFF();
116
117         if ((cpt & 0x03) != 0)
118                 return;
119
120         /* the following code is only called one interrupt among 4: every 682us
121          * (at 12 Mhz) = 8192 cycles */
122         cycles += 8192;
123         if (cycles >= 12000) {
124                 cycles -= 12000;
125                 global_ms ++;
126         }
127
128         /* called  */
129         if (stack++ == 0)
130                 LED2_ON();
131         sei();
132         if ((cpt & 0x3) == 0)
133                 callout_manage(&imuboard.intr_cm);
134         cli();
135         if (--stack == 0)
136                 LED2_OFF();
137 }
138
139 /* XXX */
140 int imu_loop(void);
141 int sd_main(void);
142
143 int main(void)
144 {
145         DDRB = 0x18 /* LEDs */;
146
147         uart_init();
148         uart_register_rx_event(CMDLINE_UART, emergency);
149
150         fdevopen(cmdline_dev_send, cmdline_dev_recv);
151         timer_init();
152         timer0_register_OV_intr(main_timer_interrupt);
153
154         callout_mgr_init(&imuboard.intr_cm, get_time_ms);
155
156         cmdline_init();
157         /* LOGS */
158         error_register_emerg(mylog);
159         error_register_error(mylog);
160         error_register_warning(mylog);
161         error_register_notice(mylog);
162         error_register_debug(mylog);
163
164         /* communication with mpu6050 */
165         i2cm_init();
166
167         sei();
168
169         eeprom_load_config();
170
171         open_log_file();
172
173         printf_P(PSTR("\r\n"));
174         rdline_newline(&imuboard.rdl, imuboard.prompt);
175
176         //sd_main();
177         //imu_init();
178         //imu_loop();
179         gps_venus_init();
180         gps_loop();
181
182         while (1) {
183                 cmdline_poll();
184         }
185
186         return 0;
187 }