From e72562fe6fe3f3669559f88ba11d1074ad0e4ffa Mon Sep 17 00:00:00 2001 From: Alexander Tulloh Date: Wed, 11 Mar 2020 18:56:05 +1100 Subject: [Keyboard] Oddball keyboard (#8352) * Initial commit of oddball keyboard * Update oddball project url * Update pointer functions to only run on master side * Add unique product version * Capitalise product name * Convert oddball keymap layer flags to enum * Remove commented keyboard boilerplate code * Remove unused keymap config * Fix incorrect layout in info.json * Add markdown link text in readme --- keyboards/oddball/adns.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 keyboards/oddball/adns.c (limited to 'keyboards/oddball/adns.c') diff --git a/keyboards/oddball/adns.c b/keyboards/oddball/adns.c new file mode 100644 index 000000000..35225f042 --- /dev/null +++ b/keyboards/oddball/adns.c @@ -0,0 +1,270 @@ +#include +#include +#include "quantum.h" +#include "pointing_device.h" +#include "adns9800_srom_A4.h" +#include "../../lib/lufa/LUFA/Drivers/Peripheral/SPI.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_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 + +// pins +#define NCS 0 + +extern const uint16_t firmware_length; +extern const uint8_t firmware_data[]; + +enum motion_burst_property{ + 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 +}; + +// used to track the motion delta between updates +volatile int32_t delta_x; +volatile int32_t delta_y; + +void adns_begin(void){ + PORTB &= ~ (1 << NCS); +} + +void adns_end(void){ + PORTB |= (1 << NCS); +} + +void adns_write(uint8_t reg_addr, uint8_t data){ + + adns_begin(); + + //send address of the register, with MSBit = 1 to indicate it's a write + SPI_TransferByte(reg_addr | 0x80 ); + SPI_TransferByte(data); + + // tSCLK-NCS for write operation + wait_us(20); + + adns_end(); + + // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound + wait_us(100); +} + +uint8_t adns_read(uint8_t reg_addr){ + + adns_begin(); + + // send adress of the register, with MSBit = 0 to indicate it's a read + SPI_TransferByte(reg_addr & 0x7f ); + uint8_t data = SPI_TransferByte(0); + + // tSCLK-NCS for read operation is 120ns + wait_us(1); + + adns_end(); + + // tSRW/tSRR (=20us) minus tSCLK-NCS + wait_us(19); + + return data; +} + +void pointing_device_init(void) { + + if(!is_keyboard_master()) + return; + + // interrupt 2 + EICRA &= ~(1 << 4); + EICRA |= (1 << 5); + EIMSK |= (1< 127 ? 127 : delta_x; + report.x = -report.x; + + report.y = delta_y < -127 ? 127 : delta_y > 127 ? 127 : delta_y; + + // reset deltas + delta_x = 0; + delta_y = 0; + + pointing_device_set_report(report); + pointing_device_send(); +} + +int16_t convertDeltaToInt(uint8_t high, uint8_t low){ + + // join bytes into twos compliment + uint16_t twos_comp = (high << 8) | low; + + // convert twos comp to int + if (twos_comp & 0x8000) + return -1 * ((twos_comp ^ 0xffff) + 1); + + return twos_comp; +} + +ISR(INT2_vect) { + // called on interrupt 2 when sensed motion + // copy burst data from the respective registers + + adns_begin(); + + // send adress of the register, with MSBit = 1 to indicate it's a write + SPI_TransferByte(REG_Motion_Burst & 0x7f); + + uint8_t burst_data[pixel_sum]; + + for (int i = 0; i < pixel_sum; ++i) { + burst_data[i] = SPI_TransferByte(0); + } + + delta_x += convertDeltaToInt(burst_data[delta_x_h], burst_data[delta_x_l]); + delta_y += convertDeltaToInt(burst_data[delta_y_h], burst_data[delta_y_l]); + + adns_end(); +} -- cgit v1.2.3