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.

95 lines
2.6 KiB

  1. /* Copyright 2019
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "i2c_master.h"
  17. #include "pca9555.h"
  18. #include "debug.h"
  19. #define SLAVE_TO_ADDR(n) (n << 1)
  20. #define TIMEOUT 100
  21. enum {
  22. CMD_INPUT_0 = 0,
  23. CMD_INPUT_1,
  24. CMD_OUTPUT_0,
  25. CMD_OUTPUT_1,
  26. CMD_INVERSION_0,
  27. CMD_INVERSION_1,
  28. CMD_CONFIG_0,
  29. CMD_CONFIG_1,
  30. };
  31. void pca9555_init(uint8_t slave_addr) {
  32. static uint8_t s_init = 0;
  33. if (!s_init) {
  34. i2c_init();
  35. s_init = 1;
  36. }
  37. // TODO: could check device connected
  38. // i2c_start(SLAVE_TO_ADDR(slave) | I2C_WRITE);
  39. // i2c_stop();
  40. }
  41. void pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
  42. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  43. uint8_t cmd = port ? CMD_CONFIG_1 : CMD_CONFIG_0;
  44. i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
  45. if (ret != I2C_STATUS_SUCCESS) {
  46. print("pca9555_set_config::FAILED\n");
  47. }
  48. }
  49. void pca9555_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) {
  50. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  51. uint8_t cmd = port ? CMD_OUTPUT_1 : CMD_OUTPUT_0;
  52. i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
  53. if (ret != I2C_STATUS_SUCCESS) {
  54. print("pca9555_set_output::FAILED\n");
  55. }
  56. }
  57. uint8_t pca9555_readPins(uint8_t slave_addr, uint8_t port) {
  58. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  59. uint8_t cmd = port ? CMD_INPUT_1 : CMD_INPUT_0;
  60. uint8_t data = 0;
  61. i2c_status_t ret = i2c_readReg(addr, cmd, &data, sizeof(data), TIMEOUT);
  62. if (ret != I2C_STATUS_SUCCESS) {
  63. print("pca9555_readPins::FAILED\n");
  64. }
  65. return data;
  66. }
  67. uint16_t pca9555_readAllPins(uint8_t slave_addr) {
  68. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  69. typedef union {
  70. uint8_t u8[2];
  71. uint16_t u16;
  72. } data16;
  73. data16 data;
  74. i2c_status_t ret = i2c_readReg(addr, CMD_INPUT_0, &data.u8[0], sizeof(data), TIMEOUT);
  75. if (ret != I2C_STATUS_SUCCESS) {
  76. print("pca9555_readAllPins::FAILED\n");
  77. }
  78. return data.u16;
  79. }