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.

167 lines
6.4 KiB

  1. # SPI Master Driver :id=spi-master-driver
  2. The SPI Master drivers used in QMK have a set of common functions to allow portability between MCUs.
  3. ## Usage :id=usage
  4. In most cases, the SPI Master driver code is automatically included if you are using a feature or driver which requires it, such as [OLED](feature_oled_driver.md).
  5. However, if you need to use the driver standalone, add the following to your `rules.mk`:
  6. ```make
  7. SPI_DRIVER_REQUIRED = yes
  8. ```
  9. You can then call the SPI API by including `spi_master.h` in your code.
  10. ## AVR Configuration :id=avr-configuration
  11. No special setup is required - just connect the `SS`, `SCK`, `MOSI` and `MISO` pins of your SPI devices to the matching pins on the MCU:
  12. |MCU |`SS`|`SCK`|`MOSI`|`MISO`|
  13. |-----------------|----|-----|------|------|
  14. |ATmega16/32U2/4 |`B0`|`B1` |`B2` |`B3` |
  15. |AT90USB64/128/162|`B0`|`B1` |`B2` |`B3` |
  16. |ATmega32A |`B4`|`B7` |`B5` |`B6` |
  17. |ATmega328/P |`B2`|`B5` |`B3` |`B4` |
  18. You may use more than one slave select pin, not just the `SS` pin. This is useful when you have multiple devices connected and need to communicate with them individually.
  19. `SPI_SS_PIN` can be passed to `spi_start()` to refer to `SS`.
  20. ## ChibiOS/ARM Configuration :id=arm-configuration
  21. You'll need to determine which pins can be used for SPI -- as an example, STM32 parts generally have multiple SPI peripherals, labeled SPI1, SPI2, SPI3 etc.
  22. To enable SPI, modify your board's `halconf.h` to enable SPI:
  23. ```c
  24. #define HAL_USE_SPI TRUE
  25. #define SPI_USE_WAIT TRUE
  26. #define SPI_SELECT_MODE SPI_SELECT_MODE_PAD
  27. ```
  28. Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
  29. ```c
  30. #undef STM32_SPI_USE_SPI2
  31. #define STM32_SPI_USE_SPI2 TRUE
  32. ```
  33. Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.
  34. |`config.h` Override|Description |Default|
  35. |-------------------|-------------------------------------------------------------|-------|
  36. |`SPI_DRIVER` |SPI peripheral to use - SPI1 -> `SPID1`, SPI2 -> `SPID2` etc.|`SPID2`|
  37. |`SPI_SCK_PIN` |The pin to use for SCK |`B13` |
  38. |`SPI_SCK_PAL_MODE` |The alternate function mode for SCK |`5` |
  39. |`SPI_MOSI_PIN` |The pin to use for MOSI |`B15` |
  40. |`SPI_MOSI_PAL_MODE`|The alternate function mode for MOSI |`5` |
  41. |`SPI_MISO_PIN` |The pin to use for MISO |`B14` |
  42. |`SPI_MISO_PAL_MODE`|The alternate function mode for MISO |`5` |
  43. As per the AVR configuration, you may choose any other standard GPIO as a slave select pin, which should be supplied to `spi_start()`.
  44. If a complete SPI interface is not required, then the following can be done to disable certain SPI pins, so they don't occupy a GPIO unnecessarily:
  45. - in `config.h`: `#define SPI_MISO_PIN NO_PIN`
  46. - in `config.h`: `#define SPI_MOSI_PIN NO_PIN`
  47. - in `mcuconf.h`: `#define SPI_SELECT_MODE SPI_SELECT_MODE_NONE`, in this case the `slavePin` argument passed to `spi_start()` may be `NO_PIN` if the slave select pin is not used.
  48. ## API :id=api
  49. ### `void spi_init(void)` :id=api-spi-init
  50. Initialize the SPI driver. This function must be called only once, before any of the below functions can be called.
  51. ---
  52. ### `bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor)` :id=api-spi-start
  53. Start an SPI transaction.
  54. #### Arguments :id=api-spi-start-arguments
  55. - `pin_t slavePin`
  56. The QMK pin to assert as the slave select pin, eg. `B4`.
  57. - `bool lsbFirst`
  58. Determines the endianness of the transmission. If `true`, the least significant bit of each byte is sent first.
  59. - `uint8_t mode`
  60. The SPI mode to use:
  61. |Mode|Clock Polarity |Clock Phase |
  62. |----|--------------------|-----------------------|
  63. |`0` |Leading edge rising |Sample on leading edge |
  64. |`1` |Leading edge rising |Sample on trailing edge|
  65. |`2` |Leading edge falling|Sample on leading edge |
  66. |`3` |Leading edge falling|Sample on trailing edge|
  67. - `uint16_t divisor`
  68. The SPI clock divisor, will be rounded up to the nearest power of two. This number can be calculated by dividing the MCU's clock speed by the desired SPI clock speed. For example, an MCU running at 8 MHz wanting to talk to an SPI device at 4 MHz would set the divisor to `2`.
  69. #### Return Value :id=api-spi-start-return
  70. `false` if the supplied parameters are invalid or the SPI peripheral is already in use, or `true`.
  71. ---
  72. ### `spi_status_t spi_write(uint8_t data)` :id=api-spi-write
  73. Write a byte to the selected SPI device.
  74. #### Arguments :id=api-spi-write-arguments
  75. - `uint8_t data`
  76. The byte to write.
  77. #### Return Value :id=api-spi-write-return
  78. `SPI_STATUS_TIMEOUT` if the timeout period elapses, or `SPI_STATUS_SUCCESS`.
  79. ---
  80. ### `spi_status_t spi_read(void)` :id=api-spi-read
  81. Read a byte from the selected SPI device.
  82. #### Return Value :id=api-spi-read-return
  83. `SPI_STATUS_TIMEOUT` if the timeout period elapses, or the byte read from the device.
  84. ---
  85. ### `spi_status_t spi_transmit(const uint8_t *data, uint16_t length)` :id=api-spi-transmit
  86. Send multiple bytes to the selected SPI device.
  87. #### Arguments :id=api-spi-transmit-arguments
  88. - `const uint8_t *data`
  89. A pointer to the data to write from.
  90. - `uint16_t length`
  91. The number of bytes to write. Take care not to overrun the length of `data`.
  92. #### Return Value :id=api-spi-transmit-return
  93. `SPI_STATUS_TIMEOUT` if the timeout period elapses, `SPI_STATUS_ERROR` if some other error occurs, otherwise `SPI_STATUS_SUCCESS`.
  94. ---
  95. ### `spi_status_t spi_receive(uint8_t *data, uint16_t length)` :id=api-spi-receive
  96. Receive multiple bytes from the selected SPI device.
  97. #### Arguments :id=api-spi-receive-arguments
  98. - `uint8_t *data`
  99. A pointer to the buffer to read into.
  100. - `uint16_t length`
  101. The number of bytes to read. Take care not to overrun the length of `data`.
  102. #### Return Value :id=api-spi-receive-return
  103. `SPI_STATUS_TIMEOUT` if the timeout period elapses, `SPI_STATUS_ERROR` if some other error occurs, otherwise `SPI_STATUS_SUCCESS`.
  104. ---
  105. ### `void spi_stop(void)` :id=api-spi-stop
  106. End the current SPI transaction. This will deassert the slave select pin and reset the endianness, mode and divisor configured by `spi_start()`.