From 0d425faf6f9633eea6154a50e6c752179f889470 Mon Sep 17 00:00:00 2001 From: Boris Honman Date: Wed, 14 Aug 2019 15:30:41 -0400 Subject: [PATCH] added massive comment in memory.h about how memory is laid out to work with the bootloader. more code cleanup --- .../nbproject/configurations.xml | 6 +- bootloader/src/VectorRemap.asm | 4 +- common_src/memory.h | 93 +++++++++++++++++-- .../nbproject/configurations.xml | 19 ++-- firmware/src/main.c | 2 +- 5 files changed, 99 insertions(+), 25 deletions(-) diff --git a/bootloader/clubdance_v2_bootloader.X/nbproject/configurations.xml b/bootloader/clubdance_v2_bootloader.X/nbproject/configurations.xml index beaeb37..007b3cf 100644 --- a/bootloader/clubdance_v2_bootloader.X/nbproject/configurations.xml +++ b/bootloader/clubdance_v2_bootloader.X/nbproject/configurations.xml @@ -90,7 +90,7 @@ - + @@ -105,7 +105,7 @@ - + @@ -131,7 +131,7 @@ - + diff --git a/bootloader/src/VectorRemap.asm b/bootloader/src/VectorRemap.asm index d3d3324..7576dbf 100644 --- a/bootloader/src/VectorRemap.asm +++ b/bootloader/src/VectorRemap.asm @@ -20,14 +20,14 @@ ;//High priority interrupt vector remapping PSECT HiVector,class=CODE,delta=1,abs org 0x08 - goto 0x2008 ;Resides at 0x0008 (hardware high priority interrupt vector), and causes PC to jump to 0x2008 upon a high priority interrupt event + goto 0x1408 ;Resides at 0x0008 (hardware high priority interrupt vector), and causes PC to jump to application's interrupt vector upon a high priority interrupt event ;//Low priority interrupt vector remapping, as well as bootloader mode absolute ;//entry point (located at 0x001C). PSECT LoVector,class=CODE,delta=1,abs org 0x18 - goto 0x2018 ;Resides at 0x0018 (hardware low priority interrupt vector), and causes PC to jump to 0x2018 upon a low priority interrupt event + goto 0x1418 ;Resides at 0x0018 (hardware low priority interrupt vector), and causes PC to jump to application's interrupt vector upon a low priority interrupt event goto 0x30 ;Resides at 0x001C //Serves as absolute entry point from application program into the bootloader mode diff --git a/common_src/memory.h b/common_src/memory.h index ac9bd80..92d29d6 100644 --- a/common_src/memory.h +++ b/common_src/memory.h @@ -28,21 +28,94 @@ #define MEMORY_ /** - * When changing the location of the application in flash memory these files and settings - * must be updated. - * In bootloader project: - * VectorRemap.asm HiVector goto -> APP_FW_MEMORY_OFFSET + 0x08 - * LoVector goto -> APP_FW_MEMORY_OFFSET + 0x18 - * Configuration > Linker > Memory Model > ROM space -> default,-APP_FW_MEMORY_OFFSET-7FFF - * In application project: - * Configuration > Linker > Memory Model > ROM space -> default,-0-[[APP_FW_MEMORY_OFFSET-1]],-[[APP_FW_MEMORY_OFFSET+6]]-[[APP_FW_MEMORY_OFFSET+7]],-[[APP_FW_MEMORY_OFFSET+0x16]]-[[APP_FW_MEMORY_OFFSET+0x17]] + * ########################################################################################## + * ### Regarding the modified memory layout of program flash to accomodate the bootloader ### + * ########################################################################################## + * + * This firmware implements a bootloader to allow the user to flash firmware over USB without requiring an external + * programmer/debugger such as the ICD or Pickit 3. The bootloader and the application are separate binaries that + * are compiled and linked independently of each other. + * + * The bootloader occupies memory in program flash starting at address 0x0000. The MCU will always start program + * execution at this address. The application occupies program flash memory starting from a predetermined address after + * the end of the memory space occupied by the bootloader through the remainder of the program flash memory. As an + * example let's assume the application starts at address 0x1400. The memory map of the corresponding program flash is + * shown below. + * + * ############################### + * # Memory Map of Program Flash # + * ############################### + * + * Address Description + * ------------------------ + * 0x0000 Bootloader reset vector + * 0x0008 Bootloader interrupt vector (high) + * 0x0018 Bootloader interrupt vector (low) + * ... + * 0x0030 Bootloader absolute entrypoint (application can enter bootloader by jumping to this address) + * ==[Remainder of bootloader code]== + * 0x1400 Application reset vector + * 0x1406 Application signature word** + * 0x1408 Application interrupt vector (high)**** + * 0x1416 Application version word*** + * 0x1418 Application interrupt vector (low)**** + * ==[Remainder of application code]== + * 0x7FFF (Last byte of program flash) + * + * Notes: ** The signature word is written with a known value by the bootloader after it writes the application + * code to flash memory and verifies the data written. If the bootloader does not read this exact value + * at this address at startup it will assume the application code is corrupted and immediately enter + * firmware update mode so valid application code can be flashed. + * *** The version word is stored at this address by the application code at link time. Currently unused + * but I'm leaving the functionality in so the bootloader can report the version of the current + * application. + * **** The bootloader's interrupt vectors will forward interrupts to the application's interrupt vectors + * because they were moved when we offset the application code to 0x1400. + * + * ############################################## + * # Changing the application's memory location # + * ############################################## + * + * 2 source files must be modified, as well as the project configurations for both the bootloader and application + * projects. + * + * # + * # File changes + * # + * File Location Changes + * ------------------------------------------------ + * VectorRemap.asm bootloader * Change address in HiVector goto instruction to address of application + * high priority interrupt vector (ex. 0x1408). + * * Change address in LoVector goto instruction (NOT the goto 0x30) to address + * of application low priority interrupt vector (ex. 0x1418). + * THIS FILE shared * Change APP_FW_MEMORY_OFFSET to the starting address of the application + * (ex. 0x1400) + * + * # + * # BOOTLOADER project configuration changes + * # + * Configuration > Linker > Memory Model > ROM space + * * Add memory range of application as a protected region. Ranges are separated by a single comma. Ranges that + * start with a '-' will be considered protected. Memory addresses specified in hexadecimal. No spaces allowed. + * (ex. 'default,-1400-7FFF') + * + * # + * # APPLICATION project configuration changes + * # + * Configuration > Linker > Memory Model > Additional Options > Code offset + * * Set to APP_FW_MEMORY_OFFSET + * Configuration > Linker > Memory Model > ROM space + * * Add memory ranges for bootloader, application signature word, and application version word as protected + * regions. Ranges are separated by a single comma. Ranges that start with a '-' will be considered protected. + * Memory addresses specified in hexadecimal. No spaces allowed. + * (ex. 'default,-0-13FF,-1406-1407,-1416-1417') */ /** * The base address of the application memory space. The application's reset vector will be * at this address. */ -#define APP_FW_MEMORY_OFFSET 0x2000 +#define APP_FW_MEMORY_OFFSET 0x1400 /** * The address in application memory space that stores the application's version number. @@ -59,7 +132,7 @@ * The absolute address of the bootloader entrypoint. The application can jump to the * bootloader by executing a "goto" instruction with this address. */ -#define BOOTLOADER_ENTRYPOINT 0x001C +#define BOOTLOADER_ENTRYPOINT 0x0030 #endif /* MEMORY_ */ diff --git a/firmware/clubdance_v2.X/nbproject/configurations.xml b/firmware/clubdance_v2.X/nbproject/configurations.xml index 5779212..1fc0fdc 100644 --- a/firmware/clubdance_v2.X/nbproject/configurations.xml +++ b/firmware/clubdance_v2.X/nbproject/configurations.xml @@ -141,7 +141,7 @@ - + @@ -172,7 +172,7 @@ - + @@ -183,7 +183,7 @@ - + @@ -275,7 +275,7 @@ localhost - PIC18F2550 + PIC18F4550 PICkit3PlatformTool @@ -294,9 +294,10 @@ + ../../bootloader/clubdance_v2_bootloader.X/dist/default/production/clubdance_v2_bootloader.X.production.hex false false - + ../../bootloader/clubdance_v2_bootloader.X/dist/default/production/clubdance_v2_bootloader.X.production.hex @@ -318,7 +319,7 @@ + value="..\src;..\..\common_src\framework\usb\inc;..\..\common_src"/> @@ -337,7 +338,7 @@ - + @@ -346,7 +347,7 @@ - + @@ -357,7 +358,7 @@ - + diff --git a/firmware/src/main.c b/firmware/src/main.c index a7eeef2..20492a1 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -31,7 +31,7 @@ #ifdef LINK_FOR_BOOTLOADER // only define this if building fw to be used with the bootloader -const uint16_t VersionWord __at(0x2016) = 0x0100; +const uint16_t VersionWord __at(APP_FW_VERSION_ADDRESS) = 0x0100; #endif void main(void)