Global Sources
EE Times-India
Stay in touch with EE Times India
 
EE Times-India > Embedded
 
 
Embedded  

Make the most out of Cortex-M3's pre-emptive context switches

Posted: 12 Jan 2012     Print Version  Bookmark and Share

Keywords:CM3  SysTick  PendSV 

The ARM Cortex-M3 (CM3) architecture is a 32bit microcontroller core designed to as substitute to many 8bit and 16bit devices by offering faster speeds and advanced system features.

Leveraging these features requires a sound understanding of the CM3 hardware as well as dedicated systems software development. This article explains the CM3 hardware used for pre-emptive context switching as well as how to develop systems software routines that enable multi-tasking programs.

CM3 hardware
The CM3 has dedicated multi-tasking hardware including task-switching interrupts (SysTick and PendSV) and two stack pointers. The SysTick hardware consists of a 24bit timer that triggers an interrupt each time it counts to zero. The PendSV interrupt is a software request, which can manually force a context switch.

Essentially, the SysTick interrupt enables round-robin style scheduling while the PendSV supports FIFO style. The stack pointers for the CM3 include the main stack pointer (MSP) and the process stack pointer (PSP). The MSP is always used when in handler mode (when an interrupt is being serviced) and can optionally be used during regular program execution. The PSP is limited to use during regular program execution. This gives the system designer several options. The ARM Cortex-M3 technical reference manual suggests one configuration:

"For a basic protected thread model, the user threads run in Thread mode using the process stack, and the kernel and the interrupts run privileged using the main stack."

Alternatively, the MSP can be used exclusively for handling interrupts (privileged) while the PSP executes all other execution threads (unprivileged).

To create a pre-emptive, multi-tasking system with the CM3 hardware, the systems designer must design a task table as well as routines for: initializing the switching system, creating new tasks, and handling the context switching interrupts. An entry in the task table can be as simple as the task stack pointer and a set of flags telling the context switcher which tasks to execute.

Typedef struct {
  Void * stack; //Task's stack pointer
  Uint32_t flags; //In Use flag and dynamic execution flag
} task_table_t;

This implementation uses two flags. One flag indicates if an entry is "in use" while the other is a dynamic execution flag. The "in use" flag is helpful when creating tasks to indicate which table entries are available.

The dynamic execution flag allows the context switcher to quickly decide whether or not to execute any given task. If memory is sparse, a NULL stack pointer can be the "in use" flag, and the LSb of the stack pointer can act as the dynamic execution flag (since the stack is word-aligned the bottom two LSb's are not used). Doing this requires additional overhead when switching to ensure the bottom two LSb's are masked properly.

A detailed understanding of the CM3 stacking hardware and register convention is imperative to implementing multi-tasking handling routines. Figure 1 shows the different values assigned to the PSP when switching tasks.

Figure 1: Here are the different values assigned to the PSP when switching tasks.

When an interrupt request is serviced on the CM3, some registers (see "Hardware Stack Frame" in figure 1) are automatically pushed by hardware onto the current stack—in this case, the process stack.

Software must save the remaining general-purpose registers (see "Software Stack Frame"). The following describes the chronological values assigned to the PSP when performing a context switch (reference figure 1):

 • PSP(0): Just before an interrupt request is serviced
 • PSP(1): Just after an interrupt request is serviced
 • PSP(2): After the context switcher saves the necessary registers on the stack

 • PSP(3): After the context switcher reassigns the PSP to a new execution thread
 • PSP(4): After the context switcher loads the last know state of the new thread
 • PSP(5): After the interrupt request returns and execution of the new thread begins/resumes

Context switching routines
Using the above details about the CM3 stacking and registers, the systems designer needs to create just three routines: the context switcher, the system initializer, and the task creator. Figure 2 shows a software flow diagram of each routine.

Figure 2: Software flow diagram of system routines.


1 • 2 Next Page Last Page



Comment on "Make the most out of Cortex-M3's pre..."
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top