Signed-off-by: Gergely Nagy <algernon@keyboard.io>
hardware/ploopy
Gergely Nagy 2 years ago
parent 9fbb820638
commit 0d9550a5cb
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -34,7 +34,7 @@ void rebootBootloader() {
}
static constexpr uint8_t direct_pins[5] = {PIN_D4, PIN_D2, PIN_E6, PIN_B6, PIN_D7};
static constexpr uint8_t unused_pins[12] = {PIN_B5, PIN_C7, PIN_D0, PIN_D1, PIN_D3, PIN_D5, PIN_D6, PIN_F1, PIN_F3, PIN_F5, PIN_F6, PIN_F7};
static constexpr uint8_t unused_pins[14] = {PIN_B5, PIN_C7, PIN_D0, PIN_D1, PIN_D3, PIN_D5, PIN_D6, PIN_F1, PIN_F3, PIN_F5, PIN_F6, PIN_F7, PIN_F0, PIN_F4};
void mcuSetup() {
wdt_disable();
@ -68,7 +68,10 @@ void setup() {
buttonSetup();
adns5050_init();
adns5050_sync();
//delay(100);
//adns5050_sync();
adns5050_set_cpi(CPI375);
//adns5050_read_burst();
while (!Serial);
@ -97,7 +100,21 @@ void handleSerial() {
void loop() {
handleSerial();
//report_adns5050_t r = adns505
#if 1
report_adns5050_t r = adns5050_read_burst();
if (r.dx != 0 || r.dy != 0) {
Serial.println(r.dx);
Serial.println(r.dy);
}
#else
uint8_t dx = adns5050_read_reg(0x03); // DELTA_X
uint8_t dy = adns5050_read_reg(0x04); // DELTA_Y
Serial.print(dx);
Serial.print(",");
Serial.println(dy);
#endif
delay(250);
delay(10);
}

@ -21,6 +21,29 @@
#include "Arduino.h"
#include "pins_and_ports.h"
#include <util/delay.h>
#define wait_ms(ms) \
do { \
if (__builtin_constant_p(ms)) { \
_delay_ms(ms); \
} else { \
for (uint16_t i = ms; i > 0; i--) { \
_delay_ms(1); \
} \
} \
} while (0)
#define wait_us(us) \
do { \
if (__builtin_constant_p(us)) { \
_delay_us(us); \
} else { \
for (uint16_t i = us; i > 0; i--) { \
_delay_us(1); \
} \
} \
} while (0)
// Registers
// clang-format off
#define REG_PRODUCT_ID 0x00
@ -56,7 +79,7 @@ void adns5050_init(void) {
// wait maximum time before adns is ready.
// this ensures that the adns is actuall ready after reset.
delay(55);
wait_ms(55);
// read a burst from the adns and then discard it.
// gets the adns ready for write commands
@ -69,7 +92,7 @@ void adns5050_init(void) {
// synchronization signal to the master.
void adns5050_sync(void) {
OUTPUT_LOW(ADNS5050_CS_PIN);
delayMicroseconds(1);
wait_us(1);
OUTPUT_HIGH(ADNS5050_CS_PIN);
}
@ -89,12 +112,12 @@ uint8_t adns5050_serial_read(void) {
for (uint8_t i = 0; i < 8; ++i) {
OUTPUT_LOW(ADNS5050_SCLK_PIN);
delayMicroseconds(1);
wait_us(1);
byte = (byte << 1) | READ_PIN(ADNS5050_SDIO_PIN);
OUTPUT_HIGH(ADNS5050_SCLK_PIN);
delayMicroseconds(1);
wait_us(1);
}
return byte;
@ -111,7 +134,7 @@ void adns5050_serial_write(uint8_t data) {
else
OUTPUT_LOW(ADNS5050_SDIO_PIN);
delayMicroseconds(2);
wait_us(2);
OUTPUT_HIGH(ADNS5050_SCLK_PIN);
}
@ -119,7 +142,7 @@ void adns5050_serial_write(uint8_t data) {
// tSWR. See page 15 of the ADNS spec sheet.
// Technically, this is only necessary if the next operation is an SDIO
// read. This is not guaranteed to be the case, but we're being lazy.
delayMicroseconds(4);
wait_us(4);
// Note that tSWW is never necessary. All write operations require at
// least 32us, which exceeds tSWW, so there's never a need to wait for it.
@ -135,7 +158,7 @@ uint8_t adns5050_read_reg(uint8_t reg_addr) {
// We don't need a minimum tSRAD here. That's because a 4ms wait time is
// already included in adns5050_serial_write(), so we're good.
// See page 10 and 15 of the ADNS spec sheet.
// delayMicroseconds(4);
// wait_us(4);
uint8_t byte = adns5050_serial_read();
@ -143,7 +166,7 @@ uint8_t adns5050_read_reg(uint8_t reg_addr) {
// Technically, this is only necessary if the next operation is an SDIO
// read or write. This is not guaranteed to be the case.
// Honestly, this wait could probably be removed.
delayMicroseconds(1);
wait_us(1);
adns5050_cs_deselect();
@ -165,11 +188,12 @@ report_adns5050_t adns5050_read_burst(void) {
data.dy = 0;
adns5050_serial_write(REG_MOTION_BURST);
wait_us(4);
// We don't need a minimum tSRAD here. That's because a 4ms wait time is
// already included in adns5050_serial_write(), so we're good.
// See page 10 and 15 of the ADNS spec sheet.
// delayMicroseconds(4);
// wait_us(4);
uint8_t x = adns5050_serial_read();
uint8_t y = adns5050_serial_read();

Loading…
Cancel
Save