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