2020-12-19 18:43:00 +00:00
|
|
|
/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
|
|
|
|
* Copyright 2019 Sunjun Kim
|
|
|
|
* Copyright 2020 Ploopy Corporation
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
#include "spi_master.h"
|
2021-08-21 04:14:23 +00:00
|
|
|
#include "pmw3360.h"
|
2021-06-05 15:08:35 +00:00
|
|
|
#include "wait.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "print.h"
|
2021-11-15 06:03:24 +00:00
|
|
|
#include PMW3360_FIRMWARE_H
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-08-21 04:14:23 +00:00
|
|
|
// Registers
|
2021-11-15 06:03:24 +00:00
|
|
|
// clang-format off
|
2021-12-27 10:26:09 +00:00
|
|
|
#define REG_Product_ID 0x00
|
|
|
|
#define REG_Revision_ID 0x01
|
|
|
|
#define REG_Motion 0x02
|
|
|
|
#define REG_Delta_X_L 0x03
|
|
|
|
#define REG_Delta_X_H 0x04
|
|
|
|
#define REG_Delta_Y_L 0x05
|
|
|
|
#define REG_Delta_Y_H 0x06
|
|
|
|
#define REG_SQUAL 0x07
|
|
|
|
#define REG_Raw_Data_Sum 0x08
|
|
|
|
#define REG_Maximum_Raw_data 0x09
|
|
|
|
#define REG_Minimum_Raw_data 0x0a
|
|
|
|
#define REG_Shutter_Lower 0x0b
|
|
|
|
#define REG_Shutter_Upper 0x0c
|
|
|
|
#define REG_Control 0x0d
|
|
|
|
#define REG_Config1 0x0f
|
|
|
|
#define REG_Config2 0x10
|
|
|
|
#define REG_Angle_Tune 0x11
|
|
|
|
#define REG_Frame_Capture 0x12
|
|
|
|
#define REG_SROM_Enable 0x13
|
|
|
|
#define REG_Run_Downshift 0x14
|
|
|
|
#define REG_Rest1_Rate_Lower 0x15
|
|
|
|
#define REG_Rest1_Rate_Upper 0x16
|
|
|
|
#define REG_Rest1_Downshift 0x17
|
|
|
|
#define REG_Rest2_Rate_Lower 0x18
|
|
|
|
#define REG_Rest2_Rate_Upper 0x19
|
|
|
|
#define REG_Rest2_Downshift 0x1a
|
|
|
|
#define REG_Rest3_Rate_Lower 0x1b
|
|
|
|
#define REG_Rest3_Rate_Upper 0x1c
|
|
|
|
#define REG_Observation 0x24
|
|
|
|
#define REG_Data_Out_Lower 0x25
|
|
|
|
#define REG_Data_Out_Upper 0x26
|
|
|
|
#define REG_Raw_Data_Dump 0x29
|
|
|
|
#define REG_SROM_ID 0x2a
|
|
|
|
#define REG_Min_SQ_Run 0x2b
|
|
|
|
#define REG_Raw_Data_Threshold 0x2c
|
|
|
|
#define REG_Config5 0x2f
|
|
|
|
#define REG_Power_Up_Reset 0x3a
|
|
|
|
#define REG_Shutdown 0x3b
|
|
|
|
#define REG_Inverse_Product_ID 0x3f
|
|
|
|
#define REG_LiftCutoff_Tune3 0x41
|
|
|
|
#define REG_Angle_Snap 0x42
|
|
|
|
#define REG_LiftCutoff_Tune1 0x4a
|
|
|
|
#define REG_Motion_Burst 0x50
|
|
|
|
#define REG_LiftCutoff_Tune_Timeout 0x58
|
|
|
|
#define REG_LiftCutoff_Tune_Min_Length 0x5a
|
|
|
|
#define REG_SROM_Load_Burst 0x62
|
|
|
|
#define REG_Lift_Config 0x63
|
|
|
|
#define REG_Raw_Data_Burst 0x64
|
|
|
|
#define REG_LiftCutoff_Tune2 0x65
|
|
|
|
|
|
|
|
#define CPI_STEP 100
|
2021-11-15 06:03:24 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2022-01-02 08:06:17 +00:00
|
|
|
// limits to 0--119, resulting in a CPI range of 100 -- 12000 (as only steps of 100 are possible).
|
|
|
|
// Note that for the PMW3389DM chip, the step size is 50 and supported range is
|
|
|
|
// up to 16000. The datasheet does not indicate the minimum CPI though, neither
|
|
|
|
// whether this uses 2 bytes (as 16000/50 == 320)
|
2021-11-15 06:03:24 +00:00
|
|
|
#ifndef MAX_CPI
|
2022-01-02 08:06:17 +00:00
|
|
|
# define MAX_CPI 0x77
|
2021-11-15 06:03:24 +00:00
|
|
|
#endif
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-08-21 04:14:23 +00:00
|
|
|
bool _inBurst = false;
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
#ifdef CONSOLE_ENABLE
|
2020-12-19 18:43:00 +00:00
|
|
|
void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); }
|
2021-11-15 06:03:24 +00:00
|
|
|
#endif
|
|
|
|
#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
bool pmw3360_spi_start(void) {
|
2021-08-21 04:14:23 +00:00
|
|
|
bool status = spi_start(PMW3360_CS_PIN, PMW3360_SPI_LSBFIRST, PMW3360_SPI_MODE, PMW3360_SPI_DIVISOR);
|
2022-01-02 08:06:17 +00:00
|
|
|
// tNCS-SCLK, 120ns
|
2020-12-19 18:43:00 +00:00
|
|
|
wait_us(1);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
spi_status_t pmw3360_write(uint8_t reg_addr, uint8_t data) {
|
|
|
|
pmw3360_spi_start();
|
2020-12-19 18:43:00 +00:00
|
|
|
|
|
|
|
if (reg_addr != REG_Motion_Burst) {
|
|
|
|
_inBurst = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send address of the register, with MSBit = 1 to indicate it's a write
|
|
|
|
spi_status_t status = spi_write(reg_addr | 0x80);
|
2021-06-21 20:00:11 +00:00
|
|
|
status = spi_write(data);
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2022-01-02 08:06:17 +00:00
|
|
|
// tSCLK-NCS for write operation is 35us
|
2021-12-27 10:18:35 +00:00
|
|
|
wait_us(35);
|
2022-01-02 08:06:17 +00:00
|
|
|
spi_stop();
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:18:35 +00:00
|
|
|
// tSWW/tSWR (=180us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
|
|
|
|
wait_us(145);
|
2020-12-19 18:43:00 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
uint8_t pmw3360_read(uint8_t reg_addr) {
|
|
|
|
pmw3360_spi_start();
|
2020-12-19 18:43:00 +00:00
|
|
|
// send adress of the register, with MSBit = 0 to indicate it's a read
|
|
|
|
spi_write(reg_addr & 0x7f);
|
2022-01-02 08:06:17 +00:00
|
|
|
// tSRAD (=160us)
|
|
|
|
wait_us(160);
|
2020-12-19 18:43:00 +00:00
|
|
|
uint8_t data = spi_read();
|
|
|
|
|
|
|
|
// tSCLK-NCS for read operation is 120ns
|
|
|
|
wait_us(1);
|
2022-01-02 08:06:17 +00:00
|
|
|
spi_stop();
|
2020-12-19 18:43:00 +00:00
|
|
|
|
|
|
|
// tSRW/tSRR (=20us) minus tSCLK-NCS
|
|
|
|
wait_us(19);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
bool pmw3360_init(void) {
|
2021-08-21 04:14:23 +00:00
|
|
|
setPinOutput(PMW3360_CS_PIN);
|
2021-06-21 20:00:11 +00:00
|
|
|
|
2020-12-19 18:43:00 +00:00
|
|
|
spi_init();
|
|
|
|
_inBurst = false;
|
|
|
|
|
|
|
|
spi_stop();
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_spi_start();
|
2020-12-19 18:43:00 +00:00
|
|
|
spi_stop();
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_Shutdown, 0xb6); // Shutdown first
|
2020-12-19 18:43:00 +00:00
|
|
|
wait_ms(300);
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_spi_start();
|
2020-12-19 18:43:00 +00:00
|
|
|
wait_us(40);
|
2021-12-27 10:26:09 +00:00
|
|
|
spi_stop();
|
2020-12-19 18:43:00 +00:00
|
|
|
wait_us(40);
|
|
|
|
|
2022-01-02 08:06:17 +00:00
|
|
|
// power up, need to first drive NCS high then low, see above.
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_Power_Up_Reset, 0x5a);
|
2020-12-19 18:43:00 +00:00
|
|
|
wait_ms(50);
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
// read registers and discard
|
|
|
|
pmw3360_read(REG_Motion);
|
|
|
|
pmw3360_read(REG_Delta_X_L);
|
|
|
|
pmw3360_read(REG_Delta_X_H);
|
|
|
|
pmw3360_read(REG_Delta_Y_L);
|
|
|
|
pmw3360_read(REG_Delta_Y_H);
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
pmw3360_upload_firmware();
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
spi_stop();
|
2020-12-19 18:43:00 +00:00
|
|
|
|
|
|
|
wait_ms(10);
|
2021-11-15 06:03:24 +00:00
|
|
|
pmw3360_set_cpi(PMW3360_CPI);
|
2020-12-19 18:43:00 +00:00
|
|
|
|
|
|
|
wait_ms(1);
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_Config2, 0x00);
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -127, 127));
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_Lift_Config, PMW3360_LIFTOFF_DISTANCE);
|
2021-12-09 07:52:07 +00:00
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
bool init_success = pmw3360_check_signature();
|
2021-12-27 10:26:09 +00:00
|
|
|
#ifdef CONSOLE_ENABLE
|
|
|
|
if (init_success) {
|
|
|
|
dprintf("pmw3360 signature verified");
|
|
|
|
} else {
|
|
|
|
dprintf("pmw3360 signature verification failed!");
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-21 20:00:11 +00:00
|
|
|
|
2021-08-21 04:14:23 +00:00
|
|
|
writePinLow(PMW3360_CS_PIN);
|
2021-06-21 20:00:11 +00:00
|
|
|
|
|
|
|
return init_success;
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
void pmw3360_upload_firmware(void) {
|
2022-01-02 08:06:17 +00:00
|
|
|
// Datasheet claims we need to disable REST mode first, but during startup
|
|
|
|
// it's already disabled and we're not turning it on ...
|
2022-01-02 08:11:49 +00:00
|
|
|
// pmw3360_write(REG_Config2, 0x00); // disable REST mode
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_SROM_Enable, 0x1d);
|
2020-12-19 18:43:00 +00:00
|
|
|
|
|
|
|
wait_ms(10);
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_SROM_Enable, 0x18);
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_spi_start();
|
2020-12-19 18:43:00 +00:00
|
|
|
spi_write(REG_SROM_Load_Burst | 0x80);
|
|
|
|
wait_us(15);
|
|
|
|
|
2022-01-10 22:57:36 +00:00
|
|
|
#ifdef PMW3360_LEGACY_FIRMWARE_UPLOAD
|
2020-12-19 18:43:00 +00:00
|
|
|
unsigned char c;
|
2021-10-24 17:46:20 +00:00
|
|
|
for (int i = 0; i < FIRMWARE_LENGTH; i++) {
|
2020-12-19 18:43:00 +00:00
|
|
|
c = (unsigned char)pgm_read_byte(firmware_data + i);
|
|
|
|
spi_write(c);
|
|
|
|
wait_us(15);
|
|
|
|
}
|
2022-01-10 22:57:36 +00:00
|
|
|
#else
|
|
|
|
spi_transmit(firmware_data, sizeof(firmware_data));
|
|
|
|
#endif
|
2020-12-19 18:43:00 +00:00
|
|
|
wait_us(200);
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_read(REG_SROM_ID);
|
|
|
|
pmw3360_write(REG_Config2, 0x00);
|
2020-12-19 18:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
bool pmw3360_check_signature(void) {
|
2021-12-27 10:26:09 +00:00
|
|
|
uint8_t pid = pmw3360_read(REG_Product_ID);
|
|
|
|
uint8_t iv_pid = pmw3360_read(REG_Inverse_Product_ID);
|
|
|
|
uint8_t SROM_ver = pmw3360_read(REG_SROM_ID);
|
2021-11-15 06:03:24 +00:00
|
|
|
return (pid == firmware_signature[0] && iv_pid == firmware_signature[1] && SROM_ver == firmware_signature[2]); // signature for SROM 0x04
|
2020-12-19 18:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
uint16_t pmw3360_get_cpi(void) {
|
|
|
|
uint8_t cpival = pmw3360_read(REG_Config1);
|
|
|
|
return (uint16_t)((cpival + 1) & 0xFF) * CPI_STEP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmw3360_set_cpi(uint16_t cpi) {
|
|
|
|
uint8_t cpival = constrain((cpi / CPI_STEP) - 1, 0, MAX_CPI);
|
|
|
|
pmw3360_write(REG_Config1, cpival);
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
report_pmw3360_t pmw3360_read_burst(void) {
|
2021-12-27 10:26:09 +00:00
|
|
|
report_pmw3360_t report = {0};
|
|
|
|
|
2020-12-19 18:43:00 +00:00
|
|
|
if (!_inBurst) {
|
2021-11-15 06:03:24 +00:00
|
|
|
#ifdef CONSOLE_ENABLE
|
2020-12-19 18:43:00 +00:00
|
|
|
dprintf("burst on");
|
2021-11-15 06:03:24 +00:00
|
|
|
#endif
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_write(REG_Motion_Burst, 0x00);
|
2020-12-19 18:43:00 +00:00
|
|
|
_inBurst = true;
|
|
|
|
}
|
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
pmw3360_spi_start();
|
2020-12-19 18:43:00 +00:00
|
|
|
spi_write(REG_Motion_Burst);
|
2022-01-02 08:06:17 +00:00
|
|
|
wait_us(35); // waits for tSRAD_MOTBR
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
report.motion = spi_read();
|
2022-01-02 08:06:17 +00:00
|
|
|
spi_read(); // skip Observation
|
|
|
|
// delta registers
|
2021-12-27 10:26:09 +00:00
|
|
|
report.dx = spi_read();
|
|
|
|
report.mdx = spi_read();
|
|
|
|
report.dy = spi_read();
|
|
|
|
report.mdy = spi_read();
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2022-01-02 08:06:17 +00:00
|
|
|
if (report.motion & 0b111) { // panic recovery, sometimes burst mode works weird.
|
|
|
|
_inBurst = false;
|
|
|
|
}
|
|
|
|
|
2020-12-19 18:43:00 +00:00
|
|
|
spi_stop();
|
|
|
|
|
2021-11-15 06:03:24 +00:00
|
|
|
#ifdef CONSOLE_ENABLE
|
2021-08-13 21:20:47 +00:00
|
|
|
if (debug_mouse) {
|
2021-12-27 10:26:09 +00:00
|
|
|
print_byte(report.motion);
|
|
|
|
print_byte(report.dx);
|
|
|
|
print_byte(report.mdx);
|
|
|
|
print_byte(report.dy);
|
|
|
|
print_byte(report.mdy);
|
2021-08-13 21:20:47 +00:00
|
|
|
dprintf("\n");
|
|
|
|
}
|
2021-11-15 06:03:24 +00:00
|
|
|
#endif
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
report.isMotion = (report.motion & 0x80) != 0;
|
|
|
|
report.isOnSurface = (report.motion & 0x08) == 0;
|
|
|
|
report.dx |= (report.mdx << 8);
|
|
|
|
report.dx = report.dx * -1;
|
|
|
|
report.dy |= (report.mdy << 8);
|
|
|
|
report.dy = report.dy * -1;
|
2020-12-19 18:43:00 +00:00
|
|
|
|
2021-12-27 10:26:09 +00:00
|
|
|
return report;
|
2020-12-19 18:43:00 +00:00
|
|
|
}
|