work on trajectory, update cobboard and ballboard too
[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
62 /* 0 means "programmed"
63  * ---- with 16 Mhz quartz
64  * CKSEL 3-0 : 0111
65  * SUT 1-0 : 10
66  * CKDIV8 : 1
67  * ---- bootloader
68  * BOOTZ 1-0 : 01 (4K bootloader)
69  * BOOTRST : 0 (reset on bootloader)
70  * ---- jtag
71  * jtagen : 0
72  */
73
74 struct genboard gen;
75 struct ballboard ballboard;
76
77 /***********************/
78
79 void bootloader(void)
80 {
81 #define BOOTLOADER_ADDR 0x3f000
82         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
83                 printf_P(PSTR("Bootloader is not present\r\n"));
84                 return;
85         }
86         cli();
87         BRAKE_ON();
88         /* ... very specific :( */
89         TIMSK0 = 0;
90         TIMSK1 = 0;
91         TIMSK2 = 0;
92         TIMSK3 = 0;
93         TIMSK4 = 0;
94         TIMSK5 = 0;
95         EIMSK = 0;
96         UCSR0B = 0;
97         UCSR1B = 0;
98         UCSR2B = 0;
99         UCSR3B = 0;
100         SPCR = 0;
101         TWCR = 0;
102         ACSR = 0;
103         ADCSRA = 0;
104
105         EIND = 1;
106         __asm__ __volatile__ ("ldi r31,0xf8\n");
107         __asm__ __volatile__ ("ldi r30,0x00\n");
108         __asm__ __volatile__ ("eijmp\n");
109
110         /* never returns */
111 }
112
113 void do_led_blink(__attribute__((unused)) void *dummy)
114 {
115 #if 1 /* simple blink */
116         static uint8_t a=0;
117
118         if(a)
119                 LED1_ON();
120         else
121                 LED1_OFF();
122
123         a = !a;
124 #endif
125 }
126
127 static void main_timer_interrupt(void)
128 {
129         static uint8_t cpt = 0;
130         cpt++;
131         sei();
132         if ((cpt & 0x3) == 0)
133                 scheduler_interrupt();
134 }
135
136 int main(void)
137 {
138         /* brake */
139         BRAKE_OFF();
140         BRAKE_DDR();
141
142         /* CPLD reset on PG3 */
143         DDRG |= 1<<3;
144         PORTG &= ~(1<<3); /* implicit */
145
146         /* LEDS */
147         DDRJ |= 0x0c;
148         DDRL = 0xc0;
149         LED1_OFF();
150         memset(&gen, 0, sizeof(gen));
151         memset(&ballboard, 0, sizeof(ballboard));
152         ballboard.flags = DO_ENCODERS | DO_CS | DO_POWER; // DO_BD
153
154         /* UART */
155         uart_init();
156 #if CMDLINE_UART == 3
157         fdevopen(uart3_dev_send, uart3_dev_recv);
158         uart_register_rx_event(3, emergency);
159 #elif CMDLINE_UART == 1
160         fdevopen(uart1_dev_send, uart1_dev_recv);
161         uart_register_rx_event(1, emergency);
162 #else
163 #  error not supported
164 #endif
165
166         //eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_BALLBOARD);
167         /* check eeprom to avoid to run the bad program */
168         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
169             EEPROM_MAGIC_BALLBOARD) {
170                 sei();
171                 printf_P(PSTR("Bad eeprom value\r\n"));
172                 while(1);
173         }
174
175         /* LOGS */
176         error_register_emerg(mylog);
177         error_register_error(mylog);
178         error_register_warning(mylog);
179         error_register_notice(mylog);
180         error_register_debug(mylog);
181
182         /* SPI + ENCODERS */
183         encoders_spi_init(); /* this will also init spi hardware */
184
185         /* I2C */
186         i2c_protocol_init();
187         i2c_init(I2C_MODE_SLAVE, I2C_BALLBOARD_ADDR);
188         i2c_register_recv_event(i2c_recvevent);
189
190         /* TIMER */
191         timer_init();
192         timer0_register_OV_intr(main_timer_interrupt);
193
194         /* PWM */
195         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
196                                  TIMER1_PRESCALER_DIV_1);
197         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
198                                  TIMER4_PRESCALER_DIV_1);
199
200         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
201                       &PORTD, 4);
202         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED,
203                       &PORTD, 5);
204         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
205                       &PORTD, 6);
206         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
207                       &PORTD, 7);
208
209
210         /* servos */
211         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
212                                  TIMER1_PRESCALER_DIV_256);
213         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
214                       NULL, 0);
215         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
216                                  TIMER1_PRESCALER_DIV_256);
217         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
218                       NULL, 0);
219         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
220                       NULL, 0);
221         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
222                       NULL, 0);
223
224         /* SCHEDULER */
225         scheduler_init();
226
227         scheduler_add_periodical_event_priority(do_led_blink, NULL,
228                                                 100000L / SCHEDULER_UNIT,
229                                                 LED_PRIO);
230         /* all cs management */
231         microb_cs_init();
232
233         /* sensors, will also init hardware adc */
234         sensor_init();
235
236         /* TIME */
237         time_init(TIME_PRIO);
238
239         /* ax12 */
240         ax12_user_init();
241
242         sei();
243
244         printf_P(PSTR("\r\n"));
245         printf_P(PSTR("Dass das Gluck deinen Haus setzt.\r\n"));
246         cmdline_interact();
247
248         return 0;
249 }