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.

942 lines
32KB

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