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

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