8000 Waveform: int/uint8_t inconsistency and implied stop PWM by dok-net · Pull Request #8008 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Waveform: int/uint8_t inconsistency and implied stop PWM #8008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
let analogWrite coexist with open-drain mode
  • Loading branch information
dok-net committed Dec 23, 2022
commit 98537bd150194d88c2140fb2dd51086810d4a11b
2 changes: 1 addition & 1 deletion cores/esp8266/core_esp8266_wiring_digital.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extern void __pinMode(uint8_t pin, uint8_t mode) {
}

extern void IRAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
stopWaveform(pin); // Disable any Tone or startWaveform on this pin
stopWaveform(pin); // Disable any Tone, startWaveform, or analogWrite on this pin
if(pin < 16){
if(val) GPOS = (1 << pin);
else GPOC = (1 << pin);
Expand Down
26 changes: 16 additions & 10 deletions cores/esp8266/core_esp8266_wiring_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ extern void __analogWriteFreq(uint32_t freq) {
}

extern void __analogWrite(uint8_t pin, int val) {
analogWriteMode(pin, val, false);
if (pin > 16) {
return;
}
bool openDrain = false;
if (analogMap & 1UL << pin) {
openDrain = GPC(pin) & (1 << GPCD);
}
analogWriteMode(pin, val, openDrain);
}

extern void __analogWriteMode(uint8_t pin, int val, bool openDrain) {
Expand All @@ -59,23 +66,22 @@ extern void __analogWriteMode(uint8_t pin, int val, bool openDrain) {
}

if (analogMap & 1UL << pin) {
// Per the Arduino docs at https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
// val: the duty cycle: between 0 (always off) and 255 (always on).
// So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH)

analogMap &= ~(1 << pin);
const bool isOpenDrain = GPC(pin) & (1 << GPCD);
if (isOpenDrain != openDrain) {
GPC(pin) ^= (1 << GPCD);
}
}
else {
if(openDrain) {
pinMode(pin, OUTPUT_OPEN_DRAIN);
} else {
pinMode(pin, OUTPUT);
}
pinMode(pin, openDrain ? OUTPUT_OPEN_DRAIN : OUTPUT);
}
uint32_t high = (analogPeriod * val) / analogScale;
uint32_t low = analogPeriod - high;
// Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t)
int phaseReference = __builtin_ffs(analogMap) - 1;
// Per the Arduino docs at https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
// val: the duty cycle: between 0 (always off) and 255 (always on).
// So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH)
if (_setPWM(pin, val, analogScale)) {
analogMap |= (1 << pin);
} else if (startWaveformClockCycles(pin, high, low, 0, phaseReference, 0, true)) {
Expand Down
3 changes: 1 addition & 2 deletions libraries/Servo/src/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ void Servo::writeMicroseconds(int value)
_servoMap &= ~(1 << _pin);
// Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t)
int phaseReference = __builtin_ffs(_servoMap) - 1;
if (startWaveform(_pin, _valueUs, REFRESH_INTERVAL - _valueUs, 0, phaseReference))
{
if (startWaveform(_pin, _valueUs, REFRESH_INTERVAL - _valueUs, 0, phaseReference)) {
_servoMap |= (1 << _pin);
}
}
Expand Down
0