ESP IDF rmt_new_copy_encoder

WEFJWEF
Posts: 1
Joined: Thu Apr 18, 2024 12:56 pm

ESP IDF rmt_new_copy_encoder

Postby WEFJWEF » Thu Apr 18, 2024 1:11 pm

Hi guys,
my friend and i are on a project right now and are quite stuck on coding some LED.
We are total beginner and help is much appreciated.

This is the following excercise:

Finish the implementation of the ws2812b_init function. In this function, return all errors to the caller instead of aborting. At the end of the function, we call ws2812b_clear() and ws2812b_show(). Add the following before that:

Save important config values for later use

Allocate heap memory for the LED pixel buffer (stores the color data for each LED as 3 bytes in GBR order), as well as the RMT symbol buffer (the RMT peripheral accesses this buffer). Use standard malloc and check for allocation errors! Hint: There are some useful macros at the top that may help you...

Create an RMT TX channel (rmt_new_tx_channel) using the provided RMT config

Create a "copy encoder" (rmt_new_copy_encoder). The encoder copies our RMT symbols as-is to the RMT peripheral

Enable the created RMT TX channel (rmt_enable). This sets up some stuff internally




We are currently on the 4th point of the excercises. Create a "copy encoder" (rmt_new_copy_encoder). The encoder copies our RMT symbols as-is to the RMT peripheral

The method rmt_new_copy_encoder takes a rmt_copy_encoder_config_t and the rmt_encoder_handle_t.
The thing is that no where is written if we should create such configuration and we also have put the handle on Null.

We dont know any further. D:



This is the code we have worked on up until now.

  1.  
  2.  
  3. #include "include/ws2812b.h"
  4. #include <driver/rmt_tx.h>
  5. #include <esp_check.h>
  6. #include <esp_err.h>
  7. #include <esp_log.h>
  8. #include <string.h>
  9.  
  10. static const char *TAG = "WS2812B";
  11.  
  12. /**** CONSTANTS ****/
  13. #define PIXEL_BYTES (pixel_count * 3)
  14. #define SYMBOL_BYTES (PIXEL_BYTES * 8 * sizeof(rmt_symbol_word_t)) // 8 bits per byte -> 8 symbols per byte
  15.  
  16. /**** TIMINGS ****/
  17. // See https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf
  18. // Timings are given in 0.1us (we use 10Mhz resolution, round if necessary)
  19. // TODO 2.2: Find out the correct timings and replace the '0's
  20. #define T0H_TIME (4) // 0 bit high time
  21. #define T0L_TIME (9) // 0 bit low time
  22. #define T1H_TIME (8) // 1 bit high time
  23. #define T1L_TIME (5) // 1 bit low time
  24. #define RST_TIME (500) // reset time
  25.  
  26. /**** GLOBAL STATE ****/
  27. static size_t pixel_count;
  28. static uint8_t *pixel_buffer;
  29. static rmt_symbol_word_t *symbol_buffer;
  30. // TODO 2.3: What else do you need here?
  31. static gpio_num_t data_gpio_num;
  32.  
  33. static rmt_channel_handle_t rmt_channel = NULL;
  34. static rmt_encoder_handle_t copy_encoder = NULL;
  35.  
  36.  
  37. esp_err_t ws2812b_init(const ws2812b_config_t *config) {
  38.     esp_err_t ret = ESP_OK;
  39.  
  40.     ESP_LOGI(TAG, "Initializing WS2812B driver...");
  41.     //ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "WS2812B driver not implemented yet. REMOVE ME!");
  42.  
  43.     // TODO 2.3: Save necessary config values in global state if you need them later and initialize everything
  44.     pixel_count = config->pixel_count;
  45.     data_gpio_num = config->data_gpio_num;
  46.    
  47.     //allocation of pixel buffer mem
  48.     pixel_buffer = (uint8_t *)malloc(PIXEL_BYTES); //GBR
  49.     if (pixel_buffer == NULL) {
  50.         ESP_LOGE(TAG, "Failed to allocate memory");
  51.         return ESP_ERR_NO_MEM; //no memory
  52.     }
  53.  
  54.     //allocation of RMT Symbol buffer mem
  55.     symbol_buffer = (rmt_symbol_word_t *)malloc(SYMBOL_BYTES);
  56.     if (symbol_buffer == NULL) {
  57.         free(pixel_buffer);
  58.         ESP_LOGE(TAG, "Failed to allocate memory");
  59.         return ESP_ERR_NO_MEM; //no mem
  60.     }
  61.  
  62.  
  63. // RMT-related use this RMT channel config
  64.     rmt_tx_channel_config_t rmt_config = {
  65.         .gpio_num = config->data_gpio_num,
  66.         .clk_src = RMT_CLK_SRC_DEFAULT,
  67.         .resolution_hz = 10000000, // 10MHz resolution, 1 tick = 0.1us (timings need to be rounded but this is ok)
  68.         .mem_block_symbols = 64,   // increasing this in steps of 64 can reduce flickering
  69.         .trans_queue_depth = 4,    // maximum pending background transfers, 4 is a good default value
  70.         // we leave everything else at default values
  71.     };
  72.  
  73.  
  74.     //create RMT channel with channel config
  75.  
  76.     esp_err_t err = rmt_new_tx_channel(&rmt_config,& rmt_channel);
  77.     if (err!= ESP_OK) {
  78.         ESP_LOGE(TAG, "Failed to create new rmt tx channel D:");
  79.         free(pixel_buffer);
  80.         free(symbol_buffer);
  81.         return err;
  82.     }
  83.  
  84.  
  85.     //create copy encoder
  86.     err = rmt_new_copy_encoder(true, &copy_encoder);
  87.     if(err!= ESP_OK){
  88.        
  89.     }
  90.  
  91.     //enable TX
  92.  
  93.  
  94.     // turn LEDs off in case they were on previously
  95.     ws2812b_clear();
  96.     ws2812b_show();
  97.  
  98.     ESP_LOGI(TAG, "WS2812B driver initialized");
  99.  
  100.     return ESP_OK;
  101.  
  102. err:
  103.     // TODO 2.3: Clean up everything that you've already initialized.
  104.     // Hint: Do you already have this functionality somewhere?
  105.     return ret;
  106. }
  107.  
Thank you for any help. And if we have done any other errors please do tell.

ESP_Sprite
Posts: 9729
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP IDF rmt_new_copy_encoder

Postby ESP_Sprite » Fri Apr 19, 2024 1:15 am

Have you looked at the example that comes with esp-idf? Probably easier to just copy/paste that.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 75 guests