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.4 2009-05-27 20:04:07 zer0 Exp $
25 #include <avr/eeprom.h>
28 #include <aversive/pgmspace.h>
29 #include <aversive/wait.h>
30 #include <aversive/error.h>
36 #include <encoders_spi.h>
39 #include <scheduler.h>
45 #include <control_system_manager.h>
46 #include <blocking_detection_manager.h>
51 #include "../common/eeprom_mapping.h"
52 #include "../common/i2c_commands.h"
55 #include "ax12_user.h"
60 #include "i2c_protocol.h"
64 /* 0 means "programmed"
65 * ---- with 16 Mhz quartz
70 * BOOTZ 1-0 : 01 (4K bootloader)
71 * BOOTRST : 0 (reset on bootloader)
77 struct sensorboard sensorboard;
79 /***********************/
83 #define BOOTLOADER_ADDR 0x3f000
84 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
85 printf_P(PSTR("Bootloader is not present\r\n"));
90 /* ... very specific :( */
108 __asm__ __volatile__ ("ldi r31,0xf8\n");
109 __asm__ __volatile__ ("ldi r30,0x00\n");
110 __asm__ __volatile__ ("eijmp\n");
115 void do_led_blink(__attribute__((unused)) void *dummy)
117 #if 1 /* simple blink */
129 static void main_timer_interrupt(void)
131 static uint8_t cpt = 0;
134 if ((cpt & 0x3) == 0)
135 scheduler_interrupt();
144 /* CPLD reset on PG3 */
146 PORTG &= ~(1<<3); /* implicit */
152 memset(&gen, 0, sizeof(gen));
153 memset(&sensorboard, 0, sizeof(sensorboard));
154 sensorboard.flags = DO_ENCODERS | DO_CS | DO_POWER; // DO_BD
158 #if CMDLINE_UART == 3
159 fdevopen(uart3_dev_send, uart3_dev_recv);
160 uart_register_rx_event(3, emergency);
161 #elif CMDLINE_UART == 1
162 fdevopen(uart1_dev_send, uart1_dev_recv);
163 uart_register_rx_event(1, emergency);
165 # error not supported
168 //eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_SENSORBOARD);
169 /* check eeprom to avoid to run the bad program */
170 if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
171 EEPROM_MAGIC_SENSORBOARD) {
173 printf_P(PSTR("Bad eeprom value\r\n"));
178 error_register_emerg(mylog);
179 error_register_error(mylog);
180 error_register_warning(mylog);
181 error_register_notice(mylog);
182 error_register_debug(mylog);
185 encoders_spi_init(); /* this will also init spi hardware */
189 i2c_init(I2C_MODE_SLAVE, I2C_SENSORBOARD_ADDR);
190 i2c_register_recv_event(i2c_recvevent);
194 timer0_register_OV_intr(main_timer_interrupt);
197 PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
198 TIMER1_PRESCALER_DIV_1);
199 PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
200 TIMER4_PRESCALER_DIV_1);
202 PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED |
203 PWM_NG_MODE_SIGN_INVERTED,
205 PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED,
207 PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
209 PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
214 PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
215 TIMER1_PRESCALER_DIV_256);
216 PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
218 PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
219 TIMER1_PRESCALER_DIV_256);
220 PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
222 PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
224 PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
230 scheduler_add_periodical_event_priority(do_led_blink, NULL,
231 100000L / SCHEDULER_UNIT,
233 /* all cs management */
236 /* sensors, will also init hardware adc */
240 time_init(TIME_PRIO);
247 scheduler_add_periodical_event_priority(beacon_calc, NULL,
248 20000L / SCHEDULER_UNIT,
257 scheduler_add_periodical_event_priority(do_scan, NULL,
258 (1024L*1L) / SCHEDULER_UNIT,
263 scan_params.speed = SCAN_DEFAULT_SPEED;
264 scan_params.debug = 0;
266 scanner_calibre_mirror();
267 scanner_calibre_laser();
269 beacon_calibre_pos();
272 printf_P(PSTR("\r\n"));
273 printf_P(PSTR("Dass das Gluck deinen Haus setzt.\r\n"));