save
[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         static uint8_t a = 0;
116
117         if (ballboard.flags & DO_ERRBLOCKING) {
118                 if (a & 1)
119                         LED1_ON();
120                 else
121                         LED1_OFF();
122         }
123         else {
124                 if (a & 4)
125                         LED1_ON();
126                 else
127                         LED1_OFF();
128         }
129         a++;
130 }
131
132 static void main_timer_interrupt(void)
133 {
134         static uint8_t cpt = 0;
135         cpt++;
136         sei();
137         if ((cpt & 0x3) == 0)
138                 scheduler_interrupt();
139 }
140
141 int main(void)
142 {
143         /* brake */
144         BRAKE_OFF();
145         BRAKE_DDR();
146
147         /* CPLD reset on PG3 */
148         DDRG |= 1<<3;
149         PORTG &= ~(1<<3); /* implicit */
150
151         /* LEDS */
152         DDRJ |= 0x0c;
153         DDRL = 0xc0;
154         LED1_OFF();
155         memset(&gen, 0, sizeof(gen));
156         memset(&ballboard, 0, sizeof(ballboard));
157         ballboard.flags = DO_ENCODERS | DO_CS | DO_POWER |
158                 DO_ERRBLOCKING | DO_BD;
159
160         /* UART */
161         uart_init();
162 #if CMDLINE_UART == 3
163         fdevopen(uart3_dev_send, uart3_dev_recv);
164         uart_register_rx_event(3, emergency);
165 #elif CMDLINE_UART == 1
166         fdevopen(uart1_dev_send, uart1_dev_recv);
167         uart_register_rx_event(1, emergency);
168 #else
169 #  error not supported
170 #endif
171
172         //eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_BALLBOARD);
173         /* check eeprom to avoid to run the bad program */
174         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
175             EEPROM_MAGIC_BALLBOARD) {
176                 sei();
177                 printf_P(PSTR("Bad eeprom value\r\n"));
178                 while(1);
179         }
180
181         /* LOGS */
182         error_register_emerg(mylog);
183         error_register_error(mylog);
184         error_register_warning(mylog);
185         error_register_notice(mylog);
186         error_register_debug(mylog);
187
188         /* SPI + ENCODERS */
189         encoders_spi_init(); /* this will also init spi hardware */
190
191         /* I2C */
192         i2c_protocol_init();
193         i2c_init(I2C_MODE_SLAVE, I2C_BALLBOARD_ADDR);
194         i2c_register_recv_event(i2c_recvevent);
195
196         /* TIMER */
197         timer_init();
198         timer0_register_OV_intr(main_timer_interrupt);
199
200         /* PWM */
201         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
202                                  TIMER1_PRESCALER_DIV_1);
203         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
204                                  TIMER4_PRESCALER_DIV_1);
205
206         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
207                       &PORTD, 4);
208         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED,
209                       &PORTD, 5);
210         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
211                       &PORTD, 6);
212         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
213                       &PORTD, 7);
214
215
216         /* servos */
217         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
218                                  TIMER1_PRESCALER_DIV_256);
219         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
220                       NULL, 0);
221         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
222                                  TIMER1_PRESCALER_DIV_256);
223         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
224                       NULL, 0);
225         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
226                       NULL, 0);
227         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
228                       NULL, 0);
229
230         /* SCHEDULER */
231         scheduler_init();
232
233         scheduler_add_periodical_event_priority(do_led_blink, NULL,
234                                                 100000L / SCHEDULER_UNIT,
235                                                 LED_PRIO);
236         /* all cs management */
237         microb_cs_init();
238
239         /* sensors, will also init hardware adc */
240         sensor_init();
241
242         /* TIME */
243         time_init(TIME_PRIO);
244
245         /* ax12 */
246         ax12_user_init();
247
248         gen.logs[0] = E_USER_ST_MACH;
249         gen.log_level = 5;
250
251         sei();
252
253         printf_P(PSTR("\r\n"));
254         printf_P(PSTR("Dass das Gluck deinen Haus setzt.\r\n"));
255         cmdline_interact();
256
257         return 0;
258 }