Fixed comments and cleaned up

This commit is contained in:
Stephan Hadinger 2022-08-02 10:07:55 +02:00
parent 47b1f40245
commit f45cd5e905
3 changed files with 59 additions and 62 deletions

View File

@ -147,6 +147,30 @@ int32_t _analog_pin2timer(uint32_t pin) { // returns -1 if uallocated
return timer;
}
// get the next unused timer, returns -1 if no free timer is available
// Keep in mind that Timer 0 is reserved, which leaves only 3 timers available
//
// This function does not reserve the timer, it is reserved only when you assign a GPIO to it
static int32_t analogNextFreeTimer() {
_analogInit(); // make sure the mapping array is initialized
bool assigned[MAX_TIMERS] = {};
assigned[0] = true;
for (uint32_t chan = 0; chan < MAX_PWMS; chan++) {
assigned[pwm_timer[chan]] = true;
}
// find first free
for (uint32_t j = 0; j < MAX_TIMERS; j++) {
if (!assigned[j]) {
// AddLog(LOG_LEVEL_INFO, "PWM: analogNextFreeTimer next_timer=%i", j);
return j;
}
}
// AddLog(LOG_LEVEL_INFO, "PWM: analogNextFreeTimer no free timer");
return -1; // none available
}
// input range is in full range, ledc needs bits
uint32_t _analogGetResolution(uint32_t x) {
uint32_t bits = 0;
@ -177,7 +201,7 @@ void analogWriteRange(uint32_t range, int32_t pin) {
// `-1`: keep unchanged
// if pin < 0 then change global value for timer 0
void analogWriteFreqRange(int32_t freq, int32_t range, int32_t pin) {
AddLog(LOG_LEVEL_INFO, "PWM: analogWriteFreqRange freq=%i range=%i pin=%i", freq, range, pin);
// AddLog(LOG_LEVEL_INFO, "PWM: analogWriteFreqRange freq=%i range=%i pin=%i", freq, range, pin);
_analogInit(); // make sure the mapping array is initialized
uint32_t timer0_freq = timer_freq_hz[0]; // global values
uint8_t timer0_res = timer_duty_resolution[0];
@ -389,27 +413,6 @@ int32_t analogGetTimerForChannel(uint8_t chan) {
// get the next unused timer, returns -1 if no free timer is available
// Keep in mind that Timer 0 is reserved, which leaves only 3 timers available
//
// This function does not reserve the timer, it is reserved only when you assign a GPIO to it
int32_t analogNextFreeTimer() {
_analogInit(); // make sure the mapping array is initialized
bool assigned[MAX_TIMERS] = {};
assigned[0] = true;
for (uint32_t chan = 0; chan < MAX_PWMS; chan++) {
assigned[pwm_timer[chan]] = true;
}
// find first free
for (uint32_t j = 0; j < MAX_TIMERS; j++) {
if (!assigned[j]) {
// AddLog(LOG_LEVEL_INFO, "PWM: analogNextFreeTimer next_timer=%i", j);
return j;
}
}
// AddLog(LOG_LEVEL_INFO, "PWM: analogNextFreeTimer no free timer");
return -1; // none available
}
// Get timer resolution (in bits) - default 10
uint8_t analogGetTimerResolution(uint8_t timer) {
_analogInit(); // make sure the mapping array is initialized

View File

@ -28,19 +28,19 @@
*
* The following supersedes Arduino framework and provides more granular control:
* - fine grained phase control (in addition to duty cycle)
* - fine control of which PWM uses which timers
* - fine control of frequency and resolution per timer
* - fine control of PWM frequency and resolution per GPIO
*
* By default, all PWM are using the same timer called Timer0.
* Changes in frequency of resolution apply to all PWM using Timer0.
* By default, all PWM are using the same timer called Timer 0.
* Changes in frequency of resolution apply to all PWM using Timer 0.
*
* You can specify a different timer per GPIO (Timer1 to Timer3).
* Then any change of resolution or frequency to these GPIOs alter only
* the corresponding timer.
* You can specify a different a different resolution/frequency for
* specific GPIOs, this will internally assign a new timer to the GPIO.
* The limit is 3 specific values in addition to the global value.
*
* Note: on ESP32-only, there are 2 groups of PWM and 2 groups of timers.
* - PWM 0..7 are using Timer 0..3
* - PWM 8..15 are using Timer 4..7
* Although there are internally 8 timers, to simplifiy management,
* Timer 4..7 are mirrored from Timer 0..3.
* So it all happens like if there were only 4 timers and a single group of PWM channels.
\*******************************************************************************************/
uint32_t ledcReadFreq2(uint8_t chan);
@ -55,29 +55,38 @@ uint8_t ledcReadResolution(uint8_t chan);
// Returns: hardware channel number, or -1 if it failed
int analogAttach(uint32_t pin, bool output_invert = false); // returns the ledc channel, or -1 if failed. This is implicitly called by analogWrite if the channel was not already allocated
// change both freq and range
// `0`: set to global value
// `-1`: keep unchanged
// if pin < 0 then change global value for timer 0
//
// analogWriteFreqRange - change the range and/or frequency of a GPIO
//
// `void analogWriteFreqRange(int32_t freq, int32_t range, int32_t pin)`
//
// The range is converted to a number of bits, so range must be a power of 2 minus 1.
// By default, the resolution is 10 bits, i.e. a range of 1023.
//
// Special cases:
// - if `pin < 0`, changes the global value for Timer 0 and all PWM using default
// - if `range == 0` or `freq == 0`, revert to using Timer 0 (i.e. reassign to global values)
// - if `range < 0` or `freq < 0`, keep the previous value unchanged
// - if `pin` is unassigned, silently ignore
void analogWriteFreqRange(int32_t freq, int32_t range, int32_t pin = -1);
//
// analogWriteRange - change the range of PWM
//
// Internally the range is converted to a number of bits, so range must be a power of 2.
// By default, the resolution is 10 bits, i.e. a range of 1024.
//
// When changing this value, it changes the underlying Timer. This means
// that the range changes for all PWM using the same timer.
//
// If `pin == 255`, the value is changed for the default Timer(s)
// I.e. `Timer 0` (and `Timer 4` on ESP32)
// short-cut for:
// `analogWriteFreqRange(-1, range, pin)`
void analogWriteRange(uint32_t range, int32_t pin = -1);
//
// analogWriteFreq - change the frequency of PWM in Hz
//
// Default value is 977Hz. TODO: use the value in `my_user_config.h`
//
// When changing this value, it changes the underlying Timer. This means
// that the range changes for all PWM using the same timer.
//
// If `pin == 255`, the value is changed for the default Timer(s)
// I.e. `Timer 0` (and `Timer 4` on ESP32)
// short-cut for:
// `analogWriteFreqRange(-1, range, pin)`
void analogWriteFreq(uint32_t freq, int32_t pin = -1);
//
@ -99,25 +108,11 @@ extern int32_t analogGetChannel2(uint32_t pin);
int32_t analogGetTimer(uint8_t pin);
int32_t analogGetTimerForChannel(uint8_t chan);
// set the timer number for a GPIO, ignore if the GPIO is not set or Timer number is invalid
// Timer range is 0..3
void analogSetTimer(uint8_t pin, uint8_t timer);
// get the next unused timer, returns -1 if no free timer is available
// Keep in mind that Timer 0 is reserved, which leaves only 3 timers available
//
// This function does not reserve the timer, it is reserved only when you assign a GPIO to it
int32_t analogNextFreeTimer();
// Get timer resolution (in bits) - default 10
uint8_t analogGetTimerResolution(uint8_t timer);
// void analogSetTimerResolution(uint8_t timer, uint8_t resolution);
// Get timer frequency (in Hz) - default 977
uint32_t analogGetTimerFrequency(uint8_t timer);
// void analogSetTimerFrequency(uint8_t timer, uint32_t freq);
// void analogSetTimerResFreq(uint8_t timer, uint8_t resolution, uint32_t freq);
/*********************************************************************************************/

View File

@ -83,7 +83,6 @@ void PwmApplyGPIO(bool force_update_all) {
if (PinUsed(GPIO_PWM1, i)) {
int32_t pin = Pin(GPIO_PWM1, i);
int32_t chan = analogGetChannel2(pin);
// int32_t timer = analogGetTimer(pin);
uint32_t res = ledcReadResolution(chan);
uint32_t range = (1 << res) - 1;
uint32_t freq = ledcReadFreq2(chan);