Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SPI.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
  3. * SPI Master library for arduino.
  4. *
  5. * This file is free software; you can redistribute it and/or modify
  6. * it under the terms of either the GNU General Public License version 2
  7. * or the GNU Lesser General Public License version 2.1, both as
  8. * published by the Free Software Foundation.
  9. */
  10. #include "SPI.h"
  11. #include "pins_arduino.h"
  12. /**********************************************************/
  13. /* 8 bit AVR-based boards */
  14. /**********************************************************/
  15. #if defined(__AVR__)
  16. SPIClass SPI;
  17. uint8_t SPIClass::interruptMode = 0;
  18. uint8_t SPIClass::interruptMask = 0;
  19. uint8_t SPIClass::interruptSave = 0;
  20. #ifdef SPI_TRANSACTION_MISMATCH_LED
  21. uint8_t SPIClass::inTransactionFlag = 0;
  22. #endif
  23. void SPIClass::begin()
  24. {
  25. // Set SS to high so a connected chip will be "deselected" by default
  26. digitalWrite(SS, HIGH);
  27. // When the SS pin is set as OUTPUT, it can be used as
  28. // a general purpose output port (it doesn't influence
  29. // SPI operations).
  30. pinMode(SS, OUTPUT);
  31. // Warning: if the SS pin ever becomes a LOW INPUT then SPI
  32. // automatically switches to Slave, so the data direction of
  33. // the SS pin MUST be kept as OUTPUT.
  34. SPCR |= _BV(MSTR);
  35. SPCR |= _BV(SPE);
  36. // Set direction register for SCK and MOSI pin.
  37. // MISO pin automatically overrides to INPUT.
  38. // By doing this AFTER enabling SPI, we avoid accidentally
  39. // clocking in a single bit since the lines go directly
  40. // from "input" to SPI control.
  41. // http://code.google.com/p/arduino/issues/detail?id=888
  42. pinMode(SCK, OUTPUT);
  43. pinMode(MOSI, OUTPUT);
  44. }
  45. void SPIClass::end() {
  46. SPCR &= ~_BV(SPE);
  47. }
  48. // mapping of interrupt numbers to bits within SPI_AVR_EIMSK
  49. #if defined(__AVR_ATmega32U4__)
  50. #define SPI_INT0_MASK (1<<INT0)
  51. #define SPI_INT1_MASK (1<<INT1)
  52. #define SPI_INT2_MASK (1<<INT2)
  53. #define SPI_INT3_MASK (1<<INT3)
  54. #define SPI_INT4_MASK (1<<INT6)
  55. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  56. #define SPI_INT0_MASK (1<<INT0)
  57. #define SPI_INT1_MASK (1<<INT1)
  58. #define SPI_INT2_MASK (1<<INT2)
  59. #define SPI_INT3_MASK (1<<INT3)
  60. #define SPI_INT4_MASK (1<<INT4)
  61. #define SPI_INT5_MASK (1<<INT5)
  62. #define SPI_INT6_MASK (1<<INT6)
  63. #define SPI_INT7_MASK (1<<INT7)
  64. #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  65. #define SPI_INT0_MASK (1<<INT4)
  66. #define SPI_INT1_MASK (1<<INT5)
  67. #define SPI_INT2_MASK (1<<INT0)
  68. #define SPI_INT3_MASK (1<<INT1)
  69. #define SPI_INT4_MASK (1<<INT2)
  70. #define SPI_INT5_MASK (1<<INT3)
  71. #define SPI_INT6_MASK (1<<INT6)
  72. #define SPI_INT7_MASK (1<<INT7)
  73. #else
  74. #ifdef INT0
  75. #define SPI_INT0_MASK (1<<INT0)
  76. #endif
  77. #ifdef INT1
  78. #define SPI_INT1_MASK (1<<INT1)
  79. #endif
  80. #ifdef INT2
  81. #define SPI_INT2_MASK (1<<INT2)
  82. #endif
  83. #endif
  84. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  85. {
  86. uint8_t stmp, mask;
  87. if (interruptMode > 1) return;
  88. stmp = SREG;
  89. noInterrupts();
  90. switch (interruptNumber) {
  91. #ifdef SPI_INT0_MASK
  92. case 0: mask = SPI_INT0_MASK; break;
  93. #endif
  94. #ifdef SPI_INT1_MASK
  95. case 1: mask = SPI_INT1_MASK; break;
  96. #endif
  97. #ifdef SPI_INT2_MASK
  98. case 2: mask = SPI_INT2_MASK; break;
  99. #endif
  100. #ifdef SPI_INT3_MASK
  101. case 3: mask = SPI_INT3_MASK; break;
  102. #endif
  103. #ifdef SPI_INT4_MASK
  104. case 4: mask = SPI_INT4_MASK; break;
  105. #endif
  106. #ifdef SPI_INT5_MASK
  107. case 5: mask = SPI_INT5_MASK; break;
  108. #endif
  109. #ifdef SPI_INT6_MASK
  110. case 6: mask = SPI_INT6_MASK; break;
  111. #endif
  112. #ifdef SPI_INT7_MASK
  113. case 7: mask = SPI_INT7_MASK; break;
  114. #endif
  115. default:
  116. interruptMode = 2;
  117. SREG = stmp;
  118. return;
  119. }
  120. interruptMode = 1;
  121. interruptMask |= mask;
  122. SREG = stmp;
  123. }
  124. /**********************************************************/
  125. /* 32 bit Teensy 3.0 and 3.1 */
  126. /**********************************************************/
  127. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
  128. SPIClass SPI;
  129. uint8_t SPIClass::interruptMasksUsed = 0;
  130. uint32_t SPIClass::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  131. uint32_t SPIClass::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  132. #ifdef SPI_TRANSACTION_MISMATCH_LED
  133. uint8_t SPIClass::inTransactionFlag = 0;
  134. #endif
  135. void SPIClass::begin()
  136. {
  137. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  138. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  139. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  140. SPI0_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  141. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  142. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  143. }
  144. void SPIClass::end() {
  145. SPCR.disable_pins();
  146. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  147. }
  148. void SPIClass::usingInterrupt(IRQ_NUMBER_t interruptName)
  149. {
  150. uint32_t n = (uint32_t)interruptName;
  151. if (n >= NVIC_NUM_INTERRUPTS) return;
  152. //Serial.print("usingInterrupt ");
  153. //Serial.println(n);
  154. interruptMasksUsed |= (1 << (n >> 5));
  155. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  156. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  157. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  158. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  159. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  160. }
  161. void SPIClass::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  162. {
  163. uint32_t n = (uint32_t)interruptName;
  164. if (n >= NVIC_NUM_INTERRUPTS) return;
  165. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  166. if (interruptMask[n >> 5] == 0) {
  167. interruptMasksUsed &= ~(1 << (n >> 5));
  168. }
  169. }
  170. const uint16_t SPISettings::ctar_div_table[23] = {
  171. 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40,
  172. 56, 64, 96, 128, 192, 256, 384, 512, 640, 768
  173. };
  174. const uint32_t SPISettings::ctar_clock_table[23] = {
  175. SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  176. SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  177. SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  178. SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  179. SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  180. SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1),
  181. SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  182. SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1),
  183. SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  184. SPI_CTAR_PBR(2) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(0),
  185. SPI_CTAR_PBR(1) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  186. SPI_CTAR_PBR(0) | SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(3),
  187. SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  188. SPI_CTAR_PBR(3) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  189. SPI_CTAR_PBR(0) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4),
  190. SPI_CTAR_PBR(1) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4),
  191. SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5),
  192. SPI_CTAR_PBR(1) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5),
  193. SPI_CTAR_PBR(0) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  194. SPI_CTAR_PBR(1) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  195. SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7),
  196. SPI_CTAR_PBR(2) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  197. SPI_CTAR_PBR(1) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7)
  198. };
  199. static void updateCTAR(uint32_t ctar)
  200. {
  201. if (SPI0_CTAR0 != ctar) {
  202. uint32_t mcr = SPI0_MCR;
  203. if (mcr & SPI_MCR_MDIS) {
  204. SPI0_CTAR0 = ctar;
  205. SPI0_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  206. } else {
  207. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  208. SPI0_CTAR0 = ctar;
  209. SPI0_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  210. SPI0_MCR = mcr;
  211. }
  212. }
  213. }
  214. void SPIClass::setBitOrder(uint8_t bitOrder)
  215. {
  216. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  217. uint32_t ctar = SPI0_CTAR0;
  218. if (bitOrder == LSBFIRST) {
  219. ctar |= SPI_CTAR_LSBFE;
  220. } else {
  221. ctar &= ~SPI_CTAR_LSBFE;
  222. }
  223. updateCTAR(ctar);
  224. }
  225. void SPIClass::setDataMode(uint8_t dataMode)
  226. {
  227. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  228. // TODO: implement with native code
  229. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  230. }
  231. void SPIClass::setClockDivider_noInline(uint32_t clk)
  232. {
  233. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  234. uint32_t ctar = SPI0_CTAR0;
  235. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  236. if (ctar & SPI_CTAR_CPHA) {
  237. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  238. }
  239. ctar |= clk;
  240. updateCTAR(ctar);
  241. }
  242. bool SPIClass::pinIsChipSelect(uint8_t pin)
  243. {
  244. if (pin == 10 || pin == 9 || pin == 6 || pin == 2 || pin == 15) return true;
  245. if (pin >= 20 && pin <= 23) return true;
  246. return false;
  247. }
  248. bool SPIClass::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  249. {
  250. if (!pinIsChipSelect(pin1) || !pinIsChipSelect(pin2)) return false;
  251. if ((pin1 == 2 && pin2 == 10) || (pin1 == 10 && pin2 == 2)) return false;
  252. if ((pin1 == 6 && pin2 == 9) || (pin1 == 9 && pin2 == 6)) return false;
  253. if ((pin1 == 20 && pin2 == 23) || (pin1 == 23 && pin2 == 20)) return false;
  254. if ((pin1 == 21 && pin2 == 22) || (pin1 == 22 && pin2 == 21)) return false;
  255. return true;
  256. }
  257. uint8_t SPIClass::setCS(uint8_t pin)
  258. {
  259. switch (pin) {
  260. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  261. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  262. case 9: CORE_PIN9_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTC3
  263. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTD4
  264. case 20: CORE_PIN20_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTD5
  265. case 23: CORE_PIN23_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTC2
  266. case 21: CORE_PIN21_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTD6
  267. case 22: CORE_PIN22_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTC1
  268. case 15: CORE_PIN15_CONFIG = PORT_PCR_MUX(2); return 0x10; // PTC0
  269. }
  270. return 0;
  271. }
  272. /**********************************************************/
  273. /* 32 bit Teensy-3.4/3.5 */
  274. /**********************************************************/
  275. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  276. SPI1Class SPI1;
  277. uint8_t SPI1Class::interruptMasksUsed = 0;
  278. uint32_t SPI1Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  279. uint32_t SPI1Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  280. #ifdef SPI_TRANSACTION_MISMATCH_LED
  281. uint8_t SPI1Class::inTransactionFlag = 0;
  282. #endif
  283. void SPI1Class::begin()
  284. {
  285. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  286. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  287. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  288. SPI1_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  289. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  290. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  291. }
  292. void SPI1Class::end() {
  293. SPCR1.disable_pins();
  294. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  295. }
  296. void SPI1Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  297. {
  298. uint32_t n = (uint32_t)interruptName;
  299. if (n >= NVIC_NUM_INTERRUPTS) return;
  300. //Serial.print("usingInterrupt ");
  301. //Serial.println(n);
  302. interruptMasksUsed |= (1 << (n >> 5));
  303. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  304. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  305. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  306. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  307. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  308. }
  309. void SPI1Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  310. {
  311. uint32_t n = (uint32_t)interruptName;
  312. if (n >= NVIC_NUM_INTERRUPTS) return;
  313. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  314. if (interruptMask[n >> 5] == 0) {
  315. interruptMasksUsed &= ~(1 << (n >> 5));
  316. }
  317. }
  318. static void updateCTAR1(uint32_t ctar)
  319. {
  320. if (SPI1_CTAR0 != ctar) {
  321. uint32_t mcr = SPI1_MCR;
  322. if (mcr & SPI_MCR_MDIS) {
  323. SPI1_CTAR0 = ctar;
  324. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  325. } else {
  326. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  327. SPI1_CTAR0 = ctar;
  328. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  329. SPI1_MCR = mcr;
  330. }
  331. }
  332. }
  333. void SPI1Class::setBitOrder(uint8_t bitOrder)
  334. {
  335. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  336. uint32_t ctar = SPI1_CTAR0;
  337. if (bitOrder == LSBFIRST) {
  338. ctar |= SPI_CTAR_LSBFE;
  339. } else {
  340. ctar &= ~SPI_CTAR_LSBFE;
  341. }
  342. updateCTAR1(ctar);
  343. }
  344. void SPI1Class::setDataMode(uint8_t dataMode)
  345. {
  346. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  347. // TODO: implement with native code
  348. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  349. }
  350. void SPI1Class::setClockDivider_noInline(uint32_t clk)
  351. {
  352. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  353. uint32_t ctar = SPI1_CTAR0;
  354. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  355. if (ctar & SPI_CTAR_CPHA) {
  356. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  357. }
  358. ctar |= clk;
  359. updateCTAR1(ctar);
  360. }
  361. bool SPI1Class::pinIsChipSelect(uint8_t pin)
  362. {
  363. if (pin == 6 || pin == 31) return true;
  364. return false;
  365. }
  366. bool SPI1Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  367. {
  368. return false; // only one CS bith 6 and 31 or logially the same.
  369. }
  370. uint8_t SPI1Class::setCS(uint8_t pin)
  371. {
  372. switch (pin) {
  373. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(7); return 0x01; // PTD4
  374. case 31: CORE_PIN31_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD5
  375. }
  376. return 0;
  377. }
  378. #endif
  379. /**********************************************************/
  380. /* 32 bit Teensy-LC */
  381. /**********************************************************/
  382. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  383. SPIClass SPI;
  384. SPI1Class SPI1;
  385. uint32_t SPIClass::interruptMask = 0;
  386. uint32_t SPIClass::interruptSave = 0;
  387. uint32_t SPI1Class::interruptMask = 0;
  388. uint32_t SPI1Class::interruptSave = 0;
  389. #ifdef SPI_TRANSACTION_MISMATCH_LED
  390. uint8_t SPIClass::inTransactionFlag = 0;
  391. uint8_t SPI1Class::inTransactionFlag = 0;
  392. #endif
  393. void SPIClass::begin()
  394. {
  395. SIM_SCGC4 |= SIM_SCGC4_SPI0;
  396. SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  397. SPI0_C2 = 0;
  398. uint8_t tmp __attribute__((unused)) = SPI0_S;
  399. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  400. }
  401. void SPIClass::end() {
  402. SPCR.disable_pins();
  403. SPI0_C1 = 0;
  404. }
  405. const uint16_t SPISettings::br_div_table[30] = {
  406. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24,
  407. 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
  408. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768,
  409. };
  410. const uint8_t SPISettings::br_clock_table[30] = {
  411. SPI_BR_SPPR(0) | SPI_BR_SPR(0),
  412. SPI_BR_SPPR(1) | SPI_BR_SPR(0),
  413. SPI_BR_SPPR(2) | SPI_BR_SPR(0),
  414. SPI_BR_SPPR(3) | SPI_BR_SPR(0),
  415. SPI_BR_SPPR(4) | SPI_BR_SPR(0),
  416. SPI_BR_SPPR(5) | SPI_BR_SPR(0),
  417. SPI_BR_SPPR(6) | SPI_BR_SPR(0),
  418. SPI_BR_SPPR(7) | SPI_BR_SPR(0),
  419. SPI_BR_SPPR(4) | SPI_BR_SPR(1),
  420. SPI_BR_SPPR(5) | SPI_BR_SPR(1),
  421. SPI_BR_SPPR(6) | SPI_BR_SPR(1),
  422. SPI_BR_SPPR(7) | SPI_BR_SPR(1),
  423. SPI_BR_SPPR(4) | SPI_BR_SPR(2),
  424. SPI_BR_SPPR(5) | SPI_BR_SPR(2),
  425. SPI_BR_SPPR(6) | SPI_BR_SPR(2),
  426. SPI_BR_SPPR(7) | SPI_BR_SPR(2),
  427. SPI_BR_SPPR(4) | SPI_BR_SPR(3),
  428. SPI_BR_SPPR(5) | SPI_BR_SPR(3),
  429. SPI_BR_SPPR(6) | SPI_BR_SPR(3),
  430. SPI_BR_SPPR(7) | SPI_BR_SPR(3),
  431. SPI_BR_SPPR(4) | SPI_BR_SPR(4),
  432. SPI_BR_SPPR(5) | SPI_BR_SPR(4),
  433. SPI_BR_SPPR(6) | SPI_BR_SPR(4),
  434. SPI_BR_SPPR(7) | SPI_BR_SPR(4),
  435. SPI_BR_SPPR(4) | SPI_BR_SPR(5),
  436. SPI_BR_SPPR(5) | SPI_BR_SPR(5),
  437. SPI_BR_SPPR(6) | SPI_BR_SPR(5),
  438. SPI_BR_SPPR(7) | SPI_BR_SPR(5),
  439. SPI_BR_SPPR(4) | SPI_BR_SPR(6),
  440. SPI_BR_SPPR(5) | SPI_BR_SPR(6)
  441. };
  442. uint8_t SPIClass::setCS(uint8_t pin)
  443. {
  444. switch (pin) {
  445. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  446. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  447. }
  448. return 0;
  449. }
  450. void SPI1Class::begin()
  451. {
  452. SIM_SCGC4 |= SIM_SCGC4_SPI1;
  453. SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  454. SPI1_C2 = 0;
  455. uint8_t tmp __attribute__((unused)) = SPI1_S;
  456. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  457. }
  458. void SPI1Class::end() {
  459. SPCR1.disable_pins();
  460. SPI1_C1 = 0;
  461. }
  462. uint8_t SPI1Class::setCS(uint8_t pin)
  463. {
  464. switch (pin) {
  465. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
  466. }
  467. return 0;
  468. }
  469. /**********************************************************/
  470. /* 32 bit Arduino Due */
  471. /**********************************************************/
  472. #elif defined(__arm__) && defined(__SAM3X8E__)
  473. #include "SPI.h"
  474. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  475. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  476. {
  477. // Empty
  478. }
  479. void SPIClass::begin() {
  480. init();
  481. // NPCS control is left to the user
  482. // Default speed set to 4Mhz
  483. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  484. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  485. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  486. }
  487. void SPIClass::begin(uint8_t _pin) {
  488. init();
  489. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  490. PIO_Configure(
  491. g_APinDescription[spiPin].pPort,
  492. g_APinDescription[spiPin].ulPinType,
  493. g_APinDescription[spiPin].ulPin,
  494. g_APinDescription[spiPin].ulPinConfiguration);
  495. // Default speed set to 4Mhz
  496. setClockDivider(_pin, 21);
  497. setDataMode(_pin, SPI_MODE0);
  498. setBitOrder(_pin, MSBFIRST);
  499. }
  500. void SPIClass::init() {
  501. if (initialized)
  502. return;
  503. interruptMode = 0;
  504. interruptMask = 0;
  505. interruptSave = 0;
  506. initCb();
  507. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  508. SPI_Enable(spi);
  509. initialized = true;
  510. }
  511. #ifndef interruptsStatus
  512. #define interruptsStatus() __interruptsStatus()
  513. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  514. static inline unsigned char __interruptsStatus(void) {
  515. unsigned int primask;
  516. asm volatile ("mrs %0, primask" : "=r" (primask));
  517. if (primask) return 0;
  518. return 1;
  519. }
  520. #endif
  521. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  522. {
  523. uint8_t irestore;
  524. irestore = interruptsStatus();
  525. noInterrupts();
  526. if (interruptMode < 2) {
  527. if (interruptNumber > NUM_DIGITAL_PINS) {
  528. interruptMode = 2;
  529. } else {
  530. uint8_t imask = interruptMask;
  531. Pio *pio = g_APinDescription[interruptNumber].pPort;
  532. if (pio == PIOA) {
  533. imask |= 1;
  534. } else if (pio == PIOB) {
  535. imask |= 2;
  536. } else if (pio == PIOC) {
  537. imask |= 4;
  538. } else if (pio == PIOD) {
  539. imask |= 8;
  540. }
  541. interruptMask = imask;
  542. interruptMode = 1;
  543. }
  544. }
  545. if (irestore) interrupts();
  546. }
  547. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  548. {
  549. if (interruptMode > 0) {
  550. if (interruptMode == 1) {
  551. uint8_t imask = interruptMask;
  552. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  553. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  554. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  555. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  556. } else {
  557. interruptSave = interruptsStatus();
  558. noInterrupts();
  559. }
  560. }
  561. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  562. bitOrder[ch] = settings.border;
  563. SPI_ConfigureNPCS(spi, ch, settings.config);
  564. }
  565. void SPIClass::endTransaction(void)
  566. {
  567. if (interruptMode > 0) {
  568. if (interruptMode == 1) {
  569. uint8_t imask = interruptMask;
  570. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  571. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  572. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  573. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  574. } else {
  575. if (interruptSave) interrupts();
  576. }
  577. }
  578. }
  579. void SPIClass::end(uint8_t _pin) {
  580. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  581. // Setting the pin as INPUT will disconnect it from SPI peripheral
  582. pinMode(spiPin, INPUT);
  583. }
  584. void SPIClass::end() {
  585. SPI_Disable(spi);
  586. initialized = false;
  587. }
  588. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  589. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  590. bitOrder[ch] = _bitOrder;
  591. }
  592. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  593. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  594. mode[ch] = _mode | SPI_CSR_CSAAT;
  595. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  596. // transfer. Some device needs that for working properly.
  597. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  598. }
  599. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  600. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  601. divider[ch] = _divider;
  602. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  603. // transfer. Some device needs that for working properly.
  604. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  605. }
  606. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  607. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  608. // Reverse bit order
  609. if (bitOrder[ch] == LSBFIRST)
  610. _data = __REV(__RBIT(_data));
  611. uint32_t d = _data | SPI_PCS(ch);
  612. if (_mode == SPI_LAST)
  613. d |= SPI_TDR_LASTXFER;
  614. // SPI_Write(spi, _channel, _data);
  615. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  616. ;
  617. spi->SPI_TDR = d;
  618. // return SPI_Read(spi);
  619. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  620. ;
  621. d = spi->SPI_RDR;
  622. // Reverse bit order
  623. if (bitOrder[ch] == LSBFIRST)
  624. d = __REV(__RBIT(d));
  625. return d & 0xFF;
  626. }
  627. void SPIClass::attachInterrupt(void) {
  628. // Should be enableInterrupt()
  629. }
  630. void SPIClass::detachInterrupt(void) {
  631. // Should be disableInterrupt()
  632. }
  633. #if SPI_INTERFACES_COUNT > 0
  634. static void SPI_0_Init(void) {
  635. PIO_Configure(
  636. g_APinDescription[PIN_SPI_MOSI].pPort,
  637. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  638. g_APinDescription[PIN_SPI_MOSI].ulPin,
  639. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  640. PIO_Configure(
  641. g_APinDescription[PIN_SPI_MISO].pPort,
  642. g_APinDescription[PIN_SPI_MISO].ulPinType,
  643. g_APinDescription[PIN_SPI_MISO].ulPin,
  644. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  645. PIO_Configure(
  646. g_APinDescription[PIN_SPI_SCK].pPort,
  647. g_APinDescription[PIN_SPI_SCK].ulPinType,
  648. g_APinDescription[PIN_SPI_SCK].ulPin,
  649. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  650. }
  651. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  652. #endif
  653. #endif