components/driver/i2s.c
/**
 * @brief   Allocate memory for DMA buffer and descriptor
 *
 * @param   i2s_num     I2S device number
 * @param   dma_obj     DMA object
 * @return
 *      - ESP_OK            Allocate success
 *      - ESP_ERR_NO_MEM    No memory for DMA buffer
 */
@brief   Allocate memory for DMA buffer and descriptor
@param   i2s_num     I2S device number
@param   dma_obj     DMA object
@return
- ESP_OK            Allocate success
- ESP_ERR_NO_MEM    No memory for DMA buffer
@brief Allocate memory for DMA buffer and descriptor @param i2s_num I2S device number @param dma_obj DMA object @return - ESP_OK Allocate success - ESP_ERR_NO_MEM No memory for DMA buffer
static esp_err_t i2s_alloc_dma_buffer(i2s_port_t i2s_num, i2s_dma_t *dma_obj)
{
    esp_err_t ret = ESP_OK;
    ESP_GOTO_ON_FALSE(dma_obj, ESP_ERR_INVALID_ARG, err, TAG, "I2S DMA object can't be NULL");
    uint32_t buf_cnt = p_i2s[i2s_num]->dma_buf_count;
    for (int cnt = 0; cnt < buf_cnt; cnt++) {
        dma_obj->buf[cnt] = (char *) heap_caps_calloc(dma_obj->buf_size, sizeof(char), MALLOC_CAP_DMA);
        ESP_GOTO_ON_FALSE(dma_obj->buf[cnt], ESP_ERR_NO_MEM, err, TAG, "Error malloc dma buffer");
        memset(dma_obj->buf[cnt], 0, dma_obj->buf_size);
        ESP_LOGD(TAG, "Addr[%d] = %d", cnt, (int)dma_obj->buf[cnt]);
        dma_obj->desc[cnt] = (lldesc_t *) heap_caps_calloc(1, sizeof(lldesc_t), MALLOC_CAP_DMA);
        ESP_GOTO_ON_FALSE(dma_obj->desc[cnt], ESP_ERR_NO_MEM, err, TAG,  "Error malloc dma description entry");
    }
    for (int cnt = 0; cnt < buf_cnt; cnt++) {
        dma_obj->desc[cnt]->owner = 1;
        dma_obj->desc[cnt]->eof = 1;
        dma_obj->desc[cnt]->sosf = 0;
        dma_obj->desc[cnt]->length = dma_obj->buf_size;
        dma_obj->desc[cnt]->size = dma_obj->buf_size;
        dma_obj->desc[cnt]->buf = (uint8_t *) dma_obj->buf[cnt];
        dma_obj->desc[cnt]->offset = 0;
        dma_obj->desc[cnt]->empty = (uint32_t)((cnt < (buf_cnt - 1)) ? (dma_obj->desc[cnt + 1]) : dma_obj->desc[0]);
    }
    ESP_LOGI(TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_obj->buf_size, buf_cnt);
    return ESP_OK;
err:
    i2s_delete_dma_buffer(i2s_num, dma_obj);
    return ret;
}
static esp_err_t i2s_alloc_dma_buffer ( i2s_port_t i2s_num , i2s_dma_t * dma_obj ) { esp_err_t ret = ESP_OK ; ESP_GOTO_ON_FALSE ( dma_obj , ESP_ERR_INVALID_ARG , err , TAG , " " ) ; uint32_t buf_cnt = p_i2s [ i2s_num ] -> dma_buf_count ; for ( int cnt = 0 ; cnt < buf_cnt ; cnt ++ ) { dma_obj -> buf [ cnt ] = ( char * ) heap_caps_calloc ( dma_obj -> buf_size , sizeof ( char ) , MALLOC_CAP_DMA ) ; ESP_GOTO_ON_FALSE ( dma_obj -> buf [ cnt ] , ESP_ERR_NO_MEM , err , TAG , " " ) ; memset ( dma_obj -> buf [ cnt ] , 0 , dma_obj -> buf_size ) ; ESP_LOGD ( TAG , " " , cnt , ( int ) dma_obj -> buf [ cnt ] ) ; dma_obj -> desc [ cnt ] = ( lldesc_t * ) heap_caps_calloc ( 1 , sizeof ( lldesc_t ) , MALLOC_CAP_DMA ) ; ESP_GOTO_ON_FALSE ( dma_obj -> desc [ cnt ] , ESP_ERR_NO_MEM , err , TAG , " " ) ; } for ( int cnt = 0 ; cnt < buf_cnt ; cnt ++ ) { dma_obj -> desc [ cnt ] -> owner = 1 ; dma_obj -> desc [ cnt ] -> eof = 1 ; dma_obj -> desc [ cnt ] -> sosf = 0 ; dma_obj -> desc [ cnt ] -> length = dma_obj -> buf_size ; dma_obj -> desc [ cnt ] -> size = dma_obj -> buf_size ; dma_obj -> desc [ cnt ] -> buf = ( uint8_t * ) dma_obj -> buf [ cnt ] ; dma_obj -> desc [ cnt ] -> offset = 0 ; dma_obj -> desc [ cnt ] -> empty = ( uint32_t ) ( ( cnt < ( buf_cnt - 1 ) ) ? ( dma_obj -> desc [ cnt + 1 ] ) : dma_obj -> desc [ 0 ] ) ; } ESP_LOGI ( TAG , " " , dma_obj -> buf_size , buf_cnt ) ; return ESP_OK ; err : i2s_delete_dma_buffer ( i2s_num , dma_obj ) ; return ret ; }
@brief   Allocate memory for DMA buffer and descriptor
 @param   i2s_num     I2S device number
 @param   dma_obj     DMA object
 @return
      - ESP_OK            Allocate success
      - ESP_ERR_NO_MEM    No memory for DMA buffer
@brief Allocate memory for DMA buffer and descriptor @param i2s_num I2S device number @param dma_obj DMA object @return - ESP_OK Allocate success - ESP_ERR_NO_MEM No memory for DMA buffer
/* Allocate DMA buffer */ /* Initialize DMA buffer to 0 */ /* Allocate DMA descpriptor */ /* DMA descriptor must be initialize after all descriptor has been created, otherwise they can't be linked together as a chain */ /* Initialize DMA descriptor */ /* Link to the next descriptor */ /* Delete DMA buffer if failed to allocate memory */