-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
290 lines (240 loc) · 7.07 KB
/
main.c
File metadata and controls
290 lines (240 loc) · 7.07 KB
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
/*****************************************************************************
* *
* 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 "lpc177x_8x_wwdt.h"
#ifndef DEBUG_MESSAGES
#define printf(...) do {} while (0)
#endif
/***************************************/
/* PINs */
/***************************************/
void config_pins(void)
{
//Config DFU_BTN
GPIO_init(DFU_BTN); GPIO_input(DFU_BTN);
//Config LEDs
GPIO_init(LED1); GPIO_output(LED1);
GPIO_init(LED2); GPIO_output(LED2);
GPIO_init(LED3); GPIO_output(LED3);
GPIO_init(LED4); GPIO_output(LED4);
//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);
}
/***************************************/
/* Leds */
/***************************************/
void setleds(int leds)
{
GPIO_write(LED1, leds & 1);
GPIO_write(LED2, leds & 2);
GPIO_write(LED3, leds & 4);
GPIO_write(LED4, leds & 8);
}
/***************************************/
/* DFU firmware */
/***************************************/
int dfu_btn_pressed(void)
{
return GPIO_get(DFU_BTN);
}
void start_dfu(void)
{
DFU_init();
usb_init();
usb_connect();
while (DFU_complete() == 0)
usb_task();
usb_disconnect();
}
//Initialize dfu and check is enter dfu mode
void check_dfu_firmware(void)
{
int dfu = 0;
if (dfu_btn_pressed() == 0)
{
printf("ISP button pressed, entering DFU mode\r\n");
dfu = 1;
}
else if (WWDT_GetStatus(WWDT_TIMEOUT_FLAG))
{
WWDT_ClrTimeOutFlag();
printf("WATCHDOG reset, entering DFU mode\r\n");
dfu = 1;
}
else if (*(uint32_t *)USER_FLASH_START == 0xFFFFFFFF)
{
printf("User flash empty, enabling DFU\r\n");
dfu = 1;
}
if (dfu) start_dfu();
}
/***************************************/
/* SD card firmware */
/***************************************/
void check_sd_firmware(void)
{
const char *firmware_file = "firmware.bin";
const char *firmware_old = "firmware.cur";
static FATFS fat;
static FIL file;
printf("Initialize SD card\r\n");
SDCard_Init();
if (SDCard_disk_initialize()) return;
printf("Check SD card\r\n");
f_mount(0, &fat);
int res = f_open(&file, firmware_file, FA_READ);
if (res == FR_OK)
{
printf("Flashing firmware: Start!\r\n");
static uint8_t readbuf[512];
unsigned int readsize = sizeof(readbuf);
uint32_t address = USER_FLASH_START;
while (readsize == sizeof(readbuf))
{
if (f_read(&file, readbuf, sizeof(readbuf), &readsize) != FR_OK)
{
f_close(&file);
return;
}
printf("address: 0x%x, readsize: %d\r\n", address, readsize);
setleds((address - USER_FLASH_START) >> 15);
write_flash((void *)address, (char *)readbuf, sizeof(readbuf));
address += readsize;
}
f_close(&file);
if (address > USER_FLASH_START)
{
printf("Flashing firmware: Complete!\r\n");
res = f_unlink(firmware_old);
res = f_rename(firmware_file, firmware_old);
}
}
}
// this seems to fix an issue with handoff after poweroff
// found here http://knowledgebase.nxp.trimm.net/showthread.php?t=2869
typedef void __attribute__((noreturn))(*exec)();
static void boot(uint32_t a)
{
uint32_t *start;
__set_MSP(*(uint32_t *)USER_FLASH_START);
start = (uint32_t *)(USER_FLASH_START + 4);
((exec)(*start))();
}
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;
// relocate vector table
SCB->VTOR = (addr & 0x1FFFFF80);
delay_loop(1000);
// reset pipeline, sync bus and memory access
__asm (
"dmb\n"
"dsb\n"
"isb\n"
);
boot(addr);
}
int main(void)
{
WWDT_Feed();
//Configure pins
config_pins();
//Turn on all leds
setleds(0xf);
//Initialize uart
Uart_Init(APPBAUD);
printf("Bootloader Start\r\n");
//Initialize sd card and check sd firmware
check_sd_firmware();
//Initialize dfu and check dfu firmware
check_dfu_firmware();
//Initialize watchdog
#ifdef WATCHDOG
WWDT_Init(WDT_CLKSRC_IRC, WDT_MODE_RESET);
WWDT_Start(1<<22);
#endif
//Jump to application
printf("Jump!\r\n");
new_execute_user_code();
Uart_Init(APPBAUD);
printf("This should never happen\r\n");
printf("Reset system\r\n");
NVIC_SystemReset();
}
//fatfs get_fattime
DWORD get_fattime(void)
{
#define YEAR 2012
#define MONTH 11
#define DAY 13
#define HOUR 20
#define MINUTE 13
#define SECOND 1
return (((DWORD)YEAR & 127) << 25) |
(((DWORD)MONTH & 15) << 21) |
(((DWORD)DAY & 31) << 16) |
(((DWORD)HOUR & 31) << 11) |
(((DWORD)MINUTE & 63) << 5) |
(((DWORD)SECOND & 63) << 0);
}
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 (;;);
}