esp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Implements the RMT peripheral on Espressif SoCs
  2. // Copyright (c) 2020 Lucian Copeland for Adafruit Industries
  3. /* Uses code from Espressif RGB LED Strip demo and drivers
  4. * Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #if defined(ESP32)
  19. #include <Arduino.h>
  20. #include "driver/rmt.h"
  21. #if defined(ESP_IDF_VERSION)
  22. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
  23. #define HAS_ESP_IDF_4
  24. #endif
  25. #endif
  26. // This code is adapted from the ESP-IDF v3.4 RMT "led_strip" example, altered
  27. // to work with the Arduino version of the ESP-IDF (3.2)
  28. #define WS2812_T0H_NS (400)
  29. #define WS2812_T0L_NS (850)
  30. #define WS2812_T1H_NS (800)
  31. #define WS2812_T1L_NS (450)
  32. #define WS2811_T0H_NS (500)
  33. #define WS2811_T0L_NS (2000)
  34. #define WS2811_T1H_NS (1200)
  35. #define WS2811_T1L_NS (1300)
  36. static uint32_t t0h_ticks = 0;
  37. static uint32_t t1h_ticks = 0;
  38. static uint32_t t0l_ticks = 0;
  39. static uint32_t t1l_ticks = 0;
  40. // Limit the number of RMT channels available for the Neopixels. Defaults to all
  41. // channels (8 on ESP32, 4 on ESP32-S2 and S3). Redefining this value will free
  42. // any channels with a higher number for other uses, such as IR send-and-recieve
  43. // libraries. Redefine as 1 to restrict Neopixels to only a single channel.
  44. #define ADAFRUIT_RMT_CHANNEL_MAX RMT_CHANNEL_MAX
  45. #define RMT_LL_HW_BASE (&RMT)
  46. bool rmt_reserved_channels[ADAFRUIT_RMT_CHANNEL_MAX];
  47. static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size,
  48. size_t wanted_num, size_t *translated_size, size_t *item_num)
  49. {
  50. if (src == NULL || dest == NULL) {
  51. *translated_size = 0;
  52. *item_num = 0;
  53. return;
  54. }
  55. const rmt_item32_t bit0 = {{{ t0h_ticks, 1, t0l_ticks, 0 }}}; //Logical 0
  56. const rmt_item32_t bit1 = {{{ t1h_ticks, 1, t1l_ticks, 0 }}}; //Logical 1
  57. size_t size = 0;
  58. size_t num = 0;
  59. uint8_t *psrc = (uint8_t *)src;
  60. rmt_item32_t *pdest = dest;
  61. while (size < src_size && num < wanted_num) {
  62. for (int i = 0; i < 8; i++) {
  63. // MSB first
  64. if (*psrc & (1 << (7 - i))) {
  65. pdest->val = bit1.val;
  66. } else {
  67. pdest->val = bit0.val;
  68. }
  69. num++;
  70. pdest++;
  71. }
  72. size++;
  73. psrc++;
  74. }
  75. *translated_size = size;
  76. *item_num = num;
  77. }
  78. void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) {
  79. // Reserve channel
  80. rmt_channel_t channel = ADAFRUIT_RMT_CHANNEL_MAX;
  81. for (size_t i = 0; i < ADAFRUIT_RMT_CHANNEL_MAX; i++) {
  82. if (!rmt_reserved_channels[i]) {
  83. rmt_reserved_channels[i] = true;
  84. channel = i;
  85. break;
  86. }
  87. }
  88. if (channel == ADAFRUIT_RMT_CHANNEL_MAX) {
  89. // Ran out of channels!
  90. return;
  91. }
  92. #if defined(HAS_ESP_IDF_4)
  93. rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin, channel);
  94. config.clk_div = 2;
  95. #else
  96. // Match default TX config from ESP-IDF version 3.4
  97. rmt_config_t config = {
  98. .rmt_mode = RMT_MODE_TX,
  99. .channel = channel,
  100. .gpio_num = pin,
  101. .clk_div = 2,
  102. .mem_block_num = 1,
  103. .tx_config = {
  104. .carrier_freq_hz = 38000,
  105. .carrier_level = RMT_CARRIER_LEVEL_HIGH,
  106. .idle_level = RMT_IDLE_LEVEL_LOW,
  107. .carrier_duty_percent = 33,
  108. .carrier_en = false,
  109. .loop_en = false,
  110. .idle_output_en = true,
  111. }
  112. };
  113. #endif
  114. rmt_config(&config);
  115. rmt_driver_install(config.channel, 0, 0);
  116. // Convert NS timings to ticks
  117. uint32_t counter_clk_hz = 0;
  118. #if defined(HAS_ESP_IDF_4)
  119. rmt_get_counter_clock(channel, &counter_clk_hz);
  120. #else
  121. // this emulates the rmt_get_counter_clock() function from ESP-IDF 3.4
  122. if (RMT_LL_HW_BASE->conf_ch[config.channel].conf1.ref_always_on == RMT_BASECLK_REF) {
  123. uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
  124. uint32_t div = div_cnt == 0 ? 256 : div_cnt;
  125. counter_clk_hz = REF_CLK_FREQ / (div);
  126. } else {
  127. uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
  128. uint32_t div = div_cnt == 0 ? 256 : div_cnt;
  129. counter_clk_hz = APB_CLK_FREQ / (div);
  130. }
  131. #endif
  132. // NS to tick converter
  133. float ratio = (float)counter_clk_hz / 1e9;
  134. if (is800KHz) {
  135. t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
  136. t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
  137. t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
  138. t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
  139. } else {
  140. t0h_ticks = (uint32_t)(ratio * WS2811_T0H_NS);
  141. t0l_ticks = (uint32_t)(ratio * WS2811_T0L_NS);
  142. t1h_ticks = (uint32_t)(ratio * WS2811_T1H_NS);
  143. t1l_ticks = (uint32_t)(ratio * WS2811_T1L_NS);
  144. }
  145. // Initialize automatic timing translator
  146. rmt_translator_init(config.channel, ws2812_rmt_adapter);
  147. // Write and wait to finish
  148. rmt_write_sample(config.channel, pixels, (size_t)numBytes, true);
  149. rmt_wait_tx_done(config.channel, pdMS_TO_TICKS(100));
  150. // Free channel again
  151. rmt_driver_uninstall(config.channel);
  152. rmt_reserved_channels[channel] = false;
  153. gpio_set_direction(pin, GPIO_MODE_OUTPUT);
  154. }
  155. #endif