Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite APA102 support #10894

Merged
merged 9 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,38 @@ else
endif
endif

RGBLIGHT_ENABLE ?= no
VALID_RGBLIGHT_TYPES := WS2812 APA102 custom

ifeq ($(strip $(RGBLIGHT_CUSTOM_DRIVER)), yes)
RGBLIGHT_DRIVER ?= custom
endif

ifeq ($(strip $(RGBLIGHT_ENABLE)), yes)
POST_CONFIG_H += $(QUANTUM_DIR)/rgblight_post_config.h
OPT_DEFS += -DRGBLIGHT_ENABLE
SRC += $(QUANTUM_DIR)/color.c
SRC += $(QUANTUM_DIR)/rgblight.c
CIE1931_CURVE := yes
RGB_KEYCODES_ENABLE := yes
ifeq ($(strip $(RGBLIGHT_CUSTOM_DRIVER)), yes)
OPT_DEFS += -DRGBLIGHT_CUSTOM_DRIVER
RGBLIGHT_DRIVER ?= WS2812

ifeq ($(filter $(RGBLIGHT_DRIVER),$(VALID_RGBLIGHT_TYPES)),)
$(error RGBLIGHT_DRIVER="$(RGBLIGHT_DRIVER)" is not a valid RGB type)
else
POST_CONFIG_H += $(QUANTUM_DIR)/rgblight_post_config.h
OPT_DEFS += -DRGBLIGHT_ENABLE
SRC += $(QUANTUM_DIR)/color.c
SRC += $(QUANTUM_DIR)/rgblight.c
CIE1931_CURVE := yes
RGB_KEYCODES_ENABLE := yes
endif

ifeq ($(strip $(RGBLIGHT_DRIVER)), WS2812)
WS2812_DRIVER_REQUIRED := yes
endif

ifeq ($(strip $(RGBLIGHT_DRIVER)), APA102)
APA102_DRIVER_REQUIRED := yes
endif

ifeq ($(strip $(RGBLIGHT_DRIVER)), custom)
OPT_DEFS += -DRGBLIGHT_CUSTOM_DRIVER
endif
endif


Expand Down Expand Up @@ -243,6 +263,11 @@ endif
WS2812_DRIVER_REQUIRED := yes
endif

ifeq ($(strip $(RGB_MATRIX_DRIVER)), APA102)
OPT_DEFS += -DAPA102
APA102_DRIVER_REQUIRED := yes
endif

ifeq ($(strip $(RGB_MATRIX_CUSTOM_KB)), yes)
OPT_DEFS += -DRGB_MATRIX_CUSTOM_KB
endif
Expand Down Expand Up @@ -345,6 +370,11 @@ ifeq ($(strip $(WS2812_DRIVER_REQUIRED)), yes)
endif
endif

ifeq ($(strip $(APA102_DRIVER_REQUIRED)), yes)
COMMON_VPATH += $(DRIVER_PATH)/apa102
SRC += apa102.c
endif

ifeq ($(strip $(VISUALIZER_ENABLE)), yes)
CIE1931_CURVE := yes
endif
Expand Down
22 changes: 22 additions & 0 deletions docs/feature_rgb_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ Configure the hardware via your `config.h`:

---

### APA102 :id=apa102

There is basic support for APA102 based addressable LED strands. To enable it, add this to your `rules.mk`:

```makefile
RGB_MATRIX_ENABLE = yes
RGB_MATRIX_DRIVER = APA102
```

Configure the hardware via your `config.h`:

```c
// The pin connected to the data pin of the LEDs
#define RGB_DI_PIN D7
// The pin connected to the clock pin of the LEDs
#define RGB_CI_PIN D6
// The number of LEDs connected
#define DRIVER_LED_TOTAL 70
```

---

From this point forward the configuration is the same for all the drivers. The `led_config_t` struct provides a key electrical matrix to led index lookup table, what the physical position of each LED is on the board, and what type of key or usage the LED if the LED represents. Here is a brief example:

