Choco OS  V.0.16.9.0
Join to the chocolate world
MCS - Machine Core Specific Module

Module with machine core specific definitions. More...

Data Structures

struct  oC_StackData_t
 stores stack data More...
 
struct  oC_MCS_MemoryRegionConfig_t
 configuration structure for the memory region More...
 

Macros

#define oC_MCS_IsStackPushDecrementPointer()
 returns true if stack push decrements SP More...
 
#define oC_MCS_AlignStackPointer(Pointer)
 align pointer to the stack alignment More...
 
#define oC_MCS_AlignPointer(Pointer)
 align pointer to the machine alignment More...
 
#define oC_MCS_AlignSize(Size, Alignment)
 align size to the machine alignment More...
 
#define oC_MCS_IsStackPointerAligned(Pointer)
 checks if stack pointer is aligned to the machine stack memory alignment
 
#define oC_MCS_IsPointerAligned(Pointer)
 checks if pointer is aligned to the machine memory alignment
 

Typedefs

typedef void(* oC_ContextHandler_t) (void *ContextParameter)
 stores handler of function, that handles context More...
 
typedef void(* oC_ContextExitHandler_t) (void)
 stores handler of function called, when context handling is finished More...
 
typedef oC_StackData_toC_Stack_t
 type for storing stack reference More...
 
typedef void(* oC_FindNextStackHandler_t) (void)
 stores pointer to function for searching next stack More...
 

Enumerations

Functions

bool oC_MCS_InitializeModule (void)
 initializes module to work More...
 
void * oC_MCS_GetHardFaultReason (void)
 returns address that cause a hard fault More...
 
void oC_MCS_EnableInterrupts (void)
 Globally enables interrupts (always) More...
 
void oC_MCS_DisableInterrupts (void)
 Globally disables interrupts (always) More...
 
bool oC_MCS_AreInterruptsEnabled (void)
 Checks if interrupts are enabled in HW. More...
 
bool oC_MCS_EnableInterrupt (IRQn_Type InterruptNumber)
 enables interrupt with specified number More...
 
bool oC_MCS_DisableInterrupt (IRQn_Type InterruptNumber)
 disables interrupt with specified number More...
 
bool oC_MCS_IsInterruptEnabled (IRQn_Type InterruptNumber)
 checks if interrupt is enabled More...
 
bool oC_MCS_SetInterruptPriority (IRQn_Type InterruptNumber, oC_InterruptPriotity_t Priority)
 sets interrupt priority More...
 
oC_InterruptPriotity_t oC_MCS_GetInterruptPriority (IRQn_Type InterruptNumber)
 returns interrupt priority More...
 
void oC_MCS_Reboot (void)
 Software reboots of machine. More...
 
bool oC_MCS_InitializeStack (oC_Stack_t *outStack, void *Buffer, oC_Int_t BufferSize, oC_ContextHandler_t ContextHandler, void *HandlerParameter, oC_ContextExitHandler_t ExitHandler)
 initializes stack for the system More...
 
void * oC_MCS_GetCurrentProcessStackPointer (void)
 returns current value of PSP More...
 
void * oC_MCS_GetCurrentMainStackPointer (void)
 returns current value of MSP More...
 
oC_Int_t oC_MCS_GetStackSize (oC_Stack_t Stack)
 returns size of stack More...
 
oC_Int_t oC_MCS_GetFreeStackSize (oC_Stack_t Stack)
 returns number of free stack More...
 
oC_Stack_t oC_MCS_GetCurrentStack (void)
 returns current stack More...
 
oC_Int_t oC_MCS_GetMinimumStackBufferSize (oC_Int_t StackSize)
 returns minimum stack buffer size More...
 
bool oC_MCS_SetNextStack (oC_Stack_t Stack)
 sets next stack for context switching More...
 
bool oC_MCS_ConfigureSystemTimer (oC_UInt_t Prescaler, oC_FindNextStackHandler_t FindNextStackHandler)
 configures system timer More...
 
