cirque_pinnacle.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright (c) 2018 Cirque Corp. Restrictions apply. See: www.cirque.com/sw-license
  2. // based on https://github.com/cirque-corp/Cirque_Pinnacle_1CA027/tree/master/Circular_Trackpad
  3. // with modifications and changes for QMK
  4. // refer to documentation: Gen2 and Gen3 (Pinnacle ASIC) at https://www.cirque.com/documentation
  5. #include "cirque_pinnacle.h"
  6. #include "wait.h"
  7. #include "timer.h"
  8. #include <stdlib.h>
  9. #ifndef CIRQUE_PINNACLE_ATTENUATION
  10. # ifdef CIRQUE_PINNACLE_CURVED_OVERLAY
  11. # define CIRQUE_PINNACLE_ATTENUATION EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_2X
  12. # else
  13. # define CIRQUE_PINNACLE_ATTENUATION EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_4X
  14. # endif
  15. #endif
  16. bool touchpad_init;
  17. uint16_t scale_data = CIRQUE_PINNACLE_DEFAULT_SCALE;
  18. void cirque_pinnacle_clear_flags(void);
  19. void cirque_pinnacle_enable_feed(bool feedEnable);
  20. void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count);
  21. void RAP_Write(uint8_t address, uint8_t data);
  22. #if CIRQUE_PINNACLE_POSITION_MODE
  23. /* Logical Scaling Functions */
  24. // Clips raw coordinates to "reachable" window of sensor
  25. // NOTE: values outside this window can only appear as a result of noise
  26. void ClipCoordinates(pinnacle_data_t* coordinates) {
  27. if (coordinates->xValue < CIRQUE_PINNACLE_X_LOWER) {
  28. coordinates->xValue = CIRQUE_PINNACLE_X_LOWER;
  29. } else if (coordinates->xValue > CIRQUE_PINNACLE_X_UPPER) {
  30. coordinates->xValue = CIRQUE_PINNACLE_X_UPPER;
  31. }
  32. if (coordinates->yValue < CIRQUE_PINNACLE_Y_LOWER) {
  33. coordinates->yValue = CIRQUE_PINNACLE_Y_LOWER;
  34. } else if (coordinates->yValue > CIRQUE_PINNACLE_Y_UPPER) {
  35. coordinates->yValue = CIRQUE_PINNACLE_Y_UPPER;
  36. }
  37. }
  38. #endif
  39. uint16_t cirque_pinnacle_get_scale(void) {
  40. return scale_data;
  41. }
  42. void cirque_pinnacle_set_scale(uint16_t scale) {
  43. scale_data = scale;
  44. }
  45. // Scales data to desired X & Y resolution
  46. void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResolution, uint16_t yResolution) {
  47. #if CIRQUE_PINNACLE_POSITION_MODE
  48. uint32_t xTemp = 0;
  49. uint32_t yTemp = 0;
  50. ClipCoordinates(coordinates);
  51. xTemp = coordinates->xValue;
  52. yTemp = coordinates->yValue;
  53. // translate coordinates to (0, 0) reference by subtracting edge-offset
  54. xTemp -= CIRQUE_PINNACLE_X_LOWER;
  55. yTemp -= CIRQUE_PINNACLE_Y_LOWER;
  56. // scale coordinates to (xResolution, yResolution) range
  57. coordinates->xValue = (uint16_t)(xTemp * xResolution / CIRQUE_PINNACLE_X_RANGE);
  58. coordinates->yValue = (uint16_t)(yTemp * yResolution / CIRQUE_PINNACLE_Y_RANGE);
  59. #else
  60. int32_t xTemp = 0, yTemp = 0;
  61. ldiv_t temp;
  62. static int32_t xRemainder, yRemainder;
  63. temp = ldiv(((int32_t)coordinates->xDelta) * (int32_t)xResolution + xRemainder, (int32_t)CIRQUE_PINNACLE_X_RANGE);
  64. xTemp = temp.quot;
  65. xRemainder = temp.rem;
  66. temp = ldiv(((int32_t)coordinates->yDelta) * (int32_t)yResolution + yRemainder, (int32_t)CIRQUE_PINNACLE_Y_RANGE);
  67. yTemp = temp.quot;
  68. yRemainder = temp.rem;
  69. coordinates->xDelta = (int16_t)xTemp;
  70. coordinates->yDelta = (int16_t)yTemp;
  71. #endif
  72. }
  73. // Clears Status1 register flags (SW_CC and SW_DR)
  74. void cirque_pinnacle_clear_flags() {
  75. RAP_Write(HOSTREG__STATUS1, HOSTREG__STATUS1_DEFVAL & ~(HOSTREG__STATUS1__COMMAND_COMPLETE | HOSTREG__STATUS1__DATA_READY));
  76. wait_us(50);
  77. }
  78. // Enables/Disables the feed
  79. void cirque_pinnacle_enable_feed(bool feedEnable) {
  80. uint8_t feedconfig1;
  81. RAP_ReadBytes(HOSTREG__FEEDCONFIG1, &feedconfig1, 1);
  82. if (feedEnable) {
  83. feedconfig1 |= HOSTREG__FEEDCONFIG1__FEED_ENABLE;
  84. } else {
  85. feedconfig1 &= ~HOSTREG__FEEDCONFIG1__FEED_ENABLE;
  86. }
  87. RAP_Write(HOSTREG__FEEDCONFIG1, feedconfig1);
  88. }
  89. /* ERA (Extended Register Access) Functions */
  90. // Reads <count> bytes from an extended register at <address> (16-bit address),
  91. // stores values in <*data>
  92. void ERA_ReadBytes(uint16_t address, uint8_t* data, uint16_t count) {
  93. uint8_t ERAControlValue = 0xFF;
  94. uint16_t timeout_timer;
  95. cirque_pinnacle_enable_feed(false); // Disable feed
  96. RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_HIGH, (uint8_t)(address >> 8)); // Send upper byte of ERA address
  97. RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_LOW, (uint8_t)(address & 0x00FF)); // Send lower byte of ERA address
  98. for (uint16_t i = 0; i < count; i++) {
  99. RAP_Write(HOSTREG__EXT_REG_AXS_CTRL, HOSTREG__EREG_AXS__INC_ADDR_READ | HOSTREG__EREG_AXS__READ); // Signal ERA-read (auto-increment) to Pinnacle
  100. // Wait for status register 0x1E to clear
  101. timeout_timer = timer_read();
  102. do {
  103. RAP_ReadBytes(HOSTREG__EXT_REG_AXS_CTRL, &ERAControlValue, 1);
  104. } while ((ERAControlValue != 0x00) && (timer_elapsed(timeout_timer) <= CIRQUE_PINNACLE_TIMEOUT));
  105. RAP_ReadBytes(HOSTREG__EXT_REG_AXS_VALUE, data + i, 1);
  106. cirque_pinnacle_clear_flags();
  107. }
  108. }
  109. // Writes a byte, <data>, to an extended register at <address> (16-bit address)
  110. void ERA_WriteByte(uint16_t address, uint8_t data) {
  111. uint8_t ERAControlValue = 0xFF;
  112. uint16_t timeout_timer;
  113. cirque_pinnacle_enable_feed(false); // Disable feed
  114. RAP_Write(HOSTREG__EXT_REG_AXS_VALUE, data); // Send data byte to be written
  115. RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_HIGH, (uint8_t)(address >> 8)); // Upper byte of ERA address
  116. RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_LOW, (uint8_t)(address & 0x00FF)); // Lower byte of ERA address
  117. RAP_Write(HOSTREG__EXT_REG_AXS_CTRL, HOSTREG__EREG_AXS__WRITE); // Signal an ERA-write to Pinnacle
  118. // Wait for status register 0x1E to clear
  119. timeout_timer = timer_read();
  120. do {
  121. RAP_ReadBytes(HOSTREG__EXT_REG_AXS_CTRL, &ERAControlValue, 1);
  122. } while ((ERAControlValue != 0x00) && (timer_elapsed(timeout_timer) <= CIRQUE_PINNACLE_TIMEOUT));
  123. cirque_pinnacle_clear_flags();
  124. }
  125. bool cirque_pinnacle_set_adc_attenuation(uint8_t adcGain) {
  126. uint8_t adcconfig = 0x00;
  127. ERA_ReadBytes(EXTREG__TRACK_ADCCONFIG, &adcconfig, 1);
  128. adcGain &= EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_MASK;
  129. if (adcGain == (adcconfig & EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_MASK)) {
  130. return false;
  131. }
  132. adcconfig &= ~EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_MASK;
  133. adcconfig |= adcGain;
  134. ERA_WriteByte(EXTREG__TRACK_ADCCONFIG, adcconfig);
  135. ERA_ReadBytes(EXTREG__TRACK_ADCCONFIG, &adcconfig, 1);
  136. return true;
  137. }
  138. // Changes thresholds to improve detection of fingers
  139. // Not needed for flat overlay?
  140. void cirque_pinnacle_tune_edge_sensitivity(void) {
  141. uint8_t widezmin = 0x00;
  142. ERA_ReadBytes(EXTREG__XAXIS_WIDEZMIN, &widezmin, 1);
  143. ERA_WriteByte(EXTREG__XAXIS_WIDEZMIN, 0x04); // magic number from Cirque sample code
  144. ERA_ReadBytes(EXTREG__XAXIS_WIDEZMIN, &widezmin, 1);
  145. ERA_ReadBytes(EXTREG__YAXIS_WIDEZMIN, &widezmin, 1);
  146. ERA_WriteByte(EXTREG__YAXIS_WIDEZMIN, 0x03); // magic number from Cirque sample code
  147. ERA_ReadBytes(EXTREG__YAXIS_WIDEZMIN, &widezmin, 1);
  148. }
  149. // Perform calibration
  150. void cirque_pinnacle_calibrate(void) {
  151. uint8_t calconfig;
  152. uint16_t timeout_timer;
  153. RAP_ReadBytes(HOSTREG__CALCONFIG1, &calconfig, 1);
  154. calconfig |= HOSTREG__CALCONFIG1__CALIBRATE;
  155. RAP_Write(HOSTREG__CALCONFIG1, calconfig);
  156. // Calibration takes ~100ms according to GT-AN-090624, doubling the timeout just to be safe
  157. timeout_timer = timer_read();
  158. do {
  159. RAP_ReadBytes(HOSTREG__CALCONFIG1, &calconfig, 1);
  160. } while ((calconfig & HOSTREG__CALCONFIG1__CALIBRATE) && (timer_elapsed(timeout_timer) <= 200));
  161. cirque_pinnacle_clear_flags();
  162. }
  163. // Enable/disable cursor smoothing, smoothing is enabled by default
  164. void cirque_pinnacle_cursor_smoothing(bool enable) {
  165. uint8_t feedconfig3;
  166. RAP_ReadBytes(HOSTREG__FEEDCONFIG3, &feedconfig3, 1);
  167. if (enable) {
  168. feedconfig3 &= ~HOSTREG__FEEDCONFIG3__DISABLE_CROSS_RATE_SMOOTHING;
  169. } else {
  170. feedconfig3 |= HOSTREG__FEEDCONFIG3__DISABLE_CROSS_RATE_SMOOTHING;
  171. }
  172. RAP_Write(HOSTREG__FEEDCONFIG3, feedconfig3);
  173. }
  174. /* Pinnacle-based TM040040/TM035035/TM023023 Functions */
  175. void cirque_pinnacle_init(void) {
  176. #if defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
  177. spi_init();
  178. #elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c)
  179. i2c_init();
  180. #endif
  181. touchpad_init = true;
  182. // send a RESET command now, in case QMK had a soft-reset without a power cycle
  183. RAP_Write(HOSTREG__SYSCONFIG1, HOSTREG__SYSCONFIG1__RESET);
  184. wait_ms(30); // Pinnacle needs 10-15ms to boot, so wait long enough before configuring
  185. RAP_Write(HOSTREG__SYSCONFIG1, HOSTREG__SYSCONFIG1_DEFVAL);
  186. wait_us(50);
  187. // Host clears SW_CC flag
  188. cirque_pinnacle_clear_flags();
  189. #if CIRQUE_PINNACLE_POSITION_MODE
  190. RAP_Write(HOSTREG__FEEDCONFIG2, HOSTREG__FEEDCONFIG2_DEFVAL);
  191. #else
  192. // FeedConfig2 (Feature flags for Relative Mode Only)
  193. uint8_t feedconfig2 = HOSTREG__FEEDCONFIG2__GLIDE_EXTEND_DISABLE | HOSTREG__FEEDCONFIG2__INTELLIMOUSE_MODE;
  194. # if !defined(CIRQUE_PINNACLE_TAP_ENABLE)
  195. feedconfig2 |= HOSTREG__FEEDCONFIG2__ALL_TAP_DISABLE;
  196. # endif
  197. # if !defined(CIRQUE_PINNACLE_SECONDARY_TAP_ENABLE)
  198. feedconfig2 |= HOSTREG__FEEDCONFIG2__SECONDARY_TAP_DISABLE;
  199. # elif !defined(CIRQUE_PINNACLE_TAP_ENABLE)
  200. # error CIRQUE_PINNACLE_TAP_ENABLE must be defined for CIRQUE_PINNACLE_SECONDARY_TAP_ENABLE to work
  201. # endif
  202. # if !defined(CIRQUE_PINNACLE_SIDE_SCROLL_ENABLE)
  203. feedconfig2 |= HOSTREG__FEEDCONFIG2__SCROLL_DISABLE;
  204. # endif
  205. RAP_Write(HOSTREG__FEEDCONFIG2, feedconfig2);
  206. #endif
  207. // FeedConfig1 (Data Output Flags)
  208. RAP_Write(HOSTREG__FEEDCONFIG1, CIRQUE_PINNACLE_POSITION_MODE ? HOSTREG__FEEDCONFIG1__DATA_TYPE__REL0_ABS1 : HOSTREG__FEEDCONFIG1_DEFVAL);
  209. #if CIRQUE_PINNACLE_POSITION_MODE
  210. // Host sets z-idle packet count to 5 (default is 0x1E/30)
  211. RAP_Write(HOSTREG__ZIDLE, 5);
  212. #endif
  213. bool calibrate = cirque_pinnacle_set_adc_attenuation(CIRQUE_PINNACLE_ATTENUATION);
  214. #ifdef CIRQUE_PINNACLE_CURVED_OVERLAY
  215. cirque_pinnacle_tune_edge_sensitivity();
  216. calibrate = true;
  217. #endif
  218. if (calibrate) {
  219. // Force a calibration after setting ADC attenuation
  220. cirque_pinnacle_calibrate();
  221. }
  222. cirque_pinnacle_enable_feed(true);
  223. }
  224. pinnacle_data_t cirque_pinnacle_read_data(void) {
  225. uint8_t data_ready = 0;
  226. uint8_t data[6] = {0};
  227. pinnacle_data_t result = {0};
  228. // Check if there is valid data available
  229. RAP_ReadBytes(HOSTREG__STATUS1, &data_ready, 1);
  230. if ((data_ready & HOSTREG__STATUS1__DATA_READY) == 0) {
  231. // no data available yet
  232. result.valid = false; // be explicit
  233. return result;
  234. }
  235. // Read all data bytes
  236. RAP_ReadBytes(HOSTREG__PACKETBYTE_0, data, 6);
  237. // Get ready for the next data sample
  238. cirque_pinnacle_clear_flags();
  239. #if CIRQUE_PINNACLE_POSITION_MODE
  240. // Decode data for absolute mode
  241. // Register 0x13 is unused in this mode (palm detection area)
  242. result.buttonFlags = data[0] & 0x3F; // bit0 to bit5 are switch 0-5, only hardware button presses (from input pin on the Pinnacle chip)
  243. result.xValue = data[2] | ((data[4] & 0x0F) << 8); // merge high and low bits for X
  244. result.yValue = data[3] | ((data[4] & 0xF0) << 4); // merge high and low bits for Y
  245. result.zValue = data[5] & 0x3F; // Z is only lower 6 bits, upper 2 bits are reserved/unused
  246. result.touchDown = (result.xValue != 0 || result.yValue != 0); // (0,0) is a "magic coordinate" to indicate "finger touched down"
  247. #else
  248. // Decode data for relative mode
  249. // Registers 0x16 and 0x17 are unused in this mode
  250. result.buttons = data[0] & 0x07; // Only three buttons are supported
  251. if ((data[0] & 0x10) && data[1] != 0) {
  252. result.xDelta = -((int16_t)256 - (int16_t)(data[1]));
  253. } else {
  254. result.xDelta = data[1];
  255. }
  256. if ((data[0] & 0x20) && data[2] != 0) {
  257. result.yDelta = ((int16_t)256 - (int16_t)(data[2]));
  258. } else {
  259. result.yDelta = -((int16_t)data[2]);
  260. }
  261. result.wheelCount = ((int8_t*)data)[3];
  262. #endif
  263. result.valid = true;
  264. return result;
  265. }