adns5050.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include <stdint.h>
  22. // CPI values
  23. // clang-format off
  24. #define CPI125 0x11
  25. #define CPI250 0x12
  26. #define CPI375 0x13
  27. #define CPI500 0x14
  28. #define CPI625 0x15
  29. #define CPI750 0x16
  30. #define CPI875 0x17
  31. #define CPI1000 0x18
  32. #define CPI1125 0x19
  33. #define CPI1250 0x1a
  34. #define CPI1375 0x1b
  35. // clang-format on
  36. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  37. // Definitions for the ADNS serial line.
  38. #ifndef ADNS5050_SCLK_PIN
  39. # ifdef POINTING_DEVICE_SCLK_PIN
  40. # define ADNS5050_SCLK_PIN POINTING_DEVICE_SCLK_PIN
  41. # else
  42. # error "No clock pin defined -- missing POINTING_DEVICE_SCLK_PIN or ADNS5050_SCLK_PIN"
  43. # endif
  44. #endif
  45. #ifndef ADNS5050_SDIO_PIN
  46. # ifdef POINTING_DEVICE_SDIO_PIN
  47. # define ADNS5050_SDIO_PIN POINTING_DEVICE_SDIO_PIN
  48. # else
  49. # error "No data pin defined -- missing POINTING_DEVICE_SDIO_PIN or ADNS5050_SDIO_PIN"
  50. # endif
  51. #endif
  52. #ifndef ADNS5050_CS_PIN
  53. # ifdef POINTING_DEVICE_CS_PIN
  54. # define ADNS5050_CS_PIN POINTING_DEVICE_CS_PIN
  55. # else
  56. # error "No chip select pin defined -- missing POINTING_DEVICE_CS_PIN or ADNS5050_CS_PIN define"
  57. # endif
  58. #endif
  59. typedef struct {
  60. int8_t dx;
  61. int8_t dy;
  62. } report_adns5050_t;
  63. // A bunch of functions to implement the ADNS5050-specific serial protocol.
  64. // Note that the "serial.h" driver is insufficient, because it does not
  65. // manually manipulate a serial clock signal.
  66. void adns5050_init(void);
  67. void adns5050_sync(void);
  68. uint8_t adns5050_serial_read(void);
  69. void adns5050_serial_write(uint8_t data);
  70. uint8_t adns5050_read_reg(uint8_t reg_addr);
  71. void adns5050_write_reg(uint8_t reg_addr, uint8_t data);
  72. report_adns5050_t adns5050_read_burst(void);
  73. void adns5050_set_cpi(uint16_t cpi);
  74. uint16_t adns5050_get_cpi(void);
  75. int8_t convert_twoscomp(uint8_t data);
  76. bool adns5050_check_signature(void);