Compare commits

...

7 Commits

Author SHA1 Message Date
Jack Humbert 8c63a02483 fix 32u2 7 years ago
Jack Humbert 486a483222 fix 32a 7 years ago
Jack Humbert a4601e0f77 simplified version with chip defines 7 years ago
Jack Humbert b653791d21 move shortcuts around 7 years ago
Jack Humbert 7dbe94d4ac fix macros 7 years ago
Jack Humbert f6ca092f0b fix up offsets a bit 7 years ago
Jack Humbert 133af22a70 add shortcuts, better port defines 7 years ago

@ -22,58 +22,177 @@
#define ROW2COL 1 #define ROW2COL 1
#define CUSTOM_MATRIX 2 /* Disables built-in matrix scanning code */ #define CUSTOM_MATRIX 2 /* Disables built-in matrix scanning code */
/* I/O pins */ #if defined(__AVR__)
#ifndef F0 #include <avr/io.h>
#define B0 0x30
#define B1 0x31 #define PORT_SHIFTER 4
#define B2 0x32
#define B3 0x33 #if defined(__AVR_ATmega32U4__)
#define B4 0x34 #define pin_t uint8_t
#define B5 0x35 #define ADDRESS_BASE 0x00
#define B6 0x36 #define PINB_ADDRESS 0x3
#define B7 0x37 #define PINC_ADDRESS 0x6
#define C0 0x60 #define PIND_ADDRESS 0x9
#define C1 0x61 #define PINE_ADDRESS 0xC
#define C2 0x62 #define PINF_ADDRESS 0xF
#define C3 0x63 #elif defined(__AVR_ATmega32U2__)
#define C4 0x64 #define pin_t uint8_t
#define C5 0x65 #define ADDRESS_BASE 0x00
#define C6 0x66 #define PINB_ADDRESS 0x3
#define C7 0x67 #define PINC_ADDRESS 0x6
#define D0 0x90 #define PIND_ADDRESS 0x9
#define D1 0x91 #elif defined(__AVR_AT90USB1286__)
#define D2 0x92 #define pin_t uint8_t
#define D3 0x93 #define ADDRESS_BASE 0x00
#define D4 0x94 #define PINA_ADDRESS 0x0
#define D5 0x95 #define PINB_ADDRESS 0x3
#define D6 0x96 #define PINC_ADDRESS 0x6
#define D7 0x97 #define PIND_ADDRESS 0x9
#define E0 0xC0 #define PINE_ADDRESS 0xC
#define E1 0xC1 #define PINF_ADDRESS 0xF
#define E2 0xC2 #elif defined(__AVR_ATmega32a__)
#define E3 0xC3 #define pin_t uint8_t
#define E4 0xC4 #define ADDRESS_BASE 0x10
#define E5 0xC5 #define PIND_ADDRESS 0x0
#define E6 0xC6 #define PINC_ADDRESS 0x3
#define E7 0xC7 #define PINB_ADDRESS 0x6
#define F0 0xF0 #define PINA_ADDRESS 0x9
#define F1 0xF1 #else
#define F2 0xF2 #define pin_t uint16_t
#define F3 0xF3 #define ADDRESS_BASE 0x0
#define F4 0xF4 #endif
#define F5 0xF5
#define F6 0xF6 /* I/O pins */
#define F7 0xF7 // #define PINDEF(port, pin) (uint8_t)((((uint16_t)&PIN##port) << 4) + PIN##port##pin)
#define A0 0x00 //#define PINDEF(port, pin) ((pin_t)(((((uint16_t)&PIN##port) - __SFR_OFFSET)<< 4) + PIN##port##pin))
#define A1 0x01 #define PINDEF(port, pin) ((PIN##port##_ADDRESS << PORT_SHIFTER) | pin)
#define A2 0x02
#define A3 0x03 #ifdef PORTA
#define A4 0x04 #define A0 PINDEF(A, 0)
#define A5 0x05 #define A1 PINDEF(A, 1)
#define A6 0x06 #define A2 PINDEF(A, 1)
#define A7 0x07 #define A3 PINDEF(A, 3)
#define A4 PINDEF(A, 4)
#define A5 PINDEF(A, 5)
#define A6 PINDEF(A, 6)
#define A7 PINDEF(A, 7)
#endif
#ifdef PORTB
#define B0 PINDEF(B, 0)
#define B1 PINDEF(B, 1)
#define B2 PINDEF(B, 2)
#define B3 PINDEF(B, 3)
#define B4 PINDEF(B, 4)
#define B5 PINDEF(B, 5)
#define B6 PINDEF(B, 6)
#define B7 PINDEF(B, 7)
#endif
#ifdef PORTC
#define C0 PINDEF(C, 0)
#define C1 PINDEF(C, 1)
#define C2 PINDEF(C, 2)
#define C3 PINDEF(C, 3)
#define C4 PINDEF(C, 4)
#define C5 PINDEF(C, 5)
#define C6 PINDEF(C, 6)
#define C7 PINDEF(C, 7)
#endif
#ifdef PORTD
#define D0 PINDEF(D, 0)
#define D1 PINDEF(D, 1)
#define D2 PINDEF(D, 2)
#define D3 PINDEF(D, 3)
#define D4 PINDEF(D, 4)
#define D5 PINDEF(D, 5)
#define D6 PINDEF(D, 6)
#define D7 PINDEF(D, 7)
#endif
#ifdef PORTE
#define E0 PINDEF(E, 0)
#define E1 PINDEF(E, 1)
#define E2 PINDEF(E, 2)
#define E3 PINDEF(E, 3)
#define E4 PINDEF(E, 4)
#define E5 PINDEF(E, 5)
#define E6 PINDEF(E, 6)
#define E7 PINDEF(E, 7)
#endif
#ifdef PORTF
#define F0 PINDEF(F, 0)
#define F1 PINDEF(F, 1)
#define F2 PINDEF(F, 2)
#define F3 PINDEF(F, 3)
#define F4 PINDEF(F, 4)
#define F5 PINDEF(F, 5)
#define F6 PINDEF(F, 6)
#define F7 PINDEF(F, 7)
#endif
#endif #endif
#if defined(PROTOCOL_CHIBIOS)
#include "hal.h"
#define pin_t uint64_t
#ifdef GPIOA
#define A0 (GPIOA + 0)
#define A1 (GPIOA + 1)
#define A2 (GPIOA + 2)
#define A3 (GPIOA + 3)
#define A4 (GPIOA + 4)
#define A5 (GPIOA + 5)
#define A6 (GPIOA + 6)
#define A7 (GPIOA + 7)
#define A8 (GPIOA + 8)
#define A9 (GPIOA + 9)
#define A10 (GPIOA + 10)
#define A11 (GPIOA + 11)
#define A12 (GPIOA + 12)
#define A13 (GPIOA + 13)
#define A14 (GPIOA + 14)
#define A15 (GPIOA + 15)
#endif
#ifdef GPIOB
#define B0 (GPIOB + 0)
#define B1 (GPIOB + 1)
#define B2 (GPIOB + 2)
#define B3 (GPIOB + 3)
#define B4 (GPIOB + 4)
#define B5 (GPIOB + 5)
#define B6 (GPIOB + 6)
#define B7 (GPIOB + 7)
#define B8 (GPIOB + 8)
#define B9 (GPIOB + 9)
#define B10 (GPIOB + 10)
#define B11 (GPIOB + 11)
#define B12 (GPIOB + 12)
#define B13 (GPIOB + 13)
#define B14 (GPIOB + 14)
#define B15 (GPIOB + 15)
#endif
#ifdef GPIOC
#define C0 (GPIOC + 0)
#define C1 (GPIOC + 1)
#define C2 (GPIOC + 2)
#define C3 (GPIOC + 3)
#define C4 (GPIOC + 4)
#define C5 (GPIOC + 5)
#define C6 (GPIOC + 6)
#define C7 (GPIOC + 7)
#define C8 (GPIOC + 8)
#define C9 (GPIOC + 9)
#define C10 (GPIOC + 10)
#define C11 (GPIOC + 11)
#define C12 (GPIOC + 12)
#define C13 (GPIOC + 13)
#define C14 (GPIOC + 14)
#define C15 (GPIOC + 15)
#endif
#endif
/* USART configuration */ /* USART configuration */
#ifdef BLUETOOTH_ENABLE #ifdef BLUETOOTH_ENABLE
# ifdef __AVR_ATmega32U4__ # ifdef __AVR_ATmega32U4__

