|
1 | 1 | #include "drivers/Cst816s.h"
|
| 2 | +#include "drivers/PinMap.h" |
2 | 3 | #include <FreeRTOS.h>
|
3 | 4 | #include <legacy/nrf_drv_gpiote.h>
|
4 | 5 | #include <nrfx_log.h>
|
5 | 6 | #include <task.h>
|
6 |
| -#include "drivers/PinMap.h" |
7 | 7 |
|
8 | 8 | using namespace Pinetime::Drivers;
|
9 | 9 |
|
10 |
| -/* References : |
| 10 | +/* |
| 11 | + * References : |
11 | 12 | * This implementation is based on this article :
|
12 | 13 | * https://medium.com/@ly.lee/building-a-rust-driver-for-pinetimes-touch-controller-cbc1a5d5d3e9 Touch panel datasheet (weird chinese
|
13 | 14 | * translation) : https://wiki.pine64.org/images/5/51/CST816S%E6%95%B0%E6%8D%AE%E6%89%8B%E5%86%8CV1.1.en.pdf
|
14 | 15 | *
|
15 |
| - * TODO : we need a complete datasheet and protocol reference! |
| 16 | + * TODO: We need a complete datasheet and protocol reference! |
| 17 | + * For register desciptions, see Cst816s_registers.h. Information was collected from various chinese datasheets and documents. |
16 | 18 | * */
|
17 | 19 |
|
18 | 20 | Cst816S::Cst816S(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, twiAddress {twiAddress} {
|
19 | 21 | }
|
20 | 22 |
|
21 | 23 | bool Cst816S::Init() {
|
| 24 | + // Reset the touch driver |
22 | 25 | nrf_gpio_cfg_output(PinMap::Cst816sReset);
|
23 | 26 | nrf_gpio_pin_clear(PinMap::Cst816sReset);
|
24 |
| - vTaskDelay(5); |
| 27 | + vTaskDelay(10); |
25 | 28 | nrf_gpio_pin_set(PinMap::Cst816sReset);
|
26 | 29 | vTaskDelay(50);
|
27 | 30 |
|
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 |
| - |
35 |
| - // TODO This function check that the device IDs from the controller are equal to the ones |
36 |
| - // we expect. However, it seems to return false positive (probably in case of communication issue). |
37 |
| - // Also, it seems that some users have pinetimes that works correctly but that report different device IDs |
38 |
| - // Until we know more about this, we'll just read the IDs but not take any action in case they are not 'valid' |
39 |
| - CheckDeviceIds(); |
40 |
| - |
41 |
| - /* |
42 |
| - [2] EnConLR - Continuous operation can slide around |
43 |
| - [1] EnConUD - Slide up and down to enable continuous operation |
44 |
| - [0] EnDClick - Enable Double-click action |
45 |
| - */ |
46 |
| - static constexpr uint8_t motionMask = 0b00000101; |
47 |
| - twiMaster.Write(twiAddress, 0xEC, &motionMask, 1); |
48 |
| - |
49 |
| - /* |
50 |
| - [7] EnTest - Interrupt pin to test, enable automatic periodic issued after a low pulse. |
51 |
| - [6] EnTouch - When a touch is detected, a periodic pulsed Low. |
52 |
| - [5] EnChange - Upon detecting a touch state changes, pulsed Low. |
53 |
| - [4] EnMotion - When the detected gesture is pulsed Low. |
54 |
| - [0] OnceWLP - Press gesture only issue a pulse signal is low. |
55 |
| - */ |
56 |
| - static constexpr uint8_t irqCtl = 0b01110000; |
57 |
| - twiMaster.Write(twiAddress, 0xFA, &irqCtl, 1); |
| 31 | + // Chip ID is suspected to be dependent on embedded firmware and factory configuration, |
| 32 | + // and not (just) on hardware type and revision. |
| 33 | + if (twiMaster.Read(twiAddress, CHIP_ID, &chipId, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
| 34 | + chipId = 0xFF; |
| 35 | + } |
| 36 | + // Vendor / project ID and firmware version can vary between devices. |
| 37 | + if (twiMaster.Read(twiAddress, PROJ_ID, &vendorId, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
| 38 | + vendorId = 0xFF; |
| 39 | + } |
| 40 | + if (twiMaster.Read(twiAddress, FW_VERSION, &fwVersion, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
| 41 | + fwVersion = 0xFF; |
| 42 | + } |
| 43 | + |
| 44 | + // These configuration settings will be ignored by chips which were |
| 45 | + // fused / pre-configured in the factory (GESTURE and REPORT settings). |
| 46 | + // This mainly applies to CST716, however there may be CST816S with static configurations as well. |
| 47 | + // The other, freely configureable ones (DYNAMIC), are configured in reporting mode here. |
| 48 | + |
| 49 | + // Configure motion behaviour |
| 50 | + static constexpr uint8_t motionMask = MOTION_MASK_EN_DCLICK | MOTION_MASK_EN_CON_UD | MOTION_MASK_EN_CON_LR; |
| 51 | + twiMaster.Write(twiAddress, MOTION_MASK, &motionMask, 1); |
| 52 | + |
| 53 | + // Configure interrupt generating events |
| 54 | + static constexpr uint8_t irqCtl = IRQ_CTL_EN_MOTION | IRQ_CTL_EN_CHANGE | IRQ_CTL_EN_TOUCH; |
| 55 | + twiMaster.Write(twiAddress, IRQ_CTL, &irqCtl, 1); |
58 | 56 |
|
59 | 57 | return true;
|
60 | 58 | }
|
61 | 59 |
|
62 | 60 | Cst816S::TouchInfos Cst816S::GetTouchInfo() {
|
63 |
| - Cst816S::TouchInfos info; |
64 |
| - uint8_t touchData[7]; |
65 |
| - |
66 |
| - auto ret = twiMaster.Read(twiAddress, 0, touchData, sizeof(touchData)); |
67 |
| - if (ret != TwiMaster::ErrorCodes::NoError) { |
68 |
| - info.isValid = false; |
69 |
| - return info; |
| 61 | + // Some chips fail to wake up even though the reset pin has been toggled. |
| 62 | + // They only provide an I2C communication window after a touch interrupt, |
| 63 | + // so the first touch interrupt is used to force initialisation. |
| 64 | + if (firstEvent) { |
| 65 | + Init(); |
| 66 | + firstEvent = false; |
| 67 | + // The data registers should now be reset, so this touch event will be detected as invalid. |
70 | 68 | }
|
71 | 69 |
|
72 |
| - // This can only be 0 or 1 |
73 |
| - uint8_t nbTouchPoints = touchData[touchPointNumIndex] & 0x0f; |
74 |
| - uint8_t xHigh = touchData[touchXHighIndex] & 0x0f; |
75 |
| - uint8_t xLow = touchData[touchXLowIndex]; |
76 |
| - uint16_t x = (xHigh << 8) | xLow; |
77 |
| - uint8_t yHigh = touchData[touchYHighIndex] & 0x0f; |
78 |
| - uint8_t yLow = touchData[touchYLowIndex]; |
79 |
| - uint16_t y = (yHigh << 8) | yLow; |
80 |
| - Gestures gesture = static_cast<Gestures>(touchData[gestureIndex]); |
81 |
| - |
82 |
| - // Validity check |
83 |
| - if (x >= maxX || y >= maxY || |
84 |
| - (gesture != Gestures::None && gesture != Gestures::SlideDown && gesture != Gestures::SlideUp && gesture != Gestures::SlideLeft && |
85 |
| - gesture != Gestures::SlideRight && gesture != Gestures::SingleTap && gesture != Gestures::DoubleTap && |
86 |
| - gesture != Gestures::LongPress)) { |
87 |
| - info.isValid = false; |
| 70 | + // Read gesture metadata and first touch point coordinate block |
| 71 | + Cst816S::TouchInfos info; |
| 72 | + uint8_t touchData[P1_Y_POS_L + 1]; |
| 73 | + auto ret = twiMaster.Read(twiAddress, 0x00, touchData, sizeof(touchData)); |
| 74 | + if (ret != TwiMaster::ErrorCodes::NoError) |
88 | 75 | return info;
|
89 |
| - } |
90 | 76 |
|
91 |
| - info.x = x; |
92 |
| - info.y = y; |
93 |
| - info.touching = (nbTouchPoints > 0); |
94 |
| - info.gesture = gesture; |
95 |
| - info.isValid = true; |
| 77 | + // Assemble 12 bit point coordinates from lower 8 bits and higher 4 bits |
| 78 | + info.x = ((touchData[P1_X_POS_H] & POS_H_POS_MASK) << 8) | touchData[P1_X_POS_L]; |
| 79 | + info.y = ((touchData[P1_Y_POS_H] & POS_H_POS_MASK) << 8) | touchData[P1_Y_POS_L]; |
| 80 | + // Evaluate number of touch points |
| 81 | + info.touching = (touchData[TD_STATUS] & TD_STATUS_MASK) > 0; |
| 82 | + // Decode gesture ID |
| 83 | + info.gesture = static_cast<Gestures>(touchData[GESTURE_ID]); |
| 84 | + |
| 85 | + // Validity check, verify value ranges |
| 86 | + info.isValid = (info.x < maxX && info.y < maxY && |
| 87 | + (info.gesture == Gestures::None || info.gesture == Gestures::SlideDown || info.gesture == Gestures::SlideUp || |
| 88 | + info.gesture == Gestures::SlideLeft || info.gesture == Gestures::SlideRight || info.gesture == Gestures::SingleTap || |
| 89 | + info.gesture == Gestures::DoubleTap || info.gesture == Gestures::LongPress)); |
| 90 | + |
96 | 91 | return info;
|
97 | 92 | }
|
98 | 93 |
|
99 | 94 | void Cst816S::Sleep() {
|
100 |
| - nrf_gpio_pin_clear(PinMap::Cst816sReset); |
101 |
| - vTaskDelay(5); |
102 |
| - nrf_gpio_pin_set(PinMap::Cst816sReset); |
103 |
| - vTaskDelay(50); |
104 |
| - static constexpr uint8_t sleepValue = 0x03; |
105 |
| - twiMaster.Write(twiAddress, 0xA5, &sleepValue, 1); |
| 95 | + // This only controls the CST716, the CST816S will ignore this register. |
| 96 | + // The CST816S power state is managed using auto-sleep. |
| 97 | + |
| 98 | + static constexpr uint8_t sleepValue = PWR_MODE_DEEP_SLEEP; |
| 99 | + twiMaster.Write(twiAddress, PWR_MODE_CST716, &sleepValue, 1); |
| 100 | + |
106 | 101 | NRF_LOG_INFO("[TOUCHPANEL] Sleep");
|
107 | 102 | }
|
108 | 103 |
|
109 | 104 | void Cst816S::Wakeup() {
|
110 | 105 | Init();
|
111 | 106 | NRF_LOG_INFO("[TOUCHPANEL] Wakeup");
|
112 | 107 | }
|
113 |
| - |
114 |
| -bool Cst816S::CheckDeviceIds() { |
115 |
| - // There's mixed information about which register contains which information |
116 |
| - if (twiMaster.Read(twiAddress, 0xA7, &chipId, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
117 |
| - chipId = 0xFF; |
118 |
| - return false; |
119 |
| - } |
120 |
| - if (twiMaster.Read(twiAddress, 0xA8, &vendorId, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
121 |
| - vendorId = 0xFF; |
122 |
| - return false; |
123 |
| - } |
124 |
| - if (twiMaster.Read(twiAddress, 0xA9, &fwVersion, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
125 |
| - fwVersion = 0xFF; |
126 |
| - return false; |
127 |
| - } |
128 |
| - |
129 |
| - return chipId == 0xb4 && vendorId == 0 && fwVersion == 1; |
130 |
| -} |
0 commit comments