diff options
Diffstat (limited to 'keyboards/oddforge/vea/matrix.c')
| -rw-r--r-- | keyboards/oddforge/vea/matrix.c | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/keyboards/oddforge/vea/matrix.c b/keyboards/oddforge/vea/matrix.c new file mode 100644 index 000000000..118d3e8ae --- /dev/null +++ b/keyboards/oddforge/vea/matrix.c | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2021 MajorKoos <github.com/majorkoos> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <string.h> | ||
| 19 | #include <stdio.h> | ||
| 20 | #include "quantum.h" | ||
| 21 | #include "i2c_master.h" | ||
| 22 | #include "vea.h" | ||
| 23 | |||
| 24 | #define RIGHT_HALF | ||
| 25 | |||
| 26 | |||
| 27 | void matrix_set_row_status(uint8_t row); | ||
| 28 | |||
| 29 | |||
| 30 | #if defined(RIGHT_HALF) | ||
| 31 | /* ----------------------- hardware I/O abstraction ------------------------ */ | ||
| 32 | #define PORTCOLUMNS PORTB ///< port on which we read the state of the columns | ||
| 33 | #define PINCOLUMNS PINB ///< port on which we read the state of the columns | ||
| 34 | #define DDRCOLUMNS DDRB ///< port on which we read the state of the columns | ||
| 35 | #define PORTROWS1 PORTA ///< first port connected to the matrix rows | ||
| 36 | #define PINROWS1 PINA ///< first port connected to the matrix rows | ||
| 37 | #define DDRROWS1 DDRA ///< first port connected to the matrix rows | ||
| 38 | #define PORTROWS2 PORTC ///< second port connected to the matrix rows | ||
| 39 | #define PINROWS2 PINC ///< second port connected to the matrix rows | ||
| 40 | #define DDRROWS2 DDRC ///< second port connected to the matrix rows | ||
| 41 | |||
| 42 | |||
| 43 | // register addresses (see "mcp23018.md") | ||
| 44 | #define IODIRA 0x00 // i/o direction register | ||
| 45 | #define IODIRB 0x01 | ||
| 46 | #define GPPUA 0x0C // GPIO pull-up resistor register | ||
| 47 | #define GPPUB 0x0D | ||
| 48 | #define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT) | ||
| 49 | #define GPIOB 0x13 | ||
| 50 | #define OLATA 0x14 // output latch register | ||
| 51 | #define OLATB 0x15 | ||
| 52 | |||
| 53 | #define TW_READ 1 | ||
| 54 | #define TW_WRITE 0 | ||
| 55 | |||
| 56 | #define MCP23018_TWI_ADDRESS 0b0100000 | ||
| 57 | |||
| 58 | // TWI aliases | ||
| 59 | #define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE ) | ||
| 60 | #define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ ) | ||
| 61 | |||
| 62 | #define I2C_TIMEOUT 10 | ||
| 63 | #define MCP_ROWS_START 8 | ||
| 64 | |||
| 65 | uint8_t mcp23018_init(void) { | ||
| 66 | uint8_t ret; | ||
| 67 | uint8_t data[3]; | ||
| 68 | // set pin direction | ||
| 69 | // - unused : input : 1 | ||
| 70 | // - input : input : 1 | ||
| 71 | // - driving : output : 0 | ||
| 72 | data[0] = IODIRA; | ||
| 73 | data[1] = 0b00000000; // IODIRA | ||
| 74 | data[2] = (0b11111111); // IODIRB | ||
| 75 | |||
| 76 | ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT); | ||
| 77 | if (ret) goto out; // make sure we got an ACK | ||
| 78 | // set pull-up | ||
| 79 | // - unused : on : 1 | ||
| 80 | // - input : on : 1 | ||
| 81 | // - driving : off : 0 | ||
| 82 | data[0] = GPPUA; | ||
| 83 | data[1] = 0b00000000; // IODIRA | ||
| 84 | data[2] = (0b11111111); // IODIRB | ||
| 85 | |||
| 86 | ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT); | ||
| 87 | if (ret) goto out; // make sure we got an ACK | ||
| 88 | |||
| 89 | // set logical value (doesn't matter on inputs) | ||
| 90 | // - unused : hi-Z : 1 | ||
| 91 | // - input : hi-Z : 1 | ||
| 92 | // - driving : hi-Z : 1 | ||
| 93 | data[0] = OLATA; | ||
| 94 | data[1] = 0b11111111; // IODIRA | ||
| 95 | data[2] = (0b11111111); // IODIRB | ||
| 96 | |||
| 97 | ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT); | ||
| 98 | out: | ||
| 99 | return ret; | ||
| 100 | } | ||
| 101 | #endif | ||
| 102 | |||
| 103 | void matrix_init_custom(void) { | ||
| 104 | // initialize matrix ports - cols, rows | ||
| 105 | // PB0-PB7 : col0 .. col7 | ||
| 106 | // PA0-PA7 : row0 .. row7 | ||
| 107 | // PC7-PC2 : row8 .. row13 | ||
| 108 | |||
| 109 | // PD0 : NUM | ||
| 110 | // PD1 : CAPS | ||
| 111 | // PD2 : D+ / Clock | ||
| 112 | // PD3 : D- / Data | ||
| 113 | // PD4 : FULL LED | ||
| 114 | // PD5 : 3.6V switch TR | ||
| 115 | // PD6 : SCRL | ||
| 116 | // PD7 : row14 | ||
| 117 | |||
| 118 | |||
| 119 | // signal direction : col -> row | ||
| 120 | |||
| 121 | // pc(PORTROWS1)0, 1 : twi | ||
| 122 | DDRCOLUMNS = 0xFF; // all outputs for cols | ||
| 123 | PORTCOLUMNS = 0xFF; // high | ||
| 124 | |||
| 125 | // all inputs for rows | ||
| 126 | DDRROWS1 = 0x00; | ||
| 127 | DDRROWS2 &= ~(0x111111<<2); //0x00; | ||
| 128 | DDRD &= ~(1<<PIND7); // row 14 | ||
| 129 | |||
| 130 | // all rows pull-up. | ||
| 131 | PORTROWS1 = 0xFF; | ||
| 132 | PORTROWS2 |= (0b111111<<2); //0x11111100; | ||
| 133 | PORTD |= (1<<PIND7);// row 14 | ||
| 134 | |||
| 135 | i2c_init(); | ||
| 136 | |||
| 137 | #if defined(RIGHT_HALF) | ||
| 138 | // Initialize the chip on the other half | ||
| 139 | mcp23018_init(); | ||
| 140 | #endif | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 145 | bool matrix_has_changed = false; | ||
| 146 | |||
| 147 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 148 | // Store last value of row prior to reading | ||
| 149 | matrix_row_t last_row_value = current_matrix[row]; | ||
| 150 | |||
| 151 | matrix_row_t cols = 0; | ||
| 152 | // Select the row to scan | ||
| 153 | matrix_set_row_status(row); | ||
| 154 | |||
| 155 | matrix_io_delay(); | ||
| 156 | //Set the local row | ||
| 157 | |||
| 158 | #if defined(RIGHT_HALF) | ||
| 159 | // Initialize to 0x7F in case I2C read fails, | ||
| 160 | // as 0x75 would be no keys pressed | ||
| 161 | uint8_t data = 0x7F; | ||
| 162 | // Receive the columns from right half | ||
| 163 | i2c_receive(TWI_ADDR_WRITE, &data, 1, I2C_TIMEOUT); | ||
| 164 | #endif | ||
| 165 | |||
| 166 | // cols |= ((~(PINA | 0x80)) & 0x7F); | ||
| 167 | cols |= ((~(PINA)) & 0xFF); | ||
| 168 | #if defined(RIGHT_HALF) | ||
| 169 | cols |= (((~(data | 0x80)) & 0x7F) << MCP_ROWS_START); | ||
| 170 | #endif | ||
| 171 | |||
| 172 | current_matrix[row] = cols; | ||
| 173 | matrix_has_changed |= (last_row_value != current_matrix[row]); | ||
| 174 | } | ||
| 175 | |||
| 176 | return matrix_has_changed; | ||
| 177 | } | ||
| 178 | |||
| 179 | void matrix_set_row_status(uint8_t row) { | ||
| 180 | #if defined(RIGHT_HALF) | ||
| 181 | uint8_t txdata[3]; | ||
| 182 | |||
| 183 | //Set the remote row on port A | ||
| 184 | txdata[0] = (GPIOA); | ||
| 185 | txdata[1] = ( 0xFF & ~(1<<row) ); | ||
| 186 | i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)txdata, 2, I2C_TIMEOUT); | ||
| 187 | #endif | ||
| 188 | |||
| 189 | //Set the local row on port B | ||
| 190 | DDRB = (1 << row); | ||
| 191 | PORTB = ~(1 << row); | ||
| 192 | } | ||
