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.

PortWrite_MCP23S17.cpp 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "PortWrite_MCP23S17.h"
  2. /* push() writes data to registerAddr.
  3. */
  4. void PortWrite_MCP23S17::push(const uint8_t registerAddr, const uint8_t data)
  5. {
  6. digitalWrite(SS, LOW); //enable Slave Select
  7. SPI.transfer(port.DEVICE_ADDR << 1); //write command
  8. SPI.transfer(registerAddr); //register address to write data to
  9. SPI.transfer(data); //write the data
  10. digitalWrite(SS, HIGH); //disable Slave Select
  11. }
  12. /* begin() is called from Scanner_IOE::begin().
  13. Initiates SPI bus and configures write pins to output.
  14. MCP23S17 SPI interface is 10 MHz max.
  15. */
  16. void PortWrite_MCP23S17::begin()
  17. {
  18. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  19. digitalWrite(SS, HIGH); //disable Slave Select
  20. SPI.begin();
  21. SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz
  22. //SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
  23. push(port.num, 0); //configure port direction (port.num) to output (0)
  24. }
  25. /* write() sets pin output to logicLevel.
  26. pin is bitwise, where pin being set is 1.
  27. logicLevel is HIGH or LOW.
  28. write() does not overwrite the other pins.
  29. */
  30. void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel)
  31. {
  32. if (logicLevel == LOW)
  33. {
  34. port.outputVal &= ~pin; //set pin output to low
  35. }
  36. else
  37. {
  38. port.outputVal |= pin; //set pin output to high
  39. }
  40. push(port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal
  41. }