mirror of
https://github.com/joeycastillo/second-movement.git
synced 2026-02-04 19:15:27 +00:00
watch_adc: enable ADC when checking battery; let's get rid of this footgun once and for all
This commit is contained in:
@ -67,6 +67,11 @@ void _watch_set_analog_reference_voltage(uint8_t reference) {
|
||||
uint16_t watch_get_vcc_voltage(void) {
|
||||
// stash the previous reference so we can restore it when we're done.
|
||||
uint8_t oldref = ADC->REFCTRL.bit.REFSEL;
|
||||
// same with the previous state of the ADC
|
||||
bool adc_was_disabled = !adc_is_enabled();
|
||||
|
||||
// enable the ADC if needed
|
||||
if (adc_was_disabled) watch_enable_adc();
|
||||
|
||||
// if we weren't already using the internal reference voltage, select it now.
|
||||
if (oldref != ADC_REFCTRL_REFSEL_INTREF_Val) _watch_set_analog_reference_voltage(ADC_REFCTRL_REFSEL_INTREF_Val);
|
||||
@ -77,6 +82,9 @@ uint16_t watch_get_vcc_voltage(void) {
|
||||
// restore the old reference, if needed.
|
||||
if (oldref != ADC_REFCTRL_REFSEL_INTREF_Val) _watch_set_analog_reference_voltage(oldref);
|
||||
|
||||
// and restore the ADC to its previous state
|
||||
if (adc_was_disabled) watch_disable_adc();
|
||||
|
||||
return (uint16_t)((raw_val * 1000) / (1024 * 1 << ADC->AVGCTRL.bit.SAMPLENUM));
|
||||
}
|
||||
|
||||
|
||||
@ -51,9 +51,7 @@ void _watch_init(void) {
|
||||
while(!SUPC->STATUS.bit.VREGRDY); // wait for voltage regulator to become ready
|
||||
|
||||
// check the battery voltage...
|
||||
watch_enable_adc();
|
||||
uint16_t battery_voltage = watch_get_vcc_voltage();
|
||||
watch_disable_adc();
|
||||
// ...because we can enable the more efficient low power regulator if the system voltage is > 2.5V
|
||||
// still, enable LPEFF only if the battery voltage is comfortably above this threshold.
|
||||
if (battery_voltage >= 2700) {
|
||||
|
||||
@ -53,7 +53,8 @@ void watch_enable_analog_input(const uint16_t pin);
|
||||
uint16_t watch_get_analog_pin_level(const uint16_t pin);
|
||||
|
||||
/** @brief Returns the voltage of the VCC supply in millivolts (i.e. 3000 mV == 3.0 V). If running on
|
||||
* a coin cell, this will be the battery voltage.
|
||||
* a coin cell, this will be the battery voltage. If the ADC is not running when this function
|
||||
* is called, it enabled the ADC briefly, and returns it to the off state.
|
||||
* @details Unlike other ADC functions, this function does not return a raw value from the ADC, but
|
||||
* rather scales it to an actual number of millivolts. This is because the ADC doesn't let
|
||||
* us measure VCC per se; it instead lets us measure VCC / 4, and we choose to measure it
|
||||
|
||||
Reference in New Issue
Block a user