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.

115 lines
3.0 KiB

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