pat9125.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "uni_avr_rpi.h"
  2. #ifdef PAT9125
  3. #include "pat9125.h"
  4. #ifdef PAT9125_SWSPI
  5. #include "swspi.h"
  6. #endif //PAT9125_SWSPI
  7. #ifdef PAT9125_SWI2C
  8. #include "swi2c.h"
  9. #endif //PAT9125_SWI2C
  10. #ifdef PAT9125_HWI2C
  11. #include <Wire.h>
  12. #endif //PAT9125_HWI2C
  13. unsigned char pat9125_PID1 = 0;
  14. unsigned char pat9125_PID2 = 0;
  15. int pat9125_x = 0;
  16. int pat9125_y = 0;
  17. unsigned char pat9125_b = 0;
  18. unsigned char pat9125_s = 0;
  19. // Init sequence, address & value.
  20. const PROGMEM unsigned char pat9125_init_seq1[] = {
  21. // Disable write protect.
  22. PAT9125_WP, 0x5a,
  23. // Set the X resolution to zero to let the sensor know that it could safely ignore movement in the X axis.
  24. PAT9125_RES_X, PAT9125_XRES,
  25. // Set the Y resolution to a maximum (or nearly a maximum).
  26. PAT9125_RES_Y, PAT9125_YRES,
  27. // Set 12-bit X/Y data format.
  28. PAT9125_ORIENTATION, 0x04,
  29. // PAT9125_ORIENTATION, 0x04 | (xinv?0x08:0) | (yinv?0x10:0), //!? direction switching does not work
  30. // Now continues the magic sequence from the PAT912EL Application Note: Firmware Guides for Tracking Optimization.
  31. 0x5e, 0x08,
  32. 0x20, 0x64,
  33. 0x2b, 0x6d,
  34. 0x32, 0x2f,
  35. // stopper
  36. 0x0ff
  37. };
  38. // Init sequence, address & value.
  39. const PROGMEM unsigned char pat9125_init_seq2[] = {
  40. // Magic sequence to enforce full frame rate of the sensor.
  41. 0x06, 0x028,
  42. 0x33, 0x0d0,
  43. 0x36, 0x0c2,
  44. 0x3e, 0x001,
  45. 0x3f, 0x015,
  46. 0x41, 0x032,
  47. 0x42, 0x03b,
  48. 0x43, 0x0f2,
  49. 0x44, 0x03b,
  50. 0x45, 0x0f2,
  51. 0x46, 0x022,
  52. 0x47, 0x03b,
  53. 0x48, 0x0f2,
  54. 0x49, 0x03b,
  55. 0x4a, 0x0f0,
  56. 0x58, 0x098,
  57. 0x59, 0x00c,
  58. 0x5a, 0x008,
  59. 0x5b, 0x00c,
  60. 0x5c, 0x008,
  61. 0x61, 0x010,
  62. 0x67, 0x09b,
  63. 0x6e, 0x022,
  64. 0x71, 0x007,
  65. 0x72, 0x008,
  66. // stopper
  67. 0x0ff
  68. };
  69. int pat9125_init()
  70. {
  71. #ifdef PAT9125_SWSPI
  72. swspi_init();
  73. #endif //PAT9125_SWSPI
  74. #ifdef PAT9125_SWI2C
  75. swi2c_init(PAT9125_SWI2C_SDA, PAT9125_SWI2C_SCL, PAT9125_SWI2C_CFG);
  76. #endif //PAT9125_SWI2C
  77. #ifdef PAT9125_HWI2C
  78. Wire.begin();
  79. #endif //PAT9125_HWI2C
  80. // Verify that the sensor responds with its correct product ID.
  81. pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
  82. pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
  83. // pat9125_PID1 = 0x31;
  84. // pat9125_PID2 = 0x91;
  85. if ((pat9125_PID1 != 0x31) || (pat9125_PID2 != 0x91))
  86. {
  87. return 0;
  88. }
  89. // Switch to bank0, not allowed to perform OTS_RegWriteRead.
  90. pat9125_wr_reg(PAT9125_BANK_SELECTION, 0);
  91. // Software reset (i.e. set bit7 to 1). It will reset to 0 automatically.
  92. // After the reset, OTS_RegWriteRead is not allowed.
  93. pat9125_wr_reg(PAT9125_CONFIG, 0x97);
  94. // Wait until the sensor reboots.
  95. // Delay 1ms.
  96. delayMicroseconds(1000);
  97. {
  98. const unsigned char *ptr = pat9125_init_seq1;
  99. for (;;) {
  100. const unsigned char addr = pgm_read_byte_near(ptr ++);
  101. if (addr == 0x0ff)
  102. break;
  103. if (! pat9125_wr_reg_verify(addr, pgm_read_byte_near(ptr ++)))
  104. // Verification of the register write failed.
  105. return 0;
  106. }
  107. }
  108. // Delay 10ms.
  109. delayMicroseconds(10000);
  110. // Switch to bank1, not allowed to perform OTS_RegWrite.
  111. pat9125_wr_reg(PAT9125_BANK_SELECTION, 0x01);
  112. {
  113. const unsigned char *ptr = pat9125_init_seq2;
  114. for (;;) {
  115. const unsigned char addr = pgm_read_byte_near(ptr ++);
  116. if (addr == 0x0ff)
  117. break;
  118. if (! pat9125_wr_reg_verify(addr, pgm_read_byte_near(ptr ++)))
  119. // Verification of the register write failed.
  120. return 0;
  121. }
  122. }
  123. // Switch to bank0, not allowed to perform OTS_RegWriteRead.
  124. pat9125_wr_reg(PAT9125_BANK_SELECTION, 0x00);
  125. // Enable write protect.
  126. pat9125_wr_reg(PAT9125_WP, 0x00);
  127. return 1;
  128. }
  129. int pat9125_update()
  130. {
  131. if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
  132. {
  133. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  134. pat9125_b = pat9125_rd_reg(PAT9125_FRAME);
  135. pat9125_s = pat9125_rd_reg(PAT9125_SHUTTER);
  136. if (ucMotion & 0x80)
  137. {
  138. unsigned char ucXL = pat9125_rd_reg(PAT9125_DELTA_XL);
  139. unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
  140. unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
  141. int iDX = ucXL | ((ucXYH << 4) & 0xf00);
  142. int iDY = ucYL | ((ucXYH << 8) & 0xf00);
  143. if (iDX & 0x800) iDX -= 4096;
  144. if (iDY & 0x800) iDY -= 4096;
  145. pat9125_x += iDX;
  146. pat9125_y -= iDY; //negative number, because direction switching does not work
  147. }
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. int pat9125_update_y()
  153. {
  154. if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
  155. {
  156. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  157. if (ucMotion & 0x80)
  158. {
  159. unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
  160. unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
  161. int iDY = ucYL | ((ucXYH << 8) & 0xf00);
  162. if (iDY & 0x800) iDY -= 4096;
  163. pat9125_y -= iDY; //negative number, because direction switching does not work
  164. }
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. unsigned char pat9125_rd_reg(unsigned char addr)
  170. {
  171. unsigned char data = 0;
  172. #ifdef PAT9125_SWSPI
  173. swspi_start();
  174. swspi_tx(addr & 0x7f);
  175. data = swspi_rx();
  176. swspi_stop();
  177. #endif //PAT9125_SWSPI
  178. #ifdef PAT9125_SWI2C
  179. int iret = swi2c_readByte_A8(PAT9125_I2C_ADDR, addr, &data);
  180. #endif //PAT9125_SWI2C
  181. #ifdef PAT9125_HWI2C
  182. Wire.beginTransmission(PAT9125_I2C_ADDR);
  183. Wire.write(addr);
  184. Wire.endTransmission();
  185. if (Wire.requestFrom(PAT9125_I2C_ADDR, 1) == 1)
  186. // if (Wire.available())
  187. data = Wire.read();
  188. #endif //PAT9125_HWI2C
  189. return data;
  190. }
  191. void pat9125_wr_reg(unsigned char addr, unsigned char data)
  192. {
  193. #ifdef PAT9125_SWSPI
  194. swspi_start();
  195. swspi_tx(addr | 0x80);
  196. swspi_tx(data);
  197. swspi_stop();
  198. #endif //PAT9125_SWSPI
  199. #ifdef PAT9125_SWI2C
  200. int iret = swi2c_writeByte_A8(PAT9125_I2C_ADDR, addr, &data);
  201. #endif //PAT9125_SWI2C
  202. #ifdef PAT9125_HWI2C
  203. Wire.beginTransmission(PAT9125_I2C_ADDR);
  204. Wire.write(addr);
  205. Wire.write(data);
  206. Wire.endTransmission();
  207. #endif //PAT9125_HWI2C
  208. }
  209. bool pat9125_wr_reg_verify(unsigned char addr, unsigned char data)
  210. {
  211. pat9125_wr_reg(addr, data);
  212. return pat9125_rd_reg(addr) == data;
  213. }
  214. #endif //PAT9125