My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HAL_SPI.cpp 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Software SPI functions originally from Arduino Sd2Card Library
  24. * Copyright (c) 2009 by William Greiman
  25. *
  26. * Completely rewritten and tuned by Eduardo José Tagle in 2017/2018
  27. * in ARM thumb2 inline assembler and tuned for maximum speed and performance
  28. * allowing SPI clocks of up to 12 Mhz to increase SD card read/write performance
  29. */
  30. /**
  31. * HAL for Arduino Due and compatible (SAM3X8E)
  32. */
  33. #ifdef ARDUINO_ARCH_SAM
  34. #include "../../inc/MarlinConfig.h"
  35. #include "../shared/Delay.h"
  36. // ------------------------
  37. // Public functions
  38. // ------------------------
  39. #if EITHER(DUE_SOFTWARE_SPI, FORCE_SOFT_SPI)
  40. // ------------------------
  41. // Software SPI
  42. // ------------------------
  43. // Make sure GCC optimizes this file.
  44. // Note that this line triggers a bug in GCC which is fixed by casting.
  45. // See the note below.
  46. #pragma GCC optimize (3)
  47. typedef uint8_t (*pfnSpiTransfer)(uint8_t b);
  48. typedef void (*pfnSpiRxBlock)(uint8_t *buf, uint32_t nbyte);
  49. typedef void (*pfnSpiTxBlock)(const uint8_t *buf, uint32_t nbyte);
  50. /* ---------------- Macros to be able to access definitions from asm */
  51. #define _PORT(IO) DIO ## IO ## _WPORT
  52. #define _PIN_MASK(IO) MASK(DIO ## IO ## _PIN)
  53. #define _PIN_SHIFT(IO) DIO ## IO ## _PIN
  54. #define PORT(IO) _PORT(IO)
  55. #define PIN_MASK(IO) _PIN_MASK(IO)
  56. #define PIN_SHIFT(IO) _PIN_SHIFT(IO)
  57. // run at ~8 .. ~10Mhz - Tx version (Rx data discarded)
  58. static uint8_t spiTransferTx0(uint8_t bout) { // using Mode 0
  59. uint32_t MOSI_PORT_PLUS30 = ((uint32_t) PORT(SD_MOSI_PIN)) + 0x30; /* SODR of port */
  60. uint32_t MOSI_MASK = PIN_MASK(SD_MOSI_PIN);
  61. uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SD_SCK_PIN)) + 0x30; /* SODR of port */
  62. uint32_t SCK_MASK = PIN_MASK(SD_SCK_PIN);
  63. uint32_t idx = 0;
  64. /* Negate bout, as the assembler requires a negated value */
  65. bout = ~bout;
  66. /* The software SPI routine */
  67. __asm__ __volatile__(
  68. A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
  69. /* Bit 7 */
  70. A("ubfx %[idx],%[txval],#7,#1") /* Place bit 7 in bit 0 of idx*/
  71. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  72. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  73. A("ubfx %[idx],%[txval],#6,#1") /* Place bit 6 in bit 0 of idx*/
  74. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  75. /* Bit 6 */
  76. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  77. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  78. A("ubfx %[idx],%[txval],#5,#1") /* Place bit 5 in bit 0 of idx*/
  79. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  80. /* Bit 5 */
  81. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  82. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  83. A("ubfx %[idx],%[txval],#4,#1") /* Place bit 4 in bit 0 of idx*/
  84. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  85. /* Bit 4 */
  86. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  87. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  88. A("ubfx %[idx],%[txval],#3,#1") /* Place bit 3 in bit 0 of idx*/
  89. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  90. /* Bit 3 */
  91. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  92. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  93. A("ubfx %[idx],%[txval],#2,#1") /* Place bit 2 in bit 0 of idx*/
  94. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  95. /* Bit 2 */
  96. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  97. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  98. A("ubfx %[idx],%[txval],#1,#1") /* Place bit 1 in bit 0 of idx*/
  99. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  100. /* Bit 1 */
  101. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  102. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  103. A("ubfx %[idx],%[txval],#0,#1") /* Place bit 0 in bit 0 of idx*/
  104. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  105. /* Bit 0 */
  106. A("str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  107. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  108. A("nop") /* Result will be 0 */
  109. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  110. : [idx]"+r"( idx )
  111. : [txval]"r"( bout ) ,
  112. [mosi_mask]"r"( MOSI_MASK ),
  113. [mosi_port]"r"( MOSI_PORT_PLUS30 ),
  114. [sck_mask]"r"( SCK_MASK ),
  115. [sck_port]"r"( SCK_PORT_PLUS30 )
  116. : "cc"
  117. );
  118. return 0;
  119. }
  120. // Calculates the bit band alias address and returns a pointer address to word.
  121. // addr: The byte address of bitbanding bit.
  122. // bit: The bit position of bitbanding bit.
  123. #define BITBAND_ADDRESS(addr, bit) \
  124. (((uint32_t)(addr) & 0xF0000000) + 0x02000000 + ((uint32_t)(addr)&0xFFFFF)*32 + (bit)*4)
  125. // run at ~8 .. ~10Mhz - Rx version (Tx line not altered)
  126. static uint8_t spiTransferRx0(uint8_t) { // using Mode 0
  127. uint32_t bin = 0;
  128. uint32_t work = 0;
  129. uint32_t BITBAND_MISO_PORT = BITBAND_ADDRESS( ((uint32_t)PORT(SD_MISO_PIN))+0x3C, PIN_SHIFT(SD_MISO_PIN)); /* PDSR of port in bitband area */
  130. uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SD_SCK_PIN)) + 0x30; /* SODR of port */
  131. uint32_t SCK_MASK = PIN_MASK(SD_SCK_PIN);
  132. /* The software SPI routine */
  133. __asm__ __volatile__(
  134. A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
  135. /* bit 7 */
  136. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  137. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  138. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  139. A("bfi %[bin],%[work],#7,#1") /* Store read bit as the bit 7 */
  140. /* bit 6 */
  141. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  142. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  143. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  144. A("bfi %[bin],%[work],#6,#1") /* Store read bit as the bit 6 */
  145. /* bit 5 */
  146. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  147. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  148. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  149. A("bfi %[bin],%[work],#5,#1") /* Store read bit as the bit 5 */
  150. /* bit 4 */
  151. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  152. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  153. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  154. A("bfi %[bin],%[work],#4,#1") /* Store read bit as the bit 4 */
  155. /* bit 3 */
  156. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  157. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  158. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  159. A("bfi %[bin],%[work],#3,#1") /* Store read bit as the bit 3 */
  160. /* bit 2 */
  161. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  162. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  163. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  164. A("bfi %[bin],%[work],#2,#1") /* Store read bit as the bit 2 */
  165. /* bit 1 */
  166. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  167. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  168. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  169. A("bfi %[bin],%[work],#1,#1") /* Store read bit as the bit 1 */
  170. /* bit 0 */
  171. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  172. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  173. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  174. A("bfi %[bin],%[work],#0,#1") /* Store read bit as the bit 0 */
  175. : [bin]"+r"(bin),
  176. [work]"+r"(work)
  177. : [bitband_miso_port]"r"( BITBAND_MISO_PORT ),
  178. [sck_mask]"r"( SCK_MASK ),
  179. [sck_port]"r"( SCK_PORT_PLUS30 )
  180. : "cc"
  181. );
  182. return bin;
  183. }
  184. // run at ~4Mhz
  185. static uint8_t spiTransfer1(uint8_t b) { // using Mode 0
  186. int bits = 8;
  187. do {
  188. WRITE(SD_MOSI_PIN, b & 0x80);
  189. b <<= 1; // little setup time
  190. WRITE(SD_SCK_PIN, HIGH);
  191. DELAY_NS(125); // 10 cycles @ 84mhz
  192. b |= (READ(SD_MISO_PIN) != 0);
  193. WRITE(SD_SCK_PIN, LOW);
  194. DELAY_NS(125); // 10 cycles @ 84mhz
  195. } while (--bits);
  196. return b;
  197. }
  198. // all the others
  199. static uint16_t spiDelayNS = 4000; // 4000ns => 125khz
  200. static uint8_t spiTransferX(uint8_t b) { // using Mode 0
  201. int bits = 8;
  202. do {
  203. WRITE(SD_MOSI_PIN, b & 0x80);
  204. b <<= 1; // little setup time
  205. WRITE(SD_SCK_PIN, HIGH);
  206. DELAY_NS_VAR(spiDelayNS);
  207. b |= (READ(SD_MISO_PIN) != 0);
  208. WRITE(SD_SCK_PIN, LOW);
  209. DELAY_NS_VAR(spiDelayNS);
  210. } while (--bits);
  211. return b;
  212. }
  213. // Pointers to generic functions for byte transfers
  214. /**
  215. * Note: The cast is unnecessary, but without it, this file triggers a GCC 4.8.3-2014 bug.
  216. * Later GCC versions do not have this problem, but at this time (May 2018) Arduino still
  217. * uses that buggy and obsolete GCC version!!
  218. */
  219. static pfnSpiTransfer spiTransferRx = (pfnSpiTransfer)spiTransferX;
  220. static pfnSpiTransfer spiTransferTx = (pfnSpiTransfer)spiTransferX;
  221. // Block transfers run at ~8 .. ~10Mhz - Tx version (Rx data discarded)
  222. static void spiTxBlock0(const uint8_t *ptr, uint32_t todo) {
  223. uint32_t MOSI_PORT_PLUS30 = ((uint32_t) PORT(SD_MOSI_PIN)) + 0x30; /* SODR of port */
  224. uint32_t MOSI_MASK = PIN_MASK(SD_MOSI_PIN);
  225. uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SD_SCK_PIN)) + 0x30; /* SODR of port */
  226. uint32_t SCK_MASK = PIN_MASK(SD_SCK_PIN);
  227. uint32_t work = 0;
  228. uint32_t txval = 0;
  229. /* The software SPI routine */
  230. __asm__ __volatile__(
  231. A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
  232. L("loop%=")
  233. A("ldrb.w %[txval], [%[ptr]], #1") /* Load value to send, increment buffer */
  234. A("mvn %[txval],%[txval]") /* Negate value */
  235. /* Bit 7 */
  236. A("ubfx %[work],%[txval],#7,#1") /* Place bit 7 in bit 0 of work*/
  237. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  238. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  239. A("ubfx %[work],%[txval],#6,#1") /* Place bit 6 in bit 0 of work*/
  240. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  241. /* Bit 6 */
  242. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  243. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  244. A("ubfx %[work],%[txval],#5,#1") /* Place bit 5 in bit 0 of work*/
  245. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  246. /* Bit 5 */
  247. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  248. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  249. A("ubfx %[work],%[txval],#4,#1") /* Place bit 4 in bit 0 of work*/
  250. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  251. /* Bit 4 */
  252. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  253. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  254. A("ubfx %[work],%[txval],#3,#1") /* Place bit 3 in bit 0 of work*/
  255. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  256. /* Bit 3 */
  257. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  258. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  259. A("ubfx %[work],%[txval],#2,#1") /* Place bit 2 in bit 0 of work*/
  260. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  261. /* Bit 2 */
  262. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  263. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  264. A("ubfx %[work],%[txval],#1,#1") /* Place bit 1 in bit 0 of work*/
  265. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  266. /* Bit 1 */
  267. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  268. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  269. A("ubfx %[work],%[txval],#0,#1") /* Place bit 0 in bit 0 of work*/
  270. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  271. /* Bit 0 */
  272. A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
  273. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  274. A("subs %[todo],#1") /* Decrement count of pending words to send, update status */
  275. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  276. A("bne.n loop%=") /* Repeat until done */
  277. : [ptr]"+r" ( ptr ) ,
  278. [todo]"+r" ( todo ) ,
  279. [work]"+r"( work ) ,
  280. [txval]"+r"( txval )
  281. : [mosi_mask]"r"( MOSI_MASK ),
  282. [mosi_port]"r"( MOSI_PORT_PLUS30 ),
  283. [sck_mask]"r"( SCK_MASK ),
  284. [sck_port]"r"( SCK_PORT_PLUS30 )
  285. : "cc"
  286. );
  287. }
  288. static void spiRxBlock0(uint8_t *ptr, uint32_t todo) {
  289. uint32_t bin = 0;
  290. uint32_t work = 0;
  291. uint32_t BITBAND_MISO_PORT = BITBAND_ADDRESS( ((uint32_t)PORT(SD_MISO_PIN))+0x3C, PIN_SHIFT(SD_MISO_PIN)); /* PDSR of port in bitband area */
  292. uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SD_SCK_PIN)) + 0x30; /* SODR of port */
  293. uint32_t SCK_MASK = PIN_MASK(SD_SCK_PIN);
  294. /* The software SPI routine */
  295. __asm__ __volatile__(
  296. A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
  297. L("loop%=")
  298. /* bit 7 */
  299. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  300. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  301. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  302. A("bfi %[bin],%[work],#7,#1") /* Store read bit as the bit 7 */
  303. /* bit 6 */
  304. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  305. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  306. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  307. A("bfi %[bin],%[work],#6,#1") /* Store read bit as the bit 6 */
  308. /* bit 5 */
  309. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  310. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  311. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  312. A("bfi %[bin],%[work],#5,#1") /* Store read bit as the bit 5 */
  313. /* bit 4 */
  314. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  315. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  316. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  317. A("bfi %[bin],%[work],#4,#1") /* Store read bit as the bit 4 */
  318. /* bit 3 */
  319. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  320. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  321. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  322. A("bfi %[bin],%[work],#3,#1") /* Store read bit as the bit 3 */
  323. /* bit 2 */
  324. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  325. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  326. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  327. A("bfi %[bin],%[work],#2,#1") /* Store read bit as the bit 2 */
  328. /* bit 1 */
  329. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  330. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  331. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  332. A("bfi %[bin],%[work],#1,#1") /* Store read bit as the bit 1 */
  333. /* bit 0 */
  334. A("str %[sck_mask],[%[sck_port]]") /* SODR */
  335. A("ldr %[work],[%[bitband_miso_port]]") /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  336. A("str %[sck_mask],[%[sck_port],#0x4]") /* CODR */
  337. A("bfi %[bin],%[work],#0,#1") /* Store read bit as the bit 0 */
  338. A("subs %[todo],#1") /* Decrement count of pending words to send, update status */
  339. A("strb.w %[bin], [%[ptr]], #1") /* Store read value into buffer, increment buffer pointer */
  340. A("bne.n loop%=") /* Repeat until done */
  341. : [ptr]"+r"(ptr),
  342. [todo]"+r"(todo),
  343. [bin]"+r"(bin),
  344. [work]"+r"(work)
  345. : [bitband_miso_port]"r"( BITBAND_MISO_PORT ),
  346. [sck_mask]"r"( SCK_MASK ),
  347. [sck_port]"r"( SCK_PORT_PLUS30 )
  348. : "cc"
  349. );
  350. }
  351. static void spiTxBlockX(const uint8_t *buf, uint32_t todo) {
  352. do {
  353. (void)spiTransferTx(*buf++);
  354. } while (--todo);
  355. }
  356. static void spiRxBlockX(uint8_t *buf, uint32_t todo) {
  357. do {
  358. *buf++ = spiTransferRx(0xFF);
  359. } while (--todo);
  360. }
  361. // Pointers to generic functions for block transfers
  362. static pfnSpiTxBlock spiTxBlock = (pfnSpiTxBlock)spiTxBlockX;
  363. static pfnSpiRxBlock spiRxBlock = (pfnSpiRxBlock)spiRxBlockX;
  364. #if MB(ALLIGATOR)
  365. #define _SS_WRITE(S) WRITE(SD_SS_PIN, S)
  366. #else
  367. #define _SS_WRITE(S) NOOP
  368. #endif
  369. void spiBegin() {
  370. SET_OUTPUT(SD_SS_PIN);
  371. _SS_WRITE(HIGH);
  372. SET_OUTPUT(SD_SCK_PIN);
  373. SET_INPUT(SD_MISO_PIN);
  374. SET_OUTPUT(SD_MOSI_PIN);
  375. }
  376. uint8_t spiRec() {
  377. _SS_WRITE(LOW);
  378. WRITE(SD_MOSI_PIN, HIGH); // Output 1s 1
  379. uint8_t b = spiTransferRx(0xFF);
  380. _SS_WRITE(HIGH);
  381. return b;
  382. }
  383. void spiRead(uint8_t *buf, uint16_t nbyte) {
  384. if (nbyte) {
  385. _SS_WRITE(LOW);
  386. WRITE(SD_MOSI_PIN, HIGH); // Output 1s 1
  387. spiRxBlock(buf, nbyte);
  388. _SS_WRITE(HIGH);
  389. }
  390. }
  391. void spiSend(uint8_t b) {
  392. _SS_WRITE(LOW);
  393. (void)spiTransferTx(b);
  394. _SS_WRITE(HIGH);
  395. }
  396. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  397. _SS_WRITE(LOW);
  398. (void)spiTransferTx(token);
  399. spiTxBlock(buf, 512);
  400. _SS_WRITE(HIGH);
  401. }
  402. /**
  403. * spiRate should be
  404. * 0 : 8 - 10 MHz
  405. * 1 : 4 - 5 MHz
  406. * 2 : 2 - 2.5 MHz
  407. * 3 : 1 - 1.25 MHz
  408. * 4 : 500 - 625 kHz
  409. * 5 : 250 - 312 kHz
  410. * 6 : 125 - 156 kHz
  411. */
  412. void spiInit(uint8_t spiRate) {
  413. switch (spiRate) {
  414. case 0:
  415. spiTransferTx = (pfnSpiTransfer)spiTransferTx0;
  416. spiTransferRx = (pfnSpiTransfer)spiTransferRx0;
  417. spiTxBlock = (pfnSpiTxBlock)spiTxBlock0;
  418. spiRxBlock = (pfnSpiRxBlock)spiRxBlock0;
  419. break;
  420. case 1:
  421. spiTransferTx = (pfnSpiTransfer)spiTransfer1;
  422. spiTransferRx = (pfnSpiTransfer)spiTransfer1;
  423. spiTxBlock = (pfnSpiTxBlock)spiTxBlockX;
  424. spiRxBlock = (pfnSpiRxBlock)spiRxBlockX;
  425. break;
  426. default:
  427. spiDelayNS = 4000 >> (6 - spiRate); // spiRate of 2 gives the maximum error with current CPU
  428. spiTransferTx = (pfnSpiTransfer)spiTransferX;
  429. spiTransferRx = (pfnSpiTransfer)spiTransferX;
  430. spiTxBlock = (pfnSpiTxBlock)spiTxBlockX;
  431. spiRxBlock = (pfnSpiRxBlock)spiRxBlockX;
  432. break;
  433. }
  434. _SS_WRITE(HIGH);
  435. WRITE(SD_MOSI_PIN, HIGH);
  436. WRITE(SD_SCK_PIN, LOW);
  437. }
  438. /** Begin SPI transaction, set clock, bit order, data mode */
  439. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  440. // TODO: to be implemented
  441. }
  442. #pragma GCC reset_options
  443. #else // !SOFTWARE_SPI
  444. #define WHILE_TX(N) while ((SPI0->SPI_SR & SPI_SR_TDRE) == (N))
  445. #define WHILE_RX(N) while ((SPI0->SPI_SR & SPI_SR_RDRF) == (N))
  446. #define FLUSH_TX() do{ WHILE_RX(1) SPI0->SPI_RDR; }while(0)
  447. #if MB(ALLIGATOR)
  448. // slave selects controlled by SPI controller
  449. // doesn't support changing SPI speeds for SD card
  450. // ------------------------
  451. // hardware SPI
  452. // ------------------------
  453. static bool spiInitialized = false;
  454. void spiInit(uint8_t spiRate) {
  455. if (spiInitialized) return;
  456. // 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
  457. constexpr int spiDivider[] = { 10, 21, 42, 84, 168, 255, 255 };
  458. if (spiRate > 6) spiRate = 1;
  459. // Set SPI mode 1, clock, select not active after transfer, with delay between transfers
  460. SPI_ConfigureNPCS(SPI0, SPI_CHAN_DAC,
  461. SPI_CSR_CSAAT | SPI_CSR_SCBR(spiDivider[spiRate]) |
  462. SPI_CSR_DLYBCT(1));
  463. // Set SPI mode 0, clock, select not active after transfer, with delay between transfers
  464. SPI_ConfigureNPCS(SPI0, SPI_CHAN_EEPROM1, SPI_CSR_NCPHA |
  465. SPI_CSR_CSAAT | SPI_CSR_SCBR(spiDivider[spiRate]) |
  466. SPI_CSR_DLYBCT(1));
  467. // Set SPI mode 0, clock, select not active after transfer, with delay between transfers
  468. SPI_ConfigureNPCS(SPI0, SPI_CHAN, SPI_CSR_NCPHA |
  469. SPI_CSR_CSAAT | SPI_CSR_SCBR(spiDivider[spiRate]) |
  470. SPI_CSR_DLYBCT(1));
  471. SPI_Enable(SPI0);
  472. spiInitialized = true;
  473. }
  474. void spiBegin() {
  475. if (spiInitialized) return;
  476. // Configure SPI pins
  477. PIO_Configure(
  478. g_APinDescription[SD_SCK_PIN].pPort,
  479. g_APinDescription[SD_SCK_PIN].ulPinType,
  480. g_APinDescription[SD_SCK_PIN].ulPin,
  481. g_APinDescription[SD_SCK_PIN].ulPinConfiguration);
  482. PIO_Configure(
  483. g_APinDescription[SD_MOSI_PIN].pPort,
  484. g_APinDescription[SD_MOSI_PIN].ulPinType,
  485. g_APinDescription[SD_MOSI_PIN].ulPin,
  486. g_APinDescription[SD_MOSI_PIN].ulPinConfiguration);
  487. PIO_Configure(
  488. g_APinDescription[SD_MISO_PIN].pPort,
  489. g_APinDescription[SD_MISO_PIN].ulPinType,
  490. g_APinDescription[SD_MISO_PIN].ulPin,
  491. g_APinDescription[SD_MISO_PIN].ulPinConfiguration);
  492. // set master mode, peripheral select, fault detection
  493. SPI_Configure(SPI0, ID_SPI0, SPI_MR_MSTR | SPI_MR_MODFDIS | SPI_MR_PS);
  494. SPI_Enable(SPI0);
  495. SET_OUTPUT(DAC0_SYNC_PIN);
  496. #if HAS_MULTI_EXTRUDER
  497. OUT_WRITE(DAC1_SYNC_PIN, HIGH);
  498. #endif
  499. WRITE(DAC0_SYNC_PIN, HIGH);
  500. OUT_WRITE(SPI_EEPROM1_CS_PIN, HIGH);
  501. OUT_WRITE(SPI_EEPROM2_CS_PIN, HIGH);
  502. OUT_WRITE(SPI_FLASH_CS_PIN, HIGH);
  503. WRITE(SD_SS_PIN, HIGH);
  504. OUT_WRITE(SDSS, LOW);
  505. PIO_Configure(
  506. g_APinDescription[SPI_PIN].pPort,
  507. g_APinDescription[SPI_PIN].ulPinType,
  508. g_APinDescription[SPI_PIN].ulPin,
  509. g_APinDescription[SPI_PIN].ulPinConfiguration
  510. );
  511. spiInit(1);
  512. }
  513. // Read single byte from SPI
  514. uint8_t spiRec() {
  515. // write dummy byte with address and end transmission flag
  516. SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN) | SPI_TDR_LASTXFER;
  517. WHILE_TX(0);
  518. WHILE_RX(0);
  519. //DELAY_US(1U);
  520. return SPI0->SPI_RDR;
  521. }
  522. uint8_t spiRec(uint32_t chan) {
  523. WHILE_TX(0);
  524. FLUSH_RX();
  525. // write dummy byte with address and end transmission flag
  526. SPI0->SPI_TDR = 0x000000FF | SPI_PCS(chan) | SPI_TDR_LASTXFER;
  527. WHILE_RX(0);
  528. return SPI0->SPI_RDR;
  529. }
  530. // Read from SPI into buffer
  531. void spiRead(uint8_t *buf, uint16_t nbyte) {
  532. if (!nbyte) return;
  533. --nbyte;
  534. for (int i = 0; i < nbyte; i++) {
  535. //WHILE_TX(0);
  536. SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN);
  537. WHILE_RX(0);
  538. buf[i] = SPI0->SPI_RDR;
  539. //DELAY_US(1U);
  540. }
  541. buf[nbyte] = spiRec();
  542. }
  543. // Write single byte to SPI
  544. void spiSend(const byte b) {
  545. // write byte with address and end transmission flag
  546. SPI0->SPI_TDR = (uint32_t)b | SPI_PCS(SPI_CHAN) | SPI_TDR_LASTXFER;
  547. WHILE_TX(0);
  548. WHILE_RX(0);
  549. SPI0->SPI_RDR;
  550. //DELAY_US(1U);
  551. }
  552. void spiSend(const uint8_t *buf, size_t nbyte) {
  553. if (!nbyte) return;
  554. --nbyte;
  555. for (size_t i = 0; i < nbyte; i++) {
  556. SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(SPI_CHAN);
  557. WHILE_TX(0);
  558. WHILE_RX(0);
  559. SPI0->SPI_RDR;
  560. //DELAY_US(1U);
  561. }
  562. spiSend(buf[nbyte]);
  563. }
  564. void spiSend(uint32_t chan, byte b) {
  565. WHILE_TX(0);
  566. // write byte with address and end transmission flag
  567. SPI0->SPI_TDR = (uint32_t)b | SPI_PCS(chan) | SPI_TDR_LASTXFER;
  568. WHILE_RX(0);
  569. FLUSH_RX();
  570. }
  571. void spiSend(uint32_t chan, const uint8_t *buf, size_t nbyte) {
  572. if (!nbyte) return;
  573. --nbyte;
  574. for (size_t i = 0; i < nbyte; i++) {
  575. WHILE_TX(0);
  576. SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(chan);
  577. WHILE_RX(0);
  578. FLUSH_RX();
  579. }
  580. spiSend(chan, buf[nbyte]);
  581. }
  582. // Write from buffer to SPI
  583. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  584. SPI0->SPI_TDR = (uint32_t)token | SPI_PCS(SPI_CHAN);
  585. WHILE_TX(0);
  586. //WHILE_RX(0);
  587. //SPI0->SPI_RDR;
  588. for (int i = 0; i < 511; i++) {
  589. SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(SPI_CHAN);
  590. WHILE_TX(0);
  591. WHILE_RX(0);
  592. SPI0->SPI_RDR;
  593. //DELAY_US(1U);
  594. }
  595. spiSend(buf[511]);
  596. }
  597. /** Begin SPI transaction, set clock, bit order, data mode */
  598. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  599. // TODO: to be implemented
  600. }
  601. #else // U8G compatible hardware SPI
  602. #define SPI_MODE_0_DUE_HW 2 // DUE CPHA control bit is inverted
  603. #define SPI_MODE_1_DUE_HW 3
  604. #define SPI_MODE_2_DUE_HW 0
  605. #define SPI_MODE_3_DUE_HW 1
  606. /**
  607. * The DUE SPI controller is set up so the upper word of the longword
  608. * written to the transmit data register selects which SPI Chip Select
  609. * Register is used. This allows different streams to have different SPI
  610. * settings.
  611. *
  612. * In practice it's spooky. Some combinations hang the system, while others
  613. * upset the peripheral device.
  614. *
  615. * SPI mode should be the same for all streams. The FYSETC_MINI_12864 gets
  616. * upset if the clock phase changes after chip select goes active.
  617. *
  618. * SPI_CSR_CSAAT should be set for all streams. If not the WHILE_TX(0)
  619. * macro returns immediately which can result in the SPI chip select going
  620. * inactive before all the data has been sent.
  621. *
  622. * The TMC2130 library uses SPI0->SPI_CSR[3].
  623. *
  624. * The U8G hardware SPI uses SPI0->SPI_CSR[0]. The system hangs and/or the
  625. * FYSETC_MINI_12864 gets upset if lower baud rates are used and the SD card
  626. * is inserted or removed.
  627. *
  628. * The SD card uses SPI0->SPI_CSR[3]. Efforts were made to use [1] and [2]
  629. * but they all resulted in hangs or garbage on the LCD.
  630. *
  631. * The SPI controlled chip selects are NOT enabled in the GPIO controller.
  632. * The application must control the chip select.
  633. *
  634. * All of the above can be avoided by defining FORCE_SOFT_SPI to force the
  635. * display to use software SPI.
  636. */
  637. void spiInit(uint8_t spiRate=6) { // Default to slowest rate if not specified)
  638. // Also sets U8G SPI rate to 4MHz and the SPI mode to 3
  639. // 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
  640. constexpr int spiDivider[] = { 10, 21, 42, 84, 168, 255, 255 };
  641. if (spiRate > 6) spiRate = 1;
  642. // Enable PIOA and SPI0
  643. REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
  644. // Disable PIO on A26 and A27
  645. REG_PIOA_PDR = 0x0C000000;
  646. OUT_WRITE(SDSS, HIGH);
  647. // Reset SPI0 (from sam lib)
  648. SPI0->SPI_CR = SPI_CR_SPIDIS;
  649. SPI0->SPI_CR = SPI_CR_SWRST;
  650. SPI0->SPI_CR = SPI_CR_SWRST;
  651. SPI0->SPI_CR = SPI_CR_SPIEN;
  652. // TMC2103 compatible setup
  653. // Master mode, no fault detection, PCS bits in data written to TDR select CSR register
  654. SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS;
  655. // SPI mode 3, 8 Bit data transfer, baud rate
  656. SPI0->SPI_CSR[3] = SPI_CSR_SCBR(spiDivider[spiRate]) | SPI_CSR_CSAAT | SPI_MODE_3_DUE_HW; // use same CSR as TMC2130
  657. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(spiDivider[1]) | SPI_CSR_CSAAT | SPI_MODE_3_DUE_HW; // U8G default to 4MHz
  658. }
  659. void spiBegin() { spiInit(); }
  660. static uint8_t spiTransfer(uint8_t data) {
  661. WHILE_TX(0);
  662. SPI0->SPI_TDR = (uint32_t)data | 0x00070000UL; // Add TMC2130 PCS bits to every byte (use SPI0->SPI_CSR[3])
  663. WHILE_TX(0);
  664. WHILE_RX(0);
  665. return SPI0->SPI_RDR;
  666. }
  667. uint8_t spiRec() { return (uint8_t)spiTransfer(0xFF); }
  668. void spiRead(uint8_t *buf, uint16_t nbyte) {
  669. for (int i = 0; i < nbyte; i++)
  670. buf[i] = spiTransfer(0xFF);
  671. }
  672. void spiSend(uint8_t data) { spiTransfer(data); }
  673. void spiSend(const uint8_t *buf, size_t nbyte) {
  674. for (uint16_t i = 0; i < nbyte; i++)
  675. spiTransfer(buf[i]);
  676. }
  677. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  678. spiTransfer(token);
  679. for (uint16_t i = 0; i < 512; i++)
  680. spiTransfer(buf[i]);
  681. }
  682. #endif // !ALLIGATOR
  683. #endif // !SOFTWARE_SPI
  684. #endif // ARDUINO_ARCH_SAM