You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

930 lines
25KB

  1. /*
  2. * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
  3. * SPI Master library for arduino.
  4. *
  5. * This file is free software; you can redistribute it and/or modify
  6. * it under the terms of either the GNU General Public License version 2
  7. * or the GNU Lesser General Public License version 2.1, both as
  8. * published by the Free Software Foundation.
  9. */
  10. #include "SPI.h"
  11. #include "pins_arduino.h"
  12. /**********************************************************/
  13. /* 8 bit AVR-based boards */
  14. /**********************************************************/
  15. #if defined(__AVR__)
  16. SPIClass SPI;
  17. uint8_t SPIClass::interruptMode = 0;
  18. uint8_t SPIClass::interruptMask = 0;
  19. uint8_t SPIClass::interruptSave = 0;
  20. #ifdef SPI_TRANSACTION_MISMATCH_LED
  21. uint8_t SPIClass::inTransactionFlag = 0;
  22. #endif
  23. void SPIClass::begin()
  24. {
  25. // Set SS to high so a connected chip will be "deselected" by default
  26. digitalWrite(SS, HIGH);
  27. // When the SS pin is set as OUTPUT, it can be used as
  28. // a general purpose output port (it doesn't influence
  29. // SPI operations).
  30. pinMode(SS, OUTPUT);
  31. // Warning: if the SS pin ever becomes a LOW INPUT then SPI
  32. // automatically switches to Slave, so the data direction of
  33. // the SS pin MUST be kept as OUTPUT.
  34. SPCR |= _BV(MSTR);
  35. SPCR |= _BV(SPE);
  36. // Set direction register for SCK and MOSI pin.
  37. // MISO pin automatically overrides to INPUT.
  38. // By doing this AFTER enabling SPI, we avoid accidentally
  39. // clocking in a single bit since the lines go directly
  40. // from "input" to SPI control.
  41. // http://code.google.com/p/arduino/issues/detail?id=888
  42. pinMode(SCK, OUTPUT);
  43. pinMode(MOSI, OUTPUT);
  44. }
  45. void SPIClass::end() {
  46. SPCR &= ~_BV(SPE);
  47. }
  48. // mapping of interrupt numbers to bits within SPI_AVR_EIMSK
  49. #if defined(__AVR_ATmega32U4__)
  50. #define SPI_INT0_MASK (1<<INT0)
  51. #define SPI_INT1_MASK (1<<INT1)
  52. #define SPI_INT2_MASK (1<<INT2)
  53. #define SPI_INT3_MASK (1<<INT3)
  54. #define SPI_INT4_MASK (1<<INT6)
  55. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  56. #define SPI_INT0_MASK (1<<INT0)
  57. #define SPI_INT1_MASK (1<<INT1)
  58. #define SPI_INT2_MASK (1<<INT2)
  59. #define SPI_INT3_MASK (1<<INT3)
  60. #define SPI_INT4_MASK (1<<INT4)
  61. #define SPI_INT5_MASK (1<<INT5)
  62. #define SPI_INT6_MASK (1<<INT6)
  63. #define SPI_INT7_MASK (1<<INT7)
  64. #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  65. #define SPI_INT0_MASK (1<<INT4)
  66. #define SPI_INT1_MASK (1<<INT5)
  67. #define SPI_INT2_MASK (1<<INT0)
  68. #define SPI_INT3_MASK (1<<INT1)
  69. #define SPI_INT4_MASK (1<<INT2)
  70. #define SPI_INT5_MASK (1<<INT3)
  71. #define SPI_INT6_MASK (1<<INT6)
  72. #define SPI_INT7_MASK (1<<INT7)
  73. #else
  74. #ifdef INT0
  75. #define SPI_INT0_MASK (1<<INT0)
  76. #endif
  77. #ifdef INT1
  78. #define SPI_INT1_MASK (1<<INT1)
  79. #endif
  80. #ifdef INT2
  81. #define SPI_INT2_MASK (1<<INT2)
  82. #endif
  83. #endif
  84. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  85. {
  86. uint8_t stmp, mask;
  87. if (interruptMode > 1) return;
  88. stmp = SREG;
  89. noInterrupts();
  90. switch (interruptNumber) {
  91. #ifdef SPI_INT0_MASK
  92. case 0: mask = SPI_INT0_MASK; break;
  93. #endif
  94. #ifdef SPI_INT1_MASK
  95. case 1: mask = SPI_INT1_MASK; break;
  96. #endif
  97. #ifdef SPI_INT2_MASK
  98. case 2: mask = SPI_INT2_MASK; break;
  99. #endif
  100. #ifdef SPI_INT3_MASK
  101. case 3: mask = SPI_INT3_MASK; break;
  102. #endif
  103. #ifdef SPI_INT4_MASK
  104. case 4: mask = SPI_INT4_MASK; break;
  105. #endif
  106. #ifdef SPI_INT5_MASK
  107. case 5: mask = SPI_INT5_MASK; break;
  108. #endif
  109. #ifdef SPI_INT6_MASK
  110. case 6: mask = SPI_INT6_MASK; break;
  111. #endif
  112. #ifdef SPI_INT7_MASK
  113. case 7: mask = SPI_INT7_MASK; break;
  114. #endif
  115. default:
  116. interruptMode = 2;
  117. SREG = stmp;
  118. return;
  119. }
  120. interruptMode = 1;
  121. interruptMask |= mask;
  122. SREG = stmp;
  123. }
  124. /**********************************************************/
  125. /* 32 bit Teensy 3.0 and 3.1 */
  126. /**********************************************************/
  127. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
  128. SPIClass SPI;
  129. uint8_t SPIClass::interruptMasksUsed = 0;
  130. uint32_t SPIClass::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  131. uint32_t SPIClass::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  132. #ifdef SPI_TRANSACTION_MISMATCH_LED
  133. uint8_t SPIClass::inTransactionFlag = 0;
  134. #endif
  135. void SPIClass::begin()
  136. {
  137. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  138. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  139. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  140. SPI0_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  141. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  142. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  143. }
  144. void SPIClass::end() {
  145. SPCR.disable_pins();
  146. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  147. }
  148. void SPIClass::usingInterrupt(IRQ_NUMBER_t interruptName)
  149. {
  150. uint32_t n = (uint32_t)interruptName;
  151. if (n >= NVIC_NUM_INTERRUPTS) return;
  152. //Serial.print("usingInterrupt ");
  153. //Serial.println(n);
  154. interruptMasksUsed |= (1 << (n >> 5));
  155. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  156. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  157. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  158. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  159. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  160. }
  161. void SPIClass::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  162. {
  163. uint32_t n = (uint32_t)interruptName;
  164. if (n >= NVIC_NUM_INTERRUPTS) return;
  165. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  166. if (interruptMask[n >> 5] == 0) {
  167. interruptMasksUsed &= ~(1 << (n >> 5));
  168. }
  169. }
  170. const uint16_t SPISettings::ctar_div_table[23] = {
  171. 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40,
  172. 56, 64, 96, 128, 192, 256, 384, 512, 640, 768
  173. };
  174. const uint32_t SPISettings::ctar_clock_table[23] = {
  175. SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  176. SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  177. SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  178. SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  179. SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  180. SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1),
  181. SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  182. SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1),
  183. SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  184. SPI_CTAR_PBR(2) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(0),
  185. SPI_CTAR_PBR(1) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  186. SPI_CTAR_PBR(0) | SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(3),
  187. SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  188. SPI_CTAR_PBR(3) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  189. SPI_CTAR_PBR(0) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4),
  190. SPI_CTAR_PBR(1) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4),
  191. SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5),
  192. SPI_CTAR_PBR(1) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5),
  193. SPI_CTAR_PBR(0) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  194. SPI_CTAR_PBR(1) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  195. SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7),
  196. SPI_CTAR_PBR(2) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  197. SPI_CTAR_PBR(1) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7)
  198. };
  199. static void updateCTAR(uint32_t ctar)
  200. {
  201. if (SPI0_CTAR0 != ctar) {
  202. uint32_t mcr = SPI0_MCR;
  203. if (mcr & SPI_MCR_MDIS) {
  204. SPI0_CTAR0 = ctar;
  205. SPI0_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  206. } else {
  207. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  208. SPI0_CTAR0 = ctar;
  209. SPI0_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  210. SPI0_MCR = mcr;
  211. }
  212. }
  213. }
  214. void SPIClass::setBitOrder(uint8_t bitOrder)
  215. {
  216. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  217. uint32_t ctar = SPI0_CTAR0;
  218. if (bitOrder == LSBFIRST) {
  219. ctar |= SPI_CTAR_LSBFE;
  220. } else {
  221. ctar &= ~SPI_CTAR_LSBFE;
  222. }
  223. updateCTAR(ctar);
  224. }
  225. void SPIClass::setDataMode(uint8_t dataMode)
  226. {
  227. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  228. // TODO: implement with native code
  229. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  230. }
  231. void SPIClass::setClockDivider_noInline(uint32_t clk)
  232. {
  233. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  234. uint32_t ctar = SPI0_CTAR0;
  235. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  236. if (ctar & SPI_CTAR_CPHA) {
  237. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  238. }
  239. ctar |= clk;
  240. updateCTAR(ctar);
  241. }
  242. uint8_t SPIClass::pinIsChipSelect(uint8_t pin)
  243. {
  244. switch (pin) {
  245. case 10: return 0x01; // PTC4
  246. case 2: return 0x01; // PTD0
  247. case 9: return 0x02; // PTC3
  248. case 6: return 0x02; // PTD4
  249. case 20: return 0x04; // PTD5
  250. case 23: return 0x04; // PTC2
  251. case 21: return 0x08; // PTD6
  252. case 22: return 0x08; // PTC1
  253. case 15: return 0x10; // PTC0
  254. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  255. case 26: return 0x01;
  256. case 45: return 0x20; // CS5
  257. #endif
  258. }
  259. return 0;
  260. }
  261. bool SPIClass::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  262. {
  263. uint8_t pin1_mask, pin2_mask;
  264. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  265. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  266. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  267. if ((pin1_mask & pin2_mask) != 0) return false;
  268. return true;
  269. }
  270. // setCS() is not intended for use from normal Arduino programs/sketches.
  271. uint8_t SPIClass::setCS(uint8_t pin)
  272. {
  273. switch (pin) {
  274. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  275. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  276. case 9: CORE_PIN9_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTC3
  277. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTD4
  278. case 20: CORE_PIN20_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTD5
  279. case 23: CORE_PIN23_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTC2
  280. case 21: CORE_PIN21_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTD6
  281. case 22: CORE_PIN22_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTC1
  282. case 15: CORE_PIN15_CONFIG = PORT_PCR_MUX(2); return 0x10; // PTC0
  283. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  284. case 26: CORE_PIN26_CONFIG = PORT_PCR_MUX(2);return 0x01;
  285. case 45: CORE_PIN45_CONFIG = PORT_PCR_MUX(3);return 0x20;
  286. #endif
  287. }
  288. return 0;
  289. }
  290. /**********************************************************/
  291. /* 32 bit Teensy-3.5/3.6 */
  292. /**********************************************************/
  293. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  294. SPI1Class SPI1;
  295. uint8_t SPI1Class::interruptMasksUsed = 0;
  296. uint32_t SPI1Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  297. uint32_t SPI1Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  298. #ifdef SPI_TRANSACTION_MISMATCH_LED
  299. uint8_t SPI1Class::inTransactionFlag = 0;
  300. #endif
  301. void SPI1Class::begin()
  302. {
  303. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  304. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  305. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  306. SPI1_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  307. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  308. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  309. }
  310. void SPI1Class::end() {
  311. SPCR1.disable_pins();
  312. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  313. }
  314. void SPI1Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  315. {
  316. uint32_t n = (uint32_t)interruptName;
  317. if (n >= NVIC_NUM_INTERRUPTS) return;
  318. //Serial.print("usingInterrupt ");
  319. //Serial.println(n);
  320. interruptMasksUsed |= (1 << (n >> 5));
  321. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  322. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  323. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  324. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  325. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  326. }
  327. void SPI1Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  328. {
  329. uint32_t n = (uint32_t)interruptName;
  330. if (n >= NVIC_NUM_INTERRUPTS) return;
  331. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  332. if (interruptMask[n >> 5] == 0) {
  333. interruptMasksUsed &= ~(1 << (n >> 5));
  334. }
  335. }
  336. static void updateCTAR1(uint32_t ctar)
  337. {
  338. if (SPI1_CTAR0 != ctar) {
  339. uint32_t mcr = SPI1_MCR;
  340. if (mcr & SPI_MCR_MDIS) {
  341. SPI1_CTAR0 = ctar;
  342. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  343. } else {
  344. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  345. SPI1_CTAR0 = ctar;
  346. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  347. SPI1_MCR = mcr;
  348. }
  349. }
  350. }
  351. void SPI1Class::setBitOrder(uint8_t bitOrder)
  352. {
  353. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  354. uint32_t ctar = SPI1_CTAR0;
  355. if (bitOrder == LSBFIRST) {
  356. ctar |= SPI_CTAR_LSBFE;
  357. } else {
  358. ctar &= ~SPI_CTAR_LSBFE;
  359. }
  360. updateCTAR1(ctar);
  361. }
  362. void SPI1Class::setDataMode(uint8_t dataMode)
  363. {
  364. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  365. // TODO: implement with native code
  366. SPCR1 = (SPCR1 & ~SPI_MODE_MASK) | dataMode;
  367. }
  368. void SPI1Class::setClockDivider_noInline(uint32_t clk)
  369. {
  370. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  371. uint32_t ctar = SPI1_CTAR0;
  372. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  373. if (ctar & SPI_CTAR_CPHA) {
  374. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  375. }
  376. ctar |= clk;
  377. updateCTAR1(ctar);
  378. }
  379. uint8_t SPI1Class::pinIsChipSelect(uint8_t pin)
  380. {
  381. switch (pin) {
  382. case 6: return 0x01; // CS0
  383. case 31: return 0x01; // CS0
  384. case 58: return 0x02; //CS1
  385. case 62: return 0x01; //CS0
  386. case 63: return 0x04; //CS2
  387. }
  388. return 0;
  389. }
  390. bool SPI1Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  391. {
  392. uint8_t pin1_mask, pin2_mask;
  393. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  394. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  395. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  396. if ((pin1_mask & pin2_mask) != 0) return false;
  397. return true;
  398. }
  399. // setCS() is not intended for use from normal Arduino programs/sketches.
  400. uint8_t SPI1Class::setCS(uint8_t pin)
  401. {
  402. switch (pin) {
  403. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(7); return 0x01; // PTD4
  404. case 31: CORE_PIN31_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD5
  405. case 58: CORE_PIN58_CONFIG = PORT_PCR_MUX(2); return 0x02; //CS1
  406. case 62: CORE_PIN62_CONFIG = PORT_PCR_MUX(2); return 0x01; //CS0
  407. case 63: CORE_PIN63_CONFIG = PORT_PCR_MUX(2); return 0x04; //CS2
  408. }
  409. return 0;
  410. }
  411. // SPI2 Class
  412. SPI2Class SPI2;
  413. uint8_t SPI2Class::interruptMasksUsed = 0;
  414. uint32_t SPI2Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  415. uint32_t SPI2Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  416. #ifdef SPI_TRANSACTION_MISMATCH_LED
  417. uint8_t SPI2Class::inTransactionFlag = 0;
  418. #endif
  419. void SPI2Class::begin()
  420. {
  421. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  422. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  423. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  424. SPI2_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  425. SPI2_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  426. SPCR2.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  427. }
  428. void SPI2Class::end() {
  429. SPCR2.disable_pins();
  430. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  431. }
  432. void SPI2Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  433. {
  434. uint32_t n = (uint32_t)interruptName;
  435. if (n >= NVIC_NUM_INTERRUPTS) return;
  436. //Serial.print("usingInterrupt ");
  437. //Serial.println(n);
  438. interruptMasksUsed |= (1 << (n >> 5));
  439. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  440. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  441. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  442. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  443. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  444. }
  445. void SPI2Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  446. {
  447. uint32_t n = (uint32_t)interruptName;
  448. if (n >= NVIC_NUM_INTERRUPTS) return;
  449. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  450. if (interruptMask[n >> 5] == 0) {
  451. interruptMasksUsed &= ~(1 << (n >> 5));
  452. }
  453. }
  454. static void updateCTAR2(uint32_t ctar)
  455. {
  456. if (SPI2_CTAR0 != ctar) {
  457. uint32_t mcr = SPI2_MCR;
  458. if (mcr & SPI_MCR_MDIS) {
  459. SPI2_CTAR0 = ctar;
  460. SPI2_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  461. } else {
  462. SPI2_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  463. SPI2_CTAR0 = ctar;
  464. SPI2_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  465. SPI2_MCR = mcr;
  466. }
  467. }
  468. }
  469. void SPI2Class::setBitOrder(uint8_t bitOrder)
  470. {
  471. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  472. uint32_t ctar = SPI2_CTAR0;
  473. if (bitOrder == LSBFIRST) {
  474. ctar |= SPI_CTAR_LSBFE;
  475. } else {
  476. ctar &= ~SPI_CTAR_LSBFE;
  477. }
  478. updateCTAR2(ctar);
  479. }
  480. void SPI2Class::setDataMode(uint8_t dataMode)
  481. {
  482. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  483. // TODO: implement with native code
  484. SPCR2 = (SPCR2 & ~SPI_MODE_MASK) | dataMode;
  485. }
  486. void SPI2Class::setClockDivider_noInline(uint32_t clk)
  487. {
  488. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  489. uint32_t ctar = SPI2_CTAR0;
  490. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  491. if (ctar & SPI_CTAR_CPHA) {
  492. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  493. }
  494. ctar |= clk;
  495. updateCTAR2(ctar);
  496. }
  497. uint8_t SPI2Class::pinIsChipSelect(uint8_t pin)
  498. {
  499. switch (pin) {
  500. case 43: return 0x01; // CS0
  501. case 54: return 0x02; // CS1
  502. case 55: return 0x01; // CS0
  503. }
  504. return 0;
  505. }
  506. bool SPI2Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  507. {
  508. uint8_t pin1_mask, pin2_mask;
  509. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  510. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  511. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  512. if ((pin1_mask & pin2_mask) != 0) return false;
  513. return true;
  514. }
  515. // setCS() is not intended for use from normal Arduino programs/sketches.
  516. uint8_t SPI2Class::setCS(uint8_t pin)
  517. {
  518. switch (pin) {
  519. case 43: CORE_PIN43_CONFIG = PORT_PCR_MUX(2); return 0x01; // CS0
  520. case 54: CORE_PIN54_CONFIG = PORT_PCR_MUX(2); return 0x02; // CS1
  521. case 55: CORE_PIN55_CONFIG = PORT_PCR_MUX(2); return 0x01; // CS0
  522. }
  523. return 0;
  524. }
  525. #endif
  526. /**********************************************************/
  527. /* 32 bit Teensy-LC */
  528. /**********************************************************/
  529. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  530. SPIClass SPI;
  531. SPI1Class SPI1;
  532. uint32_t SPIClass::interruptMask = 0;
  533. uint32_t SPIClass::interruptSave = 0;
  534. uint32_t SPI1Class::interruptMask = 0;
  535. uint32_t SPI1Class::interruptSave = 0;
  536. #ifdef SPI_TRANSACTION_MISMATCH_LED
  537. uint8_t SPIClass::inTransactionFlag = 0;
  538. uint8_t SPI1Class::inTransactionFlag = 0;
  539. #endif
  540. void SPIClass::begin()
  541. {
  542. SIM_SCGC4 |= SIM_SCGC4_SPI0;
  543. SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  544. SPI0_C2 = 0;
  545. uint8_t tmp __attribute__((unused)) = SPI0_S;
  546. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  547. }
  548. void SPIClass::end() {
  549. SPCR.disable_pins();
  550. SPI0_C1 = 0;
  551. }
  552. const uint16_t SPISettings::br_div_table[30] = {
  553. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24,
  554. 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
  555. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768,
  556. };
  557. const uint8_t SPISettings::br_clock_table[30] = {
  558. SPI_BR_SPPR(0) | SPI_BR_SPR(0),
  559. SPI_BR_SPPR(1) | SPI_BR_SPR(0),
  560. SPI_BR_SPPR(2) | SPI_BR_SPR(0),
  561. SPI_BR_SPPR(3) | SPI_BR_SPR(0),
  562. SPI_BR_SPPR(4) | SPI_BR_SPR(0),
  563. SPI_BR_SPPR(5) | SPI_BR_SPR(0),
  564. SPI_BR_SPPR(6) | SPI_BR_SPR(0),
  565. SPI_BR_SPPR(7) | SPI_BR_SPR(0),
  566. SPI_BR_SPPR(4) | SPI_BR_SPR(1),
  567. SPI_BR_SPPR(5) | SPI_BR_SPR(1),
  568. SPI_BR_SPPR(6) | SPI_BR_SPR(1),
  569. SPI_BR_SPPR(7) | SPI_BR_SPR(1),
  570. SPI_BR_SPPR(4) | SPI_BR_SPR(2),
  571. SPI_BR_SPPR(5) | SPI_BR_SPR(2),
  572. SPI_BR_SPPR(6) | SPI_BR_SPR(2),
  573. SPI_BR_SPPR(7) | SPI_BR_SPR(2),
  574. SPI_BR_SPPR(4) | SPI_BR_SPR(3),
  575. SPI_BR_SPPR(5) | SPI_BR_SPR(3),
  576. SPI_BR_SPPR(6) | SPI_BR_SPR(3),
  577. SPI_BR_SPPR(7) | SPI_BR_SPR(3),
  578. SPI_BR_SPPR(4) | SPI_BR_SPR(4),
  579. SPI_BR_SPPR(5) | SPI_BR_SPR(4),
  580. SPI_BR_SPPR(6) | SPI_BR_SPR(4),
  581. SPI_BR_SPPR(7) | SPI_BR_SPR(4),
  582. SPI_BR_SPPR(4) | SPI_BR_SPR(5),
  583. SPI_BR_SPPR(5) | SPI_BR_SPR(5),
  584. SPI_BR_SPPR(6) | SPI_BR_SPR(5),
  585. SPI_BR_SPPR(7) | SPI_BR_SPR(5),
  586. SPI_BR_SPPR(4) | SPI_BR_SPR(6),
  587. SPI_BR_SPPR(5) | SPI_BR_SPR(6)
  588. };
  589. // setCS() is not intended for use from normal Arduino programs/sketches.
  590. uint8_t SPIClass::setCS(uint8_t pin)
  591. {
  592. switch (pin) {
  593. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  594. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  595. }
  596. return 0;
  597. }
  598. void SPI1Class::begin()
  599. {
  600. SIM_SCGC4 |= SIM_SCGC4_SPI1;
  601. SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  602. SPI1_C2 = 0;
  603. uint8_t tmp __attribute__((unused)) = SPI1_S;
  604. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  605. }
  606. void SPI1Class::end() {
  607. SPCR1.disable_pins();
  608. SPI1_C1 = 0;
  609. }
  610. // setCS() is not intended for use from normal Arduino programs/sketches.
  611. uint8_t SPI1Class::setCS(uint8_t pin)
  612. {
  613. switch (pin) {
  614. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
  615. }
  616. return 0;
  617. }
  618. /**********************************************************/
  619. /* 32 bit Arduino Due */
  620. /**********************************************************/
  621. #elif defined(__arm__) && defined(__SAM3X8E__)
  622. #include "SPI.h"
  623. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  624. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  625. {
  626. // Empty
  627. }
  628. void SPIClass::begin() {
  629. init();
  630. // NPCS control is left to the user
  631. // Default speed set to 4Mhz
  632. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  633. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  634. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  635. }
  636. void SPIClass::begin(uint8_t _pin) {
  637. init();
  638. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  639. PIO_Configure(
  640. g_APinDescription[spiPin].pPort,
  641. g_APinDescription[spiPin].ulPinType,
  642. g_APinDescription[spiPin].ulPin,
  643. g_APinDescription[spiPin].ulPinConfiguration);
  644. // Default speed set to 4Mhz
  645. setClockDivider(_pin, 21);
  646. setDataMode(_pin, SPI_MODE0);
  647. setBitOrder(_pin, MSBFIRST);
  648. }
  649. void SPIClass::init() {
  650. if (initialized)
  651. return;
  652. interruptMode = 0;
  653. interruptMask = 0;
  654. interruptSave = 0;
  655. initCb();
  656. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  657. SPI_Enable(spi);
  658. initialized = true;
  659. }
  660. #ifndef interruptsStatus
  661. #define interruptsStatus() __interruptsStatus()
  662. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  663. static inline unsigned char __interruptsStatus(void) {
  664. unsigned int primask;
  665. asm volatile ("mrs %0, primask" : "=r" (primask));
  666. if (primask) return 0;
  667. return 1;
  668. }
  669. #endif
  670. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  671. {
  672. uint8_t irestore;
  673. irestore = interruptsStatus();
  674. noInterrupts();
  675. if (interruptMode < 2) {
  676. if (interruptNumber > NUM_DIGITAL_PINS) {
  677. interruptMode = 2;
  678. } else {
  679. uint8_t imask = interruptMask;
  680. Pio *pio = g_APinDescription[interruptNumber].pPort;
  681. if (pio == PIOA) {
  682. imask |= 1;
  683. } else if (pio == PIOB) {
  684. imask |= 2;
  685. } else if (pio == PIOC) {
  686. imask |= 4;
  687. } else if (pio == PIOD) {
  688. imask |= 8;
  689. }
  690. interruptMask = imask;
  691. interruptMode = 1;
  692. }
  693. }
  694. if (irestore) interrupts();
  695. }
  696. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  697. {
  698. if (interruptMode > 0) {
  699. if (interruptMode == 1) {
  700. uint8_t imask = interruptMask;
  701. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  702. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  703. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  704. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  705. } else {
  706. interruptSave = interruptsStatus();
  707. noInterrupts();
  708. }
  709. }
  710. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  711. bitOrder[ch] = settings.border;
  712. SPI_ConfigureNPCS(spi, ch, settings.config);
  713. }
  714. void SPIClass::endTransaction(void)
  715. {
  716. if (interruptMode > 0) {
  717. if (interruptMode == 1) {
  718. uint8_t imask = interruptMask;
  719. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  720. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  721. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  722. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  723. } else {
  724. if (interruptSave) interrupts();
  725. }
  726. }
  727. }
  728. void SPIClass::end(uint8_t _pin) {
  729. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  730. // Setting the pin as INPUT will disconnect it from SPI peripheral
  731. pinMode(spiPin, INPUT);
  732. }
  733. void SPIClass::end() {
  734. SPI_Disable(spi);
  735. initialized = false;
  736. }
  737. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  738. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  739. bitOrder[ch] = _bitOrder;
  740. }
  741. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  742. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  743. mode[ch] = _mode | SPI_CSR_CSAAT;
  744. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  745. // transfer. Some device needs that for working properly.
  746. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  747. }
  748. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  749. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  750. divider[ch] = _divider;
  751. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  752. // transfer. Some device needs that for working properly.
  753. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  754. }
  755. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  756. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  757. // Reverse bit order
  758. if (bitOrder[ch] == LSBFIRST)
  759. _data = __REV(__RBIT(_data));
  760. uint32_t d = _data | SPI_PCS(ch);
  761. if (_mode == SPI_LAST)
  762. d |= SPI_TDR_LASTXFER;
  763. // SPI_Write(spi, _channel, _data);
  764. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  765. ;
  766. spi->SPI_TDR = d;
  767. // return SPI_Read(spi);
  768. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  769. ;
  770. d = spi->SPI_RDR;
  771. // Reverse bit order
  772. if (bitOrder[ch] == LSBFIRST)
  773. d = __REV(__RBIT(d));
  774. return d & 0xFF;
  775. }
  776. void SPIClass::attachInterrupt(void) {
  777. // Should be enableInterrupt()
  778. }
  779. void SPIClass::detachInterrupt(void) {
  780. // Should be disableInterrupt()
  781. }
  782. #if SPI_INTERFACES_COUNT > 0
  783. static void SPI_0_Init(void) {
  784. PIO_Configure(
  785. g_APinDescription[PIN_SPI_MOSI].pPort,
  786. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  787. g_APinDescription[PIN_SPI_MOSI].ulPin,
  788. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  789. PIO_Configure(
  790. g_APinDescription[PIN_SPI_MISO].pPort,
  791. g_APinDescription[PIN_SPI_MISO].ulPinType,
  792. g_APinDescription[PIN_SPI_MISO].ulPin,
  793. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  794. PIO_Configure(
  795. g_APinDescription[PIN_SPI_SCK].pPort,
  796. g_APinDescription[PIN_SPI_SCK].ulPinType,
  797. g_APinDescription[PIN_SPI_SCK].ulPin,
  798. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  799. }
  800. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  801. #endif
  802. #endif