spi_master.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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__)
  29. # define SPI_SCK_PIN B5
  30. # define SPI_MOSI_PIN B3
  31. # define SPI_MISO_PIN B4
  32. #endif
  33. static pin_t currentSlavePin = NO_PIN;
  34. static uint8_t currentSlaveConfig = 0;
  35. static bool currentSlave2X = false;
  36. void spi_init(void) {
  37. writePinHigh(SPI_SS_PIN);
  38. setPinOutput(SPI_SCK_PIN);
  39. setPinOutput(SPI_MOSI_PIN);
  40. setPinInput(SPI_MISO_PIN);
  41. SPCR = (_BV(SPE) | _BV(MSTR));
  42. }
  43. void spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint8_t divisor) {
  44. if (currentSlavePin == NO_PIN && slavePin != NO_PIN) {
  45. if (lsbFirst) {
  46. currentSlaveConfig |= _BV(DORD);
  47. }
  48. switch (mode) {
  49. case 1:
  50. currentSlaveConfig |= _BV(CPHA);
  51. break;
  52. case 2:
  53. currentSlaveConfig |= _BV(CPOL);
  54. break;
  55. case 3:
  56. currentSlaveConfig |= (_BV(CPOL) | _BV(CPHA));
  57. break;
  58. }
  59. uint8_t roundedDivisor = 1;
  60. while (roundedDivisor < divisor) {
  61. roundedDivisor <<= 1;
  62. }
  63. switch (roundedDivisor) {
  64. case 16:
  65. currentSlaveConfig |= _BV(SPR0);
  66. break;
  67. case 64:
  68. currentSlaveConfig |= _BV(SPR1);
  69. break;
  70. case 128:
  71. currentSlaveConfig |= (_BV(SPR1) | _BV(SPR0));
  72. break;
  73. case 2:
  74. currentSlave2X = true;
  75. break;
  76. case 8:
  77. currentSlave2X = true;
  78. currentSlaveConfig |= _BV(SPR0);
  79. break;
  80. case 32:
  81. currentSlave2X = true;
  82. currentSlaveConfig |= _BV(SPR1);
  83. break;
  84. }
  85. SPSR |= currentSlaveConfig;
  86. currentSlavePin = slavePin;
  87. setPinOutput(currentSlavePin);
  88. writePinLow(currentSlavePin);
  89. }
  90. }
  91. spi_status_t spi_write(uint8_t data, uint16_t timeout) {
  92. SPDR = data;
  93. uint16_t timeout_timer = timer_read();
  94. while (!(SPSR & _BV(SPIF))) {
  95. if ((timeout != SPI_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  96. return SPI_STATUS_TIMEOUT;
  97. }
  98. }
  99. return SPDR;
  100. }
  101. spi_status_t spi_read(uint16_t timeout) {
  102. SPDR = 0x00; // Dummy
  103. uint16_t timeout_timer = timer_read();
  104. while (!(SPSR & _BV(SPIF))) {
  105. if ((timeout != SPI_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  106. return SPI_STATUS_TIMEOUT;
  107. }
  108. }
  109. return SPDR;
  110. }
  111. spi_status_t spi_transmit(const uint8_t *data, uint16_t length, uint16_t timeout) {
  112. spi_status_t status = SPI_STATUS_ERROR;
  113. for (uint16_t i = 0; i < length; i++) {
  114. status = spi_write(data[i], timeout);
  115. }
  116. return status;
  117. }
  118. spi_status_t spi_receive(uint8_t *data, uint16_t length, uint16_t timeout) {
  119. spi_status_t status = SPI_STATUS_ERROR;
  120. for (uint16_t i = 0; i < length; i++) {
  121. status = spi_read(timeout);
  122. if (status > 0) {
  123. data[i] = status;
  124. }
  125. }
  126. return (status < 0) ? status : SPI_STATUS_SUCCESS;
  127. }
  128. void spi_stop(void) {
  129. if (currentSlavePin != NO_PIN) {
  130. setPinOutput(currentSlavePin);
  131. writePinHigh(currentSlavePin);
  132. currentSlavePin = NO_PIN;
  133. SPCR &= ~(currentSlaveConfig);
  134. currentSlaveConfig = 0;
  135. SPSR = 0;
  136. currentSlave2X = false;
  137. }
  138. }