Skip to content

Commit 39b9e1e

Browse files
Implement DAC based on ESP-IDF API (#5959)
This PR is refactoring of DAC HAL in order to use IDF instead of current Register manipulation approach. Edited dacWrite() to use ESP-IDF api. Added dacDisable() so there is an option to disable dac channel. Co-authored-by: Me No Dev <[email protected]>
1 parent 40a5c1e commit 39b9e1e

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

Diff for: cores/esp32/esp32-hal-dac.c

+20-34
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,37 @@
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
30-
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"
21+
#include "soc/dac_channel.h"
22+
#include "driver/dac_common.h"
3823

3924
void ARDUINO_ISR_ATTR __dacWrite(uint8_t pin, uint8_t value)
4025
{
41-
if(pin < DAC1 || pin > DAC2){
26+
if(pin < DAC_CHANNEL_1_GPIO_NUM || pin > DAC_CHANNEL_2_GPIO_NUM){
4227
return;//not dac pin
4328
}
44-
pinMode(pin, ANALOG);
45-
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;
29+
30+
uint8_t channel = pin - DAC_CHANNEL_1_GPIO_NUM;
31+
dac_output_enable(channel);
32+
dac_output_voltage(channel, value);
33+
34+
}
35+
36+
void ARDUINO_ISR_ATTR __dacDisable(uint8_t pin)
37+
{
38+
if(pin < DAC_CHANNEL_1_GPIO_NUM || pin > DAC_CHANNEL_2_GPIO_NUM){
39+
return;//not dac pin
5740
}
58-
RTCIO.pad_dac[channel].dac = value;
41+
42+
uint8_t channel = pin - DAC_CHANNEL_1_GPIO_NUM;
43+
dac_output_disable(channel);
5944
}
6045

6146
extern void dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
47+
extern void dacDisable(uint8_t pin) __attribute__ ((weak, alias("__dacDisable")));
6248

6349
#endif

Diff for: cores/esp32/esp32-hal-dac.h

+1
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)