µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
µOS++ Application Overrides & Hooks

µOS++ application customisable functions and hooks. More...

Startup Routines

void _start (void)
 The standard C application entry point.
 
void os_startup_initialize_hardware_early (void)
 Initialise hardware early.
 
void os_startup_initialize_hardware (void)
 Initialise hardware.
 
void os_startup_initialize_free_store (void *heap_address, size_t heap_size_bytes)
 Initialise free store.
 
void os_startup_initialize_args (int *p_argc, char ***p_argv)
 Initialise arguments.
 
void os_startup_create_thread_idle (void)
 Create the idle thread.
 

Termination Routines

void os_terminate_goodbye (void)
 Display statistics and say goodbye before terminating.
 
void os_terminate (int code)
 Terminate the application. There is no more life after this.
 

Hooks

bool os_rtos_idle_enter_power_saving_mode_hook (void)
 Hook to enter a power saving mode.
 
void os_rtos_application_out_of_memory_hook (void)
 Hook to handle out of memory in the application free store.
 
void os_rtos_system_out_of_memory_hook (void)
 Hook to handle out of memory in the RTOS dynamic memory.
 

Compatibility Macros

#define os_initialize_hardware_early   os_startup_initialize_hardware_early
 
#define os_initialize_hardware   os_startup_initialize_hardware
 
#define os_initialize_args   os_startup_initialize_args
 

Detailed Description

Applications using µOS++ can customise the behaviour of the system or intercept several events by redefining these functions.

All of them have weak default definitions, providing a reasonable functionality.

Macro Definition Documentation

◆ os_initialize_args

#define os_initialize_args   os_startup_initialize_args

Definition at line 178 of file os-hooks.h.

◆ os_initialize_hardware

#define os_initialize_hardware   os_startup_initialize_hardware

Definition at line 177 of file os-hooks.h.

◆ os_initialize_hardware_early

#define os_initialize_hardware_early   os_startup_initialize_hardware_early

Definition at line 176 of file os-hooks.h.

Function Documentation

◆ _start()

void _start ( void  )
Parameters
None.
Returns
Does not return.

This is the place where the Cortex-M core will go immediately after reset (the Reset_Handler calls this function).

To reach this location, the reset stack must point to a valid internal RAM area.

Debugging new startup configurations usually begins with placing a breakpoint at _start(), and stepping through the routine.

Definition at line 241 of file startup.cpp.

