forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-hal-dac.c
80 lines (71 loc) · 2.36 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
77
78
79
80
/*
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp32-hal-dac.h"
#if SOC_DAC_SUPPORTED
#include "esp32-hal.h"
#include "esp32-hal-periman.h"
#include "soc/dac_channel.h"
#include "driver/dac_oneshot.h"
static bool dacDetachBus(void * bus){
esp_err_t err = 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_t pin, uint8_t value)
{
esp_err_t err = 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_t bus = (dac_oneshot_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if(bus == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_DAC_ONESHOT, dacDetachBus);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
return false;
}
dac_channel_t channel = (pin == DAC_CHAN0_GPIO_NUM)?DAC_CHAN_0:DAC_CHAN_1;
dac_oneshot_config_t config = {
.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)){
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_t pin)
{
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
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
} else {
log_e("pin %u is not attached to DAC", pin);
}
return false;
}
extern bool dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
extern bool dacDisable(uint8_t pin) __attribute__ ((weak, alias("__dacDisable")));
#endif