oC_Stack_t oC_MCS_GetSystemStack (void)
 returns pointer to the system stack More...
 
bool oC_MCS_ReturnToSystemStack (void)
 sets next stack as system stack More...
 
void oC_MCS_Delay (register oC_UInt_t Cycles)
 delays operations for cycles More...
 
oC_ErrorCode_t oC_MCS_ConfigureMemoryRegion (const oC_MCS_MemoryRegionConfig_t *Config)
 configures memory region More...
 
uint32_t oC_MCS_GetMaximumNumberOfRegions (void)
 returns maximum number of regions handled by the machine More...
 
uint32_t oC_MCS_GetNumberOfRegions (void)
 returns number of regions that are currently configured More...
 
bool oC_MCS_ReadFreeRegionNumber (uint32_t *outFreeRegionNumber)
 reads number of a free region More...
 
bool oC_MCS_SetMemoryAccessMode (oC_MCS_MemoryAccessMode_t Mode)
 sets the memory access mode More...
 
oC_MCS_MemoryAccessMode_t oC_MCS_GetMemoryAccessMode (void)
 reads memory access mode More...
 

Detailed Description

This module is part of the Portable Space

The module MCS (Machine Core Specific) contains interface, that is specific for machine core. This is to not duplicate a code, that is common for all machines, that are based on the same core, for example uC of TI from the LM4F family are based on ARM Cortex M4, and the uC from ST in STM32F4 family are also based on the same core. We don't want to keep it separately so here is the module, that is designed for all machines based on the same core.

Macro Definition Documentation

#define oC_MCS_AlignPointer (   Pointer)

The macro is for alignment of pointer

See also
oC_MCS_AlignStackPointer, oC_MCS_AlignSize

Definition at line 100 of file oc_mcs.h.

#define oC_MCS_AlignSize (   Size,
  Alignment 
)

The macro is for alignment of size

See also
oC_MCS_AlignStackPointer, oC_MCS_AlignPointer

Definition at line 111 of file oc_mcs.h.

#define oC_MCS_AlignStackPointer (   Pointer)

The macro is for alignment of stack pointers

See also
oC_MCS_AlignPointer, oC_MCS_AlignSize

Definition at line 89 of file oc_mcs.h.

#define oC_MCS_IsStackPushDecrementPointer ( )

The macro returns true if operation stack push decrement stack pointer. This can be also used in "hash conditions".

#if oC_MCS_IsStackPushDecrementPointer()
// Do something, the stack push operation decrements pointer
#else
// Do something other, the stack push operation increments pointer
#endif

Definition at line 77 of file oc_mcs.h.

Typedef Documentation

typedef void(* oC_ContextExitHandler_t) (void)

The function is called, when the oC_ContextHandler_t function finishes its work.

Definition at line 199 of file oc_mcs.h.

typedef void(* oC_ContextHandler_t) (void *ContextParameter)

The function is for handling RTOS context. It will be called, as new thread, with separate stack.

Parameters
ParameterOptional parameter, that can be given for function

Definition at line 190 of file oc_mcs.h.

typedef void(* oC_FindNextStackHandler_t) (void)

The function is called, when changing current stack is required.

Definition at line 265 of file oc_mcs.h.

See also
oC_StackData_t

Definition at line 256 of file oc_mcs.h.

Enumeration Type Documentation

enum IRQn_Type

This type must be defined for the CMSIS library. It is created according to definitions from the oc_interrupts_defs.h file.

Definition at line 149 of file oc_mcs.h.

The type is for storing priority of interrupts. Note, that you can store with it values from oC_InterruptPriority_Minimum to oC_InterruptPriority_Maximum. The highest priority has oC_InterruptPriority_Maximum value.

Enumerator
oC_InterruptPriority_Error 

Value for functions, that returns priority to provide error handling.

See oC_MCS_GetInterruptPriority for example

oC_InterruptPriority_Minimum 

The lowest value for priority.

oC_InterruptPriority_Maximum 

