spi_master.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2020 Nick Brassel (tzarc)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include "spi_master.h"
  17. #include "quantum.h"
  18. #include "timer.h"
  19. static pin_t currentSlavePin = NO_PIN;
  20. static SPIConfig spiConfig = {false, NULL, 0, 0, 0, 0};
  21. __attribute__((weak)) void spi_init(void) {
  22. static bool is_initialised = false;
  23. if (!is_initialised) {
  24. is_initialised = true;
  25. // Try releasing special pins for a short time
  26. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_INPUT);
  27. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_INPUT);
  28. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_INPUT);
  29. chThdSleepMilliseconds(10);
  30. #if defined(USE_GPIOV1)
  31. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  32. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  33. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  34. #else
  35. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_ALTERNATE(SPI_SCK_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
  36. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
  37. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
  38. #endif
  39. }
  40. }
  41. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
  42. if (currentSlavePin != NO_PIN || slavePin == NO_PIN) {
  43. return false;
  44. }
  45. uint16_t roundedDivisor = 2;
  46. while (roundedDivisor < divisor) {
  47. roundedDivisor <<= 1;
  48. }
  49. if (roundedDivisor < 2 || roundedDivisor > 256) {
  50. return false;
  51. }
  52. spiConfig.cr1 = 0;
  53. if (lsbFirst) {
  54. spiConfig.cr1 |= SPI_CR1_LSBFIRST;
  55. }
  56. switch (mode) {
  57. case 0:
  58. break;
  59. case 1:
  60. spiConfig.cr1 |= SPI_CR1_CPHA;
  61. break;
  62. case 2:
  63. spiConfig.cr1 |= SPI_CR1_CPOL;
  64. break;
  65. case 3:
  66. spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;
  67. break;
  68. }
  69. switch (roundedDivisor) {
  70. case 2:
  71. break;
  72. case 4:
  73. spiConfig.cr1 |= SPI_CR1_BR_0;
  74. break;
  75. case 8:
  76. spiConfig.cr1 |= SPI_CR1_BR_1;
  77. break;
  78. case 16:
  79. spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
  80. break;
  81. case 32:
  82. spiConfig.cr1 |= SPI_CR1_BR_2;
  83. break;
  84. case 64:
  85. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
  86. break;
  87. case 128:
  88. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
  89. break;
  90. case 256:
  91. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
  92. break;
  93. }
  94. currentSlavePin = slavePin;
  95. spiConfig.ssport = PAL_PORT(slavePin);
  96. spiConfig.sspad = PAL_PAD(slavePin);
  97. setPinOutput(slavePin);
  98. spiStart(&SPI_DRIVER, &spiConfig);
  99. spiSelect(&SPI_DRIVER);
  100. return true;
  101. }
  102. spi_status_t spi_write(uint8_t data) {
  103. uint8_t rxData;
  104. spiExchange(&SPI_DRIVER, 1, &data, &rxData);
  105. return rxData;
  106. }
  107. spi_status_t spi_read(void) {
  108. uint8_t data = 0;
  109. spiReceive(&SPI_DRIVER, 1, &data);
  110. return data;
  111. }
  112. spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
  113. spiSend(&SPI_DRIVER, length, data);
  114. return SPI_STATUS_SUCCESS;
  115. }
  116. spi_status_t spi_receive(uint8_t *data, uint16_t length) {
  117. spiReceive(&SPI_DRIVER, length, data);
  118. return SPI_STATUS_SUCCESS;
  119. }
  120. void spi_stop(void) {
  121. if (currentSlavePin != NO_PIN) {
  122. spiUnselect(&SPI_DRIVER);
  123. spiStop(&SPI_DRIVER);
  124. currentSlavePin = NO_PIN;
  125. }
  126. }