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.

1842 lines
63KB

  1. /*
  2. * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
  3. * Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
  4. * Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
  5. * SPI Master library for arduino.
  6. *
  7. * This file is free software; you can redistribute it and/or modify
  8. * it under the terms of either the GNU General Public License version 2
  9. * or the GNU Lesser General Public License version 2.1, both as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef _SPI_H_INCLUDED
  13. #define _SPI_H_INCLUDED
  14. #include <Arduino.h>
  15. // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
  16. // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
  17. #define SPI_HAS_TRANSACTION 1
  18. // Uncomment this line to add detection of mismatched begin/end transactions.
  19. // A mismatch occurs if other libraries fail to use SPI.endTransaction() for
  20. // each SPI.beginTransaction(). Connect a LED to this pin. The LED will turn
  21. // on if any mismatch is ever detected.
  22. //#define SPI_TRANSACTION_MISMATCH_LED 5
  23. #ifndef __SAM3X8E__
  24. #ifndef LSBFIRST
  25. #define LSBFIRST 0
  26. #endif
  27. #ifndef MSBFIRST
  28. #define MSBFIRST 1
  29. #endif
  30. #endif
  31. #define SPI_MODE0 0x00
  32. #define SPI_MODE1 0x04
  33. #define SPI_MODE2 0x08
  34. #define SPI_MODE3 0x0C
  35. #define SPI_CLOCK_DIV4 0x00
  36. #define SPI_CLOCK_DIV16 0x01
  37. #define SPI_CLOCK_DIV64 0x02
  38. #define SPI_CLOCK_DIV128 0x03
  39. #define SPI_CLOCK_DIV2 0x04
  40. #define SPI_CLOCK_DIV8 0x05
  41. #define SPI_CLOCK_DIV32 0x06
  42. #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
  43. #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
  44. #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
  45. /**********************************************************/
  46. /* 8 bit AVR-based boards */
  47. /**********************************************************/
  48. #if defined(__AVR__)
  49. // define SPI_AVR_EIMSK for AVR boards with external interrupt pins
  50. #if defined(EIMSK)
  51. #define SPI_AVR_EIMSK EIMSK
  52. #elif defined(GICR)
  53. #define SPI_AVR_EIMSK GICR
  54. #elif defined(GIMSK)
  55. #define SPI_AVR_EIMSK GIMSK
  56. #endif
  57. class SPISettings {
  58. public:
  59. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  60. if (__builtin_constant_p(clock)) {
  61. init_AlwaysInline(clock, bitOrder, dataMode);
  62. } else {
  63. init_MightInline(clock, bitOrder, dataMode);
  64. }
  65. }
  66. SPISettings() {
  67. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  68. }
  69. private:
  70. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  71. init_AlwaysInline(clock, bitOrder, dataMode);
  72. }
  73. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  74. __attribute__((__always_inline__)) {
  75. // Clock settings are defined as follows. Note that this shows SPI2X
  76. // inverted, so the bits form increasing numbers. Also note that
  77. // fosc/64 appears twice
  78. // SPR1 SPR0 ~SPI2X Freq
  79. // 0 0 0 fosc/2
  80. // 0 0 1 fosc/4
  81. // 0 1 0 fosc/8
  82. // 0 1 1 fosc/16
  83. // 1 0 0 fosc/32
  84. // 1 0 1 fosc/64
  85. // 1 1 0 fosc/64
  86. // 1 1 1 fosc/128
  87. // We find the fastest clock that is less than or equal to the
  88. // given clock rate. The clock divider that results in clock_setting
  89. // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
  90. // slowest (128 == 2 ^^ 7, so clock_div = 6).
  91. uint8_t clockDiv;
  92. // When the clock is known at compiletime, use this if-then-else
  93. // cascade, which the compiler knows how to completely optimize
  94. // away. When clock is not known, use a loop instead, which generates
  95. // shorter code.
  96. if (__builtin_constant_p(clock)) {
  97. if (clock >= F_CPU / 2) {
  98. clockDiv = 0;
  99. } else if (clock >= F_CPU / 4) {
  100. clockDiv = 1;
  101. } else if (clock >= F_CPU / 8) {
  102. clockDiv = 2;
  103. } else if (clock >= F_CPU / 16) {
  104. clockDiv = 3;
  105. } else if (clock >= F_CPU / 32) {
  106. clockDiv = 4;
  107. } else if (clock >= F_CPU / 64) {
  108. clockDiv = 5;
  109. } else {
  110. clockDiv = 6;
  111. }
  112. } else {
  113. uint32_t clockSetting = F_CPU / 2;
  114. clockDiv = 0;
  115. while (clockDiv < 6 && clock < clockSetting) {
  116. clockSetting /= 2;
  117. clockDiv++;
  118. }
  119. }
  120. // Compensate for the duplicate fosc/64
  121. if (clockDiv == 6)
  122. clockDiv = 7;
  123. // Invert the SPI2X bit
  124. clockDiv ^= 0x1;
  125. // Pack into the SPISettings class
  126. spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
  127. (dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK);
  128. spsr = clockDiv & SPI_2XCLOCK_MASK;
  129. }
  130. uint8_t spcr;
  131. uint8_t spsr;
  132. friend class SPIClass;
  133. };
  134. class SPIClass {
  135. public:
  136. // Initialize the SPI library
  137. static void begin();
  138. // If SPI is used from within an interrupt, this function registers
  139. // that interrupt with the SPI library, so beginTransaction() can
  140. // prevent conflicts. The input interruptNumber is the number used
  141. // with attachInterrupt. If SPI is used from a different interrupt
  142. // (eg, a timer), interruptNumber should be 255.
  143. static void usingInterrupt(uint8_t interruptNumber);
  144. // Before using SPI.transfer() or asserting chip select pins,
  145. // this function is used to gain exclusive access to the SPI bus
  146. // and configure the correct settings.
  147. inline static void beginTransaction(SPISettings settings) {
  148. if (interruptMode > 0) {
  149. #ifdef SPI_AVR_EIMSK
  150. if (interruptMode == 1) {
  151. interruptSave = SPI_AVR_EIMSK;
  152. SPI_AVR_EIMSK &= ~interruptMask;
  153. } else
  154. #endif
  155. {
  156. uint8_t tmp = SREG;
  157. cli();
  158. interruptSave = tmp;
  159. }
  160. }
  161. #ifdef SPI_TRANSACTION_MISMATCH_LED
  162. if (inTransactionFlag) {
  163. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  164. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  165. }
  166. inTransactionFlag = 1;
  167. #endif
  168. SPCR = settings.spcr;
  169. SPSR = settings.spsr;
  170. }
  171. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  172. inline static uint8_t transfer(uint8_t data) {
  173. SPDR = data;
  174. asm volatile("nop");
  175. while (!(SPSR & _BV(SPIF))) ; // wait
  176. return SPDR;
  177. }
  178. inline static uint16_t transfer16(uint16_t data) {
  179. union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
  180. in.val = data;
  181. if ((SPCR & _BV(DORD))) {
  182. SPDR = in.lsb;
  183. asm volatile("nop");
  184. while (!(SPSR & _BV(SPIF))) ;
  185. out.lsb = SPDR;
  186. SPDR = in.msb;
  187. asm volatile("nop");
  188. while (!(SPSR & _BV(SPIF))) ;
  189. out.msb = SPDR;
  190. } else {
  191. SPDR = in.msb;
  192. asm volatile("nop");
  193. while (!(SPSR & _BV(SPIF))) ;
  194. out.msb = SPDR;
  195. SPDR = in.lsb;
  196. asm volatile("nop");
  197. while (!(SPSR & _BV(SPIF))) ;
  198. out.lsb = SPDR;
  199. }
  200. return out.val;
  201. }
  202. inline static void transfer(void *buf, size_t count) {
  203. if (count == 0) return;
  204. uint8_t *p = (uint8_t *)buf;
  205. SPDR = *p;
  206. while (--count > 0) {
  207. uint8_t out = *(p + 1);
  208. while (!(SPSR & _BV(SPIF))) ;
  209. uint8_t in = SPDR;
  210. SPDR = out;
  211. *p++ = in;
  212. }
  213. while (!(SPSR & _BV(SPIF))) ;
  214. *p = SPDR;
  215. }
  216. // After performing a group of transfers and releasing the chip select
  217. // signal, this function allows others to access the SPI bus
  218. inline static void endTransaction(void) {
  219. #ifdef SPI_TRANSACTION_MISMATCH_LED
  220. if (!inTransactionFlag) {
  221. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  222. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  223. }
  224. inTransactionFlag = 0;
  225. #endif
  226. if (interruptMode > 0) {
  227. #ifdef SPI_AVR_EIMSK
  228. if (interruptMode == 1) {
  229. SPI_AVR_EIMSK = interruptSave;
  230. } else
  231. #endif
  232. {
  233. SREG = interruptSave;
  234. }
  235. }
  236. }
  237. // Disable the SPI bus
  238. static void end();
  239. // This function is deprecated. New applications should use
  240. // beginTransaction() to configure SPI settings.
  241. inline static void setBitOrder(uint8_t bitOrder) {
  242. if (bitOrder == LSBFIRST) SPCR |= _BV(DORD);
  243. else SPCR &= ~(_BV(DORD));
  244. }
  245. // This function is deprecated. New applications should use
  246. // beginTransaction() to configure SPI settings.
  247. inline static void setDataMode(uint8_t dataMode) {
  248. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  249. }
  250. // This function is deprecated. New applications should use
  251. // beginTransaction() to configure SPI settings.
  252. inline static void setClockDivider(uint8_t clockDiv) {
  253. SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK);
  254. SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK);
  255. }
  256. // These undocumented functions should not be used. SPI.transfer()
  257. // polls the hardware flag which is automatically cleared as the
  258. // AVR responds to SPI's interrupt
  259. inline static void attachInterrupt() { SPCR |= _BV(SPIE); }
  260. inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); }
  261. private:
  262. static uint8_t interruptMode; // 0=none, 1=mask, 2=global
  263. static uint8_t interruptMask; // which interrupts to mask
  264. static uint8_t interruptSave; // temp storage, to restore state
  265. #ifdef SPI_TRANSACTION_MISMATCH_LED
  266. static uint8_t inTransactionFlag;
  267. #endif
  268. };
  269. /**********************************************************/
  270. /* 32 bit Teensy 3.0 and 3.1, 3.2, 3.5, 3.6 */
  271. /**********************************************************/
  272. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
  273. #define SPI_HAS_NOTUSINGINTERRUPT 1
  274. class SPISettings {
  275. public:
  276. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  277. if (__builtin_constant_p(clock)) {
  278. init_AlwaysInline(clock, bitOrder, dataMode);
  279. } else {
  280. init_MightInline(clock, bitOrder, dataMode);
  281. }
  282. }
  283. SPISettings() {
  284. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  285. }
  286. private:
  287. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  288. init_AlwaysInline(clock, bitOrder, dataMode);
  289. }
  290. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  291. __attribute__((__always_inline__)) {
  292. uint32_t t, c = SPI_CTAR_FMSZ(7);
  293. if (bitOrder == LSBFIRST) c |= SPI_CTAR_LSBFE;
  294. if (__builtin_constant_p(clock)) {
  295. if (clock >= F_BUS / 2) {
  296. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  297. | SPI_CTAR_CSSCK(0);
  298. } else if (clock >= F_BUS / 3) {
  299. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  300. | SPI_CTAR_CSSCK(0);
  301. } else if (clock >= F_BUS / 4) {
  302. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  303. } else if (clock >= F_BUS / 5) {
  304. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  305. | SPI_CTAR_CSSCK(0);
  306. } else if (clock >= F_BUS / 6) {
  307. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  308. } else if (clock >= F_BUS / 8) {
  309. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  310. } else if (clock >= F_BUS / 10) {
  311. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  312. } else if (clock >= F_BUS / 12) {
  313. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  314. } else if (clock >= F_BUS / 16) {
  315. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  316. } else if (clock >= F_BUS / 20) {
  317. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(0);
  318. } else if (clock >= F_BUS / 24) {
  319. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  320. } else if (clock >= F_BUS / 32) {
  321. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(3);
  322. } else if (clock >= F_BUS / 40) {
  323. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  324. } else if (clock >= F_BUS / 56) {
  325. t = SPI_CTAR_PBR(3) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  326. } else if (clock >= F_BUS / 64) {
  327. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4);
  328. } else if (clock >= F_BUS / 96) {
  329. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4);
  330. } else if (clock >= F_BUS / 128) {
  331. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5);
  332. } else if (clock >= F_BUS / 192) {
  333. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5);
  334. } else if (clock >= F_BUS / 256) {
  335. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  336. } else if (clock >= F_BUS / 384) {
  337. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  338. } else if (clock >= F_BUS / 512) {
  339. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7);
  340. } else if (clock >= F_BUS / 640) {
  341. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  342. } else { /* F_BUS / 768 */
  343. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7);
  344. }
  345. } else {
  346. for (uint32_t i=0; i<23; i++) {
  347. t = ctar_clock_table[i];
  348. if (clock >= F_BUS / ctar_div_table[i]) break;
  349. }
  350. }
  351. if (dataMode & 0x08) {
  352. c |= SPI_CTAR_CPOL;
  353. }
  354. if (dataMode & 0x04) {
  355. c |= SPI_CTAR_CPHA;
  356. t = (t & 0xFFFF0FFF) | ((t & 0xF000) >> 4);
  357. }
  358. ctar = c | t;
  359. }
  360. static const uint16_t ctar_div_table[23];
  361. static const uint32_t ctar_clock_table[23];
  362. uint32_t ctar;
  363. friend class SPIClass;
  364. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  365. friend class SPI1Class;
  366. friend class SPI2Class;
  367. #endif
  368. };
  369. class SPIClass {
  370. public:
  371. // Initialize the SPI library
  372. static void begin();
  373. // If SPI is to used from within an interrupt, this function registers
  374. // that interrupt with the SPI library, so beginTransaction() can
  375. // prevent conflicts. The input interruptNumber is the number used
  376. // with attachInterrupt. If SPI is used from a different interrupt
  377. // (eg, a timer), interruptNumber should be 255.
  378. static void usingInterrupt(uint8_t n) {
  379. if (n == 3 || n == 4 || n == 24 || n == 33) {
  380. usingInterrupt(IRQ_PORTA);
  381. } else if (n == 0 || n == 1 || (n >= 16 && n <= 19) || n == 25 || n == 32) {
  382. usingInterrupt(IRQ_PORTB);
  383. } else if ((n >= 9 && n <= 13) || n == 15 || n == 22 || n == 23
  384. || (n >= 27 && n <= 30)) {
  385. usingInterrupt(IRQ_PORTC);
  386. } else if (n == 2 || (n >= 5 && n <= 8) || n == 14 || n == 20 || n == 21) {
  387. usingInterrupt(IRQ_PORTD);
  388. } else if (n == 26 || n == 31) {
  389. usingInterrupt(IRQ_PORTE);
  390. }
  391. }
  392. static void usingInterrupt(IRQ_NUMBER_t interruptName);
  393. static void notUsingInterrupt(IRQ_NUMBER_t interruptName);
  394. // Before using SPI.transfer() or asserting chip select pins,
  395. // this function is used to gain exclusive access to the SPI bus
  396. // and configure the correct settings.
  397. inline static void beginTransaction(SPISettings settings) {
  398. if (interruptMasksUsed) {
  399. __disable_irq();
  400. if (interruptMasksUsed & 0x01) {
  401. interruptSave[0] = NVIC_ICER0 & interruptMask[0];
  402. NVIC_ICER0 = interruptSave[0];
  403. }
  404. #if NVIC_NUM_INTERRUPTS > 32
  405. if (interruptMasksUsed & 0x02) {
  406. interruptSave[1] = NVIC_ICER1 & interruptMask[1];
  407. NVIC_ICER1 = interruptSave[1];
  408. }
  409. #endif
  410. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  411. if (interruptMasksUsed & 0x04) {
  412. interruptSave[2] = NVIC_ICER2 & interruptMask[2];
  413. NVIC_ICER2 = interruptSave[2];
  414. }
  415. #endif
  416. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  417. if (interruptMasksUsed & 0x08) {
  418. interruptSave[3] = NVIC_ICER3 & interruptMask[3];
  419. NVIC_ICER3 = interruptSave[3];
  420. }
  421. #endif
  422. __enable_irq();
  423. }
  424. #ifdef SPI_TRANSACTION_MISMATCH_LED
  425. if (inTransactionFlag) {
  426. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  427. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  428. }
  429. inTransactionFlag = 1;
  430. #endif
  431. if (SPI0_CTAR0 != settings.ctar) {
  432. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  433. SPI0_CTAR0 = settings.ctar;
  434. SPI0_CTAR1 = settings.ctar| SPI_CTAR_FMSZ(8);
  435. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  436. }
  437. }
  438. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  439. inline static uint8_t transfer(uint8_t data) {
  440. SPI0_SR = SPI_SR_TCF;
  441. SPI0_PUSHR = data;
  442. while (!(SPI0_SR & SPI_SR_TCF)) ; // wait
  443. return SPI0_POPR;
  444. }
  445. inline static uint16_t transfer16(uint16_t data) {
  446. SPI0_SR = SPI_SR_TCF;
  447. SPI0_PUSHR = data | SPI_PUSHR_CTAS(1);
  448. while (!(SPI0_SR & SPI_SR_TCF)) ; // wait
  449. return SPI0_POPR;
  450. }
  451. static void transfer(void *buf, size_t count);
  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(12000000, 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 uint8_t 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. /* Teensy 3.5 and 3.6 have SPI1 as well */
  542. /**********************************************************/
  543. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  544. class SPI1Class {
  545. public:
  546. // Initialize the SPI library
  547. static void begin();
  548. // If SPI is to used from within an interrupt, this function registers
  549. // that interrupt with the SPI library, so beginTransaction() can
  550. // prevent conflicts. The input interruptNumber is the number used
  551. // with attachInterrupt. If SPI is used from a different interrupt
  552. // (eg, a timer), interruptNumber should be 255.
  553. static void usingInterrupt(uint8_t n) {
  554. if (n == 3 || n == 4 || n == 24 || n == 33) {
  555. usingInterrupt(IRQ_PORTA);
  556. } else if (n == 0 || n == 1 || (n >= 16 && n <= 19) || n == 25 || n == 32) {
  557. usingInterrupt(IRQ_PORTB);
  558. } else if ((n >= 9 && n <= 13) || n == 15 || n == 22 || n == 23
  559. || (n >= 27 && n <= 30)) {
  560. usingInterrupt(IRQ_PORTC);
  561. } else if (n == 2 || (n >= 5 && n <= 8) || n == 14 || n == 20 || n == 21) {
  562. usingInterrupt(IRQ_PORTD);
  563. } else if (n == 26 || n == 31) {
  564. usingInterrupt(IRQ_PORTE);
  565. }
  566. }
  567. static void usingInterrupt(IRQ_NUMBER_t interruptName);
  568. static void notUsingInterrupt(IRQ_NUMBER_t interruptName);
  569. // Before using SPI.transfer() or asserting chip select pins,
  570. // this function is used to gain exclusive access to the SPI bus
  571. // and configure the correct settings.
  572. inline static void beginTransaction(SPISettings settings) {
  573. if (interruptMasksUsed) {
  574. __disable_irq();
  575. if (interruptMasksUsed & 0x01) {
  576. interruptSave[0] = NVIC_ICER0 & interruptMask[0];
  577. NVIC_ICER0 = interruptSave[0];
  578. }
  579. #if NVIC_NUM_INTERRUPTS > 32
  580. if (interruptMasksUsed & 0x02) {
  581. interruptSave[1] = NVIC_ICER1 & interruptMask[1];
  582. NVIC_ICER1 = interruptSave[1];
  583. }
  584. #endif
  585. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  586. if (interruptMasksUsed & 0x04) {
  587. interruptSave[2] = NVIC_ICER2 & interruptMask[2];
  588. NVIC_ICER2 = interruptSave[2];
  589. }
  590. #endif
  591. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  592. if (interruptMasksUsed & 0x08) {
  593. interruptSave[3] = NVIC_ICER3 & interruptMask[3];
  594. NVIC_ICER3 = interruptSave[3];
  595. }
  596. #endif
  597. __enable_irq();
  598. }
  599. #ifdef SPI_TRANSACTION_MISMATCH_LED
  600. if (inTransactionFlag) {
  601. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  602. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  603. }
  604. inTransactionFlag = 1;
  605. #endif
  606. if (SPI1_CTAR0 != settings.ctar) {
  607. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  608. SPI1_CTAR0 = settings.ctar;
  609. SPI1_CTAR1 = settings.ctar| SPI_CTAR_FMSZ(8);
  610. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  611. }
  612. }
  613. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  614. inline static uint8_t transfer(uint8_t data) {
  615. SPI1_SR = SPI_SR_TCF;
  616. SPI1_PUSHR = data;
  617. while (!(SPI1_SR & SPI_SR_TCF)) ; // wait
  618. return SPI1_POPR;
  619. }
  620. inline static uint16_t transfer16(uint16_t data) {
  621. SPI1_SR = SPI_SR_TCF;
  622. SPI1_PUSHR = data | SPI_PUSHR_CTAS(1);
  623. while (!(SPI1_SR & SPI_SR_TCF)) ; // wait
  624. return SPI1_POPR;
  625. }
  626. static void transfer(void *buf, size_t count);
  627. // After performing a group of transfers and releasing the chip select
  628. // signal, this function allows others to access the SPI bus
  629. inline static void endTransaction(void) {
  630. #ifdef SPI_TRANSACTION_MISMATCH_LED
  631. if (!inTransactionFlag) {
  632. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  633. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  634. }
  635. inTransactionFlag = 0;
  636. #endif
  637. if (interruptMasksUsed) {
  638. if (interruptMasksUsed & 0x01) {
  639. NVIC_ISER0 = interruptSave[0];
  640. }
  641. #if NVIC_NUM_INTERRUPTS > 32
  642. if (interruptMasksUsed & 0x02) {
  643. NVIC_ISER1 = interruptSave[1];
  644. }
  645. #endif
  646. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  647. if (interruptMasksUsed & 0x04) {
  648. NVIC_ISER2 = interruptSave[2];
  649. }
  650. #endif
  651. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  652. if (interruptMasksUsed & 0x08) {
  653. NVIC_ISER3 = interruptSave[3];
  654. }
  655. #endif
  656. }
  657. }
  658. // Disable the SPI bus
  659. static void end();
  660. // This function is deprecated. New applications should use
  661. // beginTransaction() to configure SPI settings.
  662. static void setBitOrder(uint8_t bitOrder);
  663. // This function is deprecated. New applications should use
  664. // beginTransaction() to configure SPI settings.
  665. static void setDataMode(uint8_t dataMode);
  666. // This function is deprecated. New applications should use
  667. // beginTransaction() to configure SPI settings.
  668. inline static void setClockDivider(uint8_t clockDiv) {
  669. if (clockDiv == SPI_CLOCK_DIV2) {
  670. setClockDivider_noInline(SPISettings(12000000, MSBFIRST, SPI_MODE0).ctar);
  671. } else if (clockDiv == SPI_CLOCK_DIV4) {
  672. setClockDivider_noInline(SPISettings(4000000, MSBFIRST, SPI_MODE0).ctar);
  673. } else if (clockDiv == SPI_CLOCK_DIV8) {
  674. setClockDivider_noInline(SPISettings(2000000, MSBFIRST, SPI_MODE0).ctar);
  675. } else if (clockDiv == SPI_CLOCK_DIV16) {
  676. setClockDivider_noInline(SPISettings(1000000, MSBFIRST, SPI_MODE0).ctar);
  677. } else if (clockDiv == SPI_CLOCK_DIV32) {
  678. setClockDivider_noInline(SPISettings(500000, MSBFIRST, SPI_MODE0).ctar);
  679. } else if (clockDiv == SPI_CLOCK_DIV64) {
  680. setClockDivider_noInline(SPISettings(250000, MSBFIRST, SPI_MODE0).ctar);
  681. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  682. setClockDivider_noInline(SPISettings(125000, MSBFIRST, SPI_MODE0).ctar);
  683. }
  684. }
  685. static void setClockDivider_noInline(uint32_t clk);
  686. // These undocumented functions should not be used. SPI.transfer()
  687. // polls the hardware flag which is automatically cleared as the
  688. // AVR responds to SPI's interrupt
  689. inline static void attachInterrupt() { }
  690. inline static void detachInterrupt() { }
  691. // Teensy 3.x can use alternate pins for these 3 SPI signals.
  692. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  693. SPCR1.setMOSI(pin);
  694. }
  695. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  696. SPCR1.setMISO(pin);
  697. }
  698. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  699. SPCR1.setSCK(pin);
  700. }
  701. // return true if "pin" has special chip select capability
  702. static uint8_t pinIsChipSelect(uint8_t pin);
  703. // return true if both pin1 and pin2 have independent chip select capability
  704. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2);
  705. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  706. static uint8_t setCS(uint8_t pin);
  707. private:
  708. static uint8_t interruptMasksUsed;
  709. static uint32_t interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  710. static uint32_t interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  711. #ifdef SPI_TRANSACTION_MISMATCH_LED
  712. static uint8_t inTransactionFlag;
  713. #endif
  714. };
  715. class SPI2Class {
  716. public:
  717. // Initialize the SPI library
  718. static void begin();
  719. // If SPI is to used from within an interrupt, this function registers
  720. // that interrupt with the SPI library, so beginTransaction() can
  721. // prevent conflicts. The input interruptNumber is the number used
  722. // with attachInterrupt. If SPI is used from a different interrupt
  723. // (eg, a timer), interruptNumber should be 255.
  724. static void usingInterrupt(uint8_t n) {
  725. if (n == 3 || n == 4 || n == 24 || n == 33) {
  726. usingInterrupt(IRQ_PORTA);
  727. } else if (n == 0 || n == 1 || (n >= 16 && n <= 19) || n == 25 || n == 32) {
  728. usingInterrupt(IRQ_PORTB);
  729. } else if ((n >= 9 && n <= 13) || n == 15 || n == 22 || n == 23
  730. || (n >= 27 && n <= 30)) {
  731. usingInterrupt(IRQ_PORTC);
  732. } else if (n == 2 || (n >= 5 && n <= 8) || n == 14 || n == 20 || n == 21) {
  733. usingInterrupt(IRQ_PORTD);
  734. } else if (n == 26 || n == 31) {
  735. usingInterrupt(IRQ_PORTE);
  736. }
  737. }
  738. static void usingInterrupt(IRQ_NUMBER_t interruptName);
  739. static void notUsingInterrupt(IRQ_NUMBER_t interruptName);
  740. // Before using SPI.transfer() or asserting chip select pins,
  741. // this function is used to gain exclusive access to the SPI bus
  742. // and configure the correct settings.
  743. inline static void beginTransaction(SPISettings settings) {
  744. if (interruptMasksUsed) {
  745. __disable_irq();
  746. if (interruptMasksUsed & 0x01) {
  747. interruptSave[0] = NVIC_ICER0 & interruptMask[0];
  748. NVIC_ICER0 = interruptSave[0];
  749. }
  750. #if NVIC_NUM_INTERRUPTS > 32
  751. if (interruptMasksUsed & 0x02) {
  752. interruptSave[1] = NVIC_ICER1 & interruptMask[1];
  753. NVIC_ICER1 = interruptSave[1];
  754. }
  755. #endif
  756. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  757. if (interruptMasksUsed & 0x04) {
  758. interruptSave[2] = NVIC_ICER2 & interruptMask[2];
  759. NVIC_ICER2 = interruptSave[2];
  760. }
  761. #endif
  762. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  763. if (interruptMasksUsed & 0x08) {
  764. interruptSave[3] = NVIC_ICER3 & interruptMask[3];
  765. NVIC_ICER3 = interruptSave[3];
  766. }
  767. #endif
  768. __enable_irq();
  769. }
  770. #ifdef SPI_TRANSACTION_MISMATCH_LED
  771. if (inTransactionFlag) {
  772. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  773. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  774. }
  775. inTransactionFlag = 1;
  776. #endif
  777. if (SPI2_CTAR0 != settings.ctar) {
  778. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  779. SPI2_CTAR0 = settings.ctar;
  780. SPI2_CTAR1 = settings.ctar| SPI_CTAR_FMSZ(8);
  781. SPI2_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  782. }
  783. }
  784. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  785. inline static uint8_t transfer(uint8_t data) {
  786. SPI2_SR = SPI_SR_TCF;
  787. SPI2_PUSHR = data;
  788. while (!(SPI2_SR & SPI_SR_TCF)) ; // wait
  789. return SPI2_POPR;
  790. }
  791. inline static uint16_t transfer16(uint16_t data) {
  792. SPI2_SR = SPI_SR_TCF;
  793. SPI2_PUSHR = data | SPI_PUSHR_CTAS(1);
  794. while (!(SPI2_SR & SPI_SR_TCF)) ; // wait
  795. return SPI2_POPR;
  796. }
  797. static void transfer(void *buf, size_t count);
  798. // After performing a group of transfers and releasing the chip select
  799. // signal, this function allows others to access the SPI bus
  800. inline static void endTransaction(void) {
  801. #ifdef SPI_TRANSACTION_MISMATCH_LED
  802. if (!inTransactionFlag) {
  803. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  804. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  805. }
  806. inTransactionFlag = 0;
  807. #endif
  808. if (interruptMasksUsed) {
  809. if (interruptMasksUsed & 0x01) {
  810. NVIC_ISER0 = interruptSave[0];
  811. }
  812. #if NVIC_NUM_INTERRUPTS > 32
  813. if (interruptMasksUsed & 0x02) {
  814. NVIC_ISER1 = interruptSave[1];
  815. }
  816. #endif
  817. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  818. if (interruptMasksUsed & 0x04) {
  819. NVIC_ISER2 = interruptSave[2];
  820. }
  821. #endif
  822. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  823. if (interruptMasksUsed & 0x08) {
  824. NVIC_ISER3 = interruptSave[3];
  825. }
  826. #endif
  827. }
  828. }
  829. // Disable the SPI bus
  830. static void end();
  831. // This function is deprecated. New applications should use
  832. // beginTransaction() to configure SPI settings.
  833. static void setBitOrder(uint8_t bitOrder);
  834. // This function is deprecated. New applications should use
  835. // beginTransaction() to configure SPI settings.
  836. static void setDataMode(uint8_t dataMode);
  837. // This function is deprecated. New applications should use
  838. // beginTransaction() to configure SPI settings.
  839. inline static void setClockDivider(uint8_t clockDiv) {
  840. if (clockDiv == SPI_CLOCK_DIV2) {
  841. setClockDivider_noInline(SPISettings(12000000, MSBFIRST, SPI_MODE0).ctar);
  842. } else if (clockDiv == SPI_CLOCK_DIV4) {
  843. setClockDivider_noInline(SPISettings(4000000, MSBFIRST, SPI_MODE0).ctar);
  844. } else if (clockDiv == SPI_CLOCK_DIV8) {
  845. setClockDivider_noInline(SPISettings(2000000, MSBFIRST, SPI_MODE0).ctar);
  846. } else if (clockDiv == SPI_CLOCK_DIV16) {
  847. setClockDivider_noInline(SPISettings(1000000, MSBFIRST, SPI_MODE0).ctar);
  848. } else if (clockDiv == SPI_CLOCK_DIV32) {
  849. setClockDivider_noInline(SPISettings(500000, MSBFIRST, SPI_MODE0).ctar);
  850. } else if (clockDiv == SPI_CLOCK_DIV64) {
  851. setClockDivider_noInline(SPISettings(250000, MSBFIRST, SPI_MODE0).ctar);
  852. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  853. setClockDivider_noInline(SPISettings(125000, MSBFIRST, SPI_MODE0).ctar);
  854. }
  855. }
  856. static void setClockDivider_noInline(uint32_t clk);
  857. // These undocumented functions should not be used. SPI.transfer()
  858. // polls the hardware flag which is automatically cleared as the
  859. // AVR responds to SPI's interrupt
  860. inline static void attachInterrupt() { }
  861. inline static void detachInterrupt() { }
  862. // Teensy 3.x can use alternate pins for these 3 SPI signals.
  863. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  864. SPCR2.setMOSI(pin);
  865. }
  866. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  867. SPCR2.setMISO(pin);
  868. }
  869. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  870. SPCR2.setSCK(pin);
  871. }
  872. // return true if "pin" has special chip select capability
  873. static uint8_t pinIsChipSelect(uint8_t pin);
  874. // return true if both pin1 and pin2 have independent chip select capability
  875. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2);
  876. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  877. static uint8_t setCS(uint8_t pin);
  878. private:
  879. static uint8_t interruptMasksUsed;
  880. static uint32_t interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  881. static uint32_t interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  882. #ifdef SPI_TRANSACTION_MISMATCH_LED
  883. static uint8_t inTransactionFlag;
  884. #endif
  885. };
  886. #endif
  887. /**********************************************************/
  888. /* 32 bit Teensy-LC */
  889. /**********************************************************/
  890. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  891. class SPISettings {
  892. public:
  893. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  894. if (__builtin_constant_p(clock)) {
  895. init_AlwaysInline(clock, bitOrder, dataMode);
  896. } else {
  897. init_MightInline(clock, bitOrder, dataMode);
  898. }
  899. }
  900. SPISettings() {
  901. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  902. }
  903. private:
  904. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  905. init_AlwaysInline(clock, bitOrder, dataMode);
  906. }
  907. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  908. __attribute__((__always_inline__)) {
  909. uint8_t c = SPI_C1_MSTR | SPI_C1_SPE;
  910. if (dataMode & 0x04) c |= SPI_C1_CPHA;
  911. if (dataMode & 0x08) c |= SPI_C1_CPOL;
  912. if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
  913. c1 = c;
  914. if (__builtin_constant_p(clock)) {
  915. if (clock >= F_BUS / 2) { c = SPI_BR_SPPR(0) | SPI_BR_SPR(0);
  916. } else if (clock >= F_BUS / 4) { c = SPI_BR_SPPR(1) | SPI_BR_SPR(0);
  917. } else if (clock >= F_BUS / 6) { c = SPI_BR_SPPR(2) | SPI_BR_SPR(0);
  918. } else if (clock >= F_BUS / 8) { c = SPI_BR_SPPR(3) | SPI_BR_SPR(0);
  919. } else if (clock >= F_BUS / 10) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(0);
  920. } else if (clock >= F_BUS / 12) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(0);
  921. } else if (clock >= F_BUS / 14) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(0);
  922. } else if (clock >= F_BUS / 16) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(0);
  923. } else if (clock >= F_BUS / 20) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(1);
  924. } else if (clock >= F_BUS / 24) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(1);
  925. } else if (clock >= F_BUS / 28) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(1);
  926. } else if (clock >= F_BUS / 32) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(1);
  927. } else if (clock >= F_BUS / 40) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(2);
  928. } else if (clock >= F_BUS / 48) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(2);
  929. } else if (clock >= F_BUS / 56) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(2);
  930. } else if (clock >= F_BUS / 64) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(2);
  931. } else if (clock >= F_BUS / 80) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(3);
  932. } else if (clock >= F_BUS / 96) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(3);
  933. } else if (clock >= F_BUS / 112) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(3);
  934. } else if (clock >= F_BUS / 128) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(3);
  935. } else if (clock >= F_BUS / 160) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(4);
  936. } else if (clock >= F_BUS / 192) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(4);
  937. } else if (clock >= F_BUS / 224) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(4);
  938. } else if (clock >= F_BUS / 256) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(4);
  939. } else if (clock >= F_BUS / 320) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(5);
  940. } else if (clock >= F_BUS / 384) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(5);
  941. } else if (clock >= F_BUS / 448) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(5);
  942. } else if (clock >= F_BUS / 512) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(5);
  943. } else if (clock >= F_BUS / 640) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(6);
  944. } else /* F_BUS / 768 */ { c = SPI_BR_SPPR(5) | SPI_BR_SPR(6);
  945. }
  946. } else {
  947. for (uint32_t i=0; i<30; i++) {
  948. c = br_clock_table[i];
  949. if (clock >= F_BUS / br_div_table[i]) break;
  950. }
  951. }
  952. br0 = c;
  953. if (__builtin_constant_p(clock)) {
  954. if (clock >= (F_PLL/2) / 2) { c = SPI_BR_SPPR(0) | SPI_BR_SPR(0);
  955. } else if (clock >= (F_PLL/2) / 4) { c = SPI_BR_SPPR(1) | SPI_BR_SPR(0);
  956. } else if (clock >= (F_PLL/2) / 6) { c = SPI_BR_SPPR(2) | SPI_BR_SPR(0);
  957. } else if (clock >= (F_PLL/2) / 8) { c = SPI_BR_SPPR(3) | SPI_BR_SPR(0);
  958. } else if (clock >= (F_PLL/2) / 10) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(0);
  959. } else if (clock >= (F_PLL/2) / 12) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(0);
  960. } else if (clock >= (F_PLL/2) / 14) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(0);
  961. } else if (clock >= (F_PLL/2) / 16) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(0);
  962. } else if (clock >= (F_PLL/2) / 20) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(1);
  963. } else if (clock >= (F_PLL/2) / 24) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(1);
  964. } else if (clock >= (F_PLL/2) / 28) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(1);
  965. } else if (clock >= (F_PLL/2) / 32) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(1);
  966. } else if (clock >= (F_PLL/2) / 40) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(2);
  967. } else if (clock >= (F_PLL/2) / 48) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(2);
  968. } else if (clock >= (F_PLL/2) / 56) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(2);
  969. } else if (clock >= (F_PLL/2) / 64) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(2);
  970. } else if (clock >= (F_PLL/2) / 80) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(3);
  971. } else if (clock >= (F_PLL/2) / 96) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(3);
  972. } else if (clock >= (F_PLL/2) / 112) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(3);
  973. } else if (clock >= (F_PLL/2) / 128) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(3);
  974. } else if (clock >= (F_PLL/2) / 160) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(4);
  975. } else if (clock >= (F_PLL/2) / 192) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(4);
  976. } else if (clock >= (F_PLL/2) / 224) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(4);
  977. } else if (clock >= (F_PLL/2) / 256) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(4);
  978. } else if (clock >= (F_PLL/2) / 320) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(5);
  979. } else if (clock >= (F_PLL/2) / 384) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(5);
  980. } else if (clock >= (F_PLL/2) / 448) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(5);
  981. } else if (clock >= (F_PLL/2) / 512) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(5);
  982. } else if (clock >= (F_PLL/2) / 640) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(6);
  983. } else /* (F_PLL/2) / 768 */ { c = SPI_BR_SPPR(5) | SPI_BR_SPR(6);
  984. }
  985. } else {
  986. for (uint32_t i=0; i<30; i++) {
  987. c = br_clock_table[i];
  988. if (clock >= (F_PLL/2) / br_div_table[i]) break;
  989. }
  990. }
  991. br1 = c;
  992. }
  993. static const uint8_t br_clock_table[30];
  994. static const uint16_t br_div_table[30];
  995. uint8_t c1, br0, br1;
  996. friend class SPIClass;
  997. friend class SPI1Class;
  998. };
  999. class SPIClass {
  1000. public:
  1001. // Initialize the SPI library
  1002. static void begin();
  1003. // If SPI is to used from within an interrupt, this function registers
  1004. // that interrupt with the SPI library, so beginTransaction() can
  1005. // prevent conflicts. The input interruptNumber is the number used
  1006. // with attachInterrupt. If SPI is used from a different interrupt
  1007. // (eg, a timer), interruptNumber should be 255.
  1008. static void usingInterrupt(uint8_t n) {
  1009. if (n == 3 || n == 4) {
  1010. usingInterrupt(IRQ_PORTA);
  1011. } else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) {
  1012. usingInterrupt(IRQ_PORTCD);
  1013. }
  1014. }
  1015. static void usingInterrupt(IRQ_NUMBER_t interruptName) {
  1016. uint32_t n = (uint32_t)interruptName;
  1017. if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n);
  1018. }
  1019. static void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
  1020. uint32_t n = (uint32_t)interruptName;
  1021. if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n);
  1022. }
  1023. // Before using SPI.transfer() or asserting chip select pins,
  1024. // this function is used to gain exclusive access to the SPI bus
  1025. // and configure the correct settings.
  1026. inline static void beginTransaction(SPISettings settings) {
  1027. if (interruptMask) {
  1028. __disable_irq();
  1029. interruptSave = NVIC_ICER0 & interruptMask;
  1030. NVIC_ICER0 = interruptSave;
  1031. __enable_irq();
  1032. }
  1033. #ifdef SPI_TRANSACTION_MISMATCH_LED
  1034. if (inTransactionFlag) {
  1035. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  1036. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  1037. }
  1038. inTransactionFlag = 1;
  1039. #endif
  1040. SPI0_C1 = settings.c1;
  1041. SPI0_BR = settings.br0;
  1042. }
  1043. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  1044. inline static uint8_t transfer(uint8_t data) {
  1045. SPI0_DL = data;
  1046. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  1047. return SPI0_DL;
  1048. }
  1049. inline static uint16_t transfer16(uint16_t data) {
  1050. SPI0_C2 = SPI_C2_SPIMODE;
  1051. SPI0_S;
  1052. SPI0_DL = data;
  1053. SPI0_DH = data >> 8;
  1054. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  1055. uint16_t r = SPI0_DL | (SPI0_DH << 8);
  1056. SPI0_C2 = 0;
  1057. SPI0_S;
  1058. return r;
  1059. }
  1060. inline static void transfer(void *buf, size_t count) {
  1061. if (count == 0) return;
  1062. uint8_t *p = (uint8_t *)buf;
  1063. while (!(SPI0_S & SPI_S_SPTEF)) ; // wait
  1064. SPI0_DL = *p;
  1065. while (--count > 0) {
  1066. uint8_t out = *(p + 1);
  1067. while (!(SPI0_S & SPI_S_SPTEF)) ; // wait
  1068. __disable_irq();
  1069. SPI0_DL = out;
  1070. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  1071. uint8_t in = SPI0_DL;
  1072. __enable_irq();
  1073. *p++ = in;
  1074. }
  1075. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  1076. *p = SPDR;
  1077. }
  1078. // After performing a group of transfers and releasing the chip select
  1079. // signal, this function allows others to access the SPI bus
  1080. inline static void endTransaction(void) {
  1081. #ifdef SPI_TRANSACTION_MISMATCH_LED
  1082. if (!inTransactionFlag) {
  1083. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  1084. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  1085. }
  1086. inTransactionFlag = 0;
  1087. #endif
  1088. if (interruptMask) {
  1089. NVIC_ISER0 = interruptSave;
  1090. }
  1091. }
  1092. // Disable the SPI bus
  1093. static void end();
  1094. // This function is deprecated. New applications should use
  1095. // beginTransaction() to configure SPI settings.
  1096. static void setBitOrder(uint8_t bitOrder) {
  1097. uint8_t c = SPI0_C1 | SPI_C1_SPE;
  1098. if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
  1099. else c &= ~SPI_C1_LSBFE;
  1100. SPI0_C1 = c;
  1101. }
  1102. // This function is deprecated. New applications should use
  1103. // beginTransaction() to configure SPI settings.
  1104. static void setDataMode(uint8_t dataMode) {
  1105. uint8_t c = SPI0_C1 | SPI_C1_SPE;
  1106. if (dataMode & 0x04) c |= SPI_C1_CPHA;
  1107. else c &= ~SPI_C1_CPHA;
  1108. if (dataMode & 0x08) c |= SPI_C1_CPOL;
  1109. else c &= ~SPI_C1_CPOL;
  1110. SPI0_C1 = c;
  1111. }
  1112. // This function is deprecated. New applications should use
  1113. // beginTransaction() to configure SPI settings.
  1114. inline static void setClockDivider(uint8_t clockDiv) {
  1115. if (clockDiv == SPI_CLOCK_DIV2) {
  1116. SPI0_BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br0);
  1117. } else if (clockDiv == SPI_CLOCK_DIV4) {
  1118. SPI0_BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br0);
  1119. } else if (clockDiv == SPI_CLOCK_DIV8) {
  1120. SPI0_BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br0);
  1121. } else if (clockDiv == SPI_CLOCK_DIV16) {
  1122. SPI0_BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br0);
  1123. } else if (clockDiv == SPI_CLOCK_DIV32) {
  1124. SPI0_BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br0);
  1125. } else if (clockDiv == SPI_CLOCK_DIV64) {
  1126. SPI0_BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br0);
  1127. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  1128. SPI0_BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br0);
  1129. }
  1130. }
  1131. // These undocumented functions should not be used. SPI.transfer()
  1132. // polls the hardware flag which is automatically cleared as the
  1133. // AVR responds to SPI's interrupt
  1134. inline static void attachInterrupt() { }
  1135. inline static void detachInterrupt() { }
  1136. // Teensy LC can use alternate pins for these 3 SPI signals.
  1137. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1138. SPCR.setMOSI(pin);
  1139. }
  1140. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1141. SPCR.setMISO(pin);
  1142. }
  1143. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1144. SPCR.setSCK(pin);
  1145. }
  1146. // return true if "pin" has special chip select capability
  1147. static bool pinIsChipSelect(uint8_t pin) { return (pin == 10 || pin == 2); }
  1148. // return true if both pin1 and pin2 have independent chip select capability
  1149. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
  1150. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  1151. static uint8_t setCS(uint8_t pin);
  1152. private:
  1153. static uint32_t interruptMask;
  1154. static uint32_t interruptSave;
  1155. #ifdef SPI_TRANSACTION_MISMATCH_LED
  1156. static uint8_t inTransactionFlag;
  1157. #endif
  1158. };
  1159. class SPI1Class {
  1160. public:
  1161. // Initialize the SPI library
  1162. static void begin();
  1163. // If SPI is to used from within an interrupt, this function registers
  1164. // that interrupt with the SPI library, so beginTransaction() can
  1165. // prevent conflicts. The input interruptNumber is the number used
  1166. // with attachInterrupt. If SPI is used from a different interrupt
  1167. // (eg, a timer), interruptNumber should be 255.
  1168. static void usingInterrupt(uint8_t n) {
  1169. if (n == 3 || n == 4) {
  1170. usingInterrupt(IRQ_PORTA);
  1171. } else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) {
  1172. usingInterrupt(IRQ_PORTCD);
  1173. }
  1174. }
  1175. static void usingInterrupt(IRQ_NUMBER_t interruptName) {
  1176. uint32_t n = (uint32_t)interruptName;
  1177. if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n);
  1178. }
  1179. static void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
  1180. uint32_t n = (uint32_t)interruptName;
  1181. if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n);
  1182. }
  1183. // Before using SPI.transfer() or asserting chip select pins,
  1184. // this function is used to gain exclusive access to the SPI bus
  1185. // and configure the correct settings.
  1186. inline static void beginTransaction(SPISettings settings) {
  1187. if (interruptMask) {
  1188. __disable_irq();
  1189. interruptSave = NVIC_ICER0 & interruptMask;
  1190. NVIC_ICER0 = interruptSave;
  1191. __enable_irq();
  1192. }
  1193. #ifdef SPI_TRANSACTION_MISMATCH_LED
  1194. if (inTransactionFlag) {
  1195. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  1196. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  1197. }
  1198. inTransactionFlag = 1;
  1199. #endif
  1200. SPI1_C1 = settings.c1;
  1201. SPI1_BR = settings.br1;
  1202. }
  1203. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  1204. inline static uint8_t transfer(uint8_t data) {
  1205. SPI1_DL = data;
  1206. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  1207. return SPI1_DL;
  1208. }
  1209. inline static uint16_t transfer16(uint16_t data) {
  1210. SPI1_C2 = SPI_C2_SPIMODE;
  1211. SPI1_S;
  1212. SPI1_DL = data;
  1213. SPI1_DH = data >> 8;
  1214. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  1215. uint16_t r = SPI1_DL | (SPI1_DH << 8);
  1216. SPI1_C2 = 0;
  1217. SPI1_S;
  1218. return r;
  1219. }
  1220. inline static void transfer(void *buf, size_t count) {
  1221. if (count == 0) return;
  1222. uint8_t *p = (uint8_t *)buf;
  1223. while (!(SPI1_S & SPI_S_SPTEF)) ; // wait
  1224. SPI1_DL = *p;
  1225. while (--count > 0) {
  1226. uint8_t out = *(p + 1);
  1227. while (!(SPI1_S & SPI_S_SPTEF)) ; // wait
  1228. __disable_irq();
  1229. SPI1_DL = out;
  1230. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  1231. uint8_t in = SPI1_DL;
  1232. __enable_irq();
  1233. *p++ = in;
  1234. }
  1235. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  1236. *p = SPDR;
  1237. }
  1238. // After performing a group of transfers and releasing the chip select
  1239. // signal, this function allows others to access the SPI bus
  1240. inline static void endTransaction(void) {
  1241. #ifdef SPI_TRANSACTION_MISMATCH_LED
  1242. if (!inTransactionFlag) {
  1243. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  1244. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  1245. }
  1246. inTransactionFlag = 0;
  1247. #endif
  1248. if (interruptMask) {
  1249. NVIC_ISER0 = interruptSave;
  1250. }
  1251. }
  1252. // Disable the SPI bus
  1253. static void end();
  1254. // This function is deprecated. New applications should use
  1255. // beginTransaction() to configure SPI settings.
  1256. static void setBitOrder(uint8_t bitOrder) {
  1257. uint8_t c = SPI1_C1 | SPI_C1_SPE;
  1258. if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
  1259. else c &= ~SPI_C1_LSBFE;
  1260. SPI1_C1 = c;
  1261. }
  1262. // This function is deprecated. New applications should use
  1263. // beginTransaction() to configure SPI settings.
  1264. static void setDataMode(uint8_t dataMode) {
  1265. uint8_t c = SPI1_C1 | SPI_C1_SPE;
  1266. if (dataMode & 0x04) c |= SPI_C1_CPHA;
  1267. else c &= ~SPI_C1_CPHA;
  1268. if (dataMode & 0x08) c |= SPI_C1_CPOL;
  1269. else c &= ~SPI_C1_CPOL;
  1270. SPI1_C1 = c;
  1271. }
  1272. // This function is deprecated. New applications should use
  1273. // beginTransaction() to configure SPI settings.
  1274. inline static void setClockDivider(uint8_t clockDiv) {
  1275. if (clockDiv == SPI_CLOCK_DIV2) {
  1276. SPI1_BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br1);
  1277. } else if (clockDiv == SPI_CLOCK_DIV4) {
  1278. SPI1_BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br1);
  1279. } else if (clockDiv == SPI_CLOCK_DIV8) {
  1280. SPI1_BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br1);
  1281. } else if (clockDiv == SPI_CLOCK_DIV16) {
  1282. SPI1_BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br1);
  1283. } else if (clockDiv == SPI_CLOCK_DIV32) {
  1284. SPI1_BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br1);
  1285. } else if (clockDiv == SPI_CLOCK_DIV64) {
  1286. SPI1_BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br1);
  1287. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  1288. SPI1_BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br1);
  1289. }
  1290. }
  1291. // These undocumented functions should not be used. SPI.transfer()
  1292. // polls the hardware flag which is automatically cleared as the
  1293. // AVR responds to SPI's interrupt
  1294. inline static void attachInterrupt() { }
  1295. inline static void detachInterrupt() { }
  1296. // Teensy LC can use alternate pins for these 3 SPI signals.
  1297. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1298. SPCR1.setMOSI(pin);
  1299. }
  1300. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1301. SPCR1.setMISO(pin);
  1302. }
  1303. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1304. SPCR1.setSCK(pin);
  1305. }
  1306. // return true if "pin" has special chip select capability
  1307. static bool pinIsChipSelect(uint8_t pin) { return (pin == 6); }
  1308. // return true if both pin1 and pin2 have independent chip select capability
  1309. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
  1310. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  1311. static uint8_t setCS(uint8_t pin);
  1312. private:
  1313. static uint32_t interruptMask;
  1314. static uint32_t interruptSave;
  1315. #ifdef SPI_TRANSACTION_MISMATCH_LED
  1316. static uint8_t inTransactionFlag;
  1317. #endif
  1318. };
  1319. /**********************************************************/
  1320. /* 32 bit Arduino Due */
  1321. /**********************************************************/
  1322. #elif defined(__arm__) && defined(__SAM3X8E__)
  1323. #undef SPI_MODE0
  1324. #undef SPI_MODE1
  1325. #undef SPI_MODE2
  1326. #undef SPI_MODE3
  1327. #define SPI_MODE0 0x02
  1328. #define SPI_MODE1 0x00
  1329. #define SPI_MODE2 0x03
  1330. #define SPI_MODE3 0x01
  1331. #undef SPI_CLOCK_DIV2
  1332. #undef SPI_CLOCK_DIV4
  1333. #undef SPI_CLOCK_DIV8
  1334. #undef SPI_CLOCK_DIV16
  1335. #undef SPI_CLOCK_DIV32
  1336. #undef SPI_CLOCK_DIV64
  1337. #undef SPI_CLOCK_DIV128
  1338. #define SPI_CLOCK_DIV2 11
  1339. #define SPI_CLOCK_DIV4 21
  1340. #define SPI_CLOCK_DIV8 42
  1341. #define SPI_CLOCK_DIV16 84
  1342. #define SPI_CLOCK_DIV32 168
  1343. #define SPI_CLOCK_DIV64 255
  1344. #define SPI_CLOCK_DIV128 255
  1345. enum SPITransferMode {
  1346. SPI_CONTINUE,
  1347. SPI_LAST
  1348. };
  1349. class SPISettings {
  1350. public:
  1351. SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
  1352. if (__builtin_constant_p(clock)) {
  1353. init_AlwaysInline(clock, bitOrder, dataMode);
  1354. } else {
  1355. init_MightInline(clock, bitOrder, dataMode);
  1356. }
  1357. }
  1358. SPISettings() {
  1359. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  1360. }
  1361. private:
  1362. void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
  1363. init_AlwaysInline(clock, bitOrder, dataMode);
  1364. }
  1365. void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode)
  1366. __attribute__((__always_inline__)) {
  1367. uint8_t div;
  1368. border = bitOrder;
  1369. if (__builtin_constant_p(clock)) {
  1370. if (clock >= F_CPU / 2) div = 2;
  1371. else if (clock >= F_CPU / 3) div = 3;
  1372. else if (clock >= F_CPU / 4) div = 4;
  1373. else if (clock >= F_CPU / 5) div = 5;
  1374. else if (clock >= F_CPU / 6) div = 6;
  1375. else if (clock >= F_CPU / 7) div = 7;
  1376. else if (clock >= F_CPU / 8) div = 8;
  1377. else if (clock >= F_CPU / 9) div = 9;
  1378. else if (clock >= F_CPU / 10) div = 10;
  1379. else if (clock >= F_CPU / 11) div = 11;
  1380. else if (clock >= F_CPU / 12) div = 12;
  1381. else if (clock >= F_CPU / 13) div = 13;
  1382. else if (clock >= F_CPU / 14) div = 14;
  1383. else if (clock >= F_CPU / 15) div = 15;
  1384. else if (clock >= F_CPU / 16) div = 16;
  1385. else if (clock >= F_CPU / 17) div = 17;
  1386. else if (clock >= F_CPU / 18) div = 18;
  1387. else if (clock >= F_CPU / 19) div = 19;
  1388. else if (clock >= F_CPU / 20) div = 20;
  1389. else if (clock >= F_CPU / 21) div = 21;
  1390. else if (clock >= F_CPU / 22) div = 22;
  1391. else if (clock >= F_CPU / 23) div = 23;
  1392. else if (clock >= F_CPU / 24) div = 24;
  1393. else if (clock >= F_CPU / 25) div = 25;
  1394. else if (clock >= F_CPU / 26) div = 26;
  1395. else if (clock >= F_CPU / 27) div = 27;
  1396. else if (clock >= F_CPU / 28) div = 28;
  1397. else if (clock >= F_CPU / 29) div = 29;
  1398. else if (clock >= F_CPU / 30) div = 30;
  1399. else if (clock >= F_CPU / 31) div = 31;
  1400. else if (clock >= F_CPU / 32) div = 32;
  1401. else if (clock >= F_CPU / 33) div = 33;
  1402. else if (clock >= F_CPU / 34) div = 34;
  1403. else if (clock >= F_CPU / 35) div = 35;
  1404. else if (clock >= F_CPU / 36) div = 36;
  1405. else if (clock >= F_CPU / 37) div = 37;
  1406. else if (clock >= F_CPU / 38) div = 38;
  1407. else if (clock >= F_CPU / 39) div = 39;
  1408. else if (clock >= F_CPU / 40) div = 40;
  1409. else if (clock >= F_CPU / 41) div = 41;
  1410. else if (clock >= F_CPU / 42) div = 42;
  1411. else if (clock >= F_CPU / 43) div = 43;
  1412. else if (clock >= F_CPU / 44) div = 44;
  1413. else if (clock >= F_CPU / 45) div = 45;
  1414. else if (clock >= F_CPU / 46) div = 46;
  1415. else if (clock >= F_CPU / 47) div = 47;
  1416. else if (clock >= F_CPU / 48) div = 48;
  1417. else if (clock >= F_CPU / 49) div = 49;
  1418. else if (clock >= F_CPU / 50) div = 50;
  1419. else if (clock >= F_CPU / 51) div = 51;
  1420. else if (clock >= F_CPU / 52) div = 52;
  1421. else if (clock >= F_CPU / 53) div = 53;
  1422. else if (clock >= F_CPU / 54) div = 54;
  1423. else if (clock >= F_CPU / 55) div = 55;
  1424. else if (clock >= F_CPU / 56) div = 56;
  1425. else if (clock >= F_CPU / 57) div = 57;
  1426. else if (clock >= F_CPU / 58) div = 58;
  1427. else if (clock >= F_CPU / 59) div = 59;
  1428. else if (clock >= F_CPU / 60) div = 60;
  1429. else if (clock >= F_CPU / 61) div = 61;
  1430. else if (clock >= F_CPU / 62) div = 62;
  1431. else if (clock >= F_CPU / 63) div = 63;
  1432. else if (clock >= F_CPU / 64) div = 64;
  1433. else if (clock >= F_CPU / 65) div = 65;
  1434. else if (clock >= F_CPU / 66) div = 66;
  1435. else if (clock >= F_CPU / 67) div = 67;
  1436. else if (clock >= F_CPU / 68) div = 68;
  1437. else if (clock >= F_CPU / 69) div = 69;
  1438. else if (clock >= F_CPU / 70) div = 70;
  1439. else if (clock >= F_CPU / 71) div = 71;
  1440. else if (clock >= F_CPU / 72) div = 72;
  1441. else if (clock >= F_CPU / 73) div = 73;
  1442. else if (clock >= F_CPU / 74) div = 74;
  1443. else if (clock >= F_CPU / 75) div = 75;
  1444. else if (clock >= F_CPU / 76) div = 76;
  1445. else if (clock >= F_CPU / 77) div = 77;
  1446. else if (clock >= F_CPU / 78) div = 78;
  1447. else if (clock >= F_CPU / 79) div = 79;
  1448. else if (clock >= F_CPU / 80) div = 80;
  1449. else if (clock >= F_CPU / 81) div = 81;
  1450. else if (clock >= F_CPU / 82) div = 82;
  1451. else if (clock >= F_CPU / 83) div = 83;
  1452. else if (clock >= F_CPU / 84) div = 84;
  1453. else if (clock >= F_CPU / 85) div = 85;
  1454. else if (clock >= F_CPU / 86) div = 86;
  1455. else if (clock >= F_CPU / 87) div = 87;
  1456. else if (clock >= F_CPU / 88) div = 88;
  1457. else if (clock >= F_CPU / 89) div = 89;
  1458. else if (clock >= F_CPU / 90) div = 90;
  1459. else if (clock >= F_CPU / 91) div = 91;
  1460. else if (clock >= F_CPU / 92) div = 92;
  1461. else if (clock >= F_CPU / 93) div = 93;
  1462. else if (clock >= F_CPU / 94) div = 94;
  1463. else if (clock >= F_CPU / 95) div = 95;
  1464. else if (clock >= F_CPU / 96) div = 96;
  1465. else if (clock >= F_CPU / 97) div = 97;
  1466. else if (clock >= F_CPU / 98) div = 98;
  1467. else if (clock >= F_CPU / 99) div = 99;
  1468. else if (clock >= F_CPU / 100) div = 100;
  1469. else if (clock >= F_CPU / 101) div = 101;
  1470. else if (clock >= F_CPU / 102) div = 102;
  1471. else if (clock >= F_CPU / 103) div = 103;
  1472. else if (clock >= F_CPU / 104) div = 104;
  1473. else if (clock >= F_CPU / 105) div = 105;
  1474. else if (clock >= F_CPU / 106) div = 106;
  1475. else if (clock >= F_CPU / 107) div = 107;
  1476. else if (clock >= F_CPU / 108) div = 108;
  1477. else if (clock >= F_CPU / 109) div = 109;
  1478. else if (clock >= F_CPU / 110) div = 110;
  1479. else if (clock >= F_CPU / 111) div = 111;
  1480. else if (clock >= F_CPU / 112) div = 112;
  1481. else if (clock >= F_CPU / 113) div = 113;
  1482. else if (clock >= F_CPU / 114) div = 114;
  1483. else if (clock >= F_CPU / 115) div = 115;
  1484. else if (clock >= F_CPU / 116) div = 116;
  1485. else if (clock >= F_CPU / 117) div = 117;
  1486. else if (clock >= F_CPU / 118) div = 118;
  1487. else if (clock >= F_CPU / 119) div = 119;
  1488. else if (clock >= F_CPU / 120) div = 120;
  1489. else if (clock >= F_CPU / 121) div = 121;
  1490. else if (clock >= F_CPU / 122) div = 122;
  1491. else if (clock >= F_CPU / 123) div = 123;
  1492. else if (clock >= F_CPU / 124) div = 124;
  1493. else if (clock >= F_CPU / 125) div = 125;
  1494. else if (clock >= F_CPU / 126) div = 126;
  1495. else if (clock >= F_CPU / 127) div = 127;
  1496. else if (clock >= F_CPU / 128) div = 128;
  1497. else if (clock >= F_CPU / 129) div = 129;
  1498. else if (clock >= F_CPU / 130) div = 130;
  1499. else if (clock >= F_CPU / 131) div = 131;
  1500. else if (clock >= F_CPU / 132) div = 132;
  1501. else if (clock >= F_CPU / 133) div = 133;
  1502. else if (clock >= F_CPU / 134) div = 134;
  1503. else if (clock >= F_CPU / 135) div = 135;
  1504. else if (clock >= F_CPU / 136) div = 136;
  1505. else if (clock >= F_CPU / 137) div = 137;
  1506. else if (clock >= F_CPU / 138) div = 138;
  1507. else if (clock >= F_CPU / 139) div = 139;
  1508. else if (clock >= F_CPU / 140) div = 140;
  1509. else if (clock >= F_CPU / 141) div = 141;
  1510. else if (clock >= F_CPU / 142) div = 142;
  1511. else if (clock >= F_CPU / 143) div = 143;
  1512. else if (clock >= F_CPU / 144) div = 144;
  1513. else if (clock >= F_CPU / 145) div = 145;
  1514. else if (clock >= F_CPU / 146) div = 146;
  1515. else if (clock >= F_CPU / 147) div = 147;
  1516. else if (clock >= F_CPU / 148) div = 148;
  1517. else if (clock >= F_CPU / 149) div = 149;
  1518. else if (clock >= F_CPU / 150) div = 150;
  1519. else if (clock >= F_CPU / 151) div = 151;
  1520. else if (clock >= F_CPU / 152) div = 152;
  1521. else if (clock >= F_CPU / 153) div = 153;
  1522. else if (clock >= F_CPU / 154) div = 154;
  1523. else if (clock >= F_CPU / 155) div = 155;
  1524. else if (clock >= F_CPU / 156) div = 156;
  1525. else if (clock >= F_CPU / 157) div = 157;
  1526. else if (clock >= F_CPU / 158) div = 158;
  1527. else if (clock >= F_CPU / 159) div = 159;
  1528. else if (clock >= F_CPU / 160) div = 160;
  1529. else if (clock >= F_CPU / 161) div = 161;
  1530. else if (clock >= F_CPU / 162) div = 162;
  1531. else if (clock >= F_CPU / 163) div = 163;
  1532. else if (clock >= F_CPU / 164) div = 164;
  1533. else if (clock >= F_CPU / 165) div = 165;
  1534. else if (clock >= F_CPU / 166) div = 166;
  1535. else if (clock >= F_CPU / 167) div = 167;
  1536. else if (clock >= F_CPU / 168) div = 168;
  1537. else if (clock >= F_CPU / 169) div = 169;
  1538. else if (clock >= F_CPU / 170) div = 170;
  1539. else if (clock >= F_CPU / 171) div = 171;
  1540. else if (clock >= F_CPU / 172) div = 172;
  1541. else if (clock >= F_CPU / 173) div = 173;
  1542. else if (clock >= F_CPU / 174) div = 174;
  1543. else if (clock >= F_CPU / 175) div = 175;
  1544. else if (clock >= F_CPU / 176) div = 176;
  1545. else if (clock >= F_CPU / 177) div = 177;
  1546. else if (clock >= F_CPU / 178) div = 178;
  1547. else if (clock >= F_CPU / 179) div = 179;
  1548. else if (clock >= F_CPU / 180) div = 180;
  1549. else if (clock >= F_CPU / 181) div = 181;
  1550. else if (clock >= F_CPU / 182) div = 182;
  1551. else if (clock >= F_CPU / 183) div = 183;
  1552. else if (clock >= F_CPU / 184) div = 184;
  1553. else if (clock >= F_CPU / 185) div = 185;
  1554. else if (clock >= F_CPU / 186) div = 186;
  1555. else if (clock >= F_CPU / 187) div = 187;
  1556. else if (clock >= F_CPU / 188) div = 188;
  1557. else if (clock >= F_CPU / 189) div = 189;
  1558. else if (clock >= F_CPU / 190) div = 190;
  1559. else if (clock >= F_CPU / 191) div = 191;
  1560. else if (clock >= F_CPU / 192) div = 192;
  1561. else if (clock >= F_CPU / 193) div = 193;
  1562. else if (clock >= F_CPU / 194) div = 194;
  1563. else if (clock >= F_CPU / 195) div = 195;
  1564. else if (clock >= F_CPU / 196) div = 196;
  1565. else if (clock >= F_CPU / 197) div = 197;
  1566. else if (clock >= F_CPU / 198) div = 198;
  1567. else if (clock >= F_CPU / 199) div = 199;
  1568. else if (clock >= F_CPU / 200) div = 200;
  1569. else if (clock >= F_CPU / 201) div = 201;
  1570. else if (clock >= F_CPU / 202) div = 202;
  1571. else if (clock >= F_CPU / 203) div = 203;
  1572. else if (clock >= F_CPU / 204) div = 204;
  1573. else if (clock >= F_CPU / 205) div = 205;
  1574. else if (clock >= F_CPU / 206) div = 206;
  1575. else if (clock >= F_CPU / 207) div = 207;
  1576. else if (clock >= F_CPU / 208) div = 208;
  1577. else if (clock >= F_CPU / 209) div = 209;
  1578. else if (clock >= F_CPU / 210) div = 210;
  1579. else if (clock >= F_CPU / 211) div = 211;
  1580. else if (clock >= F_CPU / 212) div = 212;
  1581. else if (clock >= F_CPU / 213) div = 213;
  1582. else if (clock >= F_CPU / 214) div = 214;
  1583. else if (clock >= F_CPU / 215) div = 215;
  1584. else if (clock >= F_CPU / 216) div = 216;
  1585. else if (clock >= F_CPU / 217) div = 217;
  1586. else if (clock >= F_CPU / 218) div = 218;
  1587. else if (clock >= F_CPU / 219) div = 219;
  1588. else if (clock >= F_CPU / 220) div = 220;
  1589. else if (clock >= F_CPU / 221) div = 221;
  1590. else if (clock >= F_CPU / 222) div = 222;
  1591. else if (clock >= F_CPU / 223) div = 223;
  1592. else if (clock >= F_CPU / 224) div = 224;
  1593. else if (clock >= F_CPU / 225) div = 225;
  1594. else if (clock >= F_CPU / 226) div = 226;
  1595. else if (clock >= F_CPU / 227) div = 227;
  1596. else if (clock >= F_CPU / 228) div = 228;
  1597. else if (clock >= F_CPU / 229) div = 229;
  1598. else if (clock >= F_CPU / 230) div = 230;
  1599. else if (clock >= F_CPU / 231) div = 231;
  1600. else if (clock >= F_CPU / 232) div = 232;
  1601. else if (clock >= F_CPU / 233) div = 233;
  1602. else if (clock >= F_CPU / 234) div = 234;
  1603. else if (clock >= F_CPU / 235) div = 235;
  1604. else if (clock >= F_CPU / 236) div = 236;
  1605. else if (clock >= F_CPU / 237) div = 237;
  1606. else if (clock >= F_CPU / 238) div = 238;
  1607. else if (clock >= F_CPU / 239) div = 239;
  1608. else if (clock >= F_CPU / 240) div = 240;
  1609. else if (clock >= F_CPU / 241) div = 241;
  1610. else if (clock >= F_CPU / 242) div = 242;
  1611. else if (clock >= F_CPU / 243) div = 243;
  1612. else if (clock >= F_CPU / 244) div = 244;
  1613. else if (clock >= F_CPU / 245) div = 245;
  1614. else if (clock >= F_CPU / 246) div = 246;
  1615. else if (clock >= F_CPU / 247) div = 247;
  1616. else if (clock >= F_CPU / 248) div = 248;
  1617. else if (clock >= F_CPU / 249) div = 249;
  1618. else if (clock >= F_CPU / 250) div = 250;
  1619. else if (clock >= F_CPU / 251) div = 251;
  1620. else if (clock >= F_CPU / 252) div = 252;
  1621. else if (clock >= F_CPU / 253) div = 253;
  1622. else if (clock >= F_CPU / 254) div = 254;
  1623. else /* clock >= F_CPU / 255 */ div = 255;
  1624. /*
  1625. #! /usr/bin/perl
  1626. for ($i=2; $i<256; $i++) {
  1627. printf "\t\t\telse if (clock >= F_CPU / %3d) div = %3d;\n", $i, $i;
  1628. }
  1629. */
  1630. } else {
  1631. for (div=2; div<255; div++) {
  1632. if (clock >= F_CPU / div) break;
  1633. }
  1634. }
  1635. config = (dataMode & 3) | SPI_CSR_CSAAT | SPI_CSR_SCBR(div) | SPI_CSR_DLYBCT(1);
  1636. }
  1637. uint32_t config;
  1638. BitOrder border;
  1639. friend class SPIClass;
  1640. };
  1641. class SPIClass {
  1642. public:
  1643. SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void));
  1644. byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(BOARD_SPI_DEFAULT_SS, _data, _mode); }
  1645. byte transfer(byte _channel, uint8_t _data, SPITransferMode _mode = SPI_LAST);
  1646. // Transaction Functions
  1647. void usingInterrupt(uint8_t interruptNumber);
  1648. void beginTransaction(uint8_t pin, SPISettings settings);
  1649. void beginTransaction(SPISettings settings) {
  1650. beginTransaction(BOARD_SPI_DEFAULT_SS, settings);
  1651. }
  1652. void endTransaction(void);
  1653. // SPI Configuration methods
  1654. void attachInterrupt(void);
  1655. void detachInterrupt(void);
  1656. void begin(void);
  1657. void end(void);
  1658. // Attach/Detach pin to/from SPI controller
  1659. void begin(uint8_t _pin);
  1660. void end(uint8_t _pin);
  1661. // These methods sets a parameter on a single pin
  1662. void setBitOrder(uint8_t _pin, BitOrder);
  1663. void setDataMode(uint8_t _pin, uint8_t);
  1664. void setClockDivider(uint8_t _pin, uint8_t);
  1665. // These methods sets the same parameters but on default pin BOARD_SPI_DEFAULT_SS
  1666. void setBitOrder(BitOrder _order) { setBitOrder(BOARD_SPI_DEFAULT_SS, _order); };
  1667. void setDataMode(uint8_t _mode) { setDataMode(BOARD_SPI_DEFAULT_SS, _mode); };
  1668. void setClockDivider(uint8_t _div) { setClockDivider(BOARD_SPI_DEFAULT_SS, _div); };
  1669. private:
  1670. void init();
  1671. Spi *spi;
  1672. uint32_t id;
  1673. BitOrder bitOrder[SPI_CHANNELS_NUM];
  1674. uint32_t divider[SPI_CHANNELS_NUM];
  1675. uint32_t mode[SPI_CHANNELS_NUM];
  1676. void (*initCb)(void);
  1677. bool initialized;
  1678. uint8_t interruptMode; // 0=none, 1=mask, 2=global
  1679. uint8_t interruptMask; // bits 0:3=pin change
  1680. uint8_t interruptSave; // temp storage, to restore state
  1681. };
  1682. #endif
  1683. extern SPIClass SPI;
  1684. #if defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  1685. extern SPI1Class SPI1;
  1686. #endif
  1687. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1688. extern SPI1Class SPI1;
  1689. extern SPI2Class SPI2;
  1690. #endif
  1691. #endif