Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GPIO functions for AVR atomic #9575

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 94 additions & 6 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# include <avr/pgmspace.h>
# include <avr/io.h>
# include <avr/interrupt.h>
# include <util/atomic.h>
#endif
#if defined(PROTOCOL_CHIBIOS)
# include "hal.h"
Expand Down Expand Up @@ -186,18 +187,105 @@ extern layer_state_t layer_state;
#if defined(__AVR__)
typedef uint8_t pin_t;

# define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
# define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
# define GPIO_FORCE_PRECOMPUTE(var) asm volatile("" : : "g"(var));
# define GPIO_BARRIER() asm volatile("")

# define setPinInput(pin) \
(__extension__({ \
volatile uint8_t *ddr = &DDRx_ADDRESS(pin); \
volatile uint8_t *port = &PORTx_ADDRESS(pin); \
uint8_t inv_mask = (uint8_t)~_BV((pin) & 0xF); \
GPIO_FORCE_PRECOMPUTE(inv_mask); \
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
*ddr &= inv_mask; \
*port &= inv_mask; \
} \
GPIO_BARRIER(); \
}))

# define setPinInputHigh(pin) \
(__extension__({ \
volatile uint8_t *ddr = &DDRx_ADDRESS(pin); \
volatile uint8_t *port = &PORTx_ADDRESS(pin); \
uint8_t mask = _BV((pin) & 0xF); \
uint8_t inv_mask = (uint8_t)~mask; \
GPIO_FORCE_PRECOMPUTE(mask); \
GPIO_FORCE_PRECOMPUTE(inv_mask); \
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
*ddr &= inv_mask; \
*port |= mask; \
} \
GPIO_BARRIER(); \
}))

# define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
# 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 setPinOutput(pin) \
(__extension__({ \
if (__builtin_constant_p(pin)) { \
if (((pin) & 0xF) <= 7) { \
asm volatile("sbi %0,%1" : : "I"(_SFR_IO_ADDR(DDRx_ADDRESS(pin))), "I"((pin) & 0xF)); \
} \
} else { \
volatile uint8_t *ddr = &DDRx_ADDRESS(pin); \
uint8_t mask = _BV((pin) & 0xF); \
GPIO_FORCE_PRECOMPUTE(mask); \
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
*ddr |= mask; \
} \
GPIO_BARRIER(); \
} \
}))

# define writePinHigh(pin) \
(__extension__ ({ \
if (__builtin_constant_p(pin)) { \
if (((pin) & 0xF) <= 7) { \
asm volatile("sbi %0,%1" : : "I"(_SFR_IO_ADDR(PORTx_ADDRESS(pin))), "I"((pin) & 0xF)); \
} \
} else { \
volatile uint8_t *port = &PORTx_ADDRESS(pin); \
uint8_t mask = _BV((pin) & 0xF); \
GPIO_FORCE_PRECOMPUTE(mask); \
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
*port |= mask; \
} \
GPIO_BARRIER(); \
} \
}))

# define writePinLow(pin) \
(__extension__ ({ \
if (__builtin_constant_p(pin)) { \
if (((pin) & 0xF) <= 7) { \
asm volatile("cbi %0,%1" : : "I"(_SFR_IO_ADDR(PORTx_ADDRESS(pin))), "I"((pin) & 0xF)); \
} \
} else { \
volatile uint8_t *port = &PORTx_ADDRESS(pin); \
uint8_t inv_mask = (uint8_t)~_BV((pin) & 0xF); \
GPIO_FORCE_PRECOMPUTE(inv_mask); \
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
*port &= inv_mask; \
} \
GPIO_BARRIER(); \
} \
}))

# define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))

# define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))

# define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF))
# define togglePin(pin) \
(__extension__({ \
volatile uint8_t *port = &PORTx_ADDRESS(pin); \
uint8_t mask = _BV((pin) & 0xF); \
GPIO_FORCE_PRECOMPUTE(mask); \
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
*port ^= mask; \
} \
GPIO_BARRIER(); \
}))


#elif defined(PROTOCOL_CHIBIOS)
typedef ioline_t pin_t;
Expand Down