From d88bdc6a1bb1f1e425726345279046f76b09b0cb Mon Sep 17 00:00:00 2001 From: a-chol Date: Sun, 29 Dec 2019 18:04:50 +0100 Subject: [PATCH] Fix port addressing for joystick analog read --- drivers/avr/analog.c | 33 +++++----------------- quantum/process_keycode/process_joystick.c | 9 +++--- tmk_core/protocol/vusb/vusb.c | 1 - 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/drivers/avr/analog.c b/drivers/avr/analog.c index e79e49b04a..a20f0ba68a 100644 --- a/drivers/avr/analog.c +++ b/drivers/avr/analog.c @@ -46,7 +46,6 @@ int16_t analogRead(uint8_t pin) { #endif } -<<<<<<< HEAD int16_t analogReadPin(pin_t pin) { return adc_read(pinToMux(pin)); } uint8_t pinToMux(pin_t pin) { @@ -95,36 +94,13 @@ uint8_t pinToMux(pin_t pin) { case C5: return _BV(MUX2) | _BV(MUX0); // ADC5 // ADC7:6 not present in DIP package and not shared by GPIO pins default: return _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V -======= -// Mux input -int16_t adc_read(uint8_t mux) -{ -#if defined(__AVR_AT90USB162__) - return 0; -#else - uint16_t res = 0; - - ADCSRA = (1<>>>>>> 5939a4430... Add save and restore of each pin used in reading joystick (AVR). #endif // clang-format on } } int16_t adc_read(uint8_t mux) { - uint8_t low; + uint16_t low; // Enable ADC and configure prescaler ADCSRA = _BV(ADEN) | ADC_PRESCALER; @@ -152,5 +128,10 @@ int16_t adc_read(uint8_t mux) { // Must read LSB first low = ADCL; // Must read MSB only once! - return (ADCH << 8) | low; + low |= (ADCH << 8); + + //turn off the ADC + ADCSRA &= ~(1< #include +#include #ifdef __AVR__ # include @@ -52,8 +53,8 @@ bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record){ uint8_t savePinState(uint8_t pin){ #ifdef __AVR__ uint8_t pinNumber = pin & 0xF; - return ((PIN_ADDRESS(pin, 2) >> pinNumber) & 0x1) << 1 - | ((PIN_ADDRESS(pin, 1) >> pinNumber) & 0x1) ; + return ((PORTx_ADDRESS(pin) >> pinNumber) & 0x1) << 1 + | ((DDRx_ADDRESS(pin) >> pinNumber) & 0x1) ; #else return 0; #endif @@ -62,8 +63,8 @@ uint8_t savePinState(uint8_t pin){ void restorePinState(uint8_t pin, uint8_t restoreState){ #ifdef __AVR__ uint8_t pinNumber = pin & 0xF; - PIN_ADDRESS(pin, 2) = (PIN_ADDRESS(pin, 2) & ~_BV(pinNumber)) | (((restoreState >> 1) & 0x1) << pinNumber); - PIN_ADDRESS(pin, 1) = (PIN_ADDRESS(pin, 1) & ~_BV(pinNumber)) | ((restoreState & 0x1) << pinNumber); + PORTx_ADDRESS(pin) = (PORTx_ADDRESS(pin) & ~_BV(pinNumber)) | (((restoreState >> 1) & 0x1) << pinNumber); + DDRx_ADDRESS(pin) = (DDRx_ADDRESS(pin) & ~_BV(pinNumber)) | ((restoreState & 0x1) << pinNumber); #else return; #endif diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index 571ca33890..7c9f1bca6b 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -81,7 +81,6 @@ static void send_keyboard(report_keyboard_t *report); static void send_mouse(report_mouse_t *report); static void send_system(uint16_t data); static void send_consumer(uint16_t data); -static void send_joystick(void); static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};