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

Oddball keyboard - trackball improvements #8657

Closed
wants to merge 24 commits into from
Closed
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
180 changes: 70 additions & 110 deletions keyboards/oddball/adns.c → keyboards/oddball/adns/adns.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#include <avr/io.h>
#include <avr/interrupt.h>
/* Copyright 2020 Alexander Tulloh
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "spi_master.h"
#include "quantum.h"
#include "pointing_device.h"
#include "adns9800_srom_A4.h"
#include "../../lib/lufa/LUFA/Drivers/Peripheral/SPI.h"
#include "adns9800_srom_A6.h"
#include "adns.h"

// registers
#define REG_Product_ID 0x00
Expand Down Expand Up @@ -52,103 +66,57 @@
#define REG_SROM_Load_Burst 0x62
#define REG_Pixel_Burst 0x64

// pins
#define NCS 0
#define ADNS_SPI_MODE 3
#define ADNS_SPI_DIVISOR 8

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_spi_start(void){
spi_start(SPI_SS_PIN, false, ADNS_SPI_MODE, ADNS_SPI_DIVISOR);
}

void adns_write(uint8_t reg_addr, uint8_t data){

adns_begin();
adns_spi_start();

//send address of the register, with MSBit = 1 to indicate it's a write
SPI_TransferByte(reg_addr | 0x80 );
SPI_TransferByte(data);
spi_write(reg_addr | 0x80 );
spi_write(data);

// tSCLK-NCS for write operation
wait_us(20);

adns_end();
spi_stop();

// 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();
adns_spi_start();

// 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);
spi_write(reg_addr & 0x7f );
uint8_t data = spi_read();

// tSCLK-NCS for read operation is 120ns
wait_us(1);

adns_end();
spi_stop();

// 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<<INT2);

// mode 3
SPI_Init(
SPI_SPEED_FCPU_DIV_8 |
SPI_ORDER_MSB_FIRST |
SPI_SCK_LEAD_FALLING |
SPI_SAMPLE_TRAILING |
SPI_MODE_MASTER);
void adns_init() {

// set B0 output
DDRB |= (1 << 0);
setPinOutput(SPI_SS_PIN);

// reset serial port
adns_end();
adns_begin();
adns_end();
spi_init();

// reboot
adns_write(REG_Power_Up_Reset, 0x5a);
Expand Down Expand Up @@ -178,21 +146,21 @@ void pointing_device_init(void) {
adns_write(REG_SROM_Enable, 0x18);

// write the SROM file (=firmware data)
adns_begin();
adns_spi_start();

// write burst destination adress
SPI_TransferByte(REG_SROM_Load_Burst | 0x80);
spi_write(REG_SROM_Load_Burst | 0x80);
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(firmware_data + i);
SPI_TransferByte(c);
spi_write(c);
wait_us(15);
}

adns_end();
spi_stop();

wait_ms(10);

Expand All @@ -204,36 +172,17 @@ void pointing_device_init(void) {
adns_write(REG_LASER_CTRL0, laser_ctrl0 & 0xf0);

wait_ms(1);

// set the configuration_I register to set the CPI
// 0x01 = 50, minimum
// 0x44 = 3400, default
// 0x8e = 7100
// 0xA4 = 8200, maximum
adns_write(REG_Configuration_I, 0x04);

wait_ms(100);
}

void pointing_device_task(void) {

if(!is_keyboard_master())
return;

report_mouse_t report = pointing_device_get_report();

// clamp deltas from -127 to 127
report.x = delta_x < -127 ? 127 : delta_x > 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;
config_adns_t adns_get_config(void) {
uint8_t config_1 = adns_read(REG_Configuration_I);
return (config_adns_t){ (config_1 & 0xFF) * 200 };
}

pointing_device_set_report(report);
pointing_device_send();
void adns_set_config(config_adns_t config) {
uint8_t config_1 = (config.cpi / 200) & 0xFF;
adns_write(REG_Configuration_I, config_1);
wait_ms(100);
}

int16_t convertDeltaToInt(uint8_t high, uint8_t low){
Expand All @@ -243,28 +192,39 @@ int16_t convertDeltaToInt(uint8_t high, uint8_t low){

// convert twos comp to int
if (twos_comp & 0x8000)
return -1 * ((twos_comp ^ 0xffff) + 1);
return -1 * (~twos_comp + 1);

return twos_comp;
}

ISR(INT2_vect) {
// called on interrupt 2 when sensed motion
// copy burst data from the respective registers
report_adns_t adns_get_report(void) {

report_adns_t report = {0, 0};

adns_spi_start();

// start burst mode
spi_write(REG_Motion_Burst & 0x7f);

// motion register
uint8_t motion = spi_read();

adns_begin();
if(motion & 0x80) {

// send adress of the register, with MSBit = 1 to indicate it's a write
SPI_TransferByte(REG_Motion_Burst & 0x7f);
// clear observation register
spi_read();

uint8_t burst_data[pixel_sum];
// delta registers
uint8_t delta_x_l = spi_read();
uint8_t delta_x_h = spi_read();
uint8_t delta_y_l = spi_read();
uint8_t delta_y_h = spi_read();

for (int i = 0; i < pixel_sum; ++i) {
burst_data[i] = SPI_TransferByte(0);
report.x = convertDeltaToInt(delta_x_h, delta_x_l);
report.y = convertDeltaToInt(delta_y_h, delta_y_l);
}

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]);
spi_stop();

adns_end();
return report;
}
35 changes: 35 additions & 0 deletions keyboards/oddball/adns/adns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2020 Alexander Tulloh
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdint.h>

typedef struct {
/* 200 - 8200 CPI supported */
uint16_t cpi;
} config_adns_t;

typedef struct {
int16_t x;
int16_t y;
} report_adns_t;

void adns_init(void);
config_adns_t adns_get_config(void);
void adns_set_config(config_adns_t);
/* Reads and clears the current delta values on the ADNS sensor */
report_adns_t adns_get_report(void);
Loading