VREF.h
/* Teensy 4, 3.x, LC ADC library
* https://github.com/pedvide/ADC
* Copyright (c) 2019 Pedro Villanueva
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ADC_VREF_H
#define ADC_VREF_H
#include <Arduino.h>
#include <atomic.h>
#include <settings_defines.h>
#ifdef ADC_USE_INTERNAL_VREF
namespace VREF
{
inline void start(uint8_t mode = VREF_SC_MODE_LV_HIGHPOWERBUF, uint8_t trim = 0x20) {
VREF_TRM = VREF_TRM_CHOPEN | (trim&0x3F); // enable module and set the trimmer to medium (max=0x3F=63)
// enable 1.2 volt ref with all compensations in high power mode
VREF_SC = VREF_SC_VREFEN | VREF_SC_REGEN | VREF_SC_ICOMPEN | VREF_SC_MODE_LV(mode);
// "PMC_REGSC[BGEN] bit must be set if the VREF regulator is
// required to remain operating in VLPx modes."
// Also "If the chop oscillator is to be used in very low power modes,
// the system (bandgap) voltage reference must also be enabled."
// enable bandgap, can be read directly with ADC_INTERNAL_SOURCE::BANDGAP
atomic::setBitFlag(PMC_REGSC, PMC_REGSC_BGBE);
}
inline void trim(uint8_t trim) {
bool chopen = atomic::getBitFlag(VREF_TRM, VREF_TRM_CHOPEN);
VREF_TRM = (chopen ? VREF_TRM_CHOPEN : 0) | (trim&0x3F);
}
__attribute__((always_inline)) inline void stop(){
VREF_SC = 0;
atomic::clearBitFlag(PMC_REGSC, PMC_REGSC_BGBE);
}
__attribute__((always_inline)) inline volatile bool isStable() {
return atomic::getBitFlag(VREF_SC, VREF_SC_VREFST);
}
__attribute__((always_inline)) inline volatile bool isOn() {
return atomic::getBitFlag(VREF_SC, VREF_SC_VREFEN);
}
inline void waitUntilStable() {
delay(35); // see note in isStable()
while(isOn() && !isStable()) {
yield();
}
}
}
#endif // ADC_USE_INTERNAL_VREF
#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