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

1507 行
51KB

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