Dear visitor, welcome to SEGGER Forum. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
typedef struct {
OS_U32 VarA;
OS_U32 VarB;
} T_context_extention;
...
OS_U32 _VarA;
OS_U32 _VarB;
...
static void _SaveConext(void * pStack)
{
T_context_extention* p;
p =((T_context_extention*)pStack) - (1 - OS_STACK_AT_BOTTOM); /* create pointer */
/* save all members of ther structure */
p->VarA = _VarA;
p->VarB = _VarB;
}
static void _RestoreContext(const void * pStack)
{
T_context_extention* p;
p =((T_context_extention*)pStack) - (1 - OS_STACK_AT_BOTTOM); /* creat pointer */
/* save all members of ther structure */
_VarA = p->VarA;
_VarB = p->VarB;
}
|
|
|
C/C++ Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#include "RTOS.h" OS_STACKPTR int StackHP[128], StackLP[128]; /* Task stacks */ OS_TASK TCBHP, TCBLP; /* Task-control-blocks */ OS_U32 _VarA; OS_U32 _VarB; /********************************************************************* * * _Restore * _Save * * Function description * This function pair saves and restores an extended task context. * In this case, the extended task context consists of just a single * member, which is a global variable. */ typedef struct { OS_U32 VarA; OS_U32 VarB; } CONTEXT_EXTENSION; static void _Save(void * pStack) { CONTEXT_EXTENSION * p; p = ((CONTEXT_EXTENSION*)pStack) - (1 - OS_STACK_AT_BOTTOM); // Create pointer to our structure // // Save all members of the structure // p->VarA = _VarA; p->VarB = _VarB; } static void _Restore(const void * pStack) { const CONTEXT_EXTENSION * p; p = ((const CONTEXT_EXTENSION *)pStack) - (1 - OS_STACK_AT_BOTTOM); // Create pointer to our structure // // Restore all members of the structure // _VarA = p->VarA; _VarB = p->VarB; } /********************************************************************* * * Global variable which holds the function pointers * to save and restore the task context. */ const OS_EXTEND_TASK_CONTEXT _SaveRestore = { _Save, _Restore }; /********************************************************************/ /********************************************************************* * * HPTask * * Function description * During the execution of this function, the thread-specific * global variable has always the same value of 1. */ static void HPTask(void) { OS_ExtendTaskContext(&_SaveRestore); _VarA = 1; _VarB = 2; while (1) { OS_Delay (10); } } /********************************************************************* * * LPTask * * Function description * During the execution of this function, the thread-specific * global variable has always the same value of 2. */ static void LPTask(void) { OS_ExtendTaskContext(&_SaveRestore); _VarA = 3; _VarB = 4; while (1) { OS_Delay (50); } } /********************************************************************* * * main */ int main(void) { OS_IncDI(); /* Initially disable interrupts */ OS_InitKern(); /* Initialize OS */ OS_InitHW(); /* Initialize Hardware for OS */ /* You need to create at least one task here ! */ OS_CREATETASK(&TCBHP, "HP Task", HPTask, 100, StackHP); OS_CREATETASK(&TCBLP, "LP Task", LPTask, 50, StackLP); OS_Start(); /* Start multitasking */ return 0; } |
Quoted
...where exactly will be VarA and VarB stored on the stack?
Quoted
Is this the end of the stack or a fixed place in the beginning of the stack?
Quoted
Do I need to tell the OS somehow that there is more stack needed for the context extension?
I use NEC V850 and Greenhills Multi.
Quoted
which embOS (Compiler, CPU) do you use?
I understand the pointer calculation, but how can I be sure that this place on the stack will not be overwritten?
Quoted
No, that's not necessary since we use pointer arithmetic for the stack position calculation:
Quoted
Do I need to tell the OS somehow that there is more stack needed for the context extension?
p = ((CONTEXT_EXTENSION*)pStack) - (1 - OS_STACK_AT_BOTTOM);
.
Quoted
My current understanding is the following (please correct me if I am Wrong):...
Quoted
Is there a possibility in case an interrupt occurs while a save/restore context is in processing that the OS_LeaveInterrupt at the end of the interrupthandler triggers a context change while the current context change is running?