logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (4140B)


  1. /*
  2. Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
  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 "matrix.h"
  15. #include "i2c_master.h"
  16. #define RIGHT_HALF
  17. void matrix_set_row_status(uint8_t row);
  18. #if defined(RIGHT_HALF)
  19. #define I2C_TIMEOUT 10
  20. #define MCP23018_TWI_ADDRESS 0b0100000
  21. #define TW_READ 1
  22. #define TW_WRITE 0
  23. #define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE )
  24. #define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ )
  25. #define IODIRA 0x00 // i/o direction register
  26. #define IODIRB 0x01
  27. #define IODIRA 0x00 // i/o direction register
  28. #define IODIRB 0x01
  29. #define GPPUA 0x0C // GPIO pull-up resistor register
  30. #define GPPUB 0x0D
  31. #define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  32. #define GPIOB 0x13
  33. #define OLATA 0x14 // output latch register
  34. #define OLATB 0x15
  35. #define MCP_ROWS_START 8
  36. static uint8_t mcp23018_init(void) {
  37. uint8_t ret;
  38. uint8_t data[3];
  39. // set pin direction
  40. // - unused : input : 1
  41. // - input : input : 1
  42. // - driving : output : 0
  43. data[0] = IODIRA;
  44. data[1] = 0b00000000; // IODIRA
  45. data[2] = (0b11111111); // IODIRB
  46. ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
  47. if (ret) goto out; // make sure we got an ACK
  48. // set pull-up
  49. // - unused : on : 1
  50. // - input : on : 1
  51. // - driving : off : 0
  52. data[0] = GPPUA;
  53. data[1] = 0b00000000; // IODIRA
  54. data[2] = (0b11111111); // IODIRB
  55. ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
  56. if (ret) goto out; // make sure we got an ACK
  57. // set logical value (doesn't matter on inputs)
  58. // - unused : hi-Z : 1
  59. // - input : hi-Z : 1
  60. // - driving : hi-Z : 1
  61. data[0] = OLATA;
  62. data[1] = 0b11111111; // IODIRA
  63. data[2] = (0b11111111); // IODIRB
  64. ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
  65. out:
  66. return ret;
  67. }
  68. #endif
  69. void matrix_init_custom(void) {
  70. // Set rows as output starting high
  71. DDRB = 0xFF;
  72. PORTB = 0xFF;
  73. // Set columns as inputs with pull-up enabled
  74. DDRA = 0x00;
  75. PORTA = 0xFF;
  76. // Initialize i2c communication
  77. i2c_init();
  78. #if defined(RIGHT_HALF)
  79. // Initialize the chip on the other half
  80. mcp23018_init();
  81. #endif
  82. }
  83. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  84. bool matrix_has_changed = false;
  85. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  86. // Store last value of row prior to reading
  87. matrix_row_t last_row_value = current_matrix[row];
  88. matrix_row_t cols = 0;
  89. // Select the row to scan
  90. matrix_set_row_status(row);
  91. matrix_io_delay();
  92. //Set the local row
  93. #if defined(RIGHT_HALF)
  94. // Initialize to 0x7F in case I2C read fails,
  95. // as 0x75 would be no keys pressed
  96. uint8_t data = 0x7F;
  97. // Receive the columns from right half
  98. i2c_receive(TWI_ADDR_WRITE, &data, 1, I2C_TIMEOUT);
  99. #endif
  100. cols |= ((~(PINA | 0x80)) & 0x7F);
  101. #if defined(RIGHT_HALF)
  102. cols |= (((~(data | 0x80)) & 0x7F) << 7);
  103. #endif
  104. current_matrix[row] = cols;
  105. matrix_has_changed |= (last_row_value != current_matrix[row]);
  106. }
  107. return matrix_has_changed;
  108. }
  109. void matrix_set_row_status(uint8_t row) {
  110. #if defined(RIGHT_HALF)
  111. uint8_t txdata[3];
  112. //Set the remote row on port A
  113. txdata[0] = (GPIOA);
  114. txdata[1] = ( 0xFF & ~(1<<row) );
  115. i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)txdata, 2, I2C_TIMEOUT);
  116. #endif
  117. //Set the local row on port B
  118. DDRB = (1 << row);
  119. PORTB = ~(1 << row);
  120. }