Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit caef400

Browse filesBrowse files
authored
Implement SigmaDelta based on ESP-IDF API (#6053)
Summary This PR is refactoring of SigmaDelta HAL in order to use IDF instead of current Register manipulation approach. Impact Change in API: uint32_t sigmaDeltaSetup(uint8_t channel, uint32_t freq); changed to --> uint32_t sigmaDeltaSetup(uint8_t pin, uint8_t channel, uint32_t freq); void sigmaDeltaAttachPin(uint8_t pin); removed, no longer needed. Pin is attached in sigmaDeltaSetup()
1 parent 44fbde0 commit caef400
Copy full SHA for caef400

File tree

3 files changed

+37
-83
lines changed
Filter options

3 files changed

+37
-83
lines changed

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

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-sigmadelta.c
+34-77Lines changed: 34 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -12,121 +12,78 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "esp32-hal.h"
16-
#include "freertos/FreeRTOS.h"
17-
#include "freertos/task.h"
18-
#include "freertos/semphr.h"
19-
#include "esp32-hal-matrix.h"
20-
#include "soc/gpio_sd_reg.h"
21-
#include "soc/gpio_sd_struct.h"
2215

23-
#include "esp_system.h"
24-
#ifdef ESP_IDF_VERSION_MAJOR // IDF 4+
25-
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
26-
#include "esp32/rom/ets_sys.h"
27-
#elif CONFIG_IDF_TARGET_ESP32S2
28-
#include "esp32s2/rom/ets_sys.h"
29-
#elif CONFIG_IDF_TARGET_ESP32C3
30-
#include "esp32c3/rom/ets_sys.h"
31-
#else
32-
#error Target CONFIG_IDF_TARGET is not supported
33-
#endif
34-
#else // ESP32 Before IDF 4.0
35-
#include "rom/ets_sys.h"
36-
#endif
3716

17+
#include "esp32-hal.h"
18+
#include "soc/soc_caps.h"
19+
#include "driver/sigmadelta.h"
3820

39-
#if CONFIG_DISABLE_HAL_LOCKS
40-
#define SD_MUTEX_LOCK()
41-
#define SD_MUTEX_UNLOCK()
42-
#else
43-
#define SD_MUTEX_LOCK() do {} while (xSemaphoreTake(_sd_sys_lock, portMAX_DELAY) != pdPASS)
44-
#define SD_MUTEX_UNLOCK() xSemaphoreGive(_sd_sys_lock)
45-
xSemaphoreHandle _sd_sys_lock;
46-
#endif
21+
static uint8_t duty_set[SOC_SIGMADELTA_CHANNEL_NUM] = {0};
22+
static uint32_t prescaler_set[SOC_SIGMADELTA_CHANNEL_NUM] = {0};
4723

4824
static void _on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb, uint32_t new_apb){
4925
if(old_apb == new_apb){
5026
return;
5127
}
5228
uint32_t iarg = (uint32_t)arg;
5329
uint8_t channel = iarg;
54-
if(ev_type == APB_BEFORE_CHANGE){
55-
SIGMADELTA.cg.clk_en = 0;
56-
} else {
30+
if(ev_type == APB_AFTER_CHANGE){
5731
old_apb /= 1000000;
5832
new_apb /= 1000000;
59-
SD_MUTEX_LOCK();
60-
uint32_t old_prescale = SIGMADELTA.channel[channel].prescale + 1;
61-
SIGMADELTA.channel[channel].prescale = ((new_apb * old_prescale) / old_apb) - 1;
62-
SIGMADELTA.cg.clk_en = 0;
63-
SIGMADELTA.cg.clk_en = 1;
64-
SD_MUTEX_UNLOCK();
33+
uint32_t old_prescale = prescaler_set[channel] + 1;
34+
uint32_t new_prescale = ((new_apb * old_prescale) / old_apb) - 1;
35+
sigmadelta_set_prescale(channel,new_prescale);
36+
prescaler_set[channel] = new_prescale;
6537
}
6638
}
6739

68-
uint32_t sigmaDeltaSetup(uint8_t channel, uint32_t freq) //chan 0-7 freq 1220-312500
40+
uint32_t sigmaDeltaSetup(uint8_t pin, uint8_t channel, uint32_t freq) //chan 0-x according to SOC, freq 1220-312500
6941
{
70-
if(channel > 7) {
42+
if(channel >= SOC_SIGMADELTA_CHANNEL_NUM){
7143
return 0;
7244
}
73-
#if !CONFIG_DISABLE_HAL_LOCKS
74-
static bool tHasStarted = false;
75-
if(!tHasStarted) {
76-
tHasStarted = true;
77-
_sd_sys_lock = xSemaphoreCreateMutex();
78-
}
79-
#endif
45+
8046
uint32_t apb_freq = getApbFrequency();
8147
uint32_t prescale = (apb_freq/(freq*256)) - 1;
8248
if(prescale > 0xFF) {
8349
prescale = 0xFF;
8450
}
85-
SD_MUTEX_LOCK();
86-
#ifndef CONFIG_IDF_TARGET_ESP32
87-
SIGMADELTA.misc.function_clk_en = 1;
88-
#endif
89-
SIGMADELTA.channel[channel].prescale = prescale;
90-
SIGMADELTA.cg.clk_en = 0;
91-
SIGMADELTA.cg.clk_en = 1;
92-
SD_MUTEX_UNLOCK();
51+
52+
sigmadelta_config_t sigmadelta_cfg = {
53+
.channel = channel,
54+
.sigmadelta_prescale = prescale,
55+
.sigmadelta_duty = 0,
56+
.sigmadelta_gpio = pin,
57+
};
58+
sigmadelta_config(&sigmadelta_cfg);
59+
60+
prescaler_set[channel] = prescale;
9361
uint32_t iarg = channel;
9462
addApbChangeCallback((void*)iarg, _on_apb_change);
63+
9564
return apb_freq/((prescale + 1) * 256);
9665
}
9766

98-
void sigmaDeltaWrite(uint8_t channel, uint8_t duty) //chan 0-7 duty 8 bit
67+
void sigmaDeltaWrite(uint8_t channel, uint8_t duty) //chan 0-x according to SOC duty 8 bit
9968
{
100-
if(channel > 7) {
69+
if(channel >= SOC_SIGMADELTA_CHANNEL_NUM){
10170
return;
10271
}
103-
duty -= 128;
104-
SD_MUTEX_LOCK();
105-
SIGMADELTA.channel[channel].duty = duty;
106-
SD_MUTEX_UNLOCK();
107-
}
72+
duty -= 128;
10873

109-
uint8_t sigmaDeltaRead(uint8_t channel) //chan 0-7
110-
{
111-
if(channel > 7) {
112-
return 0;
113-
}
114-
SD_MUTEX_LOCK();
115-
uint8_t duty = SIGMADELTA.channel[channel].duty + 128;
116-
SD_MUTEX_UNLOCK();
117-
return duty;
74+
sigmadelta_set_duty(channel,duty);
75+
duty_set[channel] = duty;
11876
}
11977

120-
void sigmaDeltaAttachPin(uint8_t pin, uint8_t channel) //channel 0-7
78+
uint8_t sigmaDeltaRead(uint8_t channel) //chan 0-x according to SOC
12179
{
122-
if(channel > 7) {
123-
return;
80+
if(channel >= SOC_SIGMADELTA_CHANNEL_NUM){
81+
return 0;
12482
}
125-
pinMode(pin, OUTPUT);
126-
pinMatrixOutAttach(pin, GPIO_SD0_OUT_IDX + channel, false, false);
83+
return duty_set[channel]+128;
12784
}
12885

12986
void sigmaDeltaDetachPin(uint8_t pin)
13087
{
13188
pinMatrixOutDetach(pin, false, false);
132-
}
89+
}

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

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-sigmadelta.h
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ extern "C" {
2323
#include <stdbool.h>
2424

2525
//channel 0-7 freq 1220-312500 duty 0-255
26-
uint32_t sigmaDeltaSetup(uint8_t channel, uint32_t freq);
26+
uint32_t sigmaDeltaSetup(uint8_t pin, uint8_t channel, uint32_t freq);
2727
void sigmaDeltaWrite(uint8_t channel, uint8_t duty);
2828
uint8_t sigmaDeltaRead(uint8_t channel);
29-
void sigmaDeltaAttachPin(uint8_t pin, uint8_t channel);
3029
void sigmaDeltaDetachPin(uint8_t pin);
3130

3231

‎libraries/ESP32/examples/AnalogOut/SigmaDelta/SigmaDelta.ino

Copy file name to clipboardExpand all lines: libraries/ESP32/examples/AnalogOut/SigmaDelta/SigmaDelta.ino
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
void setup()
22
{
3-
//setup channel 0 with frequency 312500 Hz
4-
sigmaDeltaSetup(0, 312500);
5-
//attach pin 18 to channel 0
6-
sigmaDeltaAttachPin(18,0);
3+
//setup on pin 18, channel 0 with frequency 312500 Hz
4+
sigmaDeltaSetup(18,0, 312500);
75
//initialize channel 0 to off
86
sigmaDeltaWrite(0, 0);
97
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.