matrix.c 4.1 KB

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