ADC  8.0
Analog to Digital Conversor library for the Teensy 3.0 microprocessor
VREF.h
1 /* Teensy 4, 3.x, LC ADC library
2  * https://github.com/pedvide/ADC
3  * Copyright (c) 2019 Pedro Villanueva
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #ifndef ADC_VREF_H
27 #define ADC_VREF_H
28 
29 #include <Arduino.h>
30 
31 #include <atomic.h>
32 #include <settings_defines.h>
33 
34 #ifdef ADC_USE_INTERNAL_VREF
35 
37 namespace VREF
38 {
39 
41 
51  inline void start(uint8_t mode = VREF_SC_MODE_LV_HIGHPOWERBUF, uint8_t trim = 0x20) {
52  VREF_TRM = VREF_TRM_CHOPEN | (trim&0x3F); // enable module and set the trimmer to medium (max=0x3F=63)
53  // enable 1.2 volt ref with all compensations in high power mode
54  VREF_SC = VREF_SC_VREFEN | VREF_SC_REGEN | VREF_SC_ICOMPEN | VREF_SC_MODE_LV(mode);
55 
56  // "PMC_REGSC[BGEN] bit must be set if the VREF regulator is
57  // required to remain operating in VLPx modes."
58  // Also "If the chop oscillator is to be used in very low power modes,
59  // the system (bandgap) voltage reference must also be enabled."
60  // enable bandgap, can be read directly with ADC_INTERNAL_SOURCE::BANDGAP
61  atomic::setBitFlag(PMC_REGSC, PMC_REGSC_BGBE);
62  }
63 
65 
68  inline void trim(uint8_t trim) {
69  bool chopen = atomic::getBitFlag(VREF_TRM, VREF_TRM_CHOPEN);
70  VREF_TRM = (chopen ? VREF_TRM_CHOPEN : 0) | (trim&0x3F);
71  }
72 
74 
76  __attribute__((always_inline)) inline void stop(){
77  VREF_SC = 0;
78  atomic::clearBitFlag(PMC_REGSC, PMC_REGSC_BGBE);
79  }
80 
82 
91  __attribute__((always_inline)) inline volatile bool isStable() {
92  return atomic::getBitFlag(VREF_SC, VREF_SC_VREFST);
93  }
94 
96 
99  __attribute__((always_inline)) inline volatile bool isOn() {
100  return atomic::getBitFlag(VREF_SC, VREF_SC_VREFEN);
101  }
102 
104 
108  inline void waitUntilStable() {
109  delay(35); // see note in isStable()
110  while(isOn() && !isStable()) {
111  yield();
112  }
113  }
114 
115 }
116 
117 #endif // ADC_USE_INTERNAL_VREF
118 
119 #endif // ADC_VREF_H
VREF::isStable
volatile bool isStable()
Check if the internal reference has stabilized.
Definition: VREF.h:91
VREF::waitUntilStable
void waitUntilStable()
Wait for the internal reference to stabilize.
Definition: VREF.h:108
VREF::trim
void trim(uint8_t trim)
Set the trim.
Definition: VREF.h:68
VREF::stop
void stop()
Stops the internal reference.
Definition: VREF.h:76
VREF::isOn
volatile bool isOn()
Check if the internal reference is on.
Definition: VREF.h:99
VREF::start
void start(uint8_t mode=VREF_SC_MODE_LV_HIGHPOWERBUF, uint8_t trim=0x20)
Start the 1.2V internal reference (if present)
Definition: VREF.h:51
VREF
Controls the Teensy internal voltage reference module (VREFV1)
Definition: VREF.h:37