Skip to content

Commit 58a46e3

Browse files
committed
Fix MISRA violations and add comments
1 parent 8bb91d6 commit 58a46e3

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed

dspic33a_bootloader_and_firmware_upgrade_demo/boot.X/mdfu/mdfu_firmware_update.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,26 @@ bool MDFU_FirmwareUpdateVerifyImage(void)
9494

9595
void MDFU_FirmwareUpdateStartApplication(void)
9696
{
97+
/*
98+
* Declare a file-scoped, constant, volatile function pointer named
99+
* 'user_application'. This pointer is set to the program memory (flash)
100+
* address defined by MDFU_CONFIG_APPLICATION_RESET_ADDRESS, which is the
101+
* entry point (reset vector) of the user application.
102+
* The 'space(prog)' attribute specifies that the pointer refers to program
103+
* memory (flash), not data memory (RAM).
104+
* The 'noload' attribute instructs the linker not to initialize this
105+
* variable at startup.
106+
*
107+
* By calling 'user_application()', the bootloader transfers execution to the
108+
* user application.
109+
*/
110+
static void (* volatile const user_application)(void) __attribute__((address(MDFU_CONFIG_APPLICATION_RESET_ADDRESS), space(prog), noload));
111+
97112
/* This is probably being replaced at some point by __builtin__setIVTBASE() but is not currently supported */
98113
/* This also assumes that the first block of memory in the application is the reset/ivt table which may not always be true if there is an application header */
99114
PACCON1bits.IVTBASEWR = 1;
100115
IVTBASE = MDFU_CONFIG_APPLICATION_RESET_ADDRESS;
101116
PACCON1bits.IVTBASEWR = 0;
102117

103-
/* cppcheck-suppress misra-c2012-11.4
104-
*
105-
* (Rule 11.4) ADVISORY: A conversion should not be performed between a
106-
* pointer to object and an integer type
107-
*
108-
* Reasoning: This is required for the bootloader to jump to the application
109-
* code. The reset vector is stored at a fixed address, and this cast is
110-
* necessary to read it.
111-
*/
112-
uint32_t resetVector = *((const uint32_t *)MDFU_CONFIG_APPLICATION_RESET_ADDRESS);
113-
114-
/* cppcheck-suppress misra-c2012-11.4
115-
*
116-
* (Rule 11.4) ADVISORY: A conversion should not be performed between a
117-
* pointer to object and an integer type
118-
*
119-
* Reasoning: This is required for the bootloader to jump to the application
120-
* code. The application entry point is stored in the reset vector and must
121-
* be cast to a function pointer.
122-
*/
123-
int (*user_application)(void) = (int (*)(void))resetVector;
124-
125-
(void)user_application();
118+
user_application();
126119
}

dspic33a_secure_boot/boot.X/mdfu/mdfu_partition_executable.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static void CacheInvalidate(void)
210210
*/
211211
static enum MDFU_PARTITION_STATUS Run(void)
212212
{
213-
/* NOTE: Before starting the executable, all interrupts used
213+
/* NOTE: Before starting the executable, all interrupts used
214214
* by the bootloader must be disabled. Add code here to return
215215
* the peripherals/interrupts to the reset state before starting
216216
* the executable code. */
@@ -226,27 +226,20 @@ static enum MDFU_PARTITION_STATUS Run(void)
226226
IVTBASE = MDFU_CONFIG_EXECUTABLE_DATA_ORIGIN;
227227
PACCON1bits.IVTBASEWR = 0;
228228

229-
/* cppcheck-suppress misra-c2012-11.4
230-
*
231-
* (Rule 11.4) ADVISORY: A conversion should not be performed between a
232-
* pointer to object and an integer type
233-
*
234-
* Reasoning: This is required for the bootloader to jump to the executable
235-
* code. The reset vector is stored at a fixed address, and this cast is
236-
* necessary to read it.
237-
*/
238-
uint32_t resetVector = *((const uint32_t *)MDFU_CONFIG_EXECUTABLE_DATA_ORIGIN);
239-
240-
/* cppcheck-suppress misra-c2012-11.4
229+
/*
230+
* Declare a file-scoped, constant, volatile function pointer named
231+
* 'user_executable'. This pointer is set to the program memory (flash)
232+
* address defined by MDFU_CONFIG_EXECUTABLE_DATA_ORIGIN, which is the entry
233+
* point of the executable partition.
234+
* The 'space(prog)' attribute specifies that the pointer refers to program
235+
* memory (flash), not data memory (RAM).
236+
* The 'noload' attribute instructs the linker not to initialize this
237+
* variable at startup.
241238
*
242-
* (Rule 11.4) ADVISORY: A conversion should not be performed between a
243-
* pointer to object and an integer type
244-
*
245-
* Reasoning: This is required for the bootloader to jump to the executable
246-
* code. The executable entry point is stored in the reset vector and must
247-
* be cast to a function pointer.
239+
* By calling 'user_executable()', the bootloader transfers execution to the
240+
* user application.
248241
*/
249-
int (*user_executable)(void) = (int (*)(void))resetVector;
242+
static void (* volatile const user_executable)(void) __attribute__((address(MDFU_CONFIG_EXECUTABLE_DATA_ORIGIN), space(prog), noload));
250243

251244
/* Disable IRT access before transferring control to the executable.
252245
*
@@ -256,8 +249,7 @@ static enum MDFU_PARTITION_STATUS Run(void)
256249
* executable non-IRT sections. The keystore, although classified as IRT, is
257250
* non-executable and thus serves as the recommended buffer. */
258251
IRTCTRLbits.DONE = 1U;
259-
260-
(void)user_executable();
252+
user_executable();
261253

262254
return MDFU_PARTITION_STATUS_OPERATION_FAILED;
263255
}

0 commit comments

Comments
 (0)