aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/coproc-firmware/bootloader.c
blob: f87cd95b35599bc5d4cb0877fd54f84fa77f179d (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *   CNC-remote-control
 *   Button processor - Bootloader
 *
 *   Copyright (C) 2011-2016 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   version 2 as published by the Free Software Foundation.
 *
 *   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.
 */

#include "util.h"
#include "spi_interface.h"

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/boot.h>

#include <util/crc16.h>

#include <string.h>
#include <stdint.h>


static uint8_t page_buffer[SPM_PAGESIZE];


static void disable_all_irq_sources(void)
{
	GICR = 0;
	TIMSK = 0;
	SPCR = 0;
	UCSRB = 0;
	ADCSRA = 0;
	EECR = 0;
	ACSR = 0;
	TWCR = 0;
	SPMCR = 0;
}

static void route_irqs_to_bootloader(void)
{
	uint8_t tmp;

	__asm__ __volatile__(
"	ldi %[_tmp], %[_IVCE]		\n"
"	out %[_GICR], %[_tmp]		\n"
"	ldi %[_tmp], %[_IVSEL]		\n"
"	out %[_GICR], %[_tmp]		\n"
	: [_tmp]	"=a" (tmp)
	: [_GICR]	"I" (_SFR_IO_ADDR(GICR))
	, [_IVSEL]	"M" (1 << IVSEL)
	, [_IVCE]	"M" (1 << IVCE)
	);
}

static void route_irqs_to_application(void)
{
	uint8_t tmp;

	__asm__ __volatile__(
"	ldi %[_tmp], %[_IVCE]		\n"
"	out %[_GICR], %[_tmp]		\n"
"	clr %[_tmp]			\n"
"	out %[_GICR], %[_tmp]		\n"
	: [_tmp]	"=a" (tmp)
	: [_GICR]	"I" (_SFR_IO_ADDR(GICR))
	, [_IVCE]	"M" (1 << IVCE)
	);
}

static inline void spi_busy(bool busy)
{
	if (busy) {
		/* Busy */
		SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT |
						    (1u << SPI_SLAVE_TRANSIRQ_BIT));
	} else {
		/* Ready */
		SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT &
						    ~(1u << SPI_SLAVE_TRANSIRQ_BIT));
	}
}

static void spi_init(void)
{
	DDRB = (uint8_t)(DDRB | (1u << 4/*MISO*/));
	DDRB = (uint8_t)(DDRB & ~((1u << 5/*SCK*/) | (1u << 3/*MOSI*/) |
				  (1u << 2/*SS*/)));
	spi_busy(1);
	SPI_SLAVE_TRANSIRQ_DDR = (uint8_t)(SPI_SLAVE_TRANSIRQ_DDR |
					   (1u << SPI_SLAVE_TRANSIRQ_BIT));

	SPCR = (1u << SPE) | (0u << SPIE) | (0u << CPOL) | (0u << CPHA);
	SPSR = 0u;
	(void)SPSR; /* clear state */
	(void)SPDR; /* clear state */
}

static void spi_disable(void)
{
	SPCR = 0;
	SPSR = 0;
	SPDR = 0;
	(void)SPSR; /* clear state */
	(void)SPDR; /* clear state */
	DDRB = 0;
}

static inline void spi_transwait(void)
{
	while (!(SPSR & (1 << SPIF)));
}

static noinline uint8_t spi_xfer_sync(uint8_t tx)
{
	uint8_t data;

	SPDR = tx;
	spi_busy(0);
	spi_transwait();
	spi_busy(1);
	data = SPDR;

	return data;
}

static noreturn noinline void exit_bootloader(void)
{
	irq_disable();
	spi_disable();
	disable_all_irq_sources();
	wdt_enable(WDTO_2S);

	route_irqs_to_application();
	/* Jump to application code */
	__asm__ __volatile__(
	"ijmp\n"
	: /* None */
	: [_Z]		"z" (0x0000)
	);
	unreachable();
}