The most important interrupt priority, that is possible in the machine.

Definition at line 174 of file oc_mcs.h.

The type is for storing mode of the memory access controlling

Enumerator
oC_MCS_MemoryAccessMode_User 

Memory controller is configured to allow for memory access only for regions that can be accessible by a user.

oC_MCS_MemoryAccessMode_Privileged 

Memory controller is configured to allow for memory access for regions that can be accessible by a privileged tasks.

Definition at line 243 of file oc_mcs.h.

Function Documentation

bool oC_MCS_AreInterruptsEnabled ( void  )

The function checks if interrupts are already enabled, by checking flag in a hardware.

Returns
true if interrupts are enabled
See also
oC_MCS_DisableInterrupts, oC_MCS_AreInterruptsEnabled, oC_MCS_EnterCriticalSection , oC_MCS_ExitCriticalSection , oC_MCS_IsCriticalSectionActive
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 356 of file oc_mcs.c.

oC_ErrorCode_t oC_MCS_ConfigureMemoryRegion ( const oC_MCS_MemoryRegionConfig_t Config)

The function is for configuration of the memory region. It allows to enable it, configure protection of the sector, etc. See oC_MCS_MemoryRegionConfig_t for more info.

Parameters
ConfigPointer to the configuration structure
Returns
code of error or #oC_ErrorCode_None if success
Note
ARM Cortex M7 notes:

Definition at line 701 of file oc_mcs.c.

bool oC_MCS_ConfigureSystemTimer ( oC_UInt_t  Prescaler,
oC_FindNextStackHandler_t  FindNextStackHandler 
)

The function is for configuring system timer - special timer, that is designed for RTOS. The timer is periodically calling the special interrupt (usually called SysTick), that is incrementing system tick counter, and call changing context interrupt if it is required.

Parameters
PrescalerPrescaler from system clock to system timer
FindNextStackHandlerHandler to call, when the changing current stack is required
Returns
true if success
See also
oC_MCS_SetNextStack
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 634 of file oc_mcs.c.

void oC_MCS_Delay ( register oC_UInt_t  Cycles)

The function is for creating delays for the number of cycles that is given as the Cycles argument.

Parameters
CyclesNumber of cycles to delay
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 679 of file oc_mcs.c.

bool oC_MCS_DisableInterrupt ( IRQn_Type  InterruptNumber)

The function is for disabling interrupt with specified number.

Parameters
InterruptNumberIRQ number to disable
Returns
true if interrupt is correctly disabled
See also
oC_MCS_EnableInterrupt , oC_MCS_IsInterruptEnabled
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 379 of file oc_mcs.c.

void oC_MCS_DisableInterrupts ( void  )

The functions is for disabling interrupts. It is not critical section - when this function is called, interrupts are always disabled

Warning
This function should not be used in most of cases! Use oC_MCS_EnterCriticalSection instead of it!
See also
oC_MCS_DisableInterrupts, oC_MCS_AreInterruptsEnabled, oC_MCS_EnterCriticalSection , oC_MCS_ExitCriticalSection , oC_MCS_IsCriticalSectionActive
Note
ARM Cortex M4 notes: In fact it does not disable interrupts - it just set possible interrupt priority to maximum. It is for correctly handling kernel and context switching
ARM Cortex M7 notes: In fact it does not disable interrupts - it just set possible interrupt priority to maximum. It is for correctly handling kernel and context switching

Definition at line 346 of file oc_mcs.c.

bool oC_MCS_EnableInterrupt ( IRQn_Type  InterruptNumber)

The function is for enabling interrupt with specified number.

Parameters
InterruptNumberIRQ number to enable
Returns
true if interrupt is correctly enabled
See also
oC_MCS_DisableInterrupt , oC_MCS_IsInterruptEnabled
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 368 of file oc_mcs.c.

void oC_MCS_EnableInterrupts ( void  )

The function is for enabling interrupts. It is not critical section - when this function is called, interrupts are always enabled.

