您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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