You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.5 KiB

  1. #include "quantum.h"
  2. #include "ws2812.h"
  3. #include <ch.h>
  4. #include <hal.h>
  5. /* Adapted from https://github.com/bigjosh/SimpleNeoPixelDemo/ */
  6. #ifndef NOP_FUDGE
  7. # if defined(STM32F0XX) || defined(STM32F1XX) || defined(GD32VF103) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX)
  8. # define NOP_FUDGE 0.4
  9. # else
  10. # error("NOP_FUDGE configuration required")
  11. # define NOP_FUDGE 1 // this just pleases the compile so the above error is easier to spot
  12. # endif
  13. #endif
  14. // Push Pull or Open Drain Configuration
  15. // Default Push Pull
  16. #ifndef WS2812_EXTERNAL_PULLUP
  17. # define WS2812_OUTPUT_MODE PAL_MODE_OUTPUT_PUSHPULL
  18. #else
  19. # define WS2812_OUTPUT_MODE PAL_MODE_OUTPUT_OPENDRAIN
  20. #endif
  21. // The reset gap can be 6000 ns, but depending on the LED strip it may have to be increased
  22. // to values like 600000 ns. If it is too small, the pixels will show nothing most of the time.
  23. #ifndef WS2812_RES
  24. # define WS2812_RES (1000 * WS2812_TRST_US) // Width of the low gap between bits to cause a frame to latch
  25. #endif
  26. #define NUMBER_NOPS 6
  27. #define CYCLES_PER_SEC (CPU_CLOCK / NUMBER_NOPS * NOP_FUDGE)
  28. #define NS_PER_SEC (1000000000L) // Note that this has to be SIGNED since we want to be able to check for negative values of derivatives
  29. #define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC)
  30. #define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE)
  31. #define wait_ns(x) \
  32. do { \
  33. for (int i = 0; i < NS_TO_CYCLES(x); i++) { \
  34. __asm__ volatile("nop\n\t" \
  35. "nop\n\t" \
  36. "nop\n\t" \
  37. "nop\n\t" \
  38. "nop\n\t" \
  39. "nop\n\t"); \
  40. } \
  41. } while (0)
  42. void sendByte(uint8_t byte) {
  43. // WS2812 protocol wants most significant bits first
  44. for (unsigned char bit = 0; bit < 8; bit++) {
  45. bool is_one = byte & (1 << (7 - bit));
  46. // using something like wait_ns(is_one ? T1L : T0L) here throws off timings
  47. if (is_one) {
  48. // 1
  49. writePinHigh(RGB_DI_PIN);
  50. wait_ns(WS2812_T1H);
  51. writePinLow(RGB_DI_PIN);
  52. wait_ns(WS2812_T1L);
  53. } else {
  54. // 0
  55. writePinHigh(RGB_DI_PIN);
  56. wait_ns(WS2812_T0H);
  57. writePinLow(RGB_DI_PIN);
  58. wait_ns(WS2812_T0L);
  59. }
  60. }
  61. }
  62. void ws2812_init(void) {
  63. palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE);
  64. }
  65. // Setleds for standard RGB
  66. void ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) {
  67. static bool s_init = false;
  68. if (!s_init) {
  69. ws2812_init();
  70. s_init = true;
  71. }
  72. // this code is very time dependent, so we need to disable interrupts
  73. chSysLock();
  74. for (uint8_t i = 0; i < leds; i++) {
  75. // WS2812 protocol dictates grb order
  76. #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
  77. sendByte(ledarray[i].g);
  78. sendByte(ledarray[i].r);
  79. sendByte(ledarray[i].b);
  80. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
  81. sendByte(ledarray[i].r);
  82. sendByte(ledarray[i].g);
  83. sendByte(ledarray[i].b);
  84. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
  85. sendByte(ledarray[i].b);
  86. sendByte(ledarray[i].g);
  87. sendByte(ledarray[i].r);
  88. #endif
  89. #ifdef RGBW
  90. sendByte(ledarray[i].w);
  91. #endif
  92. }
  93. wait_ns(WS2812_RES);
  94. chSysUnlock();
  95. }