Contiki-NG
Loading...
Searching...
No Matches
The Contiki-NG main function

Files

file  contiki-main.c
 Implementation of Contiki-NG's main routine.
 
file  platform.h
 Header file for the Contiki-NG main routine.
 

Macros

#define PLATFORM_PROVIDES_MAIN_LOOP   0
 Controls whether the platform provides a custom main loop.
 
#define PLATFORM_MAIN_ACCEPTS_ARGS   0
 Controls whether the main function accepts arguments.
 
#define CONTIKI_OPTION(prio, ...)
 Add a command line option when the compilation unit is present.
 
#define CONTIKI_USAGE(prio, msg)
 Set the usage string shown for –help.
 
#define CONTIKI_EXTRA_HELP(prio, msg)
 Set extra usage help shown at the end of –help.
 

Functions

void platform_init_stage_one (void)
 Basic (Stage 1) platform driver initialisation.
 
void platform_init_stage_two (void)
 Stage 2 of platform driver initialisation.
 
void platform_init_stage_three (void)
 Final stage of platform driver initialisation.
 
void platform_idle (void)
 The platform's idle/sleep function.
 
void platform_main_loop (void)
 The platform's main loop, if provided.
 

Detailed Description

A platform-independent main function.

Contiki-NG provides a modular, platform-independent main function that aims to support all hardware ports.

In a nutshell, the main routine has the following steps below:

The main loop will service all pending events and will then call a platform-dependent function to put the hardware in a low-power state.

For more detail of which hardware driver should be implemented in which stage of the boot process, see the documentation of platform_init_stage_one(), platform_init_stage_two() and platform_init_stage_three()

Macro Definition Documentation

◆ CONTIKI_EXTRA_HELP

#define CONTIKI_EXTRA_HELP ( prio,
msg )

Set extra usage help shown at the end of –help.

Parameters
prioPriority. If multiple compilation units set the extra usage help, the one with the highest priority takes effect.
msgThe usage message.

Definition at line 189 of file platform.h.

◆ CONTIKI_OPTION

#define CONTIKI_OPTION ( prio,
... )

Add a command line option when the compilation unit is present.

The second argument is the initialization of struct option for getopt_long.

The third argument is a callback function of type int (*)(char *), or NULL. The callback will receieve optarg from getopt_long as parameter, and should return 0 on success. Any other return value will terminate main() with that exit code.

The fourth argument is the string shown from –help. Pass NULL if the option should not be shown in the help.

Example:

CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO,
{ "v", optional_argument, NULL, 0 }, verbose_callback,
"verbosity level (0-5)\n");
#define CONTIKI_OPTION(prio,...)
Add a command line option when the compilation unit is present.
Definition platform.h:153
Parameters
prioPriority. Higher priority will appear later in –help.

Definition at line 153 of file platform.h.

◆ CONTIKI_USAGE

#define CONTIKI_USAGE ( prio,
msg )

Set the usage string shown for –help.

Parameters
prioPriority. If multiple compilation units set the usage string, the one with the highest priority takes effect.
msgThe usage message.

Definition at line 171 of file platform.h.

◆ PLATFORM_MAIN_ACCEPTS_ARGS

#define PLATFORM_MAIN_ACCEPTS_ARGS   0

Controls whether the main function accepts arguments.

By default our main does not accept arguments. However, when running on native targets, command line arguments to main are required.

Definition at line 95 of file platform.h.

◆ PLATFORM_PROVIDES_MAIN_LOOP

#define PLATFORM_PROVIDES_MAIN_LOOP   0

Controls whether the platform provides a custom main loop.

By default we will use the main loop provided here. This however does not work for some platforms, so we allow them to override it.

Definition at line 83 of file platform.h.

Function Documentation

◆ platform_idle()

void platform_idle ( void )

The platform's idle/sleep function.

This function will be called as part of the main loop after all events have been serviced. This is where you will normally put the device in a low-power state. Waking up from this state and tidying up the hardware is the port's responsibility.

It is the port developer's responsibility to implement this function.

Definition at line 181 of file platform.c.

