Skip to content

Commit

Permalink
Update driver files more
Browse files Browse the repository at this point in the history
  • Loading branch information
drashna committed Jun 7, 2021
1 parent 3e88d05 commit 6497558
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
11 changes: 3 additions & 8 deletions drivers/sensors/adns5050.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@


#include "adns5050.h"
#include "quantum.h"
#include "wait.h"

#ifdef CONSOLE_ENABLE
# include "print.h"
#endif
#include "debug.h"
#include "print.h"

#ifndef OPTIC_ROTATED
# define OPTIC_ROTATED false
#endif

// Definitions for the ADNS serial line.
// These really ought to be defined in your config.h, but defaults are
// here if you're really lazy.
#ifndef ADNS_SCLK_PIN
# define ADNS_SCLK_PIN B7
#endif
Expand All @@ -42,7 +37,7 @@
#endif

#ifndef ADNS_CS_PIN
# define ADNS_CS_PIN B4
# define ADNS_CS_PIN SPI_SS_PIN
#endif

#ifdef CONSOLE_ENABLE
Expand Down
39 changes: 29 additions & 10 deletions drivers/sensors/pmw3360.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@ bool _inBurst = false;
#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 2
# 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

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(SPI_SS_PIN, false, 3, SPI_DIVISOR);
bool status = spi_start(PMW_CS_PIN, false, SPI_MODE, SPI_DIVISOR);
wait_us(1);
return status;
}
Expand All @@ -56,7 +64,7 @@ spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) {
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);
status = spi_write(data);

// tSCLK-NCS for write operation
wait_us(20);
Expand Down Expand Up @@ -85,22 +93,29 @@ uint8_t spi_read_adv(uint8_t reg_addr) {
}

void pmw_set_cpi(uint16_t cpi) {
int cpival = constrain((cpi / 100) - 1, 0, 0x77); // limits to 0--119
uint8_t cpival = constrain((cpi / 100) - 1, 0, 0x77); // limits to 0--119

spi_start_adv();
spi_write_adv(REG_Config1, cpival);
spi_stop();
}

uint16_t pmw_get_cpi(void) {
uint8_t cpival = spi_read_adv(REG_Config1);
return (uint16_t)(cpival & 0xFF) * 100;
}

bool pmw_spi_init(void) {
setPinOutput(PMW_CS_PIN);

spi_init();
_inBurst = false;

spi_stop();
spi_start_adv();
spi_stop();

spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first
spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first
wait_ms(300);

spi_start_adv();
Expand All @@ -126,14 +141,18 @@ bool pmw_spi_init(void) {

wait_ms(1);

return pmw_check_signature();
}

void pmw_upload_firmware(void) {
spi_write_adv(REG_Config2, 0x00);

spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30));

bool init_success = pmw_check_signature();

writePinLow(PMW_CS_PIN);

return init_success;
}

void pmw_upload_firmware(void) {
spi_write_adv(REG_SROM_Enable, 0x1d);

wait_ms(10);
Expand Down
1 change: 1 addition & 0 deletions drivers/sensors/pmw3360.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data);
uint8_t spi_read_adv(uint8_t reg_addr);
bool pmw_spi_init(void);
void pmw_set_cpi(uint16_t cpi);
uint16_t pmw_get_cpi(void);
void pmw_upload_firmware(void);
bool pmw_check_signature(void);
report_pmw_t pmw_read_burst(void);
Expand Down

0 comments on commit 6497558

Please sign in to comment.