1
0
Fork 0

Convert split_common to use generic GPIO api

This commit is contained in:
James Churchill 2018-12-29 16:53:21 +11:00 committed by Drashna Jaelre
parent 54b572159f
commit 38e01a7480
2 changed files with 23 additions and 54 deletions

View file

@ -20,17 +20,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <avr/io.h>
#include "wait.h" #include "wait.h"
#include "print.h"
#include "debug.h"
#include "util.h" #include "util.h"
#include "matrix.h" #include "matrix.h"
#include "split_util.h" #include "split_util.h"
#include "pro_micro.h"
#include "config.h" #include "config.h"
#include "timer.h" #include "timer.h"
#include "split_flags.h" #include "split_flags.h"
#include "quantum.h"
#ifdef BACKLIGHT_ENABLE #ifdef BACKLIGHT_ENABLE
# include "backlight.h" # include "backlight.h"
@ -91,8 +88,8 @@ static matrix_row_t matrix_debouncing[MATRIX_ROWS];
static uint8_t error_count = 0; static uint8_t error_count = 0;
static uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
static uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
/* matrix state(1:on, 0:off) */ /* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix[MATRIX_ROWS];
@ -440,9 +437,7 @@ uint8_t matrix_key_count(void)
static void init_cols(void) static void init_cols(void)
{ {
for(uint8_t x = 0; x < MATRIX_COLS; x++) { for(uint8_t x = 0; x < MATRIX_COLS; x++) {
uint8_t pin = col_pins[x]; setPinInputHigh(col_pins[x]);
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
} }
} }
@ -460,13 +455,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
// For each col... // For each col...
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
// Select the col pin to read (active low)
uint8_t pin = col_pins[col_index];
uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
// Populate the matrix row with the state of the col pin // Populate the matrix row with the state of the col pin
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
} }
// Unselect row // Unselect row
@ -477,24 +467,19 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
static void select_row(uint8_t row) static void select_row(uint8_t row)
{ {
uint8_t pin = row_pins[row]; writePinLow(row_pins[row]);
_SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT setPinOutput(row_pins[row]);
_SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
} }
static void unselect_row(uint8_t row) static void unselect_row(uint8_t row)
{ {
uint8_t pin = row_pins[row]; setPinInputHigh(row_pins[row]);
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
} }
static void unselect_rows(void) static void unselect_rows(void)
{ {
for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
uint8_t pin = row_pins[x]; setPinInputHigh(row_pins[x]);
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
} }
} }
@ -503,9 +488,7 @@ static void unselect_rows(void)
static void init_rows(void) static void init_rows(void)
{ {
for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
uint8_t pin = row_pins[x]; setPinInputHigh(row_pins[x]);
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
} }
} }
@ -525,16 +508,16 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
matrix_row_t last_row_value = current_matrix[row_index]; matrix_row_t last_row_value = current_matrix[row_index];
// Check row pin state // Check row pin state
if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) if (readPin(row_pins[row_index]))
{
// Pin LO, set col bit
current_matrix[row_index] |= (ROW_SHIFTER << current_col);
}
else
{ {
// Pin HI, clear col bit // Pin HI, clear col bit
current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
} }
else
{
// Pin LO, set col bit
current_matrix[row_index] |= (ROW_SHIFTER << current_col);
}
// Determine if the matrix changed state // Determine if the matrix changed state
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
@ -551,24 +534,19 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
static void select_col(uint8_t col) static void select_col(uint8_t col)
{ {
uint8_t pin = col_pins[col]; writePinLow(col_pins[col]);
_SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT setPinOutput(col_pins[col]);
_SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
} }
static void unselect_col(uint8_t col) static void unselect_col(uint8_t col)
{ {
uint8_t pin = col_pins[col]; setPinInputHigh(col_pins[col]);
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
} }
static void unselect_cols(void) static void unselect_cols(void)
{ {
for(uint8_t x = 0; x < MATRIX_COLS; x++) { for(uint8_t x = 0; x < MATRIX_COLS; x++) {
uint8_t pin = col_pins[x]; setPinInputHigh(col_pins[x]);
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
} }
} }

View file

@ -1,24 +1,15 @@
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include "split_util.h" #include "split_util.h"
#include "matrix.h" #include "matrix.h"
#include "keyboard.h" #include "keyboard.h"
#include "config.h" #include "config.h"
#include "timer.h" #include "timer.h"
#include "split_flags.h" #include "split_flags.h"
#include "quantum.h"
#ifdef BACKLIGHT_ENABLE #ifdef BACKLIGHT_ENABLE
# include "backlight.h" # include "backlight.h"
#endif #endif
#ifdef SPLIT_HAND_PIN
# include "pincontrol.h"
#endif
#if defined(USE_I2C) || defined(EH) #if defined(USE_I2C) || defined(EH)
# include "i2c.h" # include "i2c.h"
#endif #endif
@ -30,8 +21,8 @@ volatile uint8_t setTries = 0;
static void setup_handedness(void) { static void setup_handedness(void) {
#ifdef SPLIT_HAND_PIN #ifdef SPLIT_HAND_PIN
// Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
pinMode(SPLIT_HAND_PIN, PinDirectionInput); setPinInput(SPLIT_HAND_PIN);
isLeftHand = digitalRead(SPLIT_HAND_PIN); isLeftHand = readPin(SPLIT_HAND_PIN);
#else #else
#ifdef EE_HANDS #ifdef EE_HANDS
isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);