You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
12 KiB

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