Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

217 lines
7.5KB

  1. /* Arduino SdSpi Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the Arduino SdSpi Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdSpi Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <SdSpi.h>
  21. #if USE_NATIVE_SAM3X_SPI
  22. /** Use SAM3X DMAC if nonzero */
  23. #define USE_SAM3X_DMAC 1
  24. /** Use extra Bus Matrix arbitration fix if nonzero */
  25. #define USE_SAM3X_BUS_MATRIX_FIX 0
  26. /** Time in ms for DMA receive timeout */
  27. #define SAM3X_DMA_TIMEOUT 100
  28. /** chip select register number */
  29. #define SPI_CHIP_SEL 3
  30. /** DMAC receive channel */
  31. #define SPI_DMAC_RX_CH 1
  32. /** DMAC transmit channel */
  33. #define SPI_DMAC_TX_CH 0
  34. /** DMAC Channel HW Interface Number for SPI TX. */
  35. #define SPI_TX_IDX 1
  36. /** DMAC Channel HW Interface Number for SPI RX. */
  37. #define SPI_RX_IDX 2
  38. //------------------------------------------------------------------------------
  39. /** Disable DMA Controller. */
  40. static void dmac_disable() {
  41. DMAC->DMAC_EN &= (~DMAC_EN_ENABLE);
  42. }
  43. /** Enable DMA Controller. */
  44. static void dmac_enable() {
  45. DMAC->DMAC_EN = DMAC_EN_ENABLE;
  46. }
  47. /** Disable DMA Channel. */
  48. static void dmac_channel_disable(uint32_t ul_num) {
  49. DMAC->DMAC_CHDR = DMAC_CHDR_DIS0 << ul_num;
  50. }
  51. /** Enable DMA Channel. */
  52. static void dmac_channel_enable(uint32_t ul_num) {
  53. DMAC->DMAC_CHER = DMAC_CHER_ENA0 << ul_num;
  54. }
  55. /** Poll for transfer complete. */
  56. static bool dmac_channel_transfer_done(uint32_t ul_num) {
  57. return (DMAC->DMAC_CHSR & (DMAC_CHSR_ENA0 << ul_num)) ? false : true;
  58. }
  59. //------------------------------------------------------------------------------
  60. void SdSpi::begin() {
  61. PIO_Configure(
  62. g_APinDescription[PIN_SPI_MOSI].pPort,
  63. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  64. g_APinDescription[PIN_SPI_MOSI].ulPin,
  65. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  66. PIO_Configure(
  67. g_APinDescription[PIN_SPI_MISO].pPort,
  68. g_APinDescription[PIN_SPI_MISO].ulPinType,
  69. g_APinDescription[PIN_SPI_MISO].ulPin,
  70. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  71. PIO_Configure(
  72. g_APinDescription[PIN_SPI_SCK].pPort,
  73. g_APinDescription[PIN_SPI_SCK].ulPinType,
  74. g_APinDescription[PIN_SPI_SCK].ulPin,
  75. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  76. pmc_enable_periph_clk(ID_SPI0);
  77. #if USE_SAM3X_DMAC
  78. pmc_enable_periph_clk(ID_DMAC);
  79. dmac_disable();
  80. DMAC->DMAC_GCFG = DMAC_GCFG_ARB_CFG_FIXED;
  81. dmac_enable();
  82. #if USE_SAM3X_BUS_MATRIX_FIX
  83. MATRIX->MATRIX_WPMR = 0x4d415400;
  84. MATRIX->MATRIX_MCFG[1] = 1;
  85. MATRIX->MATRIX_MCFG[2] = 1;
  86. MATRIX->MATRIX_SCFG[0] = 0x01000010;
  87. MATRIX->MATRIX_SCFG[1] = 0x01000010;
  88. MATRIX->MATRIX_SCFG[7] = 0x01000010;
  89. #endif // USE_SAM3X_BUS_MATRIX_FIX
  90. #endif // USE_SAM3X_DMAC
  91. }
  92. //------------------------------------------------------------------------------
  93. // start RX DMA
  94. static void spiDmaRX(uint8_t* dst, uint16_t count) {
  95. dmac_channel_disable(SPI_DMAC_RX_CH);
  96. DMAC->DMAC_CH_NUM[SPI_DMAC_RX_CH].DMAC_SADDR = (uint32_t)&SPI0->SPI_RDR;
  97. DMAC->DMAC_CH_NUM[SPI_DMAC_RX_CH].DMAC_DADDR = (uint32_t)dst;
  98. DMAC->DMAC_CH_NUM[SPI_DMAC_RX_CH].DMAC_DSCR = 0;
  99. DMAC->DMAC_CH_NUM[SPI_DMAC_RX_CH].DMAC_CTRLA = count |
  100. DMAC_CTRLA_SRC_WIDTH_BYTE | DMAC_CTRLA_DST_WIDTH_BYTE;
  101. DMAC->DMAC_CH_NUM[SPI_DMAC_RX_CH].DMAC_CTRLB = DMAC_CTRLB_SRC_DSCR |
  102. DMAC_CTRLB_DST_DSCR | DMAC_CTRLB_FC_PER2MEM_DMA_FC |
  103. DMAC_CTRLB_SRC_INCR_FIXED | DMAC_CTRLB_DST_INCR_INCREMENTING;
  104. DMAC->DMAC_CH_NUM[SPI_DMAC_RX_CH].DMAC_CFG = DMAC_CFG_SRC_PER(SPI_RX_IDX) |
  105. DMAC_CFG_SRC_H2SEL | DMAC_CFG_SOD | DMAC_CFG_FIFOCFG_ASAP_CFG;
  106. dmac_channel_enable(SPI_DMAC_RX_CH);
  107. }
  108. //------------------------------------------------------------------------------
  109. // start TX DMA
  110. static void spiDmaTX(const uint8_t* src, uint16_t count) {
  111. static uint8_t ff = 0XFF;
  112. uint32_t src_incr = DMAC_CTRLB_SRC_INCR_INCREMENTING;
  113. if (!src) {
  114. src = &ff;
  115. src_incr = DMAC_CTRLB_SRC_INCR_FIXED;
  116. }
  117. dmac_channel_disable(SPI_DMAC_TX_CH);
  118. DMAC->DMAC_CH_NUM[SPI_DMAC_TX_CH].DMAC_SADDR = (uint32_t)src;
  119. DMAC->DMAC_CH_NUM[SPI_DMAC_TX_CH].DMAC_DADDR = (uint32_t)&SPI0->SPI_TDR;
  120. DMAC->DMAC_CH_NUM[SPI_DMAC_TX_CH].DMAC_DSCR = 0;
  121. DMAC->DMAC_CH_NUM[SPI_DMAC_TX_CH].DMAC_CTRLA = count |
  122. DMAC_CTRLA_SRC_WIDTH_BYTE | DMAC_CTRLA_DST_WIDTH_BYTE;
  123. DMAC->DMAC_CH_NUM[SPI_DMAC_TX_CH].DMAC_CTRLB = DMAC_CTRLB_SRC_DSCR |
  124. DMAC_CTRLB_DST_DSCR | DMAC_CTRLB_FC_MEM2PER_DMA_FC |
  125. src_incr | DMAC_CTRLB_DST_INCR_FIXED;
  126. DMAC->DMAC_CH_NUM[SPI_DMAC_TX_CH].DMAC_CFG = DMAC_CFG_DST_PER(SPI_TX_IDX) |
  127. DMAC_CFG_DST_H2SEL | DMAC_CFG_SOD | DMAC_CFG_FIFOCFG_ALAP_CFG;
  128. dmac_channel_enable(SPI_DMAC_TX_CH);
  129. }
  130. //------------------------------------------------------------------------------
  131. // initialize SPI controller
  132. void SdSpi::init(uint8_t sckDivisor) {
  133. uint8_t scbr = sckDivisor;
  134. Spi* pSpi = SPI0;
  135. // disable SPI
  136. pSpi->SPI_CR = SPI_CR_SPIDIS;
  137. // reset SPI
  138. pSpi->SPI_CR = SPI_CR_SWRST;
  139. // no mode fault detection, set master mode
  140. pSpi->SPI_MR = SPI_PCS(SPI_CHIP_SEL) | SPI_MR_MODFDIS | SPI_MR_MSTR;
  141. // mode 0, 8-bit,
  142. pSpi->SPI_CSR[SPI_CHIP_SEL] = SPI_CSR_SCBR(scbr) | SPI_CSR_NCPHA;
  143. // enable SPI
  144. pSpi->SPI_CR |= SPI_CR_SPIEN;
  145. }
  146. //------------------------------------------------------------------------------
  147. static inline uint8_t spiTransfer(uint8_t b) {
  148. Spi* pSpi = SPI0;
  149. pSpi->SPI_TDR = b;
  150. while ((pSpi->SPI_SR & SPI_SR_RDRF) == 0) {}
  151. b = pSpi->SPI_RDR;
  152. return b;
  153. }
  154. //------------------------------------------------------------------------------
  155. /** SPI receive a byte */
  156. uint8_t SdSpi::receive() {
  157. return spiTransfer(0XFF);
  158. }
  159. //------------------------------------------------------------------------------
  160. /** SPI receive multiple bytes */
  161. uint8_t SdSpi::receive(uint8_t* buf, size_t n) {
  162. Spi* pSpi = SPI0;
  163. int rtn = 0;
  164. #if USE_SAM3X_DMAC
  165. // clear overrun error
  166. uint32_t s = pSpi->SPI_SR;
  167. spiDmaRX(buf, n);
  168. spiDmaTX(0, n);
  169. uint32_t m = millis();
  170. while (!dmac_channel_transfer_done(SPI_DMAC_RX_CH)) {
  171. if ((millis() - m) > SAM3X_DMA_TIMEOUT) {
  172. dmac_channel_disable(SPI_DMAC_RX_CH);
  173. dmac_channel_disable(SPI_DMAC_TX_CH);
  174. rtn = 2;
  175. break;
  176. }
  177. }
  178. if (pSpi->SPI_SR & SPI_SR_OVRES) rtn |= 1;
  179. #else // USE_SAM3X_DMAC
  180. for (size_t i = 0; i < n; i++) {
  181. pSpi->SPI_TDR = 0XFF;
  182. while ((pSpi->SPI_SR & SPI_SR_RDRF) == 0) {}
  183. buf[i] = pSpi->SPI_RDR;
  184. }
  185. #endif // USE_SAM3X_DMAC
  186. return rtn;
  187. }
  188. //------------------------------------------------------------------------------
  189. /** SPI send a byte */
  190. void SdSpi::send(uint8_t b) {
  191. spiTransfer(b);
  192. }
  193. //------------------------------------------------------------------------------
  194. void SdSpi::send(const uint8_t* buf , size_t n) {
  195. Spi* pSpi = SPI0;
  196. #if USE_SAM3X_DMAC
  197. spiDmaTX(buf, n);
  198. while (!dmac_channel_transfer_done(SPI_DMAC_TX_CH)) {}
  199. #else // #if USE_SAM3X_DMAC
  200. while ((pSpi->SPI_SR & SPI_SR_TXEMPTY) == 0) {}
  201. for (size_t i = 0; i < n; i++) {
  202. pSpi->SPI_TDR = buf[i];
  203. while ((pSpi->SPI_SR & SPI_SR_TDRE) == 0) {}
  204. }
  205. #endif // #if USE_SAM3X_DMAC
  206. while ((pSpi->SPI_SR & SPI_SR_TXEMPTY) == 0) {}
  207. // leave RDR empty
  208. uint8_t b = pSpi->SPI_RDR;
  209. }
  210. #endif // USE_NATIVE_SAM3X_SPI