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.

1107 lines
30KB

  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. uint8_t SPIClass::pinIsChipSelect(uint8_t pin)
  243. {
  244. switch (pin) {
  245. case 10: return 0x01; // PTC4
  246. case 2: return 0x01; // PTD0
  247. case 9: return 0x02; // PTC3
  248. case 6: return 0x02; // PTD4
  249. case 20: return 0x04; // PTD5
  250. case 23: return 0x04; // PTC2
  251. case 21: return 0x08; // PTD6
  252. case 22: return 0x08; // PTC1
  253. case 15: return 0x10; // PTC0
  254. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  255. case 26: return 0x01;
  256. #endif
  257. }
  258. return 0;
  259. }
  260. bool SPIClass::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  261. {
  262. uint8_t pin1_mask, pin2_mask;
  263. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  264. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  265. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  266. if ((pin1_mask & pin2_mask) != 0) return false;
  267. return true;
  268. }
  269. uint8_t SPIClass::setCS(uint8_t pin)
  270. {
  271. switch (pin) {
  272. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  273. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  274. case 9: CORE_PIN9_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTC3
  275. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTD4
  276. case 20: CORE_PIN20_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTD5
  277. case 23: CORE_PIN23_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTC2
  278. case 21: CORE_PIN21_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTD6
  279. case 22: CORE_PIN22_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTC1
  280. case 15: CORE_PIN15_CONFIG = PORT_PCR_MUX(2); return 0x10; // PTC0
  281. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  282. case 26: CORE_PIN26_CONFIG = PORT_PCR_MUX(2);return 0x01;
  283. #endif
  284. }
  285. return 0;
  286. }
  287. void SPIClass::transfer(void *buf, size_t count) {
  288. if (count == 0) return;
  289. uint8_t *p_write = (uint8_t *)buf;
  290. uint8_t *p_read = p_write;
  291. size_t count_read = count;
  292. // Lets clear the reader queue
  293. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
  294. uint32_t sr;
  295. // Now lets loop while we still have data to output
  296. if (count & 1) {
  297. if (count > 1)
  298. KINETISK_SPI0.PUSHR = *p_write++ | SPI_PUSHR_CONT | SPI_PUSHR_CTAS(0);
  299. else
  300. KINETISK_SPI0.PUSHR = *p_write++ | SPI_PUSHR_CTAS(0);
  301. count--;
  302. }
  303. while (count > 0) {
  304. // Push out the next byte;
  305. uint16_t w = (*p_write++) << 8;
  306. w |= *p_write++;
  307. if (count == 2)
  308. KINETISK_SPI0.PUSHR = w | SPI_PUSHR_CTAS(1);
  309. else
  310. KINETISK_SPI0.PUSHR = w | SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1);
  311. count -= 2; // how many bytes to output.
  312. // Make sure queue is not full before pushing next byte out
  313. do {
  314. sr = KINETISK_SPI0.SR;
  315. if (sr & 0xF0) {
  316. uint16_t w = KINETISK_SPI0.POPR; // Read any pending RX bytes in
  317. if (count_read & 1) {
  318. *p_read++ = w; // Read any pending RX bytes in
  319. count_read--;
  320. } else {
  321. *p_read++ = w >> 8;
  322. *p_read++ = (w & 0xff);
  323. count_read -= 2;
  324. }
  325. }
  326. } while ((sr & (15 << 12)) > (3 << 12));
  327. }
  328. // now lets wait for all of the read bytes to be returned...
  329. while (count_read) {
  330. sr = KINETISK_SPI0.SR;
  331. if (sr & 0xF0) {
  332. uint16_t w = KINETISK_SPI0.POPR; // Read any pending RX bytes in
  333. if (count_read & 1) {
  334. *p_read++ = w; // Read any pending RX bytes in
  335. count_read--;
  336. } else {
  337. *p_read++ = w >> 8;
  338. *p_read++ = (w & 0xff);
  339. count_read -= 2;
  340. }
  341. }
  342. }
  343. }
  344. /**********************************************************/
  345. /* 32 bit Teensy-3.5/3.6 */
  346. /**********************************************************/
  347. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  348. SPI1Class SPI1;
  349. uint8_t SPI1Class::interruptMasksUsed = 0;
  350. uint32_t SPI1Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  351. uint32_t SPI1Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  352. #ifdef SPI_TRANSACTION_MISMATCH_LED
  353. uint8_t SPI1Class::inTransactionFlag = 0;
  354. #endif
  355. void SPI1Class::begin()
  356. {
  357. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  358. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  359. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  360. SPI1_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  361. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  362. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  363. }
  364. void SPI1Class::end() {
  365. SPCR1.disable_pins();
  366. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  367. }
  368. void SPI1Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  369. {
  370. uint32_t n = (uint32_t)interruptName;
  371. if (n >= NVIC_NUM_INTERRUPTS) return;
  372. //Serial.print("usingInterrupt ");
  373. //Serial.println(n);
  374. interruptMasksUsed |= (1 << (n >> 5));
  375. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  376. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  377. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  378. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  379. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  380. }
  381. void SPI1Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  382. {
  383. uint32_t n = (uint32_t)interruptName;
  384. if (n >= NVIC_NUM_INTERRUPTS) return;
  385. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  386. if (interruptMask[n >> 5] == 0) {
  387. interruptMasksUsed &= ~(1 << (n >> 5));
  388. }
  389. }
  390. static void updateCTAR1(uint32_t ctar)
  391. {
  392. if (SPI1_CTAR0 != ctar) {
  393. uint32_t mcr = SPI1_MCR;
  394. if (mcr & SPI_MCR_MDIS) {
  395. SPI1_CTAR0 = ctar;
  396. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  397. } else {
  398. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  399. SPI1_CTAR0 = ctar;
  400. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  401. SPI1_MCR = mcr;
  402. }
  403. }
  404. }
  405. void SPI1Class::setBitOrder(uint8_t bitOrder)
  406. {
  407. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  408. uint32_t ctar = SPI1_CTAR0;
  409. if (bitOrder == LSBFIRST) {
  410. ctar |= SPI_CTAR_LSBFE;
  411. } else {
  412. ctar &= ~SPI_CTAR_LSBFE;
  413. }
  414. updateCTAR1(ctar);
  415. }
  416. void SPI1Class::setDataMode(uint8_t dataMode)
  417. {
  418. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  419. // TODO: implement with native code
  420. SPCR1 = (SPCR1 & ~SPI_MODE_MASK) | dataMode;
  421. }
  422. void SPI1Class::setClockDivider_noInline(uint32_t clk)
  423. {
  424. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  425. uint32_t ctar = SPI1_CTAR0;
  426. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  427. if (ctar & SPI_CTAR_CPHA) {
  428. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  429. }
  430. ctar |= clk;
  431. updateCTAR1(ctar);
  432. }
  433. uint8_t SPI1Class::pinIsChipSelect(uint8_t pin)
  434. {
  435. switch (pin) {
  436. case 6: return 0x01; // CS0
  437. case 31: return 0x01; // CS0
  438. case 58: return 0x02; //CS1
  439. case 62: return 0x01; //CS0
  440. case 63: return 0x04; //CS2
  441. }
  442. return 0;
  443. }
  444. bool SPI1Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  445. {
  446. uint8_t pin1_mask, pin2_mask;
  447. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  448. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  449. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  450. if ((pin1_mask & pin2_mask) != 0) return false;
  451. return true;
  452. }
  453. uint8_t SPI1Class::setCS(uint8_t pin)
  454. {
  455. switch (pin) {
  456. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(7); return 0x01; // PTD4
  457. case 31: CORE_PIN31_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD5
  458. case 58: CORE_PIN58_CONFIG = PORT_PCR_MUX(2); return 0x02; //CS1
  459. case 62: CORE_PIN62_CONFIG = PORT_PCR_MUX(2); return 0x01; //CS0
  460. case 63: CORE_PIN63_CONFIG = PORT_PCR_MUX(2); return 0x04; //CS2
  461. }
  462. return 0;
  463. }
  464. void SPI1Class::transfer(void *buf, size_t count) {
  465. if (count == 0) return;
  466. uint8_t *p_write = (uint8_t *)buf;
  467. uint8_t *p_read = p_write;
  468. size_t count_read = count;
  469. // Lets clear the reader queue
  470. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
  471. uint32_t sr;
  472. // Now lets loop while we still have data to output
  473. if (count & 1) {
  474. KINETISK_SPI1.PUSHR = *p_write++ | SPI_PUSHR_CTAS(0);
  475. count--;
  476. }
  477. while (count > 0) {
  478. // Push out the next byte;
  479. uint16_t w = (*p_write++) << 8;
  480. w |= *p_write++;
  481. KINETISK_SPI1.PUSHR = w | SPI_PUSHR_CTAS(1);
  482. count -= 2; // how many bytes to output.
  483. // Make sure queue is not full before pushing next byte out
  484. do {
  485. sr = KINETISK_SPI1.SR;
  486. if (sr & 0xF0) {
  487. uint16_t w = KINETISK_SPI1.POPR; // Read any pending RX bytes in
  488. if (count_read & 1) {
  489. *p_read++ = w; // Read any pending RX bytes in
  490. count_read--;
  491. } else {
  492. *p_read++ = w >> 8;
  493. *p_read++ = (w & 0xff);
  494. count_read -= 2;
  495. }
  496. }
  497. } while ((sr & (15 << 12)) > (0 << 12)); // SPI1 and 2 only have 1 item queue
  498. }
  499. // now lets wait for all of the read bytes to be returned...
  500. while (count_read) {
  501. sr = KINETISK_SPI1.SR;
  502. if (sr & 0xF0) {
  503. uint16_t w = KINETISK_SPI1.POPR; // Read any pending RX bytes in
  504. if (count_read & 1) {
  505. *p_read++ = w; // Read any pending RX bytes in
  506. count_read--;
  507. } else {
  508. *p_read++ = w >> 8;
  509. *p_read++ = (w & 0xff);
  510. count_read -= 2;
  511. }
  512. }
  513. }
  514. }
  515. // SPI2 Class
  516. SPI2Class SPI2;
  517. uint8_t SPI2Class::interruptMasksUsed = 0;
  518. uint32_t SPI2Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  519. uint32_t SPI2Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  520. #ifdef SPI_TRANSACTION_MISMATCH_LED
  521. uint8_t SPI2Class::inTransactionFlag = 0;
  522. #endif
  523. void SPI2Class::begin()
  524. {
  525. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  526. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  527. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  528. SPI2_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  529. SPI2_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  530. SPCR2.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  531. }
  532. void SPI2Class::end() {
  533. SPCR2.disable_pins();
  534. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  535. }
  536. void SPI2Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  537. {
  538. uint32_t n = (uint32_t)interruptName;
  539. if (n >= NVIC_NUM_INTERRUPTS) return;
  540. //Serial.print("usingInterrupt ");
  541. //Serial.println(n);
  542. interruptMasksUsed |= (1 << (n >> 5));
  543. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  544. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  545. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  546. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  547. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  548. }
  549. void SPI2Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  550. {
  551. uint32_t n = (uint32_t)interruptName;
  552. if (n >= NVIC_NUM_INTERRUPTS) return;
  553. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  554. if (interruptMask[n >> 5] == 0) {
  555. interruptMasksUsed &= ~(1 << (n >> 5));
  556. }
  557. }
  558. static void updateCTAR2(uint32_t ctar)
  559. {
  560. if (SPI2_CTAR0 != ctar) {
  561. uint32_t mcr = SPI2_MCR;
  562. if (mcr & SPI_MCR_MDIS) {
  563. SPI2_CTAR0 = ctar;
  564. SPI2_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  565. } else {
  566. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  567. SPI2_CTAR0 = ctar;
  568. SPI2_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  569. SPI2_MCR = mcr;
  570. }
  571. }
  572. }
  573. void SPI2Class::setBitOrder(uint8_t bitOrder)
  574. {
  575. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  576. uint32_t ctar = SPI2_CTAR0;
  577. if (bitOrder == LSBFIRST) {
  578. ctar |= SPI_CTAR_LSBFE;
  579. } else {
  580. ctar &= ~SPI_CTAR_LSBFE;
  581. }
  582. updateCTAR2(ctar);
  583. }
  584. void SPI2Class::setDataMode(uint8_t dataMode)
  585. {
  586. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  587. // TODO: implement with native code
  588. SPCR2 = (SPCR2 & ~SPI_MODE_MASK) | dataMode;
  589. }
  590. void SPI2Class::setClockDivider_noInline(uint32_t clk)
  591. {
  592. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  593. uint32_t ctar = SPI2_CTAR0;
  594. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  595. if (ctar & SPI_CTAR_CPHA) {
  596. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  597. }
  598. ctar |= clk;
  599. updateCTAR2(ctar);
  600. }
  601. uint8_t SPI2Class::pinIsChipSelect(uint8_t pin)
  602. {
  603. switch (pin) {
  604. case 43: return 0x01; // CS0
  605. case 54: return 0x02; // CS1
  606. case 55: return 0x01; // CS0
  607. }
  608. return 0;
  609. }
  610. bool SPI2Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  611. {
  612. uint8_t pin1_mask, pin2_mask;
  613. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  614. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  615. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  616. if ((pin1_mask & pin2_mask) != 0) return false;
  617. return true;
  618. }
  619. uint8_t SPI2Class::setCS(uint8_t pin)
  620. {
  621. switch (pin) {
  622. case 43: CORE_PIN43_CONFIG = PORT_PCR_MUX(2); return 0x01; // CS0
  623. case 54: CORE_PIN54_CONFIG = PORT_PCR_MUX(2); return 0x02; // CS1
  624. case 55: CORE_PIN55_CONFIG = PORT_PCR_MUX(2); return 0x01; // CS0
  625. }
  626. return 0;
  627. }
  628. void SPI2Class::transfer(void *buf, size_t count) {
  629. if (count == 0) return;
  630. uint8_t *p_write = (uint8_t *)buf;
  631. uint8_t *p_read = p_write;
  632. size_t count_read = count;
  633. // Lets clear the reader queue
  634. SPI2_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
  635. uint32_t sr;
  636. // Now lets loop while we still have data to output
  637. if (count & 1) {
  638. KINETISK_SPI2.PUSHR = *p_write++ | SPI_PUSHR_CTAS(0);
  639. count--;
  640. }
  641. while (count > 0) {
  642. // Push out the next byte;
  643. uint16_t w = (*p_write++) << 8;
  644. w |= *p_write++;
  645. KINETISK_SPI2.PUSHR = w | SPI_PUSHR_CTAS(1);
  646. count -= 2; // how many bytes to output.
  647. // Make sure queue is not full before pushing next byte out
  648. do {
  649. sr = KINETISK_SPI2.SR;
  650. if (sr & 0xF0) {
  651. uint16_t w = KINETISK_SPI2.POPR; // Read any pending RX bytes in
  652. if (count_read & 1) {
  653. *p_read++ = w; // Read any pending RX bytes in
  654. count_read--;
  655. } else {
  656. *p_read++ = w >> 8;
  657. *p_read++ = (w & 0xff);
  658. count_read -= 2;
  659. }
  660. }
  661. } while ((sr & (15 << 12)) > (0 << 12)); // SPI2 and 2 only have 1 item queue
  662. }
  663. // now lets wait for all of the read bytes to be returned...
  664. while (count_read) {
  665. sr = KINETISK_SPI2.SR;
  666. if (sr & 0xF0) {
  667. uint16_t w = KINETISK_SPI2.POPR; // Read any pending RX bytes in
  668. if (count_read & 1) {
  669. *p_read++ = w; // Read any pending RX bytes in
  670. count_read--;
  671. } else {
  672. *p_read++ = w >> 8;
  673. *p_read++ = (w & 0xff);
  674. count_read -= 2;
  675. }
  676. }
  677. }
  678. }
  679. #endif
  680. /**********************************************************/
  681. /* 32 bit Teensy-LC */
  682. /**********************************************************/
  683. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  684. SPIClass SPI;
  685. SPI1Class SPI1;
  686. uint32_t SPIClass::interruptMask = 0;
  687. uint32_t SPIClass::interruptSave = 0;
  688. uint32_t SPI1Class::interruptMask = 0;
  689. uint32_t SPI1Class::interruptSave = 0;
  690. #ifdef SPI_TRANSACTION_MISMATCH_LED
  691. uint8_t SPIClass::inTransactionFlag = 0;
  692. uint8_t SPI1Class::inTransactionFlag = 0;
  693. #endif
  694. void SPIClass::begin()
  695. {
  696. SIM_SCGC4 |= SIM_SCGC4_SPI0;
  697. SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  698. SPI0_C2 = 0;
  699. uint8_t tmp __attribute__((unused)) = SPI0_S;
  700. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  701. }
  702. void SPIClass::end() {
  703. SPCR.disable_pins();
  704. SPI0_C1 = 0;
  705. }
  706. const uint16_t SPISettings::br_div_table[30] = {
  707. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24,
  708. 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
  709. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768,
  710. };
  711. const uint8_t SPISettings::br_clock_table[30] = {
  712. SPI_BR_SPPR(0) | SPI_BR_SPR(0),
  713. SPI_BR_SPPR(1) | SPI_BR_SPR(0),
  714. SPI_BR_SPPR(2) | SPI_BR_SPR(0),
  715. SPI_BR_SPPR(3) | SPI_BR_SPR(0),
  716. SPI_BR_SPPR(4) | SPI_BR_SPR(0),
  717. SPI_BR_SPPR(5) | SPI_BR_SPR(0),
  718. SPI_BR_SPPR(6) | SPI_BR_SPR(0),
  719. SPI_BR_SPPR(7) | SPI_BR_SPR(0),
  720. SPI_BR_SPPR(4) | SPI_BR_SPR(1),
  721. SPI_BR_SPPR(5) | SPI_BR_SPR(1),
  722. SPI_BR_SPPR(6) | SPI_BR_SPR(1),
  723. SPI_BR_SPPR(7) | SPI_BR_SPR(1),
  724. SPI_BR_SPPR(4) | SPI_BR_SPR(2),
  725. SPI_BR_SPPR(5) | SPI_BR_SPR(2),
  726. SPI_BR_SPPR(6) | SPI_BR_SPR(2),
  727. SPI_BR_SPPR(7) | SPI_BR_SPR(2),
  728. SPI_BR_SPPR(4) | SPI_BR_SPR(3),
  729. SPI_BR_SPPR(5) | SPI_BR_SPR(3),
  730. SPI_BR_SPPR(6) | SPI_BR_SPR(3),
  731. SPI_BR_SPPR(7) | SPI_BR_SPR(3),
  732. SPI_BR_SPPR(4) | SPI_BR_SPR(4),
  733. SPI_BR_SPPR(5) | SPI_BR_SPR(4),
  734. SPI_BR_SPPR(6) | SPI_BR_SPR(4),
  735. SPI_BR_SPPR(7) | SPI_BR_SPR(4),
  736. SPI_BR_SPPR(4) | SPI_BR_SPR(5),
  737. SPI_BR_SPPR(5) | SPI_BR_SPR(5),
  738. SPI_BR_SPPR(6) | SPI_BR_SPR(5),
  739. SPI_BR_SPPR(7) | SPI_BR_SPR(5),
  740. SPI_BR_SPPR(4) | SPI_BR_SPR(6),
  741. SPI_BR_SPPR(5) | SPI_BR_SPR(6)
  742. };
  743. uint8_t SPIClass::setCS(uint8_t pin)
  744. {
  745. switch (pin) {
  746. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  747. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  748. }
  749. return 0;
  750. }
  751. void SPI1Class::begin()
  752. {
  753. SIM_SCGC4 |= SIM_SCGC4_SPI1;
  754. SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  755. SPI1_C2 = 0;
  756. uint8_t tmp __attribute__((unused)) = SPI1_S;
  757. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  758. }
  759. void SPI1Class::end() {
  760. SPCR1.disable_pins();
  761. SPI1_C1 = 0;
  762. }
  763. uint8_t SPI1Class::setCS(uint8_t pin)
  764. {
  765. switch (pin) {
  766. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
  767. }
  768. return 0;
  769. }
  770. /**********************************************************/
  771. /* 32 bit Arduino Due */
  772. /**********************************************************/
  773. #elif defined(__arm__) && defined(__SAM3X8E__)
  774. #include "SPI.h"
  775. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  776. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  777. {
  778. // Empty
  779. }
  780. void SPIClass::begin() {
  781. init();
  782. // NPCS control is left to the user
  783. // Default speed set to 4Mhz
  784. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  785. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  786. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  787. }
  788. void SPIClass::begin(uint8_t _pin) {
  789. init();
  790. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  791. PIO_Configure(
  792. g_APinDescription[spiPin].pPort,
  793. g_APinDescription[spiPin].ulPinType,
  794. g_APinDescription[spiPin].ulPin,
  795. g_APinDescription[spiPin].ulPinConfiguration);
  796. // Default speed set to 4Mhz
  797. setClockDivider(_pin, 21);
  798. setDataMode(_pin, SPI_MODE0);
  799. setBitOrder(_pin, MSBFIRST);
  800. }
  801. void SPIClass::init() {
  802. if (initialized)
  803. return;
  804. interruptMode = 0;
  805. interruptMask = 0;
  806. interruptSave = 0;
  807. initCb();
  808. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  809. SPI_Enable(spi);
  810. initialized = true;
  811. }
  812. #ifndef interruptsStatus
  813. #define interruptsStatus() __interruptsStatus()
  814. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  815. static inline unsigned char __interruptsStatus(void) {
  816. unsigned int primask;
  817. asm volatile ("mrs %0, primask" : "=r" (primask));
  818. if (primask) return 0;
  819. return 1;
  820. }
  821. #endif
  822. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  823. {
  824. uint8_t irestore;
  825. irestore = interruptsStatus();
  826. noInterrupts();
  827. if (interruptMode < 2) {
  828. if (interruptNumber > NUM_DIGITAL_PINS) {
  829. interruptMode = 2;
  830. } else {
  831. uint8_t imask = interruptMask;
  832. Pio *pio = g_APinDescription[interruptNumber].pPort;
  833. if (pio == PIOA) {
  834. imask |= 1;
  835. } else if (pio == PIOB) {
  836. imask |= 2;
  837. } else if (pio == PIOC) {
  838. imask |= 4;
  839. } else if (pio == PIOD) {
  840. imask |= 8;
  841. }
  842. interruptMask = imask;
  843. interruptMode = 1;
  844. }
  845. }
  846. if (irestore) interrupts();
  847. }
  848. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  849. {
  850. if (interruptMode > 0) {
  851. if (interruptMode == 1) {
  852. uint8_t imask = interruptMask;
  853. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  854. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  855. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  856. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  857. } else {
  858. interruptSave = interruptsStatus();
  859. noInterrupts();
  860. }
  861. }
  862. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  863. bitOrder[ch] = settings.border;
  864. SPI_ConfigureNPCS(spi, ch, settings.config);
  865. }
  866. void SPIClass::endTransaction(void)
  867. {
  868. if (interruptMode > 0) {
  869. if (interruptMode == 1) {
  870. uint8_t imask = interruptMask;
  871. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  872. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  873. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  874. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  875. } else {
  876. if (interruptSave) interrupts();
  877. }
  878. }
  879. }
  880. void SPIClass::end(uint8_t _pin) {
  881. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  882. // Setting the pin as INPUT will disconnect it from SPI peripheral
  883. pinMode(spiPin, INPUT);
  884. }
  885. void SPIClass::end() {
  886. SPI_Disable(spi);
  887. initialized = false;
  888. }
  889. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  890. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  891. bitOrder[ch] = _bitOrder;
  892. }
  893. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  894. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  895. mode[ch] = _mode | SPI_CSR_CSAAT;
  896. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  897. // transfer. Some device needs that for working properly.
  898. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  899. }
  900. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  901. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  902. divider[ch] = _divider;
  903. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  904. // transfer. Some device needs that for working properly.
  905. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  906. }
  907. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  908. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  909. // Reverse bit order
  910. if (bitOrder[ch] == LSBFIRST)
  911. _data = __REV(__RBIT(_data));
  912. uint32_t d = _data | SPI_PCS(ch);
  913. if (_mode == SPI_LAST)
  914. d |= SPI_TDR_LASTXFER;
  915. // SPI_Write(spi, _channel, _data);
  916. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  917. ;
  918. spi->SPI_TDR = d;
  919. // return SPI_Read(spi);
  920. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  921. ;
  922. d = spi->SPI_RDR;
  923. // Reverse bit order
  924. if (bitOrder[ch] == LSBFIRST)
  925. d = __REV(__RBIT(d));
  926. return d & 0xFF;
  927. }
  928. void SPIClass::attachInterrupt(void) {
  929. // Should be enableInterrupt()
  930. }
  931. void SPIClass::detachInterrupt(void) {
  932. // Should be disableInterrupt()
  933. }
  934. #if SPI_INTERFACES_COUNT > 0
  935. static void SPI_0_Init(void) {
  936. PIO_Configure(
  937. g_APinDescription[PIN_SPI_MOSI].pPort,
  938. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  939. g_APinDescription[PIN_SPI_MOSI].ulPin,
  940. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  941. PIO_Configure(
  942. g_APinDescription[PIN_SPI_MISO].pPort,
  943. g_APinDescription[PIN_SPI_MISO].ulPinType,
  944. g_APinDescription[PIN_SPI_MISO].ulPin,
  945. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  946. PIO_Configure(
  947. g_APinDescription[PIN_SPI_SCK].pPort,
  948. g_APinDescription[PIN_SPI_SCK].ulPinType,
  949. g_APinDescription[PIN_SPI_SCK].ulPin,
  950. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  951. }
  952. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  953. #endif
  954. #endif