Нема описа
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.

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