matrix.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #define RIGHT_HALF
  19. void matrix_set_row_status(uint8_t row);
  20. #if defined(RIGHT_HALF)
  21. /* ----------------------- hardware I/O abstraction ------------------------ */
  22. #define PORTCOLUMNS PORTB ///< port on which we read the state of the columns
  23. #define PINCOLUMNS PINB ///< port on which we read the state of the columns
  24. #define DDRCOLUMNS DDRB ///< port on which we read the state of the columns
  25. #define PORTROWS1 PORTA ///< first port connected to the matrix rows
  26. #define PINROWS1 PINA ///< first port connected to the matrix rows
  27. #define DDRROWS1 DDRA ///< first port connected to the matrix rows
  28. #define PORTROWS2 PORTC ///< second port connected to the matrix rows
  29. #define PINROWS2 PINC ///< second port connected to the matrix rows
  30. #define DDRROWS2 DDRC ///< second port connected to the matrix rows
  31. // register addresses (see "mcp23018.md")
  32. #define IODIRA 0x00 // i/o direction register
  33. #define IODIRB 0x01
  34. #define GPPUA 0x0C // GPIO pull-up resistor register
  35. #define GPPUB 0x0D
  36. #define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  37. #define GPIOB 0x13
  38. #define OLATA 0x14 // output latch register
  39. #define OLATB 0x15
  40. #define TW_READ 1
  41. #define TW_WRITE 0
  42. #define MCP23018_TWI_ADDRESS 0b0100000
  43. // TWI aliases
  44. #define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE )
  45. #define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ )
  46. #define I2C_TIMEOUT 10
  47. #define MCP_ROWS_START 8
  48. uint8_t mcp23018_init(void) {
  49. uint8_t ret;
  50. uint8_t data[3];
  51. // set pin direction
  52. // - unused : input : 1
  53. // - input : input : 1
  54. // - driving : output : 0
  55. data[0] = IODIRA;
  56. data[1] = 0b00000000; // IODIRA
  57. data[2] = (0b11111111); // IODIRB
  58. ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
  59. if (ret) goto out; // make sure we got an ACK
  60. // set pull-up
  61. // - unused : on : 1
  62. // - input : on : 1
  63. // - driving : off : 0
  64. data[0] = GPPUA;
  65. data[1] = 0b00000000; // IODIRA
  66. data[2] = (0b11111111); // IODIRB
  67. ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
  68. if (ret) goto out; // make sure we got an ACK
  69. // set logical value (doesn't matter on inputs)
  70. // - unused : hi-Z : 1
  71. // - input : hi-Z : 1
  72. // - driving : hi-Z : 1
  73. data[0] = OLATA;
  74. data[1] = 0b11111111; // IODIRA
  75. data[2] = (0b11111111); // IODIRB
  76. ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
  77. out:
  78. return ret;
  79. }
  80. #endif
  81. void matrix_init_custom(void) {
  82. // initialize matrix ports - cols, rows
  83. // PB0-PB7 : col0 .. col7
  84. // PA0-PA7 : row0 .. row7
  85. // PC7-PC2 : row8 .. row13
  86. // PD0 : NUM
  87. // PD1 : CAPS
  88. // PD2 : D+ / Clock
  89. // PD3 : D- / Data
  90. // PD4 : FULL LED
  91. // PD5 : 3.6V switch TR
  92. // PD6 : SCRL
  93. // PD7 : row14
  94. // signal direction : col -> row
  95. // pc(PORTROWS1)0, 1 : twi
  96. DDRCOLUMNS = 0xFF; // all outputs for cols
  97. PORTCOLUMNS = 0xFF; // high
  98. // all inputs for rows
  99. DDRROWS1 = 0x00;
  100. DDRROWS2 &= ~(0x111111<<2); //0x00;
  101. DDRD &= ~(1<<PIND7); // row 14
  102. // all rows pull-up.
  103. PORTROWS1 = 0xFF;
  104. PORTROWS2 |= (0b111111<<2); //0x11111100;
  105. PORTD |= (1<<PIND7);// row 14
  106. i2c_init();
  107. #if defined(RIGHT_HALF)
  108. // Initialize the chip on the other half
  109. mcp23018_init();
  110. #endif
  111. }
  112. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  113. bool matrix_has_changed = false;
  114. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  115. // Store last value of row prior to reading
  116. matrix_row_t last_row_value = current_matrix[row];
  117. matrix_row_t cols = 0;
  118. // Select the row to scan
  119. matrix_set_row_status(row);
  120. matrix_io_delay();
  121. //Set the local row
  122. #if defined(RIGHT_HALF)
  123. // Initialize to 0x7F in case I2C read fails,
  124. // as 0x75 would be no keys pressed
  125. uint8_t data = 0x7F;
  126. // Receive the columns from right half
  127. i2c_receive(TWI_ADDR_WRITE, &data, 1, I2C_TIMEOUT);
  128. #endif
  129. // cols |= ((~(PINA | 0x80)) & 0x7F);
  130. cols |= ((~(PINA)) & 0xFF);
  131. #if defined(RIGHT_HALF)
  132. cols |= (((~(data | 0x80)) & 0x7F) << MCP_ROWS_START);
  133. #endif
  134. current_matrix[row] = cols;
  135. matrix_has_changed |= (last_row_value != current_matrix[row]);
  136. }
  137. return matrix_has_changed;
  138. }
  139. void matrix_set_row_status(uint8_t row) {
  140. #if defined(RIGHT_HALF)
  141. uint8_t txdata[3];
  142. //Set the remote row on port A
  143. txdata[0] = (GPIOA);
  144. txdata[1] = ( 0xFF & ~(1<<row) );
  145. i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)txdata, 2, I2C_TIMEOUT);
  146. #endif
  147. //Set the local row on port B
  148. DDRB = (1 << row);
  149. PORTB = ~(1 << row);
  150. }