```c
Expand Down
11 changes: 10 additions & 1 deletion docs/feature_rgblight.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Currently QMK supports the following addressable LEDs (however, the white LED in

* WS2811, WS2812, WS2812B, WS2812C, etc.
* SK6812, SK6812MINI, SK6805
* APA102

These LEDs are called "addressable" because instead of using a wire per color, each LED contains a small microchip that understands a special protocol sent over a single wire. The chip passes on the remaining data to the next LED, allowing them to be chained together. In this way, you can easily control the color of the individual LEDs.

Expand All @@ -21,11 +22,19 @@ On keyboards with onboard RGB LEDs, it is usually enabled by default. If it is n
RGBLIGHT_ENABLE = yes
```

At minimum you must define the data pin your LED strip is connected to, and the number of LEDs in the strip, in your `config.h`. If your keyboard has onboard RGB LEDs, and you are simply creating a keymap, you usually won't need to modify these.
For APA102 LEDs, add the following to your `rules.mk`:

```make
RGBLIGHT_ENABLE = yes
RGBLIGHT_DRIVER = APA102
```

At minimum you must define the data pin your LED strip is connected to, and the number of LEDs in the strip, in your `config.h`. For APA102 LEDs, you must also define the clock pin. If your keyboard has onboard RGB LEDs, and you are simply creating a keymap, you usually won't need to modify these.

|Define |Description |
|---------------|---------------------------------------------------------------------------------------------------------|
|`RGB_DI_PIN` |The pin connected to the data pin of the LEDs |
|`RGB_CI_PIN` |The pin connected to the clock pin of the LEDs (APA102 only) |
|`RGBLED_NUM` |The number of LEDs connected |
|`RGBLED_SPLIT` |(Optional) For split keyboards, the number of LEDs connected on each half directly wired to `RGB_DI_PIN` |

Expand Down
151 changes: 151 additions & 0 deletions drivers/apa102/apa102.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* Copyright 2020 Aldehir Rojas
* Copyright 2017 Mikkel (Duckle29)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "apa102.h"
#include "quantum.h"

#ifndef APA102_NOPS
# if defined(__AVR__)
# define APA102_NOPS 0 // AVR at 16 MHz already spends 62.5 ns per clock, so no extra delay is needed
# elif defined(PROTOCOL_CHIBIOS)

# include "hal.h"
# if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX)
# define APA102_NOPS (100 / (1000000000L / (STM32_SYSCLK / 4))) // This calculates how many loops of 4 nops to run to delay 100 ns
# else
# error("APA102_NOPS configuration required")
# define APA102_NOPS 0 // this just pleases the compile so the above error is easier to spot
# endif
# endif
#endif

#define io_wait \
do { \
for (int i = 0; i < APA102_NOPS; i++) { \
__asm__ volatile("nop\n\t" \
"nop\n\t" \
"nop\n\t" \
"nop\n\t"); \
} \
} while (0)

#define APA102_SEND_BIT(byte, bit) \
do { \
writePin(RGB_DI_PIN, (byte >> bit) & 1); \
io_wait; \
writePinHigh(RGB_CI_PIN); \
io_wait; \
writePinLow(RGB_CI_PIN); \
io_wait; \
} while (0)

uint8_t apa102_led_brightness = APA102_DEFAULT_BRIGHTNESS;

void static apa102_start_frame(void);
void static apa102_end_frame(uint16_t num_leds);

void static apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness);
void static apa102_send_byte(uint8_t byte);

void apa102_setleds(LED_TYPE *start_led, uint16_t num_leds) {
LED_TYPE *end = start_led + num_leds;

apa102_start_frame();
for (LED_TYPE *led = start_led; led < end; led++) {
apa102_send_frame(led->r, led->g, led->b, apa102_led_brightness);
}
apa102_end_frame(num_leds);
}

// Overwrite the default rgblight_call_driver to use apa102 driver
void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds) { apa102_setleds(start_led, num_leds); }

void static apa102_init(void) {
setPinOutput(RGB_DI_PIN);
setPinOutput(RGB_CI_PIN);

writePinLow(RGB_DI_PIN);
writePinLow(RGB_CI_PIN);
}

