Skip to content

Commit

Permalink
Merge branch 'feature/rmt_encoder_memory_allocator' into 'master'
Browse files Browse the repository at this point in the history
feat(rmt): added a help function for allocating encoder memory

Closes IDFGH-11954

See merge request espressif/esp-idf!28667
  • Loading branch information
suda-morris committed Jan 30, 2024
2 parents 2ec1614 + 08845cf commit 5074cc6
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions components/esp_driver_rmt/include/driver/rmt_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ esp_err_t rmt_del_encoder(rmt_encoder_handle_t encoder);
*/
esp_err_t rmt_encoder_reset(rmt_encoder_handle_t encoder);

/**
* @brief A helper function to allocate a proper memory for RMT encoder
*
* @param size Size of memory to be allocated
* @return Pointer to the allocated memory if the allocation is successful, NULL otherwise
*/
void* rmt_alloc_encoder_mem(size_t size);

#ifdef __cplusplus
}
#endif
9 changes: 7 additions & 2 deletions components/esp_driver_rmt/src/rmt_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ esp_err_t rmt_new_bytes_encoder(const rmt_bytes_encoder_config_t *config, rmt_en
{
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
rmt_bytes_encoder_t *encoder = heap_caps_calloc(1, sizeof(rmt_bytes_encoder_t), RMT_MEM_ALLOC_CAPS);
rmt_bytes_encoder_t *encoder = rmt_alloc_encoder_mem(sizeof(rmt_bytes_encoder_t));
ESP_GOTO_ON_FALSE(encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for bytes encoder");
encoder->base.encode = rmt_encode_bytes;
encoder->base.del = rmt_del_bytes_encoder;
Expand Down Expand Up @@ -289,7 +289,7 @@ esp_err_t rmt_new_copy_encoder(const rmt_copy_encoder_config_t *config, rmt_enc
{
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
rmt_copy_encoder_t *encoder = heap_caps_calloc(1, sizeof(rmt_copy_encoder_t), RMT_MEM_ALLOC_CAPS);
rmt_copy_encoder_t *encoder = rmt_alloc_encoder_mem(sizeof(rmt_copy_encoder_t));
ESP_GOTO_ON_FALSE(encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for copy encoder");
encoder->base.encode = rmt_encode_copy;
encoder->base.del = rmt_del_copy_encoder;
Expand All @@ -312,3 +312,8 @@ esp_err_t rmt_encoder_reset(rmt_encoder_handle_t encoder)
ESP_RETURN_ON_FALSE(encoder, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return encoder->reset(encoder);
}

void* rmt_alloc_encoder_mem(size_t size)
{
return heap_caps_calloc(1, size, RMT_MEM_ALLOC_CAPS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static esp_err_t rmt_led_strip_encoder_reset(rmt_encoder_t *encoder)

esp_err_t test_rmt_new_led_strip_encoder(rmt_encoder_handle_t *ret_encoder)
{
rmt_led_strip_encoder_t *led_encoder = heap_caps_calloc(1, sizeof(rmt_led_strip_encoder_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
rmt_led_strip_encoder_t *led_encoder = rmt_alloc_encoder_mem(sizeof(rmt_led_strip_encoder_t));
led_encoder->base.encode = rmt_encode_led_strip;
led_encoder->base.del = rmt_del_led_strip_encoder;
led_encoder->base.reset = rmt_led_strip_encoder_reset;
Expand Down Expand Up @@ -188,7 +188,7 @@ static esp_err_t rmt_nec_protocol_encoder_reset(rmt_encoder_t *encoder)

esp_err_t test_rmt_new_nec_protocol_encoder(rmt_encoder_handle_t *ret_encoder)
{
rmt_nec_protocol_encoder_t *nec_encoder = heap_caps_calloc(1, sizeof(rmt_nec_protocol_encoder_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
rmt_nec_protocol_encoder_t *nec_encoder = rmt_alloc_encoder_mem(sizeof(rmt_nec_protocol_encoder_t));
nec_encoder->base.encode = rmt_encode_nec_protocol;
nec_encoder->base.del = rmt_del_nec_protocol_encoder;
nec_encoder->base.reset = rmt_nec_protocol_encoder_reset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ esp_err_t rmt_new_dshot_esc_encoder(const dshot_esc_encoder_config_t *config, rm
esp_err_t ret = ESP_OK;
rmt_dshot_esc_encoder_t *dshot_encoder = NULL;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
dshot_encoder = calloc(1, sizeof(rmt_dshot_esc_encoder_t));
dshot_encoder = rmt_alloc_encoder_mem(sizeof(rmt_dshot_esc_encoder_t));
ESP_GOTO_ON_FALSE(dshot_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for musical score encoder");
dshot_encoder->base.encode = rmt_encode_dshot_esc;
dshot_encoder->base.del = rmt_del_dshot_encoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ esp_err_t rmt_new_ir_nec_encoder(const ir_nec_encoder_config_t *config, rmt_enco
esp_err_t ret = ESP_OK;
rmt_ir_nec_encoder_t *nec_encoder = NULL;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
nec_encoder = calloc(1, sizeof(rmt_ir_nec_encoder_t));
nec_encoder = rmt_alloc_encoder_mem(sizeof(rmt_ir_nec_encoder_t));
ESP_GOTO_ON_FALSE(nec_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for ir nec encoder");
nec_encoder->base.encode = rmt_encode_ir_nec;
nec_encoder->base.del = rmt_del_ir_nec_encoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rm
esp_err_t ret = ESP_OK;
rmt_led_strip_encoder_t *led_encoder = NULL;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
led_encoder = calloc(1, sizeof(rmt_led_strip_encoder_t));
led_encoder = rmt_alloc_encoder_mem(sizeof(rmt_led_strip_encoder_t));
ESP_GOTO_ON_FALSE(led_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for led strip encoder");
led_encoder->base.encode = rmt_encode_led_strip;
led_encoder->base.del = rmt_del_led_strip_encoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ esp_err_t rmt_new_musical_score_encoder(const musical_score_encoder_config_t *co
esp_err_t ret = ESP_OK;
rmt_musical_score_encoder_t *score_encoder = NULL;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
score_encoder = calloc(1, sizeof(rmt_musical_score_encoder_t));
score_encoder = rmt_alloc_encoder_mem(sizeof(rmt_musical_score_encoder_t));
ESP_GOTO_ON_FALSE(score_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for musical score encoder");
score_encoder->base.encode = rmt_encode_musical_score;
score_encoder->base.del = rmt_del_musical_score_encoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ esp_err_t rmt_new_stepper_motor_curve_encoder(const stepper_motor_curve_encoder_
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid arguments");
ESP_GOTO_ON_FALSE(config->sample_points, ESP_ERR_INVALID_ARG, err, TAG, "sample points number can't be zero");
ESP_GOTO_ON_FALSE(config->start_freq_hz != config->end_freq_hz, ESP_ERR_INVALID_ARG, err, TAG, "start freq can't equal to end freq");
step_encoder = calloc(1, sizeof(rmt_stepper_curve_encoder_t) + config->sample_points * sizeof(rmt_symbol_word_t));
step_encoder = rmt_alloc_encoder_mem(sizeof(rmt_stepper_curve_encoder_t) + config->sample_points * sizeof(rmt_symbol_word_t));
ESP_GOTO_ON_FALSE(step_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for stepper curve encoder");
rmt_copy_encoder_config_t copy_encoder_config = {};
ESP_GOTO_ON_ERROR(rmt_new_copy_encoder(&copy_encoder_config, &step_encoder->copy_encoder), err, TAG, "create copy encoder failed");
Expand Down Expand Up @@ -161,7 +161,7 @@ esp_err_t rmt_new_stepper_motor_uniform_encoder(const stepper_motor_uniform_enco
esp_err_t ret = ESP_OK;
rmt_stepper_uniform_encoder_t *step_encoder = NULL;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid arguments");
step_encoder = calloc(1, sizeof(rmt_stepper_uniform_encoder_t));
step_encoder = rmt_alloc_encoder_mem(sizeof(rmt_stepper_uniform_encoder_t));
ESP_GOTO_ON_FALSE(step_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for stepper uniform encoder");
rmt_copy_encoder_config_t copy_encoder_config = {};
ESP_GOTO_ON_ERROR(rmt_new_copy_encoder(&copy_encoder_config, &step_encoder->copy_encoder), err, TAG, "create copy encoder failed");
Expand Down

0 comments on commit 5074cc6

Please sign in to comment.