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.

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