Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pmw3360.c 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * pmw3360.c
  3. *
  4. * Based on:
  5. * - PMW3360 Datasheet
  6. * - https://github.com/raspberrypi/pico-examples/blob/master/spi/bme280_spi/bme280_spi.c
  7. *
  8. * Pinout:
  9. * GPIO 16 (pin 21) MISO -> MISO on PMW3360 board
  10. * GPIO 17 (pin 22) CS -> NCS on PMW3360 board
  11. * GPIO 18 (pin 24) SCK -> SCK on PMW3360 board
  12. * GPIO 19 (pin 25) MOSI -> MOSI on PMW3360 board
  13. * 3.3v (pin 36) -> VCC on PMW3360 board
  14. * GND (pin 38) -> GND on PMW3360 board
  15. *
  16. * NOTE: Ensure the PMW3360 breakout board is capable of being driven at 3.3v NOT 5v.
  17. * The Pico GPIO (and therefore SPI) cannot be used at 5v.
  18. */
  19. #include "pico/stdlib.h"
  20. #include "pico/binary_info.h"
  21. #include "hardware/spi.h"
  22. #define PMW_PRINT_IDS
  23. #define PMW_IRQ_COUNTERS
  24. //#define PMW_FEATURE_WIRELESS
  25. #include "log.h"
  26. #include "pmw3360_registers.h"
  27. #include "pmw3360_srom.h"
  28. #include "pmw3360.h"
  29. #if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)
  30. #error PMW3360 API requires a board with SPI pins
  31. #endif
  32. #define PMW_MOTION_PIN 20
  33. #ifdef PMW_IRQ_COUNTERS
  34. static uint64_t pmw_irq_count_all = 0;
  35. static uint64_t pmw_irq_count_motion = 0;
  36. static uint64_t pmw_irq_count_no_motion = 0;
  37. static uint64_t pmw_irq_count_on_surface = 0;
  38. static uint64_t pmw_irq_count_lifted = 0;
  39. static uint64_t pmw_irq_count_run = 0;
  40. static uint64_t pmw_irq_count_rest1 = 0;
  41. static uint64_t pmw_irq_count_rest2 = 0;
  42. static uint64_t pmw_irq_count_rest3 = 0;
  43. #endif // PMW_IRQ_COUNTERS
  44. void print_pmw_status(void) {
  45. #ifdef PMW_IRQ_COUNTERS
  46. print(" pmw_irq_cnt_all = %llu", pmw_irq_count_all);
  47. print(" pmw_irq_cnt_motion = %llu", pmw_irq_count_motion);
  48. print("pmw_irq_cnt_no_move = %llu", pmw_irq_count_no_motion);
  49. print("pmw_irq_cnt_surface = %llu", pmw_irq_count_on_surface);
  50. print(" pmw_irq_cnt_lifted = %llu", pmw_irq_count_lifted);
  51. print(" pmw_irq_cnt_run = %llu", pmw_irq_count_run);
  52. print(" pmw_irq_cnt_rest1 = %llu", pmw_irq_count_rest1);
  53. print(" pmw_irq_cnt_rest2 = %llu", pmw_irq_count_rest2);
  54. print(" pmw_irq_cnt_rest3 = %llu", pmw_irq_count_rest3);
  55. #endif // PMW_IRQ_COUNTERS
  56. }
  57. static inline void pmw_cs_select() {
  58. asm volatile("nop \n nop \n nop");
  59. gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0); // Active low
  60. asm volatile("nop \n nop \n nop");
  61. }
  62. static inline void pmw_cs_deselect() {
  63. asm volatile("nop \n nop \n nop");
  64. gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
  65. asm volatile("nop \n nop \n nop");
  66. }
  67. static void pmw_write_register(uint8_t reg, uint8_t data) {
  68. pmw_cs_select();
  69. reg |= WRITE_BIT;
  70. spi_write_blocking(spi_default, &reg, 1);
  71. busy_wait_us(15);
  72. spi_write_blocking(spi_default, &data, 1);
  73. busy_wait_us(20);
  74. pmw_cs_deselect();
  75. busy_wait_us(100);
  76. }
  77. static uint8_t pmw_read_register(uint8_t reg) {
  78. pmw_cs_select();
  79. reg &= ~WRITE_BIT;
  80. spi_write_blocking(spi_default, &reg, 1);
  81. busy_wait_us(160);
  82. uint8_t buf = 0;
  83. spi_read_blocking(spi_default, 0, &buf, 1);
  84. busy_wait_us(1);
  85. pmw_cs_deselect();
  86. busy_wait_us(20);
  87. return buf;
  88. }
  89. static void pmw_write_register_burst(uint8_t reg, const uint8_t *buf, uint16_t len) {
  90. pmw_cs_select();
  91. reg |= WRITE_BIT;
  92. spi_write_blocking(spi_default, &reg, 1);
  93. busy_wait_us(15);
  94. for (uint16_t i = 0; i < len; i++) {
  95. spi_write_blocking(spi_default, buf + i, 1);
  96. busy_wait_us(15);
  97. }
  98. pmw_cs_deselect();
  99. busy_wait_us(1);
  100. }
  101. static void pmw_read_register_burst(uint8_t reg, uint8_t *buf, uint16_t len) {
  102. pmw_cs_select();
  103. reg &= ~WRITE_BIT;
  104. spi_write_blocking(spi_default, &reg, 1);
  105. busy_wait_us(35);
  106. spi_read_blocking(spi_default, 0, buf, len);
  107. pmw_cs_deselect();
  108. busy_wait_us(1);
  109. }
  110. static uint8_t pmw_srom_download(void) {
  111. // Write 0 to Rest_En bit of Config2 register to disable Rest mode
  112. pmw_write_register(REG_CONFIG2, 0x00);
  113. // Write 0x1d to SROM_Enable register for initializing
  114. pmw_write_register(REG_SROM_ENABLE, 0x1D);
  115. // Wait for 10 ms
  116. busy_wait_ms(10);
  117. // Write 0x18 to SROM_Enable register again to start SROM Download
  118. pmw_write_register(REG_SROM_ENABLE, 0x18);
  119. busy_wait_us(120);
  120. // Write SROM file into SROM_Load_Burst register, 1st data must start with SROM_Load_Burst address.
  121. pmw_write_register_burst(REG_SROM_LOAD_BURST, pmw_fw_data, pmw_fw_length);
  122. busy_wait_us(200);
  123. // Read the SROM_ID register to verify the ID before any other register reads or writes
  124. uint8_t srom_id = pmw_read_register(REG_SROM_ID);
  125. return srom_id;
  126. }
  127. static uint8_t pmw_power_up(void) {
  128. pmw_cs_deselect();
  129. pmw_cs_select();
  130. pmw_cs_deselect();
  131. // Write 0x5A to Power_Up_Reset register
  132. pmw_write_register(REG_POWER_UP_RESET, 0x5A);
  133. // Wait for at least 50ms
  134. busy_wait_ms(50);
  135. // Read from registers 0x02, 0x03, 0x04, 0x05 and 0x06 one time
  136. for (uint8_t reg = REG_MOTION; reg <= REG_DELTA_Y_H; reg++) {
  137. pmw_read_register(reg);
  138. }
  139. // Perform SROM download
  140. uint8_t srom_id = pmw_srom_download();
  141. return srom_id;
  142. }
  143. static struct pmw_motion_report pmw_motion_read(void) {
  144. // Write any value to Motion_Burst register
  145. pmw_write_register(REG_MOTION_BURST, 0x42);
  146. // Start reading SPI Data continuously up to 12 bytes
  147. struct pmw_motion_report motion_report;
  148. pmw_read_register_burst(REG_MOTION_BURST, (uint8_t *)&motion_report, sizeof(motion_report));
  149. return motion_report;
  150. }
  151. static uint16_t pmw_srom_checksum(void) {
  152. pmw_write_register(REG_SROM_ENABLE, 0x15);
  153. // Wait for at least 10 ms
  154. busy_wait_ms(10);
  155. uint16_t data = pmw_read_register(REG_DATA_OUT_LOWER);
  156. data |= pmw_read_register(REG_DATA_OUT_UPPER) << 8;
  157. return data;
  158. }
  159. static void pmw_spi_init(void) {
  160. // Use SPI0 at 2MHz
  161. spi_init(spi_default, 2 * 1000 * 1000);
  162. gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
  163. gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
  164. gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
  165. // Chip select is active-low, so we'll initialise it to a driven-high state
  166. gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
  167. gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
  168. gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
  169. spi_set_format(spi_default,
  170. 8, // Number of bits per transfer
  171. 1, // Polarity (CPOL)
  172. 1, // Phase (CPHA)
  173. SPI_MSB_FIRST);
  174. // make the SPI pins available to picotool
  175. bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI));
  176. bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "SPI CS"));
  177. }
  178. // TODO
  179. int16_t delta_x = 0, delta_y = 0;
  180. int convTwosComp(int b){
  181. //Convert from 2's complement
  182. if(b & 0x8000){
  183. b = -1 * ((b ^ 0xffff) + 1);
  184. }
  185. return b;
  186. }
  187. static void pmw_handle_interrupt(void) {
  188. struct pmw_motion_report motion_report = pmw_motion_read();
  189. #ifdef PMW_IRQ_COUNTERS
  190. pmw_irq_count_all++;
  191. if (motion_report.motion & (1 << REG_MOTION_MOT)) {
  192. pmw_irq_count_motion++;
  193. } else {
  194. pmw_irq_count_no_motion++;
  195. }
  196. if (motion_report.motion & (1 << REG_MOTION_LIFT)) {
  197. pmw_irq_count_lifted++;
  198. } else {
  199. pmw_irq_count_on_surface++;
  200. }
  201. if ((motion_report.motion & (1 << REG_MOTION_OP_1))
  202. && (motion_report.motion & (1 << REG_MOTION_OP_2))) {
  203. pmw_irq_count_rest3++;
  204. } else if (motion_report.motion & (1 << REG_MOTION_OP_1)) {
  205. pmw_irq_count_rest1++;
  206. } else if (motion_report.motion & (1 << REG_MOTION_OP_2)) {
  207. pmw_irq_count_rest2++;
  208. } else {
  209. pmw_irq_count_run++;
  210. }
  211. #endif // PMW_IRQ_COUNTERS
  212. delta_x += convTwosComp(motion_report.delta_x_l | (motion_report.delta_x_h << 8));
  213. delta_y += convTwosComp(motion_report.delta_y_l | (motion_report.delta_y_h << 8));
  214. }
  215. static void pmw_motion_irq(void) {
  216. if (gpio_get_irq_event_mask(PMW_MOTION_PIN) & GPIO_IRQ_EDGE_FALL) {
  217. gpio_acknowledge_irq(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL);
  218. pmw_handle_interrupt();
  219. }
  220. }
  221. int pmw_init(void) {
  222. pmw_spi_init();
  223. uint8_t srom_id = pmw_power_up();
  224. uint8_t prod_id = pmw_read_register(REG_PRODUCT_ID);
  225. uint8_t inv_prod_id = pmw_read_register(REG_INVERSE_PRODUCT_ID);
  226. uint16_t srom_checksum = pmw_srom_checksum();
  227. #ifdef PMW_PRINT_IDS
  228. uint8_t rev_id = pmw_read_register(REG_REVISION_ID);
  229. debug("SROM ID: 0x%02X", srom_id);
  230. debug("Product ID: 0x%02X", prod_id);
  231. debug("~ Prod. ID: 0x%02X", inv_prod_id);
  232. debug("Revision ID: 0x%02X", rev_id);
  233. debug("SROM CRC: 0x%04X", srom_checksum);
  234. #endif // PMW_PRINT_IDS
  235. if (prod_id != ((~inv_prod_id) & 0xFF)) {
  236. debug("SPI communication error (0x%02X != ~0x%02X)", prod_id, inv_prod_id);
  237. return -1;
  238. }
  239. if (srom_id != pmw_fw_id) {
  240. debug("PMW3360 error: invalid SROM ID (0x%02X != 0x%02X)", srom_id, pmw_fw_id);
  241. return -1;
  242. }
  243. if (srom_checksum != pmw_fw_crc) {
  244. debug("PMW3360 error: invalid SROM CRC (0x%04X != 0x%04X)", srom_checksum, pmw_fw_crc);
  245. return -1;
  246. }
  247. // Write 0x00 to Config2 register for wired mouse or 0x20 for wireless mouse design
  248. #ifdef PMW_FEATURE_WIRELESS
  249. pmw_write_register(REG_CONFIG2, 0x20);
  250. #else // ! PMW_FEATURE_WIRELESS
  251. pmw_write_register(REG_CONFIG2, 0x00);
  252. #endif // PMW_FEATURE_WIRELESS
  253. // setup MOTION pin interrupt to handle reading data
  254. gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
  255. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);
  256. irq_set_enabled(IO_IRQ_BANK0, true);
  257. // make MOTION pin available to picotool
  258. bi_decl(bi_1pin_with_name(PMW_MOTION_PIN, "PMW3360 MOTION"));
  259. return 0;
  260. }