No Description
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.

pmw3360.c 8.6KB

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