You copied the Doc URL to your clipboard.

How do I configure Cortex-M processors before jumping from IAP to application in terms of exception handling?

Article ID: 162433230

Published date: 13 Feb 2018

Last updated: -

Applies to: M-profile

Problem/Question

How do I configure Cortex-M processors before jumping from IAP to application in terms of exception handling?

Scenario

N/A

Answer

Before jumping to user code, the In-system Application Programming (IAP) might involve exception handling. You must clean the working environment to a condition similar to the reset status.

To clean the working environment, perform the following steps:

  1. Disable the interrupt response.

  2. Disable all the enabled interrupts in the Nested Vectored Interrupt Controller (NVIC).

  3. Disable all the enabled peripherals which might generate interrupt requests.

    You must also clear:

    • All the pending interrupt flags in those peripherals.

    • All the pending interrupt requests in NVIC.

  4. Disable SysTick and clear its exception pending bit.

  5. Load the vector table address of user application code into Vector Table Offset Register (VTOR).

  6. Use the Main Stack Pointer (MSP) as the current stack pointer (SP) and load its value from the vector table used by the application.

    Note: In some cases when the process stack pointer (PSP) is used, simply loading the MSP with the user main stack pointer stored in the vector table does not work. If the system is working with unprivileged accesses, you must change the project configuration to use privileged accesses.

  7. Enable the interrupt response.

After performing these steps, you are free to jump to user code for exception handling.

Pseudocode example

The following pseudocode is given for your reference:

//! 1. Disable interrupt response.

__disable_irq();

//! 2. Disable all enabled interrupts in NVIC.

memset((uint32_t *)NVIC->ICER, 0xFF, sizeof(NVIC->ICER));

/*! 3. Disable all enabled peripherals which might generate interrupt requests.

Clear all pending interrupt flags in those peripherals.

This part is device-dependent, and you can write it by referring to device datasheet.

*/

/*! Clear all pending interrupt requests in NVIC.

*/

memset((uint32_t *)NVIC->ICPR, 0xFF, sizeof(NVIC->ICPR));

//! 4. Disable SysTick and clear its exception pending bit.

SysTick->CTRL = 0;

SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;

//! 5. Load the vector table address of user application code in to VTOR.

SCB->VTOR = USER_APPLICATION_VECTOR_TABLE_ADDRESS;

//! 6. Use the MSP as the current SP.

//! Set the MSP with the value from the vector table used by the application.

__set_MSP( ((unsigned int *)(SCB->VTOR))[0] );

//! In thread mode, enable privileged access and use the MSP as the current SP.

__set_CONTROL( 0 );

//! 7. Enable interrupts.

__enable_irq();

//! 8. The reset handler address could be found with following expression

//! ((unsigned int *)(SCB->VTOR))[1]

Workaround

N/A

Example

N/A

Was this page helpful? Yes No