forked from triffid/LPC17xx-DFU-Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
294 lines (247 loc) · 6.97 KB
/
main.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
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
/*****************************************************************************
* *
* DFU/SD/SDHC Bootloader for LPC17xx *
* *
* by Triffid Hunter *
* *
* *
* This firmware is Copyright (C) 2009-2010 Michael Moon aka Triffid_Hunter *
* *
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
*****************************************************************************/
#include "usbhw.h"
#include "usbcore.h"
#include "uart.h"
#include "SDCard.h"
#include "gpio.h"
#include "sbl_iap.h"
#include "sbl_config.h"
#include "ff.h"
#include "dfu.h"
#include "min-printf.h"
#include "lpc17xx_wdt.h"
#define ISP_BTN P2_12
#if !(defined DEBUG)
#define printf(...) do {} while (0)
#endif
FATFS fat;
FIL file;
const char *firmware_file = "firmware.bin";
const char *firmware_old = "firmware.cur";
void setleds(int leds)
{
GPIO_write(LED1, leds & 1);
GPIO_write(LED2, leds & 2);
GPIO_write(LED3, leds & 4);
GPIO_write(LED4, leds & 8);
GPIO_write(LED5, leds & 16);
}
int isp_btn_pressed()
{
return GPIO_get(ISP_BTN);
}
void start_dfu()
{
DFU_init();
usb_init();
usb_connect();
while (DFU_complete() == 0)
usb_task();
usb_disconnect();
}
void check_sd_firmware()
{
int r;
printf("Check SD\n");
f_mount(0, &fat);
if ((r = f_open(&file, firmware_file, FA_READ)) == FR_OK)
{
printf("Flashing firmware...\n");
uint8_t buf[512];
unsigned int r = sizeof(buf);
uint32_t address = USER_FLASH_START;
while (r == sizeof(buf))
{
if (f_read(&file, buf, sizeof(buf), &r) != FR_OK)
{
f_close(&file);
return;
}
setleds((address - USER_FLASH_START) >> 15);
printf("\t0x%lx\n", address);
write_flash((void *) address, (char *)buf, sizeof(buf));
address += r;
}
f_close(&file);
if (address > USER_FLASH_START)
{
printf("Complete!\n");
r = f_unlink(firmware_old);
r = f_rename(firmware_file, firmware_old);
}
}
else
{
printf("open: %d\n", r);
}
}
// this seems to fix an issue with handoff after poweroff
// found here http://knowledgebase.nxp.trimm.net/showthread.php?t=2869
static void boot(uint32_t a)
{
asm("LDR SP, [%0]" : : "r"(a));
asm("LDR PC, [%0, #4]" : : "r"(a));
// never returns
}
static uint32_t delay_loop(uint32_t count)
{
volatile uint32_t j, del;
for(j=0; j<count; ++j){
del=j; // volatiles, so the compiler will not optimize the loop
}
return del;
}
static void new_execute_user_code(void)
{
uint32_t addr=(uint32_t)USER_FLASH_START;
// delay
delay_loop(3000000);
// relocate vector table
SCB->VTOR = (addr & 0x1FFFFF80);
// switch to RC generator
LPC_SC->PLL0CON = 0x1; // disconnect PLL0
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (LPC_SC->PLL0STAT&(1<<25));
LPC_SC->PLL0CON = 0x0; // power down
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (LPC_SC->PLL0STAT&(1<<24));
LPC_SC->FLASHCFG &= 0x0fff; // This is the default flash read/write setting for IRC
LPC_SC->FLASHCFG |= 0x5000;
LPC_SC->CCLKCFG = 0x0; // Select the IRC as clk
LPC_SC->CLKSRCSEL = 0x00;
LPC_SC->SCS = 0x00; // not using XTAL anymore
delay_loop(1000);
// reset pipeline, sync bus and memory access
__asm (
"dmb\n"
"dsb\n"
"isb\n"
);
boot(addr);
}
int main()
{
WDT_Feed();
GPIO_init(ISP_BTN); GPIO_input(ISP_BTN);
GPIO_init(LED1); GPIO_output(LED1);
GPIO_init(LED2); GPIO_output(LED2);
GPIO_init(LED3); GPIO_output(LED3);
GPIO_init(LED4); GPIO_output(LED4);
GPIO_init(LED5); GPIO_output(LED5);
// turn off heater outputs
GPIO_init(P2_4); GPIO_output(P2_4); GPIO_write(P2_4, 0);
GPIO_init(P2_5); GPIO_output(P2_5); GPIO_write(P2_5, 0);
GPIO_init(P2_6); GPIO_output(P2_6); GPIO_write(P2_6, 0);
GPIO_init(P2_7); GPIO_output(P2_7); GPIO_write(P2_7, 0);
setleds(31);
UART_init(UART_RX, UART_TX, 2000000);
printf("Bootloader Start\n");
// give SD card time to wake up
for (volatile int i = (1UL<<12); i; i--);
SDCard_init(P0_9, P0_8, P0_7, P0_6);
if (SDCard_disk_initialize() == 0)
check_sd_firmware();
int dfu = 0;
if (isp_btn_pressed() == 0)
{
printf("ISP button pressed, entering DFU mode\n");
dfu = 1;
}
else if (WDT_ReadTimeOutFlag()) {
WDT_ClrTimeOutFlag();
printf("WATCHDOG reset, entering DFU mode\n");
dfu = 1;
}
if (dfu)
start_dfu();
#ifdef WATCHDOG
WDT_Init(WDT_CLKSRC_IRC, WDT_MODE_RESET);
WDT_Start(1<<22);
#endif
// grab user code reset vector
#ifdef DEBUG
unsigned *p = (unsigned *)(USER_FLASH_START +4);
printf("Jumping to 0x%x\n", *p);
#endif
while (UART_busy());
printf("Jump!\n");
while (UART_busy());
UART_deinit();
new_execute_user_code();
UART_init(UART_RX, UART_TX, 2000000);
printf("This should never happen\n");
while (UART_busy());
for (volatile int i = (1<<18);i;i--);
NVIC_SystemReset();
}
void __aeabi_unwind_cpp_pr0(void){}
void __libc_init_array(void){}
uint32_t get_fattime()
{
#define YEAR 2012
#define MONTH 11
#define DAY 13
#define HOUR 20
#define MINUTE 13
#define SECOND 1
return ((YEAR & 127) << 25) |
((MONTH & 15) << 21) |
((DAY & 31) << 16) |
((HOUR & 31) << 11) |
((MINUTE & 63) << 5) |
((SECOND & 63) << 0);
}
int _write(int fd, const char *buf, int buflen)
{
if (fd < 3)
{
while (UART_cansend() < buflen);
return UART_send((const uint8_t *)buf, buflen);
}
return buflen;
}
void NMI_Handler() {
printf("NMI\n");
for (;;);
}
void HardFault_Handler() {
printf("HardFault\n");
for (;;);
}
void MemManage_Handler() {
printf("MemManage\n");
for (;;);
}
void BusFault_Handler() {
printf("BusFault\n");
for (;;);
}
void UsageFault_Handler() {
printf("UsageFault\n");
for (;;);
}