2018-01-31 18:31:20 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "hal.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "wait.h"
|
|
|
|
#include "printf.h"
|
|
|
|
#include "backlight.h"
|
|
|
|
#include "matrix.h"
|
|
|
|
|
2018-02-10 21:32:05 +00:00
|
|
|
#include "usb_main.h"
|
2018-02-14 20:35:24 +00:00
|
|
|
#include "twi2c.h"
|
2018-01-31 18:31:20 +00:00
|
|
|
|
|
|
|
/* QMK Handwire
|
|
|
|
*
|
|
|
|
* Column pins are input with internal pull-down.
|
|
|
|
* Row pins are output and strobe with high.
|
|
|
|
* Key is high or 1 when it turns on.
|
|
|
|
*
|
2018-02-01 19:48:25 +00:00
|
|
|
* col: { A13, A14, A15, B3, B4, B5, B6 }
|
|
|
|
* row: { B10, B2, B1, B0, A7, A6 }
|
2018-01-31 18:31:20 +00:00
|
|
|
*/
|
|
|
|
/* matrix state(1:on, 0:off) */
|
|
|
|
static matrix_row_t matrix[MATRIX_ROWS];
|
2018-02-01 19:48:25 +00:00
|
|
|
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
|
2018-01-31 18:31:20 +00:00
|
|
|
static bool debouncing = false;
|
|
|
|
static uint16_t debouncing_time = 0;
|
|
|
|
|
2018-02-10 21:32:05 +00:00
|
|
|
static bool master = false;
|
|
|
|
static bool right_hand = false;
|
|
|
|
|
2018-01-31 18:31:20 +00:00
|
|
|
__attribute__ ((weak))
|
|
|
|
void matrix_init_user(void) {}
|
|
|
|
|
|
|
|
__attribute__ ((weak))
|
|
|
|
void matrix_scan_user(void) {}
|
|
|
|
|
|
|
|
__attribute__ ((weak))
|
|
|
|
void matrix_init_kb(void) {
|
|
|
|
matrix_init_user();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((weak))
|
|
|
|
void matrix_scan_kb(void) {
|
|
|
|
matrix_scan_user();
|
|
|
|
}
|
|
|
|
|
|
|
|
void matrix_init(void) {
|
|
|
|
printf("matrix init\n");
|
2018-02-14 20:35:24 +00:00
|
|
|
// debug_matrix = true;
|
2018-01-31 18:31:20 +00:00
|
|
|
|
2018-02-10 21:32:05 +00:00
|
|
|
// C13 is connected to VCC on the right hand
|
|
|
|
palSetPadMode(GPIOC, 13, PAL_MODE_INPUT);
|
|
|
|
wait_us(20);
|
|
|
|
right_hand = palReadPad(GPIOC, 13);
|
|
|
|
|
|
|
|
// if USB is active, this is the master
|
2018-02-15 07:06:06 +00:00
|
|
|
// master = usbGetDriverStateI(&USB_DRIVER) == USB_ACTIVE;
|
|
|
|
master = right_hand;
|
2018-02-10 21:32:05 +00:00
|
|
|
|
2018-02-14 20:35:24 +00:00
|
|
|
if (master) {
|
|
|
|
twi2c_master_init();
|
|
|
|
} else {
|
|
|
|
twi2c_slave_init();
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:31:20 +00:00
|
|
|
/* Column(sense) */
|
2018-02-01 19:48:25 +00:00
|
|
|
palSetPadMode(GPIOA, 13, PAL_MODE_INPUT_PULLDOWN);
|
|
|
|
palSetPadMode(GPIOA, 14, PAL_MODE_INPUT_PULLDOWN);
|
|
|
|
palSetPadMode(GPIOA, 15, PAL_MODE_INPUT_PULLDOWN);
|
|
|
|
palSetPadMode(GPIOB, 3, PAL_MODE_INPUT_PULLDOWN);
|
|
|
|
palSetPadMode(GPIOB, 4, PAL_MODE_INPUT_PULLDOWN);
|
|
|
|
palSetPadMode(GPIOB, 5, PAL_MODE_INPUT_PULLDOWN);
|
|
|
|
palSetPadMode(GPIOB, 6, PAL_MODE_INPUT_PULLDOWN);
|
2018-01-31 18:31:20 +00:00
|
|
|
|
|
|
|
/* Row(strobe) */
|
2018-02-01 19:48:25 +00:00
|
|
|
palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
|
|
|
|
palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
|
|
|
|
palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
|
|
|
|
palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
|
|
|
|
palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL);
|
|
|
|
palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL);
|
2018-01-31 18:31:20 +00:00
|
|
|
|
|
|
|
memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
|
2018-02-01 19:48:25 +00:00
|
|
|
memset(matrix_debouncing, 0, MATRIX_ROWS * sizeof(matrix_row_t));
|
2018-01-31 18:31:20 +00:00
|
|
|
|
2018-02-01 19:48:25 +00:00
|
|
|
// palClearPad(GPIOB, 7); // Turn off capslock
|
2018-01-31 18:31:20 +00:00
|
|
|
matrix_init_quantum();
|
|
|
|
}
|
|
|
|
|
2018-02-10 21:32:05 +00:00
|
|
|
matrix_row_t matrix_scan_common(uint8_t row) {
|
|
|
|
matrix_row_t data;
|
|
|
|
|
|
|
|
// strobe row { A6, A7, B0, B1, B2, B10 }
|
|
|
|
switch (row) {
|
|
|
|
case 5: palSetPad(GPIOA, 6); break;
|
|
|
|
case 4: palSetPad(GPIOA, 7); break;
|
|
|
|
case 3: palSetPad(GPIOB, 0); break;
|
|
|
|
case 2: palSetPad(GPIOB, 1); break;
|
|
|
|
case 1: palSetPad(GPIOB, 2); break;
|
|
|
|
case 0: palSetPad(GPIOB, 10); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need wait to settle pin state
|
|
|
|
wait_us(20);
|
|
|
|
|
|
|
|
// read col data { B6, B5, B4, B3, A15, A14, A13 }
|
|
|
|
data = (
|
|
|
|
(palReadPad(GPIOB, 6) << 6 ) |
|
|
|
|
(palReadPad(GPIOB, 5) << 5 ) |
|
|
|
|
(palReadPad(GPIOB, 4) << 4 ) |
|
|
|
|
(palReadPad(GPIOB, 3) << 3 ) |
|
|
|
|
(palReadPad(GPIOA, 15) << 2 ) |
|
|
|
|
(palReadPad(GPIOA, 14) << 1 ) |
|
|
|
|
(palReadPad(GPIOA, 13) << 0 )
|
|
|
|
);
|
|
|
|
|
|
|
|
// unstrobe row { A6, A7, B0, B1, B2, B10 }
|
|
|
|
switch (row) {
|
|
|
|
case 5: palClearPad(GPIOA, 6); break;
|
|
|
|
case 4: palClearPad(GPIOA, 7); break;
|
|
|
|
case 3: palClearPad(GPIOB, 0); break;
|
|
|
|
case 2: palClearPad(GPIOB, 1); break;
|
|
|
|
case 1: palClearPad(GPIOB, 2); break;
|
|
|
|
case 0: palClearPad(GPIOB, 10); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-02-15 07:06:06 +00:00
|
|
|
const uint8_t command[2] = { 0x01, 0x00 };
|
|
|
|
uint8_t other_matrix[MATRIX_ROWS] = { 0 };
|
2018-02-10 21:32:05 +00:00
|
|
|
|
2018-02-15 07:06:06 +00:00
|
|
|
void matrix_scan_master(void) {
|
2018-02-10 21:32:05 +00:00
|
|
|
|
2018-02-14 20:35:24 +00:00
|
|
|
msg_t resp;
|
2018-02-23 02:22:47 +00:00
|
|
|
// resp = twi2c_master_send(slaveI2Caddress/2, command, 2, other_matrix, US2ST(100));
|
2018-02-23 17:09:03 +00:00
|
|
|
resp = i2cMasterTransmitTimeout(&I2C_DRIVER, slaveI2Caddress/2, command, 2, other_matrix, MATRIX_ROWS / 2, MS2ST(100));
|
|
|
|
// resp = i2cMasterReceiveTimeout(&I2C_DRIVER, slaveI2Caddress/2, other_matrix, MATRIX_ROWS / 2, US2ST(100));
|
2018-02-15 07:06:06 +00:00
|
|
|
// printf("%x\n", resp);
|
2018-02-23 02:22:47 +00:00
|
|
|
// if (resp != MSG_OK) {
|
|
|
|
// for (i = 0; i < MATRIX_ROWS / 2; i++) {
|
|
|
|
// resp = i2cMasterReceiveTimeout(&I2C_DRIVER, slaveI2Caddress/2, other_matrix, MATRIX_ROWS / 2, US2ST(100));
|
|
|
|
// }
|
|
|
|
// }
|
2018-02-14 20:35:24 +00:00
|
|
|
|
2018-02-15 07:06:06 +00:00
|
|
|
if (resp == MSG_OK) {
|
|
|
|
uint8_t * matrix_pointer;
|
|
|
|
if (right_hand) {
|
|
|
|
matrix_pointer = matrix;
|
|
|
|
} else {
|
|
|
|
matrix_pointer = matrix + (MATRIX_ROWS / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(matrix_pointer, other_matrix, MATRIX_ROWS / 2);
|
|
|
|
}
|
2018-02-10 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 18:31:20 +00:00
|
|
|
uint8_t matrix_scan(void) {
|
2018-02-14 20:35:24 +00:00
|
|
|
|
2018-02-01 19:48:25 +00:00
|
|
|
for (int row = 0; row < MATRIX_ROWS; row++) {
|
2018-01-31 18:31:20 +00:00
|
|
|
matrix_row_t data = 0;
|
|
|
|
|
2018-02-14 20:35:24 +00:00
|
|
|
if ((right_hand && row >= 6) || (!right_hand && row < 6)) {
|
2018-02-10 21:32:05 +00:00
|
|
|
data = matrix_scan_common(row % 6);
|
2018-01-31 18:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 19:48:25 +00:00
|
|
|
if (matrix_debouncing[row] != data) {
|
|
|
|
matrix_debouncing[row] = data;
|
2018-01-31 18:31:20 +00:00
|
|
|
debouncing = true;
|
|
|
|
debouncing_time = timer_read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
|
|
|
|
for (int row = 0; row < MATRIX_ROWS; row++) {
|
2018-02-01 19:48:25 +00:00
|
|
|
matrix[row] = matrix_debouncing[row];
|
2018-01-31 18:31:20 +00:00
|
|
|
}
|
|
|
|
debouncing = false;
|
|
|
|
}
|
2018-02-14 20:35:24 +00:00
|
|
|
|
|
|
|
if (master) {
|
|
|
|
matrix_scan_master();
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:31:20 +00:00
|
|
|
matrix_scan_quantum();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool matrix_is_on(uint8_t row, uint8_t col) {
|
|
|
|
return (matrix[row] & (1<<col));
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix_row_t matrix_get_row(uint8_t row) {
|
|
|
|
return matrix[row];
|
|
|
|
}
|
|
|
|
|
2018-02-14 20:35:24 +00:00
|
|
|
void matrix_copy(matrix_row_t * copy) {
|
|
|
|
uint8_t * matrix_pointer;
|
|
|
|
if (right_hand) {
|
|
|
|
matrix_pointer = matrix + (MATRIX_ROWS / 2);
|
|
|
|
} else {
|
|
|
|
matrix_pointer = matrix;
|
|
|
|
}
|
|
|
|
memcpy(copy, matrix_pointer, MATRIX_ROWS / 2);
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:31:20 +00:00
|
|
|
void matrix_print(void) {
|
|
|
|
printf("\nr/c 01234567\n");
|
|
|
|
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
|
|
|
printf("%X0: ", row);
|
|
|
|
matrix_row_t data = matrix_get_row(row);
|
|
|
|
for (int col = 0; col < MATRIX_COLS; col++) {
|
|
|
|
if (data & (1<<col))
|
|
|
|
printf("1");
|
|
|
|
else
|
|
|
|
printf("0");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|