X-Git-Url: http://git.droids-corp.org/?p=protos%2Fimu.git;a=blobdiff_plain;f=main.c;fp=main.c;h=91ee7b709392ed9a66a08302c2274a1253b15373;hp=0000000000000000000000000000000000000000;hb=84796cd7a01e949167a155ec21f55fd71e788015;hpb=883f5aae494ab066938b0cebb554a1ee13766713 diff --git a/main.c b/main.c new file mode 100644 index 0000000..91ee7b7 --- /dev/null +++ b/main.c @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2011, Olivier MATZ + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the University of California, Berkeley nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* fuses: + * avrdude -p atmega1284p -P usb -c avrispmkii -U lfuse:w:0xff:m -U hfuse:w:0x91:m -U efuse:w:0xff:m + * -> it failed but I answered y, then make reset and it was ok + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "eeprom_config.h" +#include "main.h" + +struct imuboard imuboard; +volatile uint32_t global_ms; + +/* global xbee device */ +struct xbee_dev *xbee_dev; + +void bootloader(void) +{ +#define BOOTLOADER_ADDR 0x3f000 + if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) { + printf_P(PSTR("Bootloader is not present\r\n")); + return; + } + cli(); + /* ... very specific :( */ + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK2 = 0; + TIMSK3 = 0; + EIMSK = 0; + UCSR0B = 0; + UCSR1B = 0; + SPCR = 0; + TWCR = 0; + ACSR = 0; + ADCSRA = 0; + + /* XXX */ + /* __asm__ __volatile__ ("ldi r31,0xf8\n"); */ + /* __asm__ __volatile__ ("ldi r30,0x00\n"); */ + /* __asm__ __volatile__ ("eijmp\n"); */ +} + +/* return time in milliseconds on unsigned 16 bits */ +uint16_t get_time_ms(void) +{ + uint16_t ms; + uint8_t flags; + IRQ_LOCK(flags); + ms = global_ms; + IRQ_UNLOCK(flags); + return ms; +} + +static void main_timer_interrupt(void) +{ + static uint16_t cycles; + static uint8_t stack = 0; + static uint8_t cpt = 0; + cpt++; + + /* LED blink */ + if (global_ms & 0x80) + LED1_ON(); + else + LED1_OFF(); + + if ((cpt & 0x03) != 0) + return; + + /* the following code is only called one interrupt among 4: every 682us + * (at 12 Mhz) = 8192 cycles */ + cycles += 8192; + if (cycles >= 12000) { + cycles -= 12000; + global_ms ++; + } + + /* called */ + if (stack++ == 0) + LED2_ON(); + sei(); + if ((cpt & 0x3) == 0) + callout_manage(&imuboard.intr_cm); + cli(); + if (--stack == 0) + LED2_OFF(); +} + +/* XXX */ +int imu_loop(void); +int sd_main(void); + +int main(void) +{ + DDRB = 0x18 /* LEDs */; + + uart_init(); + uart_register_rx_event(CMDLINE_UART, emergency); + + fdevopen(cmdline_dev_send, cmdline_dev_recv); + timer_init(); + timer0_register_OV_intr(main_timer_interrupt); + + callout_mgr_init(&imuboard.intr_cm, get_time_ms); + + cmdline_init(); + /* LOGS */ + error_register_emerg(mylog); + error_register_error(mylog); + error_register_warning(mylog); + error_register_notice(mylog); + error_register_debug(mylog); + + /* communication with mpu6050 */ + i2cm_init(); + + sei(); + + eeprom_load_config(); + + printf_P(PSTR("\r\n")); + rdline_newline(&imuboard.rdl, imuboard.prompt); + + //sd_main(); + + imu_loop(); + + while (1) { + cmdline_poll(); + } + + return 0; +}