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.

PortMCP23S17.cpp 2.8KB

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