Skip to content
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

ESP32S2: Add AnalogIn #3501

Merged
merged 12 commits into from Oct 9, 2020
Merged
10 changes: 9 additions & 1 deletion locale/circuitpython.pot
Expand Up @@ -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 <LL@li.org>\n"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ""

Expand Down
2 changes: 1 addition & 1 deletion ports/esp32s2/CMakeLists.txt
Expand Up @@ -6,7 +6,7 @@ set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)

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

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(circuitpython)
Expand Down
5 changes: 3 additions & 2 deletions ports/esp32s2/Makefile
Expand Up @@ -97,6 +97,7 @@ INC += -isystem esp-idf/components/lwip/include/apps/sntp
INC += -isystem esp-idf/components/hal/include
INC += -isystem esp-idf/components/hal/esp32s2/include
INC += -isystem esp-idf/components/log/include/
INC += -isystem esp-idf/components/driver/esp32s2/include
INC += -isystem esp-idf/components/soc/include
INC += -isystem esp-idf/components/soc/src/esp32s2/include
INC += -isystem esp-idf/components/soc/soc/include
Expand Down Expand Up @@ -271,11 +272,11 @@ menuconfig: $(BUILD)/esp-idf/config
$(HEADER_BUILD)/qstr.i.last: | $(BUILD)/esp-idf/config/sdkconfig.h

# Order here matters
ESP_IDF_COMPONENTS_LINK = freertos log 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
ESP_IDF_COMPONENTS_LINK = freertos log 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

ESP_IDF_COMPONENTS_INCLUDE = driver freertos log soc

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

ESP_IDF_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
ESP_IDF_WIFI_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_WIFI_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
Expand Down
98 changes: 98 additions & 0 deletions ports/esp32s2/common-hal/analogio/AnalogIn.c
@@ -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"));
tannewt marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
42 changes: 42 additions & 0 deletions ports/esp32s2/common-hal/analogio/AnalogIn.h
@@ -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/soc/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
59 changes: 59 additions & 0 deletions ports/esp32s2/common-hal/analogio/AnalogOut.c
@@ -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) {

}
44 changes: 44 additions & 0 deletions ports/esp32s2/common-hal/analogio/AnalogOut.h
@@ -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
1 change: 1 addition & 0 deletions ports/esp32s2/common-hal/analogio/__init__.c
@@ -0,0 +1 @@
// No analogio module functions.
1 change: 0 additions & 1 deletion ports/esp32s2/common-hal/socketpool/Socket.h
Expand Up @@ -33,7 +33,6 @@
#include "common-hal/ssl/SSLContext.h"

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

typedef struct {
mp_obj_base_t base;
Expand Down
1 change: 0 additions & 1 deletion ports/esp32s2/common-hal/wifi/ScannedNetworks.h
Expand Up @@ -36,7 +36,6 @@
#include "freertos/event_groups.h"

#include "components/esp_wifi/include/esp_wifi_types.h"
#include "components/log/include/esp_log.h"

typedef struct {
mp_obj_base_t base;
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32s2/mpconfigport.h
Expand Up @@ -31,8 +31,8 @@
#define CIRCUITPY_INTERNAL_NVM_SIZE (0)
#define MICROPY_NLR_THUMB (0)

#define MICROPY_PY_UJSON (1)
#define MICROPY_USE_INTERNAL_PRINTF (0)
#define MICROPY_PY_UJSON (1)
#define MICROPY_USE_INTERNAL_PRINTF (0)

#include "py/circuitpy_mpconfig.h"

Expand Down
1 change: 0 additions & 1 deletion ports/esp32s2/mpconfigport.mk
Expand Up @@ -14,7 +14,6 @@ LONGINT_IMPL = MPZ

# These modules are implemented in ports/<port>/common-hal:
CIRCUITPY_FULL_BUILD = 1
CIRCUITPY_ANALOGIO = 0
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_COUNTIO = 0
Expand Down