sp64.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright 2019 Neil Kettle
  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 "sp64.h"
  17. #ifdef RIGHT_HALF
  18. bool i2c_initialized = 0;
  19. i2c_status_t mcp23018_status = I2C_STATUS_ERROR;
  20. uint8_t init_mcp23018(void)
  21. {
  22. uint8_t data[3];
  23. mcp23018_status = I2C_STATUS_ERROR;
  24. // I2C subsystem
  25. if (i2c_initialized == 0) {
  26. i2c_init(); // on pins D(1,0)
  27. i2c_initialized = true;
  28. _delay_ms(1000);
  29. }
  30. // set pin direction
  31. // - unused : input : 1
  32. // - input : input : 1
  33. // - driving : output :
  34. data[0] = IODIRA;
  35. data[1] = 0b00000000;
  36. data[2] = 0b11111111;
  37. mcp23018_status = i2c_transmit(I2C_ADDR_WRITE, (uint8_t *)data, 3, MCP23018_I2C_TIMEOUT);
  38. if (mcp23018_status != I2C_STATUS_SUCCESS)
  39. goto out;
  40. // set pull-up
  41. // - unused : on : 1
  42. // - input : on : 1
  43. // - driving : off : 0
  44. data[0] = GPPUA;
  45. data[1] = 0b00000000;
  46. data[2] = 0b11111111;
  47. mcp23018_status = i2c_transmit(I2C_ADDR_WRITE, (uint8_t *)data, 3, MCP23018_I2C_TIMEOUT);
  48. if (mcp23018_status != I2C_STATUS_SUCCESS)
  49. goto out;
  50. // set logical value (doesn't matter on inputs)
  51. // - unused : hi-Z : 1
  52. // - input : hi-Z : 1
  53. // - driving : hi-Z : 1
  54. data[0] = OLATA;
  55. data[1] = 0b11111111;
  56. data[2] = 0b11111111;
  57. mcp23018_status = i2c_transmit(I2C_ADDR_WRITE, (uint8_t *)data, 3, MCP23018_I2C_TIMEOUT);
  58. out:
  59. return (mcp23018_status);
  60. }
  61. #endif