diff options
Diffstat (limited to 'drivers/sensors')
-rw-r--r-- | drivers/sensors/adns5050.c | 10 | ||||
-rw-r--r-- | drivers/sensors/adns9800.c | 20 | ||||
-rw-r--r-- | drivers/sensors/adns9800_srom_A6.h | 2 | ||||
-rw-r--r-- | drivers/sensors/analog_joystick.c | 2 | ||||
-rw-r--r-- | drivers/sensors/cirque_pinnacle.c | 38 | ||||
-rw-r--r-- | drivers/sensors/cirque_pinnacle.h | 8 | ||||
-rw-r--r-- | drivers/sensors/cirque_pinnacle_i2c.c | 4 | ||||
-rw-r--r-- | drivers/sensors/cirque_pinnacle_spi.c | 10 | ||||
-rw-r--r-- | drivers/sensors/pimoroni_trackball.c | 24 | ||||
-rw-r--r-- | drivers/sensors/pimoroni_trackball.h | 9 | ||||
-rw-r--r-- | drivers/sensors/pmw3360.c | 287 | ||||
-rw-r--r-- | drivers/sensors/pmw3360.h | 43 | ||||
-rw-r--r-- | drivers/sensors/pmw3389.c | 294 | ||||
-rw-r--r-- | drivers/sensors/pmw3389.h | 76 | ||||
-rw-r--r-- | drivers/sensors/pmw3389_firmware.h | 558 |
15 files changed, 884 insertions, 501 deletions
diff --git a/drivers/sensors/adns5050.c b/drivers/sensors/adns5050.c index c23d24d5af..b76268fba2 100644 --- a/drivers/sensors/adns5050.c +++ b/drivers/sensors/adns5050.c @@ -74,9 +74,13 @@ void adns5050_sync(void) { writePinHigh(ADNS5050_CS_PIN); } -void adns5050_cs_select(void) { writePinLow(ADNS5050_CS_PIN); } +void adns5050_cs_select(void) { + writePinLow(ADNS5050_CS_PIN); +} -void adns5050_cs_deselect(void) { writePinHigh(ADNS5050_CS_PIN); } +void adns5050_cs_deselect(void) { + writePinHigh(ADNS5050_CS_PIN); +} uint8_t adns5050_serial_read(void) { setPinInput(ADNS5050_SDIO_PIN); @@ -190,7 +194,7 @@ int8_t convert_twoscomp(uint8_t data) { // Don't forget to use the definitions for CPI in the header file. void adns5050_set_cpi(uint16_t cpi) { - uint8_t cpival = constrain((cpi / 125), 0x1, 0xD); // limits to 0--119 + uint8_t cpival = constrain((cpi / 125), 0x1, 0xD); // limits to 0--119 adns5050_write_reg(REG_MOUSE_CONTROL2, 0b10000 | cpival); } diff --git a/drivers/sensors/adns9800.c b/drivers/sensors/adns9800.c index c52f991804..3633f23e52 100644 --- a/drivers/sensors/adns9800.c +++ b/drivers/sensors/adns9800.c @@ -77,7 +77,9 @@ #define MSB1 0x80 // clang-format on -void adns9800_spi_start(void) { spi_start(ADNS9800_CS_PIN, false, ADNS9800_SPI_MODE, ADNS9800_SPI_DIVISOR); } +void adns9800_spi_start(void) { + spi_start(ADNS9800_CS_PIN, false, ADNS9800_SPI_MODE, ADNS9800_SPI_DIVISOR); +} void adns9800_write(uint8_t reg_addr, uint8_t data) { adns9800_spi_start(); @@ -135,10 +137,8 @@ void adns9800_init() { wait_us(15); // send all bytes of the firmware - unsigned char c; - for (int i = 0; i < FIRMWARE_LENGTH; i++) { - c = (unsigned char)pgm_read_byte(adns9800_firmware_data + i); - spi_write(c); + for (uint16_t i = 0; i < FIRMWARE_LENGTH; i++) { + spi_write(pgm_read_byte(firmware_data + i)); wait_us(15); } @@ -154,8 +154,8 @@ void adns9800_init() { } config_adns9800_t adns9800_get_config(void) { - uint8_t config_1 = adns9800_read(REG_Configuration_I); - return (config_adns9800_t){(config_1 & 0xFF) * CPI_STEP}; + uint8_t cpival = adns9800_read(REG_Configuration_I); + return (config_adns9800_t){(cpival & 0xFF) * CPI_STEP}; } void adns9800_set_config(config_adns9800_t config) { @@ -164,8 +164,8 @@ void adns9800_set_config(config_adns9800_t config) { } uint16_t adns9800_get_cpi(void) { - uint8_t config_1 = adns9800_read(REG_Configuration_I); - return (uint16_t){(config_1 & 0xFF) * CPI_STEP}; + uint8_t cpival = adns9800_read(REG_Configuration_I); + return (uint16_t)(cpival & 0xFF) * CPI_STEP; } void adns9800_set_cpi(uint16_t cpi) { @@ -184,7 +184,7 @@ static int16_t convertDeltaToInt(uint8_t high, uint8_t low) { } report_adns9800_t adns9800_get_report(void) { - report_adns9800_t report = {0, 0}; + report_adns9800_t report = {0}; adns9800_spi_start(); diff --git a/drivers/sensors/adns9800_srom_A6.h b/drivers/sensors/adns9800_srom_A6.h index d86ecbbd9e..e698a401b9 100644 --- a/drivers/sensors/adns9800_srom_A6.h +++ b/drivers/sensors/adns9800_srom_A6.h @@ -6,7 +6,7 @@ // clang-format off -const uint8_t adns9800_firmware_data[FIRMWARE_LENGTH] PROGMEM = { +const uint8_t firmware_data[FIRMWARE_LENGTH] PROGMEM = { 0x03, 0xA6, 0x68, 0x1E, 0x7D, 0x10, 0x7E, 0x7E, 0x5F, 0x1C, 0xB8, 0xF2, 0x47, 0x0C, 0x7B, 0x74, 0x4B, 0x14, 0x8B, 0x75, 0x66, 0x51, 0x0B, 0x8C, 0x76, 0x74, 0x4B, 0x14, 0xAA, 0xD6, 0x0F, 0x9C, 0xBA, 0xF6, 0x6E, 0x3F, 0xDD, 0x38, 0xD5, 0x02, 0x80, 0x9B, 0x82, 0x6D, 0x58, 0x13, 0xA4, 0xAB, diff --git a/drivers/sensors/analog_joystick.c b/drivers/sensors/analog_joystick.c index 1666bed047..95f8cff23a 100644 --- a/drivers/sensors/analog_joystick.c +++ b/drivers/sensors/analog_joystick.c @@ -24,7 +24,7 @@ uint16_t minAxisValue = ANALOG_JOYSTICK_AXIS_MIN; uint16_t maxAxisValue = ANALOG_JOYSTICK_AXIS_MAX; uint8_t maxCursorSpeed = ANALOG_JOYSTICK_SPEED_MAX; -uint8_t speedRegulator = ANALOG_JOYSTICK_SPEED_REGULATOR; // Lower Values Create Faster Movement +uint8_t speedRegulator = ANALOG_JOYSTICK_SPEED_REGULATOR; // Lower Values Create Faster Movement int16_t xOrigin, yOrigin; diff --git a/drivers/sensors/cirque_pinnacle.c b/drivers/sensors/cirque_pinnacle.c index b807c4f076..2db7f916fe 100644 --- a/drivers/sensors/cirque_pinnacle.c +++ b/drivers/sensors/cirque_pinnacle.c @@ -54,7 +54,9 @@ void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count); void RAP_Write(uint8_t address, uint8_t data); #ifdef CONSOLE_ENABLE -void print_byte(uint8_t byte) { xprintf("%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')); } +void print_byte(uint8_t byte) { + xprintf("%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')); +} #endif /* Logical Scaling Functions */ @@ -73,8 +75,12 @@ void ClipCoordinates(pinnacle_data_t* coordinates) { } } -uint16_t cirque_pinnacle_get_scale(void) { return scale_data; } -void cirque_pinnacle_set_scale(uint16_t scale) { scale_data = scale; } +uint16_t cirque_pinnacle_get_scale(void) { + return scale_data; +} +void cirque_pinnacle_set_scale(uint16_t scale) { + scale_data = scale; +} // Scales data to desired X & Y resolution void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResolution, uint16_t yResolution) { @@ -105,13 +111,13 @@ void cirque_pinnacle_clear_flags() { void cirque_pinnacle_enable_feed(bool feedEnable) { uint8_t temp; - RAP_ReadBytes(FEEDCONFIG_1, &temp, 1); // Store contents of FeedConfig1 register + RAP_ReadBytes(FEEDCONFIG_1, &temp, 1); // Store contents of FeedConfig1 register if (feedEnable) { - temp |= 0x01; // Set Feed Enable bit + temp |= 0x01; // Set Feed Enable bit RAP_Write(0x04, temp); } else { - temp &= ~0x01; // Clear Feed Enable bit + temp &= ~0x01; // Clear Feed Enable bit RAP_Write(0x04, temp); } } @@ -122,13 +128,13 @@ void cirque_pinnacle_enable_feed(bool feedEnable) { void ERA_ReadBytes(uint16_t address, uint8_t* data, uint16_t count) { uint8_t ERAControlValue = 0xFF; - cirque_pinnacle_enable_feed(false); // Disable feed + cirque_pinnacle_enable_feed(false); // Disable feed - RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Send upper byte of ERA address - RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Send lower byte of ERA address + RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Send upper byte of ERA address + RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Send lower byte of ERA address for (uint16_t i = 0; i < count; i++) { - RAP_Write(ERA_CONTROL, 0x05); // Signal ERA-read (auto-increment) to Pinnacle + RAP_Write(ERA_CONTROL, 0x05); // Signal ERA-read (auto-increment) to Pinnacle // Wait for status register 0x1E to clear do { @@ -145,14 +151,14 @@ void ERA_ReadBytes(uint16_t address, uint8_t* data, uint16_t count) { void ERA_WriteByte(uint16_t address, uint8_t data) { uint8_t ERAControlValue = 0xFF; - cirque_pinnacle_enable_feed(false); // Disable feed + cirque_pinnacle_enable_feed(false); // Disable feed - RAP_Write(ERA_VALUE, data); // Send data byte to be written + RAP_Write(ERA_VALUE, data); // Send data byte to be written - RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Upper byte of ERA address - RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Lower byte of ERA address + RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Upper byte of ERA address + RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Lower byte of ERA address - RAP_Write(ERA_CONTROL, 0x02); // Signal an ERA-write to Pinnacle + RAP_Write(ERA_CONTROL, 0x02); // Signal an ERA-write to Pinnacle // Wait for status register 0x1E to clear do { @@ -166,7 +172,7 @@ void cirque_pinnacle_set_adc_attenuation(uint8_t adcGain) { uint8_t temp = 0x00; ERA_ReadBytes(0x0187, &temp, 1); - temp &= 0x3F; // clear top two bits + temp &= 0x3F; // clear top two bits temp |= adcGain; ERA_WriteByte(0x0187, temp); ERA_ReadBytes(0x0187, &temp, 1); diff --git a/drivers/sensors/cirque_pinnacle.h b/drivers/sensors/cirque_pinnacle.h index db891122a6..c8cb360e03 100644 --- a/drivers/sensors/cirque_pinnacle.h +++ b/drivers/sensors/cirque_pinnacle.h @@ -26,16 +26,16 @@ void cirque_pinnacle_set_scale(uint16_t scale); // Coordinate scaling values #ifndef CIRQUE_PINNACLE_X_LOWER -# define CIRQUE_PINNACLE_X_LOWER 127 // min "reachable" X value +# define CIRQUE_PINNACLE_X_LOWER 127 // min "reachable" X value #endif #ifndef CIRQUE_PINNACLE_X_UPPER -# define CIRQUE_PINNACLE_X_UPPER 1919 // max "reachable" X value +# define CIRQUE_PINNACLE_X_UPPER 1919 // max "reachable" X value #endif #ifndef CIRQUE_PINNACLE_Y_LOWER -# define CIRQUE_PINNACLE_Y_LOWER 63 // min "reachable" Y value +# define CIRQUE_PINNACLE_Y_LOWER 63 // min "reachable" Y value #endif #ifndef CIRQUE_PINNACLE_Y_UPPER -# define CIRQUE_PINNACLE_Y_UPPER 1471 // max "reachable" Y value +# define CIRQUE_PINNACLE_Y_UPPER 1471 // max "reachable" Y value #endif #ifndef CIRQUE_PINNACLE_X_RANGE # define CIRQUE_PINNACLE_X_RANGE (CIRQUE_PINNACLE_X_UPPER - CIRQUE_PINNACLE_X_LOWER) diff --git a/drivers/sensors/cirque_pinnacle_i2c.c b/drivers/sensors/cirque_pinnacle_i2c.c index 81dd982b0c..8a38f1dcea 100644 --- a/drivers/sensors/cirque_pinnacle_i2c.c +++ b/drivers/sensors/cirque_pinnacle_i2c.c @@ -14,7 +14,7 @@ extern bool touchpad_init; /* RAP Functions */ // Reads <count> Pinnacle registers starting at <address> void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) { - uint8_t cmdByte = READ_MASK | address; // Form the READ command byte + uint8_t cmdByte = READ_MASK | address; // Form the READ command byte if (touchpad_init) { i2c_writeReg(CIRQUE_PINNACLE_ADDR << 1, cmdByte, NULL, 0, CIRQUE_PINNACLE_TIMEOUT); if (i2c_readReg(CIRQUE_PINNACLE_ADDR << 1, cmdByte, data, count, CIRQUE_PINNACLE_TIMEOUT) != I2C_STATUS_SUCCESS) { @@ -29,7 +29,7 @@ void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) { // Writes single-byte <data> to <address> void RAP_Write(uint8_t address, uint8_t data) { - uint8_t cmdByte = WRITE_MASK | address; // Form the WRITE command byte + uint8_t cmdByte = WRITE_MASK | address; // Form the WRITE command byte if (touchpad_init) { if (i2c_writeReg(CIRQUE_PINNACLE_ADDR << 1, cmdByte, &data, sizeof(data), CIRQUE_PINNACLE_TIMEOUT) != I2C_STATUS_SUCCESS) { diff --git a/drivers/sensors/cirque_pinnacle_spi.c b/drivers/sensors/cirque_pinnacle_spi.c index ed40abd9fa..e00e73eb8c 100644 --- a/drivers/sensors/cirque_pinnacle_spi.c +++ b/drivers/sensors/cirque_pinnacle_spi.c @@ -13,14 +13,14 @@ extern bool touchpad_init; /* RAP Functions */ // Reads <count> Pinnacle registers starting at <address> void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) { - uint8_t cmdByte = READ_MASK | address; // Form the READ command byte + uint8_t cmdByte = READ_MASK | address; // Form the READ command byte if (touchpad_init) { if (spi_start(CIRQUE_PINNACLE_SPI_CS_PIN, CIRQUE_PINNACLE_SPI_LSBFIRST, CIRQUE_PINNACLE_SPI_MODE, CIRQUE_PINNACLE_SPI_DIVISOR)) { spi_write(cmdByte); - spi_read(); // filler - spi_read(); // filler + spi_read(); // filler + spi_read(); // filler for (uint8_t i = 0; i < count; i++) { - data[i] = spi_read(); // each sepsequent read gets another register's contents + data[i] = spi_read(); // each sepsequent read gets another register's contents } } else { #ifdef CONSOLE_ENABLE @@ -34,7 +34,7 @@ void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) { // Writes single-byte <data> to <address> void RAP_Write(uint8_t address, uint8_t data) { - uint8_t cmdByte = WRITE_MASK | address; // Form the WRITE command byte + uint8_t cmdByte = WRITE_MASK | address; // Form the WRITE command byte if (touchpad_init) { if (spi_start(CIRQUE_PINNACLE_SPI_CS_PIN, CIRQUE_PINNACLE_SPI_LSBFIRST, CIRQUE_PINNACLE_SPI_MODE, CIRQUE_PINNACLE_SPI_DIVISOR)) { diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c index 7d390056ea..333e017a06 100644 --- a/drivers/sensors/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -33,8 +33,26 @@ static uint16_t precision = 128; -float pimoroni_trackball_get_precision(void) { return ((float)precision / 128); } -void pimoroni_trackball_set_precision(float floatprecision) { precision = (floatprecision * 128); } +uint16_t pimoroni_trackball_get_cpi(void) { + return (precision * 125); +} +/** + * @brief Sets the scaling value for pimoroni trackball + * + * Sets a scaling value for pimoroni trackball to allow runtime adjustment. This isn't used by the sensor and is an + * approximation so the functions are consistent across drivers. + * + * NOTE: This rounds down to the nearest number divisable by 125 that's a positive integer, values below 125 are clamped to 125. + * + * @param cpi uint16_t + */ +void pimoroni_trackball_set_cpi(uint16_t cpi) { + if (cpi < 249) { + precision = 1; + } else { + precision = (cpi - (cpi % 125)) / 125; + } +} void pimoroni_trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { uint8_t data[4] = {r, g, b, w}; @@ -60,7 +78,7 @@ i2c_status_t read_pimoroni_trackball(pimoroni_data_t* data) { return status; } -__attribute__((weak)) void pimironi_trackball_device_init(void) { +__attribute__((weak)) void pimoroni_trackball_device_init(void) { i2c_init(); pimoroni_trackball_set_rgbw(0x00, 0x00, 0x00, 0x00); } diff --git a/drivers/sensors/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h index 59ee8724ba..e20ee748a7 100644 --- a/drivers/sensors/pimoroni_trackball.h +++ b/drivers/sensors/pimoroni_trackball.h @@ -23,9 +23,6 @@ #ifndef PIMORONI_TRACKBALL_ADDRESS # define PIMORONI_TRACKBALL_ADDRESS 0x0A #endif -#ifndef PIMORONI_TRACKBALL_INTERVAL_MS -# define PIMORONI_TRACKBALL_INTERVAL_MS 8 -#endif #ifndef PIMORONI_TRACKBALL_SCALE # define PIMORONI_TRACKBALL_SCALE 5 #endif @@ -52,10 +49,10 @@ typedef struct { uint8_t click; } pimoroni_data_t; -void pimironi_trackball_device_init(void); +void pimoroni_trackball_device_init(void); void pimoroni_trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white); int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale); void pimoroni_trackball_adapt_values(int8_t* mouse, int16_t* offset); -float pimoroni_trackball_get_precision(void); -void pimoroni_trackball_set_precision(float precision); +uint16_t pimoroni_trackball_get_cpi(void); +void pimoroni_trackball_set_cpi(uint16_t cpi); i2c_status_t read_pimoroni_trackball(pimoroni_data_t* data); diff --git a/drivers/sensors/pmw3360.c b/drivers/sensors/pmw3360.c index 50d1c35801..8c977be1c8 100644 --- a/drivers/sensors/pmw3360.c +++ b/drivers/sensors/pmw3360.c @@ -16,136 +16,126 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "spi_master.h" #include "pmw3360.h" #include "wait.h" #include "debug.h" #include "print.h" -#include PMW3360_FIRMWARE_H +#include "pmw3360_firmware.h" // Registers // clang-format off -#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 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 // clang-format on +// limits to 0--119, resulting in a CPI range of 100 -- 12000 (as only steps of 100 are possible). #ifndef MAX_CPI -# define MAX_CPI 0x77 // limits to 0--119, should be max cpi/100 +# define MAX_CPI 0x77 #endif bool _inBurst = false; #ifdef CONSOLE_ENABLE -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')); } +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')); +} #endif #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) -bool spi_start_adv(void) { +bool pmw3360_spi_start(void) { bool status = spi_start(PMW3360_CS_PIN, PMW3360_SPI_LSBFIRST, PMW3360_SPI_MODE, PMW3360_SPI_DIVISOR); + // tNCS-SCLK, 120ns wait_us(1); return status; } -void spi_stop_adv(void) { - wait_us(1); - spi_stop(); -} +spi_status_t pmw3360_write(uint8_t reg_addr, uint8_t data) { + pmw3360_spi_start(); -spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) { if (reg_addr != REG_Motion_Burst) { _inBurst = false; } - spi_start_adv(); // send address of the register, with MSBit = 1 to indicate it's a write spi_status_t status = spi_write(reg_addr | 0x80); status = spi_write(data); - // tSCLK-NCS for write operation - wait_us(20); - - // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound - wait_us(100); + // tSCLK-NCS for write operation is 35us + wait_us(35); spi_stop(); + + // tSWW/tSWR (=180us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound + wait_us(145); return status; } -uint8_t spi_read_adv(uint8_t reg_addr) { - spi_start_adv(); +uint8_t pmw3360_read(uint8_t reg_addr) { + pmw3360_spi_start(); // send adress of the register, with MSBit = 0 to indicate it's a read spi_write(reg_addr & 0x7f); - + // tSRAD (=160us) + wait_us(160); uint8_t data = spi_read(); // tSCLK-NCS for read operation is 120ns wait_us(1); + spi_stop(); // tSRW/tSRR (=20us) minus tSCLK-NCS wait_us(19); - - spi_stop(); return data; } -void pmw3360_set_cpi(uint16_t cpi) { - uint8_t cpival = constrain((cpi / 100) - 1, 0, MAX_CPI); - - spi_start_adv(); - spi_write_adv(REG_Config1, cpival); - spi_stop(); -} - -uint16_t pmw3360_get_cpi(void) { - uint8_t cpival = spi_read_adv(REG_Config1); - return (uint16_t)((cpival + 1) & 0xFF) * 100; -} - bool pmw3360_init(void) { setPinOutput(PMW3360_CS_PIN); @@ -153,42 +143,51 @@ bool pmw3360_init(void) { _inBurst = false; spi_stop(); - spi_start_adv(); + pmw3360_spi_start(); spi_stop(); - spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first + pmw3360_write(REG_Shutdown, 0xb6); // Shutdown first wait_ms(300); - spi_start_adv(); + pmw3360_spi_start(); wait_us(40); - spi_stop_adv(); + spi_stop(); wait_us(40); - spi_write_adv(REG_Power_Up_Reset, 0x5a); + // power up, need to first drive NCS high then low, see above. + pmw3360_write(REG_Power_Up_Reset, 0x5a); wait_ms(50); - spi_read_adv(REG_Motion); - spi_read_adv(REG_Delta_X_L); - spi_read_adv(REG_Delta_X_H); - spi_read_adv(REG_Delta_Y_L); - spi_read_adv(REG_Delta_Y_H); + // 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); pmw3360_upload_firmware(); - spi_stop_adv(); + spi_stop(); wait_ms(10); pmw3360_set_cpi(PMW3360_CPI); wait_ms(1); - spi_write_adv(REG_Config2, 0x00); + pmw3360_write(REG_Config2, 0x00); - spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30)); + pmw3360_write(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -127, 127)); - spi_write_adv(REG_Lift_Config, PMW3360_LIFTOFF_DISTANCE); + pmw3360_write(REG_Lift_Config, PMW3360_LIFTOFF_DISTANCE); bool init_success = pmw3360_check_signature(); +#ifdef CONSOLE_ENABLE + if (init_success) { + dprintf("pmw3360 signature verified"); + } else { + dprintf("pmw3360 signature verification failed!"); + } +#endif writePinLow(PMW3360_CS_PIN); @@ -196,86 +195,94 @@ bool pmw3360_init(void) { } void pmw3360_upload_firmware(void) { - spi_write_adv(REG_SROM_Enable, 0x1d); + // Datasheet claims we need to disable REST mode first, but during startup + // it's already disabled and we're not turning it on ... + // pmw3360_write(REG_Config2, 0x00); // disable REST mode + pmw3360_write(REG_SROM_Enable, 0x1d); wait_ms(10); - spi_write_adv(REG_SROM_Enable, 0x18); + pmw3360_write(REG_SROM_Enable, 0x18); - spi_start_adv(); + pmw3360_spi_start(); spi_write(REG_SROM_Load_Burst | 0x80); wait_us(15); - unsigned char c; - for (int i = 0; i < FIRMWARE_LENGTH; i++) { - c = (unsigned char)pgm_read_byte(firmware_data + i); - spi_write(c); + for (uint16_t i = 0; i < FIRMWARE_LENGTH; i++) { + spi_write(pgm_read_byte(firmware_data + i)); +#ifndef PMW3360_FIRMWARE_UPLOAD_FAST wait_us(15); +#endif } wait_us(200); - spi_read_adv(REG_SROM_ID); + pmw3360_read(REG_SROM_ID); + pmw3360_write(REG_Config2, 0x00); +} - spi_write_adv(REG_Config2, 0x00); +bool pmw3360_check_signature(void) { + 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); + return (pid == firmware_signature[0] && iv_pid == firmware_signature[1] && SROM_ver == firmware_signature[2]); // signature for SROM 0x04 +} - spi_stop(); - wait_ms(10); +uint16_t pmw3360_get_cpi(void) { + uint8_t cpival = pmw3360_read(REG_Config1); + return (uint16_t)((cpival + 1) & 0xFF) * CPI_STEP; } -bool pmw3360_check_signature(void) { - uint8_t pid = spi_read_adv(REG_Product_ID); - uint8_t iv_pid = spi_read_adv(REG_Inverse_Product_ID); - uint8_t SROM_ver = spi_read_adv(REG_SROM_ID); - return (pid == firmware_signature[0] && iv_pid == firmware_signature[1] && SROM_ver == firmware_signature[2]); // signature for SROM 0x04 +void pmw3360_set_cpi(uint16_t cpi) { + uint8_t cpival = constrain((cpi / CPI_STEP) - 1, 0, MAX_CPI); + pmw3360_write(REG_Config1, cpival); } report_pmw3360_t pmw3360_read_burst(void) { + report_pmw3360_t report = {0}; + if (!_inBurst) { #ifdef CONSOLE_ENABLE dprintf("burst on"); #endif - spi_write_adv(REG_Motion_Burst, 0x00); + pmw3360_write(REG_Motion_Burst, 0x00); _inBurst = true; } - spi_start_adv(); + pmw3360_spi_start(); spi_write(REG_Motion_Burst); - wait_us(35); // waits for tSRAD + wait_us(35); // waits for tSRAD_MOTBR - report_pmw3360_t data = {0}; + report.motion = spi_read(); + spi_read(); // skip Observation + // delta registers + report.dx = spi_read(); + report.mdx = spi_read(); + report.dy = spi_read(); + report.mdy = spi_read(); - data.motion = spi_read(); - spi_write(0x00); // skip Observation - data.dx = spi_read(); - data.mdx = spi_read(); - data.dy = spi_read(); - data.mdy = spi_read(); + if (report.motion & 0b111) { // panic recovery, sometimes burst mode works weird. + _inBurst = false; + } spi_stop(); #ifdef CONSOLE_ENABLE if (debug_mouse) { - print_byte(data.motion); - print_byte(data.dx); - print_byte(data.mdx); - print_byte(data.dy); - print_byte(data.mdy); + print_byte(report.motion); + print_byte(report.dx); + print_byte(report.mdx); + print_byte(report.dy); + print_byte(report.mdy); dprintf("\n"); } #endif - data.isMotion = (data.motion & 0x80) != 0; - data.isOnSurface = (data.motion & 0x08) == 0; - data.dx |= (data.mdx << 8); - data.dx = data.dx * -1; - data.dy |= (data.mdy << 8); - data.dy = data.dy * -1; - - spi_stop(); + 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; - if (data.motion & 0b111) { // panic recovery, sometimes burst mode works weird. - _inBurst = false; - } - - return data; + return report; } diff --git a/drivers/sensors/pmw3360.h b/drivers/sensors/pmw3360.h index 9aa8e13f8e..eec7295871 100644 --- a/drivers/sensors/pmw3360.h +++ b/drivers/sensors/pmw3360.h @@ -19,8 +19,6 @@ #pragma once #include <stdint.h> -#include "report.h" -#include "spi_master.h" #ifndef PMW3360_CPI # define PMW3360_CPI 1600 @@ -58,41 +56,20 @@ # error "No chip select pin defined -- missing PMW3360_CS_PIN" #endif -/* -The pmw33660 and pmw3389 use the same registers and timing and such. -The only differences between the two is the firmware used, and the -range for the DPI. So add a semi-secret hack to allow use of the -pmw3389's firmware blob. Also, can set the max cpi range too. -This should work for the 3390 and 3391 too, in theory. -*/ -#ifndef PMW3360_FIRMWARE_H -# define PMW3360_FIRMWARE_H "pmw3360_firmware.h" -#endif - -#ifdef CONSOLE_ENABLE -void print_byte(uint8_t byte); -#endif - typedef struct { int8_t motion; - bool isMotion; // True if a motion is detected. - bool isOnSurface; // True when a chip is on a surface - int16_t dx; // displacement on x directions. Unit: Count. (CPI * Count = Inch value) + bool isMotion; // True if a motion is detected. + bool isOnSurface; // True when a chip is on a surface + int16_t dx; // displacement on x directions. Unit: Count. (CPI * Count = Inch value) int8_t mdx; - int16_t dy; // displacement on y directions. + int16_t dy; // displacement on y directions. int8_t mdy; } report_pmw3360_t; -bool spi_start_adv(void); -void spi_stop_adv(void); -spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data); -uint8_t spi_read_adv(uint8_t reg_addr); -bool pmw3360_init(void); -void pmw3360_set_cpi(uint16_t cpi); -uint16_t pmw3360_get_cpi(void); -void pmw3360_upload_firmware(void); -bool pmw3360_check_signature(void); +bool pmw3360_init(void); +void pmw3360_upload_firmware(void); +bool pmw3360_check_signature(void); +uint16_t pmw3360_get_cpi(void); +void pmw3360_set_cpi(uint16_t cpi); +/* Reads and clears the current delta values on the sensor */ report_pmw3360_t pmw3360_read_burst(void); - -#define degToRad(angleInDegrees) ((angleInDegrees)*M_PI / 180.0) -#define radToDeg(angleInRadians) ((angleInRadians)*180.0 / M_PI) diff --git a/drivers/sensors/pmw3389.c b/drivers/sensors/pmw3389.c new file mode 100644 index 0000000000..828dafa134 --- /dev/null +++ b/drivers/sensors/pmw3389.c @@ -0,0 +1,294 @@ +/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com> + * Copyright 2019 Sunjun Ki |