How to reset DAC DMA from convert-done callback?

globalsequence
Posts: 2
Joined: Sat Aug 24, 2024 9:18 pm

How to reset DAC DMA from convert-done callback?

Postby globalsequence » Sun Aug 25, 2024 3:55 pm

Hello,

I would like to set the DAC's DMA to call dac_continuous_write() from the callback which occurs 'on_convert_done', however the callback function is never executed in my example below (see first bullet point). Presented below is my code which is set up in two configurations for those who wish to try it:

- if TEST_DMA_CALLBACK is defined as 1, the code is meant to call dac_continuous_write() in the callback function instead of the main loop, however, if I do this then the DMA never resets and the callback never executes. This is the desired configuration, but it does not function as desired.

- if TEST_DMA_CALLBACK is defined as 0, the DMA is reset in the main loop. This is functional, but the code requires halting the main loop and requiring ESP_ERROR_CHECK, which is undesired. I have left this in to display what I am doing with the code. In this instance, the callback functions as 'printf("%lld\n", count_number);' shows that count_number is iterating.

Any help is appreciated, thank you!

Code: Select all

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/dac_continuous.h"
#include "esp_check.h"

#define TEST_DMA_CALLBACK 0

dac_continuous_handle_t dac_handle;
const size_t audio_size = 128;
static uint8_t table_switch = 0;
long long int count_number;
static const char *TAG = "dac_audio";

const uint8_t sine_table[2][128] = {
    {
        64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80, 82, 83, 85, 86,
        88, 89, 91, 92, 93, 95, 96, 97, 99, 100, 101, 103, 104, 105, 106, 107,
        108, 109, 111, 112, 113, 114, 115, 115, 116, 117, 118, 119, 120, 120, 121, 122,
        122, 123, 123, 124, 124, 125, 125, 125, 126, 126, 126, 127, 127, 127, 127, 127,
        127, 127, 127, 127, 127, 127, 126, 126, 126, 125, 125, 125, 124, 124, 123, 123,
        122, 122, 121, 120, 120, 119, 118, 117, 116, 115, 115, 114, 113, 112, 111, 109,
        108, 107, 106, 105, 104, 103, 101, 100, 99, 97, 96, 95, 93, 92, 91, 89,
        88, 86, 85, 83, 82, 80, 79, 77, 76, 74, 73, 71, 70, 68, 67, 65,
    }, 
    {
        64, 62, 60, 59, 57, 56, 54, 53, 51, 50, 48, 47, 45, 44, 42, 41,
        39, 38, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 21, 20,
        19, 18, 16, 15, 14, 13, 12, 12, 11, 10, 9, 8, 7, 7, 6, 5,
        5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4,
        5, 5, 6, 7, 7, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 18,
        19, 20, 21, 22, 23, 24, 26, 27, 28, 30, 31, 32, 34, 35, 36, 38,
        39, 41, 42, 44, 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62
    }
};

static bool IRAM_ATTR dacCallback(void) {
    count_number++;
if(TEST_DMA_CALLBACK){
    static uint8_t table_switch;
    dac_continuous_write(dac_handle, saw_table[table_switch], audio_size, NULL, -1);
    table_switch = table_switch? 0 : 1;
}
    return true;
}

void app_main(void)
{
    dac_continuous_config_t cont_config = {
        .chan_mask = DAC_CHANNEL_MASK_ALL,
        .desc_num = 2,
        .buf_size = 128,
        .freq_hz = 48000,
        .offset = 0,
        .clk_src = DAC_DIGI_CLK_SRC_APLL,
        .chan_mode = DAC_CHANNEL_MODE_SIMUL, 
    };
    dac_continuous_new_channels(&cont_config, &dac_handle);
    dac_continuous_enable(dac_handle);

    dac_event_callbacks_t cbs = {
        .on_convert_done = dacCallback,
        .on_stop = NULL,
    };
    dac_continuous_register_event_callback(dac_handle, &cbs, NULL);

if(TEST_DMA_CALLBACK) {
    dac_continuous_write(dac_handle, sine_table[table_switch], audio_size, NULL, -1);
}

    while(1) {
if(!TEST_DMA_CALLBACK){
    static uint8_t table_switch;
    ESP_ERROR_CHECK(dac_continuous_write(dac_handle, sine_table[table_switch], audio_size, NULL, -1));
    table_switch = table_switch? 0 : 1;
}
    printf("%lld\n", count_number);
    }
}

Who is online

Users browsing this forum: Bing [Bot] and 133 guests