Root/fw/boot.c

Source at commit 2dc2fb278cf17368265e066d025c2584d640f1a7 created 11 years 11 days ago.
By Werner Almesberger, tornado/fw/tornado.c: update signal processing from sim/alg.c
1/*
2 * fw/boot.c - Antorcha boot loader
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 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 <avr/io.h>
15#include <avr/pgmspace.h>
16
17#include "io.h"
18#include "rf.h"
19#include "proto.h"
20#include "dispatch.h"
21#include "fw.h"
22
23
24#define MS_TO_LOOP(ms) ((uint32_t) (ms)*106)
25
26
27static void wait_upload(void)
28{
29    uint8_t buf[PAYLOAD+5]; /* 3 bytes header, 2 bytes CRC */
30    uint32_t i;
31    uint8_t got;
32
33restart:
34    for (i = 0; i != MS_TO_LOOP(5000); i++) {
35        got = rf_recv(buf, sizeof(buf));
36        if (got > 2 && fw_packet(buf, got-2))
37            goto restart;
38    }
39
40}
41
42
43int main(void)
44{
45    volatile int zero = 0;
46
47    /* Port B has two LEDs and the rest is SPI */
48    PORTB = MASK(B, RF_nSS) | MASK(B, RF_nRST);
49    DDRB = MASK(B, RF_SCLK) | MASK(B, RF_MOSI) | MASK(B, RF_nSS) |
50        MASK(B, RF_nRST) | 0xc0;
51
52    /* All port C pins drive LEDs */
53    PORTC = 0;
54    DDRC = 0x3f;
55
56    /* All port D pins drive LEDs */
57    PORTD = 0;
58    DDRD = 0xff;
59
60    /* Disable pull-ups */
61    MCUCR |= 1 << PUD;
62
63    /*
64     * Disable the watchdog timer, in case the boot loader has been
65     * entered by a watchdog reset commanded by the application.
66     */
67
68    MCUSR = 0; /* Remove override */
69    WDTCSR |= 1 << WDCE; /* Enable change */
70    WDTCSR = 1 << WDCE; /* Disable watchdog while still enabling
71                   change */
72
73    rf_init();
74
75    /*
76     * Switch the LED inside the loop so that we get a short pulse one can
77     * observe on a scope.
78     */
79    do {
80        SET(LED_B8);
81        wait_upload();
82        CLR(LED_B8);
83    } while (pgm_read_byte(zero) == 0xff);
84
85    ((void (*)(void)) 0)();
86
87    /* not reached */
88    return 0;
89}
90

Archive Download this file

Branches:
master
tornado-v1



interactive