kendyte_k210.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // This is a mash-up of the Due show() code + insights from Michael Miller's
  2. // ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus
  3. // Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution.
  4. #if defined(K210)
  5. #define KENDRYTE_K210 1
  6. #endif
  7. #if defined(KENDRYTE_K210)
  8. #include <Arduino.h>
  9. #include "sysctl.h"
  10. void k210Show(
  11. uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz)
  12. {
  13. #define CYCLES_800_T0H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 2500000) // 0.4us
  14. #define CYCLES_800_T1H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 1250000) // 0.8us
  15. #define CYCLES_800 (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 800000) // 1.25us per bit
  16. #define CYCLES_400_T0H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 2000000) // 0.5uS
  17. #define CYCLES_400_T1H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 833333) // 1.2us
  18. #define CYCLES_400 (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 400000) // 2.5us per bit
  19. uint8_t *p, *end, pix, mask;
  20. uint32_t t, time0, time1, period, c, startTime;
  21. p = pixels;
  22. end = p + numBytes;
  23. pix = *p++;
  24. mask = 0x80;
  25. startTime = 0;
  26. #ifdef NEO_KHZ400
  27. if (is800KHz)
  28. {
  29. #endif
  30. time0 = CYCLES_800_T0H;
  31. time1 = CYCLES_800_T1H;
  32. period = CYCLES_800;
  33. #ifdef NEO_KHZ400
  34. }
  35. else
  36. { // 400 KHz bitstream
  37. time0 = CYCLES_400_T0H;
  38. time1 = CYCLES_400_T1H;
  39. period = CYCLES_400;
  40. }
  41. #endif
  42. for (t = time0;; t = time0)
  43. {
  44. if (pix & mask)
  45. t = time1; // Bit high duration
  46. while (((c = read_cycle()) - startTime) < period)
  47. ; // Wait for bit start
  48. digitalWrite(pin, HIGH);
  49. startTime = c; // Save start time
  50. while (((c = read_cycle()) - startTime) < t)
  51. ; // Wait high duration
  52. digitalWrite(pin, LOW);
  53. if (!(mask >>= 1))
  54. { // Next bit/byte
  55. if (p >= end)
  56. break;
  57. pix = *p++;
  58. mask = 0x80;
  59. }
  60. }
  61. while ((read_cycle() - startTime) < period)
  62. ; // Wait for last bit
  63. }
  64. #endif // KENDRYTE_K210