diff options
Diffstat (limited to 'drivers/led/apa102.c')
| -rw-r--r-- | drivers/led/apa102.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/drivers/led/apa102.c b/drivers/led/apa102.c new file mode 100644 index 000000000..7396dc3c5 --- /dev/null +++ b/drivers/led/apa102.c | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | /* Copyright 2020 Aldehir Rojas | ||
| 2 | * Copyright 2017 Mikkel (Duckle29) | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "apa102.h" | ||
| 19 | #include "quantum.h" | ||
| 20 | |||
| 21 | #ifndef APA102_NOPS | ||
| 22 | # if defined(__AVR__) | ||
| 23 | # define APA102_NOPS 0 // AVR at 16 MHz already spends 62.5 ns per clock, so no extra delay is needed | ||
| 24 | # elif defined(PROTOCOL_CHIBIOS) | ||
| 25 | |||
| 26 | # include "hal.h" | ||
| 27 | # if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX) | ||
| 28 | # define APA102_NOPS (100 / (1000000000L / (STM32_SYSCLK / 4))) // This calculates how many loops of 4 nops to run to delay 100 ns | ||
| 29 | # else | ||
| 30 | # error("APA102_NOPS configuration required") | ||
| 31 | # define APA102_NOPS 0 // this just pleases the compile so the above error is easier to spot | ||
| 32 | # endif | ||
| 33 | # endif | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #define io_wait \ | ||
| 37 | do { \ | ||
| 38 | for (int i = 0; i < APA102_NOPS; i++) { \ | ||
| 39 | __asm__ volatile("nop\n\t" \ | ||
| 40 | "nop\n\t" \ | ||
| 41 | "nop\n\t" \ | ||
| 42 | "nop\n\t"); \ | ||
| 43 | } \ | ||
| 44 | } while (0) | ||
| 45 | |||
| 46 | #define APA102_SEND_BIT(byte, bit) \ | ||
| 47 | do { \ | ||
| 48 | writePin(RGB_DI_PIN, (byte >> bit) & 1); \ | ||
| 49 | io_wait; \ | ||
| 50 | writePinHigh(RGB_CI_PIN); \ | ||
| 51 | io_wait; \ | ||
| 52 | writePinLow(RGB_CI_PIN); \ | ||
| 53 | io_wait; \ | ||
| 54 | } while (0) | ||
| 55 | |||
| 56 | uint8_t apa102_led_brightness = APA102_DEFAULT_BRIGHTNESS; | ||
| 57 | |||
| 58 | void static apa102_start_frame(void); | ||
| 59 | void static apa102_end_frame(uint16_t num_leds); | ||
| 60 | |||
| 61 | void static apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness); | ||
| 62 | void static apa102_send_byte(uint8_t byte); | ||
| 63 | |||
| 64 | void apa102_setleds(LED_TYPE *start_led, uint16_t num_leds) { | ||
| 65 | LED_TYPE *end = start_led + num_leds; | ||
| 66 | |||
| 67 | apa102_start_frame(); | ||
| 68 | for (LED_TYPE *led = start_led; led < end; led++) { | ||
| 69 | apa102_send_frame(led->r, led->g, led->b, apa102_led_brightness); | ||
| 70 | } | ||
| 71 | apa102_end_frame(num_leds); | ||
| 72 | } | ||
| 73 | |||
| 74 | // Overwrite the default rgblight_call_driver to use apa102 driver | ||
| 75 | void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds) { apa102_setleds(start_led, num_leds); } | ||
| 76 | |||
| 77 | void static apa102_init(void) { | ||
| 78 | setPinOutput(RGB_DI_PIN); | ||
| 79 | setPinOutput(RGB_CI_PIN); | ||
| 80 | |||
| 81 | writePinLow(RGB_DI_PIN); | ||
| 82 | writePinLow(RGB_CI_PIN); | ||
| 83 | } | ||
| 84 | |||
| 85 | void apa102_set_brightness(uint8_t brightness) { | ||
| 86 | if (brightness > APA102_MAX_BRIGHTNESS) { | ||
| 87 | apa102_led_brightness = APA102_MAX_BRIGHTNESS; | ||
| 88 | } else if (brightness < 0) { | ||
| 89 | apa102_led_brightness = 0; | ||
| 90 | } else { | ||
| 91 | apa102_led_brightness = brightness; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | void static apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness) { | ||
| 96 | apa102_send_byte(0b11100000 | brightness); | ||
| 97 | apa102_send_byte(blue); | ||
| 98 | apa102_send_byte(green); | ||
| 99 | apa102_send_byte(red); | ||
| 100 | } | ||
| 101 | |||
| 102 | void static apa102_start_frame(void) { | ||
| 103 | apa102_init(); | ||
| 104 | for (uint16_t i = 0; i < 4; i++) { | ||
| 105 | apa102_send_byte(0); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | void static apa102_end_frame(uint16_t num_leds) { | ||
| 110 | // This function has been taken from: https://github.com/pololu/apa102-arduino/blob/master/APA102.h | ||
| 111 | // and adapted. The code is MIT licensed. I think thats compatible? | ||
| 112 | // | ||
| 113 | // The data stream seen by the last LED in the chain will be delayed by | ||
| 114 | // (count - 1) clock edges, because each LED before it inverts the clock | ||
| 115 | // line and delays the data by one clock edge. Therefore, to make sure | ||
| 116 | // the last LED actually receives the data we wrote, the number of extra | ||
| 117 | // edges we send at the end of the frame must be at least (count - 1). | ||
| 118 | // | ||
| 119 | // Assuming we only want to send these edges in groups of size K, the | ||
| 120 | // C/C++ expression for the minimum number of groups to send is: | ||
| 121 | // | ||
| 122 | // ((count - 1) + (K - 1)) / K | ||
| 123 | // | ||
| 124 | // The C/C++ expression above is just (count - 1) divided by K, | ||
| 125 | // rounded up to the nearest whole number if there is a remainder. | ||
| 126 | // | ||
| 127 | // We set K to 16 and use the formula above as the number of frame-end | ||
| 128 | // bytes to transfer. Each byte has 16 clock edges. | ||
| 129 | // | ||
| 130 | // We are ignoring the specification for the end frame in the APA102 | ||
| 131 | // datasheet, which says to send 0xFF four times, because it does not work | ||
| 132 | // when you have 66 LEDs or more, and also it results in unwanted white | ||
| 133 | // pixels if you try to update fewer LEDs than are on your LED strip. | ||
| 134 | uint16_t iterations = (num_leds + 14) / 16; | ||
| 135 | for (uint16_t i = 0; i < iterations; i++) { | ||
| 136 | apa102_send_byte(0); | ||
| 137 | } | ||
| 138 | |||
| 139 | apa102_init(); | ||
| 140 | } | ||
| 141 | |||
| 142 | void static apa102_send_byte(uint8_t byte) { | ||
| 143 | APA102_SEND_BIT(byte, 7); | ||
| 144 | APA102_SEND_BIT(byte, 6); | ||
| 145 | APA102_SEND_BIT(byte, 5); | ||
| 146 | APA102_SEND_BIT(byte, 4); | ||
| 147 | APA102_SEND_BIT(byte, 3); | ||
| 148 | APA102_SEND_BIT(byte, 2); | ||
| 149 | APA102_SEND_BIT(byte, 1); | ||
| 150 | APA102_SEND_BIT(byte, 0); | ||
| 151 | } | ||
