summaryrefslogtreecommitdiff
path: root/src/bwbootloader/prog.c
blob: 2bd7fa642976472250da15ee733c7834efeb0229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* prog.c */
/* binwatch - Tiny binary wristwatch. {{{
 *
 * Copyright (C) 2010 Nicolas Schodet
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 * }}} */
#include "common.h"

#include "bwbootloader.h"
#include "prog.h"

#include <avr/boot.h>
#include <avr/pgmspace.h>

void
prog_read (uint16_t address, uint8_t *buffer, uint8_t length)
{
    while (length--)
	*buffer++ = pgm_read_byte (address++);
}

uint8_t
prog_page_write (uint16_t address, const uint8_t *buffer)
{
    /* Bootloader area is protected. */
    if (address >= PROG_FLASH_SIZE - BWBOOTLOADER_SIZE)
	return 0;
    /* Aligned write only. */
    if (address % PROG_PAGE_SIZE != 0)
	return 0;
    /* No EEPROM or interrupt protection, there is none in this bootloader. */
    /* Load page buffer. */
    uint16_t w;
    uint8_t i;
    for (i = 0; i < PROG_PAGE_SIZE; i += 2)
      {
	/* Protect reset vector. */
	if (address == 0 && i == 0)
	    w = pgm_read_word (0);
	else
	    w = *(const uint16_t *) (buffer + i);
	boot_page_fill (address + i, w);
      }
    /* Erase page. */
    boot_page_erase (address);
    boot_spm_busy_wait ();
    /* Program page. */
    boot_page_write (address);
    boot_spm_busy_wait ();
    /* All done. */
    return 1;
}

void
prog_read_fuses (uint8_t *buffer)
{
    buffer[0] = boot_lock_fuse_bits_get (GET_LOW_FUSE_BITS);
    buffer[1] = boot_lock_fuse_bits_get (GET_HIGH_FUSE_BITS);
    buffer[2] = boot_lock_fuse_bits_get (GET_EXTENDED_FUSE_BITS);
}

void
prog_start (void)
{
    /* Jump after vectors. */
    application_entry ();
}