@ -25,7 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "util.h" #include "util.h"
#include "matrix.h" #include "matrix.h"
#include "timer.h" #include "timer.h"
#include "quantum.h"
/* Set 0 if debouncing isn't needed */ /* Set 0 if debouncing isn't needed */
@ -60,8 +60,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#endif #endif
#if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
#endif #endif
/* matrix state(1:on, 0:off) */ /* matrix state(1:on, 0:off) */
@ -220,7 +220,10 @@ uint8_t matrix_scan(void)
} }
# endif # endif
dprintf("%x\n", B0);
matrix_scan_quantum(); matrix_scan_quantum();
return 1; return 1;
} }
@ -271,15 +274,14 @@ uint8_t matrix_key_count(void)
} }
#if (DIODE_DIRECTION == COL2ROW) #if (DIODE_DIRECTION == COL2ROW)
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]; uint8_t pin = col_pins[x];
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN DDR_INPUT(pin); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI PORT_HIGH(pin); // HI
} }
} }
@ -300,7 +302,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
// Select the col pin to read (active low) // Select the col pin to read (active low)
uint8_t pin = col_pins[col_index]; uint8_t pin = col_pins[col_index];
uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); uint8_t pin_state = PIN_VALUE(pin);
// 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] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
@ -315,23 +317,23 @@ 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]; uint8_t pin = row_pins[row];
_SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT DDR_OUTPUT(pin); // OUT
_SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW PORT_LOW(pin); // LOW
} }
static void unselect_row(uint8_t row) static void unselect_row(uint8_t row)
{ {
uint8_t pin = row_pins[row]; uint8_t pin = row_pins[row];
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN DDR_INPUT(pin); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI PORT_HIGH(pin); // HI
} }
static void unselect_rows(void) static void unselect_rows(void)
{ {
for(uint8_t x = 0; x < MATRIX_ROWS; x++) { for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
uint8_t pin = row_pins[x]; uint8_t pin = row_pins[x];
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN DDR_INPUT(pin); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI PORT_HIGH(pin); // HI
} }
} }
@ -341,8 +343,8 @@ static void init_rows(void)
{ {
for(uint8_t x = 0; x < MATRIX_ROWS; x++) { for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
uint8_t pin = row_pins[x]; uint8_t pin = row_pins[x];
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN DDR_INPUT(pin); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI PORT_HIGH(pin); // HI
} }
} }
@ -362,7 +364,7 @@ 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 (PIN_VALUE(row_pins[row_index]) == 0)
{ {
// Pin LO, set col bit // Pin LO, set col bit
current_matrix[row_index] |= (ROW_SHIFTER << current_col); current_matrix[row_index] |= (ROW_SHIFTER << current_col);
@ -389,23 +391,23 @@ 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]; uint8_t pin = col_pins[col];
_SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT DDR_OUTPUT(pin); // OUT
_SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW PORT_LOW(pin); // LOW
} }
static void unselect_col(uint8_t col) static void unselect_col(uint8_t col)
{ {
uint8_t pin = col_pins[col]; uint8_t pin = col_pins[col];
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN DDR_INPUT(pin); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI PORT_HIGH(pin); // 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]; uint8_t pin = col_pins[x];
_SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN DDR_INPUT(pin); // IN
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI PORT_HIGH(pin); // HI
} }
} }

