Fix port addressing for joystick analog read

This commit is contained in:
a-chol 2019-12-29 18:04:50 +01:00
parent b030c45705
commit d88bdc6a1b
3 changed files with 12 additions and 31 deletions

View File

@ -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<<ADEN) | ADC_PRESCALER; // enable ADC
#ifndef __AVR_ATmega32A__
ADCSRB = (1<<ADHSM) | (mux & 0x20); // high speed mode
#endif
ADMUX = aref | (mux & 0x1F); // configure mux input
ADCSRA |= (1<<ADSC); // start the conversion
while (ADCSRA & (1<<ADSC)) ; // wait for result
res = ADCL; // must read LSB first
res |= (ADCH << 8) | res;
ADCSRA &= ~(1<<ADEN);//turn off the ADC
return res;
>>>>>>> 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<<ADEN);
return low;
}

View File

@ -2,6 +2,7 @@
#include <quantum/joystick.h>
#include <quantum/quantum_keycodes.h>
#include <quantum/config_common.h>
#ifdef __AVR__
# include <drivers/avr/analog.h>
@ -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

View File

@ -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};