Skip to content

Commit ce83da6

Browse files
committed
Touch: More robust initialisation and sleep
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.
1 parent 57c986e commit ce83da6

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ elseif(USE_OPENOCD)
9898
message(" * Programmer/debugger : OpenOCD Client")
9999
endif()
100100
if(USE_DEBUG_PINS)
101-
message(" * Debug Pins : Enabled")
101+
message(" * Debug pins : Enabled")
102102
else()
103-
message(" * Debug Pins : Disabled")
103+
message(" * Debug pins : Disabled")
104104
endif()
105105
if(BUILD_DFU)
106106
message(" * Build DFU (using adafruit-nrfutil) : Enabled")

src/drivers/Cst816s.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,10 @@ Cst816S::Cst816S(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaste
2121
bool Cst816S::Init() {
2222
nrf_gpio_cfg_output(PinMap::Cst816sReset);
2323
nrf_gpio_pin_clear(PinMap::Cst816sReset);
24-
vTaskDelay(5);
24+
vTaskDelay(10);
2525
nrf_gpio_pin_set(PinMap::Cst816sReset);
2626
vTaskDelay(50);
2727

28-
// Wake the touchpanel up
29-
uint8_t dummy;
30-
twiMaster.Read(twiAddress, 0x15, &dummy, 1);
31-
vTaskDelay(5);
32-
twiMaster.Read(twiAddress, 0xa7, &dummy, 1);
33-
vTaskDelay(5);
34-
3528
// Read the device ids, and use the chip ID to know which variant is used.
3629
ReadDeviceIds();
3730

@@ -50,17 +43,28 @@ bool Cst816S::Init() {
5043
[4] EnMotion - When the detected gesture is pulsed Low.
5144
[0] OnceWLP - Press gesture only issue a pulse signal is low.
5245
53-
This configures the chip in report mode (regular interrupts), instead of gesture mode.
46+
This configures the chip in report mode (regular interrupts during touch), instead of gesture mode.
47+
0x60 = report mode, 0x11 = gesture mode, 0x71 = both.
5448
Although the CST18S supports mode switching, the CST716 can only done one (permanently configured by the factory).
5549
CST716 default is report mode.
5650
*/
57-
static constexpr uint8_t irqCtl = 0b01110000;
51+
52+
static constexpr uint8_t irqCtl = 0x70;
5853
twiMaster.Write(twiAddress, 0xFA, &irqCtl, 1);
5954

6055
return true;
6156
}
6257

6358
Cst816S::TouchInfos Cst816S::GetTouchInfo() {
59+
// Some chips fail to initialise even though the reset pin has been toggled.
60+
// The reset pin should wake them from auto-sleep, but it sometimes does not.
61+
// They only provide a I2C communication window after a touch interrupt,
62+
// so the first touch interrupt is used to force initialisation.
63+
if(firstEvent) {
64+
Init();
65+
firstEvent = false;
66+
}
67+
6468
Cst816S::TouchInfos info;
6569
uint8_t touchData[7];
6670

@@ -103,27 +107,13 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() {
103107
}
104108

105109
void Cst816S::Sleep() {
106-
// The Cst816S ignores this sleep command, as is has an auto-sleep function.
110+
// The Cst816S does not need explicit sleep, as is has an auto-sleep function.
107111
// During this auto-sleep, it can wake up by touch but has its I2C interface disabled.
108112
// The Cst716 cannot wake up by touch, and can only come out of sleep by resetting it.
109113
// The accelerometer can be used instead to generate wakeup events.
110-
111-
// Force a reset in case the chip went into auto-sleep mode
112-
nrf_gpio_pin_clear(PinMap::Cst816sReset);
113-
vTaskDelay(5);
114-
nrf_gpio_pin_set(PinMap::Cst816sReset);
115-
vTaskDelay(50);
116-
117-
// The CST816 and CST716 differ in their sleep register addresses
118-
uint8_t sleepRegister = 0;
119-
if(variant == Variant::Cst816S) {
120-
sleepRegister = 0xE5;
121-
} else if(variant == Variant::Cst716) {
122-
sleepRegister = 0xA5;
123-
}
124-
static constexpr uint8_t sleepValue = 0x03;
125-
if(variant != Variant::Unknown) {
126-
twiMaster.Write(twiAddress, sleepRegister, &sleepValue, 1);
114+
if(variant == Variant::Cst716) {
115+
static constexpr uint8_t sleepValue = 0x03;
116+
twiMaster.Write(twiAddress, 0xA5, &sleepValue, 1);
127117
}
128118

129119
NRF_LOG_INFO("[TOUCHPANEL] Sleep");

src/drivers/Cst816s.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ namespace Pinetime {
7878
uint8_t vendorId;
7979
uint8_t fwVersion;
8080
Variant variant;
81+
82+
bool firstEvent = true;
8183
};
8284

8385
}

src/touchhandler/TouchHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool TouchHandler::GetNewTouchInfo() {
4040
gesture = info.gesture;
4141
info.touching = false;
4242
}
43-
} else if(touchPanel.GetVariant() == Pinetime::Drivers::Cst816S::Variant::Cst816S) {
43+
} else { // Handle unknown chips as Cst816S
4444
if (info.gesture != Pinetime::Drivers::Cst816S::Gestures::None) {
4545
if (gestureReleased) {
4646
if (info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideDown ||

0 commit comments

Comments
 (0)