pmw33xx_common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2022 Pablo Martinez (@elpekenin)
  2. // Copyright 2022 Daniel Kao (dkao)
  3. // Copyright 2022 Stefan Kerkmann (KarlK90)
  4. // Copyright 2022 Ulrich Spörlein (@uqs)
  5. // Copyright 2021 Alabastard (@Alabastard-64)
  6. // Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  7. // Copyright 2019 Sunjun Kim
  8. // Copyright 2020 Ploopy Corporation
  9. // SPDX-License-Identifier: GPL-2.0-or-later
  10. #pragma once
  11. #include "keyboard.h"
  12. #include <stdint.h>
  13. #include "spi_master.h"
  14. #include "util.h"
  15. #if defined(POINTING_DEVICE_DRIVER_pmw3360)
  16. # include "pmw3360.h"
  17. #elif defined(POINTING_DEVICE_DRIVER_pmw3389)
  18. # include "pmw3389.h"
  19. #endif
  20. typedef struct __attribute__((packed)) {
  21. union {
  22. struct {
  23. bool capture_from_raw_data : 1; // FRAME_RData_1st
  24. uint8_t operation_mode : 2; // OP_MODE
  25. bool is_lifted : 1; // Lift_stat
  26. bool raw_data_grab_is_raw_data : 1; // RData_1st
  27. uint8_t _reserved : 2; // 1 + Reserved
  28. bool is_motion : 1; // MOT
  29. } b;
  30. uint8_t w;
  31. } motion;
  32. uint8_t observation;
  33. int16_t delta_x; // displacement on x directions. Unit: Count. (CPI * Count = Inch value)
  34. int16_t delta_y; // displacement on y directions.
  35. } pmw33xx_report_t;
  36. _Static_assert(sizeof(pmw33xx_report_t) == 6, "pmw33xx_report_t must be 6 bytes in size");
  37. _Static_assert(sizeof((pmw33xx_report_t){0}.motion) == 1, "pmw33xx_report_t.motion must be 1 byte in size");
  38. #if !defined(PMW33XX_CLOCK_SPEED)
  39. # define PMW33XX_CLOCK_SPEED 2000000
  40. #endif
  41. #if !defined(PMW33XX_SPI_DIVISOR)
  42. # ifdef __AVR__
  43. # define PMW33XX_SPI_DIVISOR (F_CPU / PMW33XX_CLOCK_SPEED)
  44. # else
  45. # define PMW33XX_SPI_DIVISOR 64
  46. # endif
  47. #endif
  48. #if !defined(PMW33XX_LIFTOFF_DISTANCE)
  49. # define PMW33XX_LIFTOFF_DISTANCE 0x02
  50. #endif
  51. #if !defined(ROTATIONAL_TRANSFORM_ANGLE)
  52. # define ROTATIONAL_TRANSFORM_ANGLE 0x00
  53. #endif
  54. #if ROTATIONAL_TRANSFORM_ANGLE > 127 || ROTATIONAL_TRANSFORM_ANGLE < (-127)
  55. # error ROTATIONAL_TRANSFORM_ANGLE has to be in the range of +/- 127 for all PMW33XX sensors.
  56. #endif
  57. // Support single and plural spellings
  58. #ifndef PMW33XX_CS_PINS
  59. # ifndef PMW33XX_CS_PIN
  60. # ifdef POINTING_DEVICE_CS_PIN
  61. # define PMW33XX_CS_PIN POINTING_DEVICE_CS_PIN
  62. # define PMW33XX_CS_PINS \
  63. { PMW33XX_CS_PIN }
  64. # else
  65. # error "No chip select pin defined -- missing PMW33XX_CS_PIN or PMW33XX_CS_PINS"
  66. # endif
  67. # else
  68. # define PMW33XX_CS_PINS \
  69. { PMW33XX_CS_PIN }
  70. # endif
  71. #endif
  72. // Support single spelling and default to be the same as left side
  73. #if !defined(PMW33XX_CS_PINS_RIGHT)
  74. # if !defined(PMW33XX_CS_PIN_RIGHT)
  75. # define PMW33XX_CS_PIN_RIGHT PMW33XX_CS_PIN
  76. # endif
  77. # define PMW33XX_CS_PINS_RIGHT \
  78. { PMW33XX_CS_PIN_RIGHT }
  79. #endif
  80. // Defines so the old variable names are swapped by the appropiate value on each half
  81. #define cs_pins (is_keyboard_left() ? cs_pins_left : cs_pins_right)
  82. #define in_burst (is_keyboard_left() ? in_burst_left : in_burst_right)
  83. #define pmw33xx_number_of_sensors (is_keyboard_left() ? ARRAY_SIZE((pin_t[])PMW33XX_CS_PINS) : ARRAY_SIZE((pin_t[])PMW33XX_CS_PINS_RIGHT))
  84. #if PMW33XX_CPI > PMW33XX_CPI_MAX || PMW33XX_CPI < PMW33XX_CPI_MIN || (PMW33XX_CPI % PMW33XX_CPI_STEP) != 0U
  85. # pragma message "PMW33XX_CPI has to be in the range of " STR(PMW33XX_CPI_MAX) "-" STR(PMW33XX_CPI_MIN) " in increments of " STR(PMW33XX_CPI_STEP) ". But it is " STR(PMW33XX_CPI) "."
  86. # error Use correct PMW33XX_CPI value.
  87. #endif
  88. #define CONSTRAIN(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  89. /**
  90. * @brief Initializes the given sensor so it is in a working state and ready to
  91. * be polled for data.
  92. *
  93. * @param sensor Index of the sensors chip select pin
  94. * @return true Initialization was a success
  95. * @return false Initialization failed, do not proceed operation
  96. */
  97. bool __attribute__((cold)) pmw33xx_init(uint8_t sensor);
  98. /**
  99. * @brief Gets the currently set CPI value from the sensor. CPI is often
  100. * refereed to as the sensors sensitivity.
  101. *
  102. * @param sensor Index of the sensors chip select pin
  103. * @return uint16_t Current CPI value of the sensor
  104. */
  105. uint16_t pmw33xx_get_cpi(uint8_t sensor);
  106. /**
  107. * @brief Sets the given CPI value for the given PMW33XX sensor. CIP is often
  108. * refereed to as the sensors sensitivity. Values outside of the allow range are
  109. * constrained into legal values.
  110. *
  111. * @param sensor Index of the sensors chip select pin
  112. * @param cpi CPI value to set, legal range depends on the PMW sensor type
  113. */
  114. void pmw33xx_set_cpi(uint8_t sensor, uint16_t cpi);
  115. /**
  116. * @brief Sets the given CPI value to all registered PMW33XX sensors. CPI is
  117. * often refereed to as the sensors sensitivity. Values outside of the allow
  118. * range are constrained into legal values.
  119. *
  120. * @param sensor Index of the sensors chip select pin
  121. * @param cpi CPI value to set, legal range depends on the PMW sensor type
  122. */
  123. void pmw33xx_set_cpi_all_sensors(uint16_t cpi);
  124. /**
  125. * @brief Reads and clears the current delta, and motion register values on the
  126. * given sensor.
  127. *
  128. * @param sensor Index of the sensors chip select pin
  129. * @return pmw33xx_report_t Current values of the sensor, if errors occurred all
  130. * fields are set to zero
  131. */
  132. pmw33xx_report_t pmw33xx_read_burst(uint8_t sensor);
  133. /**
  134. * @brief Read one byte of data from the given register on the sensor
  135. *
  136. * @param sensor Index of the sensors chip select pin
  137. * @param reg_addr Register address to read from
  138. * @return uint8_t
  139. */
  140. uint8_t pmw33xx_read(uint8_t sensor, uint8_t reg_addr);
  141. /**
  142. * @brief Writes one byte of data to the given register on the sensor
  143. *
  144. * @param sensor Index of the sensors chip select pin
  145. * @param reg_addr Registers address to write to
  146. * @param data Data to write to the register
  147. * @return true Write was a success
  148. * @return false Write failed, do not proceed operation
  149. */
  150. bool pmw33xx_write(uint8_t sensor, uint8_t reg_addr, uint8_t data);