Warning
This function should not be used in most of cases! Use oC_MCS_ExitCriticalSection instead of it!
See also
oC_MCS_DisableInterrupts, oC_MCS_AreInterruptsEnabled, oC_MCS_EnterCriticalSection , oC_MCS_ExitCriticalSection , oC_MCS_IsCriticalSectionActive
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 331 of file oc_mcs.c.

void* oC_MCS_GetCurrentMainStackPointer ( void  )

The function returns current main stack pointer

See also
oC_MCS_GetCurrentProcessStackPointer , oC_MCS_GetFreeStackSize
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 520 of file oc_mcs.c.

void* oC_MCS_GetCurrentProcessStackPointer ( void  )

The function returns current process stack pointer

See also
oC_MCS_GetCurrentMainStackPointer , oC_MCS_GetFreeStackSize
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 510 of file oc_mcs.c.

oC_Stack_t oC_MCS_GetCurrentStack ( void  )

This returns stack of current thread.

See also
oC_MCS_GetCurrentProcessStackPointer
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 591 of file oc_mcs.c.

oC_Int_t oC_MCS_GetFreeStackSize ( oC_Stack_t  Stack)

The function is for checking how much stack is free. When Stack is set to NULL, then it will check current stack.

Parameters
StackInitialized stack. Set it to NULL if you want current stack
Returns
size of free memory on stack. Note, that this can be also smaller than 0
See also
oC_MCS_GetCurrentProcessStackPointer
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 551 of file oc_mcs.c.

void* oC_MCS_GetHardFaultReason ( void  )
Note
ARM Cortex M7 notes:

Definition at line 321 of file oc_mcs.c.

oC_InterruptPriotity_t oC_MCS_GetInterruptPriority ( IRQn_Type  InterruptNumber)

Returns priority of interrupt specified by InterruptNumber. If error, it returns oC_InterruptPriority_Error

Parameters
InterruptNumberIRQ number
Returns
priority of interrupt or oC_InterruptPriority_Error if error
See also
oC_MCS_EnableInterrupt, oC_MCS_DisableInterrupt , oC_MCS_IsInterruptEnabled, oC_MCS_SetInterruptPriority
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 411 of file oc_mcs.c.

uint32_t oC_MCS_GetMaximumNumberOfRegions ( void  )
Note
ARM Cortex M7 notes:

Definition at line 763 of file oc_mcs.c.

oC_MCS_MemoryAccessMode_t oC_MCS_GetMemoryAccessMode ( void  )

The function returns memory access mode, that is currently set.

See also
oC_MCS_SetMemoryAccessMode
Note
ARM Cortex M7 notes:

Definition at line 870 of file oc_mcs.c.

oC_Int_t oC_MCS_GetMinimumStackBufferSize ( oC_Int_t  StackSize)

Returns minimum size of the buffer for storing stack

Parameters
StackSizeRequired stack size
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 601 of file oc_mcs.c.

uint32_t oC_MCS_GetNumberOfRegions ( void  )
Note
ARM Cortex M7 notes:

Definition at line 773 of file oc_mcs.c.

oC_Int_t oC_MCS_GetStackSize ( oC_Stack_t  Stack)

The function returns stack size of the stack

Parameters
StackInitialized stack. Set it to NULL if you want current stack
Returns
size of memory on stack.
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 529 of file oc_mcs.c.

oC_Stack_t oC_MCS_GetSystemStack ( void  )
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 659 of file oc_mcs.c.

bool oC_MCS_InitializeModule ( void  )

The function initializes module to work. There is no function, that deinitializes it, and does not prevents again reinitialization

Returns
true if success
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 299 of file oc_mcs.c.

bool oC_MCS_InitializeStack ( oC_Stack_t outStack,
void *  Buffer,
oC_Int_t  BufferSize,
oC_ContextHandler_t  ContextHandler,
void *  HandlerParameter,
oC_ContextExitHandler_t  ExitHandler 
)

