keybrd library is an open source library for creating custom-keyboard firmware.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Port_MCP23S17.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "Port_MCP23S17.h"
  2. /* transfer() writes data to registerAddr, reads portSate from registerAddr, and returns portState.
  3. MCP23S17 SPI interface is 10 MHz max.
  4. The electrical limitation to bus speed is bus capacitance and the length of the wires involved.
  5. Longer wires require lower clock speeds.
  6. */
  7. uint8_t Port_MCP23S17::transfer(const uint8_t command, const uint8_t registerAddr,
  8. const uint8_t data)
  9. {
  10. uint8_t portState; //bit pattern
  11. SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
  12. digitalWrite(slaveSelect, LOW); //enable Slave Select
  13. SPI.transfer(command); //write or read command
  14. SPI.transfer(registerAddr); //register address to write data to
  15. portState = SPI.transfer(data); //write data, read portState
  16. digitalWrite(slaveSelect, HIGH); //disable Slave Select
  17. SPI.endTransaction();
  18. return portState;
  19. }
  20. /* beginProtocol() is called from Scanner_IOE::begin(). Initiates SPI bus.
  21. */
  22. void Port_MCP23S17::beginProtocol()
  23. {
  24. pinMode(slaveSelect, OUTPUT); //configure controller's Slave Select pin to output
  25. digitalWrite(slaveSelect, HIGH); //disable Slave Select
  26. SPI.begin();
  27. }
  28. /* begin() is called from Scanner_IOE::begin().
  29. activeState is logic level of strobe on, HIGH or LOW
  30. configure IODIR and GPPU.
  31. */
  32. void Port_MCP23S17::begin(const uint8_t activeState)
  33. {
  34. uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
  35. if (activeState == LOW) //if active low
  36. {
  37. pullUp = readPins; //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  38. }
  39. else //if active high
  40. {
  41. pullUp = 0; //0=pull-up disabled (for external pull-down resistors)
  42. }
  43. transfer(deviceAddr << 1, portNum, readPins); //configure IODIR
  44. transfer(deviceAddr << 1, portNum + 0x0C, pullUp); //configure GPPU
  45. }
  46. /* writeLow() sets pin output LOW.
  47. pin is bit pattern, where pin being set is 1.
  48. */
  49. void Port_MCP23S17::writeLow(const uint8_t pin)
  50. {
  51. outputVal &= ~pin; //set pin output to low
  52. transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
  53. }
  54. /* writeHigh() sets pin output HIGH.
  55. pin is bit pattern, where pin being set is 1.
  56. */
  57. void Port_MCP23S17::writeHigh(const uint8_t pin)
  58. {
  59. outputVal |= pin; //set pin output to high
  60. transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
  61. }
  62. /* read() returns portState. Only portState pins with pull resistors are valid.
  63. */
  64. uint8_t Port_MCP23S17::read()
  65. {
  66. return transfer( (deviceAddr << 1) | 1, portNum + 0x12, 0); //read from GPIO
  67. }