14a2aad68ef5b18d140a0fb932e27dc6070c952a
[aversive.git] / main.c
1 /*
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: main.c,v 1.10 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
30 #include <aversive/eeprom.h>
31
32 #include <ax12.h>
33 #include <uart.h>
34 #include <spi.h>
35 #include <i2c.h>
36 #include <encoders_spi.h>
37 #include <pwm_ng.h>
38 #include <timer.h>
39 #include <scheduler.h>
40 #include <clock_time.h>
41 #include <adc.h>
42
43 #include <pid.h>
44 #include <quadramp.h>
45 #include <control_system_manager.h>
46 #include <trajectory_manager.h>
47 #include <vect_base.h>
48 #include <lines.h>
49 #include <polygon.h>
50 #include <obstacle_avoidance.h>
51 #include <blocking_detection_manager.h>
52 #include <robot_system.h>
53 #include <position_manager.h>
54
55 #include <parse.h>
56 #include <rdline.h>
57
58 #include "../common/eeprom_mapping.h"
59 #include "../common/i2c_commands.h"
60
61 #include "main.h"
62 #include "robotsim.h"
63 #include "ax12_user.h"
64 #include "strat.h"
65 #include "cmdline.h"
66 #include "sensor.h"
67 #include "actuator.h"
68 #include "cs.h"
69 #include "strat_base.h"
70 #include "strat_db.h"
71 #include "i2c_protocol.h"
72
73 /* 0 means "programmed"
74  * ---- with 16 Mhz quartz
75  * CKSEL 3-0 : 0111
76  * SUT 1-0 : 10
77  * CKDIV8 : 1
78  * ---- bootloader
79  * BOOTZ 1-0 : 01 (4K bootloader)
80  * BOOTRST : 0 (reset on bootloader)
81  * ---- jtag
82  * jtagen : 0
83  */
84
85 struct genboard gen;
86 struct mainboard mainboard;
87 volatile struct cobboard cobboard;
88 volatile struct ballboard ballboard;
89
90 #ifndef HOST_VERSION
91 /***********************/
92
93 void bootloader(void)
94 {
95 #define BOOTLOADER_ADDR 0x3f000
96         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
97                 printf_P(PSTR("Bootloader is not present\r\n"));
98                 return;
99         }
100         cli();
101         BRAKE_ON();
102         /* ... very specific :( */
103         TIMSK0 = 0;
104         TIMSK1 = 0;
105         TIMSK2 = 0;
106         TIMSK3 = 0;
107         TIMSK4 = 0;
108         TIMSK5 = 0;
109         EIMSK = 0;
110         UCSR0B = 0;
111         UCSR1B = 0;
112         UCSR2B = 0;
113         UCSR3B = 0;
114         SPCR = 0;
115         TWCR = 0;
116         ACSR = 0;
117         ADCSRA = 0;
118
119         EIND = 1;
120         __asm__ __volatile__ ("ldi r31,0xf8\n");
121         __asm__ __volatile__ ("ldi r30,0x00\n");
122         __asm__ __volatile__ ("eijmp\n");
123
124         /* never returns */
125 }
126
127 void do_time_monitor(void *dummy)
128 {
129         uint16_t seconds;
130         seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
131         seconds ++;
132         eeprom_write_word(EEPROM_TIME_ADDRESS, seconds);
133 }
134
135 void do_led_blink(void *dummy)
136 {
137         static uint8_t a = 0;
138
139         if (mainboard.flags & DO_ERRBLOCKING) {
140                 if (a & 1)
141                         LED1_ON();
142                 else
143                         LED1_OFF();
144         }
145         else {
146                 if (a & 4)
147                         LED1_ON();
148                 else
149                         LED1_OFF();
150         }
151         a++;
152 }
153
154 static void main_timer_interrupt(void)
155 {
156         static uint8_t cpt = 0;
157         cpt++;
158         sei();
159         if ((cpt & 0x3) == 0)
160                 scheduler_interrupt();
161 }
162 #endif
163
164 int main(void)
165 {
166 #ifndef HOST_VERSION
167         /* brake */
168         BRAKE_DDR();
169         BRAKE_OFF();
170
171         /* CPLD reset on PG3 */
172         DDRG |= 1<<3;
173         PORTG &= ~(1<<3); /* implicit */
174
175         /* LEDS */
176         DDRJ |= 0x0c;
177         DDRL = 0xc0;
178         LED1_OFF();
179         LED2_OFF();
180         LED3_OFF();
181         LED4_OFF();
182 #endif
183
184         memset(&gen, 0, sizeof(gen));
185         memset(&mainboard, 0, sizeof(mainboard));
186         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
187                 DO_POS | DO_POWER | DO_BD | DO_ERRBLOCKING;
188
189         /* UART */
190         uart_init();
191         uart_register_rx_event(CMDLINE_UART, emergency);
192 #ifndef HOST_VERSION
193 #if CMDLINE_UART == 3
194         fdevopen(uart3_dev_send, uart3_dev_recv);
195 #elif CMDLINE_UART == 1
196         fdevopen(uart1_dev_send, uart1_dev_recv);
197 #endif
198
199         //eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD);
200         /* check eeprom to avoid to run the bad program */
201         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
202             EEPROM_MAGIC_MAINBOARD) {
203                 sei();
204                 printf_P(PSTR("Bad eeprom value\r\n"));
205                 while(1);
206         }
207 #endif /* ! HOST_VERSION */
208
209         /* LOGS */
210         error_register_emerg(mylog);
211         error_register_error(mylog);
212         error_register_warning(mylog);
213         error_register_notice(mylog);
214         error_register_debug(mylog);
215
216 #ifndef HOST_VERSION
217         /* SPI + ENCODERS */
218         encoders_spi_init(); /* this will also init spi hardware */
219
220         /* I2C */
221         i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
222         i2c_protocol_init();
223         i2c_register_recv_event(i2c_recvevent);
224         i2c_register_send_event(i2c_sendevent);
225
226         /* TIMER */
227         timer_init();
228         timer0_register_OV_intr(main_timer_interrupt);
229
230         /* PWM */
231         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
232                                  TIMER1_PRESCALER_DIV_1);
233         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
234                                  TIMER4_PRESCALER_DIV_1);
235
236         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
237                       &PORTD, 4);
238         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED |
239                       PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5);
240         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
241                       &PORTD, 6);
242         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
243                       &PORTD, 7);
244
245
246         /* servos */
247         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
248                                  TIMER1_PRESCALER_DIV_256);
249         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
250                       NULL, 0);
251         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
252                                  TIMER1_PRESCALER_DIV_256);
253         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
254                       NULL, 0);
255         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
256                       NULL, 0);
257         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
258                       NULL, 0);
259         support_balls_deploy(); /* init pwm for servos */
260 #endif /* !HOST_VERSION */
261
262         /* SCHEDULER */
263         scheduler_init();
264 #ifdef HOST_VERSION
265         hostsim_init();
266         robotsim_init();
267 #endif
268
269 #ifndef HOST_VERSION
270         scheduler_add_periodical_event_priority(do_led_blink, NULL,
271                                                 100000L / SCHEDULER_UNIT,
272                                                 LED_PRIO);
273 #endif /* !HOST_VERSION */
274
275         /* all cs management */
276         microb_cs_init();
277
278         /* TIME */
279         time_init(TIME_PRIO);
280
281 #ifndef HOST_VERSION
282         /* sensors, will also init hardware adc */
283         sensor_init();
284
285         /* start i2c slave polling */
286         scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL,
287                                                 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO);
288 #endif /* !HOST_VERSION */
289
290         /* strat */
291         gen.logs[0] = E_USER_STRAT;
292         gen.log_level = 5;
293         strat_db_init();
294
295         /* strat-related event */
296         scheduler_add_periodical_event_priority(strat_event, NULL,
297                                                 10000L / SCHEDULER_UNIT,
298                                                 STRAT_PRIO);
299
300 #ifndef HOST_VERSION
301         /* eeprom time monitor */
302         scheduler_add_periodical_event_priority(do_time_monitor, NULL,
303                                                 1000000L / SCHEDULER_UNIT,
304                                                 EEPROM_TIME_PRIO);
305 #endif /* !HOST_VERSION */
306
307         sei();
308
309         printf_P(PSTR("\r\n"));
310         printf_P(PSTR("Respect et robustesse.\r\n"));
311 #ifndef HOST_VERSION
312         {
313                 uint16_t seconds;
314                 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
315                 printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60);
316         }
317 #endif
318
319 #ifdef HOST_VERSION
320         strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90));
321 #endif
322
323         cmdline_interact();
324
325         return 0;
326 }