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.

907 lines
31KB

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