- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathesp32-hal-dac.c
76 lines (67 loc) · 2.17 KB
/
esp32-hal-dac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include"esp32-hal-dac.h"
#ifSOC_DAC_SUPPORTED
#include"esp32-hal.h"
#include"esp32-hal-periman.h"
#include"soc/dac_channel.h"
#include"driver/dac_oneshot.h"
staticbooldacDetachBus(void*bus) {
esp_err_terr=dac_oneshot_del_channel((dac_oneshot_handle_t)bus);
if (err!=ESP_OK) {
log_e("dac_oneshot_del_channel failed with error: %d", err);
return false;
}
return true;
}
bool__dacWrite(uint8_tpin, uint8_tvalue) {
esp_err_terr=ESP_OK;
if (pin!=DAC_CHAN0_GPIO_NUM&&pin!=DAC_CHAN1_GPIO_NUM) {
log_e("pin %u is not a DAC pin", pin);
return false; //not dac pin
}
dac_oneshot_handle_tbus= (dac_oneshot_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if (bus==NULL) {
perimanSetBusDeinit(ESP32_BUS_TYPE_DAC_ONESHOT, dacDetachBus);
if (!perimanClearPinBus(pin)) {
return false;
}
dac_channel_tchannel= (pin==DAC_CHAN0_GPIO_NUM) ? DAC_CHAN_0 : DAC_CHAN_1;
dac_oneshot_config_tconfig= {.chan_id=channel};
err=dac_oneshot_new_channel(&config, &bus);
if (err!=ESP_OK) {
log_e("dac_oneshot_new_channel failed with error: %d", err);
return false;
}
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT, (void*)bus, -1, channel)) {
dacDetachBus((void*)bus);
return false;
}
}
err=dac_oneshot_output_voltage(bus, value);
if (err!=ESP_OK) {
log_e("dac_oneshot_output_voltage failed with error: %d", err);
return false;
}
return true;
}
bool__dacDisable(uint8_tpin) {
if (pin!=DAC_CHAN0_GPIO_NUM&&pin!=DAC_CHAN1_GPIO_NUM) {
log_e("pin %u is not a DAC pin", pin);
return false; //not dac pin
}
void*bus=perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if (bus!=NULL) {
// will call dacDetachBus
returnperimanClearPinBus(pin);
} else {
log_e("pin %u is not attached to DAC", pin);
}
return false;
}
externbooldacWrite(uint8_tpin, uint8_tvalue) __attribute__((weak, alias("__dacWrite")));
externbooldacDisable(uint8_tpin) __attribute__((weak, alias("__dacDisable")));
#endif