2 * Copyright Droids Corporation
3 * Olivier Matz <zer0@droids-corp.org>
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.
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.
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
19 * Revision : $Id: main.c,v 1.10 2009-11-08 17:24:33 zer0 Exp $
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
30 #include <aversive/eeprom.h>
36 #include <encoders_spi.h>
39 #include <scheduler.h>
40 #include <clock_time.h>
45 #include <control_system_manager.h>
46 #include <trajectory_manager.h>
47 #include <vect_base.h>
50 #include <obstacle_avoidance.h>
51 #include <blocking_detection_manager.h>
52 #include <robot_system.h>
53 #include <position_manager.h>
58 #include "../common/eeprom_mapping.h"
59 #include "../common/i2c_commands.h"
63 #include "ax12_user.h"
69 #include "strat_base.h"
70 #include "i2c_protocol.h"
72 /* 0 means "programmed"
73 * ---- with 16 Mhz quartz
78 * BOOTZ 1-0 : 01 (4K bootloader)
79 * BOOTRST : 0 (reset on bootloader)
85 struct mainboard mainboard;
86 volatile struct cobboard cobboard;
87 volatile struct ballboard ballboard;
90 /***********************/
94 #define BOOTLOADER_ADDR 0x3f000
95 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
96 printf_P(PSTR("Bootloader is not present\r\n"));
101 /* ... very specific :( */
119 __asm__ __volatile__ ("ldi r31,0xf8\n");
120 __asm__ __volatile__ ("ldi r30,0x00\n");
121 __asm__ __volatile__ ("eijmp\n");
126 void do_time_monitor(void *dummy)
129 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
131 eeprom_write_word(EEPROM_TIME_ADDRESS, seconds);
134 void do_led_blink(void *dummy)
136 static uint8_t a = 0;
138 if (mainboard.flags & DO_ERRBLOCKING) {
153 static void main_timer_interrupt(void)
155 static uint8_t cpt = 0;
158 if ((cpt & 0x3) == 0)
159 scheduler_interrupt();
170 /* CPLD reset on PG3 */
172 PORTG &= ~(1<<3); /* implicit */
183 memset(&gen, 0, sizeof(gen));
184 memset(&mainboard, 0, sizeof(mainboard));
185 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
186 DO_POS | DO_POWER | DO_BD | DO_ERRBLOCKING;
190 uart_register_rx_event(CMDLINE_UART, emergency);
192 #if CMDLINE_UART == 3
193 fdevopen(uart3_dev_send, uart3_dev_recv);
194 #elif CMDLINE_UART == 1
195 fdevopen(uart1_dev_send, uart1_dev_recv);
198 //eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD);
199 /* check eeprom to avoid to run the bad program */
200 if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
201 EEPROM_MAGIC_MAINBOARD) {
203 printf_P(PSTR("Bad eeprom value\r\n"));
206 #endif /* ! HOST_VERSION */
209 error_register_emerg(mylog);
210 error_register_error(mylog);
211 error_register_warning(mylog);
212 error_register_notice(mylog);
213 error_register_debug(mylog);
217 encoders_spi_init(); /* this will also init spi hardware */
220 i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
222 i2c_register_recv_event(i2c_recvevent);
223 i2c_register_send_event(i2c_sendevent);
227 timer0_register_OV_intr(main_timer_interrupt);
230 PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
231 TIMER1_PRESCALER_DIV_1);
232 PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
233 TIMER4_PRESCALER_DIV_1);
235 PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
237 PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED |
238 PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5);
239 PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
241 PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
246 PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
247 TIMER1_PRESCALER_DIV_256);
248 PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
250 PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
251 TIMER1_PRESCALER_DIV_256);
252 PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
254 PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
256 PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
258 support_balls_deploy(); /* init pwm for servos */
259 #endif /* !HOST_VERSION */
269 scheduler_add_periodical_event_priority(do_led_blink, NULL,
270 100000L / SCHEDULER_UNIT,
272 #endif /* !HOST_VERSION */
274 /* all cs management */
278 time_init(TIME_PRIO);
281 /* sensors, will also init hardware adc */
284 /* start i2c slave polling */
285 scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL,
286 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO);
287 #endif /* !HOST_VERSION */
290 gen.logs[0] = E_USER_STRAT;
294 /* strat-related event */
295 scheduler_add_periodical_event_priority(strat_event, NULL,
296 10000L / SCHEDULER_UNIT,
300 /* eeprom time monitor */
301 scheduler_add_periodical_event_priority(do_time_monitor, NULL,
302 1000000L / SCHEDULER_UNIT,
304 #endif /* !HOST_VERSION */
308 printf_P(PSTR("\r\n"));
309 printf_P(PSTR("Respect et robustesse.\r\n"));
313 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
314 printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60);
319 strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90));