242{
243 // After Reset the Cortex-M processor is in Thread mode,
244 // priority is Privileged, and the Stack is set to Main.
245
246 // --------------------------------------------------------------------------
247
248 // Initialise hardware right after reset, to switch clock to higher
249 // frequency and have the rest of the initialisations run faster.
250 //
251 // Mandatory on platforms like Kinetis, which start with the watch dog
252 // enabled and require an early sequence to disable it.
253 //
254 // Also useful on platform with external RAM, that need to be
255 // initialised before filling the BSS section.
256
258
259 // Use Old Style DATA and BSS section initialisation,
260 // that will manage a single BSS sections.
261
262#if defined(DEBUG) && (OS_BOOL_STARTUP_GUARD_CHECKS)
263
264 __data_begin_guard = DATA_GUARD_BAD_VALUE;
265 __data_end_guard = DATA_GUARD_BAD_VALUE;
266
267#endif
268
269#if !defined(OS_INCLUDE_STARTUP_INIT_MULTIPLE_RAM_SECTIONS)
270
271 // Copy the DATA segment from Flash to RAM (inlined).
273
274#else
275
276 // Copy the data sections from flash to SRAM.
277 for (unsigned int* p = &__data_regions_array_start;
278 p < &__data_regions_array_end;)
279 {
280 unsigned int* from = (unsigned int*)(*p++);
281 unsigned int* region_begin = (unsigned int*)(*p++);
282 unsigned int* region_end = (unsigned int*)(*p++);
283
284 os_initialize_data (from, region_begin, region_end);
285 }
286
287#endif
288
289#if defined(DEBUG) && (OS_BOOL_STARTUP_GUARD_CHECKS)
290
291 if ((__data_begin_guard != DATA_BEGIN_GUARD_VALUE)
292 || (__data_end_guard != DATA_END_GUARD_VALUE))
293 {
294 while (true)
295 {
296 __NOP ();
297 }
298 }
299
300#endif
301
302#if defined(DEBUG) && (OS_BOOL_STARTUP_GUARD_CHECKS)
303
304 __bss_begin_guard = BSS_GUARD_BAD_VALUE;
305 __bss_end_guard = BSS_GUARD_BAD_VALUE;
306
307#endif
308
309#if !defined(OS_INCLUDE_STARTUP_INIT_MULTIPLE_RAM_SECTIONS)
310
311 // Zero fill the BSS section (inlined).
313
314#else
315
316 // Zero fill all bss segments
317 for (unsigned int* p = &__bss_regions_array_start;
318 p < &__bss_regions_array_end;)
319 {
320 unsigned int* region_begin = (unsigned int*)(*p++);
321 unsigned int* region_end = (unsigned int*)(*p++);
322 os_initialize_bss (region_begin, region_end);
323 }
324
325#endif
326
327#if defined(DEBUG) && (OS_BOOL_STARTUP_GUARD_CHECKS)
328
329 if ((__bss_begin_guard != 0) || (__bss_end_guard != 0))
330 {
331 while (true)
332 {
333 __NOP ();
334 }
335 }
336
337#endif
338
339 // Hook to continue the initialisations. Usually compute and store the
340 // clock frequency in the global CMSIS variable, cleared above.
342
343 // Initialise the trace output device. From this moment on,
344 // trace_printf() calls are available (including in static constructors).
346
347 trace_puts (""); // Empty line
348
349 trace_printf ("Hardware initialised\n");
350 trace_printf ("Main stack %p-%p\n", &_Heap_Limit, &__stack);
351
354 static_cast<size_t> (reinterpret_cast<char*> (&_Heap_Limit)
355 - reinterpret_cast<char*> (&_Heap_Begin)));
356
357 // Get the argc/argv (useful in semihosting configurations).
358 int argc;
359 char** argv;
360 os_startup_initialize_args (&argc, &argv);
361
362 // Call the standard library initialisation (mandatory for C++ to
363 // execute the constructors for the static objects).
365 trace_printf ("Static objects constructed\n");
366
367#if defined(OS_HAS_INTERRUPTS_STACK)
370 static_cast<size_t> (reinterpret_cast<char*> (&__stack)
371 - reinterpret_cast<char*> (&_Heap_Limit)));
372#endif /* defined(OS_HAS_INTERRUPTS_STACK) */
373
374 // Call the main entry point, and save the exit code.
375 int code = main (argc, argv);
376
377 // Standard program termination;
378 // `atexit()` and C++ static destructors are executed.
379 exit (code);
380 /* NOTREACHED */
381}
void set(stack::element_t *address, std::size_t size_bytes)
Set the stack address and size.
Definition os-thread.h:2141
void exit(int code)
Definition exit.c:70
void os_startup_initialize_free_store(void *heap_address, size_t heap_size_bytes)
Initialise free store.
void os_startup_initialize_hardware_early(void)
Initialise hardware early.
void os_startup_initialize_args(int *p_argc, char ***p_argv)
Initialise arguments.
Definition startup.cpp:406
void os_startup_initialize_hardware(void)
Initialise hardware.
class thread::stack * stack(void)
Get the interrupts stack.
Definition os-core.cpp:583
unsigned long int _Heap_Limit
unsigned int _sidata
int main(int argc, char *argv[])
Default implementation of main().
Definition os-main.cpp:113
static void os_run_init_array(void)
Definition startup.cpp:163
unsigned int __bss_start__
unsigned int _Heap_Begin
static void os_initialize_data(unsigned int *from, unsigned int *region_begin, unsigned int *region_end)
Definition startup.cpp:129
unsigned int _sdata
static void os_initialize_bss(unsigned int *region_begin, unsigned int *region_end)
Definition startup.cpp:142
unsigned int _edata
unsigned int __bss_end__
unsigned long int __stack
void trace_initialize(void)
Definition trace.cpp:165
int trace_printf(const char *format,...)
int trace_puts(const char *s)

References __bss_end__, __bss_start__, __stack, _edata, _Heap_Begin, _Heap_Limit, _sdata, _sidata, exit(), main(), os_initialize_bss(), os_initialize_data(), os_run_init_array(), os_startup_initialize_args(), os_startup_initialize_free_store(), os_startup_initialize_hardware(), os_startup_initialize_hardware_early(), os::rtos::thread::stack::set(), os::rtos::interrupts::stack(), trace_initialize(), trace_printf(), and trace_puts().

