Auto Analog Audio v1.50.0
TMRh20 2020 - Automatic DAC, ADC & Timers on Arduino Due
Loading...
Searching...
No Matches
AutoAnalogAudio.cpp
Go to the documentation of this file.
1
22/****************************************************************************/
23
24#if defined (ESP32)
25
26#include "../AutoAnalogAudio.h"
27#include "driver/i2s.h"
28#include "driver/adc.h"
29#include "freertos/FreeRTOS.h"
30#include "freertos/task.h"
31#include "esp_event.h"
32
33
34/****************************************************************************/
35/* Public Functions */
36/****************************************************************************/
37
38 volatile bool dacTaskActive;
39 volatile bool dacTaskCreated;
40 uint32_t adcSamples;
41 uint16_t tmpADCBuffer16[MAX_BUFFER_SIZE];
42 volatile uint32_t lastAdcTime;
43 uint8_t *adcPtr = NULL;
44
45 uint8_t dacVar;
46
48
51
52 dacTaskActive = false;
53 dacTaskCreated = false;
54 dacEnabled = false;
55 dacTaskHandle = NULL;
56 i2sStopped = true;
57 dacVar = 1;
58 adcPtr = &adcBuffer[0];
59
60 sampleRate = 0;
61 lastDacSample = 0;
62 adcChannel = (adc1_channel_t)0;
63
64 for(int i=0; i<MAX_BUFFER_SIZE; i++){
65 dacBuffer[i] = 0;
66 dacBuffer16[i] = 0;
67 }
68 adcSamples = 0;
69 lastAdcTime = 0;
70
71}
72
73/****************************************************************************/
74
75void DACC_Handler();
76
77/****************************************************************************/
78
79void adcTask(void *args){
80 for( ;; ){
81 if(*(uint32_t*)args > 0){
82 size_t bytesRead = 0;
83 i2s_read(I2S_NUM_0, &tmpADCBuffer16, *(uint32_t*)args * 2, &bytesRead, 500 / portTICK_PERIOD_MS);
84 adcSamples = 0;
85 lastAdcTime = millis();
86 }else{
87 if(millis() - lastAdcTime > 150){
88 vTaskDelay( 5 / portTICK_PERIOD_MS);
89 }
90 }
91 }
92}
93
94/****************************************************************************/
95
96uint32_t dacTTimer = 0;
97
98void dacTask(void *args){
99 for( ;; ){
100
101 if(*(uint8_t*)args == 1 && dacTaskActive == true){
102 DACC_Handler();
103 }else{
104 *(uint8_t*)args = 3;
105 vTaskDelay( 10 / portTICK_PERIOD_MS);
106
107 }
108 }
109}
110
111/****************************************************************************/
112
113void AutoAnalog::begin(bool enADC, bool enDAC){
114
115 i2s_mode_t myMode = (i2s_mode_t)I2S_MODE_MASTER;
116
117
118 if(enADC){
119 myMode = (i2s_mode_t)(myMode | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN);
120 }
121 if(enDAC){
122 myMode = (i2s_mode_t)(myMode | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
123 }
124
125 i2s_cfg = {
126 .mode = (i2s_mode_t)myMode,
127 .sample_rate = 16000, // The format of the signal using ADC_BUILT_IN
128 .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
129 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
130 .communication_format = I2S_COMM_FORMAT_I2S_LSB,
131 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
132 .dma_buf_count = 2,
133 .dma_buf_len = MAX_BUFFER_SIZE,
134 .use_apll = false,
135 .tx_desc_auto_clear = false,
136 .fixed_mclk = 0
137 };
138 i2s_driver_install(I2S_NUM_0, &i2s_cfg, 0, NULL);
139
140
141 if(enADC){
142 i2s_set_adc_mode(ADC_UNIT_1, adcChannel); //pin 32
143 adc1_config_channel_atten(adcChannel, ADC_ATTEN_DB_11);
144 i2s_adc_enable(I2S_NUM_0);
145 }
146 if(enDAC){
147 i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
148 dacEnabled = true;
149 }
150
151 sampleRate = 16000;
152
153 setSampleRate(sampleRate,true);
154
155
156}
157
158/****************************************************************************/
159
160void AutoAnalog::dacInterrupts(bool enabled, bool withinTask){
161
162 if(enabled){
163 if(dacTaskCreated == false){
164 dacTaskCreated = true;
165 //Serial.print("Cr Task ");
166 dacVar = 1;
167 dacTaskActive = true;
168 xTaskCreate(dacTask,"DAC Task",3062,&dacVar,tskIDLE_PRIORITY + 1,&dacTaskHandle);
169 }
170 }else{
171 if(dacTaskCreated == true){
172 dacTaskCreated = false;
173 dacTaskActive = false;
174 //Serial.println("Dac Int false");
175
176 if(dacTaskHandle != NULL){
177 if(dacVar == 1 && !withinTask){
178 //Serial.println("Set Dac Var 2");
179 dacVar = 2;
180 while(dacVar != 3){ delay(3); }
181 }
182
183 //Serial.println("Del Task");
184 vTaskDelete(dacTaskHandle);
185 }
186
187 }
188 }
189
190}
191
192/****************************************************************************/
193
194void AutoAnalog::rampIn(uint8_t sample){
195
196
197 uint8_t tmpBuf[255];
198 uint8_t zeroBuff[MAX_BUFFER_SIZE];
199 memset(zeroBuff,0,MAX_BUFFER_SIZE);
200
201 uint16_t counter = 0;
202 for(uint16_t i=0; i<sample; i++){
203 tmpBuf[i] = i;
204 counter++;
205 }
206
207 size_t bytesWritten = 0;
208 size_t bytesWritten2 = 0;
209
210 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
211 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
212 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
213
214 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE-counter,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
215 i2s_write_expand(I2S_NUM_0,&tmpBuf[0],counter,8,16,&bytesWritten2, 500 / portTICK_PERIOD_MS);
216}
217
218/****************************************************************************/
219
220void AutoAnalog::rampOut(uint8_t sample){
221
222
223 uint8_t tmpBuf[255];
224 uint8_t zeroBuff[MAX_BUFFER_SIZE];
225 memset(zeroBuff,0,MAX_BUFFER_SIZE);
226
227 uint16_t counter = 0;
228 for(uint8_t i=lastDacSample; i > 0; i--){
229 tmpBuf[counter++] = i;
230 }
231 size_t bytesWritten = 0;
232
233 i2s_write_expand(I2S_NUM_0,&tmpBuf[0],counter,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
234 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE-counter,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
235 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
236 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
237 i2s_write_expand(I2S_NUM_0,&zeroBuff[0],MAX_BUFFER_SIZE,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
238
239}
240
241/****************************************************************************/
242
243void AutoAnalog::setSampleRate(uint32_t sampRate, bool stereo){
244
245 //if(sampRate == sampleRate){ return; }
246
247 sampleRate = sampRate;
248 //i2s_stop(I2S_NUM_0);
249 //i2s_driver_uninstall(I2S_NUM_0);
250 //delay(5);
251 //i2s_driver_install(I2S_NUM_0, &i2s_cfg, 0, NULL);
252 if(stereo == false){
253 i2s_set_clk(I2S_NUM_0, sampRate, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
254 }else{
255 i2s_set_clk(I2S_NUM_0, sampRate/2, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
256 }
257
258
259}
260
261/****************************************************************************/
262
264
265
266
267}
268
269/****************************************************************************/
270
271void AutoAnalog::enableAdcChannel(uint8_t pinAx){
272
273 adcChannel = (adc1_channel_t)pinAx;
274
275}
276
277/****************************************************************************/
278
279void AutoAnalog::disableAdcChannel(uint8_t pinAx){
280
281 /*if(pinAx > 6){ return; }
282 pinAx = 7 - pinAx;
283 ADC->ADC_CHDR |= 1<< pinAx;*/
284
285}
286
287/****************************************************************************/
288
289void AutoAnalog::getADC(uint32_t samples){
290
291 if(!adcTaskCreated){
292 adcTaskCreated = true;
293 xTaskCreate(adcTask,"ADC Task",2048,&adcSamples,tskIDLE_PRIORITY + 1,NULL);
294 //Serial.println("Cr ADC");
295 }
296 while(adcSamples > 0){ delayMicroseconds(100); };
297 //Serial.println("Get ADC");
298 uint16_t offset = (int)adcChannel * 0x1000 + 0xFFF; // 4high bits == channel. Data is inverted.
299 for(uint32_t i=0; i<samples;i++){
300 tmpADCBuffer16[i] = offset - tmpADCBuffer16[i];
301 adcBuffer16[i] = tmpADCBuffer16[i];
302 }
303 //Serial.println("samps");
304 adcSamples = samples;
305
306
307
308
309}
310
311/****************************************************************************/
312
313void AutoAnalog::feedDAC(uint8_t dacChannel, uint32_t samples, bool startInterrupts){
314
315
316 //uint8_t buf[MAX_BUFFER_SIZE * 2];
317 size_t bytesWritten = 0;
318
319 if(dacBitsPerSample == 16){
320 for(int i=0; i<samples; i++){
321 dacBuffer[i] = (uint8_t)dacBuffer16[i];
322 }
323 }
324
325 /*if(startInterrupts == true){
326
327 //uint8_t zeroSample[MAX_BUFFER_SIZE ];
328 memset(buf,0,MAX_BUFFER_SIZE*2);
329 i2s_write_expand(I2S_NUM_0,&buf[0],(MAX_BUFFER_SIZE*2) - dacBuffer[0],8,16,&bytesWritten, 100 / portTICK_PERIOD_MS);
330 //i2s_write_expand(I2S_NUM_0,&zeroSample,(MAX_BUFFER_SIZE),8,16,&bytesWritten, 100 / portTICK_PERIOD_MS);
331
332 for(uint i=0; i <= (dacBuffer[0] * 2); i++){
333 uint test = i / 2;
334 i2s_write_expand(I2S_NUM_0,&test,1,8,16,&bytesWritten, 100 / portTICK_PERIOD_MS);
335 }
336 }*/
337
338 /*if(i2sStopped){
339 i2sStopped = false;
340 i2s_start(I2S_NUM_0);
341 }*/
342
343 if(dacEnabled == false){
344 //dacEnabled = true;
345 //i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
346 }
347
348 i2s_write_expand(I2S_NUM_0,&dacBuffer[0],samples,8,16,&bytesWritten, 500 / portTICK_PERIOD_MS);
349 lastDacSample = dacBuffer[bytesWritten-1];
350
351
352 if(startInterrupts == true){
353 dacTaskActive = true;
354 //i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
355 dacInterrupts(true);
356
357 }
358
359
360
361}
362
363/****************************************************************************/
364/* Private Functions */
365/****************************************************************************/
366
367void AutoAnalog::dacBufferStereo(uint8_t dacChannel){
368
369
370
371}
372
373/****************************************************************************/
374
375uint32_t AutoAnalog::frequencyToTimerCount(uint32_t frequency){
376 //return VARIANT_MCK / 2UL / frequency;
377 return 0;
378}
379
380/****************************************************************************/
381
382void AutoAnalog::adcSetup(void){
383
384
385}
386
387/****************************************************************************/
388
389void AutoAnalog::adcInterrupts(bool enabled){
390
391}
392
393/****************************************************************************/
394
395void AutoAnalog::dacSetup(void){
396
397
398}
399
400/****************************************************************************/
401
402
403void AutoAnalog::disableDAC(bool withinTask){
404
405 dacTaskActive = false;
406 if(!withinTask){
407 dacInterrupts(false,false);
408 }
409
410
411 //i2s_set_dac_mode(I2S_DAC_CHANNEL_DISABLE);
412 i2s_zero_dma_buffer(I2S_NUM_0);
413 //dacEnabled = false;
414 if(withinTask){
415 dacInterrupts(false,true);
416 }
417
418}
419
420/****************************************************************************/
421
422void AutoAnalog::dacHandler(void){
423
424
425}
426
427/****************************************************************************/
428
429void AutoAnalog::tcSetup (uint32_t sampRate){
430
431
432
433}
434
435/****************************************************************************/
436
437void AutoAnalog::tc2Setup (uint32_t sampRate)
438{
439
440}
441
442/****************************************************************************/
443
444
445
446#endif //#if defined (ESP32)
void adcInterrupts(bool enabled=true)
void dacHandler(void)
uint8_t dacBitsPerSample
void rampIn(uint8_t sample)
uint8_t adcBuffer[MAX_BUFFER_SIZE]
uint8_t dacBuffer[MAX_BUFFER_SIZE]
TaskHandle_t dacTaskHandle
void getADC(uint32_t samples=MAX_BUFFER_SIZE)
uint16_t adcBuffer16[MAX_BUFFER_SIZE]
void rampOut(uint8_t sample)
void disableDAC(bool withinTask=false)
uint16_t dacBuffer16[MAX_BUFFER_SIZE]
void setSampleRate(uint32_t sampRate=0, bool stereo=true)
void dacInterrupts(bool enabled=true, bool withinTask=false)
void enableAdcChannel(uint8_t pinAx)
void feedDAC(uint8_t dacChannel=0, uint32_t samples=MAX_BUFFER_SIZE, bool startInterrupts=false)
uint8_t adcBitsPerSample
void triggerADC()
void begin(bool enADC, bool enDAC)
void disableAdcChannel(uint8_t pinAx)