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.

107 lines
3.1 KiB

  1. /* Copyright 2018 Mike Roberts
  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 <stdbool.h>
  17. #include "action.h"
  18. #include <LUFA/Drivers/Peripheral/TWI.h>
  19. #include <LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c>
  20. #include "mcp23017.h"
  21. #include "debug.h"
  22. #include "wait.h"
  23. uint8_t bit_for_pin(uint8_t pin);
  24. uint8_t expander_write(uint8_t reg, uint8_t data);
  25. uint8_t expander_read(uint8_t reg, uint8_t *data);
  26. void expander_config(void);
  27. static const char *twi_err_str(uint8_t res) {
  28. switch (res) {
  29. case TWI_ERROR_NoError:
  30. return "OK";
  31. case TWI_ERROR_BusFault:
  32. return "BUSFAULT";
  33. case TWI_ERROR_BusCaptureTimeout:
  34. return "BUSTIMEOUT";
  35. case TWI_ERROR_SlaveResponseTimeout:
  36. return "SLAVETIMEOUT";
  37. case TWI_ERROR_SlaveNotReady:
  38. return "SLAVENOTREADY";
  39. case TWI_ERROR_SlaveNAK:
  40. return "SLAVENAK";
  41. default:
  42. return "UNKNOWN";
  43. }
  44. }
  45. void expander_init(void) {
  46. TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 400000));
  47. }
  48. // set IN and HI
  49. void expander_unselect_all() {
  50. expander_write(EXPANDER_REG_IODIRA, 0xff);
  51. expander_write(EXPANDER_REG_IODIRB, 0xff);
  52. expander_write(EXPANDER_REG_OLATA, 0xff);
  53. expander_write(EXPANDER_REG_OLATB, 0xff);
  54. wait_us(EXPANDER_PAUSE);
  55. }
  56. // set OUT and LOW
  57. void expander_select(uint8_t pin) {
  58. const uint8_t mask = 0xff & ~(1 << bit_for_pin(pin));
  59. if (pin < 8) {
  60. expander_write(EXPANDER_REG_IODIRA, mask);
  61. expander_write(EXPANDER_REG_OLATA, mask);
  62. } else {
  63. expander_write(EXPANDER_REG_IODIRB, mask);
  64. expander_write(EXPANDER_REG_OLATB, mask);
  65. }
  66. wait_us(EXPANDER_PAUSE);
  67. }
  68. void expander_config() {
  69. // set everything to input
  70. expander_write(EXPANDER_REG_IODIRA, 0xff);
  71. expander_write(EXPANDER_REG_IODIRB, 0xff);
  72. // turn on pull-ups
  73. expander_write(EXPANDER_REG_GPPUA, 0xff);
  74. expander_write(EXPANDER_REG_GPPUB, 0xff);
  75. // disable interrupts
  76. expander_write(EXPANDER_REG_GPINTENA, 0x0);
  77. expander_write(EXPANDER_REG_GPINTENB, 0x0);
  78. // polarity
  79. expander_write(EXPANDER_REG_IPOLA, 0x0);
  80. expander_write(EXPANDER_REG_IPOLB, 0x0);
  81. }
  82. uint8_t bit_for_pin(uint8_t pin) {
  83. return pin % 8;
  84. }
  85. uint8_t expander_write(uint8_t reg, unsigned char val) {
  86. uint8_t addr = reg;
  87. uint8_t result = TWI_WritePacket(EXPANDER_ADDR << 1, I2C_TIMEOUT, &addr, sizeof(addr), &val, sizeof(val));
  88. if (result) {
  89. xprintf("mcp: set_register %d = %d failed: %s\n", reg, val, twi_err_str(result));
  90. }
  91. return result == 0;
  92. }