diff options
author | Alexsander Akers <me@a2.io> | 2022-01-25 15:03:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-25 15:03:22 -0500 |
commit | b8de35658ffd78ad8b22f91ccbbd3d63663afda9 (patch) | |
tree | 1f265ddfcc8e5abf0316b81b15f80bf5c70fa7b7 /watch-library/hardware/watch/watch_adc.c | |
parent | 9e24f6c336773c7404139ab4db0eaab2f99504e2 (diff) | |
download | Sensor-Watch-b8de35658ffd78ad8b22f91ccbbd3d63663afda9.tar.gz Sensor-Watch-b8de35658ffd78ad8b22f91ccbbd3d63663afda9.tar.bz2 Sensor-Watch-b8de35658ffd78ad8b22f91ccbbd3d63663afda9.zip |
Sensor Watch Simulator (#35)
* Put something on screen
* Use the 32bit watch_date_time repr to pass from JS
* Implement periodic callbacks
* Clear display on enabling
* Hook up watch_set_led_color() to SVG (green-only)
* Make debug output full-width
* Remove default Emscripten canvas
* Implement sleep and button clicks
* Fix time zone conversion bug in beats-time app
* Clean up warnings
* Fix pin levels
* Set time zone to browser value (if available)
* Add basic backup data saving
* Silence format specifier warnings in both targets
* Remove unnecessary, copied files
* Use RTC pointer to clear callbacks (if available)
* Use preprocessor define to avoid hardcoding MOVEMENT_NUM_FACES
* Change each face to const preprocessor definition
* Remove Intl.DateTimeFormat usage
* Update shell.html title, header
* Add touch start/end event handlers on SVG buttons
* Update shell.html
* Update folder structure (shared, simulator, hardware under watch-library)
* Tease out shared components from watch_slcd
* Clean up simulator watch_slcd.c inline JS calls
* Fix missing newlines at end of file
* Add simulator warnings (except format, unused-paremter)
* Implement remaining watch_rtc functions
* Fix button bug on mouse down then drag out
* Implement remaining watch_slcd functions
* Link keyboard events to buttons (for keys A, L, M)
* Rewrite event handling (mouse, touch, keyboard) in C
* Set explicit text UTF-8 charset in shell.html
* Address PR comments
* Remove unused directories from include paths
Diffstat (limited to 'watch-library/hardware/watch/watch_adc.c')
-rw-r--r-- | watch-library/hardware/watch/watch_adc.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/watch-library/hardware/watch/watch_adc.c b/watch-library/hardware/watch/watch_adc.c new file mode 100644 index 00000000..5ba7abdf --- /dev/null +++ b/watch-library/hardware/watch/watch_adc.c @@ -0,0 +1,179 @@ +/* + * MIT License + * + * Copyright (c) 2020 Joey Castillo + * + * 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. + */ + +#include "watch_adc.h" + +static void _watch_sync_adc(void) { + while (ADC->SYNCBUSY.reg); +} + +static uint16_t _watch_get_analog_value(uint16_t channel) { + if (ADC->INPUTCTRL.bit.MUXPOS != channel) { + ADC->INPUTCTRL.bit.MUXPOS = channel; + _watch_sync_adc(); + } + + ADC->SWTRIG.bit.START = 1; + while (!ADC->INTFLAG.bit.RESRDY); + + return ADC->RESULT.reg; +} + +void watch_enable_adc(void) { + MCLK->APBCMASK.reg |= MCLK_APBCMASK_ADC; + GCLK->PCHCTRL[ADC_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK0 | GCLK_PCHCTRL_CHEN; + + uint16_t calib_reg = 0; + calib_reg = ADC_CALIB_BIASREFBUF((*(uint32_t *)ADC_FUSES_BIASREFBUF_ADDR >> ADC_FUSES_BIASREFBUF_Pos)) | + ADC_CALIB_BIASCOMP((*(uint32_t *)ADC_FUSES_BIASCOMP_ADDR >> ADC_FUSES_BIASCOMP_Pos)); + + if (!ADC->SYNCBUSY.bit.SWRST) { + if (ADC->CTRLA.bit.ENABLE) { + ADC->CTRLA.bit.ENABLE = 0; + _watch_sync_adc(); + } + ADC->CTRLA.bit.SWRST = 1; + } + _watch_sync_adc(); + + if (USB->DEVICE.CTRLA.bit.ENABLE) { + // if USB is enabled, we are running an 8 MHz clock. + // divide by 16 for a 500kHz ADC clock. + ADC->CTRLB.bit.PRESCALER = ADC_CTRLB_PRESCALER_DIV16_Val; + } else { + // otherwise it's 4 Mhz. divide by 8 for a 500kHz ADC clock. + ADC->CTRLB.bit.PRESCALER = ADC_CTRLB_PRESCALER_DIV8_Val; + } + ADC->CALIB.reg = calib_reg; + ADC->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_INTVCC2_Val; + ADC->INPUTCTRL.bit.MUXNEG = ADC_INPUTCTRL_MUXNEG_GND_Val; + ADC->CTRLC.bit.RESSEL = ADC_CTRLC_RESSEL_16BIT_Val; + ADC->AVGCTRL.bit.SAMPLENUM = ADC_AVGCTRL_SAMPLENUM_16_Val; + ADC->SAMPCTRL.bit.SAMPLEN = 0; + ADC->INTENSET.reg = ADC_INTENSET_RESRDY; + ADC->CTRLA.bit.ENABLE = 1; + _watch_sync_adc(); + // throw away one measurement after reference change (the channel doesn't matter). + _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_SCALEDCOREVCC); +} + +void watch_enable_analog_input(const uint8_t pin) { + gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF); + switch (pin) { + case A0: + gpio_set_pin_function(pin, PINMUX_PB04B_ADC_AIN12); + break; + case A1: + gpio_set_pin_function(pin, PINMUX_PB01B_ADC_AIN9); + break; + case A2: + gpio_set_pin_function(pin, PINMUX_PB02B_ADC_AIN10); + break; + case A3: + gpio_set_pin_function(pin, PINMUX_PB03B_ADC_AIN11); + break; + case A4: + gpio_set_pin_function(pin, PINMUX_PB00B_ADC_AIN8); + break; + default: + return; + } +} + +uint16_t watch_get_analog_pin_level(const uint8_t pin) { + switch (pin) { + case A0: + return _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_AIN12_Val); + case A1: + return _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_AIN9_Val); + case A2: + return _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_AIN10_Val); + case A3: + return _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_AIN11_Val); + case A4: + return _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_AIN8_Val); + default: + return 0; + } +} + +void watch_set_analog_num_samples(uint16_t samples) { + // ignore any input that's not a power of 2 (i.e. only one bit set) + if (__builtin_popcount(samples) != 1) return; + // if only one bit is set, counting the trailing zeroes is equivalent to log2(samples) + uint8_t sample_val = __builtin_ctz(samples); + // make sure the desired value is within range and set it, if so. + if (sample_val <= ADC_AVGCTRL_SAMPLENUM_1024_Val) { + ADC->AVGCTRL.bit.SAMPLENUM = sample_val; + _watch_sync_adc(); + } +} + +void watch_set_analog_sampling_length(uint8_t cycles) { + // for clarity the API asks the user how many cycles they want the measurement to take. + // but the ADC always needs at least one cycle; it just wants to know how many *extra* cycles we want. + // so we subtract one from the user-provided value, and clamp to the maximum. + ADC->SAMPCTRL.bit.SAMPLEN = (cycles - 1) & 0x3F; + _watch_sync_adc(); +} + +void watch_set_analog_reference_voltage(watch_adc_reference_voltage reference) { + ADC->CTRLA.bit.ENABLE = 0; + + if (reference == ADC_REFERENCE_INTREF) SUPC->VREF.bit.VREFOE = 1; + else SUPC->VREF.bit.VREFOE = 0; + + ADC->REFCTRL.bit.REFSEL = reference; + ADC->CTRLA.bit.ENABLE = 1; + _watch_sync_adc(); + // throw away one measurement after reference change (the channel doesn't matter). + _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_SCALEDCOREVCC); +} + +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; + + // if we weren't already using the internal reference voltage, select it now. + if (oldref != ADC_REFERENCE_INTREF) watch_set_analog_reference_voltage(ADC_REFERENCE_INTREF); + + // get the data + uint32_t raw_val = _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_SCALEDIOVCC_Val); + + // restore the old reference, if needed. + if (oldref != ADC_REFERENCE_INTREF) watch_set_analog_reference_voltage(oldref); + + return (uint16_t)((raw_val * 1000) / (1024 * 1 << ADC->AVGCTRL.bit.SAMPLENUM)); +} + +inline void watch_disable_analog_input(const uint8_t pin) { + gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); +} + +inline void watch_disable_adc(void) { + ADC->CTRLA.bit.ENABLE = 0; + _watch_sync_adc(); + + MCLK->APBCMASK.reg &= ~MCLK_APBCMASK_ADC; +} |