Skip to content

Commit 128b4a0

Browse files
committed
Add non-calibrated ADC
1 parent dd86cb0 commit 128b4a0

File tree

10 files changed

+342
-78
lines changed

10 files changed

+342
-78
lines changed

locale/circuitpython.pot

Lines changed: 6 additions & 6 deletions
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-21 16:45-0500\n"
11+
"POT-Creation-Date: 2020-09-25 18:14-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"
@@ -1167,14 +1167,10 @@ msgstr ""
11671167
msgid "Must be a %q subclass."
11681168
msgstr ""
11691169

1170-
#: ports/mimxrt10xx/common-hal/busio/SPI.c ports/stm/common-hal/busio/SPI.c
1170+
#: ports/mimxrt10xx/common-hal/busio/SPI.c shared-bindings/busio/SPI.c
11711171
msgid "Must provide MISO or MOSI pin"
11721172
msgstr ""
11731173

1174-
#: ports/stm/common-hal/busio/SPI.c
1175-
msgid "Must provide SCK pin"
1176-
msgstr ""
1177-
11781174
#: shared-bindings/rgbmatrix/RGBMatrix.c
11791175
#, c-format
11801176
msgid "Must use a multiple of 6 rgb pins, not %d"
@@ -1415,6 +1411,10 @@ msgstr ""
14151411
msgid "Pull not used when direction is output."
14161412
msgstr ""
14171413

1414+
#: ports/stm/ref/pulseout-pre-timeralloc.c
1415+
msgid "PulseOut not supported on this chip"
1416+
msgstr ""
1417+
14181418
#: ports/stm/common-hal/os/__init__.c
14191419
msgid "RNG DeInit Error"
14201420
msgstr ""

ports/esp32s2/Makefile

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,31 @@ INC += -I./peripherals
7373
INC += -I../../lib/mp-readline
7474
INC += -I../../lib/tinyusb/src
7575
INC += -I../../supervisor/shared/usb
76-
INC += -Iesp-idf/components/freertos/include/freertos
77-
INC += -Iesp-idf/components/freertos/xtensa/include
78-
INC += -Iesp-idf/components/esp32s2/include
79-
INC += -Iesp-idf/components/xtensa/esp32s2/include
80-
INC += -Iesp-idf/components/esp_common/include
81-
INC += -Iesp-idf/components/esp_event/include
82-
INC += -Iesp-idf/components/esp_netif/include
83-
INC += -Iesp-idf/components/esp_ringbuf/include
84-
INC += -Iesp-idf/components/esp_rom/include
85-
INC += -Iesp-idf/components/esp_wifi/include
86-
INC += -Iesp-idf/components/xtensa/include
87-
INC += -Iesp-idf/components/esp_timer/include
88-
INC += -Iesp-idf/components/mbedtls/mbedtls/include
89-
INC += -Iesp-idf/components/mbedtls/port/include/
90-
INC += -Iesp-idf/components/newlib/platform_include
91-
INC += -Iesp-idf/components/lwip/lwip/src/include
92-
INC += -Iesp-idf/components/lwip/port/esp32/include
93-
INC += -Iesp-idf/components/lwip/include/apps/sntp
94-
INC += -Iesp-idf/components/soc/include
95-
INC += -Iesp-idf/components/soc/src/esp32s2/include
96-
INC += -Iesp-idf/components/soc/soc/include
97-
INC += -Iesp-idf/components/soc/soc/esp32s2/include
98-
INC += -Iesp-idf/components/heap/include
99-
INC += -Iesp-idf/components/esp_system/include
76+
INC += -isystem esp-idf/components/freertos/include/freertos
77+
INC += -isystem esp-idf/components/freertos/xtensa/include
78+
INC += -isystem esp-idf/components/esp32s2/include
79+
INC += -isystem esp-idf/components/driver/esp32s2/include
80+
INC += -isystem esp-idf/components/xtensa/esp32s2/include
81+
INC += -isystem esp-idf/components/esp_common/include
82+
INC += -isystem esp-idf/components/esp_event/include
83+
INC += -isystem esp-idf/components/esp_netif/include
84+
INC += -isystem esp-idf/components/esp_ringbuf/include
85+
INC += -isystem esp-idf/components/esp_rom/include
86+
INC += -isystem esp-idf/components/esp_wifi/include
87+
INC += -isystem esp-idf/components/xtensa/include
88+
INC += -isystem esp-idf/components/esp_timer/include
89+
INC += -isystem esp-idf/components/mbedtls/mbedtls/include
90+
INC += -isystem esp-idf/components/mbedtls/port/include/
91+
INC += -isystem esp-idf/components/newlib/platform_include
92+
INC += -isystem esp-idf/components/lwip/lwip/src/include
93+
INC += -isystem esp-idf/components/lwip/port/esp32/include
94+
INC += -isystem esp-idf/components/lwip/include/apps/sntp
95+
INC += -isystem esp-idf/components/soc/include
96+
INC += -isystem esp-idf/components/soc/src/esp32s2/include
97+
INC += -isystem esp-idf/components/soc/soc/include
98+
INC += -isystem esp-idf/components/soc/soc/esp32s2/include
99+
INC += -isystem esp-idf/components/heap/include
100+
INC += -isystem esp-idf/components/esp_system/include
100101
INC += -I$(BUILD)/esp-idf/config
101102

