Hi,
I have very weird issue, when reading specific number of ADC readings and I have no clue, why is that. I used to use this approach with UNO and it was OK, but here...I don't now what is wrong. Basically, I setup timers and ADC, but Timer is not started after configuration. It is started in main loop() event and when I start it, I run "while" loop to wait for conversions to be finished. ADC handler stops the timer. However, everytime I try to wait for completion with "while" controller stucks... but when I do the same with serial.read() (when I enter commands), code works. What do I miss here?
#define SMP_RATE 200000UL
#define CLK_MAIN 84000000UL
#define TMR_CNTR CLK_MAIN / (2 * SMP_RATE)
const int sampleQty = 1024;
double samples[sampleQty];
double samples2[sampleQty];
int currSample;
volatile int isr_counter = 0;
volatile int dur, nowT;
volatile int stopper;
int dum1, dum2;
void setup_pio_TIOA0 () { // Configure Ard pin 2 as output from TC0 channel A (copy of trigger event)
PIOB->PIO_PDR = PIO_PB25B_TIOA0 ; // disable PIO control
PIOB->PIO_IDR = PIO_PB25B_TIOA0 ; // disable PIO interrupts
PIOB->PIO_ABSR |= PIO_PB25B_TIOA0 ; // switch to B peripheral
}
void timerConfig() {
pmc_enable_periph_clk(ID_TC0); // enable timer 0, chan 0.
TcChannel *pTcChan = &(TC0->TC_CHANNEL)[0]; // get TC0, chan 0 pointer.
pTcChan->TC_CCR = TC_CCR_CLKDIS; // disable TC clock.
pTcChan->TC_IDR = 0xFFFFFFFF; // disable interrupts.
pTcChan->TC_SR; // clear status register.
pTcChan->TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 | // use TCLK1 (prescale by 2, so 42MHz).
TC_CMR_WAVE | // // waveform mode.
TC_CMR_WAVSEL_UP_RC | // RC as threshold for PWM.
TC_CMR_EEVT_XC0 | // external events from XC0.
TC_CMR_ACPA_CLEAR |
TC_CMR_ACPC_CLEAR |
TC_CMR_BCPB_CLEAR |
TC_CMR_BCPC_CLEAR;
pTcChan->TC_RA = TMR_CNTR / 2; // counter resets on RC.
pTcChan->TC_RC = TMR_CNTR; // square wave.
pTcChan->TC_CMR = (pTcChan->TC_CMR & 0xFFF0FFFF) | TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_SET ; // set clear and set from RA and RC compares
setup_pio_TIOA0();
//TC_Start(TC0, 0); ///
}
void adcConfig() {
pmc_enable_periph_clk(ID_ADC); // enable ADC.
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST); // dar paž?t d?l šito parametr?.
NVIC_EnableIRQ (ADC_IRQn); // enable ADC interrupt vector.
ADC->ADC_IDR = 0xFFFFFFFF; // disable all interrupts.
ADC->ADC_CGR = 0x15555555; // All gains set to x1.
ADC->ADC_COR = 0x00000000; // All offsets off, but might be used in the future TODO.
adc_disable_all_channel(ADC);
adc_enable_interrupt(ADC, ADC_IER_EOC7); // enable EOC7 interrupt.
adc_enable_channel(ADC, ADC_CHANNEL_7);
adc_enable_channel(ADC, ADC_CHANNEL_6);
adc_configure_trigger(ADC, ADC_TRIG_TIO_CH_0, 0);
}
void ADC_Handler(void) {
if ((ADC->ADC_ISR & ADC_ISR_EOC7) && (ADC->ADC_ISR & ADC_ISR_EOC6)) {
int val = *(ADC->ADC_CDR + 7);
samples[currSample] = val - 2048;
val = *(ADC->ADC_CDR + 6);
samples2[currSample] = val - 2048;
currSample += 1;
if (currSample == sampleQty) {stopper = 1; TC_Stop(TC0, 0);}
}
isr_counter += 1;
}
void setup() {
Serial.begin(115200);
adcConfig();
timerConfig();
isr_counter = 0;
dum1 = dum2 = 0;
stopper = 0;
}
void loop() {
delay(100);
stopper = 0;
Serial.println("Before");
TC_Start(TC0, 0);
while (stopper == 0);
Serial.println("Done");
/* THIS APPROACH WORKS
while (Serial.available()) {
uint8_t a = Serial.read();
if (a == 'x') {
//isr_counter = 0;
dum2 += 1;
Serial.println(dum2);
nowT = micros();
TC_Start(TC0, 0);
//adc_start(ADC);
//while ((ADC->ADC_CR & ADC_CR_SWRST) != ADC_CR_SWRST) {};
//for (int i = 0; i < sampleQty; i++) {
// Serial.println(samples[i], 2);
//}
} else {
dum1 += 1;
Serial.println(dum1);
Serial.print(isr_counter); Serial.print(" ");
Serial.print("avg. us: "); Serial.print(nowT); Serial.print(" "); Serial.print(dur); Serial.print(" ");
if (sampleQty != 0) {Serial.println((float)dur/sampleQty, 4);} else {Serial.println(0, 4);}
for (int i = 0; i < sampleQty; i++) {Serial.print(samples[i]); Serial.print(" "); Serial.println(samples2[i]);}
}
}*/
}
Is there a better way to read chunks of ADC values with DUE? Any recommendations or suggestions are welcome.
Thanks alot for your help.