Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit afe9ecf

Browse files
committedDec 1, 2021
DAC using ESP-IDF API
1 parent 399f4ec commit afe9ecf

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed
 

‎cores/esp32/esp32-hal-dac.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,41 @@
1313
// limitations under the License.
1414

1515
#include "esp32-hal.h"
16+
#include "soc/soc_caps.h"
1617

17-
#if CONFIG_IDF_TARGET_ESP32
18-
#include "soc/rtc_io_reg.h"
19-
#define DAC1 25
20-
#define DAC2 26
21-
#elif CONFIG_IDF_TARGET_ESP32S2
22-
#include "soc/rtc_io_reg.h"
23-
#define DAC1 17
24-
#define DAC2 18
25-
#elif CONFIG_IDF_TARGET_ESP32C3
18+
#ifndef SOC_DAC_SUPPORTED
2619
#define NODAC
2720
#else
28-
#error Target CONFIG_IDF_TARGET is not supported
29-
#endif
21+
#include "soc/dac_channel.h"
22+
#include "driver/dac_common.h"
3023

31-
#ifndef NODAC
32-
#include "esp_attr.h"
33-
#include "soc/rtc_cntl_reg.h"
34-
#include "soc/rtc_io_periph.h"
35-
#include "soc/sens_reg.h"
36-
#include "soc/sens_struct.h"
37-
#include "driver/dac.h"
24+
#define DAC1 DAC_CHANNEL_1_GPIO_NUM
25+
#define DAC2 DAC_CHANNEL_2_GPIO_NUM
3826

3927
void ARDUINO_ISR_ATTR __dacWrite(uint8_t pin, uint8_t value)
4028
{
4129
if(pin < DAC1 || pin > DAC2){
4230
return;//not dac pin
4331
}
44-
pinMode(pin, ANALOG);
32+
4533
uint8_t channel = pin - DAC1;
46-
#if CONFIG_IDF_TARGET_ESP32
47-
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
48-
#elif CONFIG_IDF_TARGET_ESP32S2
49-
SENS.sar_dac_ctrl1.dac_clkgate_en = 1;
50-
#endif
51-
RTCIO.pad_dac[channel].dac_xpd_force = 1;
52-
RTCIO.pad_dac[channel].xpd_dac = 1;
53-
if (channel == 0) {
54-
SENS.sar_dac_ctrl2.dac_cw_en1 = 0;
55-
} else if (channel == 1) {
56-
SENS.sar_dac_ctrl2.dac_cw_en2 = 0;
34+
35+
dac_output_enable(channel);
36+
dac_output_voltage(channel, value);
37+
38+
}
39+
40+
void ARDUINO_ISR_ATTR __dacDisable(uint8_t pin)
41+
{
42+
if(pin < DAC1 || pin > DAC2){
43+
return;//not dac pin
5744
}
58-
RTCIO.pad_dac[channel].dac = value;
45+
46+
uint8_t channel = pin - DAC1;
47+
dac_output_disable(channel);
5948
}
6049

6150
extern void dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
51+
extern void dacDisable(uint8_t pin) __attribute__ ((weak, alias("__dacDisable")));
6252

6353
#endif

‎cores/esp32/esp32-hal-dac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern "C" {
2828
#include "driver/gpio.h"
2929

3030
void dacWrite(uint8_t pin, uint8_t value);
31+
void dacDisable(uint8_t pin);
3132

3233
#ifdef __cplusplus
3334
}

0 commit comments

Comments
 (0)
Please sign in to comment.