forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
ESP32S2: Add AnalogIn #3501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
ESP32S2: Add AnalogIn #3501
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
128b4a0
Add non-calibrated ADC
hierophect c624c1e
Merge branch 'esp32-update-idf' into esp32-analogin
hierophect 0bbd067
Revert ADC calibration removal, complete merge
hierophect a8dfae5
Add ADC calibration module support
hierophect 0467a82
Fix/remove various style issues
hierophect e93a274
Merge branch 'main' into esp32-analogin
hierophect f90eb23
Merge branch 'esp32-update-idf' into esp32-analogin
hierophect 68cb384
Remove logging from shared-bindings, fix translations, revert config …
hierophect 455cf65
Merge remote-tracking branch 'upstream/main' into esp32-analogin
hierophect 7386a94
Merge remote-tracking branch 'upstream/main' into esp32-analogin
hierophect 97d217a
Merge remote-tracking branch 'upstream/main' into esp32-analogin
hierophect ccbc150
Fix submodule issue
hierophect File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ msgid "" | |
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2020-09-29 20:14-0500\n" | ||
"POT-Creation-Date: 2020-10-06 13:26-0400\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
|
@@ -271,6 +271,10 @@ msgstr "" | |
msgid "A hardware interrupt channel is already in use" | ||
msgstr "" | ||
|
||
#: ports/esp32s2/common-hal/analogio/AnalogIn.c | ||
msgid "ADC2 is being used by WiFi" | ||
msgstr "" | ||
|
||
#: shared-bindings/_bleio/Address.c shared-bindings/ipaddress/IPv4Address.c | ||
#, c-format | ||
msgid "Address must be %d bytes long" | ||
|
@@ -1225,6 +1229,7 @@ msgid "No CCCD for this Characteristic" | |
msgstr "" | ||
|
||
#: ports/atmel-samd/common-hal/analogio/AnalogOut.c | ||
#: ports/esp32s2/common-hal/analogio/AnalogOut.c | ||
#: ports/stm/common-hal/analogio/AnalogOut.c | ||
msgid "No DAC on chip" | ||
msgstr "" | ||
|
@@ -1421,6 +1426,7 @@ msgstr "" | |
|
||
#: ports/atmel-samd/common-hal/analogio/AnalogIn.c | ||
#: ports/cxd56/common-hal/analogio/AnalogIn.c | ||
#: ports/esp32s2/common-hal/analogio/AnalogIn.c | ||
#: ports/mimxrt10xx/common-hal/analogio/AnalogIn.c | ||
#: ports/nrf/common-hal/analogio/AnalogIn.c | ||
#: ports/stm/common-hal/analogio/AnalogIn.c | ||
|
@@ -3144,13 +3150,15 @@ msgstr "" | |
msgid "pow() with 3 arguments requires integers" | ||
msgstr "" | ||
|
||
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h | ||
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h | ||
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h | ||
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h | ||
#: ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.h | ||
#: ports/esp32s2/boards/microdev_micro_s2/mpconfigboard.h | ||
#: ports/esp32s2/boards/muselab_nanoesp32_s2/mpconfigboard.h | ||
#: ports/esp32s2/boards/unexpectedmaker_feathers2/mpconfigboard.h | ||
#: ports/esp32s2/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.h | ||
msgid "pressing boot button at start up.\n" | ||
msgstr "" | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Lucian Copeland 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. | ||
*/ | ||
|
||
#include "common-hal/analogio/AnalogIn.h" | ||
#include "py/mperrno.h" | ||
#include "py/runtime.h" | ||
#include "supervisor/shared/translate.h" | ||
|
||
#include "components/driver/include/driver/adc_common.h" | ||
#include "components/esp_adc_cal/include/esp_adc_cal.h" | ||
|
||
#include "shared-bindings/microcontroller/Pin.h" | ||
|
||
#define DEFAULT_VREF 1100 | ||
#define NO_OF_SAMPLES 64 | ||
#define ATTENUATION ADC_ATTEN_DB_11 | ||
#define DATA_WIDTH ADC_WIDTH_BIT_13 | ||
|
||
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, | ||
const mcu_pin_obj_t *pin) { | ||
if (pin->adc_index == 0 || pin->adc_channel == ADC_CHANNEL_MAX) { | ||
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); | ||
} | ||
common_hal_mcu_pin_claim(pin); | ||
self->pin = pin; | ||
} | ||
|
||
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { | ||
return self->pin == NULL; | ||
} | ||
|
||
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { | ||
if (common_hal_analogio_analogin_deinited(self)) { | ||
return; | ||
} | ||
reset_pin_number(self->pin->number); | ||
self->pin = NULL; | ||
} | ||
|
||
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { | ||
if (self->pin->adc_index == ADC_UNIT_1) { | ||
adc1_config_width(DATA_WIDTH); | ||
adc1_config_channel_atten((adc1_channel_t)self->pin->adc_channel, ATTENUATION); | ||
} else if (self->pin->adc_index == ADC_UNIT_2) { | ||
adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION); | ||
} | ||
|
||
// Automatically select calibration process depending on status of efuse | ||
esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); | ||
esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, adc_chars); | ||
|
||
uint32_t adc_reading = 0; | ||
//Multisampling | ||
for (int i = 0; i < NO_OF_SAMPLES; i++) { | ||
if (self->pin->adc_index == ADC_UNIT_1) { | ||
adc_reading += adc1_get_raw((adc1_channel_t)self->pin->adc_channel); | ||
} else { | ||
int raw; | ||
esp_err_t r = adc2_get_raw((adc2_channel_t)self->pin->adc_channel, DATA_WIDTH, &raw); | ||
if ( r != ESP_OK ) { | ||
mp_raise_ValueError(translate("ADC2 is being used by WiFi")); | ||
} | ||
adc_reading += raw; | ||
} | ||
} | ||
adc_reading /= NO_OF_SAMPLES; | ||
|
||
// This corrects non-linear regions of the ADC range with a LUT, so it's a better reading than raw | ||
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); | ||
return voltage * ((1 << 16) - 1)/3300; | ||
} | ||
|
||
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { | ||
return 3.3f; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Lucian Copeland 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. | ||
*/ | ||
|
||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H | ||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H | ||
|
||
#include "common-hal/microcontroller/Pin.h" | ||
|
||
#include "components/hal/include/hal/adc_types.h" | ||
#include "FreeRTOS.h" | ||
#include "freertos/semphr.h" | ||
#include "py/obj.h" | ||
|
||
typedef struct { | ||
mp_obj_base_t base; | ||
const mcu_pin_obj_t * pin; | ||
} analogio_analogin_obj_t; | ||
|
||
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George | ||
* Copyright (c) 2019, Lucian Copeland 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. | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
#include "py/mperrno.h" | ||
#include "py/runtime.h" | ||
|
||
#include "shared-bindings/analogio/AnalogOut.h" | ||
#include "shared-bindings/microcontroller/Pin.h" | ||
#include "supervisor/shared/translate.h" | ||
|
||
#include "common-hal/microcontroller/Pin.h" | ||
|
||
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, | ||
const mcu_pin_obj_t *pin) { | ||
mp_raise_NotImplementedError(translate("No DAC on chip")); | ||
tannewt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { | ||
return true; | ||
} | ||
|
||
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { | ||
|
||
} | ||
|
||
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, | ||
uint16_t value) { | ||
} | ||
|
||
void analogout_reset(void) { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Scott Shawcroft | ||
* Copyright (c) 2019 Lucian Copeland 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. | ||
*/ | ||
|
||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGOUT_H | ||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGOUT_H | ||
|
||
#include "common-hal/microcontroller/Pin.h" | ||
|
||
#include "py/obj.h" | ||
|
||
typedef struct { | ||
mp_obj_base_t base; | ||
const mcu_pin_obj_t * pin; | ||
uint8_t channel; | ||
uint8_t dac_index:1; | ||
} analogio_analogout_obj_t; | ||
|
||
void analogout_reset(void); | ||
|
||
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// No analogio module functions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.