mcp23018.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright 2020 zvecr<git@zvecr.com>
  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 "mcp23018.h"
  17. #include "i2c_master.h"
  18. #include "wait.h"
  19. #include "debug.h"
  20. #define SLAVE_TO_ADDR(n) (n << 1)
  21. #define TIMEOUT 100
  22. enum {
  23. CMD_IODIRA = 0x00, // i/o direction register
  24. CMD_IODIRB = 0x01,
  25. CMD_GPPUA = 0x0C, // GPIO pull-up resistor register
  26. CMD_GPPUB = 0x0D,
  27. CMD_GPIOA = 0x12, // general purpose i/o port register (write modifies OLAT)
  28. CMD_GPIOB = 0x13,
  29. };
  30. void mcp23018_init(uint8_t addr) {
  31. static uint8_t s_init = 0;
  32. if (!s_init) {
  33. i2c_init();
  34. wait_ms(1000);
  35. s_init = 1;
  36. }
  37. }
  38. bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
  39. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  40. uint8_t cmdDirection = port ? CMD_IODIRB : CMD_IODIRA;
  41. uint8_t cmdPullup = port ? CMD_GPPUB : CMD_GPPUA;
  42. i2c_status_t ret = i2c_writeReg(addr, cmdDirection, &conf, sizeof(conf), TIMEOUT);
  43. if (ret != I2C_STATUS_SUCCESS) {
  44. dprintf("mcp23018_set_config::directionFAILED::%u\n", ret);
  45. return false;
  46. }
  47. ret = i2c_writeReg(addr, cmdPullup, &conf, sizeof(conf), TIMEOUT);
  48. if (ret != I2C_STATUS_SUCCESS) {
  49. dprintf("mcp23018_set_config::pullupFAILED::%u\n", ret);
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool mcp23018_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) {
  55. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  56. uint8_t cmd = port ? CMD_GPIOB : CMD_GPIOA;
  57. i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
  58. if (ret != I2C_STATUS_SUCCESS) {
  59. dprintf("mcp23018_set_output::FAILED::%u\n", ret);
  60. return false;
  61. }
  62. return true;
  63. }
  64. bool mcp23018_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB) {
  65. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  66. uint8_t conf[2] = {confA, confB};
  67. i2c_status_t ret = i2c_writeReg(addr, CMD_GPIOA, &conf[0], sizeof(conf), TIMEOUT);
  68. if (ret != I2C_STATUS_SUCCESS) {
  69. dprintf("mcp23018_set_output::FAILED::%u\n", ret);
  70. return false;
  71. }
  72. return true;
  73. }
  74. bool mcp23018_readPins(uint8_t slave_addr, uint8_t port, uint8_t* out) {
  75. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  76. uint8_t cmd = port ? CMD_GPIOB : CMD_GPIOA;
  77. i2c_status_t ret = i2c_readReg(addr, cmd, out, sizeof(uint8_t), TIMEOUT);
  78. if (ret != I2C_STATUS_SUCCESS) {
  79. dprintf("mcp23018_readPins::FAILED::%u\n", ret);
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool mcp23018_readPins_all(uint8_t slave_addr, uint16_t* out) {
  85. uint8_t addr = SLAVE_TO_ADDR(slave_addr);
  86. typedef union {
  87. uint8_t u8[2];
  88. uint16_t u16;
  89. } data16;
  90. data16 data = {.u16 = 0};
  91. i2c_status_t ret = i2c_readReg(addr, CMD_GPIOA, &data.u8[0], sizeof(data), TIMEOUT);
  92. if (ret != I2C_STATUS_SUCCESS) {
  93. dprintf("mcp23018_readPins::FAILED::%u\n", ret);
  94. return false;
  95. }
  96. *out = data.u16;
  97. return true;
  98. }