qmk-keychron-q3-colemak-dh/keyboards/handwired/symmetric70_proto/matrix_fast/matrix_config_expand.c
Takeshi ISHII b89abc402b
[Keyboard] Update Symmetric70 keyboard prototype (#12707)
* update handwired/symmetric70_proto/matrix.c

* make keyboards/handwired/symmetric70_proto/promicro/

* add symmetric70_proto/debug_config.h, update symmetric70_proto/local_features.mk etc.

  set MATRIX_IO_DELAY macro
    make MTEST=mdelay0  symmetric70_proto/promicro:default:flash
    make MTEST=mdelay1  symmetric70_proto/promicro:default:flash
    make MTEST=mdelay10 symmetric70_proto/promicro:default:flash
    make MTEST=mdelay30 symmetric70_proto/promicro:default:flash
  set DEBUG_MATRIX_SCAN_RATE_ENABLE yes
    make MTEST=scan symmetric70_proto/promicro:default:flash
  set MATRIX_DEBUG_DELAY and MATRIX_IO_DELAY macro
    make MTEST=matrix_debug_delay,mdelay0 symmetric70_proto/promicro:default:flash
  set MATRIX_DEBUG_SCAN
    make MTEST=matrix_debug_scan symmetric70_proto/promicro:default:flash

* add symmetric70_proto/matrix_debug/readme.md

* update symmetric70_proto/matrix_debug/readme.md

* update handwired/symmetric70_proto/readme.md

* update handwired/symmetric70_proto/readme.md

* update handwired/symmetric70_proto/*/readme.md

* add handwired/symmetric70_proto/matrix_fast/

* update handwired/symmetric70_proto/matrix_fast/readme.md

* fix typo in handwired/symmetric70_proto/matrix_fast/readme.md

* update config.h under handwired/symmetric70_proto/promicro

* add Proton C support to handwired/symmetric70_proto

* add handwired/symmetric70_proto/proton_c/readme.md

* add promicro/*/readme.md proton_c/*/readme.md

* update handwired/symmetric70_proto/proton_c/proton_c.c

support MATRIX_IO_DELAY_DEFAULT for testing.

* Added another implementation of 'adaptive_delay'.

* update symmetric70_proto/local_features.mk

* update symmetric70_proto/matrix_fast/gpio_extr.h

* add matrix_output_unselect_delay_ports()

* add MTEST=adaptive_delay_fast option

* update symmetric70_proto/matrix_debug/readme.md

* update symmetric70_proto/matrix_fast/readme.md

* update symmetric70_proto/matrix_debug/readme.md

* Erase garbage

* fix symmetric70_proto/proton_c/proton_c.c

* improve adaptive_delay_fast in symmetric70_proto/matrix_debug/matrix.c

* update symmetric70_proto/matrix_debug/readme.md

* fix symmetric70_proto/matrix_debug/readme.md

* Update keyboards/handwired/symmetric70_proto/proton_c/rules.mk

Co-authored-by: Ryan <fauxpark@gmail.com>

* Update keyboards/handwired/symmetric70_proto/proton_c/rules.mk

Co-authored-by: Ryan <fauxpark@gmail.com>

* Update keyboards/handwired/symmetric70_proto/local_features.mk

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/local_features.mk

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/local_features.mk

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_debug/readme.md

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_debug/readme.md

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_debug/readme.md

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Update keyboards/handwired/symmetric70_proto/matrix_fast/matrix_config_expand.c

Co-authored-by: Nick Brassel <nick@tzarc.org>

Co-authored-by: Ryan <fauxpark@gmail.com>
Co-authored-by: Nick Brassel <nick@tzarc.org>
2021-07-13 18:14:27 +09:00

235 lines
8.2 KiB
C

/*
Copyright 2021 mtei
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
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// clang-format off
#include "matrix_extr.h"
#ifdef DEBUG_MATRIX_CONFIG
// config expand debug
// avr-gcc -DDEBUG_MATRIX_CONFIG=\"test_config.h\" -E -C matrix_config_expand.c
# include DEBUG_MATRIX_CONFIG
#endif
#undef NO_PIN /* cancel NO_PIN define in tmk_core/common/pin_defs.h */
#define NO_PIN NO_PORT, 0
#if MATRIX_TYPE == DIRECT_SWITCH
# undef MATRIX_OUT_PORTS
# define MATRIX_OUT_PINS (0, NO_PIN)
#endif
#include "cpp_map.h"
#if defined(MATRIX_EXTENSION_74HC157) || defined(MATRIX_EXTENSION_74HC153)
# define MATRIX_EXTENSION "matrix_extension_74hc15x.c"
#endif
#ifdef MATRIX_EXTENSION
# include MATRIX_EXTENSION
#endif
#ifdef MATRIX_GPIO_NEED_SEPARATE_ATOMIC
# ifndef setMatrixInputHigh
# define setMatrixInputHigh(dev, port, bit) do { if ((dev) == MCU_GPIO) { setPortBitInputHigh_atomic(port, bit); }} while(0)
# endif
# ifndef setMatrixOutput_writeHighZ
# define setMatrixOutput_writeHighZ(dev, port, bit) do { if ((dev) == MCU_GPIO) { setPortBitInputHigh_atomic(port, bit); }} while(0)
# endif
# ifndef setMatrixOutput_writeLow
# define setMatrixOutput_writeLow(dev, port, bit) do { if ((dev) == MCU_GPIO) { setPortBitOutput_writeLow_atomic(port, bit); }} while(0)
# endif
#else
# ifndef setMatrixInputHigh
# define setMatrixInputHigh(dev, port, bit) do { if ((dev) == MCU_GPIO) { setPortBitInputHigh(port, bit); }} while(0)
# endif
# ifndef setMatrixOutput_writeHighZ
# define setMatrixOutput_writeHighZ(dev, port, bit) do { if ((dev) == MCU_GPIO) { setPortBitInputHigh(port, bit); }} while(0)
# endif
# ifndef setMatrixOutput_writeLow
# define setMatrixOutput_writeLow(dev, port, bit) do { if ((dev) == MCU_GPIO) { setPortBitOutput_writeLow(port, bit); }} while(0)
# endif
#endif
#ifndef readMatrixPort
# define readMatrixPort(dev, port) (((dev) == MCU_GPIO) ? readPort(port) : 0)
#endif
#ifndef getMatrixInputMaskBit
# define getMatrixInputMaskBit(dev, bit) (((dev) != NO_DEVICE) ? _BV((bit)&0xF) : 0)
#endif
#ifndef init_extension
# define init_extension()
#endif
enum DEVICE_NAME {
MCU_GPIO,
NO_DEVICE,
#ifdef MATRIX_DEVICES
MATRIX_DEVICES
#endif
};
#define _INPUT_PORTS_ENUM_ELEMENT(name, dev, port) inport_index_##name,
#define INPUT_PORTS_ENUM_ELEMENT(x) _INPUT_PORTS_ENUM_ELEMENT x
enum INPUT_PORTS {
INPUT_PORTS_ENUM_ELEMENT((NO_PORT, NO_DEVICE, 0))
MAP(INPUT_PORTS_ENUM_ELEMENT, MATRIX_IN_PORTS)
NUM_OF_INPUT_PORTS
};
#define _INPUT_PINS_ENUM_ELEMENT(index, port, bit) inpin_index_##index,
#define INPUT_PINS_ENUM_ELEMENT(x) _INPUT_PINS_ENUM_ELEMENT x
enum INPUT_PINS {
MAP(INPUT_PINS_ENUM_ELEMENT, MATRIX_IN_PINS)
END_inpin_index
};
#define _OUTPUT_PORTS_ENUM_ELEMENT(name, dev, port) outport_index_##name,
#define OUTPUT_PORTS_ENUM_ELEMENT(x) _OUTPUT_PORTS_ENUM_ELEMENT x
enum OUTPUT_PORTS {
OUTPUT_PORTS_ENUM_ELEMENT((NO_PORT, NO_DEVICE, 0))
#ifdef MATRIX_OUT_PORTS
MAP(OUTPUT_PORTS_ENUM_ELEMENT, MATRIX_OUT_PORTS)
#endif
NUM_OF_OUTPUT_PORTS
};
#define _OUTPUT_PINS_ENUM_ELEMENT(index, port, bit) outpin_index_##index,
#define OUTPUT_PINS_ENUM_ELEMENT(x) _OUTPUT_PINS_ENUM_ELEMENT x
enum OUTPUT_PINS {
MAP(OUTPUT_PINS_ENUM_ELEMENT, MATRIX_OUT_PINS)
END_outpin_index
};
port_width_t iport_mask[NUM_OF_INPUT_PORTS];
#define _INPUT_PORTS_LIST_ELEMENT(name, dev, port) \
[inport_index_##name] = { dev, port },
#define INPUT_PORTS_LIST_ELEMENT(x) _INPUT_PORTS_LIST_ELEMENT x
LOCAL_DATA
const port_descriptor inport_list[NUM_OF_INPUT_PORTS] = {
INPUT_PORTS_LIST_ELEMENT((NO_PORT, NO_DEVICE, 0))
MAP(INPUT_PORTS_LIST_ELEMENT, MATRIX_IN_PORTS)
};
#define _OUTPUT_PORTS_LIST_ELEMENT(name, dev, port) \
[outport_index_##name] = { dev, port },
#define OUTPUT_PORTS_LIST_ELEMENT(x) _OUTPUT_PORTS_LIST_ELEMENT x
LOCAL_DATA
const port_descriptor outport_list[NUM_OF_OUTPUT_PORTS] = {
OUTPUT_PORTS_LIST_ELEMENT((NO_PORT, NO_DEVICE, 0))
#ifdef MATRIX_OUT_PORTS
MAP(OUTPUT_PORTS_LIST_ELEMENT, MATRIX_OUT_PORTS)
#endif
};
#define _SELECT_OUTPUT_PIN(index, pname, bit) \
case outpin_index_##index: \
setMatrixOutput_writeLow(outport_list[outport_index_##pname].device, \
outport_list[outport_index_##pname].port, bit); \
break;
#define SELECT_OUTPUT_PIN(x) _SELECT_OUTPUT_PIN x
LOCAL_FUNC ALWAYS_INLINE void select_output(uint8_t out_index);
LOCAL_FUNC
void select_output(uint8_t out_index) {
switch (out_index) {
MAP(SELECT_OUTPUT_PIN, MATRIX_OUT_PINS)
}
}
#define _UNSELECT_OUTPUT_PIN(index, pname, bit) \
case outpin_index_##index: \
setMatrixOutput_writeHighZ(outport_list[outport_index_##pname].device, \
outport_list[outport_index_##pname].port, bit); \
break;
#define UNSELECT_OUTPUT_PIN(x) _UNSELECT_OUTPUT_PIN x
LOCAL_FUNC ALWAYS_INLINE void unselect_output_inline(uint8_t out_index);
LOCAL_FUNC
void unselect_output_inline(uint8_t out_index) {
switch (out_index) {
MAP(UNSELECT_OUTPUT_PIN, MATRIX_OUT_PINS)
}
}
#define _INIT_INPUT_PIN(index, pname, bit) \
setMatrixInputHigh(inport_list[inport_index_##pname].device, \
inport_list[inport_index_##pname].port, bit);
#define INIT_INPUT_PIN(x) _INIT_INPUT_PIN x
LOCAL_FUNC
void init_input_ports(void) {
MAP(INIT_INPUT_PIN, MATRIX_IN_PINS)
}
#define _INIT_INPORT_MASK(index, pname, bit) \
iport_mask[inport_index_##pname] |= getMatrixInputMaskBit(inport_list[inport_index_##pname].device, bit);
#define INIT_INPORT_MASK(x) _INIT_INPORT_MASK x
LOCAL_FUNC
void init_inport_mask(void) {
for (int i = 0; i < NUM_OF_INPUT_PORTS; i++ ) {
iport_mask[i] = 0;
}
MAP(INIT_INPORT_MASK, MATRIX_IN_PINS)
}
#define _READ_INPUT_PORT(name, dev, port) \
buffer[inport_index_##name] = readMatrixPort(dev, port);
#define READ_INPUT_PORT(x) _READ_INPUT_PORT x
LOCAL_FUNC
ALWAYS_INLINE void read_all_input_ports(port_width_t buffer[NUM_OF_INPUT_PORTS], bool wait_unselect);
LOCAL_FUNC
void read_all_input_ports(port_width_t buffer[NUM_OF_INPUT_PORTS], bool wait_unselect) {
READ_INPUT_PORT((NO_PORT, NO_DEVICE, 0))
MAP(READ_INPUT_PORT, MATRIX_IN_PORTS)
}
#define _MASK_INPUT(name, dev, port) \
mask |= ((~buffer[inport_index_##name]) & iport_mask[inport_index_##name]);
#define MASK_INPUT(x) _MASK_INPUT x
LOCAL_FUNC ALWAYS_INLINE void wait_unselect_done(void);
LOCAL_FUNC
void wait_unselect_done(void) {
port_width_t mask;
port_width_t buffer[NUM_OF_INPUT_PORTS];
do {
read_all_input_ports(buffer, true);
MATRIX_DEBUG_DELAY_END();
mask = 0;
MAP(MASK_INPUT, MATRIX_IN_PORTS);
MATRIX_DEBUG_DELAY_START();
} while (mask != 0);
}
#define _BUILD_INPUT_PORT(index, pname, bit) \
result |= (buffer[inport_index_##pname] & _BV(bit)) ? 0 : _BV(inpin_index_##index);
#define BUILD_INPUT_PORT(x) _BUILD_INPUT_PORT x
LOCAL_FUNC ALWAYS_INLINE matrix_line_t build_matrix_line(port_width_t buffer[NUM_OF_INPUT_PORTS]);
LOCAL_FUNC
matrix_line_t build_matrix_line(port_width_t buffer[NUM_OF_INPUT_PORTS]) {
matrix_line_t result = 0;
MAP(BUILD_INPUT_PORT, MATRIX_IN_PINS);
return result;
}
#define _BUILD_INPUT_PORT_DIRECT(index, pname, bit) \
matrix[(inpin_index_##index)/MATRIX_COLS] \
|= (buffer[inport_index_##pname] & _BV(bit)) ? 0 : _BV((inpin_index_##index)%MATRIX_COLS);
#define BUILD_INPUT_PORT_DIRECT(x) _BUILD_INPUT_PORT_DIRECT x
LOCAL_FUNC ALWAYS_INLINE void build_matrix_direct(port_width_t buffer[NUM_OF_INPUT_PORTS], matrix_line_t matrix[]);
LOCAL_FUNC
void build_matrix_direct(port_width_t buffer[NUM_OF_INPUT_PORTS], matrix_line_t matrix[]) {
MAP(BUILD_INPUT_PORT_DIRECT, MATRIX_IN_PINS);
}