Skip to content

Commit 92fc3e9

Browse files
committed
Merge branch 'refa/replce_esp_dma_xxx_malloc_api' into 'master'
refa(spi): clean up esp_dma_xxx memory allocation code Closes IDF-9656 See merge request espressif/esp-idf!31239
2 parents f90b847 + d15e53d commit 92fc3e9

File tree

6 files changed

+23
-32
lines changed

6 files changed

+23
-32
lines changed

components/esp_driver_spi/include/driver/spi_common.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,13 @@ esp_err_t spi_bus_free(spi_host_device_t host_id);
174174
* @note This API will take care of the cache and hardware alignment internally.
175175
* To free/release memory allocated by this helper function, simply calling `free()`
176176
*
177-
* @param[in] size Size in bytes, the amount of memory to allocate
178-
* @param[out] out_ptr Pointer to the memory if allocated successfully
179-
* @param[in] extra_heap_caps Extra heap caps based on MALLOC_CAP_DMA
180-
* @param[out] actual_size Optional, Actual size for allocation in bytes, when the size you specified doesn't meet the internal alignment requirements,
181-
* This value might be bigger than the size you specified. Set NULL if don't care this value.
177+
* @param[in] host_id SPI peripheral who will using the memory
178+
* @param[in] size Size in bytes, the amount of memory to allocate
179+
* @param[in] extra_heap_caps Extra heap caps based on MALLOC_CAP_DMA
182180
*
183-
* @return
184-
* - ESP_ERR_INVALID_ARG Invalid argument
185-
* - ESP_ERR_NO_MEM No enough memory for allocation
186-
* - ESP_OK on success
181+
* @return Pointer to the memory if allocated successfully
187182
*/
188-
esp_err_t spi_bus_dma_memory_malloc(size_t size, void **out_ptr, uint32_t extra_heap_caps, size_t *actual_size);
183+
void *spi_bus_dma_memory_alloc(spi_host_device_t host_id, size_t size, uint32_t extra_heap_caps);
189184

190185
#ifdef __cplusplus
191186
}

