You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

expander.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2016 Kai Ryu <[email protected]>
  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 <stdbool.h>
  15. #include "action.h"
  16. #include "i2cmaster.h"
  17. #include "expander.h"
  18. #include "debug.h"
  19. static uint8_t expander_status = 0;
  20. static uint8_t expander_input = 0;
  21. void expander_config(void);
  22. uint8_t expander_write(uint8_t reg, uint8_t data);
  23. uint8_t expander_read(uint8_t reg, uint8_t *data);
  24. void expander_init(void)
  25. {
  26. i2c_init();
  27. expander_scan();
  28. }
  29. void expander_scan(void)
  30. {
  31. dprintf("expander status: %d ... ", expander_status);
  32. uint8_t ret = i2c_start(EXPANDER_ADDR | I2C_WRITE);
  33. if (ret == 0) {
  34. i2c_stop();
  35. if (expander_status == 0) {
  36. dprintf("attached\n");
  37. expander_status = 1;
  38. expander_config();
  39. clear_keyboard();
  40. }
  41. }
  42. else {
  43. if (expander_status == 1) {
  44. dprintf("detached\n");
  45. expander_status = 0;
  46. clear_keyboard();
  47. }
  48. }
  49. dprintf("%d\n", expander_status);
  50. }
  51. void expander_read_cols(void)
  52. {
  53. expander_read(EXPANDER_REG_GPIOA, &expander_input);
  54. }
  55. uint8_t expander_get_col(uint8_t col)
  56. {
  57. if (col > 4) {
  58. col++;
  59. }
  60. return expander_input & (1<<col) ? 1 : 0;
  61. }
  62. matrix_row_t expander_read_row(void)
  63. {
  64. expander_read_cols();
  65. /* make cols */
  66. matrix_row_t cols = 0;
  67. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  68. if (expander_get_col(col)) {
  69. cols |= (1UL << col);
  70. }
  71. }
  72. return cols;
  73. }
  74. void expander_unselect_rows(void)
  75. {
  76. expander_write(EXPANDER_REG_IODIRB, 0xFF);
  77. }
  78. void expander_select_row(uint8_t row)
  79. {
  80. expander_write(EXPANDER_REG_IODIRB, ~(1<<(row+1)));
  81. }
  82. void expander_config(void)
  83. {
  84. expander_write(EXPANDER_REG_IPOLA, 0xFF);
  85. expander_write(EXPANDER_REG_GPPUA, 0xFF);
  86. expander_write(EXPANDER_REG_IODIRB, 0xFF);
  87. }
  88. uint8_t expander_write(uint8_t reg, uint8_t data)
  89. {
  90. if (expander_status == 0) {
  91. return 0;
  92. }
  93. uint8_t ret;
  94. ret = i2c_start(EXPANDER_ADDR | I2C_WRITE);
  95. if (ret) goto stop;
  96. ret = i2c_write(reg);
  97. if (ret) goto stop;
  98. ret = i2c_write(data);
  99. stop:
  100. i2c_stop();
  101. return ret;
  102. }
  103. uint8_t expander_read(uint8_t reg, uint8_t *data)
  104. {
  105. if (expander_status == 0) {
  106. return 0;
  107. }
  108. uint8_t ret;
  109. ret = i2c_start(EXPANDER_ADDR | I2C_WRITE);
  110. if (ret) goto stop;
  111. ret = i2c_write(reg);
  112. if (ret) goto stop;
  113. ret = i2c_rep_start(EXPANDER_ADDR | I2C_READ);
  114. if (ret) goto stop;
  115. *data = i2c_readNak();
  116. stop:
  117. i2c_stop();
  118. return ret;
  119. }