You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.3 KiB

  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2019 Sunjun Kim
  3. * Copyright 2020 Ploopy Corporation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma once
  19. #include "spi_master.h"
  20. // Registers
  21. #define REG_Product_ID 0x00
  22. #define REG_Revision_ID 0x01
  23. #define REG_Motion 0x02
  24. #define REG_Delta_X_L 0x03
  25. #define REG_Delta_X_H 0x04
  26. #define REG_Delta_Y_L 0x05
  27. #define REG_Delta_Y_H 0x06
  28. #define REG_SQUAL 0x07
  29. #define REG_Raw_Data_Sum 0x08
  30. #define REG_Maximum_Raw_data 0x09
  31. #define REG_Minimum_Raw_data 0x0A
  32. #define REG_Shutter_Lower 0x0B
  33. #define REG_Shutter_Upper 0x0C
  34. #define REG_Control 0x0D
  35. #define REG_Config1 0x0F
  36. #define REG_Config2 0x10
  37. #define REG_Angle_Tune 0x11
  38. #define REG_Frame_Capture 0x12
  39. #define REG_SROM_Enable 0x13
  40. #define REG_Run_Downshift 0x14
  41. #define REG_Rest1_Rate_Lower 0x15
  42. #define REG_Rest1_Rate_Upper 0x16
  43. #define REG_Rest1_Downshift 0x17
  44. #define REG_Rest2_Rate_Lower 0x18
  45. #define REG_Rest2_Rate_Upper 0x19
  46. #define REG_Rest2_Downshift 0x1A
  47. #define REG_Rest3_Rate_Lower 0x1B
  48. #define REG_Rest3_Rate_Upper 0x1C
  49. #define REG_Observation 0x24
  50. #define REG_Data_Out_Lower 0x25
  51. #define REG_Data_Out_Upper 0x26
  52. #define REG_Raw_Data_Dump 0x29
  53. #define REG_SROM_ID 0x2A
  54. #define REG_Min_SQ_Run 0x2B
  55. #define REG_Raw_Data_Threshold 0x2C
  56. #define REG_Config5 0x2F
  57. #define REG_Power_Up_Reset 0x3A
  58. #define REG_Shutdown 0x3B
  59. #define REG_Inverse_Product_ID 0x3F
  60. #define REG_LiftCutoff_Tune3 0x41
  61. #define REG_Angle_Snap 0x42
  62. #define REG_LiftCutoff_Tune1 0x4A
  63. #define REG_Motion_Burst 0x50
  64. #define REG_LiftCutoff_Tune_Timeout 0x58
  65. #define REG_LiftCutoff_Tune_Min_Length 0x5A
  66. #define REG_SROM_Load_Burst 0x62
  67. #define REG_Lift_Config 0x63
  68. #define REG_Raw_Data_Burst 0x64
  69. #define REG_LiftCutoff_Tune2 0x65
  70. #ifdef CONSOLE_ENABLE
  71. void print_byte(uint8_t byte);
  72. #endif
  73. typedef struct {
  74. int8_t motion;
  75. bool isMotion; // True if a motion is detected.
  76. bool isOnSurface; // True when a chip is on a surface
  77. int16_t dx; // displacement on x directions. Unit: Count. (CPI * Count = Inch value)
  78. int8_t mdx;
  79. int16_t dy; // displacement on y directions.
  80. int8_t mdy;
  81. } report_pmw_t;
  82. bool spi_start_adv(void);
  83. void spi_stop_adv(void);
  84. spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data);
  85. uint8_t spi_read_adv(uint8_t reg_addr);
  86. bool pmw_spi_init(void);
  87. void pmw_set_cpi(uint16_t cpi);
  88. void pmw_upload_firmware(void);
  89. bool pmw_check_signature(void);
  90. report_pmw_t pmw_read_burst(void);
  91. #define degToRad(angleInDegrees) ((angleInDegrees)*M_PI / 180.0)
  92. #define radToDeg(angleInRadians) ((angleInRadians)*180.0 / M_PI)
  93. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))