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.

238 lines
7.7 KiB

  1. // Copyright (c) 2018 Cirque Corp. Restrictions apply. See: www.cirque.com/sw-license
  2. #include "cirque_pinnacle.h"
  3. #include "print.h"
  4. #include "debug.h"
  5. #include "wait.h"
  6. // Registers for RAP
  7. // clang-format off
  8. #define FIRMWARE_ID 0x00
  9. #define FIRMWARE_VERSION_C 0x01
  10. #define STATUS_1 0x02
  11. #define SYSCONFIG_1 0x03
  12. #define FEEDCONFIG_1 0x04
  13. #define FEEDCONFIG_2 0x05
  14. #define CALIBRATION_CONFIG_1 0x07
  15. #define PS2_AU_CONTROL 0x08
  16. #define SAMPLE_RATE 0x09
  17. #define Z_IDLE_COUNT 0x0A
  18. #define Z_SCALER 0x0B
  19. #define SLEEP_INTERVAL 0x0C
  20. #define SLEEP_TIMER 0x0D
  21. #define PACKET_BYTE_0 0x12
  22. #define PACKET_BYTE_1 0x13
  23. #define PACKET_BYTE_2 0x14
  24. #define PACKET_BYTE_3 0x15
  25. #define PACKET_BYTE_4 0x16
  26. #define PACKET_BYTE_5 0x17
  27. #define ERA_VALUE 0x1B
  28. #define ERA_HIGH_BYTE 0x1C
  29. #define ERA_LOW_BYTE 0x1D
  30. #define ERA_CONTROL 0x1E
  31. // ADC-attenuation settings (held in BIT_7 and BIT_6)
  32. // 1X = most sensitive, 4X = least sensitive
  33. #define ADC_ATTENUATE_1X 0x00
  34. #define ADC_ATTENUATE_2X 0x40
  35. #define ADC_ATTENUATE_3X 0x80
  36. #define ADC_ATTENUATE_4X 0xC0
  37. // Register config values for this demo
  38. #define SYSCONFIG_1_VALUE 0x00
  39. #define FEEDCONFIG_1_VALUE 0x03 // 0x03 for absolute mode 0x01 for relative mode
  40. #define FEEDCONFIG_2_VALUE 0x1C // 0x1F for normal functionality 0x1E for intellimouse disabled
  41. #define Z_IDLE_COUNT_VALUE 0x05
  42. // clang-format on
  43. bool touchpad_init;
  44. uint16_t scale_data = 1024;
  45. void cirque_pinnacle_clear_flags(void);
  46. void cirque_pinnacle_enable_feed(bool feedEnable);
  47. void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count);
  48. void RAP_Write(uint8_t address, uint8_t data);
  49. #ifdef CONSOLE_ENABLE
  50. void print_byte(uint8_t byte) {
  51. 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'));
  52. }
  53. #endif
  54. /* Logical Scaling Functions */
  55. // Clips raw coordinates to "reachable" window of sensor
  56. // NOTE: values outside this window can only appear as a result of noise
  57. void ClipCoordinates(pinnacle_data_t* coordinates) {
  58. if (coordinates->xValue < CIRQUE_PINNACLE_X_LOWER) {
  59. coordinates->xValue = CIRQUE_PINNACLE_X_LOWER;
  60. } else if (coordinates->xValue > CIRQUE_PINNACLE_X_UPPER) {
  61. coordinates->xValue = CIRQUE_PINNACLE_X_UPPER;
  62. }
  63. if (coordinates->yValue < CIRQUE_PINNACLE_Y_LOWER) {
  64. coordinates->yValue = CIRQUE_PINNACLE_Y_LOWER;
  65. } else if (coordinates->yValue > CIRQUE_PINNACLE_Y_UPPER) {
  66. coordinates->yValue = CIRQUE_PINNACLE_Y_UPPER;
  67. }
  68. }
  69. uint16_t cirque_pinnacle_get_scale(void) {
  70. return scale_data;
  71. }
  72. void cirque_pinnacle_set_scale(uint16_t scale) {
  73. scale_data = scale;
  74. }
  75. // Scales data to desired X & Y resolution
  76. void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResolution, uint16_t yResolution) {
  77. uint32_t xTemp = 0;
  78. uint32_t yTemp = 0;
  79. ClipCoordinates(coordinates);
  80. xTemp = coordinates->xValue;
  81. yTemp = coordinates->yValue;
  82. // translate coordinates to (0, 0) reference by subtracting edge-offset
  83. xTemp -= CIRQUE_PINNACLE_X_LOWER;
  84. yTemp -= CIRQUE_PINNACLE_Y_LOWER;
  85. // scale coordinates to (xResolution, yResolution) range
  86. coordinates->xValue = (uint16_t)(xTemp * xResolution / CIRQUE_PINNACLE_X_RANGE);
  87. coordinates->yValue = (uint16_t)(yTemp * yResolution / CIRQUE_PINNACLE_Y_RANGE);
  88. }
  89. // Clears Status1 register flags (SW_CC and SW_DR)
  90. void cirque_pinnacle_clear_flags() {
  91. RAP_Write(STATUS_1, 0x00);
  92. wait_us(50);
  93. }
  94. // Enables/Disables the feed
  95. void cirque_pinnacle_enable_feed(bool feedEnable) {
  96. uint8_t temp;
  97. RAP_ReadBytes(FEEDCONFIG_1, &temp, 1); // Store contents of FeedConfig1 register
  98. if (feedEnable) {
  99. temp |= 0x01; // Set Feed Enable bit
  100. RAP_Write(0x04, temp);
  101. } else {
  102. temp &= ~0x01; // Clear Feed Enable bit
  103. RAP_Write(0x04, temp);
  104. }
  105. }
  106. /* ERA (Extended Register Access) Functions */
  107. // Reads <count> bytes from an extended register at <address> (16-bit address),
  108. // stores values in <*data>
  109. void ERA_ReadBytes(uint16_t address, uint8_t* data, uint16_t count) {
  110. uint8_t ERAControlValue = 0xFF;
  111. cirque_pinnacle_enable_feed(false); // Disable feed
  112. RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Send upper byte of ERA address
  113. RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Send lower byte of ERA address
  114. for (uint16_t i = 0; i < count; i++) {
  115. RAP_Write(ERA_CONTROL, 0x05); // Signal ERA-read (auto-increment) to Pinnacle
  116. // Wait for status register 0x1E to clear
  117. do {
  118. RAP_ReadBytes(ERA_CONTROL, &ERAControlValue, 1);
  119. } while (ERAControlValue != 0x00);
  120. RAP_ReadBytes(ERA_VALUE, data + i, 1);
  121. cirque_pinnacle_clear_flags();
  122. }
  123. }
  124. // Writes a byte, <data>, to an extended register at <address> (16-bit address)
  125. void ERA_WriteByte(uint16_t address, uint8_t data) {
  126. uint8_t ERAControlValue = 0xFF;
  127. cirque_pinnacle_enable_feed(false); // Disable feed
  128. RAP_Write(ERA_VALUE, data); // Send data byte to be written
  129. RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Upper byte of ERA address
  130. RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Lower byte of ERA address
  131. RAP_Write(ERA_CONTROL, 0x02); // Signal an ERA-write to Pinnacle
  132. // Wait for status register 0x1E to clear
  133. do {
  134. RAP_ReadBytes(ERA_CONTROL, &ERAControlValue, 1);
  135. } while (ERAControlValue != 0x00);
  136. cirque_pinnacle_clear_flags();
  137. }
  138. void cirque_pinnacle_set_adc_attenuation(uint8_t adcGain) {
  139. uint8_t temp = 0x00;
  140. ERA_ReadBytes(0x0187, &temp, 1);
  141. temp &= 0x3F; // clear top two bits
  142. temp |= adcGain;
  143. ERA_WriteByte(0x0187, temp);
  144. ERA_ReadBytes(0x0187, &temp, 1);
  145. }
  146. // Changes thresholds to improve detection of fingers
  147. void cirque_pinnacle_tune_edge_sensitivity(void) {
  148. uint8_t temp = 0x00;
  149. ERA_ReadBytes(0x0149, &temp, 1);
  150. ERA_WriteByte(0x0149, 0x04);
  151. ERA_ReadBytes(0x0149, &temp, 1);
  152. ERA_ReadBytes(0x0168, &temp, 1);
  153. ERA_WriteByte(0x0168, 0x03);
  154. ERA_ReadBytes(0x0168, &temp, 1);
  155. }
  156. /* Pinnacle-based TM040040 Functions */
  157. void cirque_pinnacle_init(void) {
  158. #if defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
  159. spi_init();
  160. #elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c)
  161. i2c_init();
  162. #endif
  163. touchpad_init = true;
  164. // Host clears SW_CC flag
  165. cirque_pinnacle_clear_flags();
  166. // Host configures bits of registers 0x03 and 0x05
  167. RAP_Write(SYSCONFIG_1, SYSCONFIG_1_VALUE);
  168. RAP_Write(FEEDCONFIG_2, FEEDCONFIG_2_VALUE);
  169. // Host enables preferred output mode (absolute)
  170. RAP_Write(FEEDCONFIG_1, FEEDCONFIG_1_VALUE);
  171. // Host sets z-idle packet count to 5 (default is 30)
  172. RAP_Write(Z_IDLE_COUNT, Z_IDLE_COUNT_VALUE);
  173. cirque_pinnacle_set_adc_attenuation(0xFF);
  174. cirque_pinnacle_tune_edge_sensitivity();
  175. cirque_pinnacle_enable_feed(true);
  176. }
  177. // Reads XYZ data from Pinnacle registers 0x14 through 0x17
  178. // Stores result in pinnacle_data_t struct with xValue, yValue, and zValue members
  179. pinnacle_data_t cirque_pinnacle_read_data(void) {
  180. uint8_t data[6] = {0};
  181. pinnacle_data_t result = {0};
  182. RAP_ReadBytes(PACKET_BYTE_0, data, 6);
  183. cirque_pinnacle_clear_flags();
  184. result.buttonFlags = data[0] & 0x3F;
  185. result.xValue = data[2] | ((data[4] & 0x0F) << 8);
  186. result.yValue = data[3] | ((data[4] & 0xF0) << 4);
  187. result.zValue = data[5] & 0x3F;
  188. result.touchDown = (result.xValue != 0 || result.yValue != 0);
  189. return result;
  190. }