◆ os_rtos_application_out_of_memory_hook()

void os_rtos_application_out_of_memory_hook ( void  )
Parameters
None.
Returns
Nothing.

This function is called when the application memory manager detects an out of memory condition.

This function is usually used to gracefully reset the device.

However, for special memory managers, which do not coalesce automatically, it might be possible to first try to coalesce. If this succeeds, this call can return, and the allocation will be resumed.

Note
Since most allocations are done in critical sections, this function is very likely to be called with the scheduler locked.

Definition at line 299 of file initialise-free-store.cpp.

300{
302}
void __throw_bad_alloc(void)

References os::estd::__throw_bad_alloc().

Referenced by os_startup_initialize_free_store().

◆ os_rtos_idle_enter_power_saving_mode_hook()

bool os_rtos_idle_enter_power_saving_mode_hook ( void  )
Parameters
None.
Return values
trueThe hook entered a power saving mode.
falseThe hook did not enter a power saving mode.

The hook must check an application specific condition to determine if it is required to enter a power saving mode, and, if necessary, actually enter the desired power saving mode.

The application must ensure that all interrupts associated with the external events used to awake the device are enabled. Usually the RTC is used for this purpose, but other devices too, like USB, GPIO pins, etc may be used to end the power saving mode.

This function is executed by the idle thread on each iteration, and must limit complexity to reasonably levels.

If the user function decides not to enter a power saving mode, it must return false, which will make the idle thread proceed as usual, by entering a shallow sleep waiting for the next interrupt.

Definition at line 57 of file os-idle.cpp.

58{
59 return false;
60}

Referenced by os_rtos_idle_actions().

◆ os_rtos_system_out_of_memory_hook()

void os_rtos_system_out_of_memory_hook ( void  )
Parameters
None.
Returns
Nothing.

This function is called when the RTOS system memory manager detects an out of memory condition.

This function is usually used to gracefully reset the device.

However, for special memory managers, which do not coalesce automatically, it might be possible to first try to coalesce. If this succeeds, this call can return, and the allocation will be resumed.

Note
Since most allocations are done in critical sections, this function is very likely to be called with the scheduler locked.

Definition at line 322 of file initialise-free-store.cpp.

323{
325}

References os::estd::__throw_bad_alloc().

Referenced by os_startup_initialize_free_store().

◆ os_startup_create_thread_idle()

void os_startup_create_thread_idle ( void  )
Parameters
None.
Returns
Nothing.

Referenced by main().

◆ os_startup_initialize_args()

void os_startup_initialize_args ( int *  p_argc,
char ***  p_argv 
)
Parameters
[out]p_argcPointer to argc.
[out]p_argvPointer to argv.

Definition at line 406 of file startup.cpp.

407{
408 // By the time we reach this, the data and bss should have been initialised.
409
410 // The strings pointed to by the argv array shall be modifiable by the
411 // program, and retain their last-stored values between program startup
412 // and program termination. (static, no const)
413 static char name[] = "";
414
415 // The string pointed to by argv[0] represents the program name;
416 // argv[0][0] shall be the null character if the program name is not
417 // available from the host environment. argv[argc] shall be a null pointer.
418 // (static, no const)
419 static char* argv[2] = { name, NULL };
420
421 *p_argc = 1;
422 *p_argv = &argv[0];
423
424 return;
425}

Referenced by _start().

◆ os_startup_initialize_free_store()

void os_startup_initialize_free_store ( void *  heap_address,
size_t  heap_size_bytes 
)
Parameters
heap_addressThe first unallocated RAM address (after the BSS).
heap_size_bytesThe free store size.
Returns
Nothing.

Referenced by _start().

◆ os_startup_initialize_hardware()

void os_startup_initialize_hardware ( void  )
Parameters
None.
Returns
Nothing.

This is the default implementation for the second hardware initialisation routine.

It is called from _start(), right after DATA & BSS init, before the static constructors.

The application can redefine it for more complex cases that require custom inits (before constructors), otherwise these inits can be done in main().

Definition at line 114 of file initialize-hardware.c.

115{
116 // Call the CSMSIS system clock routine to store the clock frequency
117 // in the SystemCoreClock global RAM location.
118 SystemCoreClockUpdate ();
119}

