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.

63 lines
2.0 KiB

  1. /*
  2. * Copyright 2020 Richard Titmuss (richard.titmuss@gmail.com)
  3. * Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include QMK_KEYBOARD_H
  19. #include "i2c_master.h"
  20. #include "mcp23018.h"
  21. #define MCP23018_ADDR 0b0100000
  22. #define MCP23018_TIMEOUT 100
  23. static i2c_status_t mcp23018_status = I2C_STATUS_ERROR;
  24. void msp23018_init(void) {
  25. mcp23018_status = I2C_STATUS_SUCCESS;
  26. // Set pin direction
  27. uint8_t iodir[] = {0b00001111, 0b11111111};
  28. mcp23018_writeReg(IODIRA, iodir, 2);
  29. // Set pull-up
  30. uint8_t gppu[] = {0b00001111, 0b11111000};
  31. mcp23018_writeReg(GPPUA, gppu, 2);
  32. // LEDs output high
  33. uint8_t gpio[] = {0b00000000, 0b00000111};
  34. mcp23018_writeReg(GPIOA, gpio, 2);
  35. }
  36. bool mcp23018_reset_required(void) { return mcp23018_status != I2C_STATUS_SUCCESS; }
  37. i2c_status_t mcp23018_writeReg(uint8_t regaddr, const uint8_t* data, uint16_t length) {
  38. if (mcp23018_status) {
  39. return mcp23018_status;
  40. }
  41. mcp23018_status = i2c_writeReg((MCP23018_ADDR << 1), regaddr, data, length, MCP23018_TIMEOUT);
  42. return mcp23018_status;
  43. }
  44. i2c_status_t mcp23018_readReg(uint8_t regaddr, uint8_t* data, uint16_t length) {
  45. if (mcp23018_status) {
  46. return mcp23018_status;
  47. }
  48. mcp23018_status = i2c_readReg((MCP23018_ADDR << 1), regaddr, data, length, MCP23018_TIMEOUT);
  49. return mcp23018_status;
  50. }