Code for open gamma spectrometer works on teensy 3.6, fails on teensy 4

Status
Not open for further replies.

MakerV

Member
Having a great deal of issues with the following code. without the #if defined (arduino_teensy40)
this code runs flawlessly on a teensy 3.6. When the code has been updated to T4 -- there is a major error reported with the following errors:

'ADC0_CFG1' was not declared in this scope

sketch_feb18a: In function 'void setup()':
sketch_feb18a:39: error: 'ADC0_CFG1' was not declared in this scope
ADC0_CFG1 = 0b00011000;
^
sketch_feb18a:48: error: 'ADC0_CFG2' was not declared in this scope
ADC0_CFG2 = 0b00000;
^
sketch_feb18a:59: error: 'ADC0_SC2' was not declared in this scope
ADC0_SC2 = 0b0000000;
^
sketch_feb18a:71: error: 'ADC0_SC3' was not declared in this scope
ADC0_SC3 = 0b10000111;
^
sketch_feb18a:74: error: 'ADC_SC3_CAL' was not declared in this scope
while (ADC0_SC3 & ADC_SC3_CAL) {}
^
sketch_feb18a:78: error: 'ADC0_CLPS' was not declared in this scope
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
^
sketch_feb18a:78: error: 'ADC0_CLP4' was not declared in this scope
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
^
sketch_feb18a:78: error: 'ADC0_CLP3' was not declared in this scope
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
^
sketch_feb18a:78: error: 'ADC0_CLP2' was not declared in this scope
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
^
sketch_feb18a:78: error: 'ADC0_CLP1' was not declared in this scope
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
^
sketch_feb18a:78: error: 'ADC0_CLP0' was not declared in this scope
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
^
sketch_feb18a:80: error: 'ADC0_PG' was not declared in this scope
ADC0_PG = sum;
^
sketch_feb18a:81: error: 'ADC0_CLMS' was not declared in this scope
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
^
sketch_feb18a:81: error: 'ADC0_CLM4' was not declared in this scope
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
^
sketch_feb18a:81: error: 'ADC0_CLM3' was not declared in this scope
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
^
sketch_feb18a:81: error: 'ADC0_CLM2' was not declared in this scope
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
^
sketch_feb18a:81: error: 'ADC0_CLM1' was not declared in this scope
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
^
sketch_feb18a:81: error: 'ADC0_CLM0' was not declared in this scope
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
^
sketch_feb18a:83: error: 'ADC0_MG' was not declared in this scope
ADC0_MG = sum;
^
sketch_feb18a: In function 'void processData()':
sketch_feb18a:107: error: 'ADC0_SC1A' was not declared in this scope
ADC0_SC1A = 0b0000000;
^
sketch_feb18a:110: error: 'ADC_SC1_COCO' was not declared in this scope
while (!(ADC0_SC1A & ADC_SC1_COCO)) {}
^
sketch_feb18a:113: error: 'ADC0_RA' was not declared in this scope
ADC_res = ADC0_RA;
^
sketch_feb18a:120: error: 'GPIOC_PDOR' was not declared in this scope
GPIOC_PDOR |= 1; // 1 = 1<<0
^


I am unable to find documentation that points to the setting of the ADC for teensy 4
If anyone can help me figure out the errors that are happening... I am assuming that the the code is referencing registers that don't exist?
Wrong ADC registers?




Code:
#if defined(ARDUINO_TEENSY40)
  //
  // code for Teensy 4.0 only
  //
#elif defined(ARDUINO_TEENSY40)
  //
  // code for Teensy 4.0 only
  //
#endif
 
// Result from ADC
int ADC_res;
 
void setup() {
  // Prepare USB connection
  Serial.begin(0);
  
  // Prepare output signal
  pinMode(13, OUTPUT);
  
  // Prepare reset signal
  pinMode(15, OUTPUT);
  
  // Prepare intterrupt signal
  pinMode(14, INPUT);
  attachInterrupt(14, processData, RISING);
  
  // Configure ADC
  /*  DEFAULT
      0       ADLPC (Low-Power Configuration)
      0       ADIV (Clock Divide Select)
      0
      0       ADLSMP (Sample time configuration)
      0       MODE (Conversion mode selection) (00=8/9, 01=12/13, 10=10/11, 11=16/16 bit; diff=0/1)
      0
      0       ADICLK (Input Clock Select)
      0
  */
  ADC0_CFG1 = 0b00011000;
  
  /*  DEFAULT
      0       MUXSEL (ADC Mux Select)
      0       ADACKEN (Asynchrononous Clock Output Enable)
      0       ADHSC (High-Speed Configuration)
      0       ADLSTS (Long Sample Time Select) (00=+20 cycles, 01=+12, 10=+6, 11=+2)
      0
  */
  ADC0_CFG2 = 0b00000;
  
  /*  DEFAULT
      0       ADTRG (Conversion Trigger Select)
      0       ACFE (Compare Function Enable)
      0       ACFGT (Compare Function Greater than Enable)
      0       ACREN (Compare Function Range Enable)
      0       ACREN (COmpare Function Range Enable)
      0       DMAEN (DMA Enable)
      0       REFSEL (Voltage Reference Selection) (00=default,01=alternate,10=reserved,11=reserved)
  */
  ADC0_SC2 = 0b0000000;
  
  /*  DEFAULT
      0       CAL (Calibration)
      0       CALF (read only)
      0       (Reserved)
      0
      0       ADCO (Continuous Conversion Enable)
      0       AVGS (Hardware Average Enable)
      0       AVGS (Hardware Average Select) (00=4 times, 01=8, 10=16, 11=32)
      0
  */
  ADC0_SC3 = 0b10000111;
  
  // Waiting for calibration to finish. The documentation is confused as to what flag to be waiting for (SC3[CAL] on page 663 and SC1n[COCO] on page 687+688).
  while (ADC0_SC3 & ADC_SC3_CAL) {}
  

  uint16_t sum;
  sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
  sum = (sum / 2) | 0x8000;
  ADC0_PG = sum;
  sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
  sum = (sum / 2) | 0x8000;
  ADC0_MG = sum;
  /* End of borrowed code */
}
 
void loop() {
}
 
void processData() {
  // Wait for signal to reach peak
  delayMicroseconds(2);
  
  // DEBUG: Signal HIGH that ADC and then USB will be running
  //GPIOC_PDOR |= 1<<5;
  
  // Start ADC
  /*  DEFAULT
      0       AIEN (Interrupt Enable)
      0       DIFF (0=single, 1=diff)
      1       ADCH (00000=A10 for DIFF=0; 00000=A10+A11 for DIFF=1; See page 98 in documentation)
      1
      1
      1
      1
  */
  ADC0_SC1A = 0b0000000;
  
  // Wait for ADC
  while (!(ADC0_SC1A & ADC_SC1_COCO)) {}
  
  // Retrieve data from ADC
  ADC_res = ADC0_RA;
  
  // Send data away
  Serial.println( ADC_res );
  Serial.send_now();
  
  // Set RESET signal to HIGH for 1 µs
  GPIOC_PDOR |= 1; // 1 = 1<<0
  delayMicroseconds(1);
  
  // DEBUG: Signal LOW that ADC and USB are done (takes ~40 µs with max cycles and sampling at 16 bit)
  // And set RESET signal to LOW
  //GPIOC_PDOR &= ~(1<<5 | 1);
  GPIOC_PDOR &= ~1;
}
 
Status
Not open for further replies.
Back
Top