adns5050.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # error "No clock pin defined -- missing ADNS5050_SCLK_PIN"
  39. #endif
  40. #ifndef ADNS5050_SDIO_PIN
  41. # error "No data pin defined -- missing ADNS5050_SDIO_PIN"
  42. #endif
  43. #ifndef ADNS5050_CS_PIN
  44. # error "No chip select pin defined -- missing ADNS5050_CS_PIN"
  45. #endif
  46. typedef struct {
  47. int8_t dx;
  48. int8_t dy;
  49. } report_adns5050_t;
  50. // A bunch of functions to implement the ADNS5050-specific serial protocol.
  51. // Note that the "serial.h" driver is insufficient, because it does not
  52. // manually manipulate a serial clock signal.
  53. void adns5050_init(void);
  54. void adns5050_sync(void);
  55. uint8_t adns5050_serial_read(void);
  56. void adns5050_serial_write(uint8_t data);
  57. uint8_t adns5050_read_reg(uint8_t reg_addr);
  58. void adns5050_write_reg(uint8_t reg_addr, uint8_t data);
  59. report_adns5050_t adns5050_read_burst(void);
  60. void adns5050_set_cpi(uint16_t cpi);
  61. uint16_t adns5050_get_cpi(void);
  62. int8_t convert_twoscomp(uint8_t data);
  63. bool adns5050_check_signature(void);