Skip to content

Commit 6bbb986

Browse files
committed
Add basic analogWrite support
1 parent 16a9cf7 commit 6bbb986

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cores/esp32/esp32-hal-ledc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "freertos/task.h"
1818
#include "freertos/semphr.h"
1919
#include "esp32-hal-matrix.h"
20+
#include "soc/soc_caps.h"
2021
#include "soc/ledc_reg.h"
2122
#include "soc/ledc_struct.h"
2223
#include "driver/periph_ctrl.h"
@@ -331,3 +332,18 @@ double ledcChangeFrequency(uint8_t chan, double freq, uint8_t bit_num)
331332
double res_freq = _ledcSetupTimerFreq(chan, freq, bit_num);
332333
return res_freq;
333334
}
335+
336+
static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 };
337+
static int cnt_channel = SOC_LEDC_CHANNEL_NUM;
338+
void analogWrite(pin_t pin, int value) {
339+
// Use ledc hardware for internal pins
340+
if (pin < SOC_GPIO_PIN_COUNT) {
341+
if (pin_to_channel[pin] == 0) {
342+
pin_to_channel[pin] = cnt_channel--;
343+
ledcAttachPin(pin, cnt_channel);
344+
ledcSetup(cnt_channel, 1000, 8);
345+
ledcWrite(cnt_channel, value);
346+
}
347+
ledcWrite(pin_to_channel[pin] - 1, value);
348+
}
349+
}

cores/esp32/esp32-hal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ void yield(void);
9090
#include "esp32-hal-psram.h"
9191
#include "esp32-hal-cpu.h"
9292

93+
void analogWrite(pin_t pin, int value);
94+
9395
//returns chip temperature in Celsius
9496
float temperatureRead();
9597

0 commit comments

Comments
 (0)