Skip to content

Commit

Permalink
Merge pull request #99 from thorrak/s2_iic
Browse files Browse the repository at this point in the history
Fix issues with ESP32-S2 IIC & Freezing
  • Loading branch information
thorrak authored Sep 10, 2023
2 parents fa75e21 + f80331d commit 0842f97
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 62 deletions.
Binary file modified data/index.js.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ board = lolin_s2_mini
framework = ${common.framework}
; v4.4.4 of esp-idf introduces a bug in the IIC implementation that causes MASSIVE delays
; in the IIC bus on the S2 (meaning that writes to the LCD grind the controller to a halt).
; This is a workaround until the bug is fixed.
; This appears to be related to https://github.com/espressif/arduino-esp32/issues/8480#issuecomment-1708909457
platform_packages =
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.6
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.12
; For esp32_s2_wifi we want to enable LCD support, IIC support, and WiFi support
; There is no Bluetooth support on this chip.
build_flags =
Expand Down Expand Up @@ -245,9 +245,9 @@ board = lolin_s2_mini
framework = ${common.framework}
; v4.4.4 of esp-idf introduces a bug in the IIC implementation that causes MASSIVE delays
; in the IIC bus on the S2 (meaning that writes to the LCD grind the controller to a halt).
; This is a workaround until the bug is fixed.
; This appears to be related to https://github.com/espressif/arduino-esp32/issues/8480#issuecomment-1708909457
platform_packages =
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.10
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.12
; For esp32_s2_serial we want to enable LCD support, IIC support, and Serial support
; There is no Bluetooth support on this chip.
build_flags =
Expand Down
124 changes: 66 additions & 58 deletions src/IicLcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ IIClcd::IIClcd(uint8_t lcd_Addr, uint8_t lcd_cols, uint8_t lcd_rows)
_cols = lcd_cols;
_rows = lcd_rows;
_backlightval = LCD_NOBACKLIGHT;
_displayFound = false;
}

void IIClcd::scan_address() {
Expand All @@ -63,8 +64,13 @@ void IIClcd::scan_address() {
_Addr = i;
i = 120;
delay(1);
_displayFound = true;
break;
}
}

Wire.setClock(1000000); // Set the I2C bus rate
Wire.setClock(400000); // Try and reset clock rate
}


Expand Down Expand Up @@ -175,66 +181,66 @@ void IIClcd::setCursor(uint8_t col, uint8_t row) {
}

// Turn the display on/off (quickly)
void IIClcd::noDisplay() {
_displaycontrol &= ~LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// void IIClcd::noDisplay() {
// _displaycontrol &= ~LCD_DISPLAYON;
// command(LCD_DISPLAYCONTROL | _displaycontrol);
// }
void IIClcd::display() {
_displaycontrol |= LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}

// Turns the underline cursor on/off
void IIClcd::noCursor() {
_displaycontrol &= ~LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void IIClcd::cursor() {
_displaycontrol |= LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}

// Turn on and off the blinking cursor
void IIClcd::noBlink() {
_displaycontrol &= ~LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void IIClcd::blink() {
_displaycontrol |= LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// // Turns the underline cursor on/off
// void IIClcd::noCursor() {
// _displaycontrol &= ~LCD_CURSORON;
// command(LCD_DISPLAYCONTROL | _displaycontrol);
// }
// void IIClcd::cursor() {
// _displaycontrol |= LCD_CURSORON;
// command(LCD_DISPLAYCONTROL | _displaycontrol);
// }

// // Turn on and off the blinking cursor
// void IIClcd::noBlink() {
// _displaycontrol &= ~LCD_BLINKON;
// command(LCD_DISPLAYCONTROL | _displaycontrol);
// }
// void IIClcd::blink() {
// _displaycontrol |= LCD_BLINKON;
// command(LCD_DISPLAYCONTROL | _displaycontrol);
// }

// These commands scroll the display without changing the RAM
void IIClcd::scrollDisplayLeft() {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
void IIClcd::scrollDisplayRight() {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}

// This is for text that flows Left to Right
void IIClcd::leftToRight() {
_displaymode |= LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}

// This is for text that flows Right to Left
void IIClcd::rightToLeft() {
_displaymode &= ~LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}

// This will 'right justify' text from the cursor
void IIClcd::autoscroll() {
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}

// This will 'left justify' text from the cursor
void IIClcd::noAutoscroll() {
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
// void IIClcd::scrollDisplayLeft() {
// command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
// }
// void IIClcd::scrollDisplayRight() {
// command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
// }

// // This is for text that flows Left to Right
// void IIClcd::leftToRight() {
// _displaymode |= LCD_ENTRYLEFT;
// command(LCD_ENTRYMODESET | _displaymode);
// }

// // This is for text that flows Right to Left
// void IIClcd::rightToLeft() {
// _displaymode &= ~LCD_ENTRYLEFT;
// command(LCD_ENTRYMODESET | _displaymode);
// }

// // This will 'right justify' text from the cursor
// void IIClcd::autoscroll() {
// _displaymode |= LCD_ENTRYSHIFTINCREMENT;
// command(LCD_ENTRYMODESET | _displaymode);
// }

// // This will 'left justify' text from the cursor
// void IIClcd::noAutoscroll() {
// _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
// command(LCD_ENTRYMODESET | _displaymode);
// }

// Allows us to fill the first 8 CGRAM locations
// with custom characters
Expand Down Expand Up @@ -290,11 +296,13 @@ void IIClcd::write4bits(uint8_t value) {
}

void IIClcd::expanderWrite(uint8_t _data) {
uint8_t data = ((uint8_t)(_data) | _backlightval);
// twi_writeTo(_Addr, &data, 1, true, true);
Wire.beginTransmission(_Addr);
Wire.write(data);
Wire.endTransmission();
if(_displayFound) {
uint8_t data = ((uint8_t)(_data) | _backlightval);
// twi_writeTo(_Addr, &data, 1, true, true);
Wire.beginTransmission(_Addr);
Wire.write(data);
Wire.endTransmission();
}
}

void IIClcd::pulseEnable(uint8_t _data) {
Expand Down
3 changes: 3 additions & 0 deletions src/IicLcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class IIClcd : public Print {
uint16_t _backlightTime;
bool _bufferOnly;

bool _displayFound; // Needed since writes to a non-existant display can lock I2C bus on ESP32-S2 - see: https://github.com/espressif/arduino-esp32/issues/8480#issuecomment-1708909457


char content[4][21]; // always keep a copy of the display content in this variable
};

Expand Down

0 comments on commit 0842f97

Please sign in to comment.