matrix.c 5.2 KB

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