Skip to content

Commit

Permalink
Touch: More robust initialisation and sleep
Browse files Browse the repository at this point in the history
Report mode is enforced.
Unknown chips are handles as CST816S.
Deep sleep is only used on the CST716.
Reset is duplicated to first interrupt, if reset pin does not work.
  • Loading branch information
StarGate01 committed Mar 25, 2022
1 parent 28a6930 commit 402ebae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ elseif(USE_OPENOCD)
message(" * Programmer/debugger : OpenOCD Client")
endif()
if(USE_DEBUG_PINS)
message(" * Debug Pins : Enabled")
message(" * Debug pins : Enabled")
else()
message(" * Debug Pins : Disabled")
message(" * Debug pins : Disabled")
endif()
if(BUILD_DFU)
message(" * Build DFU (using adafruit-nrfutil) : Enabled")
Expand Down
46 changes: 18 additions & 28 deletions src/drivers/Cst816s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@ Cst816S::Cst816S(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaste
bool Cst816S::Init() {
nrf_gpio_cfg_output(PinMap::Cst816sReset);
nrf_gpio_pin_clear(PinMap::Cst816sReset);
vTaskDelay(5);
vTaskDelay(10);
nrf_gpio_pin_set(PinMap::Cst816sReset);
vTaskDelay(50);

// Wake the touchpanel up
uint8_t dummy;
twiMaster.Read(twiAddress, 0x15, &dummy, 1);
vTaskDelay(5);
twiMaster.Read(twiAddress, 0xa7, &dummy, 1);
vTaskDelay(5);

// Read the device ids, and use the chip ID to know which variant is used.
ReadDeviceIds();

Expand All @@ -50,17 +43,28 @@ bool Cst816S::Init() {
[4] EnMotion - When the detected gesture is pulsed Low.
[0] OnceWLP - Press gesture only issue a pulse signal is low.
This configures the chip in report mode (regular interrupts), instead of gesture mode.
This configures the chip in report mode (regular interrupts during touch), instead of gesture mode.
0x60 = report mode, 0x11 = gesture mode, 0x71 = both.
Although the CST18S supports mode switching, the CST716 can only done one (permanently configured by the factory).
CST716 default is report mode.
*/
static constexpr uint8_t irqCtl = 0b01110000;

static constexpr uint8_t irqCtl = 0x70;
twiMaster.Write(twiAddress, 0xFA, &irqCtl, 1);

return true;
}

Cst816S::TouchInfos Cst816S::GetTouchInfo() {
// Some chips fail to initialise even though the reset pin has been toggled.
// The reset pin should wake them from auto-sleep, but it sometimes does not.
// They only provide a I2C communication window after a touch interrupt,
// so the first touch interrupt is used to force initialisation.
if(firstEvent) {
Init();
firstEvent = false;
}

Cst816S::TouchInfos info;
uint8_t touchData[7];

Expand Down Expand Up @@ -103,27 +107,13 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() {
}

void Cst816S::Sleep() {
// The Cst816S ignores this sleep command, as is has an auto-sleep function.
// The Cst816S does not need explicit sleep, as is has an auto-sleep function.
// During this auto-sleep, it can wake up by touch but has its I2C interface disabled.
// The Cst716 cannot wake up by touch, and can only come out of sleep by resetting it.
// The accelerometer can be used instead to generate wakeup events.

// Force a reset in case the chip went into auto-sleep mode
nrf_gpio_pin_clear(PinMap::Cst816sReset);
vTaskDelay(5);
nrf_gpio_pin_set(PinMap::Cst816sReset);
vTaskDelay(50);

// The CST816 and CST716 differ in their sleep register addresses
uint8_t sleepRegister = 0;
if(variant == Variant::Cst816S) {
sleepRegister = 0xE5;
} else if(variant == Variant::Cst716) {
sleepRegister = 0xA5;
}
static constexpr uint8_t sleepValue = 0x03;
if(variant != Variant::Unknown) {
twiMaster.Write(twiAddress, sleepRegister, &sleepValue, 1);
if(variant == Variant::Cst716) {
static constexpr uint8_t sleepValue = 0x03;
twiMaster.Write(twiAddress, 0xA5, &sleepValue, 1);
}

NRF_LOG_INFO("[TOUCHPANEL] Sleep");
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/Cst816s.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace Pinetime {
uint8_t vendorId;
uint8_t fwVersion;
Variant variant;

bool firstEvent = true;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/touchhandler/TouchHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool TouchHandler::GetNewTouchInfo() {
gesture = info.gesture;
info.touching = false;
}
} else if(touchPanel.GetVariant() == Pinetime::Drivers::Cst816S::Variant::Cst816S) {
} else { // Handle unknown chips as Cst816S
if (info.gesture != Pinetime::Drivers::Cst816S::Gestures::None) {
if (gestureReleased) {
if (info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideDown ||
Expand Down

0 comments on commit 402ebae

Please sign in to comment.