HomeCore 0.1.3 (07b5544)
Bare-metal personal computer firmware for Cortex-M
Loading...
Searching...
No Matches

Numbered LEDs, their /dev/ledN files, and the driver interface. More...

Collaboration diagram for LEDs:

Data Structures

struct  led_ops_t
 Operations an LED driver provides; every one is required. More...
 
struct  led_t
 An LED: driver operations bound to one device instance. More...
 
struct  led_table_t
 Every LED in index order; generated in devicetree.c. More...
 

Driver interface

const led_table_t dt_led_table
 The board's LEDs; generated in devicetree.c.
 
void led_register_node (vfs_node_t *node, const char *path, const led_t *led)
 Register the device file of an LED.
 

Using LEDs

unsigned led_count (void)
 Report how many LEDs the board describes.
 
int led_set (unsigned index, bool on)
 Turn an LED on or off.
 
int led_get (unsigned index, bool *on)
 Read whether an LED is lit.
 
int led_toggle (unsigned index)
 Invert an LED.
 

Detailed Description

Numbered LEDs, their /dev/ledN files, and the driver interface.

Every enabled device whose binding has led: true is an LED. They are numbered in description order, and each registers a device file, by default /dev/ plus its node name, so boards name the nodes led0, led1, and so on. The generated dt_led_table lists them.

Driver Compatible Use
GPIO LED homecore,gpio-led An LED on a GPIO pin, optionally active-low
Console LED homecore,console-led A stand-in for emulators without LEDs: prints each

change on the console |

Device files

Reading a /dev/ledN file returns its state captured at open, 0\n or 1\n, then end of file. Each write sets the LED: 1 or on lights it, 0 or off turns it off, and toggle inverts it. Surrounding whitespace, such as a trailing newline, is ignored; anything else fails with EINVAL. The file accepts O_CREAT, O_TRUNC, and O_APPEND, so fopen(path, "w") works.

write /dev/led0 on
cat /dev/led0

The functions below are for thread context, like the VFS.

See also
LEDs for the Drivers, their configuration, and tests.

Function Documentation

◆ led_count()

unsigned led_count ( void  )

Report how many LEDs the board describes.

Returns
The number of LEDs; valid indices are 0 to one less.

◆ led_set()

int led_set ( unsigned  index,
bool  on 
)

Turn an LED on or off.

Parameters
indexLED number.
ontrue to light the LED.
Return values
0The LED was set.
-1errno is ENODEV for an index without an LED, or set by the driver.

◆ led_get()

int led_get ( unsigned  index,
bool *  on 
)

Read whether an LED is lit.

Parameters
indexLED number.
[out]ontrue if the LED is lit. Must not be NULL.
Return values
0on holds the state.
-1errno is ENODEV for an index without an LED, or set by the driver.

◆ led_toggle()

int led_toggle ( unsigned  index)

Invert an LED.

Parameters
indexLED number.
Return values
0The LED was inverted.
-1errno as for led_get() and led_set().

◆ led_register_node()

void led_register_node ( vfs_node_t *  node,
const char *  path,
const led_t *  led 
)

Register the device file of an LED.

Called by LED drivers from their init function. Sets up node as a readable and writable device file for led and registers it.

Parameters
nodeNode storage owned by the driver instance; must remain valid for the rest of the program.
pathDevice file path, such as /dev/led0; must remain valid.
ledThe LED; must remain valid for the rest of the program.