References clock_arch_enter_idle(), clock_arch_exit_idle(), lpm_drop(), process_nevents(), timer_expired(), timer_reset(), watchdog_periodic(), watchdog_start(), and watchdog_stop().

Referenced by platform_main_loop().

◆ platform_init_stage_one()

void platform_init_stage_one ( void )

Basic (Stage 1) platform driver initialisation.

This function will get called early on in the Contiki-NG boot sequence.

In this function, the platform should initialise all core device drivers. For example, this is where you will normally want to initialise hardware timers/clocks, GPIO, LEDS. Normally this function will also enable the MCU's global interrupt.

The Contiki-NG process scheduler, software clocks and timers will not be running yet, so any platform drivers that rely on it should not be initialised here. Instead, they should be initialised in platform_init_stage_two() or in platform_init_stage_three()

It is the port developer's responsibility to implement this function.

See also
platform_init_stage_two()
platform_init_stage_three()

Definition at line 113 of file platform.c.

References board_init(), button_hal_init(), csprng_feed(), gpio_hal_init(), leds_init(), oscillators_select_lf_xosc(), soc_init(), soc_rtc_init(), and csprng_seed::u8.

◆ platform_init_stage_three()

void platform_init_stage_three ( void )

Final stage of platform driver initialisation.

Initialisation of platform-specific drivers that require networking to be running. This is also a good place to initialise sensor drivers.

When this function returns, the main routine will assume that the hardware is fully initialised.

It is the port developer's responsibility to implement this function.

See also
platform_init_stage_one()
platform_init_stage_two()

Definition at line 165 of file platform.c.

References adc_init(), board_init(), CLOCK_SECOND, linkaddr_copy(), linkaddr_node_addr, process_start(), RADIO_PARAM_CHANNEL, RADIO_PARAM_PAN_ID, RADIO_RESULT_OK, rf_ble_beacond_init(), soc_print_info(), and timer_set().

◆ platform_init_stage_two()

void platform_init_stage_two ( void )

Stage 2 of platform driver initialisation.

This function will be called by the Contiki-NG boot sequence after parts of the core have been initialised. More specifically, when this function gets called, the following modules will be running:

  • Software clock
  • Process scheduler
  • Event timer (etimer)
  • Callback timer (ctimer)
  • rtimer
  • Energest (if enabled)

Therefore, any platform driver that relies on any of the above modules should be initialised here or in platform_init_stage_three(), but not in platform_init_stage_one()

The Contiki-NG network stack will not be running yet, so any platform drivers that rely on networking should not be initialised here.

When this function returns, the main routine will assume that the platform has enabled character I/O and can print to console. When this function returns, main() will attempt to initialise the network stack. For this to work properly, this function should also populate linkaddr_node_addr.

It is the port developer's responsibility to implement this function.

See also
platform_init_stage_one()
platform_init_stage_three()

Definition at line 122 of file platform.c.

References ble_eui64_addr_cpy_to(), board_init(), button_hal_init(), cc26xx_uart_init(), cc26xx_uart_set_input(), clock_wait(), crypto_disable(), crypto_init(), csprng_feed(), dfu_trigger_usb_init(), i2c_init(), ieee_addr_cpy_to(), INTERRUPTS_ENABLE, LEDS_ALL, linkaddr_node_addr, populate_link_address(), reset_debug(), SERIAL_LINE_CONF_UART, trng_rand(), csprng_seed::u8, uart0_init(), uart0_set_callback(), uart1_init(), uart_init(), uart_set_input(), uarte_init(), uarte_set_input(), udma_init(), uip_lladdr, usb_init(), usb_serial_init(), usb_serial_set_input(), usb_set_input(), and xmem_init().

◆ platform_main_loop()

void platform_main_loop ( void )

The platform's main loop, if provided.

If the platform developer wishes to do so, it is possible to override the main loop provided by Contiki-NG's core. To do so, define PLATFORM_CONF_PROVIDES_MAIN_LOOP as 1.

It is the port developer's responsibility to implement this function.

Definition at line 103 of file tz-normal.c.

References etimer_request_poll(), platform_idle(), process_nevents(), process_run(), process_start(), and watchdog_periodic().