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