Root/atusb/fw/boot.c

1/*
2 * fw/boot.c - DFU boot loader for ATUSB
3 *
4 * Written 2008-2011 by Werner Almesberger
5 * Copyright 2008-2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include <avr/io.h>
17#include <avr/interrupt.h>
18#include <avr/pgmspace.h>
19
20#define F_CPU 8000000UL
21#include <util/delay.h>
22
23#include "usb.h"
24#include "dfu.h"
25
26#include "board.h"
27#include "spi.h"
28#include "atusb/ep0.h"
29
30
31#define MS_TO_LOOPS(ms) ((uint32_t) (ms)*335)
32
33
34static void (*run_payload)(void) = 0;
35
36
37int main(void)
38{
39    /*
40     * pgm_read_byte gets cached and there doesn't seem to be any other
41     * way to dissuade gcc from doing this.
42     */
43    volatile int zero = 0;
44    uint32_t loop = 0;
45
46    board_init();
47    reset_rf();
48
49    /* now we should be at 8 MHz */
50
51    usb_init();
52    dfu_init();
53
54    /* move interrupt vectors to the boot loader */
55    MCUCR = 1 << IVCE;
56    MCUCR = 1 << IVSEL;
57
58    sei();
59
60    led(1);
61
62    while (loop != MS_TO_LOOPS(2500)) {
63        if (dfu.state == dfuIDLE && pgm_read_byte(zero) != 0xff)
64            loop++;
65        else
66            loop = 0;
67    }
68
69    led(0);
70
71    cli();
72
73    usb_reset();
74    run_payload();
75
76    while (1); /* not reached */
77}
78

Archive Download this file



interactive