components/esp_driver_spi/include/esp_private/spi_common_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct {
5050
uint32_t flags; ///< Flags (attributes) of the bus
5151
int max_transfer_sz; ///< Maximum length of bytes available to send
5252
bool dma_enabled; ///< To enable DMA or not
53-
uint16_t internal_mem_align_size; ///< Buffer align byte requirement for internal memory
53+
size_t internal_mem_align_size; ///< Buffer align byte requirement for internal memory
5454
spi_bus_lock_handle_t lock;
5555
#ifdef CONFIG_PM_ENABLE
5656
esp_pm_lock_handle_t pm_lock; ///< Power management lock

components/esp_driver_spi/src/gpspi/spi_common.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,8 @@ esp_err_t spicommon_dma_desc_alloc(spi_dma_ctx_t *dma_ctx, int cfg_max_sz, int *
287287
dma_desc_ct = 1; //default to 4k when max is not given
288288
}
289289

290-
esp_dma_mem_info_t dma_mem_info = {
291-
.dma_alignment_bytes = DMA_DESC_MEM_ALIGN_SIZE,
292-
};
293-
esp_dma_capable_malloc(sizeof(spi_dma_desc_t) * dma_desc_ct, &dma_mem_info, (void*)&dma_ctx->dmadesc_tx, NULL);
294-
esp_dma_capable_malloc(sizeof(spi_dma_desc_t) * dma_desc_ct, &dma_mem_info, (void*)&dma_ctx->dmadesc_rx, NULL);
295-
290+
dma_ctx->dmadesc_tx = heap_caps_aligned_calloc(DMA_DESC_MEM_ALIGN_SIZE, 1, sizeof(spi_dma_desc_t) * dma_desc_ct, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
291+
dma_ctx->dmadesc_rx = heap_caps_aligned_calloc(DMA_DESC_MEM_ALIGN_SIZE, 1, sizeof(spi_dma_desc_t) * dma_desc_ct, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
296292
if (dma_ctx->dmadesc_tx == NULL || dma_ctx->dmadesc_rx == NULL) {
297293
if (dma_ctx->dmadesc_tx) {
298294
free(dma_ctx->dmadesc_tx);
@@ -886,13 +882,13 @@ esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t *
886882
return err;
887883
}
888884

889-
esp_err_t spi_bus_dma_memory_malloc(size_t size, void **out_ptr, uint32_t extra_heap_caps, size_t *actual_size)
885+
void *spi_bus_dma_memory_alloc(spi_host_device_t host_id, size_t size, uint32_t extra_heap_caps)
890886
{
891-
esp_dma_mem_info_t dma_mem_info = {
892-
.extra_heap_caps = extra_heap_caps,
893-
.dma_alignment_bytes = DMA_DESC_MEM_ALIGN_SIZE,
894-
};
895-
return esp_dma_capable_malloc(size, &dma_mem_info, out_ptr, actual_size);
887+
(void) host_id; //remain for extendability
888+
ESP_RETURN_ON_FALSE((extra_heap_caps & MALLOC_CAP_SPIRAM) == 0, NULL, SPI_TAG, "external memory is not supported now");
889+
890+
size_t dma_requir = 16; //TODO: IDF-10111, using max alignment temp, refactor to "gdma_get_alignment_constraints" instead
891+
return heap_caps_aligned_calloc(dma_requir, 1, size, extra_heap_caps | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
896892
}
897893

898894
const spi_bus_attr_t* spi_bus_get_attr(spi_host_device_t host_id)

components/esp_driver_spi/test_apps/master/main/test_spi_master.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,10 @@ TEST_CASE("SPI master hd dma TX without RX test", "[spi]")
10921092
same_pin_func_sel(bus_cfg, dev_cfg, 0);
10931093

10941094
uint32_t buf_size = 32;
1095-
uint8_t *mst_send_buf = heap_caps_malloc(buf_size, MALLOC_CAP_DMA);
1096-
uint8_t *mst_recv_buf = heap_caps_calloc(buf_size, 1, MALLOC_CAP_DMA);
1097-
uint8_t *slv_send_buf = heap_caps_malloc(buf_size, MALLOC_CAP_DMA);
1098-
uint8_t *slv_recv_buf = heap_caps_calloc(buf_size, 1, MALLOC_CAP_DMA);
1095+
uint8_t *mst_send_buf = spi_bus_dma_memory_alloc(TEST_SPI_HOST, buf_size, 0);
1096+
uint8_t *mst_recv_buf = spi_bus_dma_memory_alloc(TEST_SPI_HOST, buf_size, 0);
1097+
uint8_t *slv_send_buf = spi_bus_dma_memory_alloc(TEST_SLAVE_HOST, buf_size, 0);
1098+
uint8_t *slv_recv_buf = spi_bus_dma_memory_alloc(TEST_SLAVE_HOST, buf_size, 0);
10991099

11001100
srand(199);
11011101
for (int i = 0; i < buf_size; i++) {

examples/peripherals/spi_master/lcd/main/spi_master_example_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ DRAM_ATTR static const lcd_init_cmd_t st_init_cmds[] = {
100100
};
101101

102102
DRAM_ATTR static const lcd_init_cmd_t ili_init_cmds[] = {
103-
/* Power contorl B, power control = 0, DC_ENA = 1 */
103+
/* Power control B, power control = 0, DC_ENA = 1 */
104104
{0xCF, {0x00, 0x83, 0X30}, 3},
105105
/* Power on sequence control,
106106
* cp1 keeps 1 frame, 1st frame enable
@@ -128,7 +128,7 @@ DRAM_ATTR static const lcd_init_cmd_t ili_init_cmds[] = {
128128
{0xC5, {0x35, 0x3E}, 2},
129129
/* VCOM control 2, VCOMH=VMH-2, VCOML=VML-2 */
130130
{0xC7, {0xBE}, 1},
131-
/* Memory access contorl, MX=MY=0, MV=1, ML=0, BGR=1, MH=0 */
131+
/* Memory access control, MX=MY=0, MV=1, ML=0, BGR=1, MH=0 */
132132
{0x36, {0x28}, 1},
133133
/* Pixel format, 16bits/pixel for RGB/MCU interface */
134134
{0x3A, {0x55}, 1},
@@ -377,7 +377,7 @@ static void display_pretty_colors(spi_device_handle_t spi)
377377
uint16_t *lines[2];
378378
//Allocate memory for the pixel buffers
379379
for (int i = 0; i < 2; i++) {
380-
lines[i] = heap_caps_malloc(320 * PARALLEL_LINES * sizeof(uint16_t), MALLOC_CAP_DMA);
380+
lines[i] = spi_bus_dma_memory_alloc(LCD_HOST, 320 * PARALLEL_LINES * sizeof(uint16_t), 0);
381381
assert(lines[i] != NULL);
382382
}
383383
int frame = 0;

examples/peripherals/spi_slave_hd/segment_mode/seg_master/main/app_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ void app_main(void)
203203
printf("Slave MAX Send Buffer Size: %"PRIu32"\n", slave_max_tx_buf_size);
204204
printf("Slave MAX Receive Buffer Size: %"PRIu32"\n", slave_max_rx_buf_size);
205205

206-
uint8_t *recv_buf = heap_caps_calloc(1, rx_buf_size, MALLOC_CAP_DMA);
206+
uint8_t *recv_buf = spi_bus_dma_memory_alloc(MASTER_HOST, rx_buf_size, 0);
207207
if (!recv_buf) {
208208
ESP_LOGE(TAG, "No enough memory!");
209209
abort();
210210
}
211-
uint8_t *send_buf = heap_caps_calloc(1, slave_max_rx_buf_size, MALLOC_CAP_DMA);
211+
uint8_t *send_buf = spi_bus_dma_memory_alloc(MASTER_HOST, slave_max_rx_buf_size, 0);
212212
if (!send_buf) {
213213
ESP_LOGE(TAG, "No enough memory!");
214214
abort();

0 commit comments

Comments
 (0)