void apa102_set_brightness(uint8_t brightness) {
if (brightness > APA102_MAX_BRIGHTNESS) {
apa102_led_brightness = APA102_MAX_BRIGHTNESS;
} else if (brightness < 0) {
apa102_led_brightness = 0;
} else {
apa102_led_brightness = brightness;
}
}

void static apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness) {
apa102_send_byte(0b11100000 | brightness);
apa102_send_byte(blue);
apa102_send_byte(green);
apa102_send_byte(red);
}

void static apa102_start_frame(void) {
apa102_init();
for (uint16_t i = 0; i < 4; i++) {
apa102_send_byte(0);
}
}

void static apa102_end_frame(uint16_t num_leds) {
// This function has been taken from: https://github.com/pololu/apa102-arduino/blob/master/APA102.h
// and adapted. The code is MIT licensed. I think thats compatible?
//
// The data stream seen by the last LED in the chain will be delayed by
// (count - 1) clock edges, because each LED before it inverts the clock
// line and delays the data by one clock edge. Therefore, to make sure
// the last LED actually receives the data we wrote, the number of extra
// edges we send at the end of the frame must be at least (count - 1).
//
// Assuming we only want to send these edges in groups of size K, the
// C/C++ expression for the minimum number of groups to send is:
//
// ((count - 1) + (K - 1)) / K
//
// The C/C++ expression above is just (count - 1) divided by K,
// rounded up to the nearest whole number if there is a remainder.
//
// We set K to 16 and use the formula above as the number of frame-end
// bytes to transfer. Each byte has 16 clock edges.
//
// We are ignoring the specification for the end frame in the APA102
// datasheet, which says to send 0xFF four times, because it does not work
// when you have 66 LEDs or more, and also it results in unwanted white
// pixels if you try to update fewer LEDs than are on your LED strip.
uint16_t iterations = (num_leds + 14) / 16;
for (uint16_t i = 0; i < iterations; i++) {
apa102_send_byte(0);
}

apa102_init();
}

void static apa102_send_byte(uint8_t byte) {
APA102_SEND_BIT(byte, 7);
APA102_SEND_BIT(byte, 6);
APA102_SEND_BIT(byte, 5);
APA102_SEND_BIT(byte, 4);
APA102_SEND_BIT(byte, 3);
APA102_SEND_BIT(byte, 2);
APA102_SEND_BIT(byte, 1);
APA102_SEND_BIT(byte, 0);
}
32 changes: 14 additions & 18 deletions drivers/avr/apa102.h → drivers/apa102/apa102.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/*
* light weight WS2812 lib include
*
* Version 2.3 - Nev 29th 2015
* Author: Tim (cpldcpu@gmail.com)
*
* Please do not change this file! All configuration is handled in "ws2812_config.h"
/* Copyright 2020 Aldehir Rojas
* Copyright 2017 Mikkel (Duckle29)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,24 +17,25 @@

#pragma once

#include <avr/io.h>
#include <avr/interrupt.h>

#include "color.h"

#ifndef APA102_DEFAULT_BRIGHTNESS
# define APA102_DEFAULT_BRIGHTNESS 31
#endif

#define APA102_MAX_BRIGHTNESS 31

extern uint8_t apa102_led_brightness;

/* User Interface
*
* Input:
* ledarray: An array of GRB data describing the LED colors
* number_of_leds: The number of LEDs to write
* pinmask (optional): Bitmask describing the output bin. e.g. _BV(PB0)
* start_led: An array of GRB data describing the LED colors
* num_leds: The number of LEDs to write
*
* The functions will perform the following actions:
* - Set the data-out pin as output
* - Send out the LED data
* - Wait 50�s to reset the LEDs
*/

void apa102_setleds(LED_TYPE *ledarray, uint16_t number_of_leds);
void apa102_setleds_pin(LED_TYPE *ledarray, uint16_t number_of_leds, uint8_t pinmask);
void apa102_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds);
void apa102_setleds(LED_TYPE *start_led, uint16_t num_leds);
void apa102_set_brightness(uint8_t brightness);
Loading