adns5050.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright 2021 Colin Lam (Ploopy Corporation)
  2. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. * Copyright 2019 Sunjun Kim
  4. * Copyright 2019 Hiroyuki Okada
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include <stdbool.h>
  21. // CPI values
  22. // clang-format off
  23. #define CPI125 0x11
  24. #define CPI250 0x12
  25. #define CPI375 0x13
  26. #define CPI500 0x14
  27. #define CPI625 0x15
  28. #define CPI750 0x16
  29. #define CPI875 0x17
  30. #define CPI1000 0x18
  31. #define CPI1125 0x19
  32. #define CPI1250 0x1a
  33. #define CPI1375 0x1b
  34. // clang-format on
  35. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  36. // Definitions for the ADNS serial line.
  37. #ifndef ADNS5050_SCLK_PIN
  38. # ifdef POINTING_DEVICE_SCLK_PIN
  39. # define ADNS5050_SCLK_PIN POINTING_DEVICE_SCLK_PIN
  40. # else
  41. # error "No clock pin defined -- missing POINTING_DEVICE_SCLK_PIN or ADNS5050_SCLK_PIN"
  42. # endif
  43. #endif
  44. #ifndef ADNS5050_SDIO_PIN
  45. # ifdef POINTING_DEVICE_SDIO_PIN
  46. # define ADNS5050_SDIO_PIN POINTING_DEVICE_SDIO_PIN
  47. # else
  48. # error "No data pin defined -- missing POINTING_DEVICE_SDIO_PIN or ADNS5050_SDIO_PIN"
  49. # endif
  50. #endif
  51. #ifndef ADNS5050_CS_PIN
  52. # ifdef POINTING_DEVICE_CS_PIN
  53. # define ADNS5050_CS_PIN POINTING_DEVICE_CS_PIN
  54. # else
  55. # error "No chip select pin defined -- missing POINTING_DEVICE_CS_PIN or ADNS5050_CS_PIN define"
  56. # endif
  57. #endif
  58. typedef struct {
  59. int8_t dx;
  60. int8_t dy;
  61. } report_adns5050_t;
  62. // A bunch of functions to implement the ADNS5050-specific serial protocol.
  63. // Note that the "serial.h" driver is insufficient, because it does not
  64. // manually manipulate a serial clock signal.
  65. void adns5050_init(void);
  66. void adns5050_sync(void);
  67. uint8_t adns5050_serial_read(void);
  68. void adns5050_serial_write(uint8_t data);
  69. uint8_t adns5050_read_reg(uint8_t reg_addr);
  70. void adns5050_write_reg(uint8_t reg_addr, uint8_t data);
  71. report_adns5050_t adns5050_read_burst(void);
  72. void adns5050_set_cpi(uint16_t cpi);
  73. uint16_t adns5050_get_cpi(void);
  74. int8_t convert_twoscomp(uint8_t data);
  75. bool adns5050_check_signature(void);