2016-11-03 18:50:59 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "common-hal/analogio/AnalogIn.h"
|
2017-01-19 14:26:41 -05:00
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-01-17 17:40:43 -05:00
|
|
|
#include "py/gc.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/mphal.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/analogio/AnalogIn.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
#include "asf/sam0/drivers/adc/adc.h"
|
2016-12-06 13:31:38 -05:00
|
|
|
#include "samd21_pins.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
|
2016-11-03 18:50:59 -04:00
|
|
|
const mcu_pin_obj_t *pin) {
|
|
|
|
if (!pin->has_adc) {
|
|
|
|
// No ADC function on that pin
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_ValueError("Pin does not have ADC capabilities");
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-04-12 18:24:50 -04:00
|
|
|
claim_pin(pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
self->pin = pin;
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
|
|
|
|
return self->pin == mp_const_none;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_analogio_analogin_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-06 13:31:38 -05:00
|
|
|
reset_pin(self->pin->pin);
|
2017-10-02 20:49:40 -04:00
|
|
|
self->pin = mp_const_none;
|
2016-12-06 13:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-01-19 14:26:41 -05:00
|
|
|
void analogin_reset() {
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
|
2017-09-16 13:06:23 -04:00
|
|
|
// Something else might have used the ADC in a different way,
|
2017-10-15 11:49:41 -04:00
|
|
|
// so we completely re-initialize it.
|
2017-01-17 17:40:43 -05:00
|
|
|
|
2017-10-15 11:49:41 -04:00
|
|
|
struct adc_config config_adc;
|
|
|
|
adc_get_config_defaults(&config_adc);
|
|
|
|
|
|
|
|
config_adc.reference = ADC_REFERENCE_INTVCC1;
|
|
|
|
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
|
|
|
|
config_adc.positive_input = self->pin->adc_input;
|
|
|
|
config_adc.resolution = ADC_RESOLUTION_16BIT;
|
|
|
|
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128;
|
|
|
|
|
|
|
|
struct adc_module adc_instance;
|
|
|
|
// ADC must have been disabled before adc_init() is called.
|
|
|
|
adc_init(&adc_instance, ADC, &config_adc);
|
|
|
|
adc_enable(&adc_instance);
|
2017-09-16 13:06:23 -04:00
|
|
|
|
|
|
|
// Read twice and discard first result, as recommended in section 14 of
|
|
|
|
// http://www.atmel.com/images/Atmel-42645-ADC-Configurations-with-Examples_ApplicationNote_AT11481.pdf
|
|
|
|
// "Discard the first conversion result whenever there is a change in ADC configuration
|
|
|
|
// like voltage reference / ADC channel change"
|
|
|
|
// Empirical observation shows the first reading is quite different than subsequent ones.
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
uint16_t data;
|
2017-09-16 13:06:23 -04:00
|
|
|
enum status_code status;
|
|
|
|
|
2017-10-15 11:49:41 -04:00
|
|
|
adc_start_conversion(&adc_instance);
|
2017-09-16 13:06:23 -04:00
|
|
|
do {
|
2017-10-15 11:49:41 -04:00
|
|
|
status = adc_read(&adc_instance, &data);
|
2017-09-16 13:06:23 -04:00
|
|
|
} while (status == STATUS_BUSY);
|
|
|
|
if (status == STATUS_ERR_OVERFLOW) {
|
2017-10-15 11:49:41 -04:00
|
|
|
mp_raise_RuntimeError("ADC result overwritten before reading");
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-09-16 13:06:23 -04:00
|
|
|
|
2017-10-15 11:49:41 -04:00
|
|
|
adc_start_conversion(&adc_instance);
|
2017-09-16 13:06:23 -04:00
|
|
|
do {
|
2017-10-15 11:49:41 -04:00
|
|
|
status = adc_read(&adc_instance, &data);
|
2017-09-16 13:06:23 -04:00
|
|
|
} while (status == STATUS_BUSY);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (status == STATUS_ERR_OVERFLOW) {
|
2017-10-15 11:49:41 -04:00
|
|
|
mp_raise_RuntimeError("ADC result overwritten before reading");
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-15 11:49:41 -04:00
|
|
|
adc_disable(&adc_instance);
|
2016-11-03 18:50:59 -04:00
|
|
|
return data;
|
|
|
|
}
|
2016-12-13 19:09:00 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
|
2016-12-13 19:09:00 -05:00
|
|
|
return 3.3f;
|
|
|
|
}
|