From 1c5b0cbbeb049b1ce3fb2da6a81fbf83dd9a3ea7 Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Thu, 22 Aug 2019 09:10:47 +0900 Subject: [PATCH] AVR GPIO macro defines more readable (#5937) * A little easier to read the definition of the GPIO control macro for AVR. No change in build result. * Changed to not use GNU statement expression extension. No change in build result. * Modified split_common/serial.c to use qmk_firmware standard GPIO control macro. No change in build result. * fix PE6 -> E6 * remove some space * add some comment to config_common.h * Changed split_common/serial.c to use a newer version of qmk_firmware standard GPIO control macro. --- quantum/config_common.h | 10 ++++++++++ quantum/quantum.h | 15 +++++++-------- quantum/split_common/serial.c | 31 +++++++++++++------------------ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/quantum/config_common.h b/quantum/config_common.h index bc4d9ec1a..ae72701da 100644 --- a/quantum/config_common.h +++ b/quantum/config_common.h @@ -132,6 +132,16 @@ #define F7 PINDEF(F, 7) #endif + #ifndef __ASSEMBLER__ + #define _PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + (p >> PORT_SHIFTER) + offset) + // Port X Input Pins Address + #define PINx_ADDRESS(p) _PIN_ADDRESS(p, 0) + // Port X Data Direction Register, 0:input 1:output + #define DDRx_ADDRESS(p) _PIN_ADDRESS(p, 1) + // Port X Data Register + #define PORTx_ADDRESS(p) _PIN_ADDRESS(p, 2) + #endif + #elif defined(PROTOCOL_CHIBIOS) // Defines mapping for Proton C replacement #ifdef CONVERT_TO_PROTON_C diff --git a/quantum/quantum.h b/quantum/quantum.h index 221462567..2cb26d4f4 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -149,18 +149,17 @@ extern layer_state_t default_layer_state; #if defined(__AVR__) typedef uint8_t pin_t; - #define PIN_ADDRESS(p, offset) (_SFR_IO8(ADDRESS_BASE + ((p) >> PORT_SHIFTER) + (offset))) - #define setPinInput(pin) (PIN_ADDRESS(pin, 1) &= ~_BV((pin) & 0xF)) - #define setPinInputHigh(pin) (PIN_ADDRESS(pin, 1) &= ~_BV((pin) & 0xF), \ - PIN_ADDRESS(pin, 2) |= _BV((pin) & 0xF)) + #define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin) & 0xF)) + #define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin) & 0xF), \ + PORTx_ADDRESS(pin) |= _BV((pin) & 0xF)) #define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low") - #define setPinOutput(pin) (PIN_ADDRESS(pin, 1) |= _BV((pin) & 0xF)) + #define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin) & 0xF)) - #define writePinHigh(pin) (PIN_ADDRESS(pin, 2) |= _BV((pin) & 0xF)) - #define writePinLow(pin) (PIN_ADDRESS(pin, 2) &= ~_BV((pin) & 0xF)) + #define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin) & 0xF)) + #define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin) & 0xF)) #define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin)) - #define readPin(pin) ((bool)(PIN_ADDRESS(pin, 0) & _BV((pin) & 0xF))) + #define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin) & 0xF))) #elif defined(PROTOCOL_CHIBIOS) typedef ioline_t pin_t; diff --git a/quantum/split_common/serial.c b/quantum/split_common/serial.c index 1315377a3..322ab8030 100644 --- a/quantum/split_common/serial.c +++ b/quantum/split_common/serial.c @@ -29,36 +29,32 @@ #endif #endif + #define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin) & 0xF), \ + PORTx_ADDRESS(pin) |= _BV((pin) & 0xF)) + #define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin) & 0xF)) + #define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin) & 0xF)) + #define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin) & 0xF)) + #define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin) & 0xF))) + #if SOFT_SERIAL_PIN >= D0 && SOFT_SERIAL_PIN <= D3 - #define SERIAL_PIN_DDR DDRD - #define SERIAL_PIN_PORT PORTD - #define SERIAL_PIN_INPUT PIND #if SOFT_SERIAL_PIN == D0 - #define SERIAL_PIN_MASK _BV(PD0) #define EIMSK_BIT _BV(INT0) #define EICRx_BIT (~(_BV(ISC00) | _BV(ISC01))) #define SERIAL_PIN_INTERRUPT INT0_vect #elif SOFT_SERIAL_PIN == D1 - #define SERIAL_PIN_MASK _BV(PD1) #define EIMSK_BIT _BV(INT1) #define EICRx_BIT (~(_BV(ISC10) | _BV(ISC11))) #define SERIAL_PIN_INTERRUPT INT1_vect #elif SOFT_SERIAL_PIN == D2 - #define SERIAL_PIN_MASK _BV(PD2) #define EIMSK_BIT _BV(INT2) #define EICRx_BIT (~(_BV(ISC20) | _BV(ISC21))) #define SERIAL_PIN_INTERRUPT INT2_vect #elif SOFT_SERIAL_PIN == D3 - #define SERIAL_PIN_MASK _BV(PD3) #define EIMSK_BIT _BV(INT3) #define EICRx_BIT (~(_BV(ISC30) | _BV(ISC31))) #define SERIAL_PIN_INTERRUPT INT3_vect #endif #elif SOFT_SERIAL_PIN == E6 - #define SERIAL_PIN_DDR DDRE - #define SERIAL_PIN_PORT PORTE - #define SERIAL_PIN_INPUT PINE - #define SERIAL_PIN_MASK _BV(PE6) #define EIMSK_BIT _BV(INT6) #define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61))) #define SERIAL_PIN_INTERRUPT INT6_vect @@ -200,33 +196,32 @@ void serial_delay_half2(void) { inline static void serial_output(void) ALWAYS_INLINE; inline static void serial_output(void) { - SERIAL_PIN_DDR |= SERIAL_PIN_MASK; + setPinOutput(SOFT_SERIAL_PIN); } // make the serial pin an input with pull-up resistor inline static void serial_input_with_pullup(void) ALWAYS_INLINE; inline static void serial_input_with_pullup(void) { - SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; - SERIAL_PIN_PORT |= SERIAL_PIN_MASK; + setPinInputHigh(SOFT_SERIAL_PIN); } inline static uint8_t serial_read_pin(void) ALWAYS_INLINE; inline static uint8_t serial_read_pin(void) { - return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); + return !! readPin(SOFT_SERIAL_PIN); } inline static void serial_low(void) ALWAYS_INLINE; inline static void serial_low(void) { - SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; + writePinLow(SOFT_SERIAL_PIN); } inline static void serial_high(void) ALWAYS_INLINE; inline static void serial_high(void) { - SERIAL_PIN_PORT |= SERIAL_PIN_MASK; + writePinHigh(SOFT_SERIAL_PIN); } void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size) @@ -245,7 +240,7 @@ void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size) // Enable INT0-INT3,INT6 EIMSK |= EIMSK_BIT; -#if SERIAL_PIN_MASK == _BV(PE6) +#if SOFT_SERIAL_PIN == E6 // Trigger on falling edge of INT6 EICRB &= EICRx_BIT; #else