diff --git a/components/driver/include/driver/rmt_encoder.h b/components/driver/include/driver/rmt_encoder.h index 1847d3f0bd..9981af6203 100644 --- a/components/driver/include/driver/rmt_encoder.h +++ b/components/driver/include/driver/rmt_encoder.h @@ -149,6 +149,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 diff --git a/components/driver/rmt/rmt_encoder.c b/components/driver/rmt/rmt_encoder.c index c555a1debf..d55e64c89e 100644 --- a/components/driver/rmt/rmt_encoder.c +++ b/components/driver/rmt/rmt_encoder.c @@ -258,7 +258,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; @@ -287,7 +287,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; @@ -310,3 +310,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); +} diff --git a/components/driver/test_apps/rmt/main/test_util_rmt_encoders.c b/components/driver/test_apps/rmt/main/test_util_rmt_encoders.c index 1e6550648d..b0d47b3078 100644 --- a/components/driver/test_apps/rmt/main/test_util_rmt_encoders.c +++ b/components/driver/test_apps/rmt/main/test_util_rmt_encoders.c @@ -80,7 +80,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; @@ -195,7 +195,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; diff --git a/examples/peripherals/rmt/dshot_esc/main/dshot_esc_encoder.c b/examples/peripherals/rmt/dshot_esc/main/dshot_esc_encoder.c index 1f75d28203..0ba5c7090b 100644 --- a/examples/peripherals/rmt/dshot_esc/main/dshot_esc_encoder.c +++ b/examples/peripherals/rmt/dshot_esc/main/dshot_esc_encoder.c @@ -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; diff --git a/examples/peripherals/rmt/ir_nec_transceiver/main/ir_nec_encoder.c b/examples/peripherals/rmt/ir_nec_transceiver/main/ir_nec_encoder.c index 66f309f9d0..74c403568a 100644 --- a/examples/peripherals/rmt/ir_nec_transceiver/main/ir_nec_encoder.c +++ b/examples/peripherals/rmt/ir_nec_transceiver/main/ir_nec_encoder.c @@ -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; diff --git a/examples/peripherals/rmt/led_strip/main/led_strip_encoder.c b/examples/peripherals/rmt/led_strip/main/led_strip_encoder.c index 8ec7e1c541..de8638cfab 100644 --- a/examples/peripherals/rmt/led_strip/main/led_strip_encoder.c +++ b/examples/peripherals/rmt/led_strip/main/led_strip_encoder.c @@ -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; diff --git a/examples/peripherals/rmt/musical_buzzer/main/musical_score_encoder.c b/examples/peripherals/rmt/musical_buzzer/main/musical_score_encoder.c index d014ee14a3..26035bec02 100644 --- a/examples/peripherals/rmt/musical_buzzer/main/musical_score_encoder.c +++ b/examples/peripherals/rmt/musical_buzzer/main/musical_score_encoder.c @@ -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; diff --git a/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c b/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c index 6eb5d93534..c9a6d78f8b 100644 --- a/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c +++ b/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c @@ -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(©_encoder_config, &step_encoder->copy_encoder), err, TAG, "create copy encoder failed"); @@ -159,7 +159,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(©_encoder_config, &step_encoder->copy_encoder), err, TAG, "create copy encoder failed");