Skip to content

Commit e2e6fe9

Browse files
committed
Pull functionality out into functions, mask upper 16 bits of opcode, and add information about the GOTO instruction
1 parent b290a5f commit e2e6fe9

File tree

1 file changed

+29
-8
lines changed
  • secure_boot_and_secure_firmware_upgrade_over_canfd/app.X

1 file changed

+29
-8
lines changed

secure_boot_and_secure_firmware_upgrade_over_canfd/app.X/main.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
THIS SOFTWARE.
2020
*/
2121
#include "mcc_generated_files/system/system.h"
22-
#include "mcc_generated_files/system/pins.h"
2322
#include "mcc_generated_files/flash/flash.h"
2423
#include "mcc_generated_files/flash/flash_types.h"
2524
#include "mcc_generated_files/boot/boot_config.h"
@@ -28,20 +27,42 @@
2827
Main application
2928
*/
3029

30+
/* The following code finds the address used by GOTO instruction programmed
31+
* at the reset vector in order to determine whether or not the application
32+
* was downloaded directly or via the bootloader (i.e. if the address within the
33+
* GOTO instruction is within the bootloader or application), as the ICSP
34+
* inhibit functionality is only permitted if the application was
35+
* downloaded via the bootloader.
36+
*
37+
* The GOTO instruction contains two 24 bit words, the first of which contains
38+
* the lower 15 bits of the address and the second of which contains the upper
39+
* 7 bits. The following code reads the GOTO address at the reset vector and
40+
* parses the two words in order to determine the reset address.
41+
*
42+
* Refer to https://ww1.microchip.com/downloads/en/DeviceDoc/70000157g.pdf for
43+
* additional details on the GOTO instruction format.
44+
*/
45+
uint32_t GetResetAddress()
46+
{
47+
flash_data_t flashData[2];
48+
FLASH_Read(0x000000, 2, flashData);
49+
return (flashData[1]<<16) | flashData[0];
50+
}
51+
52+
bool WasLoadedByBootloader()
53+
{
54+
return ((GetResetAddress() & 0x0000FFFF) < BOOT_CONFIG_PROGRAMMABLE_ADDRESS_LOW);
55+
}
56+
3157
int main(void) {
3258

3359
SYSTEM_Initialize();
3460

35-
flash_data_t flashData[2];
36-
FLASH_Read(0x000000, 2, flashData);
37-
uint16_t resetAddressLower = flashData[0];
38-
uint32_t combinedResetAddress = (flashData[1]<<16) | resetAddressLower;
39-
4061
while (1)
4162
{
42-
if (combinedResetAddress < BOOT_CONFIG_PROGRAMMABLE_ADDRESS_LOW)
63+
if (WasLoadedByBootloader())
4364
{
44-
// ICSP Feature
65+
// ICSP Inhibit
4566
}
4667
}
4768
}

0 commit comments

Comments
 (0)