Referenced by _start().

◆ os_startup_initialize_hardware_early()

void os_startup_initialize_hardware_early ( void  )
Parameters
None.
Returns
Nothing.

This is the default early hardware initialisation routine.

It is called right at the beginning of _start(), to switch clocks to higher frequencies and have the rest of the initialisations run faster.

The application can redefine it for more complex cases that requires inits before DATA and BSS init.

It is mandatory on platforms like Kinetis, which start with the watch dog enabled and require an early sequence to disable it.

Also useful on platform with external RAM, that need to be initialised before filling the BSS section.

Definition at line 54 of file initialize-hardware.c.

55{
56 // Call the CSMSIS system initialisation routine.
57 SystemInit ();
58
59#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
60
61 // Set VTOR to the actual address, provided by the linker script.
62 // Override the manual, possibly wrong, SystemInit() setting.
63 SCB->VTOR = (uint32_t)(&__vectors_start);
64 // Ensure all subsequence instructions use the new configuration.
65 __DSB ();
66
67#endif /* defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) */
68
69 // The current version of SystemInit() leaves the value of the clock
70 // in a RAM variable (SystemCoreClock), which will be cleared shortly,
71 // so it needs to be recomputed after the RAM initialisations
72 // are completed.
73
74#if defined(OS_INCLUDE_STARTUP_INIT_FP) || defined(__ARM_FP)
75
76 // Normally FP init is done by SystemInit(). In case this is not done
77 // there, it is possible to force its inclusion by defining
78 // OS_INCLUDE_STARTUP_INIT_FP.
79
80 // Enable the Cortex-M4 FPU only when -mfloat-abi=hard or -mfloat-abi=softfp.
81 // Code taken from Section 7.1, Cortex-M4 TRM (DDI0439C)
82
83 // Set bits 20-23 to enable CP10 and CP11 coprocessor.
84 SCB->CPACR |= (0xF << 20);
85
86 // Lazy save.
87 FPU->FPCCR |= FPU_FPCCR_ASPEN_Msk | FPU_FPCCR_LSPEN_Msk;
88
89#endif /* defined(OS_INCLUDE_STARTUP_INIT_FP) || defined (__ARM_FP) */
90
91#if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
92
93 SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk;
94
95#endif
96}

Referenced by _start().

◆ os_terminate()

void os_terminate ( int  code)
Parameters
[in]codeExit code, 0 for success, non 0 for failure.
Returns
Nothing.

Referenced by _Exit(), and exit().

◆ os_terminate_goodbye()

void os_terminate_goodbye ( void  )
Parameters
None.
Returns
Nothing.

Definition at line 194 of file os-main.cpp.

195{
196#if defined(TRACE)
197
198 trace::printf ("\n");
199
200#if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
201
202 // Application memory.
204
205#if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
207#endif /* defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES) */
208
209#endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
210
212
213 trace::printf ("Main thread stack: %u/%u bytes used\n",
214 st.size () - st.available (), st.size ());
215
216#if defined(OS_HAS_INTERRUPTS_STACK)
217 trace::printf ("Interrupts stack: %u/%u bytes used\n",
221#endif /* defined(OS_HAS_INTERRUPTS_STACK) */
222
223 trace::printf ("\nHasta la Vista!\n");
224
225#endif /* defined(TRACE) */
226}
void trace_print_statistics(void)
Print a long message with usage statistics.
Definition os-memory.h:1444
std::size_t size(void)
Get the stack size.
Definition os-thread.h:2175
std::size_t available(void)
Compute how much available stack remains.
thread::stack & stack(void)
Get the thread context stack.
Definition os-thread.h:2451
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
memory_resource * get_default_resource(void) noexcept
Get the default RTOS system memory manager.
Definition os-memory.h:1136
memory_resource * get_default_resource(void) noexcept
Get the default application memory manager.
rtos::thread * os_main_thread
Definition os-main.cpp:92

References os::rtos::thread::stack::available(), os::estd::pmr::get_default_resource(), os::rtos::memory::get_default_resource(), os_main_thread, os::trace::printf(), os::rtos::thread::stack::size(), os::rtos::thread::stack(), os::rtos::interrupts::stack(), and os::rtos::memory::memory_resource::trace_print_statistics().

Referenced by _Exit().