logo

qmk_firmware

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

tca6424.c (3077B)


  1. /**
  2. * @file tca6424.c
  3. * @author astro
  4. * @brief driver for the tca6424
  5. *
  6. * Copyright 2020 astro <yuleiz@gmail.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "tca6424.h"
  22. #include "i2c_master.h"
  23. #define TCA6424_INPUT_PORT0 0x0
  24. #define TCA6424_INPUT_PORT1 0x01
  25. #define TCA6424_INPUT_PORT2 0x02
  26. #define TCA6424_OUTPUT_PORT0 0x04
  27. #define TCA6424_OUTPUT_PORT1 0x05
  28. #define TCA6424_OUTPUT_PORT2 0x06
  29. #define TCA6424_POLARITY_PORT0 0x08
  30. #define TCA6424_POLARITY_PORT1 0x09
  31. #define TCA6424_POLARITY_PORT2 0x0A
  32. #define TCA6424_CONF_PORT0 0x0C
  33. #define TCA6424_CONF_PORT1 0x0D
  34. #define TCA6424_CONF_PORT2 0x0E
  35. #define TIMEOUT 100
  36. void tca6424_init(void)
  37. {
  38. i2c_init();
  39. }
  40. static void write_port(uint8_t p, uint8_t d)
  41. {
  42. i2c_write_register(TCA6424_ADDR, p, &d, 1, TIMEOUT);
  43. }
  44. static uint8_t read_port(uint8_t port)
  45. {
  46. uint8_t data = 0;
  47. i2c_read_register(TCA6424_ADDR, port, &data, 1, TIMEOUT);
  48. return data;
  49. }
  50. void tca6424_write_config(TCA6424_PORT port, uint8_t data)
  51. {
  52. switch(port) {
  53. case TCA6424_PORT0:
  54. write_port(TCA6424_CONF_PORT0, data);
  55. break;
  56. case TCA6424_PORT1:
  57. write_port(TCA6424_CONF_PORT1, data);
  58. break;
  59. case TCA6424_PORT2:
  60. write_port(TCA6424_CONF_PORT2, data);
  61. break;
  62. }
  63. }
  64. void tca6424_write_polarity(TCA6424_PORT port, uint8_t data)
  65. {
  66. switch(port) {
  67. case TCA6424_PORT0:
  68. write_port(TCA6424_POLARITY_PORT0, data);
  69. break;
  70. case TCA6424_PORT1:
  71. write_port(TCA6424_POLARITY_PORT1, data);
  72. break;
  73. case TCA6424_PORT2:
  74. write_port(TCA6424_POLARITY_PORT2, data);
  75. break;
  76. }
  77. }
  78. void tca6424_write_port(TCA6424_PORT port, uint8_t data)
  79. {
  80. switch(port) {
  81. case TCA6424_PORT0:
  82. write_port(TCA6424_OUTPUT_PORT0, data);
  83. break;
  84. case TCA6424_PORT1:
  85. write_port(TCA6424_OUTPUT_PORT1, data);
  86. break;
  87. case TCA6424_PORT2:
  88. write_port(TCA6424_OUTPUT_PORT2, data);
  89. break;
  90. }
  91. }
  92. uint8_t tca6424_read_port(TCA6424_PORT port)
  93. {
  94. switch(port) {
  95. case TCA6424_PORT0:
  96. return read_port(TCA6424_INPUT_PORT0);
  97. case TCA6424_PORT1:
  98. return read_port(TCA6424_INPUT_PORT1);
  99. case TCA6424_PORT2:
  100. return read_port(TCA6424_INPUT_PORT2);
  101. }
  102. return 0;
  103. }