Skip to content

Commit 34d574a

Browse files
unused float temperature patch
1 parent b7fce80 commit 34d574a

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed

patches/modesp32.c

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
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 <stdio.h>
28+
#include <string.h>
29+
30+
#include <time.h>
31+
#include <sys/time.h>
32+
#include "driver/gpio.h"
33+
#include "driver/adc.h"
34+
#include "esp_heap_caps.h"
35+
#include "multi_heap.h"
36+
37+
#include "py/nlr.h"
38+
#include "py/obj.h"
39+
#include "py/runtime.h"
40+
#include "py/mphal.h"
41+
#include "shared/timeutils/timeutils.h"
42+
#include "modmachine.h"
43+
#include "machine_rtc.h"
44+
#include "modesp32.h"
45+
46+
// These private includes are needed for idf_heap_info.
47+
#define MULTI_HEAP_FREERTOS
48+
#include "../multi_heap_platform.h"
49+
#include "../heap_private.h"
50+
51+
static mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) {
52+
53+
if (machine_rtc_config.ext0_pin != -1) {
54+
mp_raise_ValueError(MP_ERROR_TEXT("no resources"));
55+
}
56+
// mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("touchpad wakeup not available for this version of ESP-IDF"));
57+
58+
machine_rtc_config.wake_on_touch = mp_obj_is_true(wake);
59+
return mp_const_none;
60+
}
61+
static MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_touch_obj, esp32_wake_on_touch);
62+
63+
static mp_obj_t esp32_wake_on_ext0(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
64+
65+
if (machine_rtc_config.wake_on_touch) {
66+
mp_raise_ValueError(MP_ERROR_TEXT("no resources"));
67+
}
68+
enum {ARG_pin, ARG_level};
69+
const mp_arg_t allowed_args[] = {
70+
{ MP_QSTR_pin, MP_ARG_OBJ, {.u_obj = mp_obj_new_int(machine_rtc_config.ext0_pin)} },
71+
{ MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext0_level} },
72+
};
73+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
74+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
75+
76+
if (args[ARG_pin].u_obj == mp_const_none) {
77+
machine_rtc_config.ext0_pin = -1; // "None"
78+
} else {
79+
gpio_num_t pin_id = machine_pin_get_id(args[ARG_pin].u_obj);
80+
if (pin_id != machine_rtc_config.ext0_pin) {
81+
if (!RTC_IS_VALID_EXT_PIN(pin_id)) {
82+
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
83+
}
84+
machine_rtc_config.ext0_pin = pin_id;
85+
}
86+
}
87+
88+
machine_rtc_config.ext0_level = args[ARG_level].u_bool;
89+
machine_rtc_config.ext0_wake_types = MACHINE_WAKE_SLEEP | MACHINE_WAKE_DEEPSLEEP;
90+
91+
return mp_const_none;
92+
}
93+
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext0_obj, 0, esp32_wake_on_ext0);
94+
95+
static mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
96+
enum {ARG_pins, ARG_level};
97+
const mp_arg_t allowed_args[] = {
98+
{ MP_QSTR_pins, MP_ARG_OBJ, {.u_obj = mp_const_none} },
99+
{ MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.ext1_level} },
100+
};
101+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
102+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
103+
uint64_t ext1_pins = machine_rtc_config.ext1_pins;
104+
105+
106+
// Check that all pins are allowed
107+
if (args[ARG_pins].u_obj != mp_const_none) {
108+
size_t len = 0;
109+
mp_obj_t *elem;
110+
mp_obj_get_array(args[ARG_pins].u_obj, &len, &elem);
111+
ext1_pins = 0;
112+
113+
for (int i = 0; i < len; i++) {
114+
115+
gpio_num_t pin_id = machine_pin_get_id(elem[i]);
116+
if (!RTC_IS_VALID_EXT_PIN(pin_id)) {
117+
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
118+
break;
119+
}
120+
ext1_pins |= (1ll << pin_id);
121+
}
122+
}
123+
124+
machine_rtc_config.ext1_level = args[ARG_level].u_bool;
125+
machine_rtc_config.ext1_pins = ext1_pins;
126+
127+
return mp_const_none;
128+
}
129+
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1);
130+
131+
static mp_obj_t esp32_wake_on_ulp(const mp_obj_t wake) {
132+
if (machine_rtc_config.ext0_pin != -1) {
133+
mp_raise_ValueError(MP_ERROR_TEXT("no resources"));
134+
}
135+
machine_rtc_config.wake_on_ulp = mp_obj_is_true(wake);
136+
return mp_const_none;
137+
}
138+
static MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_ulp_obj, esp32_wake_on_ulp);
139+
140+
#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
141+
static mp_obj_t esp32_gpio_deep_sleep_hold(const mp_obj_t enable) {
142+
if (mp_obj_is_true(enable)) {
143+
gpio_deep_sleep_hold_en();
144+
} else {
145+
gpio_deep_sleep_hold_dis();
146+
}
147+
return mp_const_none;
148+
}
149+
static MP_DEFINE_CONST_FUN_OBJ_1(esp32_gpio_deep_sleep_hold_obj, esp32_gpio_deep_sleep_hold);
150+
#endif
151+
152+
#if CONFIG_IDF_TARGET_ESP32
153+
154+
#include "soc/sens_reg.h"
155+
156+
static mp_obj_t esp32_raw_temperature(void) {
157+
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
158+
SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S);
159+
CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
160+
CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
161+
SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
162+
SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
163+
esp_rom_delay_us(100);
164+
SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
165+
esp_rom_delay_us(5);
166+
int res = GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT, SENS_TSENS_OUT_S);
167+
168+
return mp_obj_new_int(res);
169+
}
170+
static MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperature);
171+
172+
#else
173+
174+
// IDF 5 exposes new internal temperature interface, and the ESP32C3/S2/S3
175+
// now have calibrated temperature settings in 5 discrete ranges.
176+
#include "driver/temperature_sensor.h"
177+
178+
static mp_obj_t esp32_mcu_temperature(void) {
179+
static temperature_sensor_handle_t temp_sensor = NULL;
180+
float tvalue;
181+
if (temp_sensor == NULL) {
182+
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
183+
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
184+
}
185+
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
186+
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tvalue));
187+
ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor));
188+
return mp_obj_new_int((int)(tvalue + 0.5));
189+
//return mp_obj_new_float(tvalue); // Always seems to return XX.80 degrees...
190+
}
191+
static MP_DEFINE_CONST_FUN_OBJ_0(esp32_mcu_temperature_obj, esp32_mcu_temperature);
192+
193+
#endif
194+
195+
static mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) {
196+
mp_int_t cap = mp_obj_get_int(cap_in);
197+
multi_heap_info_t info;
198+
heap_t *heap;
199+
mp_obj_t heap_list = mp_obj_new_list(0, 0);
200+
SLIST_FOREACH(heap, &registered_heaps, next) {
201+
if (heap_caps_match(heap, cap)) {
202+
multi_heap_get_info(heap->heap, &info);
203+
mp_obj_t data[] = {
204+
MP_OBJ_NEW_SMALL_INT(heap->end - heap->start), // total heap size
205+
MP_OBJ_NEW_SMALL_INT(info.total_free_bytes), // total free bytes
206+
MP_OBJ_NEW_SMALL_INT(info.largest_free_block), // largest free contiguous
207+
MP_OBJ_NEW_SMALL_INT(info.minimum_free_bytes), // minimum free seen
208+
};
209+
mp_obj_t this_heap = mp_obj_new_tuple(4, data);
210+
mp_obj_list_append(heap_list, this_heap);
211+
}
212+
}
213+
return heap_list;
214+
}
215+
static MP_DEFINE_CONST_FUN_OBJ_1(esp32_idf_heap_info_obj, esp32_idf_heap_info);
216+
217+
static const mp_rom_map_elem_t esp32_module_globals_table[] = {
218+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp32) },
219+
220+
{ MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) },
221+
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) },
222+
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) },
223+
{ MP_ROM_QSTR(MP_QSTR_wake_on_ulp), MP_ROM_PTR(&esp32_wake_on_ulp_obj) },
224+
#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
225+
{ MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) },
226+
#endif
227+
#if CONFIG_IDF_TARGET_ESP32
228+
{ MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
229+
#else
230+
{ MP_ROM_QSTR(MP_QSTR_mcu_temperature), MP_ROM_PTR(&esp32_mcu_temperature_obj) },
231+
#endif
232+
{ MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) },
233+
234+
{ MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) },
235+
{ MP_ROM_QSTR(MP_QSTR_Partition), MP_ROM_PTR(&esp32_partition_type) },
236+
{ MP_ROM_QSTR(MP_QSTR_RMT), MP_ROM_PTR(&esp32_rmt_type) },
237+
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
238+
{ MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) },
239+
#endif
240+
241+
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE },
242+
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE },
243+
244+
{ MP_ROM_QSTR(MP_QSTR_HEAP_DATA), MP_ROM_INT(MALLOC_CAP_8BIT) },
245+
{ MP_ROM_QSTR(MP_QSTR_HEAP_EXEC), MP_ROM_INT(MALLOC_CAP_EXEC) },
246+
};
247+
248+
static MP_DEFINE_CONST_DICT(esp32_module_globals, esp32_module_globals_table);
249+
250+
const mp_obj_module_t esp32_module = {
251+
.base = { &mp_type_module },
252+
.globals = (mp_obj_dict_t *)&esp32_module_globals,
253+
};
254+
255+
MP_REGISTER_MODULE(MP_QSTR_esp32, esp32_module);

0 commit comments

Comments
 (0)