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

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