Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Prerequisites

...

A header file is a file with the *.h extension, which contains C function declarations and macro definitions to be shared between several source files. In a nutshell, it provides an Application Program Interface (API) that does not contain or expose any implementation details.

...

Note that the IODirection enum is anonymous.

Functions

See the Coding Standards for function naming conventions. Read the document, and follow the standards. Please.

...

Essentially, after the board performs its initialization sequence, it enters the event loop and just processes events forever, until it loses power.

volatile keyword

The volatile keyword tells the compiler that the value of the variable may change at any time.

Example

Consider the following example.

Code Block
languagecpp
themeConfluence
volatile uint32_t total = 0;


void add() {
  uint16_t i = 0;
  for (i = 0; i < N; ++i) {
    ++total;
  }
}

Without volatile the compiler could optimize the code. The volatile keyword forces the compiler to load and store the value on every use.

Code Block
languagecpp
themeConfluence
volatile uint32_t total = 0;


void add() {
  // load total into a register
  for (i = 0; i < N; ++i) {
    // increment total
  }
  // save total
}

With the volatile keyword, it forces the compiler to reload the value each time

Code Block
languagecpp
themeConfluence
volatile uint32_t total = 0;


void add() {
  for (i = 0; i < N; ++i) {
    // load total into a register
    // increment total
    // save total
  }
}

Example

Consider the following code, where val is modified by some ISR that sets it to false.

Code Block
languagecpp
themeConfluence
bool val = true;


while (val) {
  // do stuff
}

If the value is not set to volatile, if val is set to false, the loop will not terminate.

The Importance of Modular Code

...