Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pmw3360 sensor and make it more hardware agnostic #14097

Merged
merged 2 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 56 additions & 24 deletions drivers/sensors/pmw3360.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,69 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "pmw3360.h"
#include "wait.h"
#include "debug.h"
#include "print.h"
#include "pmw3360.h"
#include "pmw3360_firmware.h"

bool _inBurst = false;
// Registers
#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

#ifndef PMW_CPI
# define PMW_CPI 1600
#endif
#ifndef PMW_CLOCK_SPEED
# define PMW_CLOCK_SPEED 70000000
#endif
#ifndef SPI_MODE
# define SPI_MODE 3
#endif
#ifndef SPI_DIVISOR
# define SPI_DIVISOR (F_CPU / PMW_CLOCK_SPEED)
#endif
#ifndef ROTATIONAL_TRANSFORM_ANGLE
# define ROTATIONAL_TRANSFORM_ANGLE 0x00
#endif
#ifndef PMW_CS_PIN
# define PMW_CS_PIN SPI_SS_PIN
#endif
bool _inBurst = false;

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')); }

bool spi_start_adv(void) {
bool status = spi_start(PMW_CS_PIN, false, SPI_MODE, SPI_DIVISOR);
bool status = spi_start(PMW3360_CS_PIN, PMW3360_SPI_LSBFIRST, PMW3360_SPI_MODE, PMW3360_SPI_DIVISOR);
wait_us(1);
return status;
}
Expand Down Expand Up @@ -106,7 +138,7 @@ uint16_t pmw_get_cpi(void) {
}

bool pmw_spi_init(void) {
setPinOutput(PMW_CS_PIN);
setPinOutput(PMW3360_CS_PIN);

spi_init();
_inBurst = false;
Expand Down Expand Up @@ -137,7 +169,7 @@ bool pmw_spi_init(void) {
spi_stop_adv();

wait_ms(10);
pmw_set_cpi(PMW_CPI);
pmw_set_cpi(PMW3360_CPI);

wait_ms(1);

Expand All @@ -147,7 +179,7 @@ bool pmw_spi_init(void) {

bool init_success = pmw_check_signature();

writePinLow(PMW_CS_PIN);
writePinLow(PMW3360_CS_PIN);

return init_success;
}
Expand Down
81 changes: 31 additions & 50 deletions drivers/sensors/pmw3360.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,37 @@

#include "spi_master.h"

// Registers
#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
#ifndef PMW3360_CPI
# define PMW3360_CPI 1600
#endif

#ifndef PMW3360_CLOCK_SPEED
# define PMW3360_CLOCK_SPEED 70000000
#endif

#ifndef PMW3360_SPI_LSBFIRST
# define PMW3360_SPI_LSBFIRST false
#endif

#ifndef PMW3360_SPI_MODE
# define PMW3360_SPI_MODE 3
#endif

#ifndef PMW3360_SPI_DIVISOR
# ifdef __AVR__
# define PMW3360_SPI_DIVISOR (F_CPU / PMW3360_CLOCK_SPEED)
# else
# define EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR 64
# endif
#endif

#ifndef ROTATIONAL_TRANSFORM_ANGLE
# define ROTATIONAL_TRANSFORM_ANGLE 0x00
#endif

#ifndef PMW3360_CS_PIN
# error "No chip select pin defined -- missing PMW3360_CS_PIN"
#endif

#ifdef CONSOLE_ENABLE
void print_byte(uint8_t byte);
Expand Down
3 changes: 3 additions & 0 deletions keyboards/handwired/tractyl_manuform/4x6_right/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#define SERIAL_USE_MULTI_TRANSACTION
#define SPLIT_TRANSACTION_IDS_KB RPC_ID_STATE_SYNC, RPC_ID_SLAVE_STATE

/* PMW3360 Settings */
#define PMW3360_CS_PIN B0
3 changes: 3 additions & 0 deletions keyboards/handwired/tractyl_manuform/5x6_right/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#define SERIAL_USE_MULTI_TRANSACTION
#define SPLIT_TRANSACTION_IDS_KB RPC_ID_STATE_SYNC, RPC_ID_SLAVE_STATE

/* PMW3360 Settings */
#define PMW3360_CS_PIN B0
3 changes: 3 additions & 0 deletions keyboards/ploopyco/mouse/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@
#define RGBLIGHT_EFFECT_RAINBOW_SWIRL

// #define DEBUG_LED_PIN F7

/* PMW3360 Settings */
#define PMW3360_CS_PIN B0
3 changes: 3 additions & 0 deletions keyboards/ploopyco/trackball/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@

// If board has a debug LED, you can enable it by defining this
// #define DEBUG_LED_PIN F7

/* PMW3360 Settings */
#define PMW3360_CS_PIN B0