|
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 && |
85 | | - gesture != Gestures::SlideDown && |
86 | | - gesture != Gestures::SlideUp && |
87 | | - gesture != Gestures::SlideLeft && |
88 | | - gesture != Gestures::SlideRight && |
89 | | - gesture != Gestures::SingleTap && |
90 | | - gesture != Gestures::DoubleTap && |
91 | | - gesture != Gestures::LongPress)) { |
92 | | - 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) |
93 | 75 | return info; |
94 | | - } |
95 | 76 |
|
96 | | - info.x = x; |
97 | | - info.y = y; |
98 | | - info.touching = (nbTouchPoints > 0); |
99 | | - info.gesture = gesture; |
100 | | - 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 | + |
101 | 91 | return info; |
102 | 92 | } |
103 | 93 |
|
104 | 94 | void Cst816S::Sleep() { |
105 | | - nrf_gpio_pin_clear(PinMap::Cst816sReset); |
106 | | - vTaskDelay(5); |
107 | | - nrf_gpio_pin_set(PinMap::Cst816sReset); |
108 | | - vTaskDelay(50); |
109 | | - static constexpr uint8_t sleepValue = 0x03; |
110 | | - 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 | + |
111 | 101 | NRF_LOG_INFO("[TOUCHPANEL] Sleep"); |
112 | 102 | } |
113 | 103 |
|
114 | 104 | void Cst816S::Wakeup() { |
115 | 105 | Init(); |
116 | 106 | NRF_LOG_INFO("[TOUCHPANEL] Wakeup"); |
117 | 107 | } |
118 | | - |
119 | | -bool Cst816S::CheckDeviceIds() { |
120 | | - // There's mixed information about which register contains which information |
121 | | - if (twiMaster.Read(twiAddress, 0xA7, &chipId, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
122 | | - chipId = 0xFF; |
123 | | - return false; |
124 | | - } |
125 | | - if (twiMaster.Read(twiAddress, 0xA8, &vendorId, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
126 | | - vendorId = 0xFF; |
127 | | - return false; |
128 | | - } |
129 | | - if (twiMaster.Read(twiAddress, 0xA9, &fwVersion, 1) == TwiMaster::ErrorCodes::TransactionFailed) { |
130 | | - fwVersion = 0xFF; |
131 | | - return false; |
132 | | - } |
133 | | - |
134 | | - return chipId == 0xb4 && vendorId == 0 && fwVersion == 1; |
135 | | -} |
0 commit comments