pat9125.c 6.8 KB

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