spi_master.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "timer.h"
  18. static pin_t currentSlavePin = NO_PIN;
  19. #if defined(K20x) || defined(KL2x)
  20. static SPIConfig spiConfig = {NULL, 0, 0, 0};
  21. #else
  22. static SPIConfig spiConfig = {false, NULL, 0, 0, 0, 0};
  23. #endif
  24. __attribute__((weak)) void spi_init(void) {
  25. static bool is_initialised = false;
  26. if (!is_initialised) {
  27. is_initialised = true;
  28. // Try releasing special pins for a short time
  29. setPinInput(SPI_SCK_PIN);
  30. setPinInput(SPI_MOSI_PIN);
  31. setPinInput(SPI_MISO_PIN);
  32. chThdSleepMilliseconds(10);
  33. #if defined(USE_GPIOV1)
  34. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), SPI_SCK_PAL_MODE);
  35. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_PAL_MODE);
  36. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_PAL_MODE);
  37. #else
  38. 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);
  39. 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);
  40. 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);
  41. #endif
  42. }
  43. }
  44. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
  45. if (currentSlavePin != NO_PIN || slavePin == NO_PIN) {
  46. return false;
  47. }
  48. uint16_t roundedDivisor = 2;
  49. while (roundedDivisor < divisor) {
  50. roundedDivisor <<= 1;
  51. }
  52. if (roundedDivisor < 2 || roundedDivisor > 256) {
  53. return false;
  54. }
  55. #if defined(K20x) || defined(KL2x)
  56. spiConfig.tar0 = SPIx_CTARn_FMSZ(7) | SPIx_CTARn_ASC(1);
  57. if (lsbFirst) {
  58. spiConfig.tar0 |= SPIx_CTARn_LSBFE;
  59. }
  60. switch (mode) {
  61. case 0:
  62. break;
  63. case 1:
  64. spiConfig.tar0 |= SPIx_CTARn_CPHA;
  65. break;
  66. case 2:
  67. spiConfig.tar0 |= SPIx_CTARn_CPOL;
  68. break;
  69. case 3:
  70. spiConfig.tar0 |= SPIx_CTARn_CPHA | SPIx_CTARn_CPOL;
  71. break;
  72. }
  73. switch (roundedDivisor) {
  74. case 2:
  75. spiConfig.tar0 |= SPIx_CTARn_BR(0);
  76. break;
  77. case 4:
  78. spiConfig.tar0 |= SPIx_CTARn_BR(1);
  79. break;
  80. case 8:
  81. spiConfig.tar0 |= SPIx_CTARn_BR(3);
  82. break;
  83. case 16:
  84. spiConfig.tar0 |= SPIx_CTARn_BR(4);
  85. break;
  86. case 32:
  87. spiConfig.tar0 |= SPIx_CTARn_BR(5);
  88. break;
  89. case 64:
  90. spiConfig.tar0 |= SPIx_CTARn_BR(6);
  91. break;
  92. case 128:
  93. spiConfig.tar0 |= SPIx_CTARn_BR(7);
  94. break;
  95. case 256:
  96. spiConfig.tar0 |= SPIx_CTARn_BR(8);
  97. break;
  98. }
  99. #else
  100. spiConfig.cr1 = 0;
  101. if (lsbFirst) {
  102. spiConfig.cr1 |= SPI_CR1_LSBFIRST;
  103. }
  104. switch (mode) {
  105. case 0:
  106. break;
  107. case 1:
  108. spiConfig.cr1 |= SPI_CR1_CPHA;
  109. break;
  110. case 2:
  111. spiConfig.cr1 |= SPI_CR1_CPOL;
  112. break;
  113. case 3:
  114. spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;
  115. break;
  116. }
  117. switch (roundedDivisor) {
  118. case 2:
  119. break;
  120. case 4:
  121. spiConfig.cr1 |= SPI_CR1_BR_0;
  122. break;
  123. case 8:
  124. spiConfig.cr1 |= SPI_CR1_BR_1;
  125. break;
  126. case 16:
  127. spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
  128. break;
  129. case 32:
  130. spiConfig.cr1 |= SPI_CR1_BR_2;
  131. break;
  132. case 64:
  133. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
  134. break;
  135. case 128:
  136. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
  137. break;
  138. case 256:
  139. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
  140. break;
  141. }
  142. #endif
  143. currentSlavePin = slavePin;
  144. spiConfig.ssport = PAL_PORT(slavePin);
  145. spiConfig.sspad = PAL_PAD(slavePin);
  146. setPinOutput(slavePin);
  147. spiStart(&SPI_DRIVER, &spiConfig);
  148. spiSelect(&SPI_DRIVER);
  149. return true;
  150. }
  151. spi_status_t spi_write(uint8_t data) {
  152. uint8_t rxData;
  153. spiExchange(&SPI_DRIVER, 1, &data, &rxData);
  154. return rxData;
  155. }
  156. spi_status_t spi_read(void) {
  157. uint8_t data = 0;
  158. spiReceive(&SPI_DRIVER, 1, &data);
  159. return data;
  160. }
  161. spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
  162. spiSend(&SPI_DRIVER, length, data);
  163. return SPI_STATUS_SUCCESS;
  164. }
  165. spi_status_t spi_receive(uint8_t *data, uint16_t length) {
  166. spiReceive(&SPI_DRIVER, length, data);
  167. return SPI_STATUS_SUCCESS;
  168. }
  169. void spi_stop(void) {
  170. if (currentSlavePin != NO_PIN) {
  171. spiUnselect(&SPI_DRIVER);
  172. spiStop(&SPI_DRIVER);
  173. currentSlavePin = NO_PIN;
  174. }
  175. }