Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cortex-M7 : Is _tx_thread_context_save/_tx_thread_context_restore required in interrupts? #381

Open
maxkunes opened this issue Apr 2, 2024 · 7 comments

Comments

@maxkunes
Copy link

maxkunes commented Apr 2, 2024

The ThreadX docs under Chapter 3 - Functional Components: ISR Template suggest that context saving and restoring must be done in assembly before executing any ThreadX code (i.e. notifying semaphores) or calling user code.

Is this required on Cortex-M7? Or is this only needed on specific architectures?

@maxkunes maxkunes changed the title Cortex-M7 : Is _tx_thread_context_save/_tx_thread_context_restore required? Cortex-M7 : Is _tx_thread_context_save/_tx_thread_context_restore required in interrupts? Apr 2, 2024
@rayc345
Copy link

rayc345 commented May 16, 2024

Not manually, For Armv7-M ISA based CPU like Cortex-M7, caller save registers (r0-r3,r12,LR(r14),PC(r15), xPSR) would be saved into stack by hardware, and r4-r9, r10,r11,SP would be saved by callee functions, save and restore instructions would be generated by compilers, so for C programmers, no manual procedure is required to save/restore contexts.

@amgross
Copy link

amgross commented May 16, 2024

Hi @rayc345
Threadx context save/restore not only saves register, but also manages the option that while the interrupt happened something that requires context switch. For this case stuff needed to be stored on the stack and context restore returns from the interrupt to the scheduler instead of to the original point where the interrupt was called from.
And hence I think the answer to @maxkunes is that indeed it should be called from assembly code.

@maxkunes
Copy link
Author

@amgross Thank you for the reply.

Honestly, I've been working with ThreadX for 3 years now without doing this, and I've yet to see anything that is obviously wrong.
Can you explain what behavior might be seen in theory without these calls? Maybe I have some intermittent bugs due to this that I haven't connected to anything else.

@amgross
Copy link

amgross commented May 19, 2024

Hi @maxkunes ,
By 'without doing it' you mean you call it from C code or not calling it at all?

@maxkunes
Copy link
Author

maxkunes commented May 19, 2024 via email

@amgross
Copy link

amgross commented May 19, 2024

Surprisingly (for me), it seems like I was mistaken and there is no need for cortex M to use save/restore

6.2 Managed Interrupts
ISRs for Cortex-M can be written completely in C (or assembly language) without any
calls to _tx_thread_context_save or _tx_thread_context_restore. These ISRs are allowed
access to the ThreadX API that is available to ISRs.
ISRs written in C will take the form (where "your_C_isr" is an entry in the vector table):
void your_C_isr(void)
{
/* ISR processing goes here, including any needed function calls. */
}

So @rayc345 is correct

@maxkunes
Copy link
Author

maxkunes commented May 19, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants