aboutsummaryrefslogtreecommitdiff
path: root/firmware/firmware.c
blob: 4a681951d3f48abcd8288064d208eba3554487d7 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* $Id$ */
/*
 * Copyright (c) 2011 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <avr/io.h>
#include <util/setbaud.h>	/* depends on BAUD & F_CPU env vars */
#include <avr/boot.h>

#define TIMEOUT (F_CPU >> 3)	/* ca. 2 sec */

union {
	uint16_t word;
	uint8_t	byte[2];
} data;

void
reboot(void)
{
	boot_rww_enable();

	((void(*)(void))0)();	/* jump to app */
}

void
putch(char c)
{
	loop_until_bit_is_set(UCSRA, UDRE);

	UDR = c;
}

uint8_t
getch(void)
{
	uint32_t counter = 0;

	do {
		if (++counter > TIMEOUT)
			reboot();
	} while (bit_is_clear(UCSRA, RXC));

	return UDR;
}

enum { INIT, PAGE, DATA, CKSUM };

int
main(void)
{

	uint16_t off = 0;
	uint8_t ch = 0;
	uint8_t n = 0;
	uint8_t sum = 0;
	uint8_t state = INIT;

	UCSRB = _BV(RXEN) | _BV(TXEN);
	UBRRH = UBRRH_VALUE;
	UBRRL = UBRRL_VALUE;
	UCSRA |= _BV(U2X);

	putch('+');		/* say hallo */
	for (;;) {
		ch = getch();
		switch (state) {
		case INIT:
			switch (ch) {
			case 'D':
				state = PAGE;
				break;
			case 'P':	/* legacy: confim programming state */
				putch('p');
				break;
			case 'R':
				reboot();
				break;
			}
			break;
		case PAGE:
			n = 0;
			sum = ch;
			off = (uint16_t)ch * SPM_PAGESIZE;
			state = DATA;
			break;
		case DATA:
			sum += ch;
			data.byte[n % 2] = ch;
			if (n % 2)
				boot_page_fill(off + n - 1, data.word);
			if (n++ == SPM_PAGESIZE)
				state = CKSUM;
			break;
		case CKSUM:
			if (sum == ch) {
				boot_page_erase(off);
				boot_spm_busy_wait();
				boot_page_write(off);
				boot_spm_busy_wait();
				putch('d');	/* confirm */
			} else
				putch('D');
			state = INIT;
			break;
		}
	}

	return 0;
}