Not able to switch Stack pointer to PSP by setting CONTROL[1].

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 By default, SP points to MSP on init.

   

Steps used:

   

    1. Set MSP and PSP registers.

   

    2. Assert SP reset to MSP upon setting.

   

    3. Set CONTROL[1] to switch SP to PSP.

   

    4. Asset CONTROL[1] as "high"

   

 

   

Issue:

   

    SP still points to MSP. SP is not switching to PSP.

   

 

   

Question:

   

Are you aware of such behaviour? if yes, Is there any work around?

0 Likes
10 Replies
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Attaching test project for reference.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

No, I didn't have problems like that.

   

What is the context you are trying to set the CONTROL, what are the exact instruction you use.

   

 

   

ARM Cortex M0 Devices Generic User Guide states:

   

Handler mode always uses the MSP, so the processor ignores explicit writes to the
active stack pointer bit of the CONTROL register when in Handler mode. The
exception entry and return mechanisms update the CONTROL register.

   

Can it be, that you are in handler mode? if so, you'll have to modify on stack the PSP bit.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Posts crossover.

   

As I said, you cannot set CONTROL within a handler, You'll have to modify the stack bit on stack.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob,

   

Thanks for prompt answer!

   

 

   

Info:

   

I am not setting the control bit in handler mode, I am setting it in thread mode. I have attached the project also for reference.

   

 

   

Bit more info on failure: [ I debugged more]

   

Actual issue is not with the setting the control bit, setting control bit is toggling the stack pointer to PSP.

   

But, at the entry / end of each function call, there are some redundant assembly instruction e.g. 

   

[Start]

   

0x0000029C push {r7, lr}

   

0x0000029E sub sp, #8

   

0x000002A0 add r7, sp, #0

   

[End]

   

0x000002BE mov sp, r7

   

0x000002C0 add sp, #8

   

0x000002C2 pop {r7, pc}

   

 

   

So, at the begining of the function call, it saved the stack in R7 register. And at the end it restore back the same. 

   

Hence ultimate result is negligible.

   

Any thought on not to have these instruction?

0 Likes
Anonymous
Not applicable

 ARM CC compiler option: "-fomit-frame-pointer" is the answer for not to generate the frame pointer (R7) assembly code.

   

 

   

Thanks for help!

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I needed to debug your code.

   

You allocated all your variables as local in main. This forces them to be allocated on the stack which you later change in your code. All results are unpredictable.

   

__set_PSP() and __set_MSP() require the top of stack to be supplied as parameter., but you supply the bottom of stack (stack growth is down to zero)

   

Modified project attached.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

... and just for curiosity: What is the overall goal you want to archieve?

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Stil the same issue with your ameded code. Omit frame pointer compiler switch is the answer.

   

 

   

End Goal:

   

Designing / improving RTOS for ARM devices, so wanted to test it for ARM Cortex M0 core.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I already wrote something like an RTOS. Best way to task-swithch is by interrupt (could be software interrupt by SVCALL) what implies to be in handler mode. When restoring environment for a task part of the stored information (the EXC_RETURN value) contains processor mode and which stack to use.

   

So I followed to set up an artificial interrupt stack for a task at initialization and issued a POP PC instruction in handler mode to start that task. (I hope you can follow my suggestions, I'm not english)

   

 

   

Bob

0 Likes
Anonymous
Not applicable

 I have similar implementation for task switching, but different for task management.

   

Bigger goal is to design utilities like file system, graphics lib, CLI, user session manegment etc.

   

I also need to play some audio on PSoC with speaker, if you have any reference please pass-on. Also let me know, if I can port any mp3 or any other format for the same like any song on a toy. Sorry! For off topic.

0 Likes