#include "spi_master.h"
#include "adns.h"
#include "debug.h"
#include "quantum.h"
#include "pointing_device.h"
#include "adns9800_srom_A6.h"
#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_Pixel_Sum 0x08
#define REG_Maximum_Pixel 0x09
#define REG_Minimum_Pixel 0x0a
#define REG_Shutter_Lower 0x0b
#define REG_Shutter_Upper 0x0c
#define REG_Frame_Period_Lower 0x0d
#define REG_Frame_Period_Upper 0x0e
#define REG_Configuration_I 0x0f
#define REG_Configuration_II 0x10
#define REG_Frame_Capture 0x12
#define REG_SROM_Enable 0x13
#define REG_Run_Downshift 0x14
#define REG_Rest1_Rate 0x15
#define REG_Rest1_Downshift 0x16
#define REG_Rest2_Rate 0x17
#define REG_Rest2_Downshift 0x18
#define REG_Rest3_Rate 0x19
#define REG_Frame_Period_Max_Bound_Lower 0x1a
#define REG_Frame_Period_Max_Bound_Upper 0x1b
#define REG_Frame_Period_Min_Bound_Lower 0x1c
#define REG_Frame_Period_Min_Bound_Upper 0x1d
#define REG_Shutter_Max_Bound_Lower 0x1e
#define REG_Shutter_Max_Bound_Upper 0x1f
#define REG_LASER_CTRL0 0x20
#define REG_Observation 0x24
#define REG_Data_Out_Lower 0x25
#define REG_Data_Out_Upper 0x26
#define REG_SROM_ID 0x2a
#define REG_Lift_Detection_Thr 0x2e
#define REG_Configuration_V 0x2f
#define REG_Configuration_IV 0x39
#define REG_Power_Up_Reset 0x3a
#define REG_Shutdown 0x3b
#define REG_Inverse_Product_ID 0x3f
#define REG_Motion_Burst 0x50
#define REG_SROM_Load_Burst 0x62
#define REG_Pixel_Burst 0x64
#define NCS F7
extern const uint16_t firmware_length;
extern const uint8_t firmware_data[];
enum motion_burst_propertr{
motion = 0,
observation,
delta_x_l,
delta_x_h,
delta_y_l,
delta_y_h,
squal,
pixel_sum,
maximum_pixel,
minimum_pixel,
shutter_upper,
shutter_lower,
frame_period_upper,
frame_period_lower,
end_data
};
void adns_begin(void){
spi_start(NCS, false, 3, 8);
}
void adns_end(void){
spi_stop();
}
void adns_write(uint8_t reg_addr, uint8_t data){
adns_begin();
spi_write(reg_addr | 0x80 );
spi_write(data);
wait_us(20);
wait_us(100);
adns_end();
}
uint8_t adns_read(uint8_t reg_addr){
adns_begin();
spi_write(reg_addr & 0x7f );
uint8_t data = spi_read();
wait_us(1);
wait_us(19);
adns_end();
return data;
}
void pointing_device_init(void) {
dprint("STARTING INTI\n");
spi_init();
adns_begin();
adns_end();
adns_write(REG_Power_Up_Reset, 0x5a);
wait_ms(50);
adns_read(REG_Motion);
adns_read(REG_Delta_X_L);
adns_read(REG_Delta_X_H);
adns_read(REG_Delta_Y_L);
adns_read(REG_Delta_Y_H);
adns_write(REG_Configuration_IV, 0x02);
adns_write(REG_SROM_Enable, 0x1d);
wait_ms(10);
adns_write(REG_SROM_Enable, 0x18);
adns_begin();
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);
wait_us(15);
}
adns_end();
wait_ms(10);
uint8_t laser_ctrl0 = adns_read(REG_LASER_CTRL0);
adns_write(REG_LASER_CTRL0, laser_ctrl0 & 0xf0);
wait_ms(1);
adns_write(REG_Configuration_I, 0x10);
wait_ms(100);
dprint("INIT ENDED\n");
}
int16_t convertDeltaToInt(uint8_t high, uint8_t low){
return (high << 8) | low;
}
motion_delta_t readSensor(void) {
adns_begin();
spi_write(REG_Motion_Burst & 0x7f);
wait_us(100);
uint8_t burst_data[pixel_sum];
for (int i = 0; i < pixel_sum; ++i) {
burst_data[i] = spi_read();
}
uint16_t delta_x = convertDeltaToInt(burst_data[delta_x_h], burst_data[delta_x_l]);
uint16_t delta_y = convertDeltaToInt(burst_data[delta_y_h], burst_data[delta_y_l]);
uint8_t motion_ind = burst_data[motion] & 0b10000000;
adns_end();
motion_delta_t delta = {delta_x, delta_y, motion_ind};
return delta;
}
void pointing_device_task(void) {
motion_delta_t delta = readSensor();
report_mouse_t report = pointing_device_get_report();
if(delta.motion_ind) {
report.x = delta.delta_x < -127 ? -127 : delta.delta_x > 127 ? 127 : delta.delta_x;
report.x = -report.x;
report.y = delta.delta_y < -127 ? -127 : delta.delta_y > 127 ? 127 : delta.delta_y;
}
pointing_device_set_report(report);
pointing_device_send();
}