@ -1,4 +1,5 @@
/* Copyright 2016 Wez Furlong /* Copyright 2016 Wez Furlong
* 2017 Jack Humbert
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -14,39 +15,72 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
// Some helpers for controlling gpio pins // Some helpers for controlling gpio pins
#include <avr/io.h>
#if defined(PROTOCOL_CHIBIOS)
enum { #include "hal.h"
PinDirectionInput = 0,
PinDirectionOutput = 1, #define PIN_VALUE(p) palReadPad(p & 0xFFFFFFF0, p & 0xF)
PinLevelHigh = 1,
PinLevelLow = 0, #define DDR_OUTPUT(p) palSetPadMode(p & 0xFFFFFFF0, p & 0xF, PAL_MODE_OUTPUT_PUSHPULL)
}; #define DDR_INPUT(p) palSetPadMode(p & 0xFFFFFFF0, p & 0xF, PAL_MODE_INPUT_PULLDOWN)
// ex: pinMode(B0, PinDirectionOutput); #define PORT_HIGH(p) palSetPad(p & 0xFFFFFFF0, p & 0xF)
static inline void pinMode(uint8_t pin, int mode) { #define PORT_LOW(p) palClearPad(p & 0xFFFFFFF0, p & 0xF)
uint8_t bv = _BV(pin & 0xf);
if (mode == PinDirectionOutput) { #endif
_SFR_IO8((pin >> 4) + 1) |= bv;
} else { #if defined(__AVR__)
_SFR_IO8((pin >> 4) + 1) &= ~bv; #include <avr/io.h>
_SFR_IO8((pin >> 4) + 2) &= ~bv;
#define PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + (p >> PORT_SHIFTER) + offset)
#define PIN(p) PIN_ADDRESS(p, 0)
#define PIN_VALUE(p) (PIN(p) & _BV(p & 0xF))
#define DDR(p) PIN_ADDRESS(p, 1)
#define DDR_OUTPUT(p) (DDR(p) |= _BV(p & 0xF))
#define DDR_INPUT(p) (DDR(p) &= ~_BV(p & 0xF))
#define PORT(p) PIN_ADDRESS(p, 2)
#define PORT_HIGH(p) (PORT(p) |= _BV(p & 0xF))
#define PORT_LOW(p) (PORT(p) &= ~_BV(p & 0xF))
// Arduino shortcuts
enum {
PinDirectionInput = 0,
PinDirectionOutput = 1,
PinLevelHigh = 1,
PinLevelLow = 0,
};
// ex: pinMode(B0, PinDirectionOutput);
static inline void pinMode(uint8_t pin, int mode) {
uint8_t bv = _BV(pin & 0xf);
if (mode == PinDirectionOutput) {
_SFR_IO8((pin >> 4) + 1) |= bv;
} else {
_SFR_IO8((pin >> 4) + 1) &= ~bv;
_SFR_IO8((pin >> 4) + 2) &= ~bv;
}
}
// ex: digitalWrite(B0, PinLevelHigh);
static inline void digitalWrite(uint8_t pin, int mode) {
uint8_t bv = _BV(pin & 0xf);
if (mode == PinLevelHigh) {
_SFR_IO8((pin >> 4) + 2) |= bv;
} else {
_SFR_IO8((pin >> 4) + 2) &= ~bv;
}
} }
}
// Return true if the pin is HIGH
// ex: digitalWrite(B0, PinLevelHigh); // digitalRead(B0)
static inline void digitalWrite(uint8_t pin, int mode) { static inline bool digitalRead(uint8_t pin) {
uint8_t bv = _BV(pin & 0xf); return _SFR_IO8(pin >> 4) & _BV(pin & 0xf);
if (mode == PinLevelHigh) {
_SFR_IO8((pin >> 4) + 2) |= bv;
} else {
_SFR_IO8((pin >> 4) + 2) &= ~bv;
} }
}
// Return true if the pin is HIGH #endif
// digitalRead(B0)
static inline bool digitalRead(uint8_t pin) {
return _SFR_IO8(pin >> 4) & _BV(pin & 0xf);
}

@ -41,6 +41,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "print.h" #include "print.h"
#include "send_string_keycodes.h" #include "send_string_keycodes.h"
#include "pincontrol.h"
extern uint32_t default_layer_state; extern uint32_t default_layer_state;

Loading…
Cancel
Save