Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
9 лет назад
10 лет назад
10 лет назад
10 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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-LC */
  274. /**********************************************************/
  275. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  276. SPIClass SPI;
  277. SPI1Class SPI1;
  278. uint32_t SPIClass::interruptMask = 0;
  279. uint32_t SPIClass::interruptSave = 0;
  280. uint32_t SPI1Class::interruptMask = 0;
  281. uint32_t SPI1Class::interruptSave = 0;
  282. #ifdef SPI_TRANSACTION_MISMATCH_LED
  283. uint8_t SPIClass::inTransactionFlag = 0;
  284. uint8_t SPI1Class::inTransactionFlag = 0;
  285. #endif
  286. void SPIClass::begin()
  287. {
  288. SIM_SCGC4 |= SIM_SCGC4_SPI0;
  289. SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  290. SPI0_C2 = 0;
  291. uint8_t tmp __attribute__((unused)) = SPI0_S;
  292. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  293. }
  294. void SPIClass::end() {
  295. SPCR.disable_pins();
  296. SPI0_C1 = 0;
  297. }
  298. const uint16_t SPISettings::br_div_table[30] = {
  299. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24,
  300. 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
  301. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768,
  302. };
  303. const uint8_t SPISettings::br_clock_table[30] = {
  304. SPI_BR_SPPR(0) | SPI_BR_SPR(0),
  305. SPI_BR_SPPR(1) | SPI_BR_SPR(0),
  306. SPI_BR_SPPR(2) | SPI_BR_SPR(0),
  307. SPI_BR_SPPR(3) | SPI_BR_SPR(0),
  308. SPI_BR_SPPR(4) | SPI_BR_SPR(0),
  309. SPI_BR_SPPR(5) | SPI_BR_SPR(0),
  310. SPI_BR_SPPR(6) | SPI_BR_SPR(0),
  311. SPI_BR_SPPR(7) | SPI_BR_SPR(0),
  312. SPI_BR_SPPR(4) | SPI_BR_SPR(1),
  313. SPI_BR_SPPR(5) | SPI_BR_SPR(1),
  314. SPI_BR_SPPR(6) | SPI_BR_SPR(1),
  315. SPI_BR_SPPR(7) | SPI_BR_SPR(1),
  316. SPI_BR_SPPR(4) | SPI_BR_SPR(2),
  317. SPI_BR_SPPR(5) | SPI_BR_SPR(2),
  318. SPI_BR_SPPR(6) | SPI_BR_SPR(2),
  319. SPI_BR_SPPR(7) | SPI_BR_SPR(2),
  320. SPI_BR_SPPR(4) | SPI_BR_SPR(3),
  321. SPI_BR_SPPR(5) | SPI_BR_SPR(3),
  322. SPI_BR_SPPR(6) | SPI_BR_SPR(3),
  323. SPI_BR_SPPR(7) | SPI_BR_SPR(3),
  324. SPI_BR_SPPR(4) | SPI_BR_SPR(4),
  325. SPI_BR_SPPR(5) | SPI_BR_SPR(4),
  326. SPI_BR_SPPR(6) | SPI_BR_SPR(4),
  327. SPI_BR_SPPR(7) | SPI_BR_SPR(4),
  328. SPI_BR_SPPR(4) | SPI_BR_SPR(5),
  329. SPI_BR_SPPR(5) | SPI_BR_SPR(5),
  330. SPI_BR_SPPR(6) | SPI_BR_SPR(5),
  331. SPI_BR_SPPR(7) | SPI_BR_SPR(5),
  332. SPI_BR_SPPR(4) | SPI_BR_SPR(6),
  333. SPI_BR_SPPR(5) | SPI_BR_SPR(6)
  334. };
  335. uint8_t SPIClass::setCS(uint8_t pin)
  336. {
  337. switch (pin) {
  338. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  339. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  340. }
  341. return 0;
  342. }
  343. void SPI1Class::begin()
  344. {
  345. SIM_SCGC4 |= SIM_SCGC4_SPI1;
  346. SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  347. SPI1_C2 = 0;
  348. uint8_t tmp __attribute__((unused)) = SPI1_S;
  349. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  350. }
  351. void SPI1Class::end() {
  352. SPCR1.disable_pins();
  353. SPI1_C1 = 0;
  354. }
  355. uint8_t SPI1Class::setCS(uint8_t pin)
  356. {
  357. switch (pin) {
  358. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
  359. }
  360. return 0;
  361. }
  362. /**********************************************************/
  363. /* 32 bit Arduino Due */
  364. /**********************************************************/
  365. #elif defined(__arm__) && defined(__SAM3X8E__)
  366. #include "SPI.h"
  367. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  368. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  369. {
  370. // Empty
  371. }
  372. void SPIClass::begin() {
  373. init();
  374. // NPCS control is left to the user
  375. // Default speed set to 4Mhz
  376. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  377. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  378. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  379. }
  380. void SPIClass::begin(uint8_t _pin) {
  381. init();
  382. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  383. PIO_Configure(
  384. g_APinDescription[spiPin].pPort,
  385. g_APinDescription[spiPin].ulPinType,
  386. g_APinDescription[spiPin].ulPin,
  387. g_APinDescription[spiPin].ulPinConfiguration);
  388. // Default speed set to 4Mhz
  389. setClockDivider(_pin, 21);
  390. setDataMode(_pin, SPI_MODE0);
  391. setBitOrder(_pin, MSBFIRST);
  392. }
  393. void SPIClass::init() {
  394. if (initialized)
  395. return;
  396. interruptMode = 0;
  397. interruptMask = 0;
  398. interruptSave = 0;
  399. initCb();
  400. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  401. SPI_Enable(spi);
  402. initialized = true;
  403. }
  404. #ifndef interruptsStatus
  405. #define interruptsStatus() __interruptsStatus()
  406. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  407. static inline unsigned char __interruptsStatus(void) {
  408. unsigned int primask;
  409. asm volatile ("mrs %0, primask" : "=r" (primask));
  410. if (primask) return 0;
  411. return 1;
  412. }
  413. #endif
  414. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  415. {
  416. uint8_t irestore;
  417. irestore = interruptsStatus();
  418. noInterrupts();
  419. if (interruptMode < 2) {
  420. if (interruptNumber > NUM_DIGITAL_PINS) {
  421. interruptMode = 2;
  422. } else {
  423. uint8_t imask = interruptMask;
  424. Pio *pio = g_APinDescription[interruptNumber].pPort;
  425. if (pio == PIOA) {
  426. imask |= 1;
  427. } else if (pio == PIOB) {
  428. imask |= 2;
  429. } else if (pio == PIOC) {
  430. imask |= 4;
  431. } else if (pio == PIOD) {
  432. imask |= 8;
  433. }
  434. interruptMask = imask;
  435. interruptMode = 1;
  436. }
  437. }
  438. if (irestore) interrupts();
  439. }
  440. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  441. {
  442. if (interruptMode > 0) {
  443. if (interruptMode == 1) {
  444. uint8_t imask = interruptMask;
  445. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  446. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  447. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  448. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  449. } else {
  450. interruptSave = interruptsStatus();
  451. noInterrupts();
  452. }
  453. }
  454. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  455. bitOrder[ch] = settings.border;
  456. SPI_ConfigureNPCS(spi, ch, settings.config);
  457. }
  458. void SPIClass::endTransaction(void)
  459. {
  460. if (interruptMode > 0) {
  461. if (interruptMode == 1) {
  462. uint8_t imask = interruptMask;
  463. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  464. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  465. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  466. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  467. } else {
  468. if (interruptSave) interrupts();
  469. }
  470. }
  471. }
  472. void SPIClass::end(uint8_t _pin) {
  473. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  474. // Setting the pin as INPUT will disconnect it from SPI peripheral
  475. pinMode(spiPin, INPUT);
  476. }
  477. void SPIClass::end() {
  478. SPI_Disable(spi);
  479. initialized = false;
  480. }
  481. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  482. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  483. bitOrder[ch] = _bitOrder;
  484. }
  485. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  486. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  487. mode[ch] = _mode | SPI_CSR_CSAAT;
  488. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  489. // transfer. Some device needs that for working properly.
  490. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  491. }
  492. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  493. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  494. divider[ch] = _divider;
  495. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  496. // transfer. Some device needs that for working properly.
  497. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  498. }
  499. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  500. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  501. // Reverse bit order
  502. if (bitOrder[ch] == LSBFIRST)
  503. _data = __REV(__RBIT(_data));
  504. uint32_t d = _data | SPI_PCS(ch);
  505. if (_mode == SPI_LAST)
  506. d |= SPI_TDR_LASTXFER;
  507. // SPI_Write(spi, _channel, _data);
  508. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  509. ;
  510. spi->SPI_TDR = d;
  511. // return SPI_Read(spi);
  512. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  513. ;
  514. d = spi->SPI_RDR;
  515. // Reverse bit order
  516. if (bitOrder[ch] == LSBFIRST)
  517. d = __REV(__RBIT(d));
  518. return d & 0xFF;
  519. }
  520. void SPIClass::attachInterrupt(void) {
  521. // Should be enableInterrupt()
  522. }
  523. void SPIClass::detachInterrupt(void) {
  524. // Should be disableInterrupt()
  525. }
  526. #if SPI_INTERFACES_COUNT > 0
  527. static void SPI_0_Init(void) {
  528. PIO_Configure(
  529. g_APinDescription[PIN_SPI_MOSI].pPort,
  530. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  531. g_APinDescription[PIN_SPI_MOSI].ulPin,
  532. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  533. PIO_Configure(
  534. g_APinDescription[PIN_SPI_MISO].pPort,
  535. g_APinDescription[PIN_SPI_MISO].ulPinType,
  536. g_APinDescription[PIN_SPI_MISO].ulPin,
  537. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  538. PIO_Configure(
  539. g_APinDescription[PIN_SPI_SCK].pPort,
  540. g_APinDescription[PIN_SPI_SCK].ulPinType,
  541. g_APinDescription[PIN_SPI_SCK].ulPin,
  542. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  543. }
  544. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  545. #endif
  546. #endif