beacon from 2009
[aversive.git] / projects / microb2010 / ballboard / 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.4 2009-05-27 20:04:07 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <avr/eeprom.h>
26
27 #include <aversive.h>
28 #include <aversive/pgmspace.h>
29 #include <aversive/wait.h>
30 #include <aversive/error.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 <blocking_detection_manager.h>
47
48 #include <parse.h>
49 #include <rdline.h>
50
51 #include "../common/eeprom_mapping.h"
52 #include "../common/i2c_commands.h"
53
54 #include "main.h"
55 #include "ax12_user.h"
56 #include "cmdline.h"
57 #include "sensor.h"
58 #include "actuator.h"
59 #include "cs.h"
60 #include "i2c_protocol.h"
61 #include "state.h"
62 #include "beacon.h"
63
64 /* 0 means "programmed"
65  * ---- with 16 Mhz quartz
66  * CKSEL 3-0 : 0111
67  * SUT 1-0 : 10
68  * CKDIV8 : 1
69  * ---- bootloader
70  * BOOTZ 1-0 : 01 (4K bootloader)
71  * BOOTRST : 0 (reset on bootloader)
72  * ---- jtag
73  * jtagen : 0
74  */
75
76 struct genboard gen;
77 struct ballboard ballboard;
78
79 /***********************/
80
81 void bootloader(void)
82 {
83 #define BOOTLOADER_ADDR 0x3f000
84         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
85                 printf_P(PSTR("Bootloader is not present\r\n"));
86                 return;
87         }
88         cli();
89         BRAKE_ON();
90         /* ... very specific :( */
91         TIMSK0 = 0;
92         TIMSK1 = 0;
93         TIMSK2 = 0;
94         TIMSK3 = 0;
95         TIMSK4 = 0;
96         TIMSK5 = 0;
97         EIMSK = 0;
98         UCSR0B = 0;
99         UCSR1B = 0;
100         UCSR2B = 0;
101         UCSR3B = 0;
102         SPCR = 0;
103         TWCR = 0;
104         ACSR = 0;
105         ADCSRA = 0;
106
107         EIND = 1;
108         __asm__ __volatile__ ("ldi r31,0xf8\n");
109         __asm__ __volatile__ ("ldi r30,0x00\n");
110         __asm__ __volatile__ ("eijmp\n");
111
112         /* never returns */
113 }
114
115 void do_led_blink(__attribute__((unused)) void *dummy)
116 {
117         static uint8_t a = 0;
118
119         if (ballboard.flags & DO_ERRBLOCKING) {
120                 if (a & 1)
121                         LED1_ON();
122                 else
123                         LED1_OFF();
124         }
125         else {
126                 if (a & 4)
127                         LED1_ON();
128                 else
129                         LED1_OFF();
130         }
131         a++;
132 }
133
134 static void main_timer_interrupt(void)
135 {
136         static uint8_t cpt = 0;
137         cpt++;
138         sei();
139         if ((cpt & 0x3) == 0)
140                 scheduler_interrupt();
141 }
142
143 int main(void)
144 {
145         /* brake */
146         BRAKE_OFF();
147         BRAKE_DDR();
148
149         /* CPLD reset on PG3 */
150         DDRG |= 1<<3;
151         PORTG &= ~(1<<3); /* implicit */
152
153         /* LEDS */
154         DDRJ |= 0x0c;
155         DDRL = 0xc0;
156         LED1_OFF();
157         memset(&gen, 0, sizeof(gen));
158         memset(&ballboard, 0, sizeof(ballboard));
159         ballboard.flags = DO_ENCODERS | DO_CS | DO_POWER |
160                 DO_ERRBLOCKING | DO_BD;
161
162         /* UART */
163         uart_init();
164 #if CMDLINE_UART == 3
165         fdevopen(uart3_dev_send, uart3_dev_recv);
166         uart_register_rx_event(3, emergency);
167 #elif CMDLINE_UART == 1
168         fdevopen(uart1_dev_send, uart1_dev_recv);
169         uart_register_rx_event(1, emergency);
170 #else
171 #  error not supported
172 #endif
173
174         /* check eeprom to avoid to run the bad program */
175         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
176             EEPROM_MAGIC_BALLBOARD) {
177                 int c;
178                 sei();
179                 printf_P(PSTR("Bad eeprom value ('f' to force)\r\n"));
180                 c = uart_recv(CMDLINE_UART);
181                 if (c == 'f')
182                         eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_BALLBOARD);
183                 wait_ms(100);
184                 bootloader();
185         }
186
187         /* LOGS */
188         error_register_emerg(mylog);
189         error_register_error(mylog);
190         error_register_warning(mylog);
191         error_register_notice(mylog);
192         error_register_debug(mylog);
193
194         /* SPI + ENCODERS */
195         encoders_spi_init(); /* this will also init spi hardware */
196
197         /* I2C */
198         i2c_protocol_init();
199         i2c_init(I2C_MODE_SLAVE, I2C_BALLBOARD_ADDR);
200         i2c_register_recv_event(i2c_recvevent);
201
202         /* TIMER */
203         timer_init();
204         timer0_register_OV_intr(main_timer_interrupt);
205
206         /* PWM */
207         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
208                                  TIMER1_PRESCALER_DIV_1);
209         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
210                                  TIMER4_PRESCALER_DIV_1);
211
212         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
213                       &PORTD, 4);
214         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED,
215                       &PORTD, 5);
216         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED |
217                       PWM_NG_MODE_SIGN_INVERTED, &PORTD, 6);
218         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED |
219                       PWM_NG_MODE_SIGN_INVERTED,
220                       &PORTD, 7);
221
222
223         /* servos */
224         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
225                                  TIMER1_PRESCALER_DIV_256);
226         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
227                       NULL, 0);
228         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
229                                  TIMER1_PRESCALER_DIV_256);
230         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
231                       NULL, 0);
232         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
233                       NULL, 0);
234         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
235                       NULL, 0);
236
237         /* SCHEDULER */
238         scheduler_init();
239
240         scheduler_add_periodical_event_priority(do_led_blink, NULL,
241                                                 100000L / SCHEDULER_UNIT,
242                                                 LED_PRIO);
243         /* all cs management */
244         microb_cs_init();
245
246         /* sensors, will also init hardware adc */
247         sensor_init();
248
249         /* beacon */
250         beacon_init();
251         scheduler_add_periodical_event_priority(beacon_calc, NULL, 
252                                                 20000L / SCHEDULER_UNIT, 
253                                                 BEACON_PRIO);
254         /* TIME */
255         time_init(TIME_PRIO);
256
257         /* ax12 */
258         ax12_user_init();
259
260         gen.logs[0] = E_USER_ST_MACH;
261         gen.log_level = 5;
262
263         sei();
264
265         actuator_init();
266         beacon_calibre_pos();
267
268         printf_P(PSTR("\r\n"));
269         printf_P(PSTR("Dass das Gluck deinen Haus setzt.\r\n"));
270
271         state_machine();
272         cmdline_interact();
273
274         return 0;
275 }