logo

qmk_firmware

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

mcp23018.c (2002B)


  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 "i2c_master.h"
  19. #include "mcp23018.h"
  20. #define MCP23018_ADDR 0b0100000
  21. #define MCP23018_TIMEOUT 100
  22. static i2c_status_t mcp23018_status = I2C_STATUS_ERROR;
  23. void msp23018_init(void) {
  24. mcp23018_status = I2C_STATUS_SUCCESS;
  25. // Set pin direction
  26. uint8_t iodir[] = {0b00001111, 0b11111111};
  27. mcp23018_writeReg(IODIRA, iodir, 2);
  28. // Set pull-up
  29. uint8_t gppu[] = {0b00001111, 0b11111000};
  30. mcp23018_writeReg(GPPUA, gppu, 2);
  31. // LEDs output high
  32. uint8_t gpio[] = {0b00000000, 0b00000111};
  33. mcp23018_writeReg(GPIOA, gpio, 2);
  34. }
  35. bool mcp23018_reset_required(void) { return mcp23018_status != I2C_STATUS_SUCCESS; }
  36. i2c_status_t mcp23018_writeReg(uint8_t regaddr, const uint8_t* data, uint16_t length) {
  37. if (mcp23018_status) {
  38. return mcp23018_status;
  39. }
  40. mcp23018_status = i2c_write_register((MCP23018_ADDR << 1), regaddr, data, length, MCP23018_TIMEOUT);
  41. return mcp23018_status;
  42. }
  43. i2c_status_t mcp23018_readReg(uint8_t regaddr, uint8_t* data, uint16_t length) {
  44. if (mcp23018_status) {
  45. return mcp23018_status;
  46. }
  47. mcp23018_status = i2c_read_register((MCP23018_ADDR << 1), regaddr, data, length, MCP23018_TIMEOUT);
  48. return mcp23018_status;
  49. }