The issue is that I am getting very low frequency (7-10Hz) cosine waves for certain frequency-step values given to DAC control register (SENS_SAR_DAC_CTRL1_REG). I am expecting frequencies of around 20KHz and 25KHz on those frequency-step points.
I am using a modified version of DAC Cosine Wave Generator example and a few elements from a GitHub repository in which the author is generating cosine wave by writing directly to the registers. Links to both resources here :
https://github.com/espressif/esp-idf/bl ... ple_main.c
https://github.com/krzychb/dac-cosine/b ... c-cosine.c
In addition to that, I have added a timer callback that updates the frequency of the Cosine Wave by a random frequency-step value. The calculation for that frequency step value is given in section 29.5.4 : Cosine Waveform Generator of the ESP32 technical reference manual.
freq = dig_clk_rtc_freq · SENS_SAR_SW_FSTEP/65536
In short, providing a factor of 8 to SENS_SAR_SW_FSTEP, produces a output frequency (freq) of approximately 1KHz, given the ESP32 clock frequency (dig_clk_rtc_freq) is 8MHz.
So coming back to my application. I am producing cosine waves of frequencies between 20KHz - 30KHz over a period of 1000ms. After 1000ms, the wave generation is stopped. Furthermore, every 50ms, the wave frequency is randomly changed between the specified range of 20KHz - 30KHz using a timer callback function.
My timer callback has the following structure :
- static void TimerCallback(void* arg)
- {
- // NOTE : for a output frequency range 20KHz - 30KHz, a frequencyStep in range (160-240) is required
- //calculate a random frequencyStep value (between 160-240) here
- //provide that value to the DAC control register
- SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL1_REG, SENS_SW_FSTEP, frequencyStep, SENS_SW_FSTEP_S);
- }
Using my code, a normal cosine wave for me would look like as displayed like this :
However, when certain frequency-step values occur such as 160, 216 (& some others), I am getting glitches all the time. Like this :
Furthermore, I have verified that the correct frequency-step value is being fed to the DAC control register by using the following get method :
- GET_PERI_REG_BITS(SENS_SAR_DAC_CTRL1_REG,7,0);
My query is why such a strange behavior is being exhibited by the DAC control register on certain values ?