Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
  3. * Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
  4. * Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
  5. * SPI Master library for arduino.
  6. *
  7. * This file is free software; you can redistribute it and/or modify
  8. * it under the terms of either the GNU General Public License version 2
  9. * or the GNU Lesser General Public License version 2.1, both as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef _SPI_H_INCLUDED
  13. #define _SPI_H_INCLUDED
  14. #include <Arduino.h>
  15. // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
  16. // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
  17. #define SPI_HAS_TRANSACTION 1
  18. // Uncomment this line to add detection of mismatched begin/end transactions.
  19. // A mismatch occurs if other libraries fail to use SPI.endTransaction() for
  20. // each SPI.beginTransaction(). Connect a LED to this pin. The LED will turn
  21. // on if any mismatch is ever detected.
  22. //#define SPI_TRANSACTION_MISMATCH_LED 5
  23. #ifndef __SAM3X8E__
  24. #ifndef LSBFIRST
  25. #define LSBFIRST 0
  26. #endif
  27. #ifndef MSBFIRST
  28. #define MSBFIRST 1
  29. #endif
  30. #endif
  31. #define SPI_MODE0 0x00
  32. #define SPI_MODE1 0x04
  33. #define SPI_MODE2 0x08
  34. #define SPI_MODE3 0x0C
  35. #define SPI_CLOCK_DIV4 0x00
  36. #define SPI_CLOCK_DIV16 0x01
  37. #define SPI_CLOCK_DIV64 0x02
  38. #define SPI_CLOCK_DIV128 0x03
  39. #define SPI_CLOCK_DIV2 0x04
  40. #define SPI_CLOCK_DIV8 0x05
  41. #define SPI_CLOCK_DIV32 0x06
  42. #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
  43. #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
  44. #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
  45. /**********************************************************/
  46. /* 8 bit AVR-based boards */
  47. /**********************************************************/
  48. #if defined(__AVR__)
  49. // define SPI_AVR_EIMSK for AVR boards with external interrupt pins
  50. #if defined(EIMSK)
  51. #define SPI_AVR_EIMSK EIMSK
  52. #elif defined(GICR)
  53. #define SPI_AVR_EIMSK GICR
  54. #elif defined(GIMSK)
  55. #define SPI_AVR_EIMSK GIMSK
  56. #endif
  57. class SPISettings {
  58. public:
  59. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  60. if (__builtin_constant_p(clock)) {
  61. init_AlwaysInline(clock, bitOrder, dataMode);
  62. } else {
  63. init_MightInline(clock, bitOrder, dataMode);
  64. }
  65. }
  66. SPISettings() {
  67. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  68. }
  69. private:
  70. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  71. init_AlwaysInline(clock, bitOrder, dataMode);
  72. }
  73. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  74. __attribute__((__always_inline__)) {
  75. // Clock settings are defined as follows. Note that this shows SPI2X
  76. // inverted, so the bits form increasing numbers. Also note that
  77. // fosc/64 appears twice
  78. // SPR1 SPR0 ~SPI2X Freq
  79. // 0 0 0 fosc/2
  80. // 0 0 1 fosc/4
  81. // 0 1 0 fosc/8
  82. // 0 1 1 fosc/16
  83. // 1 0 0 fosc/32
  84. // 1 0 1 fosc/64
  85. // 1 1 0 fosc/64
  86. // 1 1 1 fosc/128
  87. // We find the fastest clock that is less than or equal to the
  88. // given clock rate. The clock divider that results in clock_setting
  89. // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
  90. // slowest (128 == 2 ^^ 7, so clock_div = 6).
  91. uint8_t clockDiv;
  92. // When the clock is known at compiletime, use this if-then-else
  93. // cascade, which the compiler knows how to completely optimize
  94. // away. When clock is not known, use a loop instead, which generates
  95. // shorter code.
  96. if (__builtin_constant_p(clock)) {
  97. if (clock >= F_CPU / 2) {
  98. clockDiv = 0;
  99. } else if (clock >= F_CPU / 4) {
  100. clockDiv = 1;
  101. } else if (clock >= F_CPU / 8) {
  102. clockDiv = 2;
  103. } else if (clock >= F_CPU / 16) {
  104. clockDiv = 3;
  105. } else if (clock >= F_CPU / 32) {
  106. clockDiv = 4;
  107. } else if (clock >= F_CPU / 64) {
  108. clockDiv = 5;
  109. } else {
  110. clockDiv = 6;
  111. }
  112. } else {
  113. uint32_t clockSetting = F_CPU / 2;
  114. clockDiv = 0;
  115. while (clockDiv < 6 && clock < clockSetting) {
  116. clockSetting /= 2;
  117. clockDiv++;
  118. }
  119. }
  120. // Compensate for the duplicate fosc/64
  121. if (clockDiv == 6)
  122. clockDiv = 7;
  123. // Invert the SPI2X bit
  124. clockDiv ^= 0x1;
  125. // Pack into the SPISettings class
  126. spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
  127. (dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK);
  128. spsr = clockDiv & SPI_2XCLOCK_MASK;
  129. }
  130. uint8_t spcr;
  131. uint8_t spsr;
  132. friend class SPIClass;
  133. };
  134. class SPIClass {
  135. public:
  136. // Initialize the SPI library
  137. static void begin();
  138. // If SPI is used from within an interrupt, this function registers
  139. // that interrupt with the SPI library, so beginTransaction() can
  140. // prevent conflicts. The input interruptNumber is the number used
  141. // with attachInterrupt. If SPI is used from a different interrupt
  142. // (eg, a timer), interruptNumber should be 255.
  143. static void usingInterrupt(uint8_t interruptNumber);
  144. // Before using SPI.transfer() or asserting chip select pins,
  145. // this function is used to gain exclusive access to the SPI bus
  146. // and configure the correct settings.
  147. inline static void beginTransaction(SPISettings settings) {
  148. if (interruptMode > 0) {
  149. #ifdef SPI_AVR_EIMSK
  150. if (interruptMode == 1) {
  151. interruptSave = SPI_AVR_EIMSK;
  152. SPI_AVR_EIMSK &= ~interruptMask;
  153. } else
  154. #endif
  155. {
  156. uint8_t tmp = SREG;
  157. cli();
  158. interruptSave = tmp;
  159. }
  160. }
  161. #ifdef SPI_TRANSACTION_MISMATCH_LED
  162. if (inTransactionFlag) {
  163. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  164. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  165. }
  166. inTransactionFlag = 1;
  167. #endif
  168. SPCR = settings.spcr;
  169. SPSR = settings.spsr;
  170. }
  171. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  172. inline static uint8_t transfer(uint8_t data) {
  173. SPDR = data;
  174. asm volatile("nop");
  175. while (!(SPSR & _BV(SPIF))) ; // wait
  176. return SPDR;
  177. }
  178. inline static uint16_t transfer16(uint16_t data) {
  179. union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
  180. in.val = data;
  181. if ((SPCR & _BV(DORD))) {
  182. SPDR = in.lsb;
  183. asm volatile("nop");
  184. while (!(SPSR & _BV(SPIF))) ;
  185. out.lsb = SPDR;
  186. SPDR = in.msb;
  187. asm volatile("nop");
  188. while (!(SPSR & _BV(SPIF))) ;
  189. out.msb = SPDR;
  190. } else {
  191. SPDR = in.msb;
  192. asm volatile("nop");
  193. while (!(SPSR & _BV(SPIF))) ;
  194. out.msb = SPDR;
  195. SPDR = in.lsb;
  196. asm volatile("nop");
  197. while (!(SPSR & _BV(SPIF))) ;
  198. out.lsb = SPDR;
  199. }
  200. return out.val;
  201. }
  202. inline static void transfer(void *buf, size_t count) {
  203. if (count == 0) return;
  204. uint8_t *p = (uint8_t *)buf;
  205. SPDR = *p;
  206. while (--count > 0) {
  207. uint8_t out = *(p + 1);
  208. while (!(SPSR & _BV(SPIF))) ;
  209. uint8_t in = SPDR;
  210. SPDR = out;
  211. *p++ = in;
  212. }
  213. while (!(SPSR & _BV(SPIF))) ;
  214. *p = SPDR;
  215. }
  216. // After performing a group of transfers and releasing the chip select
  217. // signal, this function allows others to access the SPI bus
  218. inline static void endTransaction(void) {
  219. #ifdef SPI_TRANSACTION_MISMATCH_LED
  220. if (!inTransactionFlag) {
  221. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  222. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  223. }
  224. inTransactionFlag = 0;
  225. #endif
  226. if (interruptMode > 0) {
  227. #ifdef SPI_AVR_EIMSK
  228. if (interruptMode == 1) {
  229. SPI_AVR_EIMSK = interruptSave;
  230. } else
  231. #endif
  232. {
  233. SREG = interruptSave;
  234. }
  235. }
  236. }
  237. // Disable the SPI bus
  238. static void end();
  239. // This function is deprecated. New applications should use
  240. // beginTransaction() to configure SPI settings.
  241. inline static void setBitOrder(uint8_t bitOrder) {
  242. if (bitOrder == LSBFIRST) SPCR |= _BV(DORD);
  243. else SPCR &= ~(_BV(DORD));
  244. }
  245. // This function is deprecated. New applications should use
  246. // beginTransaction() to configure SPI settings.
  247. inline static void setDataMode(uint8_t dataMode) {
  248. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  249. }
  250. // This function is deprecated. New applications should use
  251. // beginTransaction() to configure SPI settings.
  252. inline static void setClockDivider(uint8_t clockDiv) {
  253. SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK);
  254. SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK);
  255. }
  256. // These undocumented functions should not be used. SPI.transfer()
  257. // polls the hardware flag which is automatically cleared as the
  258. // AVR responds to SPI's interrupt
  259. inline static void attachInterrupt() { SPCR |= _BV(SPIE); }
  260. inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); }
  261. private:
  262. static uint8_t interruptMode; // 0=none, 1=mask, 2=global
  263. static uint8_t interruptMask; // which interrupts to mask
  264. static uint8_t interruptSave; // temp storage, to restore state
  265. #ifdef SPI_TRANSACTION_MISMATCH_LED
  266. static uint8_t inTransactionFlag;
  267. #endif
  268. };
  269. /**********************************************************/
  270. /* 32 bit Teensy 3.0 and 3.1 */
  271. /**********************************************************/
  272. #elif defined(__arm__) && defined(TEENSYDUINO)
  273. #define SPI_HAS_NOTUSINGINTERRUPT 1
  274. class SPISettings {
  275. public:
  276. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  277. if (__builtin_constant_p(clock)) {
  278. init_AlwaysInline(clock, bitOrder, dataMode);
  279. } else {
  280. init_MightInline(clock, bitOrder, dataMode);
  281. }
  282. }
  283. SPISettings() {
  284. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  285. }
  286. private:
  287. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  288. init_AlwaysInline(clock, bitOrder, dataMode);
  289. }
  290. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  291. __attribute__((__always_inline__)) {
  292. uint32_t t, c = SPI_CTAR_FMSZ(7);
  293. if (bitOrder == LSBFIRST) c |= SPI_CTAR_LSBFE;
  294. if (__builtin_constant_p(clock)) {
  295. if (clock >= F_BUS / 2) {
  296. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  297. | SPI_CTAR_CSSCK(0);
  298. } else if (clock >= F_BUS / 3) {
  299. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  300. | SPI_CTAR_CSSCK(0);
  301. } else if (clock >= F_BUS / 4) {
  302. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  303. } else if (clock >= F_BUS / 5) {
  304. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  305. | SPI_CTAR_CSSCK(0);
  306. } else if (clock >= F_BUS / 6) {
  307. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  308. } else if (clock >= F_BUS / 8) {
  309. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  310. } else if (clock >= F_BUS / 10) {
  311. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  312. } else if (clock >= F_BUS / 12) {
  313. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  314. } else if (clock >= F_BUS / 16) {
  315. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  316. } else if (clock >= F_BUS / 20) {
  317. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(0);
  318. } else if (clock >= F_BUS / 24) {
  319. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  320. } else if (clock >= F_BUS / 32) {
  321. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(3);
  322. } else if (clock >= F_BUS / 40) {
  323. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  324. } else if (clock >= F_BUS / 56) {
  325. t = SPI_CTAR_PBR(3) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  326. } else if (clock >= F_BUS / 64) {
  327. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4);
  328. } else if (clock >= F_BUS / 96) {
  329. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4);
  330. } else if (clock >= F_BUS / 128) {
  331. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5);
  332. } else if (clock >= F_BUS / 192) {
  333. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5);
  334. } else if (clock >= F_BUS / 256) {
  335. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  336. } else if (clock >= F_BUS / 384) {
  337. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  338. } else if (clock >= F_BUS / 512) {
  339. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7);
  340. } else if (clock >= F_BUS / 640) {
  341. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  342. } else { /* F_BUS / 768 */
  343. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7);
  344. }
  345. } else {
  346. for (uint32_t i=0; i<23; i++) {
  347. t = ctar_clock_table[i];
  348. if (clock >= F_BUS / ctar_div_table[i]) break;
  349. }
  350. }
  351. if (dataMode & 0x08) {
  352. c |= SPI_CTAR_CPOL;
  353. }
  354. if (dataMode & 0x04) {
  355. c |= SPI_CTAR_CPHA;
  356. t = (t & 0xFFFF0FFF) | ((t & 0xF000) >> 4);
  357. }
  358. ctar = c | t;
  359. }
  360. static const uint16_t ctar_div_table[23];
  361. static const uint32_t ctar_clock_table[23];
  362. uint32_t ctar;
  363. friend class SPIClass;
  364. };
  365. class SPIClass {
  366. public:
  367. // Initialize the SPI library
  368. static void begin();
  369. // If SPI is to used from within an interrupt, this function registers
  370. // that interrupt with the SPI library, so beginTransaction() can
  371. // prevent conflicts. The input interruptNumber is the number used
  372. // with attachInterrupt. If SPI is used from a different interrupt
  373. // (eg, a timer), interruptNumber should be 255.
  374. static void usingInterrupt(uint8_t n) {
  375. if (n == 3 || n == 4 || n == 24 || n == 33) {
  376. usingInterrupt(IRQ_PORTA);
  377. } else if (n == 0 || n == 1 || (n >= 16 && n <= 19) || n == 25 || n == 32) {
  378. usingInterrupt(IRQ_PORTB);
  379. } else if ((n >= 9 && n <= 13) || n == 15 || n == 22 || n == 23
  380. || (n >= 27 && n <= 30)) {
  381. usingInterrupt(IRQ_PORTC);
  382. } else if (n == 2 || (n >= 5 && n <= 8) || n == 14 || n == 20 || n == 21) {
  383. usingInterrupt(IRQ_PORTD);
  384. } else if (n == 26 || n == 31) {
  385. usingInterrupt(IRQ_PORTE);
  386. }
  387. }
  388. static void usingInterrupt(IRQ_NUMBER_t interruptName);
  389. static void notUsingInterrupt(IRQ_NUMBER_t interruptName);
  390. // Before using SPI.transfer() or asserting chip select pins,
  391. // this function is used to gain exclusive access to the SPI bus
  392. // and configure the correct settings.
  393. inline static void beginTransaction(SPISettings settings) {
  394. if (interruptMasksUsed) {
  395. __disable_irq();
  396. if (interruptMasksUsed & 0x01) {
  397. interruptSave[0] = NVIC_ICER0 & interruptMask[0];
  398. NVIC_ICER0 = interruptSave[0];
  399. }
  400. #if NVIC_NUM_INTERRUPTS > 32
  401. if (interruptMasksUsed & 0x02) {
  402. interruptSave[1] = NVIC_ICER1 & interruptMask[1];
  403. NVIC_ICER1 = interruptSave[1];
  404. }
  405. #endif
  406. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  407. if (interruptMasksUsed & 0x04) {
  408. interruptSave[2] = NVIC_ICER2 & interruptMask[2];
  409. NVIC_ICER2 = interruptSave[2];
  410. }
  411. #endif
  412. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  413. if (interruptMasksUsed & 0x08) {
  414. interruptSave[3] = NVIC_ICER3 & interruptMask[3];
  415. NVIC_ICER3 = interruptSave[3];
  416. }
  417. #endif
  418. __enable_irq();
  419. }
  420. #ifdef SPI_TRANSACTION_MISMATCH_LED
  421. if (inTransactionFlag) {
  422. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  423. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  424. }
  425. inTransactionFlag = 1;
  426. #endif
  427. if (SPI0_CTAR0 != settings.ctar) {
  428. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  429. SPI0_CTAR0 = settings.ctar;
  430. SPI0_CTAR1 = settings.ctar| SPI_CTAR_FMSZ(8);
  431. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  432. }
  433. }
  434. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  435. inline static uint8_t transfer(uint8_t data) {
  436. SPI0_SR = SPI_SR_TCF;
  437. SPI0_PUSHR = data;
  438. while (!(SPI0_SR & SPI_SR_TCF)) ; // wait
  439. return SPI0_POPR;
  440. }
  441. inline static uint8_t transfer16(uint16_t data) {
  442. SPI0_SR = SPI_SR_TCF;
  443. SPI0_PUSHR = data | SPI_PUSHR_CTAS(1);
  444. while (!(SPI0_SR & SPI_SR_TCF)) ; // wait
  445. return SPI0_POPR;
  446. }
  447. inline static void transfer(void *buf, size_t count) {
  448. if (count == 0) return;
  449. uint8_t *p = (uint8_t *)buf;
  450. SPDR = *p;
  451. while (--count > 0) {
  452. uint8_t out = *(p + 1);
  453. while (!(SPSR & _BV(SPIF))) ;
  454. uint8_t in = SPDR;
  455. SPDR = out;
  456. *p++ = in;
  457. }
  458. while (!(SPSR & _BV(SPIF))) ;
  459. *p = SPDR;
  460. }
  461. // After performing a group of transfers and releasing the chip select
  462. // signal, this function allows others to access the SPI bus
  463. inline static void endTransaction(void) {
  464. #ifdef SPI_TRANSACTION_MISMATCH_LED
  465. if (!inTransactionFlag) {
  466. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  467. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  468. }
  469. inTransactionFlag = 0;
  470. #endif
  471. if (interruptMasksUsed) {
  472. if (interruptMasksUsed & 0x01) {
  473. NVIC_ISER0 = interruptSave[0];
  474. }
  475. #if NVIC_NUM_INTERRUPTS > 32
  476. if (interruptMasksUsed & 0x02) {
  477. NVIC_ISER1 = interruptSave[1];
  478. }
  479. #endif
  480. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  481. if (interruptMasksUsed & 0x04) {
  482. NVIC_ISER2 = interruptSave[2];
  483. }
  484. #endif
  485. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  486. if (interruptMasksUsed & 0x08) {
  487. NVIC_ISER3 = interruptSave[3];
  488. }
  489. #endif
  490. }
  491. }
  492. // Disable the SPI bus
  493. static void end();
  494. // This function is deprecated. New applications should use
  495. // beginTransaction() to configure SPI settings.
  496. static void setBitOrder(uint8_t bitOrder);
  497. // This function is deprecated. New applications should use
  498. // beginTransaction() to configure SPI settings.
  499. static void setDataMode(uint8_t dataMode);
  500. // This function is deprecated. New applications should use
  501. // beginTransaction() to configure SPI settings.
  502. inline static void setClockDivider(uint8_t clockDiv) {
  503. if (clockDiv == SPI_CLOCK_DIV2) {
  504. setClockDivider_noInline(SPISettings(8000000, MSBFIRST, SPI_MODE0).ctar);
  505. } else if (clockDiv == SPI_CLOCK_DIV4) {
  506. setClockDivider_noInline(SPISettings(4000000, MSBFIRST, SPI_MODE0).ctar);
  507. } else if (clockDiv == SPI_CLOCK_DIV8) {
  508. setClockDivider_noInline(SPISettings(2000000, MSBFIRST, SPI_MODE0).ctar);
  509. } else if (clockDiv == SPI_CLOCK_DIV16) {
  510. setClockDivider_noInline(SPISettings(1000000, MSBFIRST, SPI_MODE0).ctar);
  511. } else if (clockDiv == SPI_CLOCK_DIV32) {
  512. setClockDivider_noInline(SPISettings(500000, MSBFIRST, SPI_MODE0).ctar);
  513. } else if (clockDiv == SPI_CLOCK_DIV64) {
  514. setClockDivider_noInline(SPISettings(250000, MSBFIRST, SPI_MODE0).ctar);
  515. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  516. setClockDivider_noInline(SPISettings(125000, MSBFIRST, SPI_MODE0).ctar);
  517. }
  518. }
  519. static void setClockDivider_noInline(uint32_t clk);
  520. // These undocumented functions should not be used. SPI.transfer()
  521. // polls the hardware flag which is automatically cleared as the
  522. // AVR responds to SPI's interrupt
  523. inline static void attachInterrupt() { }
  524. inline static void detachInterrupt() { }
  525. // Teensy 3.x can use alternate pins for these 3 SPI signals.
  526. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  527. SPCR.setMOSI(pin);
  528. }
  529. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  530. SPCR.setMISO(pin);
  531. }
  532. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  533. SPCR.setSCK(pin);
  534. }
  535. // return true if "pin" has special chip select capability
  536. static bool pinIsChipSelect(uint8_t pin);
  537. // return true if both pin1 and pin2 have independent chip select capability
  538. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2);
  539. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  540. static uint8_t setCS(uint8_t pin);
  541. private:
  542. static uint8_t interruptMasksUsed;
  543. static uint32_t interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  544. static uint32_t interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  545. #ifdef SPI_TRANSACTION_MISMATCH_LED
  546. static uint8_t inTransactionFlag;
  547. #endif
  548. };
  549. /**********************************************************/
  550. /* 32 bit Arduino Due */
  551. /**********************************************************/
  552. #elif defined(__arm__) && defined(__SAM3X8E__)
  553. #undef SPI_MODE0
  554. #undef SPI_MODE1
  555. #undef SPI_MODE2
  556. #undef SPI_MODE3
  557. #define SPI_MODE0 0x02
  558. #define SPI_MODE1 0x00
  559. #define SPI_MODE2 0x03
  560. #define SPI_MODE3 0x01
  561. #undef SPI_CLOCK_DIV2
  562. #undef SPI_CLOCK_DIV4
  563. #undef SPI_CLOCK_DIV8
  564. #undef SPI_CLOCK_DIV16
  565. #undef SPI_CLOCK_DIV32
  566. #undef SPI_CLOCK_DIV64
  567. #undef SPI_CLOCK_DIV128
  568. #define SPI_CLOCK_DIV2 11
  569. #define SPI_CLOCK_DIV4 21
  570. #define SPI_CLOCK_DIV8 42
  571. #define SPI_CLOCK_DIV16 84
  572. #define SPI_CLOCK_DIV32 168
  573. #define SPI_CLOCK_DIV64 255
  574. #define SPI_CLOCK_DIV128 255
  575. enum SPITransferMode {
  576. SPI_CONTINUE,
  577. SPI_LAST
  578. };
  579. class SPISettings {
  580. public:
  581. SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
  582. if (__builtin_constant_p(clock)) {
  583. init_AlwaysInline(clock, bitOrder, dataMode);
  584. } else {
  585. init_MightInline(clock, bitOrder, dataMode);
  586. }
  587. }
  588. SPISettings() {
  589. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  590. }
  591. private:
  592. void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
  593. init_AlwaysInline(clock, bitOrder, dataMode);
  594. }
  595. void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode)
  596. __attribute__((__always_inline__)) {
  597. uint8_t div;
  598. border = bitOrder;
  599. if (__builtin_constant_p(clock)) {
  600. if (clock >= F_CPU / 2) div = 2;
  601. else if (clock >= F_CPU / 3) div = 3;
  602. else if (clock >= F_CPU / 4) div = 4;
  603. else if (clock >= F_CPU / 5) div = 5;
  604. else if (clock >= F_CPU / 6) div = 6;
  605. else if (clock >= F_CPU / 7) div = 7;
  606. else if (clock >= F_CPU / 8) div = 8;
  607. else if (clock >= F_CPU / 9) div = 9;
  608. else if (clock >= F_CPU / 10) div = 10;
  609. else if (clock >= F_CPU / 11) div = 11;
  610. else if (clock >= F_CPU / 12) div = 12;
  611. else if (clock >= F_CPU / 13) div = 13;
  612. else if (clock >= F_CPU / 14) div = 14;
  613. else if (clock >= F_CPU / 15) div = 15;
  614. else if (clock >= F_CPU / 16) div = 16;
  615. else if (clock >= F_CPU / 17) div = 17;
  616. else if (clock >= F_CPU / 18) div = 18;
  617. else if (clock >= F_CPU / 19) div = 19;
  618. else if (clock >= F_CPU / 20) div = 20;
  619. else if (clock >= F_CPU / 21) div = 21;
  620. else if (clock >= F_CPU / 22) div = 22;
  621. else if (clock >= F_CPU / 23) div = 23;
  622. else if (clock >= F_CPU / 24) div = 24;
  623. else if (clock >= F_CPU / 25) div = 25;
  624. else if (clock >= F_CPU / 26) div = 26;
  625. else if (clock >= F_CPU / 27) div = 27;
  626. else if (clock >= F_CPU / 28) div = 28;
  627. else if (clock >= F_CPU / 29) div = 29;
  628. else if (clock >= F_CPU / 30) div = 30;
  629. else if (clock >= F_CPU / 31) div = 31;
  630. else if (clock >= F_CPU / 32) div = 32;
  631. else if (clock >= F_CPU / 33) div = 33;
  632. else if (clock >= F_CPU / 34) div = 34;
  633. else if (clock >= F_CPU / 35) div = 35;
  634. else if (clock >= F_CPU / 36) div = 36;
  635. else if (clock >= F_CPU / 37) div = 37;
  636. else if (clock >= F_CPU / 38) div = 38;
  637. else if (clock >= F_CPU / 39) div = 39;
  638. else if (clock >= F_CPU / 40) div = 40;
  639. else if (clock >= F_CPU / 41) div = 41;
  640. else if (clock >= F_CPU / 42) div = 42;
  641. else if (clock >= F_CPU / 43) div = 43;
  642. else if (clock >= F_CPU / 44) div = 44;
  643. else if (clock >= F_CPU / 45) div = 45;
  644. else if (clock >= F_CPU / 46) div = 46;
  645. else if (clock >= F_CPU / 47) div = 47;
  646. else if (clock >= F_CPU / 48) div = 48;
  647. else if (clock >= F_CPU / 49) div = 49;
  648. else if (clock >= F_CPU / 50) div = 50;
  649. else if (clock >= F_CPU / 51) div = 51;
  650. else if (clock >= F_CPU / 52) div = 52;
  651. else if (clock >= F_CPU / 53) div = 53;
  652. else if (clock >= F_CPU / 54) div = 54;
  653. else if (clock >= F_CPU / 55) div = 55;
  654. else if (clock >= F_CPU / 56) div = 56;
  655. else if (clock >= F_CPU / 57) div = 57;
  656. else if (clock >= F_CPU / 58) div = 58;
  657. else if (clock >= F_CPU / 59) div = 59;
  658. else if (clock >= F_CPU / 60) div = 60;
  659. else if (clock >= F_CPU / 61) div = 61;
  660. else if (clock >= F_CPU / 62) div = 62;
  661. else if (clock >= F_CPU / 63) div = 63;
  662. else if (clock >= F_CPU / 64) div = 64;
  663. else if (clock >= F_CPU / 65) div = 65;
  664. else if (clock >= F_CPU / 66) div = 66;
  665. else if (clock >= F_CPU / 67) div = 67;
  666. else if (clock >= F_CPU / 68) div = 68;
  667. else if (clock >= F_CPU / 69) div = 69;
  668. else if (clock >= F_CPU / 70) div = 70;
  669. else if (clock >= F_CPU / 71) div = 71;
  670. else if (clock >= F_CPU / 72) div = 72;
  671. else if (clock >= F_CPU / 73) div = 73;
  672. else if (clock >= F_CPU / 74) div = 74;
  673. else if (clock >= F_CPU / 75) div = 75;
  674. else if (clock >= F_CPU / 76) div = 76;
  675. else if (clock >= F_CPU / 77) div = 77;
  676. else if (clock >= F_CPU / 78) div = 78;
  677. else if (clock >= F_CPU / 79) div = 79;
  678. else if (clock >= F_CPU / 80) div = 80;
  679. else if (clock >= F_CPU / 81) div = 81;
  680. else if (clock >= F_CPU / 82) div = 82;
  681. else if (clock >= F_CPU / 83) div = 83;
  682. else if (clock >= F_CPU / 84) div = 84;
  683. else if (clock >= F_CPU / 85) div = 85;
  684. else if (clock >= F_CPU / 86) div = 86;
  685. else if (clock >= F_CPU / 87) div = 87;
  686. else if (clock >= F_CPU / 88) div = 88;
  687. else if (clock >= F_CPU / 89) div = 89;
  688. else if (clock >= F_CPU / 90) div = 90;
  689. else if (clock >= F_CPU / 91) div = 91;
  690. else if (clock >= F_CPU / 92) div = 92;
  691. else if (clock >= F_CPU / 93) div = 93;
  692. else if (clock >= F_CPU / 94) div = 94;
  693. else if (clock >= F_CPU / 95) div = 95;
  694. else if (clock >= F_CPU / 96) div = 96;
  695. else if (clock >= F_CPU / 97) div = 97;
  696. else if (clock >= F_CPU / 98) div = 98;
  697. else if (clock >= F_CPU / 99) div = 99;
  698. else if (clock >= F_CPU / 100) div = 100;
  699. else if (clock >= F_CPU / 101) div = 101;
  700. else if (clock >= F_CPU / 102) div = 102;
  701. else if (clock >= F_CPU / 103) div = 103;
  702. else if (clock >= F_CPU / 104) div = 104;
  703. else if (clock >= F_CPU / 105) div = 105;
  704. else if (clock >= F_CPU / 106) div = 106;
  705. else if (clock >= F_CPU / 107) div = 107;
  706. else if (clock >= F_CPU / 108) div = 108;
  707. else if (clock >= F_CPU / 109) div = 109;
  708. else if (clock >= F_CPU / 110) div = 110;
  709. else if (clock >= F_CPU / 111) div = 111;
  710. else if (clock >= F_CPU / 112) div = 112;
  711. else if (clock >= F_CPU / 113) div = 113;
  712. else if (clock >= F_CPU / 114) div = 114;
  713. else if (clock >= F_CPU / 115) div = 115;
  714. else if (clock >= F_CPU / 116) div = 116;
  715. else if (clock >= F_CPU / 117) div = 117;
  716. else if (clock >= F_CPU / 118) div = 118;
  717. else if (clock >= F_CPU / 119) div = 119;
  718. else if (clock >= F_CPU / 120) div = 120;
  719. else if (clock >= F_CPU / 121) div = 121;
  720. else if (clock >= F_CPU / 122) div = 122;
  721. else if (clock >= F_CPU / 123) div = 123;
  722. else if (clock >= F_CPU / 124) div = 124;
  723. else if (clock >= F_CPU / 125) div = 125;
  724. else if (clock >= F_CPU / 126) div = 126;
  725. else if (clock >= F_CPU / 127) div = 127;
  726. else if (clock >= F_CPU / 128) div = 128;
  727. else if (clock >= F_CPU / 129) div = 129;
  728. else if (clock >= F_CPU / 130) div = 130;
  729. else if (clock >= F_CPU / 131) div = 131;
  730. else if (clock >= F_CPU / 132) div = 132;
  731. else if (clock >= F_CPU / 133) div = 133;
  732. else if (clock >= F_CPU / 134) div = 134;
  733. else if (clock >= F_CPU / 135) div = 135;
  734. else if (clock >= F_CPU / 136) div = 136;
  735. else if (clock >= F_CPU / 137) div = 137;
  736. else if (clock >= F_CPU / 138) div = 138;
  737. else if (clock >= F_CPU / 139) div = 139;
  738. else if (clock >= F_CPU / 140) div = 140;
  739. else if (clock >= F_CPU / 141) div = 141;
  740. else if (clock >= F_CPU / 142) div = 142;
  741. else if (clock >= F_CPU / 143) div = 143;
  742. else if (clock >= F_CPU / 144) div = 144;
  743. else if (clock >= F_CPU / 145) div = 145;
  744. else if (clock >= F_CPU / 146) div = 146;
  745. else if (clock >= F_CPU / 147) div = 147;
  746. else if (clock >= F_CPU / 148) div = 148;
  747. else if (clock >= F_CPU / 149) div = 149;
  748. else if (clock >= F_CPU / 150) div = 150;
  749. else if (clock >= F_CPU / 151) div = 151;
  750. else if (clock >= F_CPU / 152) div = 152;
  751. else if (clock >= F_CPU / 153) div = 153;
  752. else if (clock >= F_CPU / 154) div = 154;
  753. else if (clock >= F_CPU / 155) div = 155;
  754. else if (clock >= F_CPU / 156) div = 156;
  755. else if (clock >= F_CPU / 157) div = 157;
  756. else if (clock >= F_CPU / 158) div = 158;
  757. else if (clock >= F_CPU / 159) div = 159;
  758. else if (clock >= F_CPU / 160) div = 160;
  759. else if (clock >= F_CPU / 161) div = 161;
  760. else if (clock >= F_CPU / 162) div = 162;
  761. else if (clock >= F_CPU / 163) div = 163;
  762. else if (clock >= F_CPU / 164) div = 164;
  763. else if (clock >= F_CPU / 165) div = 165;
  764. else if (clock >= F_CPU / 166) div = 166;
  765. else if (clock >= F_CPU / 167) div = 167;
  766. else if (clock >= F_CPU / 168) div = 168;
  767. else if (clock >= F_CPU / 169) div = 169;
  768. else if (clock >= F_CPU / 170) div = 170;
  769. else if (clock >= F_CPU / 171) div = 171;
  770. else if (clock >= F_CPU / 172) div = 172;
  771. else if (clock >= F_CPU / 173) div = 173;
  772. else if (clock >= F_CPU / 174) div = 174;
  773. else if (clock >= F_CPU / 175) div = 175;
  774. else if (clock >= F_CPU / 176) div = 176;
  775. else if (clock >= F_CPU / 177) div = 177;
  776. else if (clock >= F_CPU / 178) div = 178;
  777. else if (clock >= F_CPU / 179) div = 179;
  778. else if (clock >= F_CPU / 180) div = 180;
  779. else if (clock >= F_CPU / 181) div = 181;
  780. else if (clock >= F_CPU / 182) div = 182;
  781. else if (clock >= F_CPU / 183) div = 183;
  782. else if (clock >= F_CPU / 184) div = 184;
  783. else if (clock >= F_CPU / 185) div = 185;
  784. else if (clock >= F_CPU / 186) div = 186;
  785. else if (clock >= F_CPU / 187) div = 187;
  786. else if (clock >= F_CPU / 188) div = 188;
  787. else if (clock >= F_CPU / 189) div = 189;
  788. else if (clock >= F_CPU / 190) div = 190;
  789. else if (clock >= F_CPU / 191) div = 191;
  790. else if (clock >= F_CPU / 192) div = 192;
  791. else if (clock >= F_CPU / 193) div = 193;
  792. else if (clock >= F_CPU / 194) div = 194;
  793. else if (clock >= F_CPU / 195) div = 195;
  794. else if (clock >= F_CPU / 196) div = 196;
  795. else if (clock >= F_CPU / 197) div = 197;
  796. else if (clock >= F_CPU / 198) div = 198;
  797. else if (clock >= F_CPU / 199) div = 199;
  798. else if (clock >= F_CPU / 200) div = 200;
  799. else if (clock >= F_CPU / 201) div = 201;
  800. else if (clock >= F_CPU / 202) div = 202;
  801. else if (clock >= F_CPU / 203) div = 203;
  802. else if (clock >= F_CPU / 204) div = 204;
  803. else if (clock >= F_CPU / 205) div = 205;
  804. else if (clock >= F_CPU / 206) div = 206;
  805. else if (clock >= F_CPU / 207) div = 207;
  806. else if (clock >= F_CPU / 208) div = 208;
  807. else if (clock >= F_CPU / 209) div = 209;
  808. else if (clock >= F_CPU / 210) div = 210;
  809. else if (clock >= F_CPU / 211) div = 211;
  810. else if (clock >= F_CPU / 212) div = 212;
  811. else if (clock >= F_CPU / 213) div = 213;
  812. else if (clock >= F_CPU / 214) div = 214;
  813. else if (clock >= F_CPU / 215) div = 215;
  814. else if (clock >= F_CPU / 216) div = 216;
  815. else if (clock >= F_CPU / 217) div = 217;
  816. else if (clock >= F_CPU / 218) div = 218;
  817. else if (clock >= F_CPU / 219) div = 219;
  818. else if (clock >= F_CPU / 220) div = 220;
  819. else if (clock >= F_CPU / 221) div = 221;
  820. else if (clock >= F_CPU / 222) div = 222;
  821. else if (clock >= F_CPU / 223) div = 223;
  822. else if (clock >= F_CPU / 224) div = 224;
  823. else if (clock >= F_CPU / 225) div = 225;
  824. else if (clock >= F_CPU / 226) div = 226;
  825. else if (clock >= F_CPU / 227) div = 227;
  826. else if (clock >= F_CPU / 228) div = 228;
  827. else if (clock >= F_CPU / 229) div = 229;
  828. else if (clock >= F_CPU / 230) div = 230;
  829. else if (clock >= F_CPU / 231) div = 231;
  830. else if (clock >= F_CPU / 232) div = 232;
  831. else if (clock >= F_CPU / 233) div = 233;
  832. else if (clock >= F_CPU / 234) div = 234;
  833. else if (clock >= F_CPU / 235) div = 235;
  834. else if (clock >= F_CPU / 236) div = 236;
  835. else if (clock >= F_CPU / 237) div = 237;
  836. else if (clock >= F_CPU / 238) div = 238;
  837. else if (clock >= F_CPU / 239) div = 239;
  838. else if (clock >= F_CPU / 240) div = 240;
  839. else if (clock >= F_CPU / 241) div = 241;
  840. else if (clock >= F_CPU / 242) div = 242;
  841. else if (clock >= F_CPU / 243) div = 243;
  842. else if (clock >= F_CPU / 244) div = 244;
  843. else if (clock >= F_CPU / 245) div = 245;
  844. else if (clock >= F_CPU / 246) div = 246;
  845. else if (clock >= F_CPU / 247) div = 247;
  846. else if (clock >= F_CPU / 248) div = 248;
  847. else if (clock >= F_CPU / 249) div = 249;
  848. else if (clock >= F_CPU / 250) div = 250;
  849. else if (clock >= F_CPU / 251) div = 251;
  850. else if (clock >= F_CPU / 252) div = 252;
  851. else if (clock >= F_CPU / 253) div = 253;
  852. else if (clock >= F_CPU / 254) div = 254;
  853. else /* clock >= F_CPU / 255 */ div = 255;
  854. /*
  855. #! /usr/bin/perl
  856. for ($i=2; $i<256; $i++) {
  857. printf "\t\t\telse if (clock >= F_CPU / %3d) div = %3d;\n", $i, $i;
  858. }
  859. */
  860. } else {
  861. for (div=2; div<255; div++) {
  862. if (clock >= F_CPU / div) break;
  863. }
  864. }
  865. config = (dataMode & 3) | SPI_CSR_CSAAT | SPI_CSR_SCBR(div) | SPI_CSR_DLYBCT(1);
  866. }
  867. uint32_t config;
  868. BitOrder border;
  869. friend class SPIClass;
  870. };
  871. class SPIClass {
  872. public:
  873. SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void));
  874. byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(BOARD_SPI_DEFAULT_SS, _data, _mode); }
  875. byte transfer(byte _channel, uint8_t _data, SPITransferMode _mode = SPI_LAST);
  876. // Transaction Functions
  877. void usingInterrupt(uint8_t interruptNumber);
  878. void beginTransaction(uint8_t pin, SPISettings settings);
  879. void beginTransaction(SPISettings settings) {
  880. beginTransaction(BOARD_SPI_DEFAULT_SS, settings);
  881. }
  882. void endTransaction(void);
  883. // SPI Configuration methods
  884. void attachInterrupt(void);
  885. void detachInterrupt(void);
  886. void begin(void);
  887. void end(void);
  888. // Attach/Detach pin to/from SPI controller
  889. void begin(uint8_t _pin);
  890. void end(uint8_t _pin);
  891. // These methods sets a parameter on a single pin
  892. void setBitOrder(uint8_t _pin, BitOrder);
  893. void setDataMode(uint8_t _pin, uint8_t);
  894. void setClockDivider(uint8_t _pin, uint8_t);
  895. // These methods sets the same parameters but on default pin BOARD_SPI_DEFAULT_SS
  896. void setBitOrder(BitOrder _order) { setBitOrder(BOARD_SPI_DEFAULT_SS, _order); };
  897. void setDataMode(uint8_t _mode) { setDataMode(BOARD_SPI_DEFAULT_SS, _mode); };
  898. void setClockDivider(uint8_t _div) { setClockDivider(BOARD_SPI_DEFAULT_SS, _div); };
  899. private:
  900. void init();
  901. Spi *spi;
  902. uint32_t id;
  903. BitOrder bitOrder[SPI_CHANNELS_NUM];
  904. uint32_t divider[SPI_CHANNELS_NUM];
  905. uint32_t mode[SPI_CHANNELS_NUM];
  906. void (*initCb)(void);
  907. bool initialized;
  908. uint8_t interruptMode; // 0=none, 1=mask, 2=global
  909. uint8_t interruptMask; // bits 0:3=pin change
  910. uint8_t interruptSave; // temp storage, to restore state
  911. };
  912. #endif
  913. extern SPIClass SPI;
  914. #endif