Skip to content

Commit

Permalink
[crt0] remove after-main handler
Browse files Browse the repository at this point in the history
main should not return
  • Loading branch information
stnolting committed Dec 12, 2023
1 parent 1fec0bc commit 37c2e1f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 49 deletions.
35 changes: 0 additions & 35 deletions docs/datasheet/software.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@ The `crt0.S` start-up performs the following operations:
** All interrupt sources are disabled by clearing <<_mie>>.
** The return value of `main` is copied to the <<_mscratch>> CSR to allow inspection by the debugger.
** Call all _destructors_ (if there are any).
** An optional <<_after_main_handler>> is called (if defined at all).
** The CPU enters sleep mode executing the `wfi` instruction in an endless loop.

.Bootloader Start-Up Code
Expand All @@ -535,40 +534,6 @@ The early-trap handler should be replaced by a more capable / informative one as
(for example by using the <<_neorv32_runtime_environment>>).


:sectnums:
===== After-Main Handler

If the application's `main()` function actually returns, an _after main handler_ can be executed. This handler is a "normal" function
as the C runtime is still available when executed. If this handler uses any kind of peripheral/IO modules make sure these are
already initialized within the application. Otherwise you have to initialize them _inside_ the handler.

.After-main handler - function prototype
[source,c]
----
void __neorv32_crt0_after_main(int32_t return_code);
----

The function has exactly one argument (`return_code`) that provides the _return value_ of the application's main function.
For instance, this variable contains `-1` if the main function returned with `return -1;`. The after-main handler itself does
not provide a return value.

A simple UART output can be used to inform the user when the application's main function returns
(this example assumes that UART0 has been already properly configured in the actual application):

.After-main handler - simple example
[source,c]
----
void __neorv32_crt0_after_main(int32_t return_code) {
neorv32_uart0_printf("\n<RTE> main function returned with exit code %i. </RTE>\n", return_code); <1>
}
----
<1> Use `<RTE>` here to make clear this is a message comes from the runtime environment.

[NOTE]
The after-main handler is executed _after_ executing all destructor functions (if there are any at all).


<<<
// ####################################################################################################################

Expand Down
14 changes: 0 additions & 14 deletions sw/common/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,6 @@ __crt0_call_destructors_loop_end:
#endif


// ************************************************************************************************
// Call "after main" handler (if there is any) if main really returns
// ************************************************************************************************
#ifndef make_bootloader // after_main handler not supported for bootloader
__crt0_main_aftermath:
.weak __neorv32_crt0_after_main
la ra, __neorv32_crt0_after_main
beqz ra, __crt0_main_aftermath_end // check if an aftermath handler has been specified
jalr ra // execute handler with main's return code still in a0

__crt0_main_aftermath_end:
#endif


// ************************************************************************************************
// Go to endless sleep mode
// ************************************************************************************************
Expand Down

0 comments on commit 37c2e1f

Please sign in to comment.