PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

135 rindas
3.7KB

  1. #ifndef __INC_FASTSPI_APOLLO3_H
  2. #define __INC_FASTSPI_APOLLO3_H
  3. // This is the implementation of fastspi for the Apollo3.
  4. // It uses fastgpio instead of actual SPI, which means you can use it on all pins.
  5. // It can run slightly faster than the default fastpin (bit banging).
  6. #include "FastLED.h"
  7. FASTLED_NAMESPACE_BEGIN
  8. #if defined(FASTLED_APOLLO3)
  9. #define FASTLED_ALL_PINS_HARDWARE_SPI
  10. template <uint8_t _DATA_PIN, uint8_t _CLOCK_PIN, uint32_t _SPI_CLOCK_DIVIDER>
  11. class APOLLO3HardwareSPIOutput {
  12. Selectable *m_pSelect;
  13. public:
  14. APOLLO3HardwareSPIOutput() { m_pSelect = NULL; }
  15. APOLLO3HardwareSPIOutput(Selectable *pSelect) { m_pSelect = pSelect; }
  16. // set the object representing the selectable
  17. void setSelect(Selectable *pSelect) { m_pSelect = pSelect; }
  18. // initialize the pins for fastgpio
  19. void init() {
  20. FastPin<_CLOCK_PIN>::setOutput();
  21. FastPin<_CLOCK_PIN>::lo();
  22. FastPin<_DATA_PIN>::setOutput();
  23. FastPin<_DATA_PIN>::lo();
  24. }
  25. // latch the CS select
  26. void inline select() { /* TODO */ }
  27. // release the CS select
  28. void inline release() { /* TODO */ }
  29. // wait until all queued up data has been written
  30. static void waitFully() { /* TODO */ }
  31. // write a byte as bits
  32. static void writeByte(uint8_t b) {
  33. writeBit<7>(b);
  34. writeBit<6>(b);
  35. writeBit<5>(b);
  36. writeBit<4>(b);
  37. writeBit<3>(b);
  38. writeBit<2>(b);
  39. writeBit<1>(b);
  40. writeBit<0>(b);
  41. }
  42. // write a word out via SPI (returns immediately on writing register)
  43. static void writeWord(uint16_t w) {
  44. writeByte((uint8_t)((w >> 8) & 0xff));
  45. writeByte((uint8_t)(w & 0xff));
  46. }
  47. // A raw set of writing byte values, assumes setup/init/waiting done elsewhere
  48. static void writeBytesValueRaw(uint8_t value, int len) {
  49. while(len--) { writeByte(value); }
  50. }
  51. // A full cycle of writing a value for len bytes, including select, release, and waiting
  52. void writeBytesValue(uint8_t value, int len) {
  53. select();
  54. writeBytesValueRaw(value, len);
  55. release();
  56. }
  57. // A full cycle of writing a value for len bytes, including select, release, and waiting
  58. template <class D> void writeBytes(register uint8_t *data, int len) {
  59. uint8_t *end = data + len;
  60. select();
  61. // could be optimized to write 16bit words out instead of 8bit bytes
  62. while(data != end) {
  63. writeByte(D::adjust(*data++));
  64. }
  65. D::postBlock(len);
  66. waitFully();
  67. release();
  68. }
  69. // A full cycle of writing a value for len bytes, including select, release, and waiting
  70. void writeBytes(register uint8_t *data, int len) { writeBytes<DATA_NOP>(data, len); }
  71. // write a single bit out, which bit from the passed in byte is determined by template parameter
  72. template <uint8_t BIT> inline static void writeBit(uint8_t b) {
  73. //waitFully();
  74. if(b & (1 << BIT)) {
  75. FastPin<_DATA_PIN>::hi();
  76. } else {
  77. FastPin<_DATA_PIN>::lo();
  78. }
  79. FastPin<_CLOCK_PIN>::hi();
  80. for (uint32_t d = (_SPI_CLOCK_DIVIDER >> 1); d > 0; d--) { __NOP(); }
  81. FastPin<_CLOCK_PIN>::lo();
  82. for (uint32_t d = (_SPI_CLOCK_DIVIDER >> 1); d > 0; d--) { __NOP(); }
  83. }
  84. // write a block of uint8_ts out in groups of three. len is the total number of uint8_ts to write out. The template
  85. // parameters indicate how many uint8_ts to skip at the beginning and/or end of each grouping
  86. template <uint8_t FLAGS, class D, EOrder RGB_ORDER> void writePixels(PixelController<RGB_ORDER> pixels) {
  87. select();
  88. int len = pixels.mLen;
  89. while(pixels.has(1)) {
  90. if(FLAGS & FLAG_START_BIT) {
  91. writeBit<0>(1);
  92. }
  93. writeByte(D::adjust(pixels.loadAndScale0()));
  94. writeByte(D::adjust(pixels.loadAndScale1()));
  95. writeByte(D::adjust(pixels.loadAndScale2()));
  96. pixels.advanceData();
  97. pixels.stepDithering();
  98. }
  99. D::postBlock(len);
  100. //waitFully();
  101. release();
  102. }
  103. };
  104. #endif
  105. FASTLED_NAMESPACE_END
  106. #endif