SdSpiTeensy3.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 20011-2017 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "SdSpiDriver.h"
  26. #if defined(__arm__) && defined(CORE_TEENSY)
  27. // SPI definitions
  28. #include "kinetis.h"
  29. //------------------------------------------------------------------------------
  30. void SdSpiAltDriver::activate() {
  31. SPI.beginTransaction(m_spiSettings);
  32. }
  33. //------------------------------------------------------------------------------
  34. void SdSpiAltDriver::begin(uint8_t chipSelectPin) {
  35. m_csPin = chipSelectPin;
  36. pinMode(m_csPin, OUTPUT);
  37. digitalWrite(m_csPin, HIGH);
  38. SPI.begin();
  39. }
  40. //------------------------------------------------------------------------------
  41. void SdSpiAltDriver::deactivate() {
  42. SPI.endTransaction();
  43. }
  44. //==============================================================================
  45. #ifdef KINETISK
  46. // use 16-bit frame if SPI_USE_8BIT_FRAME is zero
  47. #define SPI_USE_8BIT_FRAME 0
  48. // Limit initial fifo to three entries to avoid fifo overrun
  49. #define SPI_INITIAL_FIFO_DEPTH 3
  50. // define some symbols that are not in mk20dx128.h
  51. #ifndef SPI_SR_RXCTR
  52. #define SPI_SR_RXCTR 0XF0
  53. #endif // SPI_SR_RXCTR
  54. #ifndef SPI_PUSHR_CONT
  55. #define SPI_PUSHR_CONT 0X80000000
  56. #endif // SPI_PUSHR_CONT
  57. #ifndef SPI_PUSHR_CTAS
  58. #define SPI_PUSHR_CTAS(n) (((n) & 7) << 28)
  59. #endif // SPI_PUSHR_CTAS
  60. //------------------------------------------------------------------------------
  61. /** SPI receive a byte */
  62. uint8_t SdSpiAltDriver::receive() {
  63. SPI0_MCR |= SPI_MCR_CLR_RXF;
  64. SPI0_SR = SPI_SR_TCF;
  65. SPI0_PUSHR = 0xFF;
  66. while (!(SPI0_SR & SPI_SR_TCF)) {}
  67. return SPI0_POPR;
  68. }
  69. //------------------------------------------------------------------------------
  70. /** SPI receive multiple bytes */
  71. uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
  72. // clear any data in RX FIFO
  73. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
  74. #if SPI_USE_8BIT_FRAME
  75. // initial number of bytes to push into TX FIFO
  76. int nf = n < SPI_INITIAL_FIFO_DEPTH ? n : SPI_INITIAL_FIFO_DEPTH;
  77. for (int i = 0; i < nf; i++) {
  78. SPI0_PUSHR = 0XFF;
  79. }
  80. // limit for pushing dummy data into TX FIFO
  81. uint8_t* limit = buf + n - nf;
  82. while (buf < limit) {
  83. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  84. SPI0_PUSHR = 0XFF;
  85. *buf++ = SPI0_POPR;
  86. }
  87. // limit for rest of RX data
  88. limit += nf;
  89. while (buf < limit) {
  90. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  91. *buf++ = SPI0_POPR;
  92. }
  93. #else // SPI_USE_8BIT_FRAME
  94. // use 16 bit frame to avoid TD delay between frames
  95. // get one byte if n is odd
  96. if (n & 1) {
  97. *buf++ = receive();
  98. n--;
  99. }
  100. // initial number of words to push into TX FIFO
  101. int nf = n/2 < SPI_INITIAL_FIFO_DEPTH ? n/2 : SPI_INITIAL_FIFO_DEPTH;
  102. for (int i = 0; i < nf; i++) {
  103. SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | 0XFFFF;
  104. }
  105. uint8_t* limit = buf + n - 2*nf;
  106. while (buf < limit) {
  107. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  108. SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | 0XFFFF;
  109. uint16_t w = SPI0_POPR;
  110. *buf++ = w >> 8;
  111. *buf++ = w & 0XFF;
  112. }
  113. // limit for rest of RX data
  114. limit += 2*nf;
  115. while (buf < limit) {
  116. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  117. uint16_t w = SPI0_POPR;
  118. *buf++ = w >> 8;
  119. *buf++ = w & 0XFF;
  120. }
  121. #endif // SPI_USE_8BIT_FRAME
  122. return 0;
  123. }
  124. //------------------------------------------------------------------------------
  125. /** SPI send a byte */
  126. void SdSpiAltDriver::send(uint8_t b) {
  127. SPI0_MCR |= SPI_MCR_CLR_RXF;
  128. SPI0_SR = SPI_SR_TCF;
  129. SPI0_PUSHR = b;
  130. while (!(SPI0_SR & SPI_SR_TCF)) {}
  131. }
  132. //------------------------------------------------------------------------------
  133. /** SPI send multiple bytes */
  134. void SdSpiAltDriver::send(const uint8_t* buf , size_t n) {
  135. // clear any data in RX FIFO
  136. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
  137. #if SPI_USE_8BIT_FRAME
  138. // initial number of bytes to push into TX FIFO
  139. int nf = n < SPI_INITIAL_FIFO_DEPTH ? n : SPI_INITIAL_FIFO_DEPTH;
  140. // limit for pushing data into TX fifo
  141. const uint8_t* limit = buf + n;
  142. for (int i = 0; i < nf; i++) {
  143. SPI0_PUSHR = *buf++;
  144. }
  145. // write data to TX FIFO
  146. while (buf < limit) {
  147. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  148. SPI0_PUSHR = *buf++;
  149. SPI0_POPR;
  150. }
  151. // wait for data to be sent
  152. while (nf) {
  153. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  154. SPI0_POPR;
  155. nf--;
  156. }
  157. #else // SPI_USE_8BIT_FRAME
  158. // use 16 bit frame to avoid TD delay between frames
  159. // send one byte if n is odd
  160. if (n & 1) {
  161. send(*buf++);
  162. n--;
  163. }
  164. // initial number of words to push into TX FIFO
  165. int nf = n/2 < SPI_INITIAL_FIFO_DEPTH ? n/2 : SPI_INITIAL_FIFO_DEPTH;
  166. // limit for pushing data into TX fifo
  167. const uint8_t* limit = buf + n;
  168. for (int i = 0; i < nf; i++) {
  169. uint16_t w = (*buf++) << 8;
  170. w |= *buf++;
  171. SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | w;
  172. }
  173. // write data to TX FIFO
  174. while (buf < limit) {
  175. uint16_t w = *buf++ << 8;
  176. w |= *buf++;
  177. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  178. SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | w;
  179. SPI0_POPR;
  180. }
  181. // wait for data to be sent
  182. while (nf) {
  183. while (!(SPI0_SR & SPI_SR_RXCTR)) {}
  184. SPI0_POPR;
  185. nf--;
  186. }
  187. #endif // SPI_USE_8BIT_FRAME
  188. }
  189. #else // KINETISK
  190. //==============================================================================
  191. // Use standard SPI library if not KINETISK
  192. //------------------------------------------------------------------------------
  193. /** Receive a byte.
  194. *
  195. * \return The byte.
  196. */
  197. uint8_t SdSpiAltDriver::receive() {
  198. return SPI.transfer(0XFF);
  199. }
  200. /** Receive multiple bytes.
  201. *
  202. * \param[out] buf Buffer to receive the data.
  203. * \param[in] n Number of bytes to receive.
  204. *
  205. * \return Zero for no error or nonzero error code.
  206. */
  207. uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
  208. for (size_t i = 0; i < n; i++) {
  209. buf[i] = SPI.transfer(0XFF);
  210. }
  211. return 0;
  212. }
  213. /** Send a byte.
  214. *
  215. * \param[in] b Byte to send
  216. */
  217. void SdSpiAltDriver::send(uint8_t b) {
  218. SPI.transfer(b);
  219. }
  220. /** Send multiple bytes.
  221. *
  222. * \param[in] buf Buffer for data to be sent.
  223. * \param[in] n Number of bytes to send.
  224. */
  225. void SdSpiAltDriver::send(const uint8_t* buf , size_t n) {
  226. for (size_t i = 0; i < n; i++) {
  227. SPI.transfer(buf[i]);
  228. }
  229. }
  230. #endif // KINETISK
  231. #endif // defined(__arm__) && defined(CORE_TEENSY)