rmt_encoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/cdefs.h>
  9. #include <sys/param.h>
  10. #include "sdkconfig.h"
  11. #if CONFIG_RMT_ENABLE_DEBUG_LOG
  12. // The local log level must be defined before including esp_log.h
  13. // Set the maximum log level for this source file
  14. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  15. #endif
  16. #include "esp_log.h"
  17. #include "esp_check.h"
  18. #include "driver/rmt_encoder.h"
  19. #include "rmt_private.h"
  20. static const char *TAG = "rmt";
  21. typedef struct rmt_bytes_encoder_t {
  22. rmt_encoder_t base; // encoder base class
  23. size_t last_bit_index; // index of the encoding bit position in the encoding byte
  24. size_t last_byte_index; // index of the encoding byte in the primary stream
  25. rmt_symbol_word_t bit0; // bit zero representing
  26. rmt_symbol_word_t bit1; // bit one representing
  27. struct {
  28. uint32_t msb_first: 1; // encode MSB firstly
  29. } flags;
  30. } rmt_bytes_encoder_t;
  31. typedef struct rmt_copy_encoder_t {
  32. rmt_encoder_t base; // encoder base class
  33. size_t last_symbol_index; // index of symbol position in the primary stream
  34. } rmt_copy_encoder_t;
  35. static esp_err_t rmt_bytes_encoder_reset(rmt_encoder_t *encoder)
  36. {
  37. rmt_bytes_encoder_t *bytes_encoder = __containerof(encoder, rmt_bytes_encoder_t, base);
  38. // reset index to zero
  39. bytes_encoder->last_bit_index = 0;
  40. bytes_encoder->last_byte_index = 0;
  41. return ESP_OK;
  42. }
  43. static inline uint8_t _bitwise_reverse(uint8_t n)
  44. {
  45. n = ((n & 0xf0) >> 4) | ((n & 0x0f) << 4);
  46. n = ((n & 0xcc) >> 2) | ((n & 0x33) << 2);
  47. n = ((n & 0xaa) >> 1) | ((n & 0x55) << 1);
  48. return n;
  49. }
  50. static size_t IRAM_ATTR rmt_encode_bytes(rmt_encoder_t *encoder, rmt_channel_handle_t channel,
  51. const void *primary_data, size_t data_size, rmt_encode_state_t *ret_state)
  52. {
  53. rmt_bytes_encoder_t *bytes_encoder = __containerof(encoder, rmt_bytes_encoder_t, base);
  54. rmt_tx_channel_t *tx_chan = __containerof(channel, rmt_tx_channel_t, base);
  55. const uint8_t *nd = (const uint8_t *)primary_data;
  56. rmt_encode_state_t state = 0;
  57. dma_descriptor_t *desc0 = NULL;
  58. dma_descriptor_t *desc1 = NULL;
  59. size_t byte_index = bytes_encoder->last_byte_index;
  60. size_t bit_index = bytes_encoder->last_bit_index;
  61. // how many symbols will be generated by the encoder
  62. size_t mem_want = (data_size - byte_index - 1) * 8 + (8 - bit_index);
  63. // how many symbols we can save for this round
  64. size_t mem_have = tx_chan->mem_end - tx_chan->mem_off;
  65. // where to put the encoded symbols? DMA buffer or RMT HW memory
  66. rmt_symbol_word_t *mem_to = channel->dma_chan ? channel->dma_mem_base : channel->hw_mem_base;
  67. // how many symbols will be encoded in this round
  68. size_t encode_len = MIN(mem_want, mem_have);
  69. bool encoding_truncated = mem_have < mem_want;
  70. bool encoding_space_free = mem_have > mem_want;
  71. if (channel->dma_chan) {
  72. // mark the start descriptor
  73. if (tx_chan->mem_off < tx_chan->ping_pong_symbols) {
  74. desc0 = &tx_chan->dma_nodes[0];
  75. } else {
  76. desc0 = &tx_chan->dma_nodes[1];
  77. }
  78. }
  79. size_t len = encode_len;
  80. while (len > 0) {
  81. // start from last time truncated encoding
  82. uint8_t cur_byte = nd[byte_index];
  83. // bit-wise reverse
  84. if (bytes_encoder->flags.msb_first) {
  85. cur_byte = _bitwise_reverse(cur_byte);
  86. }
  87. while ((len > 0) && (bit_index < 8)) {
  88. if (cur_byte & (1 << bit_index)) {
  89. mem_to[tx_chan->mem_off++] = bytes_encoder->bit1;
  90. } else {
  91. mem_to[tx_chan->mem_off++] = bytes_encoder->bit0;
  92. }
  93. len--;
  94. bit_index++;
  95. }
  96. if (bit_index >= 8) {
  97. byte_index++;
  98. bit_index = 0;
  99. }
  100. }
  101. if (channel->dma_chan) {
  102. // mark the end descriptor
  103. if (tx_chan->mem_off < tx_chan->ping_pong_symbols) {
  104. desc1 = &tx_chan->dma_nodes[0];
  105. } else {
  106. desc1 = &tx_chan->dma_nodes[1];
  107. }
  108. // cross line, means desc0 has prepared with sufficient data buffer
  109. if (desc0 != desc1) {
  110. desc0->dw0.length = tx_chan->ping_pong_symbols * sizeof(rmt_symbol_word_t);
  111. desc0->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  112. }
  113. }
  114. if (encoding_truncated) {
  115. // this encoding has not finished yet, save the truncated position
  116. bytes_encoder->last_bit_index = bit_index;
  117. bytes_encoder->last_byte_index = byte_index;
  118. } else {
  119. // reset internal index if encoding session has finished
  120. bytes_encoder->last_bit_index = 0;
  121. bytes_encoder->last_byte_index = 0;
  122. state |= RMT_ENCODING_COMPLETE;
  123. }
  124. if (!encoding_space_free) {
  125. // no more free memory, the caller should yield
  126. state |= RMT_ENCODING_MEM_FULL;
  127. }
  128. // reset offset pointer when exceeds maximum range
  129. if (tx_chan->mem_off >= tx_chan->ping_pong_symbols * 2) {
  130. if (channel->dma_chan) {
  131. desc1->dw0.length = tx_chan->ping_pong_symbols * sizeof(rmt_symbol_word_t);
  132. desc1->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  133. }
  134. tx_chan->mem_off = 0;
  135. }
  136. *ret_state = state;
  137. return encode_len;
  138. }
  139. static esp_err_t rmt_copy_encoder_reset(rmt_encoder_t *encoder)
  140. {
  141. rmt_copy_encoder_t *copy_encoder = __containerof(encoder, rmt_copy_encoder_t, base);
  142. copy_encoder->last_symbol_index = 0;
  143. return ESP_OK;
  144. }
  145. static size_t IRAM_ATTR rmt_encode_copy(rmt_encoder_t *encoder, rmt_channel_handle_t channel,
  146. const void *primary_data, size_t data_size, rmt_encode_state_t *ret_state)
  147. {
  148. rmt_copy_encoder_t *copy_encoder = __containerof(encoder, rmt_copy_encoder_t, base);
  149. rmt_tx_channel_t *tx_chan = __containerof(channel, rmt_tx_channel_t, base);
  150. rmt_symbol_word_t *symbols = (rmt_symbol_word_t *)primary_data;
  151. rmt_encode_state_t state = 0;
  152. dma_descriptor_t *desc0 = NULL;
  153. dma_descriptor_t *desc1 = NULL;
  154. size_t symbol_index = copy_encoder->last_symbol_index;
  155. // how many symbols will be copied by the encoder
  156. size_t mem_want = (data_size / 4 - symbol_index);
  157. // how many symbols we can save for this round
  158. size_t mem_have = tx_chan->mem_end - tx_chan->mem_off;
  159. // where to put the encoded symbols? DMA buffer or RMT HW memory
  160. rmt_symbol_word_t *mem_to = channel->dma_chan ? channel->dma_mem_base : channel->hw_mem_base;
  161. // how many symbols will be encoded in this round
  162. size_t encode_len = MIN(mem_want, mem_have);
  163. bool encoding_truncated = mem_have < mem_want;
  164. bool encoding_space_free = mem_have > mem_want;
  165. if (channel->dma_chan) {
  166. // mark the start descriptor
  167. if (tx_chan->mem_off < tx_chan->ping_pong_symbols) {
  168. desc0 = &tx_chan->dma_nodes[0];
  169. } else {
  170. desc0 = &tx_chan->dma_nodes[1];
  171. }
  172. }
  173. size_t len = encode_len;
  174. while (len > 0) {
  175. mem_to[tx_chan->mem_off++] = symbols[symbol_index++];
  176. len--;
  177. }
  178. if (channel->dma_chan) {
  179. // mark the end descriptor
  180. if (tx_chan->mem_off < tx_chan->ping_pong_symbols) {
  181. desc1 = &tx_chan->dma_nodes[0];
  182. } else {
  183. desc1 = &tx_chan->dma_nodes[1];
  184. }
  185. // cross line, means desc0 has prepared with sufficient data buffer
  186. if (desc0 != desc1) {
  187. desc0->dw0.length = tx_chan->ping_pong_symbols * sizeof(rmt_symbol_word_t);
  188. desc0->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  189. }
  190. }
  191. if (encoding_truncated) {
  192. // this encoding has not finished yet, save the truncated position
  193. copy_encoder->last_symbol_index = symbol_index;
  194. } else {
  195. // reset internal index if encoding session has finished
  196. copy_encoder->last_symbol_index = 0;
  197. state |= RMT_ENCODING_COMPLETE;
  198. }
  199. if (!encoding_space_free) {
  200. // no more free memory, the caller should yield
  201. state |= RMT_ENCODING_MEM_FULL;
  202. }
  203. // reset offset pointer when exceeds maximum range
  204. if (tx_chan->mem_off >= tx_chan->ping_pong_symbols * 2) {
  205. if (channel->dma_chan) {
  206. desc1->dw0.length = tx_chan->ping_pong_symbols * sizeof(rmt_symbol_word_t);
  207. desc1->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  208. }
  209. tx_chan->mem_off = 0;
  210. }
  211. *ret_state = state;
  212. return encode_len;
  213. }
  214. static esp_err_t rmt_del_bytes_encoder(rmt_encoder_t *encoder)
  215. {
  216. rmt_bytes_encoder_t *bytes_encoder = __containerof(encoder, rmt_bytes_encoder_t, base);
  217. free(bytes_encoder);
  218. return ESP_OK;
  219. }
  220. static esp_err_t rmt_del_copy_encoder(rmt_encoder_t *encoder)
  221. {
  222. rmt_copy_encoder_t *copy_encoder = __containerof(encoder, rmt_copy_encoder_t, base);
  223. free(copy_encoder);
  224. return ESP_OK;
  225. }
  226. esp_err_t rmt_new_bytes_encoder(const rmt_bytes_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder)
  227. {
  228. esp_err_t ret = ESP_OK;
  229. ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
  230. rmt_bytes_encoder_t *encoder = heap_caps_calloc(1, sizeof(rmt_bytes_encoder_t), RMT_MEM_ALLOC_CAPS);
  231. ESP_GOTO_ON_FALSE(encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for bytes encoder");
  232. encoder->base.encode = rmt_encode_bytes;
  233. encoder->base.del = rmt_del_bytes_encoder;
  234. encoder->base.reset = rmt_bytes_encoder_reset;
  235. encoder->bit0 = config->bit0;
  236. encoder->bit1 = config->bit1;
  237. encoder->flags.msb_first = config->flags.msb_first;
  238. // return general encoder handle
  239. *ret_encoder = &encoder->base;
  240. ESP_LOGD(TAG, "new bytes encoder @%p", encoder);
  241. err:
  242. return ret;
  243. }
  244. esp_err_t rmt_new_copy_encoder(const rmt_copy_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder)
  245. {
  246. esp_err_t ret = ESP_OK;
  247. ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
  248. rmt_copy_encoder_t *encoder = heap_caps_calloc(1, sizeof(rmt_copy_encoder_t), RMT_MEM_ALLOC_CAPS);
  249. ESP_GOTO_ON_FALSE(encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for copy encoder");
  250. encoder->base.encode = rmt_encode_copy;
  251. encoder->base.del = rmt_del_copy_encoder;
  252. encoder->base.reset = rmt_copy_encoder_reset;
  253. // return general encoder handle
  254. *ret_encoder = &encoder->base;
  255. ESP_LOGD(TAG, "new copy encoder @%p", encoder);
  256. err:
  257. return ret;
  258. }
  259. esp_err_t rmt_del_encoder(rmt_encoder_handle_t encoder)
  260. {
  261. ESP_RETURN_ON_FALSE(encoder, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  262. return encoder->del(encoder);
  263. }
  264. esp_err_t rmt_encoder_reset(rmt_encoder_handle_t encoder)
  265. {
  266. ESP_RETURN_ON_FALSE(encoder, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  267. return encoder->reset(encoder);
  268. }