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_Due.cpp 32KB

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