|
17 | 17 | #include "freertos/task.h"
|
18 | 18 | #include "freertos/semphr.h"
|
19 | 19 | #include "esp32-hal-matrix.h"
|
| 20 | +#include "soc/soc_caps.h" |
20 | 21 | #include "soc/ledc_reg.h"
|
21 | 22 | #include "soc/ledc_struct.h"
|
22 | 23 | #include "driver/periph_ctrl.h"
|
@@ -331,3 +332,21 @@ double ledcChangeFrequency(uint8_t chan, double freq, uint8_t bit_num)
|
331 | 332 | double res_freq = _ledcSetupTimerFreq(chan, freq, bit_num);
|
332 | 333 | return res_freq;
|
333 | 334 | }
|
| 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(uint8_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 | + if (!cnt_channel) { |
| 343 | + log_e("No more analogWrite channels available! You can have maximum %u", SOC_LEDC_CHANNEL_NUM); |
| 344 | + return; |
| 345 | + } |
| 346 | + pin_to_channel[pin] = cnt_channel--; |
| 347 | + ledcAttachPin(pin, cnt_channel); |
| 348 | + ledcSetup(cnt_channel, 1000, 8); |
| 349 | + } |
| 350 | + ledcWrite(pin_to_channel[pin] - 1, value); |
| 351 | + } |
| 352 | +} |
0 commit comments