6a36e805134fbdee451a6c1c499feb679fd16fa3
[aversive.git] / projects / microb2010 / mainboard / 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 "ax12_user.h"
63 #include "strat.h"
64 #include "cmdline.h"
65 #include "sensor.h"
66 #include "actuator.h"
67 #include "cs.h"
68 #include "i2c_protocol.h"
69
70 /* 0 means "programmed"
71  * ---- with 16 Mhz quartz
72  * CKSEL 3-0 : 0111
73  * SUT 1-0 : 10 
74  * CKDIV8 : 1
75  * ---- bootloader
76  * BOOTZ 1-0 : 01 (4K bootloader)
77  * BOOTRST : 0 (reset on bootloader)
78  * ---- jtag
79  * jtagen : 0
80  */
81
82 struct genboard gen;
83 struct mainboard mainboard;
84 struct cobboard cobboard;
85 struct ballboard ballboard;
86
87 /***********************/
88
89 void bootloader(void)
90 {
91 #define BOOTLOADER_ADDR 0x3f000
92         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
93                 printf_P(PSTR("Bootloader is not present\r\n"));
94                 return;
95         }
96         cli();
97         BRAKE_ON();
98         /* ... very specific :( */
99         TIMSK0 = 0;
100         TIMSK1 = 0;
101         TIMSK2 = 0;
102         TIMSK3 = 0;
103         TIMSK4 = 0;
104         TIMSK5 = 0;
105         EIMSK = 0;
106         UCSR0B = 0;
107         UCSR1B = 0;
108         UCSR2B = 0;
109         UCSR3B = 0;
110         SPCR = 0;
111         TWCR = 0;
112         ACSR = 0;
113         ADCSRA = 0;
114
115         EIND = 1;
116         __asm__ __volatile__ ("ldi r31,0xf8\n");
117         __asm__ __volatile__ ("ldi r30,0x00\n");
118         __asm__ __volatile__ ("eijmp\n");
119         
120         /* never returns */
121 }
122
123 void do_time_monitor(void *dummy)
124 {
125         uint16_t seconds;
126         seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
127         seconds ++;
128         eeprom_write_word(EEPROM_TIME_ADDRESS, seconds);
129 }
130
131 void do_led_blink(void *dummy)
132 {
133 #if 1 /* simple blink */
134         LED1_TOGGLE();
135 #endif
136 }
137
138 static void main_timer_interrupt(void)
139 {
140         static uint8_t cpt = 0;
141         cpt++;
142         sei();
143         if ((cpt & 0x3) == 0)
144                 scheduler_interrupt();
145 }
146
147 int main(void)
148 {
149         uint16_t seconds;
150
151         /* brake */
152         BRAKE_DDR();
153         BRAKE_OFF();
154
155         /* CPLD reset on PG3 */
156         DDRG |= 1<<3;
157         PORTG &= ~(1<<3); /* implicit */
158
159         /* LEDS */
160         DDRJ |= 0x0c;
161         DDRL = 0xc0;
162         LED1_OFF();
163         LED2_OFF();
164         LED3_OFF();
165         LED4_OFF();
166
167         memset(&gen, 0, sizeof(gen));
168         memset(&mainboard, 0, sizeof(mainboard));
169         mainboard.flags = DO_ENCODERS | DO_RS |
170                 DO_POS | DO_POWER | DO_BD;
171
172         /* UART */
173         uart_init();
174 #if CMDLINE_UART == 3
175         fdevopen(uart3_dev_send, uart3_dev_recv);
176         uart_register_rx_event(3, emergency);
177 #elif CMDLINE_UART == 1
178         fdevopen(uart1_dev_send, uart1_dev_recv);
179         uart_register_rx_event(1, emergency);
180 #else
181 #  error not supported
182 #endif
183
184         eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD);
185         /* check eeprom to avoid to run the bad program */
186         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
187             EEPROM_MAGIC_MAINBOARD) {
188                 sei();
189                 printf_P(PSTR("Bad eeprom value\r\n"));
190                 while(1);
191         }
192
193         /* LOGS */
194         error_register_emerg(mylog);
195         error_register_error(mylog);
196         error_register_warning(mylog);
197         error_register_notice(mylog);
198         error_register_debug(mylog);
199
200         /* SPI + ENCODERS */
201         encoders_spi_init(); /* this will also init spi hardware */
202
203         /* I2C */
204         i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
205         i2c_protocol_init();
206         i2c_register_recv_event(i2c_recvevent);
207         i2c_register_send_event(i2c_sendevent);
208
209         /* TIMER */
210         timer_init();
211         timer0_register_OV_intr(main_timer_interrupt);
212
213         /* PWM */
214         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10, 
215                                  TIMER1_PRESCALER_DIV_1);
216         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10, 
217                                  TIMER4_PRESCALER_DIV_1);
218         
219         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
220                       &PORTD, 4);
221         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED | 
222                       PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5);
223         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
224                       &PORTD, 6);
225         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
226                       &PORTD, 7);
227
228
229         /* servos */
230         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10, 
231                                  TIMER1_PRESCALER_DIV_256);
232         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
233                       NULL, 0);
234         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10, 
235                                  TIMER1_PRESCALER_DIV_256);
236         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
237                       NULL, 0);
238         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
239                       NULL, 0);
240         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
241                       NULL, 0);
242         support_balls_deploy(); /* init pwm for servos */
243
244         /* SCHEDULER */
245         scheduler_init();
246
247         scheduler_add_periodical_event_priority(do_led_blink, NULL, 
248                                                 100000L / SCHEDULER_UNIT, 
249                                                 LED_PRIO);
250         /* all cs management */
251         microb_cs_init();
252
253         /* sensors, will also init hardware adc */
254         sensor_init();
255
256         /* TIME */
257         time_init(TIME_PRIO);
258
259         /* start i2c slave polling */
260         scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL,
261                                                 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO);
262
263         /* strat */
264         gen.logs[0] = E_USER_STRAT;
265         gen.log_level = 5;
266         strat_reset_infos();
267
268         /* strat-related event */
269         scheduler_add_periodical_event_priority(strat_event, NULL,
270                                                 25000L / SCHEDULER_UNIT,
271                                                 STRAT_PRIO);
272
273         /* eeprom time monitor */
274         scheduler_add_periodical_event_priority(do_time_monitor, NULL,
275                                                 1000000L / SCHEDULER_UNIT,
276                                                 EEPROM_TIME_PRIO);
277
278         sei();
279
280         printf_P(PSTR("\r\n"));
281         printf_P(PSTR("Respect et robustesse.\r\n"));
282         seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
283         printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60);
284         cmdline_interact();
285
286         return 0;
287 }