- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathesp32-hal-sigmadelta.c
83 lines (77 loc) · 2.48 KB
/
esp32-hal-sigmadelta.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
81
82
83
/*
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include"esp32-hal-sigmadelta.h"
#ifSOC_SDM_SUPPORTED
#include"esp32-hal.h"
#include"esp32-hal-periman.h"
#include"driver/sdm.h"
staticboolsigmaDeltaDetachBus(void*bus) {
esp_err_terr=sdm_channel_disable((sdm_channel_handle_t)bus);
if (err!=ESP_OK) {
log_w("sdm_channel_disable failed with error: %d", err);
}
err=sdm_del_channel((sdm_channel_handle_t)bus);
if (err!=ESP_OK) {
log_e("sdm_del_channel failed with error: %d", err);
return false;
}
return true;
}
boolsigmaDeltaAttach(uint8_tpin, uint32_tfreq) //freq 1220-312500
{
perimanSetBusDeinit(ESP32_BUS_TYPE_SIGMADELTA, sigmaDeltaDetachBus);
sdm_channel_handle_tbus=NULL;
// pin may be previously attached to other peripheral -> detach it.
// if attached to sigmaDelta, detach it and set the new frequency
if (perimanGetPinBusType(pin) !=ESP32_BUS_TYPE_INIT&& !perimanClearPinBus(pin)) {
log_e("Pin %u could not be detached.", pin);
return false;
}
sdm_config_tconfig= {.gpio_num= (int)pin, .clk_src=SDM_CLK_SRC_DEFAULT, .sample_rate_hz=freq, .flags= {.invert_out=0, .io_loop_back=0}};
esp_err_terr=sdm_new_channel(&config, &bus);
if (err!=ESP_OK) {
log_e("sdm_new_channel failed with error: %d", err);
return false;
}
err=sdm_channel_enable(bus);
if (err!=ESP_OK) {
sigmaDeltaDetachBus((void*)bus);
log_e("sdm_channel_enable failed with error: %d", err);
return false;
}
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA, (void*)bus, -1, -1)) {
sigmaDeltaDetachBus((void*)bus);
return false;
}
return true;
}
boolsigmaDeltaWrite(uint8_tpin, uint8_tduty) //chan 0-x according to SOC duty 8 bit
{
sdm_channel_handle_tbus= (sdm_channel_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA);
if (bus!=NULL) {
int8_td=duty-128;
esp_err_terr=sdm_channel_set_duty(bus, d);
if (err!=ESP_OK) {
log_e("sdm_channel_set_duty failed with error: %d", err);
return false;
}
return true;
} else {
log_e("pin %u is not attached to SigmaDelta", pin);
}
return false;
}
boolsigmaDeltaDetach(uint8_tpin) {
void*bus=perimanGetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA);
if (bus!=NULL) {
// will call sigmaDeltaDetachBus
returnperimanClearPinBus(pin);
} else {
log_e("pin %u is not attached to SigmaDelta", pin);
}
return false;
}
#endif