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.

PortRead_MCP23S17.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "PortRead_MCP23S17.h"
  2. /*
  3. SPI bus is configured in PortWrite_MCP23S17::begin().
  4. */
  5. void PortRead_MCP23S17::begin(const uint8_t strobeOn)
  6. {
  7. uint8_t pullUp; //bitwise, 1 means internal pull-up resistor enabled
  8. if (strobeOn == LOW) //if active low
  9. {
  10. pullUp = readPins;
  11. }
  12. else
  13. {
  14. pullUp = 0;
  15. }
  16. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  17. digitalWrite(SS, HIGH); //disable Slave Select
  18. SPI.begin();
  19. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo is slow clock needed?
  20. digitalWrite(SS, LOW); //enable Slave Select
  21. SPI.transfer(port.ADDR << 1); //write command
  22. SPI.transfer(port.num); //configure IODIR
  23. SPI.transfer(readPins); //0=output (for LED), 1=input (for read)
  24. digitalWrite(SS, LOW); //enable Slave Select
  25. digitalWrite(SS, HIGH); //disable Slave Select
  26. SPI.transfer(port.ADDR << 1); //write command
  27. SPI.transfer(port.num + 0x0C); //configure GPPU
  28. SPI.transfer(pullUp); //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  29. digitalWrite(SS, HIGH); //disable Slave Select
  30. //SPI.endTransaction() is not called to release the SPI bus
  31. // because keyboard only has one SPI device.
  32. }
  33. /*
  34. read() returns portState.
  35. */
  36. uint8_t PortRead_MCP23S17::read()
  37. {
  38. uint8_t portState; //bit wise
  39. digitalWrite(SS, LOW); //enable Slave Select
  40. SPI.transfer(port.ADDR << 1 | 1); //read command
  41. SPI.transfer(port.num + 0x12); //GPIO register address to read data from
  42. portState = SPI.transfer(0); //save the data (0 is dummy data to send)
  43. digitalWrite(SS, HIGH); //disable Slave Select
  44. return portState;
  45. }