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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <stdio.h>
  20. #include "pico/stdlib.h"
  21. #include "pico/binary_info.h"
  22. #include "hardware/spi.h"
  23. #include "hardware/watchdog.h"
  24. #include "ff.h"
  25. #include "config.h"
  26. #include "log.h"
  27. #include "util.h"
  28. #include "pmw3360_registers.h"
  29. #include "pmw3360_srom.h"
  30. #include "pmw3360.h"
  31. #define HEALTH_CHECK_INTERVAL_MS 1000
  32. #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)
  33. #error PMW3360 API requires a board with SPI pins
  34. #endif
  35. static volatile int32_t delta_x = 0, delta_y = 0;
  36. static volatile bool mouse_motion = false;
  37. static uint32_t last_health_check = 0;
  38. #ifdef PMW_IRQ_COUNTERS
  39. static uint64_t pmw_irq_count_all = 0;
  40. static uint64_t pmw_irq_count_motion = 0;
  41. static uint64_t pmw_irq_count_no_motion = 0;
  42. static uint64_t pmw_irq_count_on_surface = 0;
  43. static uint64_t pmw_irq_count_lifted = 0;
  44. static uint64_t pmw_irq_count_run = 0;
  45. static uint64_t pmw_irq_count_rest1 = 0;
  46. static uint64_t pmw_irq_count_rest2 = 0;
  47. static uint64_t pmw_irq_count_rest3 = 0;
  48. #endif // PMW_IRQ_COUNTERS
  49. void pmw_print_status(char *buff, size_t len) {
  50. size_t pos = 0;
  51. bool com = pmw_is_alive();
  52. if (com) {
  53. pos += snprintf(buff + pos, len - pos, "Communication to PMW3360 is working\r\n");
  54. } else {
  55. pos += snprintf(buff + pos, len - pos, "ERROR: can not communicate to PMW3360\r\n");
  56. }
  57. #ifdef PMW_IRQ_COUNTERS
  58. pos += snprintf(buff + pos, len - pos, "Interrupt statistics:\r\n");
  59. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_all = %llu\r\n", pmw_irq_count_all);
  60. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_motion = %llu\r\n", pmw_irq_count_motion);
  61. pos += snprintf(buff + pos, len - pos, "pmw_irq_cnt_no_move = %llu\r\n", pmw_irq_count_no_motion);
  62. pos += snprintf(buff + pos, len - pos, "pmw_irq_cnt_surface = %llu\r\n", pmw_irq_count_on_surface);
  63. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_lifted = %llu\r\n", pmw_irq_count_lifted);
  64. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_run = %llu\r\n", pmw_irq_count_run);
  65. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_rest1 = %llu\r\n", pmw_irq_count_rest1);
  66. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_rest2 = %llu\r\n", pmw_irq_count_rest2);
  67. pos += snprintf(buff + pos, len - pos, " pmw_irq_cnt_rest3 = %llu\r\n", pmw_irq_count_rest3);
  68. #endif // PMW_IRQ_COUNTERS
  69. }
  70. struct pmw_motion pmw_get(void) {
  71. struct pmw_motion r;
  72. r.motion = mouse_motion;
  73. r.delta_x = 0;
  74. r.delta_y = 0;
  75. if (r.motion) {
  76. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW, false);
  77. r.delta_x = delta_x;
  78. r.delta_y = delta_y;
  79. delta_x = 0;
  80. delta_y = 0;
  81. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW, true);
  82. }
  83. return r;
  84. }
  85. static inline void pmw_cs_select() {
  86. asm volatile("nop \n nop \n nop");
  87. gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0); // Active low
  88. asm volatile("nop \n nop \n nop");
  89. }
  90. static inline void pmw_cs_deselect() {
  91. asm volatile("nop \n nop \n nop");
  92. gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
  93. asm volatile("nop \n nop \n nop");
  94. }
  95. static void pmw_write_register(uint8_t reg, uint8_t data) {
  96. pmw_cs_select();
  97. reg |= WRITE_BIT;
  98. spi_write_blocking(spi_default, &reg, 1);
  99. busy_wait_us(15);
  100. spi_write_blocking(spi_default, &data, 1);
  101. busy_wait_us(20);
  102. pmw_cs_deselect();
  103. busy_wait_us(100);
  104. }
  105. static uint8_t pmw_read_register(uint8_t reg) {
  106. pmw_cs_select();
  107. reg &= ~WRITE_BIT;
  108. spi_write_blocking(spi_default, &reg, 1);
  109. busy_wait_us(160);
  110. uint8_t buf = 0;
  111. spi_read_blocking(spi_default, 0, &buf, 1);
  112. busy_wait_us(1);
  113. pmw_cs_deselect();
  114. busy_wait_us(20);
  115. return buf;
  116. }
  117. static void pmw_write_register_burst(uint8_t reg, const uint8_t *buf, uint16_t len) {
  118. pmw_cs_select();
  119. reg |= WRITE_BIT;
  120. spi_write_blocking(spi_default, &reg, 1);
  121. busy_wait_us(15);
  122. for (uint16_t i = 0; i < len; i++) {
  123. spi_write_blocking(spi_default, buf + i, 1);
  124. busy_wait_us(15);
  125. }
  126. pmw_cs_deselect();
  127. busy_wait_us(1);
  128. }
  129. static void pmw_read_register_burst(uint8_t reg, uint8_t *buf, uint16_t len) {
  130. pmw_cs_select();
  131. reg &= ~WRITE_BIT;
  132. spi_write_blocking(spi_default, &reg, 1);
  133. busy_wait_us(35);
  134. spi_read_blocking(spi_default, 0, buf, len);
  135. pmw_cs_deselect();
  136. busy_wait_us(1);
  137. }
  138. static uint8_t pmw_srom_download(void) {
  139. // Write 0 to Rest_En bit of Config2 register to disable Rest mode
  140. pmw_write_register(REG_CONFIG2, 0x00);
  141. // Write 0x1d to SROM_Enable register for initializing
  142. pmw_write_register(REG_SROM_ENABLE, 0x1D);
  143. // Wait for 10 ms
  144. busy_wait_ms(10);
  145. // Write 0x18 to SROM_Enable register again to start SROM Download
  146. pmw_write_register(REG_SROM_ENABLE, 0x18);
  147. busy_wait_us(120);
  148. // Write SROM file into SROM_Load_Burst register, 1st data must start with SROM_Load_Burst address.
  149. pmw_write_register_burst(REG_SROM_LOAD_BURST, pmw_fw_data, pmw_fw_length);
  150. busy_wait_us(200);
  151. // Read the SROM_ID register to verify the ID before any other register reads or writes
  152. uint8_t srom_id = pmw_read_register(REG_SROM_ID);
  153. return srom_id;
  154. }
  155. static uint8_t pmw_power_up(void) {
  156. pmw_cs_deselect();
  157. pmw_cs_select();
  158. pmw_cs_deselect();
  159. // Write 0x5A to Power_Up_Reset register
  160. pmw_write_register(REG_POWER_UP_RESET, 0x5A);
  161. // Wait for at least 50ms
  162. busy_wait_ms(50);
  163. // Read from registers 0x02, 0x03, 0x04, 0x05 and 0x06 one time
  164. for (uint8_t reg = REG_MOTION; reg <= REG_DELTA_Y_H; reg++) {
  165. pmw_read_register(reg);
  166. }
  167. // Perform SROM download
  168. uint8_t srom_id = pmw_srom_download();
  169. return srom_id;
  170. }
  171. static struct pmw_motion_report pmw_motion_read(void) {
  172. // Write any value to Motion_Burst register
  173. pmw_write_register(REG_MOTION_BURST, 0x42);
  174. // Start reading SPI Data continuously up to 12 bytes
  175. struct pmw_motion_report motion_report;
  176. pmw_read_register_burst(REG_MOTION_BURST, (uint8_t *)&motion_report, sizeof(motion_report));
  177. return motion_report;
  178. }
  179. static uint16_t pmw_srom_checksum(void) {
  180. pmw_write_register(REG_SROM_ENABLE, 0x15);
  181. // Wait for at least 10 ms
  182. busy_wait_ms(10);
  183. uint16_t data = pmw_read_register(REG_DATA_OUT_LOWER);
  184. data |= pmw_read_register(REG_DATA_OUT_UPPER) << 8;
  185. return data;
  186. }
  187. static void pmw_spi_init(void) {
  188. // Use SPI0 at 2MHz
  189. spi_init(spi_default, 2 * 1000 * 1000);
  190. gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
  191. gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
  192. gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
  193. // Chip select is active-low, so we'll initialise it to a driven-high state
  194. gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
  195. gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
  196. gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
  197. spi_set_format(spi_default,
  198. 8, // Number of bits per transfer
  199. 1, // Polarity (CPOL)
  200. 1, // Phase (CPHA)
  201. SPI_MSB_FIRST);
  202. // make the SPI pins available to picotool
  203. bi_decl(bi_3pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI));
  204. bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "SPI CS"));
  205. }
  206. static void pmw_handle_interrupt(void) {
  207. struct pmw_motion_report motion_report = pmw_motion_read();
  208. #ifdef PMW_IRQ_COUNTERS
  209. pmw_irq_count_all++;
  210. if (motion_report.motion & (1 << REG_MOTION_MOT)) {
  211. pmw_irq_count_motion++;
  212. } else {
  213. pmw_irq_count_no_motion++;
  214. }
  215. if (motion_report.motion & (1 << REG_MOTION_LIFT)) {
  216. pmw_irq_count_lifted++;
  217. } else {
  218. pmw_irq_count_on_surface++;
  219. }
  220. if ((motion_report.motion & (1 << REG_MOTION_OP_1))
  221. && (motion_report.motion & (1 << REG_MOTION_OP_2))) {
  222. pmw_irq_count_rest3++;
  223. } else if (motion_report.motion & (1 << REG_MOTION_OP_1)) {
  224. pmw_irq_count_rest1++;
  225. } else if (motion_report.motion & (1 << REG_MOTION_OP_2)) {
  226. pmw_irq_count_rest2++;
  227. } else {
  228. pmw_irq_count_run++;
  229. }
  230. #endif // PMW_IRQ_COUNTERS
  231. uint16_t delta_x_raw = motion_report.delta_x_l | (motion_report.delta_x_h << 8);
  232. uint16_t delta_y_raw = motion_report.delta_y_l | (motion_report.delta_y_h << 8);
  233. delta_x += convert_two_complement(delta_x_raw);
  234. delta_y += convert_two_complement(delta_y_raw);
  235. mouse_motion = true;
  236. }
  237. static void pmw_motion_irq(void) {
  238. if (gpio_get_irq_event_mask(PMW_MOTION_PIN) & GPIO_IRQ_LEVEL_LOW) {
  239. gpio_acknowledge_irq(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW);
  240. pmw_handle_interrupt();
  241. }
  242. }
  243. void pmw_set_sensitivity(uint8_t sens) {
  244. if (sens > 0x77) {
  245. debug("invalid sense, clamping (0x%X > 0x77)", sens);
  246. sens = 0x77;
  247. }
  248. pmw_write_register(REG_CONFIG1, sens);
  249. pmw_write_register(REG_CONFIG5, sens);
  250. }
  251. uint8_t pmw_get_sensitivity(void) {
  252. uint8_t sense_y = pmw_read_register(REG_CONFIG1);
  253. uint8_t sense_x = pmw_read_register(REG_CONFIG5);
  254. if (sense_y != sense_x) {
  255. debug("sensitivity differs for x (0x%02X) and y (0x%02X). resetting.", sense_x, sense_y);
  256. pmw_write_register(REG_CONFIG5, sense_y);
  257. }
  258. return sense_y;
  259. }
  260. static void pmw_irq_start(void) {
  261. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW, true);
  262. }
  263. static void pmw_irq_stop(void) {
  264. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW, false);
  265. }
  266. static void pmw_irq_init(void) {
  267. // setup MOTION pin interrupt to handle reading data
  268. gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
  269. pmw_irq_start();
  270. irq_set_enabled(IO_IRQ_BANK0, true);
  271. // make MOTION pin available to picotool
  272. bi_decl(bi_1pin_with_name(PMW_MOTION_PIN, "PMW3360 MOTION"));
  273. }
  274. bool pmw_is_alive(void) {
  275. bool r = true;
  276. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW, false);
  277. uint8_t prod_id = pmw_read_register(REG_PRODUCT_ID);
  278. uint8_t inv_prod_id = pmw_read_register(REG_INVERSE_PRODUCT_ID);
  279. if (prod_id != ((~inv_prod_id) & 0xFF)) {
  280. r = false;
  281. }
  282. gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_LEVEL_LOW, true);
  283. return r;
  284. }
  285. ssize_t pmw_frame_capture(uint8_t *buff, size_t buffsize) {
  286. if ((buffsize < PMW_FRAME_CAPTURE_LEN) || (buff == NULL)) {
  287. debug("invalid or too small buffer (%u < %u)", buffsize, PMW_FRAME_CAPTURE_LEN);
  288. return -1;
  289. }
  290. pmw_irq_stop();
  291. // write 0 to Rest_En bit of Config2 register to disable Rest mode
  292. pmw_write_register(REG_CONFIG2, 0x00);
  293. // write 0x83 to Frame_Capture register
  294. pmw_write_register(REG_FRAME_CAPTURE, 0x83);
  295. // write 0xC5 to Frame_Capture register
  296. pmw_write_register(REG_FRAME_CAPTURE, 0xC5);
  297. // wait for 20ms
  298. busy_wait_ms(20);
  299. // continue burst read from Raw_data_Burst register until all 1296 raw data are transferred
  300. pmw_read_register_burst(REG_RAW_DATA_BURST, buff, PMW_FRAME_CAPTURE_LEN);
  301. return PMW_FRAME_CAPTURE_LEN;
  302. }
  303. #define PMW_DATA_DUMP_SAMPLES 1000
  304. #define PRINTRAW(fmt, ...) { \
  305. if (serial) { \
  306. print(fmt, ##__VA_ARGS__); \
  307. } else { \
  308. int n = snprintf(line, 100, fmt, ##__VA_ARGS__); \
  309. UINT bw; \
  310. f_write(&file, line, n, &bw); \
  311. } \
  312. }
  313. void pmw_dump_data(bool serial) {
  314. char line[100];
  315. FIL file;
  316. if (!serial) {
  317. FRESULT res = f_open(&file, "pmw_data.csv", FA_CREATE_ALWAYS | FA_WRITE);
  318. if (res != FR_OK) {
  319. debug("error: f_open returned %d", res);
  320. return;
  321. }
  322. }
  323. struct pmw_motion_report buff[PMW_DATA_DUMP_SAMPLES];
  324. println("Will now capture %u data samples from PMW3360", PMW_DATA_DUMP_SAMPLES);
  325. println("Move trackball to generate some data!");
  326. pmw_irq_stop();
  327. for (size_t i = 0; i < PMW_DATA_DUMP_SAMPLES; i++) {
  328. // wait until MOTION pin is set
  329. while (gpio_get(PMW_MOTION_PIN)) {
  330. watchdog_update();
  331. }
  332. buff[i] = pmw_motion_read();
  333. }
  334. if (serial) {
  335. println();
  336. } else {
  337. println("Now writing data");
  338. }
  339. PRINTRAW("time,motion,observation,delta_x,delta_y,squal,raw_sum,raw_max,raw_min,shutter\r\n");
  340. for (size_t i = 0; i < PMW_DATA_DUMP_SAMPLES; i++) {
  341. watchdog_update();
  342. uint16_t delta_x_raw = buff[i].delta_x_l | (buff[i].delta_x_h << 8);
  343. uint16_t delta_y_raw = buff[i].delta_y_l | (buff[i].delta_y_h << 8);
  344. uint16_t shutter_raw = buff[i].shutter_lower | (buff[i].shutter_upper << 8);
  345. PRINTRAW("%llu,", to_us_since_boot(get_absolute_time()));
  346. PRINTRAW("%u,", buff[i].motion);
  347. PRINTRAW("%u,", buff[i].observation);
  348. PRINTRAW("%ld,", convert_two_complement(delta_x_raw));
  349. PRINTRAW("%ld,", convert_two_complement(delta_y_raw));
  350. PRINTRAW("%u,", buff[i].squal);
  351. PRINTRAW("%u,", buff[i].raw_data_sum);
  352. PRINTRAW("%u,", buff[i].maximum_raw_data);
  353. PRINTRAW("%u,", buff[i].minimum_raw_data);
  354. PRINTRAW("%u\r\n", shutter_raw);
  355. }
  356. if (serial) {
  357. println();
  358. }
  359. pmw_irq_start();
  360. if (!serial) {
  361. FRESULT res = f_close(&file);
  362. if (res != FR_OK) {
  363. debug("error: f_close returned %d", res);
  364. }
  365. }
  366. }
  367. int pmw_init(void) {
  368. // initializing takes a while (~160ms)
  369. watchdog_update();
  370. pmw_irq_stop();
  371. pmw_spi_init();
  372. uint8_t srom_id = pmw_power_up();
  373. uint8_t prod_id = pmw_read_register(REG_PRODUCT_ID);
  374. uint8_t inv_prod_id = pmw_read_register(REG_INVERSE_PRODUCT_ID);
  375. uint16_t srom_checksum = pmw_srom_checksum();
  376. #ifdef PMW_PRINT_IDS
  377. uint8_t rev_id = pmw_read_register(REG_REVISION_ID);
  378. debug("SROM ID: 0x%02X", srom_id);
  379. debug("Product ID: 0x%02X", prod_id);
  380. debug("~ Prod. ID: 0x%02X", inv_prod_id);
  381. debug("Revision ID: 0x%02X", rev_id);
  382. debug("SROM CRC: 0x%04X", srom_checksum);
  383. #endif // PMW_PRINT_IDS
  384. if (prod_id != ((~inv_prod_id) & 0xFF)) {
  385. debug("SPI communication error (0x%02X != ~0x%02X)", prod_id, inv_prod_id);
  386. return -1;
  387. }
  388. if ((srom_id != pmw_fw_id) || (srom_checksum != pmw_fw_crc)) {
  389. if (srom_id != pmw_fw_id) {
  390. debug("PMW3360 error: invalid SROM ID (0x%02X != 0x%02X)", srom_id, pmw_fw_id);
  391. }
  392. if (srom_checksum != pmw_fw_crc) {
  393. debug("PMW3360 error: invalid SROM CRC (0x%04X != 0x%04X)", srom_checksum, pmw_fw_crc);
  394. }
  395. debug("this may require a power-cycle to fix!");
  396. return -1;
  397. }
  398. // Write 0x00 to Config2 register for wired mouse or 0x20 for wireless mouse design
  399. #ifdef PMW_FEATURE_WIRELESS
  400. pmw_write_register(REG_CONFIG2, 0x20);
  401. #else // ! PMW_FEATURE_WIRELESS
  402. pmw_write_register(REG_CONFIG2, 0x00);
  403. #endif // PMW_FEATURE_WIRELESS
  404. // Set sensitivity for each axis
  405. pmw_write_register(REG_CONFIG2, pmw_read_register(REG_CONFIG2) | 0x04);
  406. pmw_set_sensitivity(DEFAULT_MOUSE_SENSITIVITY);
  407. // Set lift-detection threshold to 3mm (max)
  408. pmw_write_register(REG_LIFT_CONFIG, 0x03);
  409. pmw_irq_init();
  410. return 0;
  411. }
  412. void pmw_run(void) {
  413. uint32_t now = to_ms_since_boot(get_absolute_time());
  414. if (now >= (last_health_check + HEALTH_CHECK_INTERVAL_MS)) {
  415. last_health_check = now;
  416. if (!pmw_is_alive()) {
  417. debug("PMW3360 is dead. resetting!");
  418. reset_to_main();
  419. }
  420. }
  421. }