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.

985 lines
33KB

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