Skip to content

Commit 375676f

Browse files
authored
Merge pull request adafruit#3501 from hierophect/esp32-analogin
ESP32S2: Add AnalogIn
2 parents 699f19f + ccbc150 commit 375676f

File tree

17 files changed

+311
-65
lines changed

17 files changed

+311
-65
lines changed

locale/circuitpython.pot

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-09-29 20:14-0500\n"
11+
"POT-Creation-Date: 2020-10-06 13:26-0400\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -271,6 +271,10 @@ msgstr ""
271271
msgid "A hardware interrupt channel is already in use"
272272
msgstr ""
273273

274+
#: ports/esp32s2/common-hal/analogio/AnalogIn.c
275+
msgid "ADC2 is being used by WiFi"
276+
msgstr ""
277+
274278
#: shared-bindings/_bleio/Address.c shared-bindings/ipaddress/IPv4Address.c
275279
#, c-format
276280
msgid "Address must be %d bytes long"
@@ -1225,6 +1229,7 @@ msgid "No CCCD for this Characteristic"
12251229
msgstr ""
12261230

12271231
#: ports/atmel-samd/common-hal/analogio/AnalogOut.c
1232+
#: ports/esp32s2/common-hal/analogio/AnalogOut.c
12281233
#: ports/stm/common-hal/analogio/AnalogOut.c
12291234
msgid "No DAC on chip"
12301235
msgstr ""
@@ -1421,6 +1426,7 @@ msgstr ""
14211426

14221427
#: ports/atmel-samd/common-hal/analogio/AnalogIn.c
14231428
#: ports/cxd56/common-hal/analogio/AnalogIn.c
1429+
#: ports/esp32s2/common-hal/analogio/AnalogIn.c
14241430
#: ports/mimxrt10xx/common-hal/analogio/AnalogIn.c
14251431
#: ports/nrf/common-hal/analogio/AnalogIn.c
14261432
#: ports/stm/common-hal/analogio/AnalogIn.c
@@ -3144,13 +3150,15 @@ msgstr ""
31443150
msgid "pow() with 3 arguments requires integers"
31453151
msgstr ""
31463152

3153+
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
31473154
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
31483155
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
31493156
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
31503157
#: ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.h
31513158
#: ports/esp32s2/boards/microdev_micro_s2/mpconfigboard.h
31523159
#: ports/esp32s2/boards/muselab_nanoesp32_s2/mpconfigboard.h
31533160
#: ports/esp32s2/boards/unexpectedmaker_feathers2/mpconfigboard.h
3161+
#: ports/esp32s2/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.h
31543162
msgid "pressing boot button at start up.\n"
31553163
msgstr ""
31563164

ports/esp32s2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)
66

77
# The component list here determines what options we get in menuconfig and what the ninja file
88
# can build.
9-
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls esp_event esp_netif esp_wifi lwip wpa_supplicant freertos)
9+
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls esp_event esp_adc_cal esp_netif esp_wifi lwip wpa_supplicant freertos)
1010

1111
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
1212
project(circuitpython)

ports/esp32s2/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ INC += -isystem esp-idf/components/lwip/include/apps/sntp
9797
INC += -isystem esp-idf/components/hal/include
9898
INC += -isystem esp-idf/components/hal/esp32s2/include
9999
INC += -isystem esp-idf/components/log/include/
100+
INC += -isystem esp-idf/components/driver/esp32s2/include
100101
INC += -isystem esp-idf/components/soc/include
101102
INC += -isystem esp-idf/components/soc/src/esp32s2/include
102103
INC += -isystem esp-idf/components/soc/soc/include
@@ -270,12 +271,11 @@ menuconfig: $(BUILD)/esp-idf/config
270271
# qstr builds include headers so we need to make sure they are up to date
271272
$(HEADER_BUILD)/qstr.i.last: | $(BUILD)/esp-idf/config/sdkconfig.h
272273

