Skip to content

Commit 73a2623

Browse files
Wayne RenWayne Ren
authored andcommitted
example: bootloader: optimize the bootloader example
* make it compatible for iotdk * code cleanup Signed-off-by: Wayne Ren <[email protected]>
1 parent 5d25786 commit 73a2623

File tree

3 files changed

+59
-167
lines changed

3 files changed

+59
-167
lines changed

example/baremetal/bootloader/boot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"boot_file": "json.bin",
44
"ram_startaddress": 268435456,
55
"wifi":{
6-
"wifi_sel": "RW009",
6+
"wifi_sel": "PMWIFI",
77
"SSID": "embARC",
88
"PSK": "qazwsxedc",
99
"DHCP": true,

example/baremetal/bootloader/main.c

Lines changed: 51 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -28,151 +28,26 @@
2828
*
2929
--------------------------------------------- */
3030

31-
/**
32-
* \defgroup EMBARC_APP_BAREMETAL_BOOTLOADER embARC Secondary Bootloader Example
33-
* \ingroup EMBARC_APPS_TOTAL
34-
* \ingroup EMBARC_APPS_BAREMETAL
35-
* \ingroup EMBARC_APPS_MID_NTSHELL
36-
* \ingroup EMBARC_APPS_MID_FATFS
37-
* \ingroup EMBARC_APPS_MID_PARSON
38-
* \ingroup EMBARC_APPS_MID_IHEX
39-
* \brief embARC example for secondary bootloader
40-
*
41-
* \details
42-
* ### Extra Required Tools
43-
*
44-
* ### Extra Required Peripherals
45-
* - SDCard with boot binary(boot.hex or boot.bin)
46-
*
47-
* ### Design Concept
48-
* This example is designed to work as a secondary bootloader for embARC, it will load boot.hex or boot.bin on SDCard and run that program.
49-
* And this example itself can be used as ntshell application.
50-
*
51-
* ### Usage Manual
52-
* As shown in the following picture, when the EMSK configuration in SPI flash is loaded into the FPGA,
53-
* a simple primary bootloader is also loaded in ICCM. Through the primary bootloader, the application or secondary bootloader can be
54-
* loaded into external memory (DDR memory), bootloader start address is 0x17F00004, ram address is 0x17F00000.
55-
*
56-
* For EMSK1.x, bootloader core configuration must be arcem6, for EMSK2.x, bootloader core configuration must be arcem7d.
57-
* For EMSK 1.0, it can be upgraded to 1.1 by 1.1's firmware. For EMSK 2.0/2.1/2.2, it can be upgraded to 2.3 by 2.3's firmware.
58-
*
59-
* \htmlonly
60-
* <div class="imagebox">
61-
* <div style="width: 500px">
62-
* <img src="pic/bootloader.jpg" alt="Secondary Bootloader"/>
63-
* <p>Secondary Bootloader</p>
64-
* </div>
65-
* </div>
66-
* \endhtmlonly
67-
*
68-
* Here are steps for how to program the secondary bootloader application into onboard SPIFlash(Take EMSK2.3 - ARC EM7D as example) and automatically load and run *boot.hex* or *boot.bin* in SDCard.
69-
* - Generate a secondary bootloader binary file
70-
* + cd <embARC>/example/baremetal/bootloader
71-
* + Generate binary file: make TOOLCHAIN=gnu BD_VER=23 CUR_CORE=arcem7d bin
72-
* - If the binary file is generated successfully, you will output as follows:
73-
* \code
74-
* "Compiling : " ../../../middleware/ntshell/cmds/cmds_fs/ymodem/ymodemio.c
75-
* "Compiling : " ../../../middleware/ntshell/port/ntshell_usrcmd.c
76-
* "Compiling : " ../../../middleware/ntshell/port/ntshell_task.c
77-
* "Archiving : " obj_emsk_23/gnu_arcem7d/libmidntshell.a
78-
* "Compiling : " ../../../middleware/parson/parson.c
79-
* "Archiving : " obj_emsk_23/gnu_arcem7d/libmidparson.a
80-
* "Compiling : " ../../../arc/arc_timer.c
81-
* "Compiling : " ../../../arc/arc_cache.c
82-
* "Compiling : " ../../../arc/arc_exception.c
83-
* "Compiling : " ../../../arc/arc_udma.c
84-
* "Assembling : " ../../../arc/arc_exc_asm.s
85-
* "Archiving : " obj_emsk_23/gnu_arcem7d/libcpuarc.a
86-
* "Compiling : " ../../../library/clib/fatfs_dirent.c
87-
* "Compiling : " ../../../library/clib/malloc.c
88-
* "Compiling : " ../../../library/clib/embARC_sbrk.c
89-
* "Compiling : " ../../../library/clib/embARC_misc.c
90-
* "Compiling : " ../../../library/clib/embARC_syscalls.c
91-
* "Compiling : " ../../../library/clib/ya_getopt.c
92-
* "Compiling : " ../../../library/clib/embARC_target.c
93-
* "Archiving : " obj_emsk_23/gnu_arcem7d/liblibclib.a
94-
* "Archiving : " obj_emsk_23/gnu_arcem7d/libembarc.a
95-
* "Linking : " obj_emsk_23/gnu_arcem7d/emsk_bootloader_gnu_arcem7d.elf
96-
* "Generating Binary obj_emsk_23/gnu_arcem7d/emsk_bootloader_gnu_arcem7d.bin"
97-
* \endcode
98-
* - Program generated secondary bootloader binary file into SPIFlash
99-
* + Insert SDCard to your PC, and copy the binary file *obj_emsk_23/gnu_arcem7d/emsk_bootloader_gnu_arcem7d.bin* to SDCard Root, and rename it to *em7d_2bt.bin*
100-
* + Insert the SDCard to EMSK Board, please choose the right core configuration, build and run the <em><embARC>/example/baremetal/bootloader</em> example,
101-
* then press any button to stop auto boot process, and enter to ntshell command mode.
102-
* + Then use ntshell command *spirw* to program the *em7d_2bt.bin* into spiflash.
103-
* - Firstly, run *spirw* to show help
104-
* - Secondly, run *spirw -i* to check SPIFlash ID, it should be **Device ID = ef4018**
105-
* - Thirdly, run *spirw -w em7d_2bt.bin 0x17f00000 0x17f00004* to program spiflash
106-
* - Check the output message to see if if was programmed successfully.
107-
* - ![ScreenShot of program secondary bootloader to spiflash](pic/images/example/emsk/emsk_bootloader_program2splflash.jpg)
108-
* + If programmed successfully, when the board is reset, make sure Bit 4 of the onboard DIP switch is ON to enable secondary bootloader run.
109-
* + If the sdcard already contains the *boot.bin* in it, the bootloader will automatically load it from sdcard, if not, it will enter to ntshell mode.
110-
* + You can goto the next step to generate the *boot.bin* for proper application you want to be auto-loaded in sdcard.
111-
* ![ScreenShot of secondary bootloader autoboot when board configuration is reloaded](pic/images/example/emsk/emsk_bootloader_onspiflash.jpg)
112-
* - Generate *boot.bin* using any embARC example which ram start address should be 0x10000000 and use bootloader to run it
113-
* + Here take <em><embARC>/example/freertos/kernel</em> for example
114-
* + cd <embARC>/example/freertos/kernel
115-
* + Build and generate binary file: *make TOOLCHAIN=gnu BD_VER=23 CUR_CORE=arcem7d bin*
116-
* + Insert SDCard to PC, and copy generated binary file *obj_emsk_23/gnu_arcem7d/freertos_kernel_gnu_arcem7d.bin* to SDCard Root, and rename it to boot.bin
117-
* + Insert SDCard back to EMSK, make sure bit 4 of DIP Switch is ON, and press re-configure button above letter **C**, and wait for autoload.
118-
* + ![ScreenShot of secondary bootloader auto load boot.bin](pic/images/example/emsk/emsk_bootloader_loadbootbin.jpg)
119-
* - Know Issues
120-
* + Bootrom of EMSK1.x is not able to load secondary bootloader on SPIFlash, you need a modified EMSK1.x mcs file to enable this function, please send request in forum about this mcs file.
121-
*
122-
* **The secondary bootloader is a complement of the primary bootloader, and provides the following functions:**
123-
* - File operations on SD card
124-
* - Operations on the EMSK, GPIO, I2C, SPI flash
125-
* - Operations on ARC processors
126-
* - Automatic boot from SD card, using following instructions:
127-
* + burn the bin file of bootloader into EMSK spiflash using spirw command <b>spirw -w bootloader.bin 0x17f00000 0x17f00004 </b> with the help of JTAG
128-
* + the primary bootloader should be able to load the secondary bootloader
129-
* + put the file you want to boot in the root directory of SD card, name it boot.bin
130-
* + plug in SD card
131-
* - LED Status of loading application(boot.bin)
132-
* + Start to load application: LED on board -> 0x0F
133-
* + Load application finished: LED on board -> 0xFF, if application is running normally, LED will quickly change to 0x0
134-
* + Load application failed: LED on board -> 0xAA
135-
* + Skip loading application, and enter to NTShell runtime: LED on board -> 0x0
136-
* - Type *help* command in ntshell to show the list of supported commands.
137-
*
138-
* ![ScreenShot of bootloader under baremetal](pic/images/example/emsk/emsk_bootloader.jpg)
139-
*
140-
* ### Extra Comments
141-
* - Bootrom of EMSK1.x is not able to load secondary bootloader on SPIFlash, you need a modified EMSK1.x mcs file to enable this function, please send request in forum about this mcs file.
142-
* - Make sure that the bootloader can only load application for extract the same core you built for.
143-
* - Currently the 2nd bootloader ran in a different place(end of DDR) in DDR which has no conflict with other normal examples placed at start of DDR.
144-
* - Bootloader example only support arc core configuration which has DDR with cache enabled.
145-
* - Bootloader example runs in DDR ram, not in CCM.
146-
* - Bootloader example itself can be treated as a ntshell example, you can run some ntshell commands using this example.
147-
* - The bootloader example is also built optimized for the ARC core you selected, so the example built for one specified core may not run sucessfully for another core configuration.
148-
* - Supported core configurations
149-
* + EMSK 1.1: EM6, EM6GP
150-
* + EMSK 2.2/2.3: EM7D, EM11D
151-
*/
152-
153-
/**
154-
* \file
155-
* \ingroup EMBARC_APP_BAREMETAL_BOOTLOADER
156-
* \brief example of secondary bootloader after the bootrom of EMSK
157-
* \todo
158-
*/
159-
160-
/**
161-
* \addtogroup EMBARC_APP_BAREMETAL_BOOTLOADER
162-
* @{
163-
*/
164-
16531
#include "embARC.h"
16632
#include "embARC_debug.h"
16733

16834
#include "ntshell_common.h"
16935
#include "ihex_load.h"
17036
#include "parson.h"
17137

38+
#include "target_mem_config.h"
39+
17240
#define BOOT_CFG_FILE_NAME "boot.json"
17341
#define BOOT_FILE_NAME "0:\\boot.bin" /*!< default autoload full file name */
42+
43+
#if defined(BOARD_EMSK)
17444
#define RAM_STARTADDRESS 0x10000000 /*!< default ram start address of boot.bin */
17545
#define APP_CFG_ADDR 0x17f00000 /*!< save the app configuration file name */
46+
#elif defined(BOARD_IOTDK)
47+
#define RAM_STARTADDRESS EXT_RAM_START
48+
#define APP_CFG_ADDR ARC_X_MEM_START
49+
#endif
50+
17651
#define PROMT_DELAY_S (5) /*!< default wait time for autoload */
17752

17853
typedef int (*fp_t)(void);
@@ -190,11 +65,11 @@ struct wifi_cfg_t {
19065
};
19166

19267
struct boot_cfg_t {
193-
uint8_t ntshell;
194-
const char *boot_file;
195-
uint32_t ram_startaddress;
196-
struct wifi_cfg_t *wifi;
197-
const char *app_cfg;
68+
uint8_t ntshell;
69+
const char *boot_file;
70+
uint32_t ram_startaddress;
71+
struct wifi_cfg_t *wifi;
72+
const char *app_cfg;
19873
};
19974

20075
struct wifi_cfg_t wifi_cfg;
@@ -229,48 +104,60 @@ int main(void)
229104
boot_cfg.wifi = &wifi_cfg;
230105
//read the json to boot_json;
231106
res = f_open(&file, BOOT_CFG_FILE_NAME, FA_READ | FA_OPEN_EXISTING);
107+
232108
if (res) {
233109
EMBARC_PRINTF("%s open error. use default bootloader\r\n", BOOT_CFG_FILE_NAME);
234110
default_bt_flag = 1;
235111
} else {
236-
f_read(&file, boot_json, file.fsize, &cnt);
112+
res = f_read(&file, boot_json, file.fsize, &cnt);
113+
if (res) {
114+
EMBARC_PRINTF("read boot cfg file error\r\n");
115+
default_bt_flag = 1;
116+
}
237117
f_close(&file);
238-
// EMBARC_PRINTF("fsize:%d\tcnt:%d\n", file.fsize, cnt);
118+
}
119+
120+
if (default_bt_flag == 0) {
239121
JSON_Value *user_data = json_parse_string((const char *)boot_json);
240-
if(user_data != NULL){
122+
if (user_data != NULL) {
241123
/*parse the boot.json*/
242124
const char *str;
243125
str = json_object_get_string(json_object(user_data), "boot_file");
244-
if(str != NULL){
126+
if (str != NULL) {
245127
boot_cfg.boot_file = str;
246128
}
247129
str = json_object_get_string(json_object(user_data), "app_cfg");
248-
if(str != NULL){
130+
if (str != NULL) {
249131
boot_cfg.app_cfg = str;
250132
}
133+
251134
uint32_t num;
252135
num = json_object_get_number(json_object(user_data), "ram_startaddress");
253-
if(num != 0){
136+
if (num != 0) {
254137
boot_cfg.ram_startaddress = num;
255138
}
139+
256140
int32_t boo;
257141
boo = json_object_get_boolean(json_object(user_data), "ntshell");
258-
if(boo != -1){
142+
if (boo != -1) {
259143
boot_cfg.ntshell = boo;
260144
}
145+
261146
JSON_Object *obj;
262147
obj = json_object_get_object(json_object(user_data), "wifi");
263-
if(obj != NULL){
148+
if (obj != NULL) {
264149
/*TODO: parse wifi setting*/
265150
}
151+
152+
EMBARC_PRINTF("boot_file:%s\napp_cfg:%s\nram:0x%x\nntshell:%d", \
153+
boot_cfg.boot_file, boot_cfg.app_cfg, boot_cfg.ram_startaddress, boot_cfg.ntshell);
266154
} else {
267155
EMBARC_PRINTF("Cannot parse boot.json, please check it. Now use default bootloader\n");
268156
default_bt_flag = 1;
269157
}
270-
EMBARC_PRINTF("boot_file:%s\napp_cfg:%s\nram:0x%x\nntshell:%d", \
271-
boot_cfg.boot_file, boot_cfg.app_cfg, boot_cfg.ram_startaddress, boot_cfg.ntshell);
272158
}
273-
if(default_bt_flag == 1){
159+
160+
if (default_bt_flag == 1) {
274161
EMBARC_PRINTF("\r\nPress any button on board to stop auto boot in %d s\r\n", PROMT_DELAY_S);
275162
cur_ms = OSP_GET_CUR_MS();
276163
do {
@@ -285,51 +172,52 @@ int main(void)
285172
}
286173
} while((OSP_GET_CUR_MS() - cur_ms) < max_promt_ms);
287174
}
288-
if(default_bt_flag == 0 && boot_cfg.ntshell == 1 || load_flag == 0){
175+
176+
if (default_bt_flag == 0 && boot_cfg.ntshell == 1 || load_flag == 0) {
289177
EMBARC_PRINTF("\r\nStart NTShell Runtime...\r\n");
290-
led_write(0x00, 0xFF); /* Load application finished */
178+
led_write(0x00, 0xFF);
291179
nt_io = get_ntshell_io(BOARD_ONBOARD_NTSHELL_ID);
292180
/** enter ntshell command routine no return */
293181
ntshell_task((void *)nt_io);
294182
}
183+
295184
EMBARC_PRINTF("\r\nStart loading %s from sdcard to 0x%x and run...\r\n", boot_cfg.boot_file, boot_cfg.ram_startaddress);
296185
led_write(0xF, 0xFF); /* Start to load application */
297-
if(boot_cfg.boot_file[strlen(boot_cfg.boot_file) - 3] == 'h'){
186+
if (boot_cfg.boot_file[strlen(boot_cfg.boot_file) - 3] == 'h') {
298187
ihex_start();
299188
res = ihex_load(IHEX_SD_CARD, (void *)boot_cfg.boot_file);
300-
if (res != IHEX_END){
189+
if (res != IHEX_END) {
301190
EMBARC_PRINTF("%s open or read error\r\n", boot_cfg.boot_file);
302191
load_flag = 0;
303192
}
304-
}else if(boot_cfg.boot_file[strlen(boot_cfg.boot_file) - 3] == 'b'){
193+
} else if (boot_cfg.boot_file[strlen(boot_cfg.boot_file) - 3] == 'b') {
305194
res = f_open(&file, boot_cfg.boot_file, FA_READ | FA_OPEN_EXISTING);
306195
if (res) {
307196
EMBARC_PRINTF("%s open error\r\n", boot_cfg.boot_file);
308197
load_flag = 0;
309-
}else{
198+
} else {
310199
ram = (void *)boot_cfg.ram_startaddress;
311200
res = f_read(&file, ram, file.fsize, &cnt);
312201
f_close(&file);
313-
if (res || ((uint32_t)cnt != file.fsize))
314-
{
202+
if (res || ((uint32_t)cnt != file.fsize)) {
315203
EMBARC_PRINTF("%s read error\r\n", boot_cfg.boot_file);
316204
load_flag = 0;
317205
}
318206
}
319-
}else{
207+
} else {
320208
EMBARC_PRINTF("%s unknown file type\r\n", boot_cfg.boot_file);
321209
load_flag = 0;
322210
}
323-
if(load_flag == 0){
211+
212+
if (load_flag == 0) {
324213
EMBARC_PRINTF("\r\nStart NTShell Runtime...\r\n");
325-
led_write(0x00, 0xFF); /* Load application finished */
214+
led_write(0x00, 0xFF);
326215
nt_io = get_ntshell_io(BOARD_ONBOARD_NTSHELL_ID);
327216
/** enter ntshell command routine no return */
328217
ntshell_task((void *)nt_io);
329218
}
330-
331-
332219
led_write(0xF0, 0xFF); /* Load application finished */
220+
333221
ram = (void *)APP_CFG_ADDR;
334222
memcpy(ram, boot_cfg.app_cfg, strlen(boot_cfg.app_cfg) + 1);
335223
cpu_lock();
@@ -351,5 +239,3 @@ int main(void)
351239
fp(); /* jump to program */
352240
return E_SYS;
353241
}
354-
355-
/** @} */

example/baremetal/bootloader/makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Application name
2-
APPL ?= emsk_bootloader
2+
APPL ?= simple_bootloader
33

4+
BOARD ?= emsk
5+
6+
ifeq ($(BOARD), emsk)
47
EXT_DEV_LIST += sensor/temperature/adt7420
8+
endif
59

10+
# 8 KB stack
11+
STACKSZ = 8192
612
##
713
# link script
814
#

0 commit comments

Comments
 (0)