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.

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
9 vuotta sitten
10 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
9 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
  3. * Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
  4. * Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
  5. * SPI Master library for arduino.
  6. *
  7. * This file is free software; you can redistribute it and/or modify
  8. * it under the terms of either the GNU General Public License version 2
  9. * or the GNU Lesser General Public License version 2.1, both as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef _SPI_H_INCLUDED
  13. #define _SPI_H_INCLUDED
  14. #include <Arduino.h>
  15. // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
  16. // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
  17. #define SPI_HAS_TRANSACTION 1
  18. // Uncomment this line to add detection of mismatched begin/end transactions.
  19. // A mismatch occurs if other libraries fail to use SPI.endTransaction() for
  20. // each SPI.beginTransaction(). Connect a LED to this pin. The LED will turn
  21. // on if any mismatch is ever detected.
  22. //#define SPI_TRANSACTION_MISMATCH_LED 5
  23. #ifndef __SAM3X8E__
  24. #ifndef LSBFIRST
  25. #define LSBFIRST 0
  26. #endif
  27. #ifndef MSBFIRST
  28. #define MSBFIRST 1
  29. #endif
  30. #endif
  31. #define SPI_MODE0 0x00
  32. #define SPI_MODE1 0x04
  33. #define SPI_MODE2 0x08
  34. #define SPI_MODE3 0x0C
  35. #define SPI_CLOCK_DIV4 0x00
  36. #define SPI_CLOCK_DIV16 0x01
  37. #define SPI_CLOCK_DIV64 0x02
  38. #define SPI_CLOCK_DIV128 0x03
  39. #define SPI_CLOCK_DIV2 0x04
  40. #define SPI_CLOCK_DIV8 0x05
  41. #define SPI_CLOCK_DIV32 0x06
  42. #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
  43. #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
  44. #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
  45. /**********************************************************/
  46. /* 8 bit AVR-based boards */
  47. /**********************************************************/
  48. #if defined(__AVR__)
  49. // define SPI_AVR_EIMSK for AVR boards with external interrupt pins
  50. #if defined(EIMSK)
  51. #define SPI_AVR_EIMSK EIMSK
  52. #elif defined(GICR)
  53. #define SPI_AVR_EIMSK GICR
  54. #elif defined(GIMSK)
  55. #define SPI_AVR_EIMSK GIMSK
  56. #endif
  57. class SPISettings {
  58. public:
  59. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  60. if (__builtin_constant_p(clock)) {
  61. init_AlwaysInline(clock, bitOrder, dataMode);
  62. } else {
  63. init_MightInline(clock, bitOrder, dataMode);
  64. }
  65. }
  66. SPISettings() {
  67. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  68. }
  69. private:
  70. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  71. init_AlwaysInline(clock, bitOrder, dataMode);
  72. }
  73. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  74. __attribute__((__always_inline__)) {
  75. // Clock settings are defined as follows. Note that this shows SPI2X
  76. // inverted, so the bits form increasing numbers. Also note that
  77. // fosc/64 appears twice
  78. // SPR1 SPR0 ~SPI2X Freq
  79. // 0 0 0 fosc/2
  80. // 0 0 1 fosc/4
  81. // 0 1 0 fosc/8
  82. // 0 1 1 fosc/16
  83. // 1 0 0 fosc/32
  84. // 1 0 1 fosc/64
  85. // 1 1 0 fosc/64
  86. // 1 1 1 fosc/128
  87. // We find the fastest clock that is less than or equal to the
  88. // given clock rate. The clock divider that results in clock_setting
  89. // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
  90. // slowest (128 == 2 ^^ 7, so clock_div = 6).
  91. uint8_t clockDiv;
  92. // When the clock is known at compiletime, use this if-then-else
  93. // cascade, which the compiler knows how to completely optimize
  94. // away. When clock is not known, use a loop instead, which generates
  95. // shorter code.
  96. if (__builtin_constant_p(clock)) {
  97. if (clock >= F_CPU / 2) {
  98. clockDiv = 0;
  99. } else if (clock >= F_CPU / 4) {
  100. clockDiv = 1;
  101. } else if (clock >= F_CPU / 8) {
  102. clockDiv = 2;
  103. } else if (clock >= F_CPU / 16) {
  104. clockDiv = 3;
  105. } else if (clock >= F_CPU / 32) {
  106. clockDiv = 4;
  107. } else if (clock >= F_CPU / 64) {
  108. clockDiv = 5;
  109. } else {
  110. clockDiv = 6;
  111. }
  112. } else {
  113. uint32_t clockSetting = F_CPU / 2;
  114. clockDiv = 0;
  115. while (clockDiv < 6 && clock < clockSetting) {
  116. clockSetting /= 2;
  117. clockDiv++;
  118. }
  119. }
  120. // Compensate for the duplicate fosc/64
  121. if (clockDiv == 6)
  122. clockDiv = 7;
  123. // Invert the SPI2X bit
  124. clockDiv ^= 0x1;
  125. // Pack into the SPISettings class
  126. spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
  127. (dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK);
  128. spsr = clockDiv & SPI_2XCLOCK_MASK;
  129. }
  130. uint8_t spcr;
  131. uint8_t spsr;
  132. friend class SPIClass;
  133. };
  134. class SPIClass {
  135. public:
  136. // Initialize the SPI library
  137. static void begin();
  138. // If SPI is used from within an interrupt, this function registers
  139. // that interrupt with the SPI library, so beginTransaction() can
  140. // prevent conflicts. The input interruptNumber is the number used
  141. // with attachInterrupt. If SPI is used from a different interrupt
  142. // (eg, a timer), interruptNumber should be 255.
  143. static void usingInterrupt(uint8_t interruptNumber);
  144. // Before using SPI.transfer() or asserting chip select pins,
  145. // this function is used to gain exclusive access to the SPI bus
  146. // and configure the correct settings.
  147. inline static void beginTransaction(SPISettings settings) {
  148. if (interruptMode > 0) {
  149. #ifdef SPI_AVR_EIMSK
  150. if (interruptMode == 1) {
  151. interruptSave = SPI_AVR_EIMSK;
  152. SPI_AVR_EIMSK &= ~interruptMask;
  153. } else
  154. #endif
  155. {
  156. uint8_t tmp = SREG;
  157. cli();
  158. interruptSave = tmp;
  159. }
  160. }
  161. #ifdef SPI_TRANSACTION_MISMATCH_LED
  162. if (inTransactionFlag) {
  163. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  164. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  165. }
  166. inTransactionFlag = 1;
  167. #endif
  168. SPCR = settings.spcr;
  169. SPSR = settings.spsr;
  170. }
  171. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  172. inline static uint8_t transfer(uint8_t data) {
  173. SPDR = data;
  174. asm volatile("nop");
  175. while (!(SPSR & _BV(SPIF))) ; // wait
  176. return SPDR;
  177. }
  178. inline static uint16_t transfer16(uint16_t data) {
  179. union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
  180. in.val = data;
  181. if ((SPCR & _BV(DORD))) {
  182. SPDR = in.lsb;
  183. asm volatile("nop");
  184. while (!(SPSR & _BV(SPIF))) ;
  185. out.lsb = SPDR;
  186. SPDR = in.msb;
  187. asm volatile("nop");
  188. while (!(SPSR & _BV(SPIF))) ;
  189. out.msb = SPDR;
  190. } else {
  191. SPDR = in.msb;
  192. asm volatile("nop");
  193. while (!(SPSR & _BV(SPIF))) ;
  194. out.msb = SPDR;
  195. SPDR = in.lsb;
  196. asm volatile("nop");
  197. while (!(SPSR & _BV(SPIF))) ;
  198. out.lsb = SPDR;
  199. }
  200. return out.val;
  201. }
  202. inline static void transfer(void *buf, size_t count) {
  203. if (count == 0) return;
  204. uint8_t *p = (uint8_t *)buf;
  205. SPDR = *p;
  206. while (--count > 0) {
  207. uint8_t out = *(p + 1);
  208. while (!(SPSR & _BV(SPIF))) ;
  209. uint8_t in = SPDR;
  210. SPDR = out;
  211. *p++ = in;
  212. }
  213. while (!(SPSR & _BV(SPIF))) ;
  214. *p = SPDR;
  215. }
  216. // After performing a group of transfers and releasing the chip select
  217. // signal, this function allows others to access the SPI bus
  218. inline static void endTransaction(void) {
  219. #ifdef SPI_TRANSACTION_MISMATCH_LED
  220. if (!inTransactionFlag) {
  221. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  222. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  223. }
  224. inTransactionFlag = 0;
  225. #endif
  226. if (interruptMode > 0) {
  227. #ifdef SPI_AVR_EIMSK
  228. if (interruptMode == 1) {
  229. SPI_AVR_EIMSK = interruptSave;
  230. } else
  231. #endif
  232. {
  233. SREG = interruptSave;
  234. }
  235. }
  236. }
  237. // Disable the SPI bus
  238. static void end();
  239. // This function is deprecated. New applications should use
  240. // beginTransaction() to configure SPI settings.
  241. inline static void setBitOrder(uint8_t bitOrder) {
  242. if (bitOrder == LSBFIRST) SPCR |= _BV(DORD);
  243. else SPCR &= ~(_BV(DORD));
  244. }
  245. // This function is deprecated. New applications should use
  246. // beginTransaction() to configure SPI settings.
  247. inline static void setDataMode(uint8_t dataMode) {
  248. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  249. }
  250. // This function is deprecated. New applications should use
  251. // beginTransaction() to configure SPI settings.
  252. inline static void setClockDivider(uint8_t clockDiv) {
  253. SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK);
  254. SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK);
  255. }
  256. // These undocumented functions should not be used. SPI.transfer()
  257. // polls the hardware flag which is automatically cleared as the
  258. // AVR responds to SPI's interrupt
  259. inline static void attachInterrupt() { SPCR |= _BV(SPIE); }
  260. inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); }
  261. private:
  262. static uint8_t interruptMode; // 0=none, 1=mask, 2=global
  263. static uint8_t interruptMask; // which interrupts to mask
  264. static uint8_t interruptSave; // temp storage, to restore state
  265. #ifdef SPI_TRANSACTION_MISMATCH_LED
  266. static uint8_t inTransactionFlag;
  267. #endif
  268. };
  269. /**********************************************************/
  270. /* 32 bit Teensy 3.0 and 3.1 */
  271. /**********************************************************/
  272. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
  273. #define SPI_HAS_NOTUSINGINTERRUPT 1
  274. class SPISettings {
  275. public:
  276. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  277. if (__builtin_constant_p(clock)) {
  278. init_AlwaysInline(clock, bitOrder, dataMode);
  279. } else {
  280. init_MightInline(clock, bitOrder, dataMode);
  281. }
  282. }
  283. SPISettings() {
  284. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  285. }
  286. private:
  287. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  288. init_AlwaysInline(clock, bitOrder, dataMode);
  289. }
  290. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  291. __attribute__((__always_inline__)) {
  292. uint32_t t, c = SPI_CTAR_FMSZ(7);
  293. if (bitOrder == LSBFIRST) c |= SPI_CTAR_LSBFE;
  294. if (__builtin_constant_p(clock)) {
  295. if (clock >= F_BUS / 2) {
  296. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  297. | SPI_CTAR_CSSCK(0);
  298. } else if (clock >= F_BUS / 3) {
  299. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  300. | SPI_CTAR_CSSCK(0);
  301. } else if (clock >= F_BUS / 4) {
  302. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  303. } else if (clock >= F_BUS / 5) {
  304. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR
  305. | SPI_CTAR_CSSCK(0);
  306. } else if (clock >= F_BUS / 6) {
  307. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  308. } else if (clock >= F_BUS / 8) {
  309. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  310. } else if (clock >= F_BUS / 10) {
  311. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0);
  312. } else if (clock >= F_BUS / 12) {
  313. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  314. } else if (clock >= F_BUS / 16) {
  315. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  316. } else if (clock >= F_BUS / 20) {
  317. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(0);
  318. } else if (clock >= F_BUS / 24) {
  319. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  320. } else if (clock >= F_BUS / 32) {
  321. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(3);
  322. } else if (clock >= F_BUS / 40) {
  323. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  324. } else if (clock >= F_BUS / 56) {
  325. t = SPI_CTAR_PBR(3) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2);
  326. } else if (clock >= F_BUS / 64) {
  327. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4);
  328. } else if (clock >= F_BUS / 96) {
  329. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4);
  330. } else if (clock >= F_BUS / 128) {
  331. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5);
  332. } else if (clock >= F_BUS / 192) {
  333. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5);
  334. } else if (clock >= F_BUS / 256) {
  335. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  336. } else if (clock >= F_BUS / 384) {
  337. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  338. } else if (clock >= F_BUS / 512) {
  339. t = SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7);
  340. } else if (clock >= F_BUS / 640) {
  341. t = SPI_CTAR_PBR(2) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6);
  342. } else { /* F_BUS / 768 */
  343. t = SPI_CTAR_PBR(1) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7);
  344. }
  345. } else {
  346. for (uint32_t i=0; i<23; i++) {
  347. t = ctar_clock_table[i];
  348. if (clock >= F_BUS / ctar_div_table[i]) break;
  349. }
  350. }
  351. if (dataMode & 0x08) {
  352. c |= SPI_CTAR_CPOL;
  353. }
  354. if (dataMode & 0x04) {
  355. c |= SPI_CTAR_CPHA;
  356. t = (t & 0xFFFF0FFF) | ((t & 0xF000) >> 4);
  357. }
  358. ctar = c | t;
  359. }
  360. static const uint16_t ctar_div_table[23];
  361. static const uint32_t ctar_clock_table[23];
  362. uint32_t ctar;
  363. friend class SPIClass;
  364. };
  365. class SPIClass {
  366. public:
  367. // Initialize the SPI library
  368. static void begin();
  369. // If SPI is to used from within an interrupt, this function registers
  370. // that interrupt with the SPI library, so beginTransaction() can
  371. // prevent conflicts. The input interruptNumber is the number used
  372. // with attachInterrupt. If SPI is used from a different interrupt
  373. // (eg, a timer), interruptNumber should be 255.
  374. static void usingInterrupt(uint8_t n) {
  375. if (n == 3 || n == 4 || n == 24 || n == 33) {
  376. usingInterrupt(IRQ_PORTA);
  377. } else if (n == 0 || n == 1 || (n >= 16 && n <= 19) || n == 25 || n == 32) {
  378. usingInterrupt(IRQ_PORTB);
  379. } else if ((n >= 9 && n <= 13) || n == 15 || n == 22 || n == 23
  380. || (n >= 27 && n <= 30)) {
  381. usingInterrupt(IRQ_PORTC);
  382. } else if (n == 2 || (n >= 5 && n <= 8) || n == 14 || n == 20 || n == 21) {
  383. usingInterrupt(IRQ_PORTD);
  384. } else if (n == 26 || n == 31) {
  385. usingInterrupt(IRQ_PORTE);
  386. }
  387. }
  388. static void usingInterrupt(IRQ_NUMBER_t interruptName);
  389. static void notUsingInterrupt(IRQ_NUMBER_t interruptName);
  390. // Before using SPI.transfer() or asserting chip select pins,
  391. // this function is used to gain exclusive access to the SPI bus
  392. // and configure the correct settings.
  393. inline static void beginTransaction(SPISettings settings) {
  394. if (interruptMasksUsed) {
  395. __disable_irq();
  396. if (interruptMasksUsed & 0x01) {
  397. interruptSave[0] = NVIC_ICER0 & interruptMask[0];
  398. NVIC_ICER0 = interruptSave[0];
  399. }
  400. #if NVIC_NUM_INTERRUPTS > 32
  401. if (interruptMasksUsed & 0x02) {
  402. interruptSave[1] = NVIC_ICER1 & interruptMask[1];
  403. NVIC_ICER1 = interruptSave[1];
  404. }
  405. #endif
  406. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  407. if (interruptMasksUsed & 0x04) {
  408. interruptSave[2] = NVIC_ICER2 & interruptMask[2];
  409. NVIC_ICER2 = interruptSave[2];
  410. }
  411. #endif
  412. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  413. if (interruptMasksUsed & 0x08) {
  414. interruptSave[3] = NVIC_ICER3 & interruptMask[3];
  415. NVIC_ICER3 = interruptSave[3];
  416. }
  417. #endif
  418. __enable_irq();
  419. }
  420. #ifdef SPI_TRANSACTION_MISMATCH_LED
  421. if (inTransactionFlag) {
  422. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  423. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  424. }
  425. inTransactionFlag = 1;
  426. #endif
  427. if (SPI0_CTAR0 != settings.ctar) {
  428. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  429. SPI0_CTAR0 = settings.ctar;
  430. SPI0_CTAR1 = settings.ctar| SPI_CTAR_FMSZ(8);
  431. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  432. }
  433. }
  434. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  435. inline static uint8_t transfer(uint8_t data) {
  436. SPI0_SR = SPI_SR_TCF;
  437. SPI0_PUSHR = data;
  438. while (!(SPI0_SR & SPI_SR_TCF)) ; // wait
  439. return SPI0_POPR;
  440. }
  441. inline static uint16_t transfer16(uint16_t data) {
  442. SPI0_SR = SPI_SR_TCF;
  443. SPI0_PUSHR = data | SPI_PUSHR_CTAS(1);
  444. while (!(SPI0_SR & SPI_SR_TCF)) ; // wait
  445. return SPI0_POPR;
  446. }
  447. inline static void transfer(void *buf, size_t count) {
  448. if (count == 0) return;
  449. uint8_t *p = (uint8_t *)buf;
  450. SPDR = *p;
  451. while (--count > 0) {
  452. uint8_t out = *(p + 1);
  453. while (!(SPSR & _BV(SPIF))) ;
  454. uint8_t in = SPDR;
  455. SPDR = out;
  456. *p++ = in;
  457. }
  458. while (!(SPSR & _BV(SPIF))) ;
  459. *p = SPDR;
  460. }
  461. // After performing a group of transfers and releasing the chip select
  462. // signal, this function allows others to access the SPI bus
  463. inline static void endTransaction(void) {
  464. #ifdef SPI_TRANSACTION_MISMATCH_LED
  465. if (!inTransactionFlag) {
  466. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  467. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  468. }
  469. inTransactionFlag = 0;
  470. #endif
  471. if (interruptMasksUsed) {
  472. if (interruptMasksUsed & 0x01) {
  473. NVIC_ISER0 = interruptSave[0];
  474. }
  475. #if NVIC_NUM_INTERRUPTS > 32
  476. if (interruptMasksUsed & 0x02) {
  477. NVIC_ISER1 = interruptSave[1];
  478. }
  479. #endif
  480. #if NVIC_NUM_INTERRUPTS > 64 && defined(NVIC_ISER2)
  481. if (interruptMasksUsed & 0x04) {
  482. NVIC_ISER2 = interruptSave[2];
  483. }
  484. #endif
  485. #if NVIC_NUM_INTERRUPTS > 96 && defined(NVIC_ISER3)
  486. if (interruptMasksUsed & 0x08) {
  487. NVIC_ISER3 = interruptSave[3];
  488. }
  489. #endif
  490. }
  491. }
  492. // Disable the SPI bus
  493. static void end();
  494. // This function is deprecated. New applications should use
  495. // beginTransaction() to configure SPI settings.
  496. static void setBitOrder(uint8_t bitOrder);
  497. // This function is deprecated. New applications should use
  498. // beginTransaction() to configure SPI settings.
  499. static void setDataMode(uint8_t dataMode);
  500. // This function is deprecated. New applications should use
  501. // beginTransaction() to configure SPI settings.
  502. inline static void setClockDivider(uint8_t clockDiv) {
  503. if (clockDiv == SPI_CLOCK_DIV2) {
  504. setClockDivider_noInline(SPISettings(12000000, MSBFIRST, SPI_MODE0).ctar);
  505. } else if (clockDiv == SPI_CLOCK_DIV4) {
  506. setClockDivider_noInline(SPISettings(4000000, MSBFIRST, SPI_MODE0).ctar);
  507. } else if (clockDiv == SPI_CLOCK_DIV8) {
  508. setClockDivider_noInline(SPISettings(2000000, MSBFIRST, SPI_MODE0).ctar);
  509. } else if (clockDiv == SPI_CLOCK_DIV16) {
  510. setClockDivider_noInline(SPISettings(1000000, MSBFIRST, SPI_MODE0).ctar);
  511. } else if (clockDiv == SPI_CLOCK_DIV32) {
  512. setClockDivider_noInline(SPISettings(500000, MSBFIRST, SPI_MODE0).ctar);
  513. } else if (clockDiv == SPI_CLOCK_DIV64) {
  514. setClockDivider_noInline(SPISettings(250000, MSBFIRST, SPI_MODE0).ctar);
  515. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  516. setClockDivider_noInline(SPISettings(125000, MSBFIRST, SPI_MODE0).ctar);
  517. }
  518. }
  519. static void setClockDivider_noInline(uint32_t clk);
  520. // These undocumented functions should not be used. SPI.transfer()
  521. // polls the hardware flag which is automatically cleared as the
  522. // AVR responds to SPI's interrupt
  523. inline static void attachInterrupt() { }
  524. inline static void detachInterrupt() { }
  525. // Teensy 3.x can use alternate pins for these 3 SPI signals.
  526. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  527. SPCR.setMOSI(pin);
  528. }
  529. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  530. SPCR.setMISO(pin);
  531. }
  532. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  533. SPCR.setSCK(pin);
  534. }
  535. // return true if "pin" has special chip select capability
  536. static bool pinIsChipSelect(uint8_t pin);
  537. // return true if both pin1 and pin2 have independent chip select capability
  538. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2);
  539. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  540. static uint8_t setCS(uint8_t pin);
  541. private:
  542. static uint8_t interruptMasksUsed;
  543. static uint32_t interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  544. static uint32_t interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  545. #ifdef SPI_TRANSACTION_MISMATCH_LED
  546. static uint8_t inTransactionFlag;
  547. #endif
  548. };
  549. /**********************************************************/
  550. /* 32 bit Teensy-LC */
  551. /**********************************************************/
  552. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  553. class SPISettings {
  554. public:
  555. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  556. if (__builtin_constant_p(clock)) {
  557. init_AlwaysInline(clock, bitOrder, dataMode);
  558. } else {
  559. init_MightInline(clock, bitOrder, dataMode);
  560. }
  561. }
  562. SPISettings() {
  563. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  564. }
  565. private:
  566. void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
  567. init_AlwaysInline(clock, bitOrder, dataMode);
  568. }
  569. void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  570. __attribute__((__always_inline__)) {
  571. uint8_t c = SPI_C1_MSTR | SPI_C1_SPE;
  572. if (dataMode & 0x04) c |= SPI_C1_CPHA;
  573. if (dataMode & 0x08) c |= SPI_C1_CPOL;
  574. if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
  575. c1 = c;
  576. if (__builtin_constant_p(clock)) {
  577. if (clock >= F_BUS / 2) { c = SPI_BR_SPPR(0) | SPI_BR_SPR(0);
  578. } else if (clock >= F_BUS / 4) { c = SPI_BR_SPPR(1) | SPI_BR_SPR(0);
  579. } else if (clock >= F_BUS / 6) { c = SPI_BR_SPPR(2) | SPI_BR_SPR(0);
  580. } else if (clock >= F_BUS / 8) { c = SPI_BR_SPPR(3) | SPI_BR_SPR(0);
  581. } else if (clock >= F_BUS / 10) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(0);
  582. } else if (clock >= F_BUS / 12) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(0);
  583. } else if (clock >= F_BUS / 14) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(0);
  584. } else if (clock >= F_BUS / 16) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(0);
  585. } else if (clock >= F_BUS / 20) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(1);
  586. } else if (clock >= F_BUS / 24) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(1);
  587. } else if (clock >= F_BUS / 28) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(1);
  588. } else if (clock >= F_BUS / 32) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(1);
  589. } else if (clock >= F_BUS / 40) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(2);
  590. } else if (clock >= F_BUS / 48) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(2);
  591. } else if (clock >= F_BUS / 56) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(2);
  592. } else if (clock >= F_BUS / 64) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(2);
  593. } else if (clock >= F_BUS / 80) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(3);
  594. } else if (clock >= F_BUS / 96) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(3);
  595. } else if (clock >= F_BUS / 112) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(3);
  596. } else if (clock >= F_BUS / 128) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(3);
  597. } else if (clock >= F_BUS / 160) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(4);
  598. } else if (clock >= F_BUS / 192) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(4);
  599. } else if (clock >= F_BUS / 224) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(4);
  600. } else if (clock >= F_BUS / 256) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(4);
  601. } else if (clock >= F_BUS / 320) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(5);
  602. } else if (clock >= F_BUS / 384) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(5);
  603. } else if (clock >= F_BUS / 448) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(5);
  604. } else if (clock >= F_BUS / 512) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(5);
  605. } else if (clock >= F_BUS / 640) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(6);
  606. } else /* F_BUS / 768 */ { c = SPI_BR_SPPR(5) | SPI_BR_SPR(6);
  607. }
  608. } else {
  609. for (uint32_t i=0; i<30; i++) {
  610. c = br_clock_table[i];
  611. if (clock >= F_BUS / br_div_table[i]) break;
  612. }
  613. }
  614. br0 = c;
  615. if (__builtin_constant_p(clock)) {
  616. if (clock >= (F_PLL/2) / 2) { c = SPI_BR_SPPR(0) | SPI_BR_SPR(0);
  617. } else if (clock >= (F_PLL/2) / 4) { c = SPI_BR_SPPR(1) | SPI_BR_SPR(0);
  618. } else if (clock >= (F_PLL/2) / 6) { c = SPI_BR_SPPR(2) | SPI_BR_SPR(0);
  619. } else if (clock >= (F_PLL/2) / 8) { c = SPI_BR_SPPR(3) | SPI_BR_SPR(0);
  620. } else if (clock >= (F_PLL/2) / 10) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(0);
  621. } else if (clock >= (F_PLL/2) / 12) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(0);
  622. } else if (clock >= (F_PLL/2) / 14) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(0);
  623. } else if (clock >= (F_PLL/2) / 16) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(0);
  624. } else if (clock >= (F_PLL/2) / 20) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(1);
  625. } else if (clock >= (F_PLL/2) / 24) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(1);
  626. } else if (clock >= (F_PLL/2) / 28) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(1);
  627. } else if (clock >= (F_PLL/2) / 32) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(1);
  628. } else if (clock >= (F_PLL/2) / 40) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(2);
  629. } else if (clock >= (F_PLL/2) / 48) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(2);
  630. } else if (clock >= (F_PLL/2) / 56) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(2);
  631. } else if (clock >= (F_PLL/2) / 64) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(2);
  632. } else if (clock >= (F_PLL/2) / 80) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(3);
  633. } else if (clock >= (F_PLL/2) / 96) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(3);
  634. } else if (clock >= (F_PLL/2) / 112) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(3);
  635. } else if (clock >= (F_PLL/2) / 128) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(3);
  636. } else if (clock >= (F_PLL/2) / 160) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(4);
  637. } else if (clock >= (F_PLL/2) / 192) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(4);
  638. } else if (clock >= (F_PLL/2) / 224) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(4);
  639. } else if (clock >= (F_PLL/2) / 256) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(4);
  640. } else if (clock >= (F_PLL/2) / 320) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(5);
  641. } else if (clock >= (F_PLL/2) / 384) { c = SPI_BR_SPPR(5) | SPI_BR_SPR(5);
  642. } else if (clock >= (F_PLL/2) / 448) { c = SPI_BR_SPPR(6) | SPI_BR_SPR(5);
  643. } else if (clock >= (F_PLL/2) / 512) { c = SPI_BR_SPPR(7) | SPI_BR_SPR(5);
  644. } else if (clock >= (F_PLL/2) / 640) { c = SPI_BR_SPPR(4) | SPI_BR_SPR(6);
  645. } else /* (F_PLL/2) / 768 */ { c = SPI_BR_SPPR(5) | SPI_BR_SPR(6);
  646. }
  647. } else {
  648. for (uint32_t i=0; i<30; i++) {
  649. c = br_clock_table[i];
  650. if (clock >= (F_PLL/2) / br_div_table[i]) break;
  651. }
  652. }
  653. br1 = c;
  654. }
  655. static const uint8_t br_clock_table[30];
  656. static const uint16_t br_div_table[30];
  657. uint8_t c1, br0, br1;
  658. friend class SPIClass;
  659. friend class SPI1Class;
  660. };
  661. class SPIClass {
  662. public:
  663. // Initialize the SPI library
  664. static void begin();
  665. // If SPI is to used from within an interrupt, this function registers
  666. // that interrupt with the SPI library, so beginTransaction() can
  667. // prevent conflicts. The input interruptNumber is the number used
  668. // with attachInterrupt. If SPI is used from a different interrupt
  669. // (eg, a timer), interruptNumber should be 255.
  670. static void usingInterrupt(uint8_t n) {
  671. if (n == 3 || n == 4) {
  672. usingInterrupt(IRQ_PORTA);
  673. } else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) {
  674. usingInterrupt(IRQ_PORTCD);
  675. }
  676. }
  677. static void usingInterrupt(IRQ_NUMBER_t interruptName) {
  678. uint32_t n = (uint32_t)interruptName;
  679. if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n);
  680. }
  681. static void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
  682. uint32_t n = (uint32_t)interruptName;
  683. if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n);
  684. }
  685. // Before using SPI.transfer() or asserting chip select pins,
  686. // this function is used to gain exclusive access to the SPI bus
  687. // and configure the correct settings.
  688. inline static void beginTransaction(SPISettings settings) {
  689. if (interruptMask) {
  690. __disable_irq();
  691. interruptSave = NVIC_ICER0 & interruptMask;
  692. NVIC_ICER0 = interruptSave;
  693. __enable_irq();
  694. }
  695. #ifdef SPI_TRANSACTION_MISMATCH_LED
  696. if (inTransactionFlag) {
  697. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  698. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  699. }
  700. inTransactionFlag = 1;
  701. #endif
  702. SPI0_C1 = settings.c1;
  703. SPI0_BR = settings.br0;
  704. }
  705. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  706. inline static uint8_t transfer(uint8_t data) {
  707. SPI0_DL = data;
  708. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  709. return SPI0_DL;
  710. }
  711. inline static uint16_t transfer16(uint16_t data) {
  712. SPI0_C2 = SPI_C2_SPIMODE;
  713. SPI0_S;
  714. SPI0_DL = data;
  715. SPI0_DH = data >> 8;
  716. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  717. uint16_t r = SPI0_DL | (SPI0_DH << 8);
  718. SPI0_C2 = 0;
  719. SPI0_S;
  720. return r;
  721. }
  722. inline static void transfer(void *buf, size_t count) {
  723. if (count == 0) return;
  724. uint8_t *p = (uint8_t *)buf;
  725. while (!(SPI0_S & SPI_S_SPTEF)) ; // wait
  726. SPI0_DL = *p;
  727. while (--count > 0) {
  728. uint8_t out = *(p + 1);
  729. while (!(SPI0_S & SPI_S_SPTEF)) ; // wait
  730. __disable_irq();
  731. SPI0_DL = out;
  732. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  733. uint8_t in = SPI0_DL;
  734. __enable_irq();
  735. *p++ = in;
  736. }
  737. while (!(SPI0_S & SPI_S_SPRF)) ; // wait
  738. *p = SPDR;
  739. }
  740. // After performing a group of transfers and releasing the chip select
  741. // signal, this function allows others to access the SPI bus
  742. inline static void endTransaction(void) {
  743. #ifdef SPI_TRANSACTION_MISMATCH_LED
  744. if (!inTransactionFlag) {
  745. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  746. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  747. }
  748. inTransactionFlag = 0;
  749. #endif
  750. if (interruptMask) {
  751. NVIC_ISER0 = interruptSave;
  752. }
  753. }
  754. // Disable the SPI bus
  755. static void end();
  756. // This function is deprecated. New applications should use
  757. // beginTransaction() to configure SPI settings.
  758. static void setBitOrder(uint8_t bitOrder) {
  759. uint8_t c = SPI0_C1 | SPI_C1_SPE;
  760. if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
  761. else c &= ~SPI_C1_LSBFE;
  762. SPI0_C1 = c;
  763. }
  764. // This function is deprecated. New applications should use
  765. // beginTransaction() to configure SPI settings.
  766. static void setDataMode(uint8_t dataMode) {
  767. uint8_t c = SPI0_C1 | SPI_C1_SPE;
  768. if (dataMode & 0x04) c |= SPI_C1_CPHA;
  769. else c &= ~SPI_C1_CPHA;
  770. if (dataMode & 0x08) c |= SPI_C1_CPOL;
  771. else c &= ~SPI_C1_CPOL;
  772. SPI0_C1 = c;
  773. }
  774. // This function is deprecated. New applications should use
  775. // beginTransaction() to configure SPI settings.
  776. inline static void setClockDivider(uint8_t clockDiv) {
  777. if (clockDiv == SPI_CLOCK_DIV2) {
  778. SPI0_BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br0);
  779. } else if (clockDiv == SPI_CLOCK_DIV4) {
  780. SPI0_BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br0);
  781. } else if (clockDiv == SPI_CLOCK_DIV8) {
  782. SPI0_BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br0);
  783. } else if (clockDiv == SPI_CLOCK_DIV16) {
  784. SPI0_BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br0);
  785. } else if (clockDiv == SPI_CLOCK_DIV32) {
  786. SPI0_BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br0);
  787. } else if (clockDiv == SPI_CLOCK_DIV64) {
  788. SPI0_BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br0);
  789. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  790. SPI0_BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br0);
  791. }
  792. }
  793. // These undocumented functions should not be used. SPI.transfer()
  794. // polls the hardware flag which is automatically cleared as the
  795. // AVR responds to SPI's interrupt
  796. inline static void attachInterrupt() { }
  797. inline static void detachInterrupt() { }
  798. // Teensy LC can use alternate pins for these 3 SPI signals.
  799. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  800. SPCR.setMOSI(pin);
  801. }
  802. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  803. SPCR.setMISO(pin);
  804. }
  805. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  806. SPCR.setSCK(pin);
  807. }
  808. // return true if "pin" has special chip select capability
  809. static bool pinIsChipSelect(uint8_t pin) { return (pin == 10 || pin == 2); }
  810. // return true if both pin1 and pin2 have independent chip select capability
  811. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
  812. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  813. static uint8_t setCS(uint8_t pin);
  814. private:
  815. static uint32_t interruptMask;
  816. static uint32_t interruptSave;
  817. #ifdef SPI_TRANSACTION_MISMATCH_LED
  818. static uint8_t inTransactionFlag;
  819. #endif
  820. };
  821. class SPI1Class {
  822. public:
  823. // Initialize the SPI library
  824. static void begin();
  825. // If SPI is to used from within an interrupt, this function registers
  826. // that interrupt with the SPI library, so beginTransaction() can
  827. // prevent conflicts. The input interruptNumber is the number used
  828. // with attachInterrupt. If SPI is used from a different interrupt
  829. // (eg, a timer), interruptNumber should be 255.
  830. static void usingInterrupt(uint8_t n) {
  831. if (n == 3 || n == 4) {
  832. usingInterrupt(IRQ_PORTA);
  833. } else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) {
  834. usingInterrupt(IRQ_PORTCD);
  835. }
  836. }
  837. static void usingInterrupt(IRQ_NUMBER_t interruptName) {
  838. uint32_t n = (uint32_t)interruptName;
  839. if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n);
  840. }
  841. static void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
  842. uint32_t n = (uint32_t)interruptName;
  843. if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n);
  844. }
  845. // Before using SPI.transfer() or asserting chip select pins,
  846. // this function is used to gain exclusive access to the SPI bus
  847. // and configure the correct settings.
  848. inline static void beginTransaction(SPISettings settings) {
  849. if (interruptMask) {
  850. __disable_irq();
  851. interruptSave = NVIC_ICER0 & interruptMask;
  852. NVIC_ICER0 = interruptSave;
  853. __enable_irq();
  854. }
  855. #ifdef SPI_TRANSACTION_MISMATCH_LED
  856. if (inTransactionFlag) {
  857. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  858. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  859. }
  860. inTransactionFlag = 1;
  861. #endif
  862. SPI1_C1 = settings.c1;
  863. SPI1_BR = settings.br1;
  864. }
  865. // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  866. inline static uint8_t transfer(uint8_t data) {
  867. SPI1_DL = data;
  868. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  869. return SPI1_DL;
  870. }
  871. inline static uint16_t transfer16(uint16_t data) {
  872. SPI1_C2 = SPI_C2_SPIMODE;
  873. SPI1_S;
  874. SPI1_DL = data;
  875. SPI1_DH = data >> 8;
  876. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  877. uint16_t r = SPI1_DL | (SPI1_DH << 8);
  878. SPI1_C2 = 0;
  879. SPI1_S;
  880. return r;
  881. }
  882. inline static void transfer(void *buf, size_t count) {
  883. if (count == 0) return;
  884. uint8_t *p = (uint8_t *)buf;
  885. while (!(SPI1_S & SPI_S_SPTEF)) ; // wait
  886. SPI1_DL = *p;
  887. while (--count > 0) {
  888. uint8_t out = *(p + 1);
  889. while (!(SPI1_S & SPI_S_SPTEF)) ; // wait
  890. __disable_irq();
  891. SPI1_DL = out;
  892. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  893. uint8_t in = SPI1_DL;
  894. __enable_irq();
  895. *p++ = in;
  896. }
  897. while (!(SPI1_S & SPI_S_SPRF)) ; // wait
  898. *p = SPDR;
  899. }
  900. // After performing a group of transfers and releasing the chip select
  901. // signal, this function allows others to access the SPI bus
  902. inline static void endTransaction(void) {
  903. #ifdef SPI_TRANSACTION_MISMATCH_LED
  904. if (!inTransactionFlag) {
  905. pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
  906. digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
  907. }
  908. inTransactionFlag = 0;
  909. #endif
  910. if (interruptMask) {
  911. NVIC_ISER0 = interruptSave;
  912. }
  913. }
  914. // Disable the SPI bus
  915. static void end();
  916. // This function is deprecated. New applications should use
  917. // beginTransaction() to configure SPI settings.
  918. static void setBitOrder(uint8_t bitOrder) {
  919. uint8_t c = SPI1_C1 | SPI_C1_SPE;
  920. if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
  921. else c &= ~SPI_C1_LSBFE;
  922. SPI1_C1 = c;
  923. }
  924. // This function is deprecated. New applications should use
  925. // beginTransaction() to configure SPI settings.
  926. static void setDataMode(uint8_t dataMode) {
  927. uint8_t c = SPI1_C1 | SPI_C1_SPE;
  928. if (dataMode & 0x04) c |= SPI_C1_CPHA;
  929. else c &= ~SPI_C1_CPHA;
  930. if (dataMode & 0x08) c |= SPI_C1_CPOL;
  931. else c &= ~SPI_C1_CPOL;
  932. SPI1_C1 = c;
  933. }
  934. // This function is deprecated. New applications should use
  935. // beginTransaction() to configure SPI settings.
  936. inline static void setClockDivider(uint8_t clockDiv) {
  937. if (clockDiv == SPI_CLOCK_DIV2) {
  938. SPI1_BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br1);
  939. } else if (clockDiv == SPI_CLOCK_DIV4) {
  940. SPI1_BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br1);
  941. } else if (clockDiv == SPI_CLOCK_DIV8) {
  942. SPI1_BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br1);
  943. } else if (clockDiv == SPI_CLOCK_DIV16) {
  944. SPI1_BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br1);
  945. } else if (clockDiv == SPI_CLOCK_DIV32) {
  946. SPI1_BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br1);
  947. } else if (clockDiv == SPI_CLOCK_DIV64) {
  948. SPI1_BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br1);
  949. } else { /* clockDiv == SPI_CLOCK_DIV128 */
  950. SPI1_BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br1);
  951. }
  952. }
  953. // These undocumented functions should not be used. SPI.transfer()
  954. // polls the hardware flag which is automatically cleared as the
  955. // AVR responds to SPI's interrupt
  956. inline static void attachInterrupt() { }
  957. inline static void detachInterrupt() { }
  958. // Teensy LC can use alternate pins for these 3 SPI signals.
  959. inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  960. SPCR1.setMOSI(pin);
  961. }
  962. inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
  963. SPCR1.setMISO(pin);
  964. }
  965. inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
  966. SPCR1.setSCK(pin);
  967. }
  968. // return true if "pin" has special chip select capability
  969. static bool pinIsChipSelect(uint8_t pin) { return (pin == 6); }
  970. // return true if both pin1 and pin2 have independent chip select capability
  971. static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
  972. // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
  973. static uint8_t setCS(uint8_t pin);
  974. private:
  975. static uint32_t interruptMask;
  976. static uint32_t interruptSave;
  977. #ifdef SPI_TRANSACTION_MISMATCH_LED
  978. static uint8_t inTransactionFlag;
  979. #endif
  980. };
  981. /**********************************************************/
  982. /* 32 bit Arduino Due */
  983. /**********************************************************/
  984. #elif defined(__arm__) && defined(__SAM3X8E__)
  985. #undef SPI_MODE0
  986. #undef SPI_MODE1
  987. #undef SPI_MODE2
  988. #undef SPI_MODE3
  989. #define SPI_MODE0 0x02
  990. #define SPI_MODE1 0x00
  991. #define SPI_MODE2 0x03
  992. #define SPI_MODE3 0x01
  993. #undef SPI_CLOCK_DIV2
  994. #undef SPI_CLOCK_DIV4
  995. #undef SPI_CLOCK_DIV8
  996. #undef SPI_CLOCK_DIV16
  997. #undef SPI_CLOCK_DIV32
  998. #undef SPI_CLOCK_DIV64
  999. #undef SPI_CLOCK_DIV128
  1000. #define SPI_CLOCK_DIV2 11
  1001. #define SPI_CLOCK_DIV4 21
  1002. #define SPI_CLOCK_DIV8 42
  1003. #define SPI_CLOCK_DIV16 84
  1004. #define SPI_CLOCK_DIV32 168
  1005. #define SPI_CLOCK_DIV64 255
  1006. #define SPI_CLOCK_DIV128 255
  1007. enum SPITransferMode {
  1008. SPI_CONTINUE,
  1009. SPI_LAST
  1010. };
  1011. class SPISettings {
  1012. public:
  1013. SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
  1014. if (__builtin_constant_p(clock)) {
  1015. init_AlwaysInline(clock, bitOrder, dataMode);
  1016. } else {
  1017. init_MightInline(clock, bitOrder, dataMode);
  1018. }
  1019. }
  1020. SPISettings() {
  1021. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
  1022. }
  1023. private:
  1024. void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
  1025. init_AlwaysInline(clock, bitOrder, dataMode);
  1026. }
  1027. void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode)
  1028. __attribute__((__always_inline__)) {
  1029. uint8_t div;
  1030. border = bitOrder;
  1031. if (__builtin_constant_p(clock)) {
  1032. if (clock >= F_CPU / 2) div = 2;
  1033. else if (clock >= F_CPU / 3) div = 3;
  1034. else if (clock >= F_CPU / 4) div = 4;
  1035. else if (clock >= F_CPU / 5) div = 5;
  1036. else if (clock >= F_CPU / 6) div = 6;
  1037. else if (clock >= F_CPU / 7) div = 7;
  1038. else if (clock >= F_CPU / 8) div = 8;
  1039. else if (clock >= F_CPU / 9) div = 9;
  1040. else if (clock >= F_CPU / 10) div = 10;
  1041. else if (clock >= F_CPU / 11) div = 11;
  1042. else if (clock >= F_CPU / 12) div = 12;
  1043. else if (clock >= F_CPU / 13) div = 13;
  1044. else if (clock >= F_CPU / 14) div = 14;
  1045. else if (clock >= F_CPU / 15) div = 15;
  1046. else if (clock >= F_CPU / 16) div = 16;
  1047. else if (clock >= F_CPU / 17) div = 17;
  1048. else if (clock >= F_CPU / 18) div = 18;
  1049. else if (clock >= F_CPU / 19) div = 19;
  1050. else if (clock >= F_CPU / 20) div = 20;
  1051. else if (clock >= F_CPU / 21) div = 21;
  1052. else if (clock >= F_CPU / 22) div = 22;
  1053. else if (clock >= F_CPU / 23) div = 23;
  1054. else if (clock >= F_CPU / 24) div = 24;
  1055. else if (clock >= F_CPU / 25) div = 25;
  1056. else if (clock >= F_CPU / 26) div = 26;
  1057. else if (clock >= F_CPU / 27) div = 27;
  1058. else if (clock >= F_CPU / 28) div = 28;
  1059. else if (clock >= F_CPU / 29) div = 29;
  1060. else if (clock >= F_CPU / 30) div = 30;
  1061. else if (clock >= F_CPU / 31) div = 31;
  1062. else if (clock >= F_CPU / 32) div = 32;
  1063. else if (clock >= F_CPU / 33) div = 33;
  1064. else if (clock >= F_CPU / 34) div = 34;
  1065. else if (clock >= F_CPU / 35) div = 35;
  1066. else if (clock >= F_CPU / 36) div = 36;
  1067. else if (clock >= F_CPU / 37) div = 37;
  1068. else if (clock >= F_CPU / 38) div = 38;
  1069. else if (clock >= F_CPU / 39) div = 39;
  1070. else if (clock >= F_CPU / 40) div = 40;
  1071. else if (clock >= F_CPU / 41) div = 41;
  1072. else if (clock >= F_CPU / 42) div = 42;
  1073. else if (clock >= F_CPU / 43) div = 43;
  1074. else if (clock >= F_CPU / 44) div = 44;
  1075. else if (clock >= F_CPU / 45) div = 45;
  1076. else if (clock >= F_CPU / 46) div = 46;
  1077. else if (clock >= F_CPU / 47) div = 47;
  1078. else if (clock >= F_CPU / 48) div = 48;
  1079. else if (clock >= F_CPU / 49) div = 49;
  1080. else if (clock >= F_CPU / 50) div = 50;
  1081. else if (clock >= F_CPU / 51) div = 51;
  1082. else if (clock >= F_CPU / 52) div = 52;
  1083. else if (clock >= F_CPU / 53) div = 53;
  1084. else if (clock >= F_CPU / 54) div = 54;
  1085. else if (clock >= F_CPU / 55) div = 55;
  1086. else if (clock >= F_CPU / 56) div = 56;
  1087. else if (clock >= F_CPU / 57) div = 57;
  1088. else if (clock >= F_CPU / 58) div = 58;
  1089. else if (clock >= F_CPU / 59) div = 59;
  1090. else if (clock >= F_CPU / 60) div = 60;
  1091. else if (clock >= F_CPU / 61) div = 61;
  1092. else if (clock >= F_CPU / 62) div = 62;
  1093. else if (clock >= F_CPU / 63) div = 63;
  1094. else if (clock >= F_CPU / 64) div = 64;
  1095. else if (clock >= F_CPU / 65) div = 65;
  1096. else if (clock >= F_CPU / 66) div = 66;
  1097. else if (clock >= F_CPU / 67) div = 67;
  1098. else if (clock >= F_CPU / 68) div = 68;
  1099. else if (clock >= F_CPU / 69) div = 69;
  1100. else if (clock >= F_CPU / 70) div = 70;
  1101. else if (clock >= F_CPU / 71) div = 71;
  1102. else if (clock >= F_CPU / 72) div = 72;
  1103. else if (clock >= F_CPU / 73) div = 73;
  1104. else if (clock >= F_CPU / 74) div = 74;
  1105. else if (clock >= F_CPU / 75) div = 75;
  1106. else if (clock >= F_CPU / 76) div = 76;
  1107. else if (clock >= F_CPU / 77) div = 77;
  1108. else if (clock >= F_CPU / 78) div = 78;
  1109. else if (clock >= F_CPU / 79) div = 79;
  1110. else if (clock >= F_CPU / 80) div = 80;
  1111. else if (clock >= F_CPU / 81) div = 81;
  1112. else if (clock >= F_CPU / 82) div = 82;
  1113. else if (clock >= F_CPU / 83) div = 83;
  1114. else if (clock >= F_CPU / 84) div = 84;
  1115. else if (clock >= F_CPU / 85) div = 85;
  1116. else if (clock >= F_CPU / 86) div = 86;
  1117. else if (clock >= F_CPU / 87) div = 87;
  1118. else if (clock >= F_CPU / 88) div = 88;
  1119. else if (clock >= F_CPU / 89) div = 89;
  1120. else if (clock >= F_CPU / 90) div = 90;
  1121. else if (clock >= F_CPU / 91) div = 91;
  1122. else if (clock >= F_CPU / 92) div = 92;
  1123. else if (clock >= F_CPU / 93) div = 93;
  1124. else if (clock >= F_CPU / 94) div = 94;
  1125. else if (clock >= F_CPU / 95) div = 95;
  1126. else if (clock >= F_CPU / 96) div = 96;
  1127. else if (clock >= F_CPU / 97) div = 97;
  1128. else if (clock >= F_CPU / 98) div = 98;
  1129. else if (clock >= F_CPU / 99) div = 99;
  1130. else if (clock >= F_CPU / 100) div = 100;
  1131. else if (clock >= F_CPU / 101) div = 101;
  1132. else if (clock >= F_CPU / 102) div = 102;
  1133. else if (clock >= F_CPU / 103) div = 103;
  1134. else if (clock >= F_CPU / 104) div = 104;
  1135. else if (clock >= F_CPU / 105) div = 105;
  1136. else if (clock >= F_CPU / 106) div = 106;
  1137. else if (clock >= F_CPU / 107) div = 107;
  1138. else if (clock >= F_CPU / 108) div = 108;
  1139. else if (clock >= F_CPU / 109) div = 109;
  1140. else if (clock >= F_CPU / 110) div = 110;
  1141. else if (clock >= F_CPU / 111) div = 111;
  1142. else if (clock >= F_CPU / 112) div = 112;
  1143. else if (clock >= F_CPU / 113) div = 113;
  1144. else if (clock >= F_CPU / 114) div = 114;
  1145. else if (clock >= F_CPU / 115) div = 115;
  1146. else if (clock >= F_CPU / 116) div = 116;
  1147. else if (clock >= F_CPU / 117) div = 117;
  1148. else if (clock >= F_CPU / 118) div = 118;
  1149. else if (clock >= F_CPU / 119) div = 119;
  1150. else if (clock >= F_CPU / 120) div = 120;
  1151. else if (clock >= F_CPU / 121) div = 121;
  1152. else if (clock >= F_CPU / 122) div = 122;
  1153. else if (clock >= F_CPU / 123) div = 123;
  1154. else if (clock >= F_CPU / 124) div = 124;
  1155. else if (clock >= F_CPU / 125) div = 125;
  1156. else if (clock >= F_CPU / 126) div = 126;
  1157. else if (clock >= F_CPU / 127) div = 127;
  1158. else if (clock >= F_CPU / 128) div = 128;
  1159. else if (clock >= F_CPU / 129) div = 129;
  1160. else if (clock >= F_CPU / 130) div = 130;
  1161. else if (clock >= F_CPU / 131) div = 131;
  1162. else if (clock >= F_CPU / 132) div = 132;
  1163. else if (clock >= F_CPU / 133) div = 133;
  1164. else if (clock >= F_CPU / 134) div = 134;
  1165. else if (clock >= F_CPU / 135) div = 135;
  1166. else if (clock >= F_CPU / 136) div = 136;
  1167. else if (clock >= F_CPU / 137) div = 137;
  1168. else if (clock >= F_CPU / 138) div = 138;
  1169. else if (clock >= F_CPU / 139) div = 139;
  1170. else if (clock >= F_CPU / 140) div = 140;
  1171. else if (clock >= F_CPU / 141) div = 141;
  1172. else if (clock >= F_CPU / 142) div = 142;
  1173. else if (clock >= F_CPU / 143) div = 143;
  1174. else if (clock >= F_CPU / 144) div = 144;
  1175. else if (clock >= F_CPU / 145) div = 145;
  1176. else if (clock >= F_CPU / 146) div = 146;
  1177. else if (clock >= F_CPU / 147) div = 147;
  1178. else if (clock >= F_CPU / 148) div = 148;
  1179. else if (clock >= F_CPU / 149) div = 149;
  1180. else if (clock >= F_CPU / 150) div = 150;
  1181. else if (clock >= F_CPU / 151) div = 151;
  1182. else if (clock >= F_CPU / 152) div = 152;
  1183. else if (clock >= F_CPU / 153) div = 153;
  1184. else if (clock >= F_CPU / 154) div = 154;
  1185. else if (clock >= F_CPU / 155) div = 155;
  1186. else if (clock >= F_CPU / 156) div = 156;
  1187. else if (clock >= F_CPU / 157) div = 157;
  1188. else if (clock >= F_CPU / 158) div = 158;
  1189. else if (clock >= F_CPU / 159) div = 159;
  1190. else if (clock >= F_CPU / 160) div = 160;
  1191. else if (clock >= F_CPU / 161) div = 161;
  1192. else if (clock >= F_CPU / 162) div = 162;
  1193. else if (clock >= F_CPU / 163) div = 163;
  1194. else if (clock >= F_CPU / 164) div = 164;
  1195. else if (clock >= F_CPU / 165) div = 165;
  1196. else if (clock >= F_CPU / 166) div = 166;
  1197. else if (clock >= F_CPU / 167) div = 167;
  1198. else if (clock >= F_CPU / 168) div = 168;
  1199. else if (clock >= F_CPU / 169) div = 169;
  1200. else if (clock >= F_CPU / 170) div = 170;
  1201. else if (clock >= F_CPU / 171) div = 171;
  1202. else if (clock >= F_CPU / 172) div = 172;
  1203. else if (clock >= F_CPU / 173) div = 173;
  1204. else if (clock >= F_CPU / 174) div = 174;
  1205. else if (clock >= F_CPU / 175) div = 175;
  1206. else if (clock >= F_CPU / 176) div = 176;
  1207. else if (clock >= F_CPU / 177) div = 177;
  1208. else if (clock >= F_CPU / 178) div = 178;
  1209. else if (clock >= F_CPU / 179) div = 179;
  1210. else if (clock >= F_CPU / 180) div = 180;
  1211. else if (clock >= F_CPU / 181) div = 181;
  1212. else if (clock >= F_CPU / 182) div = 182;
  1213. else if (clock >= F_CPU / 183) div = 183;
  1214. else if (clock >= F_CPU / 184) div = 184;
  1215. else if (clock >= F_CPU / 185) div = 185;
  1216. else if (clock >= F_CPU / 186) div = 186;
  1217. else if (clock >= F_CPU / 187) div = 187;
  1218. else if (clock >= F_CPU / 188) div = 188;
  1219. else if (clock >= F_CPU / 189) div = 189;
  1220. else if (clock >= F_CPU / 190) div = 190;
  1221. else if (clock >= F_CPU / 191) div = 191;
  1222. else if (clock >= F_CPU / 192) div = 192;
  1223. else if (clock >= F_CPU / 193) div = 193;
  1224. else if (clock >= F_CPU / 194) div = 194;
  1225. else if (clock >= F_CPU / 195) div = 195;
  1226. else if (clock >= F_CPU / 196) div = 196;
  1227. else if (clock >= F_CPU / 197) div = 197;
  1228. else if (clock >= F_CPU / 198) div = 198;
  1229. else if (clock >= F_CPU / 199) div = 199;
  1230. else if (clock >= F_CPU / 200) div = 200;
  1231. else if (clock >= F_CPU / 201) div = 201;
  1232. else if (clock >= F_CPU / 202) div = 202;
  1233. else if (clock >= F_CPU / 203) div = 203;
  1234. else if (clock >= F_CPU / 204) div = 204;
  1235. else if (clock >= F_CPU / 205) div = 205;
  1236. else if (clock >= F_CPU / 206) div = 206;
  1237. else if (clock >= F_CPU / 207) div = 207;
  1238. else if (clock >= F_CPU / 208) div = 208;
  1239. else if (clock >= F_CPU / 209) div = 209;
  1240. else if (clock >= F_CPU / 210) div = 210;
  1241. else if (clock >= F_CPU / 211) div = 211;
  1242. else if (clock >= F_CPU / 212) div = 212;
  1243. else if (clock >= F_CPU / 213) div = 213;
  1244. else if (clock >= F_CPU / 214) div = 214;
  1245. else if (clock >= F_CPU / 215) div = 215;
  1246. else if (clock >= F_CPU / 216) div = 216;
  1247. else if (clock >= F_CPU / 217) div = 217;
  1248. else if (clock >= F_CPU / 218) div = 218;
  1249. else if (clock >= F_CPU / 219) div = 219;
  1250. else if (clock >= F_CPU / 220) div = 220;
  1251. else if (clock >= F_CPU / 221) div = 221;
  1252. else if (clock >= F_CPU / 222) div = 222;
  1253. else if (clock >= F_CPU / 223) div = 223;
  1254. else if (clock >= F_CPU / 224) div = 224;
  1255. else if (clock >= F_CPU / 225) div = 225;
  1256. else if (clock >= F_CPU / 226) div = 226;
  1257. else if (clock >= F_CPU / 227) div = 227;
  1258. else if (clock >= F_CPU / 228) div = 228;
  1259. else if (clock >= F_CPU / 229) div = 229;
  1260. else if (clock >= F_CPU / 230) div = 230;
  1261. else if (clock >= F_CPU / 231) div = 231;
  1262. else if (clock >= F_CPU / 232) div = 232;
  1263. else if (clock >= F_CPU / 233) div = 233;
  1264. else if (clock >= F_CPU / 234) div = 234;
  1265. else if (clock >= F_CPU / 235) div = 235;
  1266. else if (clock >= F_CPU / 236) div = 236;
  1267. else if (clock >= F_CPU / 237) div = 237;
  1268. else if (clock >= F_CPU / 238) div = 238;
  1269. else if (clock >= F_CPU / 239) div = 239;
  1270. else if (clock >= F_CPU / 240) div = 240;
  1271. else if (clock >= F_CPU / 241) div = 241;
  1272. else if (clock >= F_CPU / 242) div = 242;
  1273. else if (clock >= F_CPU / 243) div = 243;
  1274. else if (clock >= F_CPU / 244) div = 244;
  1275. else if (clock >= F_CPU / 245) div = 245;
  1276. else if (clock >= F_CPU / 246) div = 246;
  1277. else if (clock >= F_CPU / 247) div = 247;
  1278. else if (clock >= F_CPU / 248) div = 248;
  1279. else if (clock >= F_CPU / 249) div = 249;
  1280. else if (clock >= F_CPU / 250) div = 250;
  1281. else if (clock >= F_CPU / 251) div = 251;
  1282. else if (clock >= F_CPU / 252) div = 252;
  1283. else if (clock >= F_CPU / 253) div = 253;
  1284. else if (clock >= F_CPU / 254) div = 254;
  1285. else /* clock >= F_CPU / 255 */ div = 255;
  1286. /*
  1287. #! /usr/bin/perl
  1288. for ($i=2; $i<256; $i++) {
  1289. printf "\t\t\telse if (clock >= F_CPU / %3d) div = %3d;\n", $i, $i;
  1290. }
  1291. */
  1292. } else {
  1293. for (div=2; div<255; div++) {
  1294. if (clock >= F_CPU / div) break;
  1295. }
  1296. }
  1297. config = (dataMode & 3) | SPI_CSR_CSAAT | SPI_CSR_SCBR(div) | SPI_CSR_DLYBCT(1);
  1298. }
  1299. uint32_t config;
  1300. BitOrder border;
  1301. friend class SPIClass;
  1302. };
  1303. class SPIClass {
  1304. public:
  1305. SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void));
  1306. byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(BOARD_SPI_DEFAULT_SS, _data, _mode); }
  1307. byte transfer(byte _channel, uint8_t _data, SPITransferMode _mode = SPI_LAST);
  1308. // Transaction Functions
  1309. void usingInterrupt(uint8_t interruptNumber);
  1310. void beginTransaction(uint8_t pin, SPISettings settings);
  1311. void beginTransaction(SPISettings settings) {
  1312. beginTransaction(BOARD_SPI_DEFAULT_SS, settings);
  1313. }
  1314. void endTransaction(void);
  1315. // SPI Configuration methods
  1316. void attachInterrupt(void);
  1317. void detachInterrupt(void);
  1318. void begin(void);
  1319. void end(void);
  1320. // Attach/Detach pin to/from SPI controller
  1321. void begin(uint8_t _pin);
  1322. void end(uint8_t _pin);
  1323. // These methods sets a parameter on a single pin
  1324. void setBitOrder(uint8_t _pin, BitOrder);
  1325. void setDataMode(uint8_t _pin, uint8_t);
  1326. void setClockDivider(uint8_t _pin, uint8_t);
  1327. // These methods sets the same parameters but on default pin BOARD_SPI_DEFAULT_SS
  1328. void setBitOrder(BitOrder _order) { setBitOrder(BOARD_SPI_DEFAULT_SS, _order); };
  1329. void setDataMode(uint8_t _mode) { setDataMode(BOARD_SPI_DEFAULT_SS, _mode); };
  1330. void setClockDivider(uint8_t _div) { setClockDivider(BOARD_SPI_DEFAULT_SS, _div); };
  1331. private:
  1332. void init();
  1333. Spi *spi;
  1334. uint32_t id;
  1335. BitOrder bitOrder[SPI_CHANNELS_NUM];
  1336. uint32_t divider[SPI_CHANNELS_NUM];
  1337. uint32_t mode[SPI_CHANNELS_NUM];
  1338. void (*initCb)(void);
  1339. bool initialized;
  1340. uint8_t interruptMode; // 0=none, 1=mask, 2=global
  1341. uint8_t interruptMask; // bits 0:3=pin change
  1342. uint8_t interruptSave; // temp storage, to restore state
  1343. };
  1344. #endif
  1345. extern SPIClass SPI;
  1346. #if defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  1347. extern SPI1Class SPI1;
  1348. #endif
  1349. #endif