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 3.2KB

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