273-
# Order here matters
274-
ESP_IDF_COMPONENTS_LINK = freertos log hal esp_system esp32s2 bootloader_support pthread esp_timer vfs spi_flash app_update esp_common esp32s2 heap newlib driver xtensa soc esp_ringbuf esp_wifi esp_event wpa_supplicant mbedtls efuse nvs_flash esp_netif lwip esp_rom esp-tls
274+
ESP_IDF_COMPONENTS_LINK = freertos log hal esp_system esp_adc_cal esp32s2 bootloader_support pthread esp_timer vfs spi_flash app_update esp_common esp32s2 heap newlib driver xtensa soc esp_ringbuf esp_wifi esp_event wpa_supplicant mbedtls efuse nvs_flash esp_netif lwip esp_rom esp-tls
275275

276276
ESP_IDF_COMPONENTS_INCLUDE = driver freertos log soc
277277

278-
INC += $(foreach component, $(ESP_IDF_COMPONENTS_INCLUDE), -Iesp-idf/components/$(component)/include)
278+
INC += $(foreach component, $(ESP_IDF_COMPONENTS_INCLUDE), -isystem esp-idf/components/$(component)/include)
279279

280280
ESP_IDF_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
281281
ESP_IDF_WIFI_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_WIFI_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
29+
30+
#include "common-hal/microcontroller/Pin.h"
31+
32+
#include "components/hal/include/hal/adc_types.h"
33+
#include "FreeRTOS.h"
34+
#include "freertos/semphr.h"
35+
#include "py/obj.h"
36+
37+
typedef struct {
38+
mp_obj_base_t base;
39+
const mcu_pin_obj_t * pin;
40+
} analogio_analogin_obj_t;
41+
42+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2019, Lucian Copeland for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <stdint.h>
29+
#include <string.h>
30+
31+
#include "py/mperrno.h"
32+
#include "py/runtime.h"
33+
34+
#include "shared-bindings/analogio/AnalogOut.h"
35+
#include "shared-bindings/microcontroller/Pin.h"
36+
#include "supervisor/shared/translate.h"
37+
38+
#include "common-hal/microcontroller/Pin.h"
39+
40+
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
41+
const mcu_pin_obj_t *pin) {
42+
mp_raise_NotImplementedError(translate("No DAC on chip"));
43+
}
44+
45+
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
46+
return true;
47+
}
48+
49+
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
50+
51+
}
52+
53+
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
54+
uint16_t value) {
55+
}
56+
57+
void analogout_reset(void) {
58+
59+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft
7+
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGOUT_H
29+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGOUT_H
30+
31+
#include "common-hal/microcontroller/Pin.h"
32+
33+
#include "py/obj.h"
34+
35+
typedef struct {
36+
mp_obj_base_t base;
37+
const mcu_pin_obj_t * pin;
38+
uint8_t channel;
39+
uint8_t dac_index:1;
40+
} analogio_analogout_obj_t;
41+
42+
void analogout_reset(void);
43+
44+
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No analogio module functions.

ports/esp32s2/common-hal/socketpool/Socket.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "common-hal/ssl/SSLContext.h"
3434

3535
#include "components/esp-tls/esp_tls.h"
36-
#include "components/log/include/esp_log.h"
3736

3837
typedef struct {
3938
mp_obj_base_t base;

ports/esp32s2/common-hal/wifi/ScannedNetworks.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "freertos/event_groups.h"
3737

3838
#include "components/esp_wifi/include/esp_wifi_types.h"
39-
#include "components/log/include/esp_log.h"
4039

4140
typedef struct {
4241
mp_obj_base_t base;

ports/esp32s2/mpconfigport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#define CIRCUITPY_INTERNAL_NVM_SIZE (0)
3232
#define MICROPY_NLR_THUMB (0)
3333

34-
#define MICROPY_PY_UJSON (1)
35-
#define MICROPY_USE_INTERNAL_PRINTF (0)
34+
#define MICROPY_PY_UJSON (1)
35+
#define MICROPY_USE_INTERNAL_PRINTF (0)
3636

3737
#include "py/circuitpy_mpconfig.h"
3838

ports/esp32s2/mpconfigport.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ LONGINT_IMPL = MPZ
1414

1515
# These modules are implemented in ports/<port>/common-hal:
1616
CIRCUITPY_FULL_BUILD = 1
17-
CIRCUITPY_ANALOGIO = 0
1817
CIRCUITPY_AUDIOBUSIO = 0
1918
CIRCUITPY_AUDIOIO = 0
2019
CIRCUITPY_COUNTIO = 0

0 commit comments

Comments
 (0)