Contiki-NG
Files | Macros | Functions
The Contiki-NG main function

A platform-independent main function. More...

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. More...
 
#define PLATFORM_MAIN_ACCEPTS_ARGS   0
 Controls whether the main function accepts arguments. More...
 

Functions

void platform_init_stage_one (void)
 Basic (Stage 1) platform driver initialisation. More...
 
void platform_init_stage_two (void)
 Stage 2 of platform driver initialisation. More...
 
void platform_init_stage_three (void)
 Final stage of platform driver initialisation. More...
 
void platform_idle (void)
 The platform's idle/sleep function. More...
 
void platform_main_loop (void)
 The platform's main loop, if provided. More...
 
void platform_process_args (int argc, char **argv)
 Allow the platform to process main's command line arguments. More...
 

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

◆ 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 185 of file platform.c.

References clock_arch_enter_idle(), clock_arch_exit_idle(), clock_seconds(), lpm_drop(), and watchdog_periodic().

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 114 of file platform.c.

References board_init(), button_hal_init(), gpio_hal_init(), leds_init(), leds_on(), and soc_init().

◆ 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 169 of file platform.c.

References ble_get_mac(), eeprom_init(), linkaddr_copy(), linkaddr_node_addr, linkaddr_set_node_addr(), process_start(), and rf_ble_beacond_init().

◆ 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 123 of file platform.c.

References cc26xx_uart_init(), clock_wait(), i2c_init(), leds_on(), random_init(), SERIAL_LINE_CONF_UART, serial_line_input_byte(), uart0_init(), uart1_init(), uart_init(), uart_set_input(), usb_serial_init(), and usb_serial_set_input().

◆ 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 195 of file platform.c.

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

◆ platform_process_args()

void platform_process_args ( int  argc,
char **  argv 
)

Allow the platform to process main's command line arguments.

If the platform wishes main() to accept arguments, then the Contiki-NG main will call this function here so that the platform can process/store those arguments.

This function will only get called if PLATFORM_MAIN_ACCEPTS_ARGS is non-zero.

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

Definition at line 233 of file platform.c.