SmartLeds.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. #pragma once
  2. /*
  3. * A C++ driver for the WS2812 LEDs using the RMT peripheral on the ESP32.
  4. *
  5. * Jan "yaqwsx" Mrázek <email@honzamrazek.cz>
  6. *
  7. * Based on the work by Martin F. Falatic - https://github.com/FozzTexx/ws2812-demo
  8. */
  9. /*
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <memory>
  29. #include <cassert>
  30. #include <cstring>
  31. #if defined ( ARDUINO )
  32. extern "C" { // ...someone forgot to put in the includes...
  33. #include "esp32-hal.h"
  34. #include "esp_intr_alloc.h"
  35. #include "esp_ipc.h"
  36. #include "driver/gpio.h"
  37. #include "driver/periph_ctrl.h"
  38. #include "freertos/semphr.h"
  39. #include "soc/rmt_struct.h"
  40. #include <driver/spi_master.h>
  41. #include "esp_idf_version.h"
  42. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL( 4, 0, 0 )
  43. #include "soc/dport_reg.h"
  44. #endif
  45. }
  46. #elif defined ( ESP_PLATFORM )
  47. extern "C" { // ...someone forgot to put in the includes...
  48. #include <esp_intr_alloc.h>
  49. #include <esp_ipc.h>
  50. #include <driver/gpio.h>
  51. #include <freertos/FreeRTOS.h>
  52. #include <freertos/semphr.h>
  53. #include <soc/dport_reg.h>
  54. #include <soc/gpio_sig_map.h>
  55. #include <soc/rmt_struct.h>
  56. #include <driver/spi_master.h>
  57. }
  58. #include <stdio.h>
  59. #endif
  60. #include "Color.h"
  61. namespace detail {
  62. struct TimingParams {
  63. uint32_t T0H;
  64. uint32_t T1H;
  65. uint32_t T0L;
  66. uint32_t T1L;
  67. uint32_t TRS;
  68. };
  69. union RmtPulsePair {
  70. struct {
  71. int duration0:15;
  72. int level0:1;
  73. int duration1:15;
  74. int level1:1;
  75. };
  76. uint32_t value;
  77. };
  78. static const int DIVIDER = 4; // 8 still seems to work, but timings become marginal
  79. static const int MAX_PULSES = 32; // A channel has a 64 "pulse" buffer - we use half per pass
  80. static const double RMT_DURATION_NS = 12.5; // minimum time of a single RMT duration based on clock ns
  81. } // namespace detail
  82. using LedType = detail::TimingParams;
  83. static const LedType LED_WS2812 = { 350, 700, 800, 600, 50000 };
  84. static const LedType LED_WS2812B = { 400, 850, 850, 400, 50100 };
  85. static const LedType LED_SK6812 = { 300, 600, 900, 600, 80000 };
  86. static const LedType LED_WS2813 = { 350, 800, 350, 350, 300000 };
  87. enum BufferType { SingleBuffer = 0, DoubleBuffer };
  88. enum IsrCore { CoreFirst = 0, CoreSecond = 1, CoreCurrent = 2};
  89. class SmartLed {
  90. public:
  91. // The RMT interrupt must not run on the same core as WiFi interrupts, otherwise SmartLeds
  92. // can't fill the RMT buffer fast enough, resulting in rendering artifacts.
  93. // Usually, that means you have to set isrCore == CoreSecond.
  94. //
  95. // If you use anything other than CoreCurrent, the FreeRTOS scheduler MUST be already running,
  96. // so you can't use it if you define SmartLed as global variable.
  97. SmartLed( const LedType& type, int count, int pin, int channel = 0, BufferType doubleBuffer = SingleBuffer, IsrCore isrCore = CoreCurrent)
  98. : _timing( type ),
  99. _channel( channel ),
  100. _count( count ),
  101. _firstBuffer( new Rgb[ count ] ),
  102. _secondBuffer( doubleBuffer ? new Rgb[ count ] : nullptr ),
  103. _finishedFlag( xSemaphoreCreateBinary() )
  104. {
  105. assert( channel >= 0 && channel < 8 );
  106. assert( ledForChannel( channel ) == nullptr );
  107. xSemaphoreGive( _finishedFlag );
  108. DPORT_SET_PERI_REG_MASK( DPORT_PERIP_CLK_EN_REG, DPORT_RMT_CLK_EN );
  109. DPORT_CLEAR_PERI_REG_MASK( DPORT_PERIP_RST_EN_REG, DPORT_RMT_RST );
  110. PIN_FUNC_SELECT( GPIO_PIN_MUX_REG[ pin ], 2 );
  111. gpio_set_direction( static_cast< gpio_num_t >( pin ), GPIO_MODE_OUTPUT );
  112. gpio_matrix_out( static_cast< gpio_num_t >( pin ), RMT_SIG_OUT0_IDX + _channel, 0, 0 );
  113. initChannel( _channel );
  114. RMT.tx_lim_ch[ _channel ].limit = detail::MAX_PULSES;
  115. RMT.int_ena.val |= 1 << ( 24 + _channel );
  116. RMT.int_ena.val |= 1 << ( 3 * _channel );
  117. _bitToRmt[ 0 ].level0 = 1;
  118. _bitToRmt[ 0 ].level1 = 0;
  119. _bitToRmt[ 0 ].duration0 = _timing.T0H / ( detail::RMT_DURATION_NS * detail::DIVIDER );
  120. _bitToRmt[ 0 ].duration1 = _timing.T0L / ( detail::RMT_DURATION_NS * detail::DIVIDER );
  121. _bitToRmt[ 1 ].level0 = 1;
  122. _bitToRmt[ 1 ].level1 = 0;
  123. _bitToRmt[ 1 ].duration0 = _timing.T1H / ( detail::RMT_DURATION_NS * detail::DIVIDER );
  124. _bitToRmt[ 1 ].duration1 = _timing.T1L / ( detail::RMT_DURATION_NS * detail::DIVIDER );
  125. if ( !anyAlive() ) {
  126. _interruptCore = isrCore;
  127. if(isrCore != CoreCurrent) {
  128. ESP_ERROR_CHECK(esp_ipc_call_blocking(isrCore, registerInterrupt, NULL));
  129. } else {
  130. registerInterrupt(NULL);
  131. }
  132. }
  133. ledForChannel( channel ) = this;
  134. }
  135. ~SmartLed() {
  136. ledForChannel( _channel ) = nullptr;
  137. if ( !anyAlive() ) {
  138. if(_interruptCore != CoreCurrent) {
  139. ESP_ERROR_CHECK(esp_ipc_call_blocking(_interruptCore, unregisterInterrupt, NULL));
  140. } else {
  141. unregisterInterrupt(NULL);
  142. }
  143. }
  144. vSemaphoreDelete( _finishedFlag );
  145. }
  146. Rgb& operator[]( int idx ) {
  147. return _firstBuffer[ idx ];
  148. }
  149. const Rgb& operator[]( int idx ) const {
  150. return _firstBuffer[ idx ];
  151. }
  152. void show() {
  153. _buffer = _firstBuffer.get();
  154. startTransmission();
  155. swapBuffers();
  156. }
  157. bool wait( TickType_t timeout = portMAX_DELAY ) {
  158. if( xSemaphoreTake( _finishedFlag, timeout ) == pdTRUE ) {
  159. xSemaphoreGive( _finishedFlag );
  160. return true;
  161. }
  162. return false;
  163. }
  164. int size() const {
  165. return _count;
  166. }
  167. Rgb *begin() { return _firstBuffer.get(); }
  168. const Rgb *begin() const { return _firstBuffer.get(); }
  169. const Rgb *cbegin() const { return _firstBuffer.get(); }
  170. Rgb *end() { return _firstBuffer.get() + _count; }
  171. const Rgb *end() const { return _firstBuffer.get() + _count; }
  172. const Rgb *cend() const { return _firstBuffer.get() + _count; }
  173. private:
  174. static intr_handle_t _interruptHandle;
  175. static IsrCore _interruptCore;
  176. static void initChannel( int channel ) {
  177. RMT.apb_conf.fifo_mask = 1; //enable memory access, instead of FIFO mode.
  178. RMT.apb_conf.mem_tx_wrap_en = 1; //wrap around when hitting end of buffer
  179. RMT.conf_ch[ channel ].conf0.div_cnt = detail::DIVIDER;
  180. RMT.conf_ch[ channel ].conf0.mem_size = 1;
  181. RMT.conf_ch[ channel ].conf0.carrier_en = 0;
  182. RMT.conf_ch[ channel ].conf0.carrier_out_lv = 1;
  183. RMT.conf_ch[ channel ].conf0.mem_pd = 0;
  184. RMT.conf_ch[ channel ].conf1.rx_en = 0;
  185. RMT.conf_ch[ channel ].conf1.mem_owner = 0;
  186. RMT.conf_ch[ channel ].conf1.tx_conti_mode = 0; //loop back mode.
  187. RMT.conf_ch[ channel ].conf1.ref_always_on = 1; // use apb clock: 80M
  188. RMT.conf_ch[ channel ].conf1.idle_out_en = 1;
  189. RMT.conf_ch[ channel ].conf1.idle_out_lv = 0;
  190. }
  191. static void registerInterrupt(void *) {
  192. ESP_ERROR_CHECK(esp_intr_alloc( ETS_RMT_INTR_SOURCE, 0, interruptHandler, nullptr, &_interruptHandle));
  193. }
  194. static void unregisterInterrupt(void*) {
  195. esp_intr_free( _interruptHandle );
  196. }
  197. static SmartLed*& IRAM_ATTR ledForChannel( int channel );
  198. static void IRAM_ATTR interruptHandler( void* );
  199. void IRAM_ATTR copyRmtHalfBlock();
  200. void swapBuffers() {
  201. if ( _secondBuffer )
  202. _firstBuffer.swap( _secondBuffer );
  203. }
  204. void startTransmission() {
  205. // Invalid use of the library
  206. if( xSemaphoreTake( _finishedFlag, 0 ) != pdTRUE )
  207. abort();
  208. _pixelPosition = _componentPosition = _halfIdx = 0;
  209. copyRmtHalfBlock();
  210. if ( _pixelPosition < _count )
  211. copyRmtHalfBlock();
  212. RMT.conf_ch[ _channel ].conf1.mem_rd_rst = 1;
  213. RMT.conf_ch[ _channel ].conf1.tx_start = 1;
  214. }
  215. static bool anyAlive() {
  216. for ( int i = 0; i != 8; i++ )
  217. if ( ledForChannel( i ) != nullptr ) return true;
  218. return false;
  219. }
  220. const LedType& _timing;
  221. int _channel;
  222. detail::RmtPulsePair _bitToRmt[ 2 ];
  223. int _count;
  224. std::unique_ptr< Rgb[] > _firstBuffer;
  225. std::unique_ptr< Rgb[] > _secondBuffer;
  226. Rgb *_buffer;
  227. xSemaphoreHandle _finishedFlag;
  228. int _pixelPosition;
  229. int _componentPosition;
  230. int _halfIdx;
  231. };
  232. class Apa102 {
  233. public:
  234. struct ApaRgb {
  235. ApaRgb( uint8_t r = 0, uint8_t g = 0, uint32_t b = 0, uint32_t v = 0xFF )
  236. : v( 0xE0 | v ), b( b ), g( g ), r( r )
  237. {}
  238. ApaRgb& operator=( const Rgb& o ) {
  239. r = o.r;
  240. g = o.g;
  241. b = o.b;
  242. return *this;
  243. }
  244. ApaRgb& operator=( const Hsv& o ) {
  245. *this = Rgb{ o };
  246. return *this;
  247. }
  248. uint8_t v, b, g, r;
  249. };
  250. static const int FINAL_FRAME_SIZE = 4;
  251. static const int TRANS_COUNT = 2 + 8;
  252. Apa102( int count, int clkpin, int datapin, BufferType doubleBuffer = SingleBuffer )
  253. : _count( count ),
  254. _firstBuffer( new ApaRgb[ count ] ),
  255. _secondBuffer( doubleBuffer ? new ApaRgb[ count ] : nullptr ),
  256. _initFrame( 0 )
  257. {
  258. spi_bus_config_t buscfg;
  259. memset( &buscfg, 0, sizeof( buscfg ) );
  260. buscfg.mosi_io_num = datapin;
  261. buscfg.miso_io_num = -1;
  262. buscfg.sclk_io_num = clkpin;
  263. buscfg.quadwp_io_num = -1;
  264. buscfg.quadhd_io_num = -1;
  265. buscfg.max_transfer_sz = 65535;
  266. spi_device_interface_config_t devcfg;
  267. memset( &devcfg, 0, sizeof( devcfg ) );
  268. devcfg.clock_speed_hz = 1000000;
  269. devcfg.mode = 0;
  270. devcfg.spics_io_num = -1;
  271. devcfg.queue_size = TRANS_COUNT;
  272. devcfg.pre_cb = nullptr;
  273. auto ret = spi_bus_initialize( HSPI_HOST, &buscfg, 1 );
  274. assert( ret == ESP_OK );
  275. ret = spi_bus_add_device( HSPI_HOST, &devcfg, &_spi );
  276. assert( ret == ESP_OK );
  277. std::fill_n( _finalFrame, FINAL_FRAME_SIZE, 0xFFFFFFFF );
  278. }
  279. ~Apa102() {
  280. // ToDo
  281. }
  282. ApaRgb& operator[]( int idx ) {
  283. return _firstBuffer[ idx ];
  284. }
  285. const ApaRgb& operator[]( int idx ) const {
  286. return _firstBuffer[ idx ];
  287. }
  288. void show() {
  289. _buffer = _firstBuffer.get();
  290. startTransmission();
  291. swapBuffers();
  292. }
  293. void wait() {
  294. for ( int i = 0; i != _transCount; i++ ) {
  295. spi_transaction_t *t;
  296. spi_device_get_trans_result( _spi, &t, portMAX_DELAY );
  297. }
  298. }
  299. private:
  300. void swapBuffers() {
  301. if ( _secondBuffer )
  302. _firstBuffer.swap( _secondBuffer );
  303. }
  304. void startTransmission() {
  305. for ( int i = 0; i != TRANS_COUNT; i++ ) {
  306. _transactions[ i ].cmd = 0;
  307. _transactions[ i ].addr = 0;
  308. _transactions[ i ].flags = 0;
  309. _transactions[ i ].rxlength = 0;
  310. _transactions[ i ].rx_buffer = nullptr;
  311. }
  312. // Init frame
  313. _transactions[ 0 ].length = 32;
  314. _transactions[ 0 ].tx_buffer = &_initFrame;
  315. spi_device_queue_trans( _spi, _transactions + 0, portMAX_DELAY );
  316. // Data
  317. _transactions[ 1 ].length = 32 * _count;
  318. _transactions[ 1 ].tx_buffer = _buffer;
  319. spi_device_queue_trans( _spi, _transactions + 1, portMAX_DELAY );
  320. _transCount = 2;
  321. // End frame
  322. for ( int i = 0; i != 1 + _count / 32 / FINAL_FRAME_SIZE; i++ ) {
  323. _transactions[ 2 + i ].length = 32 * FINAL_FRAME_SIZE;
  324. _transactions[ 2 + i ].tx_buffer = _finalFrame;
  325. spi_device_queue_trans( _spi, _transactions + 2 + i, portMAX_DELAY );
  326. _transCount++;
  327. }
  328. }
  329. spi_device_handle_t _spi;
  330. int _count;
  331. std::unique_ptr< ApaRgb[] > _firstBuffer, _secondBuffer;
  332. ApaRgb *_buffer;
  333. spi_transaction_t _transactions[ TRANS_COUNT ];
  334. int _transCount;
  335. uint32_t _initFrame;
  336. uint32_t _finalFrame[ FINAL_FRAME_SIZE ];
  337. };
  338. class LDP8806 {
  339. public:
  340. struct LDP8806_GRB {
  341. LDP8806_GRB( uint8_t g_7bit = 0, uint8_t r_7bit = 0, uint32_t b_7bit = 0 )
  342. : g( g_7bit ), r( r_7bit ), b( b_7bit )
  343. {
  344. }
  345. LDP8806_GRB& operator=( const Rgb& o ) {
  346. //Convert 8->7bit colour
  347. r = ( o.r * 127 / 256 ) | 0x80;
  348. g = ( o.g * 127 / 256 ) | 0x80;
  349. b = ( o.b * 127 / 256 ) | 0x80;
  350. return *this;
  351. }
  352. LDP8806_GRB& operator=( const Hsv& o ) {
  353. *this = Rgb{ o };
  354. return *this;
  355. }
  356. uint8_t g, r, b;
  357. };
  358. static const int LED_FRAME_SIZE_BYTES = sizeof( LDP8806_GRB );
  359. static const int LATCH_FRAME_SIZE_BYTES = 3;
  360. static const int TRANS_COUNT_MAX = 20;//Arbitrary, supports up to 600 LED
  361. LDP8806( int count, int clkpin, int datapin, BufferType doubleBuffer = SingleBuffer, uint32_t clock_speed_hz = 2000000 )
  362. : _count( count ),
  363. _firstBuffer( new LDP8806_GRB[ count ] ),
  364. _secondBuffer( doubleBuffer ? new LDP8806_GRB[ count ] : nullptr ),
  365. // one 'latch'/start-of-data mark frame for every 32 leds
  366. _latchFrames( ( count + 31 ) / 32 )
  367. {
  368. spi_bus_config_t buscfg;
  369. memset( &buscfg, 0, sizeof( buscfg ) );
  370. buscfg.mosi_io_num = datapin;
  371. buscfg.miso_io_num = -1;
  372. buscfg.sclk_io_num = clkpin;
  373. buscfg.quadwp_io_num = -1;
  374. buscfg.quadhd_io_num = -1;
  375. buscfg.max_transfer_sz = 65535;
  376. spi_device_interface_config_t devcfg;
  377. memset( &devcfg, 0, sizeof( devcfg ) );
  378. devcfg.clock_speed_hz = clock_speed_hz;
  379. devcfg.mode = 0;
  380. devcfg.spics_io_num = -1;
  381. devcfg.queue_size = TRANS_COUNT_MAX;
  382. devcfg.pre_cb = nullptr;
  383. auto ret = spi_bus_initialize( HSPI_HOST, &buscfg, 1 );
  384. assert( ret == ESP_OK );
  385. ret = spi_bus_add_device( HSPI_HOST, &devcfg, &_spi );
  386. assert( ret == ESP_OK );
  387. std::fill_n( _latchBuffer, LATCH_FRAME_SIZE_BYTES, 0x0 );
  388. }
  389. ~LDP8806() {
  390. // noop
  391. }
  392. LDP8806_GRB& operator[]( int idx ) {
  393. return _firstBuffer[ idx ];
  394. }
  395. const LDP8806_GRB& operator[]( int idx ) const {
  396. return _firstBuffer[ idx ];
  397. }
  398. void show() {
  399. _buffer = _firstBuffer.get();
  400. startTransmission();
  401. swapBuffers();
  402. }
  403. void wait() {
  404. while ( _transCount-- ) {
  405. spi_transaction_t *t;
  406. spi_device_get_trans_result( _spi, &t, portMAX_DELAY );
  407. }
  408. }
  409. private:
  410. void swapBuffers() {
  411. if ( _secondBuffer )
  412. _firstBuffer.swap( _secondBuffer );
  413. }
  414. void startTransmission() {
  415. _transCount = 0;
  416. for ( int i = 0; i != TRANS_COUNT_MAX; i++ ) {
  417. _transactions[ i ].cmd = 0;
  418. _transactions[ i ].addr = 0;
  419. _transactions[ i ].flags = 0;
  420. _transactions[ i ].rxlength = 0;
  421. _transactions[ i ].rx_buffer = nullptr;
  422. }
  423. // LED Data
  424. _transactions[ 0 ].length = ( LED_FRAME_SIZE_BYTES * 8 ) * _count;
  425. _transactions[ 0 ].tx_buffer = _buffer;
  426. spi_device_queue_trans( _spi, _transactions + _transCount, portMAX_DELAY );
  427. _transCount++;
  428. // 'latch'/start-of-data marker frames
  429. for ( int i = 0; i < _latchFrames; i++ ) {
  430. _transactions[ _transCount ].length = ( LATCH_FRAME_SIZE_BYTES * 8 );
  431. _transactions[ _transCount ].tx_buffer = _latchBuffer;
  432. spi_device_queue_trans( _spi, _transactions + _transCount, portMAX_DELAY );
  433. _transCount++;
  434. }
  435. }
  436. spi_device_handle_t _spi;
  437. int _count;
  438. std::unique_ptr< LDP8806_GRB[] > _firstBuffer, _secondBuffer;
  439. LDP8806_GRB *_buffer;
  440. spi_transaction_t _transactions[ TRANS_COUNT_MAX ];
  441. int _transCount;
  442. int _latchFrames;
  443. uint8_t _latchBuffer[ LATCH_FRAME_SIZE_BYTES ];
  444. };