Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
181 andfri 1
#include "bios_funs.h"
223 andfri 2
#include "avr_cfg.h"
181 andfri 3
#include <inttypes.h>
228 andfri 4
#include <avr/io.h>
181 andfri 5
#include <avr/interrupt.h>
223 andfri 6
#include <avr/wdt.h>
181 andfri 7
#include <avr/pgmspace.h>
8
#include <stdio.h>
228 andfri 9
#include "flash.h"
10
#include "vectors.h" // Must be included after sfr_defs.h
181 andfri 11
 
12
//---------------------------------------------------------------------------
223 andfri 13
// Globals
14
 
15
volatile long gMilliSecTick;
16
 
17
//---------------------------------------------------------------------------
181 andfri 18
// Function definitions for the exported interface
19
 
223 andfri 20
void reset(void) {
21
    //Just loop and let the watchdog reset
22
    cli();
23
    while(1);
24
};
25
 
26
void bios_putchar(char c) {
228 andfri 27
    UART_PUTCHAR(c);
181 andfri 28
};
29
 
223 andfri 30
char bios_getchar(void) {
228 andfri 31
    char c;
32
    UART_GETCHAR(c)
33
    return c;
181 andfri 34
};
35
 
223 andfri 36
long timebase_get(void) {
37
    uint8_t sreg;
38
    long res;
39
 
40
    //Disable interrupts while reading tick count
41
    sreg=SREG;
42
    cli();
43
 
44
    res = gMilliSecTick;
45
 
46
    SREG=sreg;
47
 
48
    return res;
49
}
181 andfri 50
 
51
//---------------------------------------------------------------------------
52
// Function definitions for the private functions
223 andfri 53
 
54
static int console_getchar(FILE *stream) {
228 andfri 55
    return bios_getchar();
223 andfri 56
}
57
 
58
static int console_putchar(char c, FILE *stream) {
228 andfri 59
    bios_putchar(c);
223 andfri 60
    return 0;
61
}
62
 
63
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
181 andfri 64
                                         _FDEV_SETUP_RW);
65
 
223 andfri 66
 
67
//---------------------------------------------------------------------------
68
// Interrupt Service Routines
69
// Read app_vectors.h!
70
 
71
ISR(TIMEBASE_VECTOR) {
72
    TIMEBASE_COUNTER -= TIMEBASE_RELOAD;
73
    gMilliSecTick++;
74
    wdt_reset();
75
}
76
 
181 andfri 77
int main() {
78
    void (*app_reset)(void) = 0;
223 andfri 79
 
181 andfri 80
    // Setup USART for debug channel
228 andfri 81
    UART_INIT();
181 andfri 82
    stdout = &bios_stdio;
83
    stdin = &bios_stdio;
84
 
223 andfri 85
    // Initialize a suitable hardware timer to create a 1KHz interrupt.
86
    TIMEBASE_INIT();
87
 
88
    // Enable watchdog timer to protect against an application locking the
89
    // node by leaving interrupts disabled. 
90
    wdt_enable(WDTO_500MS);
91
 
92
    // Move interrupt vectors to start of bootloader section and enable interrupts
228 andfri 93
    IVSEL_REG = _BV(IVCE);
94
    IVSEL_REG = _BV(IVSEL);
223 andfri 95
    sei();
96
 
181 andfri 97
    printf("AVR BIOS\n");
98
 
223 andfri 99
    if (pgm_read_word(0) != 0xffff) {
100
        printf("Starting application.\n");
101
        app_reset();
102
    }
103
 
104
    printf("No application found.\n");
181 andfri 105
    return 0;
106
}