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.

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