Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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