Skip to content

Commit 30122ba

Browse files
committed
esp32/DAC: Add DAC deinit() method.
esp32/DAC: Add DAC deinit() method. Update quickref.rst Signed-off-by: Ihor Nehrutsa <IhorNehrutsa@gmail.com>
1 parent 5232847 commit 30122ba

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

docs/esp32/quickref.rst

100644100755
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ These are working configurations for LAN interfaces of popular boards::
141141
# Olimex ESP32-GATEWAY: power controlled by Pin(5)
142142
# Olimex ESP32 PoE and ESP32-PoE ISO: power controlled by Pin(12)
143143

144-
lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5),
144+
lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5),
145145
phy_type=network.PHY_LAN8720, phy_addr=0,
146146
ref_clk=machine.Pin(17), ref_clk_mode=machine.Pin.OUT)
147147

@@ -330,6 +330,18 @@ possible at the same frequency.
330330

331331
See more examples in the :ref:`esp32_pwm` tutorial.
332332

333+
DAC (digital to analog conversion)
334+
----------------------------------
335+
336+
On the ESP32, DAC functionality is available on pins 25, 26.
337+
338+
Use the DAC::
339+
340+
from machine import DAC, Pin
341+
342+
dac = DAC(Pin(25)) # create an DAC object acting on a pin
343+
dac.write(128) # set a raw analog value in the range 0-255, 50% now
344+
333345
ADC (analog to digital conversion)
334346
----------------------------------
335347

ports/esp32/machine_dac.c

100644100755
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ STATIC mp_obj_t mdac_write(mp_obj_t self_in, mp_obj_t value_in) {
8787
mdac_obj_t *self = self_in;
8888
int value = mp_obj_get_int(value_in);
8989
if (value < 0 || value > 255) {
90-
mp_raise_ValueError(MP_ERROR_TEXT("value out of range"));
90+
mp_raise_ValueError(MP_ERROR_TEXT("value out of range [0..255]"));
9191
}
9292

9393
esp_err_t err = dac_output_voltage(self->dac_id, value);
@@ -98,8 +98,24 @@ STATIC mp_obj_t mdac_write(mp_obj_t self_in, mp_obj_t value_in) {
9898
}
9999
MP_DEFINE_CONST_FUN_OBJ_2(mdac_write_obj, mdac_write);
100100

101+
STATIC mp_obj_t mdac_deinit(mp_obj_t self_in) {
102+
mdac_obj_t *self = self_in;
103+
check_esp_err(dac_output_disable(self->dac_id));
104+
return mp_const_none;
105+
}
106+
MP_DEFINE_CONST_FUN_OBJ_1(mdac_deinit_obj, mdac_deinit);
107+
108+
// This called from Ctrl-D soft reboot
109+
void machine_dac_deinit_all(void) {
110+
for (int i = 0; i < MP_ARRAY_SIZE(mdac_obj); i++) {
111+
mdac_deinit((mdac_obj_t *)&mdac_obj[i]);
112+
}
113+
}
114+
101115
STATIC const mp_rom_map_elem_t mdac_locals_dict_table[] = {
102116
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mdac_write_obj) },
117+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&mdac_deinit_obj) },
118+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mdac_deinit_obj) },
103119
};
104120

105121
STATIC MP_DEFINE_CONST_DICT(mdac_locals_dict, mdac_locals_dict_table);

ports/esp32/main.c

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ void mp_task(void *pvParameter) {
179179

180180
// deinitialise peripherals
181181
machine_pwm_deinit_all();
182+
#if MICROPY_PY_MACHINE_DAC
183+
machine_dac_deinit_all();
184+
#endif
182185
// TODO: machine_rmt_deinit_all();
183186
machine_pins_deinit();
184187
machine_deinit();

ports/esp32/modmachine.h

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ void machine_deinit(void);
2828
void machine_pins_init(void);
2929
void machine_pins_deinit(void);
3030
void machine_pwm_deinit_all(void);
31+
#if MICROPY_PY_MACHINE_DAC
32+
void machine_dac_deinit_all(void);
33+
#endif
3134
// TODO: void machine_rmt_deinit_all(void);
3235
void machine_timer_deinit_all(void);
3336
void machine_i2s_init0();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy