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

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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)
  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 Arduino Due */
  274. /**********************************************************/
  275. #elif defined(__arm__) && defined(__SAM3X8E__)
  276. #include "SPI.h"
  277. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  278. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  279. {
  280. // Empty
  281. }
  282. void SPIClass::begin() {
  283. init();
  284. // NPCS control is left to the user
  285. // Default speed set to 4Mhz
  286. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  287. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  288. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  289. }
  290. void SPIClass::begin(uint8_t _pin) {
  291. init();
  292. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  293. PIO_Configure(
  294. g_APinDescription[spiPin].pPort,
  295. g_APinDescription[spiPin].ulPinType,
  296. g_APinDescription[spiPin].ulPin,
  297. g_APinDescription[spiPin].ulPinConfiguration);
  298. // Default speed set to 4Mhz
  299. setClockDivider(_pin, 21);
  300. setDataMode(_pin, SPI_MODE0);
  301. setBitOrder(_pin, MSBFIRST);
  302. }
  303. void SPIClass::init() {
  304. if (initialized)
  305. return;
  306. interruptMode = 0;
  307. interruptMask = 0;
  308. interruptSave = 0;
  309. initCb();
  310. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  311. SPI_Enable(spi);
  312. initialized = true;
  313. }
  314. #ifndef interruptsStatus
  315. #define interruptsStatus() __interruptsStatus()
  316. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  317. static inline unsigned char __interruptsStatus(void) {
  318. unsigned int primask;
  319. asm volatile ("mrs %0, primask" : "=r" (primask));
  320. if (primask) return 0;
  321. return 1;
  322. }
  323. #endif
  324. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  325. {
  326. uint8_t irestore;
  327. irestore = interruptsStatus();
  328. noInterrupts();
  329. if (interruptMode < 2) {
  330. if (interruptNumber > NUM_DIGITAL_PINS) {
  331. interruptMode = 2;
  332. } else {
  333. uint8_t imask = interruptMask;
  334. Pio *pio = g_APinDescription[interruptNumber].pPort;
  335. if (pio == PIOA) {
  336. imask |= 1;
  337. } else if (pio == PIOB) {
  338. imask |= 2;
  339. } else if (pio == PIOC) {
  340. imask |= 4;
  341. } else if (pio == PIOD) {
  342. imask |= 8;
  343. }
  344. interruptMask = imask;
  345. interruptMode = 1;
  346. }
  347. }
  348. if (irestore) interrupts();
  349. }
  350. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  351. {
  352. if (interruptMode > 0) {
  353. if (interruptMode == 1) {
  354. uint8_t imask = interruptMask;
  355. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  356. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  357. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  358. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  359. } else {
  360. interruptSave = interruptsStatus();
  361. noInterrupts();
  362. }
  363. }
  364. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  365. bitOrder[ch] = settings.border;
  366. SPI_ConfigureNPCS(spi, ch, settings.config);
  367. }
  368. void SPIClass::endTransaction(void)
  369. {
  370. if (interruptMode > 0) {
  371. if (interruptMode == 1) {
  372. uint8_t imask = interruptMask;
  373. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  374. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  375. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  376. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  377. } else {
  378. if (interruptSave) interrupts();
  379. }
  380. }
  381. }
  382. void SPIClass::end(uint8_t _pin) {
  383. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  384. // Setting the pin as INPUT will disconnect it from SPI peripheral
  385. pinMode(spiPin, INPUT);
  386. }
  387. void SPIClass::end() {
  388. SPI_Disable(spi);
  389. initialized = false;
  390. }
  391. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  392. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  393. bitOrder[ch] = _bitOrder;
  394. }
  395. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  396. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  397. mode[ch] = _mode | SPI_CSR_CSAAT;
  398. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  399. // transfer. Some device needs that for working properly.
  400. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  401. }
  402. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  403. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  404. divider[ch] = _divider;
  405. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  406. // transfer. Some device needs that for working properly.
  407. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  408. }
  409. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  410. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  411. // Reverse bit order
  412. if (bitOrder[ch] == LSBFIRST)
  413. _data = __REV(__RBIT(_data));
  414. uint32_t d = _data | SPI_PCS(ch);
  415. if (_mode == SPI_LAST)
  416. d |= SPI_TDR_LASTXFER;
  417. // SPI_Write(spi, _channel, _data);
  418. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  419. ;
  420. spi->SPI_TDR = d;
  421. // return SPI_Read(spi);
  422. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  423. ;
  424. d = spi->SPI_RDR;
  425. // Reverse bit order
  426. if (bitOrder[ch] == LSBFIRST)
  427. d = __REV(__RBIT(d));
  428. return d & 0xFF;
  429. }
  430. void SPIClass::attachInterrupt(void) {
  431. // Should be enableInterrupt()
  432. }
  433. void SPIClass::detachInterrupt(void) {
  434. // Should be disableInterrupt()
  435. }
  436. #if SPI_INTERFACES_COUNT > 0
  437. static void SPI_0_Init(void) {
  438. PIO_Configure(
  439. g_APinDescription[PIN_SPI_MOSI].pPort,
  440. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  441. g_APinDescription[PIN_SPI_MOSI].ulPin,
  442. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  443. PIO_Configure(
  444. g_APinDescription[PIN_SPI_MISO].pPort,
  445. g_APinDescription[PIN_SPI_MISO].ulPinType,
  446. g_APinDescription[PIN_SPI_MISO].ulPin,
  447. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  448. PIO_Configure(
  449. g_APinDescription[PIN_SPI_SCK].pPort,
  450. g_APinDescription[PIN_SPI_SCK].ulPinType,
  451. g_APinDescription[PIN_SPI_SCK].ulPin,
  452. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  453. }
  454. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  455. #endif
  456. #endif