static bool verify_page(uint16_t page_address)
{
	uint8_t i, data0, data1;

	for (i = 0; i < ARRAY_SIZE(page_buffer); i++) {
		wdt_reset();
		data0 = page_buffer[i];
		data1 = pgm_read_byte((void PROGPTR *)(void *)(page_address + i));
		if (data0 != data1)
			return 0;
	}

	return 1;
}

static void write_page(uint16_t page_address)
{
	uint8_t i, sreg;
	uint16_t data;

	eeprom_busy_wait();
	boot_spm_busy_wait();

	sreg = irq_disable_save();

	boot_page_erase(page_address);
	boot_spm_busy_wait();
	for (i = 0; i < SPM_PAGESIZE; i = (uint8_t)(i + 2u)) {
		wdt_reset();
		data = (uint16_t)(page_buffer[i]);
		data |= ((uint16_t)(page_buffer[i + 1]) << 8);
		boot_page_fill(page_address + i, data);
	}
	boot_page_write(page_address);
	boot_spm_busy_wait();
	boot_rww_enable();

	irq_restore(sreg);
}

static noinline uint8_t calc_crc8(uint8_t crc, uint8_t data)
{
	return spi_crc8(crc, data);
}

static void do_flash(void)
{
	uint8_t data, addr_lo, addr_hi;
	uint8_t crc = 0, expected_crc;
	uint16_t page_address, i;
	bool ok;

	addr_lo = spi_xfer_sync(0);
	crc = calc_crc8(crc, addr_lo);
	addr_hi = spi_xfer_sync(0);
	crc = calc_crc8(crc, addr_hi);
	page_address = (uint16_t)addr_lo | ((uint16_t)addr_hi << 8);

	for (i = 0; i < ARRAY_SIZE(page_buffer); i++) {
		data = spi_xfer_sync(0);
		page_buffer[i] = data;
		crc = calc_crc8(crc, data);
	}

	crc ^= 0xFF;
	expected_crc = spi_xfer_sync(0);
	if (expected_crc != crc) {
		spi_xfer_sync(SPI_RESULT_FAIL);
		return;
	}
	spi_xfer_sync(SPI_RESULT_OK);

	write_page(page_address);
	ok = verify_page(page_address);
	memset(page_buffer, 0xFF, sizeof(page_buffer));
	if (ok)
		spi_xfer_sync(SPI_RESULT_OK);
	else
		spi_xfer_sync(SPI_RESULT_FAIL);
}

static void handle_spi(void)
{
	uint8_t data, txdata = 0;

	while (1) {
		data = spi_xfer_sync(txdata);
		txdata = 0;
		switch (data) {
		case SPI_CONTROL_ENTERBOOT:
		case SPI_CONTROL_ENTERBOOT2:
			/* We're already here. */
			txdata = SPI_RESULT_OK;
			break;
		case SPI_CONTROL_TESTAPP:
			txdata = SPI_RESULT_FAIL;
			break;
		case SPI_CONTROL_ENTERAPP:
			exit_bootloader();
			break;
		case SPI_CONTROL_STARTFLASH:
			do_flash();
			break;
		default:
			/* Ignore unknown commands */
			break;
		}
	}
}

static uint8_t saved_mcucsr __attribute__((section(".noinit")));

void early_init(void) __attribute__((naked, section(".init3"), used));
void early_init(void)
{
	irq_disable();
	saved_mcucsr = MCUCSR;
	MCUCSR = 0;
	wdt_enable(WDTO_2S);
}

int main(void) _mainfunc;
int main(void)
{
	uint8_t mcucsr;

	irq_disable();
	wdt_enable(WDTO_2S);

	mcucsr = saved_mcucsr;
	saved_mcucsr = 0;

	if (!(mcucsr & (1 << PORF))) {
		if ((mcucsr & (1 << WDRF)) ||
		    (mcucsr & (1 << BORF)))
			exit_bootloader();
	}

	disable_all_irq_sources();
	route_irqs_to_bootloader();

	spi_init();
	while (1) {
		wdt_reset();
		handle_spi();
	}
}
bues.ch cgit interface