From 9fbb82063863b6f689907e13fb8c1eb7dd3294ca Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 29 Jun 2022 07:52:10 +0200 Subject: [PATCH] Revert "wip: adns + kaleidoscope, ~" This reverts commit d5e75e2ed759d3af1362d697f4e385c46f8fc79d. --- .../examples/PloopyADNS/PloopyADNS.ino | 70 ++++++-- .../examples/PloopyADNS/adns5050.c | 1 + .../examples/PloopyADNS/adns5050.h | 2 +- .../examples/PloopyADNS/pins_and_ports.h | 164 ++++++++++++++++++ 4 files changed, 225 insertions(+), 12 deletions(-) create mode 100644 plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/pins_and_ports.h diff --git a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/PloopyADNS.ino b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/PloopyADNS.ino index 10ed8969..82e2b8db 100644 --- a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/PloopyADNS.ino +++ b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/PloopyADNS.ino @@ -1,20 +1,71 @@ // -*- mode: c++ -*- -#include "Kaleidoscope.h" -#include "Kaleidoscope-FocusSerial.h" +#include // for uint16_t, uint8_t -#include "kaleidoscope/device/avr/pins_and_ports.h" // IWYU pragma: keep +#include "pins_and_ports.h" // IWYU pragma: keep #include extern "C" { #include "adns5050.h" } -KALEIDOSCOPE_INIT_PLUGINS(Focus); +#define BOOTLOADER_RESET_KEY 0xB007B007 +static uint32_t reset_key __attribute__((section(".noinit"))); + +/* + * This function runs before main(), and jumps to the bootloader after a reset + * initiated by .resetDevice(). + */ +static void _bootloader_jump_after_watchdog_reset() + __attribute__((used, naked, section(".init3"))); +static void _bootloader_jump_after_watchdog_reset() { + if ((MCUSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) { + reset_key = 0; + + ((void (*)(void))(((FLASHEND + 1L) - 4096) >> 1))(); + } +} + +void rebootBootloader() { + reset_key = BOOTLOADER_RESET_KEY; + wdt_enable(WDTO_250MS); + while (true) {} // This infinite loop ensures nothing else + // happens before the watchdog reboots us +} + +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}; + +void mcuSetup() { + wdt_disable(); + + // disable jtag + MCUCR |= (1 << JTD); + MCUCR |= (1 << JTD); + + // disable clock division + CLKPR = (1 << CLKPCE); + CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); +} + +void buttonSetup() { + // Initialize keyscanner + + for (uint8_t i = 0; i < sizeof(unused_pins); i++) { + DDR_OUTPUT(unused_pins[i]); + DISABLE_PULLUP(unused_pins[i]); + } + + for (uint8_t i = 0; i < sizeof(direct_pins); i++) { + DDR_INPUT(direct_pins[i]); + ENABLE_PULLUP(direct_pins[i]); + } +} void setup() { - Kaleidoscope.setup(); + mcuSetup(); Serial.begin(9600); + buttonSetup(); adns5050_init(); adns5050_sync(); @@ -38,7 +89,7 @@ void handleSerial() { uint8_t b = Serial.read(); - if (b == 'r') Kaleidoscope.device().rebootBootloader(); + if (b == 'r') rebootBootloader(); Serial.flush(); } @@ -46,10 +97,7 @@ void handleSerial() { void loop() { handleSerial(); - report_adns5050_t r = adns5050_read_burst(); - if (r.dx != 0 || r.dy != 0) { - Focus.send("dx=", r.dx, "dy=", r.dy, "\n"); - } + //report_adns5050_t r = adns505 - //delay(250); + delay(250); } diff --git a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.c b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.c index 286eb586..11ddbc85 100644 --- a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.c +++ b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.c @@ -19,6 +19,7 @@ #include "adns5050.h" #include "Arduino.h" +#include "pins_and_ports.h" // Registers // clang-format off diff --git a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.h b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.h index a4db8ecb..21d00218 100644 --- a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.h +++ b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/adns5050.h @@ -20,7 +20,7 @@ #pragma once #include -#include "kaleidoscope/device/avr/pins_and_ports.h" +#include "pins_and_ports.h" // CPI values // clang-format off diff --git a/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/pins_and_ports.h b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/pins_and_ports.h new file mode 100644 index 00000000..9c5fb855 --- /dev/null +++ b/plugins/Kaleidoscope-Hardware-Ploopy-Trackball-Mini/examples/PloopyADNS/pins_and_ports.h @@ -0,0 +1,164 @@ +/* This file originated in QMK. https://github.com/qmk/qmk_firmware + * quantum/config_common.h 73e634482ea8f57d1f1a5f1e16bc3ffd74f84b8e + * + * Original copyright statement: + * + * Copyright 2015-2018 Jack Humbert + * + * 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 . + */ + +#pragma once + +#if !defined(__ASSEMBLER__) && !defined(KALEIDOSCOPE_VIRTUAL_BUILD) +#include +#endif +#define PORT_SHIFTER 4 // this may be 4 for all AVR chips + +// If you want to add more to this list, reference the PINx definitions in these header +// files: https://github.com/vancegroup-mirrors/avr-libc/tree/master/avr-libc/include/avr + +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) +#define ADDRESS_BASE 0x00 +#define PINB_ADDRESS 0x3 +#define PINC_ADDRESS 0x6 +#define PIND_ADDRESS 0x9 +#define PINE_ADDRESS 0xC +#define PINF_ADDRESS 0xF +#elif defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) +#define ADDRESS_BASE 0x00 +#define PINB_ADDRESS 0x3 +#define PINC_ADDRESS 0x6 +#define PIND_ADDRESS 0x9 +#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) +#define ADDRESS_BASE 0x00 +#define PINA_ADDRESS 0x0 +#define PINB_ADDRESS 0x3 +#define PINC_ADDRESS 0x6 +#define PIND_ADDRESS 0x9 +#define PINE_ADDRESS 0xC +#define PINF_ADDRESS 0xF +#elif defined(__AVR_ATmega32A__) +#define ADDRESS_BASE 0x10 +#define PIND_ADDRESS 0x0 +#define PINC_ADDRESS 0x3 +#define PINB_ADDRESS 0x6 +#define PINA_ADDRESS 0x9 +#elif !defined(KALEIDOSCOPE_VIRTUAL_BUILD) +#error "Pins are not defined" +#endif + +/* I/O pins */ +#ifndef KALEIDOSCOPE_VIRTUAL_BUILD +#define PINDEF(port, pin) ((PIN##port##_ADDRESS << PORT_SHIFTER) | pin) +#else // ifndef KALEIDOSCOPE_VIRTUAL_BUILD +#define PINDEF(port, pin) 0 +#define PORTA +#define PORTB +#define PORTC +#define PORTD +#define PORTE +#define PORTF +#endif // ifndef KALEIDOSCOPE_VIRTUAL_BUILD + +#ifdef PORTA +#define PIN_A0 PINDEF(A, 0) +#define PIN_A1 PINDEF(A, 1) +#define PIN_A2 PINDEF(A, 2) +#define PIN_A3 PINDEF(A, 3) +#define PIN_A4 PINDEF(A, 4) +#define PIN_A5 PINDEF(A, 5) +#define PIN_A6 PINDEF(A, 6) +#define PIN_A7 PINDEF(A, 7) +#endif +#ifdef PORTB +#define PIN_B0 PINDEF(B, 0) +#define PIN_B1 PINDEF(B, 1) +#define PIN_B2 PINDEF(B, 2) +#define PIN_B3 PINDEF(B, 3) +#define PIN_B4 PINDEF(B, 4) +#define PIN_B5 PINDEF(B, 5) +#define PIN_B6 PINDEF(B, 6) +#define PIN_B7 PINDEF(B, 7) +#endif +#ifdef PORTC +#define PIN_C0 PINDEF(C, 0) +#define PIN_C1 PINDEF(C, 1) +#define PIN_C2 PINDEF(C, 2) +#define PIN_C3 PINDEF(C, 3) +#define PIN_C4 PINDEF(C, 4) +#define PIN_C5 PINDEF(C, 5) +#define PIN_C6 PINDEF(C, 6) +#define PIN_C7 PINDEF(C, 7) +#endif +#ifdef PORTD +#define PIN_D0 PINDEF(D, 0) +#define PIN_D1 PINDEF(D, 1) +#define PIN_D2 PINDEF(D, 2) +#define PIN_D3 PINDEF(D, 3) +#define PIN_D4 PINDEF(D, 4) +#define PIN_D5 PINDEF(D, 5) +#define PIN_D6 PINDEF(D, 6) +#define PIN_D7 PINDEF(D, 7) +#endif +#ifdef PORTE +#define PIN_E0 PINDEF(E, 0) +#define PIN_E1 PINDEF(E, 1) +#define PIN_E2 PINDEF(E, 2) +#define PIN_E3 PINDEF(E, 3) +#define PIN_E4 PINDEF(E, 4) +#define PIN_E5 PINDEF(E, 5) +#define PIN_E6 PINDEF(E, 6) +#define PIN_E7 PINDEF(E, 7) +#endif +#ifdef PORTF +#define PIN_F0 PINDEF(F, 0) +#define PIN_F1 PINDEF(F, 1) +#define PIN_F2 PINDEF(F, 2) +#define PIN_F3 PINDEF(F, 3) +#define PIN_F4 PINDEF(F, 4) +#define PIN_F5 PINDEF(F, 5) +#define PIN_F6 PINDEF(F, 6) +#define PIN_F7 PINDEF(F, 7) +#endif + +/* converting pins to ports */ +enum { PIN_OFFSET, + DDR_OFFSET, + PORT_OFFSET }; + +#define PIN_ADDRESS_MASK 0xF + +#define PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + (p >> PORT_SHIFTER) + offset) +#define PIN_REG_FOR_PIN(pin) PIN_ADDRESS(pin, PIN_OFFSET) +#define DDR_REG_FOR_PIN(pin) PIN_ADDRESS(pin, DDR_OFFSET) +#define PORT_REG_FOR_PIN(pin) PIN_ADDRESS(pin, PORT_OFFSET) +#define PIN_NUM_FOR_PIN(pin) (pin & PIN_ADDRESS_MASK) +#define PIN_MASK_FOR_PIN(pin) _BV(PIN_NUM_FOR_PIN(pin)) + +#define DDR_INPUT(pin) (DDR_REG_FOR_PIN(pin) &= ~(PIN_MASK_FOR_PIN(pin))) +#define DDR_OUTPUT(pin) (DDR_REG_FOR_PIN(pin) |= (PIN_MASK_FOR_PIN(pin))) + + +#define ENABLE_PULLUP(pin) (PORT_REG_FOR_PIN(pin) |= (PIN_MASK_FOR_PIN(pin))) +#define DISABLE_PULLUP(pin) (PORT_REG_FOR_PIN(pin) &= ~(PIN_MASK_FOR_PIN(pin))) + + +#define OUTPUT_HIGH(pin) (PORT_REG_FOR_PIN(pin) |= (PIN_MASK_FOR_PIN(pin))) +#define OUTPUT_LOW(pin) (PORT_REG_FOR_PIN(pin) &= ~(PIN_MASK_FOR_PIN(pin))) +#define OUTPUT_TOGGLE(pin) (PORT_REG_FOR_PIN(pin) ^= (PIN_MASK_FOR_PIN(pin))) + +#define READ_PIN(pin) (!!(PIN_REG_FOR_PIN(pin) & PIN_MASK_FOR_PIN(pin))) + +#define NO_PIN (uint8_t)(~0)