|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Lucian Copeland for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "common-hal/analogio/AnalogIn.h" |
| 28 | +#include "py/mperrno.h" |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "supervisor/shared/translate.h" |
| 31 | + |
| 32 | +#include "components/driver/include/driver/adc_common.h" |
| 33 | +#include "components/esp_adc_cal/include/esp_adc_cal.h" |
| 34 | + |
| 35 | +#include "shared-bindings/microcontroller/Pin.h" |
| 36 | + |
| 37 | +#define DEFAULT_VREF 1100 |
| 38 | +#define NO_OF_SAMPLES 64 |
| 39 | +#define ATTENUATION ADC_ATTEN_DB_11 |
| 40 | +#define DATA_WIDTH ADC_WIDTH_BIT_13 |
| 41 | + |
| 42 | +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, |
| 43 | + const mcu_pin_obj_t *pin) { |
| 44 | + if (pin->adc_index == 0 || pin->adc_channel == ADC_CHANNEL_MAX) { |
| 45 | + mp_raise_ValueError(translate("Pin does not have ADC capabilities")); |
| 46 | + } |
| 47 | + common_hal_mcu_pin_claim(pin); |
| 48 | + self->pin = pin; |
| 49 | +} |
| 50 | + |
| 51 | +bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { |
| 52 | + return self->pin == NULL; |
| 53 | +} |
| 54 | + |
| 55 | +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { |
| 56 | + if (common_hal_analogio_analogin_deinited(self)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + reset_pin_number(self->pin->number); |
| 60 | + self->pin = NULL; |
| 61 | +} |
| 62 | + |
| 63 | +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { |
| 64 | + if (self->pin->adc_index == ADC_UNIT_1) { |
| 65 | + adc1_config_width(DATA_WIDTH); |
| 66 | + adc1_config_channel_atten((adc1_channel_t)self->pin->adc_channel, ATTENUATION); |
| 67 | + } else if (self->pin->adc_index == ADC_UNIT_2) { |
| 68 | + adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION); |
| 69 | + } |
| 70 | + |
| 71 | + // Automatically select calibration process depending on status of efuse |
| 72 | + esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); |
| 73 | + esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, adc_chars); |
| 74 | + |
| 75 | + uint32_t adc_reading = 0; |
| 76 | + //Multisampling |
| 77 | + for (int i = 0; i < NO_OF_SAMPLES; i++) { |
| 78 | + if (self->pin->adc_index == ADC_UNIT_1) { |
| 79 | + adc_reading += adc1_get_raw((adc1_channel_t)self->pin->adc_channel); |
| 80 | + } else { |
| 81 | + int raw; |
| 82 | + esp_err_t r = adc2_get_raw((adc2_channel_t)self->pin->adc_channel, DATA_WIDTH, &raw); |
| 83 | + if ( r != ESP_OK ) { |
| 84 | + mp_raise_ValueError(translate("ADC2 is being used by WiFi")); |
| 85 | + } |
| 86 | + adc_reading += raw; |
| 87 | + } |
| 88 | + } |
| 89 | + adc_reading /= NO_OF_SAMPLES; |
| 90 | + |
| 91 | + // This corrects non-linear regions of the ADC range with a LUT, so it's a better reading than raw |
| 92 | + uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); |
| 93 | + return voltage * ((1 << 16) - 1)/3300; |
| 94 | +} |
| 95 | + |
| 96 | +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { |
| 97 | + return 3.3f; |
| 98 | +} |
0 commit comments