mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-30 02:37:14 +02:00
Add basic analogWrite support based on LEDC (#5861)
This commit is contained in:
@ -17,6 +17,7 @@
|
|||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
#include "esp32-hal-matrix.h"
|
#include "esp32-hal-matrix.h"
|
||||||
|
#include "soc/soc_caps.h"
|
||||||
#include "soc/ledc_reg.h"
|
#include "soc/ledc_reg.h"
|
||||||
#include "soc/ledc_struct.h"
|
#include "soc/ledc_struct.h"
|
||||||
#include "driver/periph_ctrl.h"
|
#include "driver/periph_ctrl.h"
|
||||||
@ -331,3 +332,21 @@ double ledcChangeFrequency(uint8_t chan, double freq, uint8_t bit_num)
|
|||||||
double res_freq = _ledcSetupTimerFreq(chan, freq, bit_num);
|
double res_freq = _ledcSetupTimerFreq(chan, freq, bit_num);
|
||||||
return res_freq;
|
return res_freq;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 };
|
||||||
|
static int cnt_channel = SOC_LEDC_CHANNEL_NUM;
|
||||||
|
void analogWrite(uint8_t pin, int value) {
|
||||||
|
// Use ledc hardware for internal pins
|
||||||
|
if (pin < SOC_GPIO_PIN_COUNT) {
|
||||||
|
if (pin_to_channel[pin] == 0) {
|
||||||
|
if (!cnt_channel) {
|
||||||
|
log_e("No more analogWrite channels available! You can have maximum %u", SOC_LEDC_CHANNEL_NUM);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pin_to_channel[pin] = cnt_channel--;
|
||||||
|
ledcAttachPin(pin, cnt_channel);
|
||||||
|
ledcSetup(cnt_channel, 1000, 8);
|
||||||
|
}
|
||||||
|
ledcWrite(pin_to_channel[pin] - 1, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -90,6 +90,8 @@ void yield(void);
|
|||||||
#include "esp32-hal-psram.h"
|
#include "esp32-hal-psram.h"
|
||||||
#include "esp32-hal-cpu.h"
|
#include "esp32-hal-cpu.h"
|
||||||
|
|
||||||
|
void analogWrite(uint8_t pin, int value);
|
||||||
|
|
||||||
//returns chip temperature in Celsius
|
//returns chip temperature in Celsius
|
||||||
float temperatureRead();
|
float temperatureRead();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user