spi_master.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright 2020
  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 <avr/io.h>
  17. #include "spi_master.h"
  18. #include "quantum.h"
  19. #include "timer.h"
  20. #if defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
  21. # define SPI_SCK_PIN B1
  22. # define SPI_MOSI_PIN B2
  23. # define SPI_MISO_PIN B3
  24. #elif defined(__AVR_ATmega32A__)
  25. # define SPI_SCK_PIN B7
  26. # define SPI_MOSI_PIN B5
  27. # define SPI_MISO_PIN B6
  28. #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
  29. # define SPI_SCK_PIN B5
  30. # define SPI_MOSI_PIN B3
  31. # define SPI_MISO_PIN B4
  32. #endif
  33. #ifndef SPI_TIMEOUT
  34. # define SPI_TIMEOUT 100
  35. #endif
  36. static pin_t currentSlavePin = NO_PIN;
  37. static uint8_t currentSlaveConfig = 0;
  38. static bool currentSlave2X = false;
  39. void spi_init(void) {
  40. writePinHigh(SPI_SS_PIN);
  41. setPinOutput(SPI_SCK_PIN);
  42. setPinOutput(SPI_MOSI_PIN);
  43. setPinInput(SPI_MISO_PIN);
  44. SPCR = (_BV(SPE) | _BV(MSTR));
  45. }
  46. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
  47. if (currentSlavePin != NO_PIN || slavePin == NO_PIN) {
  48. return false;
  49. }
  50. currentSlaveConfig = 0;
  51. if (lsbFirst) {
  52. currentSlaveConfig |= _BV(DORD);
  53. }
  54. switch (mode) {
  55. case 1:
  56. currentSlaveConfig |= _BV(CPHA);
  57. break;
  58. case 2:
  59. currentSlaveConfig |= _BV(CPOL);
  60. break;
  61. case 3:
  62. currentSlaveConfig |= (_BV(CPOL) | _BV(CPHA));
  63. break;
  64. }
  65. uint16_t roundedDivisor = 1;
  66. while (roundedDivisor < divisor) {
  67. roundedDivisor <<= 1;
  68. }
  69. switch (roundedDivisor) {
  70. case 16:
  71. currentSlaveConfig |= _BV(SPR0);
  72. break;
  73. case 64:
  74. currentSlaveConfig |= _BV(SPR1);
  75. break;
  76. case 128:
  77. currentSlaveConfig |= (_BV(SPR1) | _BV(SPR0));
  78. break;
  79. case 2:
  80. currentSlave2X = true;
  81. break;
  82. case 8:
  83. currentSlave2X = true;
  84. currentSlaveConfig |= _BV(SPR0);
  85. break;
  86. case 32:
  87. currentSlave2X = true;
  88. currentSlaveConfig |= _BV(SPR1);
  89. break;
  90. }
  91. SPCR |= currentSlaveConfig;
  92. if (currentSlave2X) {
  93. SPSR |= _BV(SPI2X);
  94. }
  95. currentSlavePin = slavePin;
  96. setPinOutput(currentSlavePin);
  97. writePinLow(currentSlavePin);
  98. return true;
  99. }
  100. spi_status_t spi_write(uint8_t data) {
  101. SPDR = data;
  102. uint16_t timeout_timer = timer_read();
  103. while (!(SPSR & _BV(SPIF))) {
  104. if ((timer_read() - timeout_timer) >= SPI_TIMEOUT) {
  105. return SPI_STATUS_TIMEOUT;
  106. }
  107. }
  108. return SPDR;
  109. }
  110. spi_status_t spi_read() {
  111. SPDR = 0x00; // Dummy
  112. uint16_t timeout_timer = timer_read();
  113. while (!(SPSR & _BV(SPIF))) {
  114. if ((timer_read() - timeout_timer) >= SPI_TIMEOUT) {
  115. return SPI_STATUS_TIMEOUT;
  116. }
  117. }
  118. return SPDR;
  119. }
  120. spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
  121. spi_status_t status = SPI_STATUS_ERROR;
  122. for (uint16_t i = 0; i < length; i++) {
  123. status = spi_write(data[i]);
  124. }
  125. return status;
  126. }
  127. spi_status_t spi_receive(uint8_t *data, uint16_t length) {
  128. spi_status_t status = SPI_STATUS_ERROR;
  129. for (uint16_t i = 0; i < length; i++) {
  130. status = spi_read();
  131. if (status > 0) {
  132. data[i] = status;
  133. }
  134. }
  135. return (status < 0) ? status : SPI_STATUS_SUCCESS;
  136. }
  137. void spi_stop(void) {
  138. if (currentSlavePin != NO_PIN) {
  139. setPinOutput(currentSlavePin);
  140. writePinHigh(currentSlavePin);
  141. currentSlavePin = NO_PIN;
  142. SPSR &= ~(_BV(SPI2X));
  143. SPCR &= ~(currentSlaveConfig);
  144. currentSlaveConfig = 0;
  145. currentSlave2X = false;
  146. }
  147. }