The function is initializing the stack for threads. It prepares machine general registers, such as stack pointers, returns pointers, etc to work with new thread. When the stack is switched, then the machine reads registers states from the stack. In this case from the stack, that was initialized by this function. Returns true if success

Parameters
outStack[out] Stack to initialize
BufferBuffer for storing stack
BufferSizeSize of the buffer, cannot be smaller than value returned by function oC_MCS_GetMinimumStackBufferSize
ContextHandlerHandler of the function that should be called during this context execution
HandlerParameterParameter to give to the context handler
ExitHandlerHandler of the function that should be called when the task is return
Returns
true if success
See also
oC_MCS_SetNextStack , oC_MCS_GetFreeStackSize , oC_MCS_GetMinimumStackBufferSize
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 432 of file oc_mcs.c.

bool oC_MCS_IsInterruptEnabled ( IRQn_Type  InterruptNumber)

Checks if an interrupt with specified number was enabled.

Parameters
InterruptNumberIRQ number
Returns
true if interrupt is enabled
See also
oC_MCS_EnableInterrupt, oC_MCS_DisableInterrupt
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 390 of file oc_mcs.c.

bool oC_MCS_ReadFreeRegionNumber ( uint32_t *  outFreeRegionNumber)
Parameters
outFreeRegionNumberPointer to the variable, where the region number should be stored
Returns
true if found
Note
ARM Cortex M7 notes:

Definition at line 807 of file oc_mcs.c.

void oC_MCS_Reboot ( void  )

The function reboots the machine using software bits. It should never exit. If it does, it means, that it is not implemented yet for the given machine core.

Warning
This function should never ends!
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 421 of file oc_mcs.c.

bool oC_MCS_ReturnToSystemStack ( void  )
Returns
true if success
See also
oC_MCS_SetNextStack
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 669 of file oc_mcs.c.

bool oC_MCS_SetInterruptPriority ( IRQn_Type  InterruptNumber,
oC_InterruptPriotity_t  Priority 
)

Change priority of interrupt specified by InterruptNumber. Priority can be value between oC_InterruptPriority_Minimum and oC_InterruptPriority_Maximum. The maximum value is the highest priority.

Parameters
InterruptNumberIRQ number
PriorityPriority of interrupt, see #oC_InterruptPriority_t for details
Returns
true priority was set
See also
oC_MCS_EnableInterrupt, oC_MCS_DisableInterrupt , oC_MCS_IsInterruptEnabled, oC_MCS_GetInterruptPriority
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 400 of file oc_mcs.c.

bool oC_MCS_SetMemoryAccessMode ( oC_MCS_MemoryAccessMode_t  Mode)

The function is for select between modes privileged and unprivileged. Thanks to that some memory regions can be protected against not privileged user access.

Parameters
ModeAccess mode to set
Returns
true if success
Note
ARM Cortex M7 notes:

Definition at line 847 of file oc_mcs.c.

bool oC_MCS_SetNextStack ( oC_Stack_t  Stack)

The function sets next stack for kernel. The stack will be changed in the next context switching interrupt.

Warning
Remember, that stack should be initialized by oC_MCS_InitializeStack function before this function, and that the system timer should be configured first by using function oC_MCS_ConfigureSystemTimer
Parameters
StackInitialized stack
Returns
true if success
See also
oC_MCS_GetCurrentStack, oC_MCS_InitializeStack, oC_MCS_ConfigureSystemTimer
void MyThread( void * Argument )
{
while(1);
}
void MyThreadEnds( void )
{
printf("My thread does not exist anymore!");
}
oC_Stack_t stack = NULL;
oC_Int_t bufferSize = oC_MCS_GetMinimumStackBufferSize( 512 );
void * buffer = malloc( bufferSize );
oC_MCS_InitializeStack( &stack , buffer , bufferSize , MyThread , NULL , MyThreadEnds );
// When context will be switched, the MyThread function will be called.
Note
ARM Cortex M4 notes:
ARM Cortex M7 notes:

Definition at line 613 of file oc_mcs.c.