pmw33xx_common.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "compiler_support.h"
  12. #include "keyboard.h"
  13. #include <stdint.h>
  14. #include "spi_master.h"
  15. #include "util.h"
  16. #include "pointing_device.h"
  17. #if defined(POINTING_DEVICE_DRIVER_pmw3360)
  18. # include "pmw3360.h"
  19. #elif defined(POINTING_DEVICE_DRIVER_pmw3389)
  20. # include "pmw3389.h"
  21. #endif
  22. typedef struct __attribute__((packed)) {
  23. union {
  24. struct {
  25. bool capture_from_raw_data : 1; // FRAME_RData_1st
  26. uint8_t operation_mode : 2; // OP_MODE
  27. bool is_lifted : 1; // Lift_stat
  28. bool raw_data_grab_is_raw_data : 1; // RData_1st
  29. uint8_t _reserved : 2; // 1 + Reserved
  30. bool is_motion : 1; // MOT
  31. } b;
  32. uint8_t w;
  33. } motion;
  34. uint8_t observation;
  35. int16_t delta_x; // displacement on x directions. Unit: Count. (CPI * Count = Inch value)
  36. int16_t delta_y; // displacement on y directions.
  37. } pmw33xx_report_t;
  38. STATIC_ASSERT(sizeof(pmw33xx_report_t) == 6, "pmw33xx_report_t must be 6 bytes in size");
  39. STATIC_ASSERT(sizeof((pmw33xx_report_t){0}.motion) == 1, "pmw33xx_report_t.motion must be 1 byte in size");
  40. #if !defined(PMW33XX_CLOCK_SPEED)
  41. # define PMW33XX_CLOCK_SPEED 2000000
  42. #endif
  43. #if !defined(PMW33XX_SPI_DIVISOR)
  44. # ifdef __AVR__
  45. # define PMW33XX_SPI_DIVISOR (F_CPU / PMW33XX_CLOCK_SPEED)
  46. # else
  47. # define PMW33XX_SPI_DIVISOR 64
  48. # endif
  49. #endif
  50. #if !defined(PMW33XX_LIFTOFF_DISTANCE)
  51. # define PMW33XX_LIFTOFF_DISTANCE 0x02
  52. #endif
  53. #if !defined(ROTATIONAL_TRANSFORM_ANGLE)
  54. # define ROTATIONAL_TRANSFORM_ANGLE 0x00
  55. #endif
  56. #if ROTATIONAL_TRANSFORM_ANGLE > 127 || ROTATIONAL_TRANSFORM_ANGLE < (-127)
  57. # error ROTATIONAL_TRANSFORM_ANGLE has to be in the range of +/- 127 for all PMW33XX sensors.
  58. #endif
  59. // Support single and plural spellings
  60. #ifndef PMW33XX_CS_PINS
  61. # ifndef PMW33XX_CS_PIN
  62. # ifdef POINTING_DEVICE_CS_PIN
  63. # define PMW33XX_CS_PIN POINTING_DEVICE_CS_PIN
  64. # define PMW33XX_CS_PINS {PMW33XX_CS_PIN}
  65. # else
  66. # error "No chip select pin defined -- missing PMW33XX_CS_PIN or PMW33XX_CS_PINS"
  67. # endif
  68. # else
  69. # define PMW33XX_CS_PINS {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 {PMW33XX_CS_PIN_RIGHT}
  78. #endif
  79. // Defines so the old variable names are swapped by the appropiate value on each half
  80. #define cs_pins (is_keyboard_left() ? cs_pins_left : cs_pins_right)
  81. #define in_burst (is_keyboard_left() ? in_burst_left : in_burst_right)
  82. #define pmw33xx_number_of_sensors (is_keyboard_left() ? ARRAY_SIZE((pin_t[])PMW33XX_CS_PINS) : ARRAY_SIZE((pin_t[])PMW33XX_CS_PINS_RIGHT))
  83. #if PMW33XX_CPI > PMW33XX_CPI_MAX || PMW33XX_CPI < PMW33XX_CPI_MIN || (PMW33XX_CPI % PMW33XX_CPI_STEP) != 0U
  84. # 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) "."
  85. # error Use correct PMW33XX_CPI value.
  86. #endif
  87. #define CONSTRAIN(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  88. #define pmw3360_pointing_device_driver pmw33xx_pointing_device_driver;
  89. #define pmw3389_pointing_device_driver pmw33xx_pointing_device_driver;
  90. extern const pointing_device_driver_t pmw33xx_pointing_device_driver;
  91. /**
  92. * @brief Initializes the given sensor so it is in a working state and ready to
  93. * be polled for data.
  94. *
  95. * @param sensor Index of the sensors chip select pin
  96. * @return true Initialization was a success
  97. * @return false Initialization failed, do not proceed operation
  98. */
  99. bool __attribute__((cold)) pmw33xx_init(uint8_t sensor);
  100. /**
  101. * @brief Gets the currently set CPI value from the sensor. CPI is often
  102. * refereed to as the sensors sensitivity.
  103. *
  104. * @param sensor Index of the sensors chip select pin
  105. * @return uint16_t Current CPI value of the sensor
  106. */
  107. uint16_t pmw33xx_get_cpi(uint8_t sensor);
  108. /**
  109. * @brief Sets the given CPI value for the given PMW33XX sensor. CIP is often
  110. * refereed to as the sensors sensitivity. Values outside of the allow range are
  111. * constrained into legal values.
  112. *
  113. * @param sensor Index of the sensors chip select pin
  114. * @param cpi CPI value to set, legal range depends on the PMW sensor type
  115. */
  116. void pmw33xx_set_cpi(uint8_t sensor, uint16_t cpi);
  117. /**
  118. * @brief Sets the given CPI value to all registered PMW33XX sensors. CPI is
  119. * often refereed to as the sensors sensitivity. Values outside of the allow
  120. * range are constrained into legal values.
  121. *
  122. * @param sensor Index of the sensors chip select pin
  123. * @param cpi CPI value to set, legal range depends on the PMW sensor type
  124. */
  125. void pmw33xx_set_cpi_all_sensors(uint16_t cpi);
  126. /**
  127. * @brief Reads and clears the current delta, and motion register values on the
  128. * given sensor.
  129. *
  130. * @param sensor Index of the sensors chip select pin
  131. * @return pmw33xx_report_t Current values of the sensor, if errors occurred all
  132. * fields are set to zero
  133. */
  134. pmw33xx_report_t pmw33xx_read_burst(uint8_t sensor);
  135. /**
  136. * @brief Read one byte of data from the given register on the sensor
  137. *
  138. * @param sensor Index of the sensors chip select pin
  139. * @param reg_addr Register address to read from
  140. * @return uint8_t
  141. */
  142. uint8_t pmw33xx_read(uint8_t sensor, uint8_t reg_addr);
  143. /**
  144. * @brief Writes one byte of data to the given register on the sensor
  145. *
  146. * @param sensor Index of the sensors chip select pin
  147. * @param reg_addr Registers address to write to
  148. * @param data Data to write to the register
  149. * @return true Write was a success
  150. * @return false Write failed, do not proceed operation
  151. */
  152. bool pmw33xx_write(uint8_t sensor, uint8_t reg_addr, uint8_t data);
  153. bool pmw33xx_init_wrapper(void);
  154. void pmw33xx_set_cpi_wrapper(uint16_t cpi);
  155. uint16_t pmw33xx_get_cpi_wrapper(void);
  156. report_mouse_t pmw33xx_get_report(report_mouse_t mouse_report);