apa102 1.0
apa102 driver library
Loading...
Searching...
No Matches
apa102.c File Reference

Implementation of LED control functions using SPI interface. More...

#include "apa102.h"
Include dependency graph for apa102.c:

Functions

void apa102_init (void)
 Initialize the LED control interface and hardware.
void apa102_xof (APA102_Transmission type)
 Transmit a specified value repeatedly over SPI to form a data frame.
void apa102_led (const GFX_RGBA_Color *color)
 Send an LED data frame with specified color and intensity.
void apa102_leds (const GFX_RGBA_Color *color)
 Send an LED data frame to all LEDs with specified color and intensity.
void apa102_led_off (void)
 Send an LED data frame to a single LED to turn it off.
void apa102_leds_off (void)
 Disable all LEDs by sending sleep commands (if supported) and turning off the SPI peripheral.

Detailed Description

Implementation of LED control functions using SPI interface.

This source file provides functions to initialize and control addressable APA102 LEDs, including setting colors and turning LEDs on or off. It utilizes the SPI communication peripheral to send LED data frames.

Author
g.raf
Date
2026-02-20
Version
1.0 Release
Note
This file is part of a larger embedded systems project and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/drivers-led-apa102 "APA102 LED Driver GitHub Repository"

Function Documentation

◆ apa102_init()

void apa102_init ( void )

Initialize the LED control interface and hardware.

This function initializes the SPI peripheral to communicate with the LEDs using most significant bit (MSB) first, with clock polarity and phase set to rising edges. After SPI initialization, it sends a start-of-frame (SOF) signal followed by initializing all configured LEDs with the enable flag and zero color data (LEDs initially off). Finally, it sends an end-of-frame (EOF) signal to mark completion of the initialization sequence. This setup prepares the LEDs for subsequent color and blink control operations by configuring the communication and initial states.

Note
This function must be called before any other LED control operations. It assumes SPI_MSB, SPI_Rising macros and LED frame configurations are correctly defined.
See also
APA102_SOF() and APA102_EOF() macros for frame delimiters.
apa102_frame() for sending individual LED data frames.

◆ apa102_led()

void apa102_led ( const GFX_RGBA_Color * color)

Send an LED data frame with specified color and intensity.

Parameters
colorLED_Data structure containing intensity and RGB color values.

This function constructs and transmits a single LED data frame over SPI, combining the LED enable flag with the masked intensity value, followed by the blue, green, and red color components. The intensity value is masked with 0x3F to limit it to valid bits.

The frame format is:

  • Mode byte: LED_ENABLE_FLAG OR'ed with intensity (0x3F mask).
  • Blue color byte.
  • Green color byte.
  • Red color byte.
Note
Ensure the LED is initialized before calling this function.

◆ apa102_led_off()

void apa102_led_off ( void )

Send an LED data frame to a single LED to turn it off.

This function constructs and transmits a single LED data frame covered with zeroes over SPI to a single LED, combining the LED enable flag with the minimum intensity value to switch the LED off.

The frame format is:

  • Mode byte: LED_ENABLE_FLAG or APA102_SLEEP_FLAG if supported
  • Intensity byte: APA102_MIN_INTENSITY (minimum brightness)
  • Blue color byte set to zero.
  • Green color byte set to zero.
  • Red color byte set to zero.
Note
Ensure the LED is initialized before calling this function.
Here is the caller graph for this function:

◆ apa102_leds()

void apa102_leds ( const GFX_RGBA_Color * color)

Send an LED data frame to all LEDs with specified color and intensity.

Parameters
colorLED_Data structure containing intensity and RGB color values.

This function constructs and transmits a single LED data frame over SPI to multiple LEDs, combining the LED enable flag with the masked intensity value, followed by the blue, green, and red color components. The intensity value is masked with 0x3F to limit it to valid bits.

The frame format is:

  • Mode byte: LED_ENABLE_FLAG OR'ed with intensity (0x3F mask).
  • Blue color byte.
  • Green color byte.
  • Red color byte.
Note
Ensure the LED is initialized before calling this function.

◆ apa102_leds_off()

void apa102_leds_off ( void )

Disable all LEDs by sending sleep commands (if supported) and turning off the SPI peripheral.

This function sends sleep commands (if supported) to all configured LEDs to put them into a low power state and disables all leds.

This operation is used to safely turn off the LEDs and reduce power consumption when LED functionality is not needed.

Note
Ensure no ongoing LED data transmission occurs before calling this function to avoid communication issues with the LED hardware. After calling this function, the SPI peripheral may be disabled to save power.
Here is the call graph for this function:

◆ apa102_xof()

void apa102_xof ( APA102_Transmission type)

Transmit a specified value repeatedly over SPI to form a data frame.

Parameters
valueThe byte value to be sent repeatedly.

This function sends the given value repeatedly for LED_FRAME_SIZE times via SPI using the spi_transfer function. It is commonly used to send start or stop frames for LED data sequences to synchronize communication with the LED hardware.

Note
The function blocks until all bytes are transmitted. Ensure the SPI interface is initialized before calling this function.
Here is the call graph for this function: