logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

apa102.c (5885B)


  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. #include "apa102.h"
  18. #include "gpio.h"
  19. #ifndef APA102_NOPS
  20. # if defined(__AVR__)
  21. # define APA102_NOPS 0 // AVR at 16 MHz already spends 62.5 ns per clock, so no extra delay is needed
  22. # elif defined(PROTOCOL_CHIBIOS)
  23. # include "hal.h"
  24. # include "chibios_config.h"
  25. # if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX) || defined(AT32F415) || defined(GD32VF103) || defined(MCU_RP)
  26. # define APA102_NOPS (100 / (1000000000L / (CPU_CLOCK / 4))) // This calculates how many loops of 4 nops to run to delay 100 ns
  27. # else
  28. # error APA102_NOPS configuration required
  29. # define APA102_NOPS 0 // this just pleases the compile so the above error is easier to spot
  30. # endif
  31. # endif
  32. #endif
  33. #define io_wait \
  34. do { \
  35. for (int i = 0; i < APA102_NOPS; i++) { \
  36. __asm__ volatile("nop\n\t" \
  37. "nop\n\t" \
  38. "nop\n\t" \
  39. "nop\n\t"); \
  40. } \
  41. } while (0)
  42. #define APA102_SEND_BIT(byte, bit) \
  43. do { \
  44. gpio_write_pin(APA102_DI_PIN, (byte >> bit) & 1); \
  45. io_wait; \
  46. gpio_write_pin_high(APA102_CI_PIN); \
  47. io_wait; \
  48. gpio_write_pin_low(APA102_CI_PIN); \
  49. io_wait; \
  50. } while (0)
  51. rgb_t apa102_leds[APA102_LED_COUNT];
  52. uint8_t apa102_led_brightness = APA102_DEFAULT_BRIGHTNESS;
  53. static void apa102_send_byte(uint8_t byte) {
  54. APA102_SEND_BIT(byte, 7);
  55. APA102_SEND_BIT(byte, 6);
  56. APA102_SEND_BIT(byte, 5);
  57. APA102_SEND_BIT(byte, 4);
  58. APA102_SEND_BIT(byte, 3);
  59. APA102_SEND_BIT(byte, 2);
  60. APA102_SEND_BIT(byte, 1);
  61. APA102_SEND_BIT(byte, 0);
  62. }
  63. static void apa102_start_frame(void) {
  64. gpio_write_pin_low(APA102_DI_PIN);
  65. gpio_write_pin_low(APA102_CI_PIN);
  66. for (uint16_t i = 0; i < 4; i++) {
  67. apa102_send_byte(0);
  68. }
  69. }
  70. static void apa102_end_frame(uint16_t num_leds) {
  71. // This function has been taken from: https://github.com/pololu/apa102-arduino/blob/master/APA102.h
  72. // and adapted. The code is MIT licensed. I think thats compatible?
  73. //
  74. // The data stream seen by the last LED in the chain will be delayed by
  75. // (count - 1) clock edges, because each LED before it inverts the clock
  76. // line and delays the data by one clock edge. Therefore, to make sure
  77. // the last LED actually receives the data we wrote, the number of extra
  78. // edges we send at the end of the frame must be at least (count - 1).
  79. //
  80. // Assuming we only want to send these edges in groups of size K, the
  81. // C/C++ expression for the minimum number of groups to send is:
  82. //
  83. // ((count - 1) + (K - 1)) / K
  84. //
  85. // The C/C++ expression above is just (count - 1) divided by K,
  86. // rounded up to the nearest whole number if there is a remainder.
  87. //
  88. // We set K to 16 and use the formula above as the number of frame-end
  89. // bytes to transfer. Each byte has 16 clock edges.
  90. //
  91. // We are ignoring the specification for the end frame in the APA102
  92. // datasheet, which says to send 0xFF four times, because it does not work
  93. // when you have 66 LEDs or more, and also it results in unwanted white
  94. // pixels if you try to update fewer LEDs than are on your LED strip.
  95. uint16_t iterations = (num_leds + 14) / 16;
  96. for (uint16_t i = 0; i < iterations; i++) {
  97. apa102_send_byte(0);
  98. }
  99. gpio_write_pin_low(APA102_DI_PIN);
  100. gpio_write_pin_low(APA102_CI_PIN);
  101. }
  102. static void apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness) {
  103. apa102_send_byte(0b11100000 | brightness);
  104. apa102_send_byte(blue);
  105. apa102_send_byte(green);
  106. apa102_send_byte(red);
  107. }
  108. void apa102_init(void) {
  109. gpio_set_pin_output(APA102_DI_PIN);
  110. gpio_set_pin_output(APA102_CI_PIN);
  111. }
  112. void apa102_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  113. apa102_leds[index].r = red;
  114. apa102_leds[index].g = green;
  115. apa102_leds[index].b = blue;
  116. }
  117. void apa102_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  118. for (uint16_t i = 0; i < APA102_LED_COUNT; i++) {
  119. apa102_set_color(i, red, green, blue);
  120. }
  121. }
  122. void apa102_flush(void) {
  123. apa102_start_frame();
  124. for (uint8_t i = 0; i < APA102_LED_COUNT; i++) {
  125. apa102_send_frame(apa102_leds[i].r, apa102_leds[i].g, apa102_leds[i].b, apa102_led_brightness);
  126. }
  127. apa102_end_frame(APA102_LED_COUNT);
  128. }
  129. void apa102_set_brightness(uint8_t brightness) {
  130. if (brightness > APA102_MAX_BRIGHTNESS) {
  131. apa102_led_brightness = APA102_MAX_BRIGHTNESS;
  132. } else if (brightness < 0) {
  133. apa102_led_brightness = 0;
  134. } else {
  135. apa102_led_brightness = brightness;
  136. }
  137. }