Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
181 andfri 1
#include "bios_funs.h"
2
#include <inttypes.h>
3
#include <avr/interrupt.h>
4
#include <avr/pgmspace.h>
5
#include <avr/io.h>
6
#include <stdio.h>
7
 
8
//---------------------------------------------------------------------------
9
// Function definitions for the exported interface
10
 
11
int bios_putchar(char c, FILE *stream) {
12
    loop_until_bit_is_set(UCSRA, UDRE);
13
    UDR = c;
14
    return 0;
15
};
16
 
17
int bios_getchar(FILE *stream) {
18
    loop_until_bit_is_set(UCSRA, RXC);
19
    return UDR;
20
};
21
 
22
void bios_reset(void) {
23
    bios_putchar('\n', 0);
24
};
25
 
26
unsigned bios_version(void) {
27
    return BIOS_VERSION;
28
};
29
 
30
//---------------------------------------------------------------------------
31
// Function definitions for the private functions
32
 
33
void program_page (uint32_t page, uint8_t *buf)
34
{
35
    uint16_t i;
36
    uint8_t sreg;
37
 
38
    // Disable interrupts.
39
 
40
    sreg = SREG;
41
    cli();
42
 
43
    eeprom_busy_wait ();
44
 
45
    boot_page_erase (page);
46
    boot_spm_busy_wait ();      // Wait until the memory is erased.
47
 
48
    for (i=0; i<SPM_PAGESIZE; i+=2)
49
    {
50
        // Set up little-endian word.
51
 
52
        uint16_t w = *buf++;
53
        w += (*buf++) << 8;
54
 
55
        boot_page_fill (page + i, w);
56
    }
57
 
58
    boot_page_write (page);     // Store buffer in flash page.
59
    boot_spm_busy_wait();       // Wait until the memory is written.
60
 
61
    // Reenable RWW-section again. We need this if we want to jump back
62
    // to the application after bootloading.
63
 
64
    boot_rww_enable ();
65
 
66
    // Re-enable interrupts (if they were ever enabled).
67
 
68
    SREG = sreg;
69
}
70
 
71
static FILE bios_stdio = FDEV_SETUP_STREAM(bios_putchar, bios_getchar,
72
                                         _FDEV_SETUP_RW);
73
 
74
int main() {
75
    void (*app_reset)(void) = 0;
76
    // Setup USART for debug channel
77
    UCSRB = _BV(RXEN)|_BV(TXEN);
78
    UBRRL = (F_CPU / 16 / F_BAUD) - 1;
79
    stdout = &bios_stdio;
80
    stdin = &bios_stdio;
81
 
82
    bios->reset();
83
    printf("AVR BIOS\n");
84
    app_reset();
85
 
86
    return 0;
87
}