-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.c
244 lines (204 loc) · 5.12 KB
/
bootloader.c
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
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/boot.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include "bootloader_protocol.h"
static void bl_init();
static uint8_t bl_read();
static void bl_write(uint8_t);
static void bl_reset() __attribute__((noreturn));
static void bl_check();
static void bl_vfd_init();
static void bl_vfd_write(uint8_t command, uint8_t data);
static void bl_vfd_write_buffer_P(const char *data);
static void bl_vfd_set_cursor(uint8_t line, uint8_t column);
/*
* This is largly taken from Atmel Application note AVR109
* */
/* VFD Commands */
/* Command or Data packet */
#define HD_COMMAND 0xF8
#define HD_DATA 0xFA
/* To set line numbers for vfd_set_cursor */
#define HD_LINE_0 0x00
#define HD_LINE_1 0x40
int
main(void)
{
uint16_t address = 0;
uint16_t erase = 0;
uint16_t data;
const static char str_erasing[] PROGMEM = "Erasing...";
const static char str_programming[] PROGMEM = "Programming...";
const static char str_update[] PROGMEM = "Firmware Update Mode";
/* Configure the bootloader enable pin as an input, and check to see if it is set */
/*
if(bl_override == 0){
bl_check();
}
*/
bl_init();
bl_vfd_init();
bl_vfd_write_buffer_P(str_update);
bl_vfd_set_cursor(HD_LINE_1,0);
for(;;){
const uint8_t cmd = bl_read();
switch(cmd){
case BL_PING: // ping
bl_write(BL_ACK);
break;
case BL_SET_ADDR: // Manually set address
address = (bl_read() << 8) | bl_read();
bl_write(BL_ACK);
break;
case BL_ERASE: // Chip Erase
bl_vfd_write(HD_COMMAND, 0x01);
bl_vfd_set_cursor(HD_LINE_0,0);
bl_vfd_write_buffer_P(str_erasing);
bl_vfd_set_cursor(HD_LINE_1,0);
for(address = 0; address < BOOTAPPEND ; address += 2){
boot_spm_busy_wait();
boot_page_erase(address);
erase += 2;
if(erase > (BOOTAPPEND / 1 / 20)){
erase = 0;
bl_vfd_write(HD_DATA,'*');
}
}
address = 0;
bl_write(BL_ACK);
bl_vfd_write(HD_COMMAND, 0x01);
bl_vfd_write_buffer_P(str_update);
break;
case BL_ERASE_EEPROM: // EEPROM Erase
for(address = 0; address < EEPROM_SZ; address += 2){
eeprom_busy_wait();
eeprom_write_word(&address, 0);
}
address = 0;
break;
case BL_PROGRAM:
if(address > BOOTAPPEND){
bl_write(BL_NAK);
break;
}
data = (bl_read() << 8) | bl_read();
boot_spm_busy_wait();
boot_page_fill(address, data);
boot_spm_busy_wait();
boot_page_write(address);
address+=2;
bl_write(BL_ACK);
break;
case BL_PROGRAM_MESSAGE: // show the 'programming' message
bl_vfd_write(HD_COMMAND, 0x01);
bl_vfd_set_cursor(HD_LINE_0,0);
bl_vfd_write_buffer_P(str_programming);
bl_vfd_set_cursor(HD_LINE_1,0);
break;
case BL_PERCENT: // Programing percentage complete
bl_vfd_write(HD_DATA,'*');
break;
case BL_RESET: // reset
bl_reset();
break;
}
}
}
static void
bl_reset()
{
//((void(*)(void))(0))();
// abuse the watchdog timer
wdt_enable(WDTO_250MS);
for(;;);
}
static void
bl_init()
{
/* Init the serial port to default values*/
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
/* Force baud rate to makefile defined value */
#define BAUD_PRESCALE(x) (F_CPU / ((x) * 16UL) - 1UL)
UBRR0H = (uint8_t) (BAUD_PRESCALE(USART_BAUD) >> 8);
UBRR0L = (uint8_t) BAUD_PRESCALE(USART_BAUD);
#undef BAUD_PRESCALE
}
static uint8_t
bl_read()
{
while(!(UCSR0A & (1 << RXC0)));
return UDR0;
}
static void
bl_write(uint8_t byte)
{
UDR0 = byte;
while(!(UCSR0A & (1 << TXC0)));
UCSR0A |= (1 << TXC0);
}
#define VFD_CS_PORT PORTC
#define VFD_CS_DDR DDRC
#define VFD_CS_PIN PC0
#define RAISE_CS() VFD_CS_PORT = VFD_CS_PORT | (1 << VFD_CS_PIN)
#define LOWER_CS() VFD_CS_PORT = VFD_CS_PORT & ~(1 << VFD_CS_PIN)
#define WAIT_TX() while(!(SPSR & (1 << SPIF)));
static void
bl_vfd_init()
{
/* SS, MIS0, MOSI, SCK - set as outputs */
DDRB = DDRB | (1 << PB2) | (1 << PB3) | (1 << PB4) | (1 << PB5);
/* Configure user defined chip select pin */
VFD_CS_DDR = VFD_CS_DDR | (1 << VFD_CS_PIN);
RAISE_CS();
/* Enable SPI, Master, Mode 3 */
SPCR = (1 << SPE) | (1 << MSTR) | (1 << CPOL) | (1 << CPHA) | (0 << SPR1) | (0 << SPR0);
SPSR = SPSR | (1 << SPI2X);
bl_vfd_write(HD_COMMAND,0x01);
bl_vfd_write(HD_COMMAND,0x02);
bl_vfd_write(HD_COMMAND,0x06);
bl_vfd_write(HD_COMMAND,0x0C);
bl_vfd_write(HD_COMMAND,0x38);
bl_vfd_write(HD_COMMAND,0x80);
}
static void
bl_vfd_write(uint8_t command, uint8_t data)
{
LOWER_CS();
SPDR = command;
WAIT_TX();
SPDR = data;
WAIT_TX();
RAISE_CS();
}
static void
bl_vfd_write_buffer_P(const char *data)
{
while(pgm_read_byte(data)){
bl_vfd_write(HD_DATA,pgm_read_byte(data));
data++;
}
}
static void
bl_vfd_set_cursor(uint8_t line, uint8_t column)
{
bl_vfd_write(HD_COMMAND, 0x80 | (line + column));
}
static void
bl_check()
{
DDRB = DDRB & ~(1 << PB0);
PORTB = PORTB | (1 << PB0);
/* Sleep a bit, allow the pull up to kick in */
_delay_ms(200.0);
/* If the pin is low, bootloader enabled */
if(!(PINB & (1 << PB0))){
/* Bootloader is enabled. */
return;
}
/* Bootloader is not enabled, start the main program */
((void(*)(void))(0x0000))();
}