102103
CFLAGS += -DHAVE_CONFIG_H \
@@ -260,7 +261,7 @@ ESP_IDF_COMPONENTS_LINK = freertos log esp_system esp32s2 bootloader_support pth
260261

261262
ESP_IDF_COMPONENTS_INCLUDE = driver freertos log soc
262263

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

265266
ESP_IDF_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
266267
ESP_IDF_WIFI_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_WIFI_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 "driver/adc.h"
33+
// TODO: Add when ESP-IDF is updated latest version
34+
// #include "esp-idf/components/esp_adc_cal/include/esp_adc_cal.h"
35+
36+
#include "shared-bindings/microcontroller/Pin.h"
37+
38+
#define DEFAULT_VREF 1100
39+
#define NO_OF_SAMPLES 64
40+
#define ATTENUATION ADC_ATTEN_DB_11
41+
#define DATA_WIDTH ADC_WIDTH_BIT_13
42+
43+
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
44+
const mcu_pin_obj_t *pin) {
45+
if (pin->adc_index == 0 || pin->adc_channel == ADC_CHANNEL_MAX) {
46+
mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
47+
}
48+
common_hal_mcu_pin_claim(pin);
49+
self->pin = pin;
50+
}
51+
52+
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
53+
return self->pin == NULL;
54+
}
55+
56+
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
57+
if (common_hal_analogio_analogin_deinited(self)) {
58+
return;
59+
}
60+
reset_pin_number(self->pin->number);
61+
self->pin = NULL;
62+
}
63+
64+
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
65+
if (self->pin->adc_index == ADC_UNIT_1) {
66+
adc1_config_width(DATA_WIDTH);
67+
adc1_config_channel_atten((adc1_channel_t)self->pin->adc_channel, ATTENUATION);
68+
} else if (self->pin->adc_index == ADC_UNIT_2) {
69+
adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION);
70+
}
71+
72+
// TODO: esp_adc_cal is only available in the latest version of the esp-idf. Enable when we update.
73+
// Automatically select calibration process depending on status of efuse
74+
// esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
75+
// esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, adc_chars);
76+
77+
uint32_t adc_reading = 0;
78+
//Multisampling
79+
for (int i = 0; i < NO_OF_SAMPLES; i++) {
80+
if (self->pin->adc_index == ADC_UNIT_1) {
81+
adc_reading += adc1_get_raw((adc1_channel_t)self->pin->adc_channel);
82+
} else {
83+
int raw;
84+
esp_err_t r = adc2_get_raw((adc2_channel_t)self->pin->adc_channel, DATA_WIDTH, &raw);
85+
if ( r != ESP_OK ) {
86+
mp_raise_ValueError(translate("ADC2 is being used by WiFi"));
87+
}
88+
adc_reading += raw;
89+
}
90+
}
91+
adc_reading /= NO_OF_SAMPLES;
92+
93+
// This corrects non-linear regions of the ADC range with a LUT, so it's a better reading than raw
94+
// Enable when ESP-IDF is updated
95+
//uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
96+
//return voltage * ((1 << 16) - 1)/3300;
97+
98+
return adc_reading;
99+
}
100+
101+
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
102+
return 3.3f;
103+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 "esp-idf/components/soc/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+
static inline uint8_t stm32_adc_units(uint8_t adc_packed) {
43+
return adc_packed >> 5;
44+
}
45+
46+
static inline uint8_t stm32_adc_channel(uint8_t adc_packed) {
47+
return adc_packed & 0x1f;
48+
}
49+
50+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_ValueError(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+
58+
void analogout_reset(void) {
59+
60+
}
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_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
29+
#define MICROPY_INCLUDED_STM32F4_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/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ USB_SERIAL_NUMBER_LENGTH = 12
1313
LONGINT_IMPL = MPZ
1414

1515
# These modules are implemented in ports/<port>/common-hal:
16+
# CIRCUITPY_ANALOGIO = 0
1617
CIRCUITPY_FULL_BUILD = 1
17-
CIRCUITPY_ANALOGIO = 0
1818
CIRCUITPY_AUDIOBUSIO = 0
1919
CIRCUITPY_AUDIOIO = 0
2020
CIRCUITPY_COUNTIO = 0

0 commit comments

Comments
 (0)