My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HAL_spi_Due.cpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. // --------------------------------------------------------------------------
  41. // Public Variables
  42. // --------------------------------------------------------------------------
  43. // --------------------------------------------------------------------------
  44. // Public functions
  45. // --------------------------------------------------------------------------
  46. #if ENABLED(SOFTWARE_SPI)
  47. // --------------------------------------------------------------------------
  48. // software SPI
  49. // --------------------------------------------------------------------------
  50. // set optimization so ARDUINO optimizes this file
  51. #pragma GCC optimize (3)
  52. /* ---------------- Delay Cycles routine -------------- */
  53. /* https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles */
  54. #define nop() __asm__ __volatile__("nop;\n\t":::)
  55. FORCE_INLINE static void __delay_4cycles(uint32_t cy) { // +1 cycle
  56. #if ARCH_PIPELINE_RELOAD_CYCLES<2
  57. #define EXTRA_NOP_CYCLES "nop"
  58. #else
  59. #define EXTRA_NOP_CYCLES ""
  60. #endif
  61. __asm__ __volatile__(
  62. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  63. "loop%=:" "\n\t"
  64. " subs %[cnt],#1" "\n\t"
  65. EXTRA_NOP_CYCLES "\n\t"
  66. " bne loop%=" "\n\t"
  67. : [cnt]"+r"(cy) // output: +r means input+output
  68. : // input:
  69. : "cc" // clobbers:
  70. );
  71. }
  72. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  73. if (__builtin_constant_p(x)) {
  74. #define MAXNOPS 4
  75. if (x <= (MAXNOPS)) {
  76. switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
  77. }
  78. else { // because of +1 cycle inside delay_4cycles
  79. const uint32_t rem = (x - 1) % (MAXNOPS);
  80. switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
  81. if ((x = (x - 1) / (MAXNOPS)))
  82. __delay_4cycles(x); // if need more then 4 nop loop is more optimal
  83. }
  84. }
  85. else
  86. __delay_4cycles(x / 4);
  87. }
  88. /* ---------------- Delay in nanoseconds and in microseconds */
  89. #define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000) / 1000)
  90. typedef uint8_t (*pfnSpiTransfer) (uint8_t b);
  91. typedef void (*pfnSpiRxBlock)(uint8_t* buf, uint32_t nbyte);
  92. typedef void (*pfnSpiTxBlock)(const uint8_t* buf, uint32_t nbyte);
  93. /* ---------------- Macros to be able to access definitions from asm */
  94. #define _PORT(IO) DIO ## IO ## _WPORT
  95. #define _PIN_MASK(IO) MASK(DIO ## IO ## _PIN)
  96. #define _PIN_SHIFT(IO) DIO ## IO ## _PIN
  97. #define PORT(IO) _PORT(IO)
  98. #define PIN_MASK(IO) _PIN_MASK(IO)
  99. #define PIN_SHIFT(IO) _PIN_SHIFT(IO)
  100. // run at ~8 .. ~10Mhz - Tx version (Rx data discarded)
  101. static uint8_t spiTransferTx0(uint8_t bout) { // using Mode 0
  102. register uint32_t MOSI_PORT_PLUS30 = ((uint32_t) PORT(MOSI_PIN)) + 0x30; /* SODR of port */
  103. register uint32_t MOSI_MASK = PIN_MASK(MOSI_PIN);
  104. register uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SCK_PIN)) + 0x30; /* SODR of port */
  105. register uint32_t SCK_MASK = PIN_MASK(SCK_PIN);
  106. register uint32_t idx = 0;
  107. /* Negate bout, as the assembler requires a negated value */
  108. bout = ~bout;
  109. /* The software SPI routine */
  110. __asm__ __volatile__(
  111. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  112. /* Bit 7 */
  113. " ubfx %[idx],%[txval],#7,#1" "\n\t" /* Place bit 7 in bit 0 of idx*/
  114. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  115. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  116. " ubfx %[idx],%[txval],#6,#1" "\n\t" /* Place bit 6 in bit 0 of idx*/
  117. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  118. /* Bit 6 */
  119. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  120. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  121. " ubfx %[idx],%[txval],#5,#1" "\n\t" /* Place bit 5 in bit 0 of idx*/
  122. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  123. /* Bit 5 */
  124. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  125. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  126. " ubfx %[idx],%[txval],#4,#1" "\n\t" /* Place bit 4 in bit 0 of idx*/
  127. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  128. /* Bit 4 */
  129. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  130. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  131. " ubfx %[idx],%[txval],#3,#1" "\n\t" /* Place bit 3 in bit 0 of idx*/
  132. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  133. /* Bit 3 */
  134. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  135. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  136. " ubfx %[idx],%[txval],#2,#1" "\n\t" /* Place bit 2 in bit 0 of idx*/
  137. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  138. /* Bit 2 */
  139. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  140. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  141. " ubfx %[idx],%[txval],#1,#1" "\n\t" /* Place bit 1 in bit 0 of idx*/
  142. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  143. /* Bit 1 */
  144. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  145. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  146. " ubfx %[idx],%[txval],#0,#1" "\n\t" /* Place bit 0 in bit 0 of idx*/
  147. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  148. /* Bit 0 */
  149. " str %[mosi_mask],[%[mosi_port], %[idx],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  150. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  151. " nop" "\n\t" /* Result will be 0 */
  152. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  153. : [idx]"+r"( idx )
  154. : [txval]"r"( bout ) ,
  155. [mosi_mask]"r"( MOSI_MASK ),
  156. [mosi_port]"r"( MOSI_PORT_PLUS30 ),
  157. [sck_mask]"r"( SCK_MASK ),
  158. [sck_port]"r"( SCK_PORT_PLUS30 )
  159. : "cc"
  160. );
  161. return 0;
  162. }
  163. // Calculates the bit band alias address and returns a pointer address to word.
  164. // addr: The byte address of bitbanding bit.
  165. // bit: The bit position of bitbanding bit.
  166. #define BITBAND_ADDRESS(addr, bit) \
  167. (((uint32_t)(addr) & 0xF0000000) + 0x02000000 + ((uint32_t)(addr)&0xFFFFF)*32 + (bit)*4)
  168. // run at ~8 .. ~10Mhz - Rx version (Tx line not altered)
  169. static uint8_t spiTransferRx0(uint8_t bout) { // using Mode 0
  170. register uint32_t bin = 0;
  171. register uint32_t work = 0;
  172. register uint32_t BITBAND_MISO_PORT = BITBAND_ADDRESS( ((uint32_t)PORT(MISO_PIN))+0x3C, PIN_SHIFT(MISO_PIN)); /* PDSR of port in bitband area */
  173. register uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SCK_PIN)) + 0x30; /* SODR of port */
  174. register uint32_t SCK_MASK = PIN_MASK(SCK_PIN);
  175. UNUSED(bout);
  176. /* The software SPI routine */
  177. __asm__ __volatile__(
  178. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  179. /* bit 7 */
  180. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  181. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  182. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  183. " bfi %[bin],%[work],#7,#1" "\n\t" /* Store read bit as the bit 7 */
  184. /* bit 6 */
  185. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  186. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  187. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  188. " bfi %[bin],%[work],#6,#1" "\n\t" /* Store read bit as the bit 6 */
  189. /* bit 5 */
  190. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  191. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  192. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  193. " bfi %[bin],%[work],#5,#1" "\n\t" /* Store read bit as the bit 5 */
  194. /* bit 4 */
  195. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  196. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  197. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  198. " bfi %[bin],%[work],#4,#1" "\n\t" /* Store read bit as the bit 4 */
  199. /* bit 3 */
  200. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  201. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  202. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  203. " bfi %[bin],%[work],#3,#1" "\n\t" /* Store read bit as the bit 3 */
  204. /* bit 2 */
  205. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  206. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  207. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  208. " bfi %[bin],%[work],#2,#1" "\n\t" /* Store read bit as the bit 2 */
  209. /* bit 1 */
  210. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  211. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  212. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  213. " bfi %[bin],%[work],#1,#1" "\n\t" /* Store read bit as the bit 1 */
  214. /* bit 0 */
  215. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  216. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  217. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  218. " bfi %[bin],%[work],#0,#1" "\n\t" /* Store read bit as the bit 0 */
  219. : [bin]"+r"(bin),
  220. [work]"+r"(work)
  221. : [bitband_miso_port]"r"( BITBAND_MISO_PORT ),
  222. [sck_mask]"r"( SCK_MASK ),
  223. [sck_port]"r"( SCK_PORT_PLUS30 )
  224. : "cc"
  225. );
  226. return bin;
  227. }
  228. // run at ~4Mhz
  229. static uint8_t spiTransfer1(uint8_t b) { // using Mode 0
  230. int bits = 8;
  231. do {
  232. WRITE(MOSI_PIN, b & 0x80);
  233. b <<= 1; // little setup time
  234. WRITE(SCK_PIN, HIGH);
  235. DELAY_NS(125); // 10 cycles @ 84mhz
  236. b |= (READ(MISO_PIN) != 0);
  237. WRITE(SCK_PIN, LOW);
  238. DELAY_NS(125); // 10 cycles @ 84mhz
  239. } while (--bits);
  240. return b;
  241. }
  242. // all the others
  243. static uint32_t spiDelayCyclesX4 = (F_CPU/1000000); // 4uS => 125khz
  244. static uint8_t spiTransferX(uint8_t b) { // using Mode 0
  245. int bits = 8;
  246. do {
  247. WRITE(MOSI_PIN, b & 0x80);
  248. b <<= 1; // little setup time
  249. WRITE(SCK_PIN, HIGH);
  250. __delay_4cycles(spiDelayCyclesX4);
  251. b |= (READ(MISO_PIN) != 0);
  252. WRITE(SCK_PIN, LOW);
  253. __delay_4cycles(spiDelayCyclesX4);
  254. } while (--bits);
  255. return b;
  256. }
  257. // Pointers to generic functions for byte transfers
  258. static pfnSpiTransfer spiTransferTx = spiTransferX;
  259. static pfnSpiTransfer spiTransferRx = spiTransferX;
  260. // Block transfers run at ~8 .. ~10Mhz - Tx version (Rx data discarded)
  261. static void spiTxBlock0(const uint8_t* ptr, uint32_t todo) {
  262. register uint32_t MOSI_PORT_PLUS30 = ((uint32_t) PORT(MOSI_PIN)) + 0x30; /* SODR of port */
  263. register uint32_t MOSI_MASK = PIN_MASK(MOSI_PIN);
  264. register uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SCK_PIN)) + 0x30; /* SODR of port */
  265. register uint32_t SCK_MASK = PIN_MASK(SCK_PIN);
  266. register uint32_t work = 0;
  267. register uint32_t txval = 0;
  268. /* The software SPI routine */
  269. __asm__ __volatile__(
  270. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  271. " loop%=:" "\n\t"
  272. " ldrb.w %[txval], [%[ptr]], #1" "\n\t" /* Load value to send, increment buffer */
  273. " mvn %[txval],%[txval]" "\n\t" /* Negate value */
  274. /* Bit 7 */
  275. " ubfx %[work],%[txval],#7,#1" "\n\t" /* Place bit 7 in bit 0 of work*/
  276. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  277. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  278. " ubfx %[work],%[txval],#6,#1" "\n\t" /* Place bit 6 in bit 0 of work*/
  279. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  280. /* Bit 6 */
  281. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  282. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  283. " ubfx %[work],%[txval],#5,#1" "\n\t" /* Place bit 5 in bit 0 of work*/
  284. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  285. /* Bit 5 */
  286. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  287. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  288. " ubfx %[work],%[txval],#4,#1" "\n\t" /* Place bit 4 in bit 0 of work*/
  289. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  290. /* Bit 4 */
  291. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  292. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  293. " ubfx %[work],%[txval],#3,#1" "\n\t" /* Place bit 3 in bit 0 of work*/
  294. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  295. /* Bit 3 */
  296. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  297. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  298. " ubfx %[work],%[txval],#2,#1" "\n\t" /* Place bit 2 in bit 0 of work*/
  299. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  300. /* Bit 2 */
  301. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  302. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  303. " ubfx %[work],%[txval],#1,#1" "\n\t" /* Place bit 1 in bit 0 of work*/
  304. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  305. /* Bit 1 */
  306. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  307. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  308. " ubfx %[work],%[txval],#0,#1" "\n\t" /* Place bit 0 in bit 0 of work*/
  309. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  310. /* Bit 0 */
  311. " str %[mosi_mask],[%[mosi_port], %[work],LSL #2]" "\n\t" /* Access the proper SODR or CODR registers based on that bit */
  312. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  313. " subs %[todo],#1" "\n\t" /* Decrement count of pending words to send, update status */
  314. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  315. " bne.n loop%=" "\n\t" /* Repeat until done */
  316. : [ptr]"+r" ( ptr ) ,
  317. [todo]"+r" ( todo ) ,
  318. [work]"+r"( work ) ,
  319. [txval]"+r"( txval )
  320. : [mosi_mask]"r"( MOSI_MASK ),
  321. [mosi_port]"r"( MOSI_PORT_PLUS30 ),
  322. [sck_mask]"r"( SCK_MASK ),
  323. [sck_port]"r"( SCK_PORT_PLUS30 )
  324. : "cc"
  325. );
  326. }
  327. static void spiRxBlock0(uint8_t* ptr, uint32_t todo) {
  328. register uint32_t bin = 0;
  329. register uint32_t work = 0;
  330. register uint32_t BITBAND_MISO_PORT = BITBAND_ADDRESS( ((uint32_t)PORT(MISO_PIN))+0x3C, PIN_SHIFT(MISO_PIN)); /* PDSR of port in bitband area */
  331. register uint32_t SCK_PORT_PLUS30 = ((uint32_t) PORT(SCK_PIN)) + 0x30; /* SODR of port */
  332. register uint32_t SCK_MASK = PIN_MASK(SCK_PIN);
  333. /* The software SPI routine */
  334. __asm__ __volatile__(
  335. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  336. " loop%=:" "\n\t"
  337. /* bit 7 */
  338. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  339. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  340. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  341. " bfi %[bin],%[work],#7,#1" "\n\t" /* Store read bit as the bit 7 */
  342. /* bit 6 */
  343. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  344. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  345. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  346. " bfi %[bin],%[work],#6,#1" "\n\t" /* Store read bit as the bit 6 */
  347. /* bit 5 */
  348. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  349. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  350. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  351. " bfi %[bin],%[work],#5,#1" "\n\t" /* Store read bit as the bit 5 */
  352. /* bit 4 */
  353. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  354. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  355. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  356. " bfi %[bin],%[work],#4,#1" "\n\t" /* Store read bit as the bit 4 */
  357. /* bit 3 */
  358. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  359. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  360. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  361. " bfi %[bin],%[work],#3,#1" "\n\t" /* Store read bit as the bit 3 */
  362. /* bit 2 */
  363. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  364. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  365. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  366. " bfi %[bin],%[work],#2,#1" "\n\t" /* Store read bit as the bit 2 */
  367. /* bit 1 */
  368. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  369. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  370. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  371. " bfi %[bin],%[work],#1,#1" "\n\t" /* Store read bit as the bit 1 */
  372. /* bit 0 */
  373. " str %[sck_mask],[%[sck_port]]" "\n\t" /* SODR */
  374. " ldr %[work],[%[bitband_miso_port]]" "\n\t" /* PDSR on bitband area for required bit: work will be 1 or 0 based on port */
  375. " str %[sck_mask],[%[sck_port],#0x4]" "\n\t" /* CODR */
  376. " bfi %[bin],%[work],#0,#1" "\n\t" /* Store read bit as the bit 0 */
  377. " subs %[todo],#1" "\n\t" /* Decrement count of pending words to send, update status */
  378. " strb.w %[bin], [%[ptr]], #1" "\n\t" /* Store read value into buffer, increment buffer pointer */
  379. " bne.n loop%=" "\n\t" /* Repeat until done */
  380. : [ptr]"+r"(ptr),
  381. [todo]"+r"(todo),
  382. [bin]"+r"(bin),
  383. [work]"+r"(work)
  384. : [bitband_miso_port]"r"( BITBAND_MISO_PORT ),
  385. [sck_mask]"r"( SCK_MASK ),
  386. [sck_port]"r"( SCK_PORT_PLUS30 )
  387. : "cc"
  388. );
  389. }
  390. static void spiTxBlockX(const uint8_t* buf, uint32_t todo) {
  391. do {
  392. (void) spiTransferTx(*buf++);
  393. } while (--todo);
  394. }
  395. static void spiRxBlockX(uint8_t* buf, uint32_t todo) {
  396. do {
  397. *buf++ = spiTransferRx(0xff);
  398. } while (--todo);
  399. }
  400. // Pointers to generic functions for block tranfers
  401. static pfnSpiTxBlock spiTxBlock = spiTxBlockX;
  402. static pfnSpiRxBlock spiRxBlock = spiRxBlockX;
  403. #if MB(ALLIGATOR) // control SDSS pin
  404. void spiBegin() {
  405. SET_OUTPUT(SS_PIN);
  406. WRITE(SS_PIN, HIGH);
  407. SET_OUTPUT(SCK_PIN);
  408. SET_INPUT(MISO_PIN);
  409. SET_OUTPUT(MOSI_PIN);
  410. }
  411. uint8_t spiRec() {
  412. WRITE(SS_PIN, LOW);
  413. WRITE(MOSI_PIN, 1); /* Output 1s 1*/
  414. uint8_t b = spiTransferRx(0xFF);
  415. WRITE(SS_PIN, HIGH);
  416. return b;
  417. }
  418. void spiRead(uint8_t* buf, uint16_t nbyte) {
  419. uint32_t todo = nbyte;
  420. if (todo == 0) return;
  421. WRITE(SS_PIN, LOW);
  422. WRITE(MOSI_PIN, 1); /* Output 1s 1*/
  423. spiRxBlock(buf,nbyte);
  424. WRITE(SS_PIN, HIGH);
  425. }
  426. void spiSend(uint8_t b) {
  427. WRITE(SS_PIN, LOW);
  428. (void) spiTransferTx(b);
  429. WRITE(SS_PIN, HIGH);
  430. }
  431. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  432. WRITE(SS_PIN, LOW);
  433. (void) spiTransferTx(token);
  434. spiTxBlock(buf,512);
  435. WRITE(SS_PIN, HIGH);
  436. #else // let calling routine control SDSS
  437. void spiBegin() {
  438. SET_OUTPUT(SS_PIN);
  439. SET_OUTPUT(SCK_PIN);
  440. SET_INPUT(MISO_PIN);
  441. SET_OUTPUT(MOSI_PIN);
  442. }
  443. uint8_t spiRec() {
  444. WRITE(MOSI_PIN, 1); /* Output 1s 1*/
  445. uint8_t b = spiTransferRx(0xFF);
  446. return b;
  447. }
  448. void spiRead(uint8_t* buf, uint16_t nbyte) {
  449. uint32_t todo = nbyte;
  450. if (todo == 0) return;
  451. WRITE(MOSI_PIN, 1); /* Output 1s 1*/
  452. spiRxBlock(buf,nbyte);
  453. }
  454. void spiSend(uint8_t b) {
  455. (void) spiTransferTx(b);
  456. }
  457. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  458. (void) spiTransferTx(token);
  459. spiTxBlock(buf,512);
  460. #endif
  461. }
  462. /**
  463. * spiRate should be
  464. * 0 : 8 - 10 MHz
  465. * 1 : 4 - 5 MHz
  466. * 2 : 2 - 2.5 MHz
  467. * 3 : 1 - 1.25 MHz
  468. * 4 : 500 - 625 kHz
  469. * 5 : 250 - 312 kHz
  470. * 6 : 125 - 156 kHz
  471. */
  472. void spiInit(uint8_t spiRate) {
  473. switch (spiRate) {
  474. case 0:
  475. spiTransferTx = spiTransferTx0;
  476. spiTransferRx = spiTransferRx0;
  477. spiTxBlock = spiTxBlock0;
  478. spiRxBlock = spiRxBlock0;
  479. break;
  480. case 1:
  481. spiTransferTx = spiTransfer1;
  482. spiTransferRx = spiTransfer1;
  483. spiTxBlock = spiTxBlockX;
  484. spiRxBlock = spiRxBlockX;
  485. break;
  486. default:
  487. spiDelayCyclesX4 = (F_CPU/1000000) >> (6 - spiRate);
  488. spiTransferTx = spiTransferX;
  489. spiTransferRx = spiTransferX;
  490. spiTxBlock = spiTxBlockX;
  491. spiRxBlock = spiRxBlockX;
  492. break;
  493. }
  494. #if MB(ALLIGATOR)
  495. WRITE(SS_PIN, HIGH);
  496. #endif
  497. WRITE(MOSI_PIN, HIGH);
  498. WRITE(SCK_PIN, LOW);
  499. }
  500. /** Begin SPI transaction, set clock, bit order, data mode */
  501. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  502. // TODO: to be implemented
  503. }
  504. #pragma GCC reset_options
  505. #else
  506. #if MB(ALLIGATOR)
  507. // slave selects controlled by SPI controller
  508. // doesn't support changing SPI speeds for SD card
  509. // --------------------------------------------------------------------------
  510. // hardware SPI
  511. // --------------------------------------------------------------------------
  512. // 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
  513. int spiDueDividors[] = { 10, 21, 42, 84, 168, 255, 255 };
  514. bool spiInitMaded = false;
  515. void spiBegin() {
  516. if(spiInitMaded == false) {
  517. // Configure SPI pins
  518. PIO_Configure(
  519. g_APinDescription[SCK_PIN].pPort,
  520. g_APinDescription[SCK_PIN].ulPinType,
  521. g_APinDescription[SCK_PIN].ulPin,
  522. g_APinDescription[SCK_PIN].ulPinConfiguration);
  523. PIO_Configure(
  524. g_APinDescription[MOSI_PIN].pPort,
  525. g_APinDescription[MOSI_PIN].ulPinType,
  526. g_APinDescription[MOSI_PIN].ulPin,
  527. g_APinDescription[MOSI_PIN].ulPinConfiguration);
  528. PIO_Configure(
  529. g_APinDescription[MISO_PIN].pPort,
  530. g_APinDescription[MISO_PIN].ulPinType,
  531. g_APinDescription[MISO_PIN].ulPin,
  532. g_APinDescription[MISO_PIN].ulPinConfiguration);
  533. // set master mode, peripheral select, fault detection
  534. SPI_Configure(SPI0, ID_SPI0, SPI_MR_MSTR | SPI_MR_MODFDIS | SPI_MR_PS);
  535. SPI_Enable(SPI0);
  536. #if MB(ALLIGATOR)
  537. SET_OUTPUT(DAC0_SYNC);
  538. #if EXTRUDERS > 1
  539. SET_OUTPUT(DAC1_SYNC);
  540. WRITE(DAC1_SYNC, HIGH);
  541. #endif
  542. SET_OUTPUT(SPI_EEPROM1_CS);
  543. SET_OUTPUT(SPI_EEPROM2_CS);
  544. SET_OUTPUT(SPI_FLASH_CS);
  545. WRITE(DAC0_SYNC, HIGH);
  546. WRITE(SPI_EEPROM1_CS, HIGH );
  547. WRITE(SPI_EEPROM2_CS, HIGH );
  548. WRITE(SPI_FLASH_CS, HIGH );
  549. WRITE(SS_PIN, HIGH );
  550. #endif // MB(ALLIGATOR)
  551. OUT_WRITE(SDSS,0);
  552. PIO_Configure(
  553. g_APinDescription[SPI_PIN].pPort,
  554. g_APinDescription[SPI_PIN].ulPinType,
  555. g_APinDescription[SPI_PIN].ulPin,
  556. g_APinDescription[SPI_PIN].ulPinConfiguration);
  557. spiInit(1);
  558. spiInitMaded = true;
  559. }
  560. }
  561. void spiInit(uint8_t spiRate) {
  562. if(spiInitMaded == false) {
  563. if(spiRate > 6) spiRate = 1;
  564. #if MB(ALLIGATOR)
  565. // Set SPI mode 1, clock, select not active after transfer, with delay between transfers
  566. SPI_ConfigureNPCS(SPI0, SPI_CHAN_DAC,
  567. SPI_CSR_CSAAT | SPI_CSR_SCBR(spiDueDividors[spiRate]) |
  568. SPI_CSR_DLYBCT(1));
  569. // Set SPI mode 0, clock, select not active after transfer, with delay between transfers
  570. SPI_ConfigureNPCS(SPI0, SPI_CHAN_EEPROM1, SPI_CSR_NCPHA |
  571. SPI_CSR_CSAAT | SPI_CSR_SCBR(spiDueDividors[spiRate]) |
  572. SPI_CSR_DLYBCT(1));
  573. #endif//MB(ALLIGATOR)
  574. // Set SPI mode 0, clock, select not active after transfer, with delay between transfers
  575. SPI_ConfigureNPCS(SPI0, SPI_CHAN, SPI_CSR_NCPHA |
  576. SPI_CSR_CSAAT | SPI_CSR_SCBR(spiDueDividors[spiRate]) |
  577. SPI_CSR_DLYBCT(1));
  578. SPI_Enable(SPI0);
  579. spiInitMaded = true;
  580. }
  581. }
  582. // Write single byte to SPI
  583. void spiSend(byte b) {
  584. // write byte with address and end transmission flag
  585. SPI0->SPI_TDR = (uint32_t)b | SPI_PCS(SPI_CHAN) | SPI_TDR_LASTXFER;
  586. // wait for transmit register empty
  587. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  588. // wait for receive register
  589. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  590. // clear status
  591. SPI0->SPI_RDR;
  592. //delayMicroseconds(1U);
  593. }
  594. void spiSend(const uint8_t* buf, size_t n) {
  595. if (n == 0) return;
  596. for (size_t i = 0; i < n - 1; i++) {
  597. SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(SPI_CHAN);
  598. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  599. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  600. SPI0->SPI_RDR;
  601. //delayMicroseconds(1U);
  602. }
  603. spiSend(buf[n - 1]);
  604. }
  605. void spiSend(uint32_t chan, byte b) {
  606. uint8_t dummy_read = 0;
  607. // wait for transmit register empty
  608. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  609. // write byte with address and end transmission flag
  610. SPI0->SPI_TDR = (uint32_t)b | SPI_PCS(chan) | SPI_TDR_LASTXFER;
  611. // wait for receive register
  612. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  613. // clear status
  614. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 1)
  615. dummy_read = SPI0->SPI_RDR;
  616. UNUSED(dummy_read);
  617. }
  618. void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
  619. uint8_t dummy_read = 0;
  620. if (n == 0) return;
  621. for (int i = 0; i < (int)n - 1; i++) {
  622. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  623. SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(chan);
  624. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  625. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 1)
  626. dummy_read = SPI0->SPI_RDR;
  627. UNUSED(dummy_read);
  628. }
  629. spiSend(chan, buf[n - 1]);
  630. }
  631. // Read single byte from SPI
  632. uint8_t spiRec() {
  633. // write dummy byte with address and end transmission flag
  634. SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN) | SPI_TDR_LASTXFER;
  635. // wait for transmit register empty
  636. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  637. // wait for receive register
  638. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  639. // get byte from receive register
  640. //delayMicroseconds(1U);
  641. return SPI0->SPI_RDR;
  642. }
  643. uint8_t spiRec(uint32_t chan) {
  644. uint8_t spirec_tmp;
  645. // wait for transmit register empty
  646. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  647. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 1)
  648. spirec_tmp = SPI0->SPI_RDR;
  649. UNUSED(spirec_tmp);
  650. // write dummy byte with address and end transmission flag
  651. SPI0->SPI_TDR = 0x000000FF | SPI_PCS(chan) | SPI_TDR_LASTXFER;
  652. // wait for receive register
  653. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  654. // get byte from receive register
  655. return SPI0->SPI_RDR;
  656. }
  657. // Read from SPI into buffer
  658. void spiRead(uint8_t*buf, uint16_t nbyte) {
  659. if (nbyte-- == 0) return;
  660. for (int i = 0; i < nbyte; i++) {
  661. //while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  662. SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN);
  663. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  664. buf[i] = SPI0->SPI_RDR;
  665. //delayMicroseconds(1U);
  666. }
  667. buf[nbyte] = spiRec();
  668. }
  669. // Write from buffer to SPI
  670. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  671. SPI0->SPI_TDR = (uint32_t)token | SPI_PCS(SPI_CHAN);
  672. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  673. //while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  674. //SPI0->SPI_RDR;
  675. for (int i = 0; i < 511; i++) {
  676. SPI0->SPI_TDR = (uint32_t)buf[i] | SPI_PCS(SPI_CHAN);
  677. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  678. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  679. SPI0->SPI_RDR;
  680. //delayMicroseconds(1U);
  681. }
  682. spiSend(buf[511]);
  683. }
  684. /** Begin SPI transaction, set clock, bit order, data mode */
  685. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  686. // TODO: to be implemented
  687. }
  688. #else // U8G compatible hardware SPI
  689. void spiInit(uint8_t spiRate = 6 ) { // default to slowest rate if not specified)
  690. // 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
  691. int spiDueDividors[] = { 10, 21, 42, 84, 168, 255, 255 };
  692. if(spiRate > 6) spiRate = 1;
  693. /* enable PIOA and SPI0 */
  694. REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
  695. /* disable PIO on A26 and A27 */
  696. REG_PIOA_PDR = 0x0c000000;
  697. OUT_WRITE(SDSS, 1);
  698. /* reset SPI0 (from sam lib) */
  699. SPI0->SPI_CR = SPI_CR_SPIDIS;
  700. SPI0->SPI_CR = SPI_CR_SWRST;
  701. SPI0->SPI_CR = SPI_CR_SWRST;
  702. SPI0->SPI_CR = SPI_CR_SPIEN;
  703. /* master mode, no fault detection, chip select 0 */
  704. SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PCSDEC | SPI_MR_MODFDIS;
  705. /* SPI mode 0, 8 Bit data transfer, baud rate */
  706. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(spiDueDividors[spiRate]) | 1;
  707. }
  708. static uint8_t spiTransfer(uint8_t data) {
  709. /* wait until tx register is empty */
  710. while( (SPI0->SPI_SR & SPI_SR_TDRE) == 0 );
  711. /* send data */
  712. SPI0->SPI_TDR = (uint32_t)data; // | SPI_PCS(0xF);
  713. // wait for transmit register empty
  714. while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
  715. // wait for receive register
  716. while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
  717. // get byte from receive register
  718. return SPI0->SPI_RDR;
  719. }
  720. void spiBegin() {
  721. spiInit();
  722. }
  723. uint8_t spiRec() {
  724. uint8_t data = spiTransfer(0xff);
  725. return data;
  726. }
  727. void spiRead(uint8_t*buf, uint16_t nbyte) {
  728. if (nbyte == 0) return;
  729. for (int i = 0; i < nbyte; i++) {
  730. buf[i] = spiTransfer(0xff);
  731. }
  732. }
  733. void spiSend(uint8_t data) {
  734. spiTransfer(data);
  735. }
  736. void spiSend(const uint8_t* buf, size_t n) {
  737. if (n == 0) return;
  738. for (uint16_t i = 0; i < n; i++)
  739. spiTransfer(buf[i]);
  740. }
  741. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  742. spiTransfer(token);
  743. for (uint16_t i = 0; i < 512; i++)
  744. spiTransfer(buf[i]);
  745. }
  746. #endif //MB(ALLIGATOR)
  747. #endif // ENABLED(SOFTWARE_SPI)
  748. #endif // ARDUINO_ARCH_SAM