Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

927 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. #endif
  257. }
  258. return 0;
  259. }
  260. bool SPIClass::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  261. {
  262. uint8_t pin1_mask, pin2_mask;
  263. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  264. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  265. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  266. if ((pin1_mask & pin2_mask) != 0) return false;
  267. return true;
  268. }
  269. uint8_t SPIClass::setCS(uint8_t pin)
  270. {
  271. switch (pin) {
  272. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  273. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  274. case 9: CORE_PIN9_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTC3
  275. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTD4
  276. case 20: CORE_PIN20_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTD5
  277. case 23: CORE_PIN23_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTC2
  278. case 21: CORE_PIN21_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTD6
  279. case 22: CORE_PIN22_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTC1
  280. case 15: CORE_PIN15_CONFIG = PORT_PCR_MUX(2); return 0x10; // PTC0
  281. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  282. case 26: CORE_PIN26_CONFIG = PORT_PCR_MUX(2);return 0x01;
  283. #endif
  284. }
  285. return 0;
  286. }
  287. /**********************************************************/
  288. /* 32 bit Teensy-3.5/3.6 */
  289. /**********************************************************/
  290. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  291. SPI1Class SPI1;
  292. uint8_t SPI1Class::interruptMasksUsed = 0;
  293. uint32_t SPI1Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  294. uint32_t SPI1Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  295. #ifdef SPI_TRANSACTION_MISMATCH_LED
  296. uint8_t SPI1Class::inTransactionFlag = 0;
  297. #endif
  298. void SPI1Class::begin()
  299. {
  300. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  301. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  302. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  303. SPI1_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  304. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  305. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  306. }
  307. void SPI1Class::end() {
  308. SPCR1.disable_pins();
  309. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  310. }
  311. void SPI1Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  312. {
  313. uint32_t n = (uint32_t)interruptName;
  314. if (n >= NVIC_NUM_INTERRUPTS) return;
  315. //Serial.print("usingInterrupt ");
  316. //Serial.println(n);
  317. interruptMasksUsed |= (1 << (n >> 5));
  318. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  319. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  320. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  321. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  322. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  323. }
  324. void SPI1Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  325. {
  326. uint32_t n = (uint32_t)interruptName;
  327. if (n >= NVIC_NUM_INTERRUPTS) return;
  328. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  329. if (interruptMask[n >> 5] == 0) {
  330. interruptMasksUsed &= ~(1 << (n >> 5));
  331. }
  332. }
  333. static void updateCTAR1(uint32_t ctar)
  334. {
  335. if (SPI1_CTAR0 != ctar) {
  336. uint32_t mcr = SPI1_MCR;
  337. if (mcr & SPI_MCR_MDIS) {
  338. SPI1_CTAR0 = ctar;
  339. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  340. } else {
  341. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  342. SPI1_CTAR0 = ctar;
  343. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  344. SPI1_MCR = mcr;
  345. }
  346. }
  347. }
  348. void SPI1Class::setBitOrder(uint8_t bitOrder)
  349. {
  350. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  351. uint32_t ctar = SPI1_CTAR0;
  352. if (bitOrder == LSBFIRST) {
  353. ctar |= SPI_CTAR_LSBFE;
  354. } else {
  355. ctar &= ~SPI_CTAR_LSBFE;
  356. }
  357. updateCTAR1(ctar);
  358. }
  359. void SPI1Class::setDataMode(uint8_t dataMode)
  360. {
  361. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  362. // TODO: implement with native code
  363. SPCR1 = (SPCR1 & ~SPI_MODE_MASK) | dataMode;
  364. }
  365. void SPI1Class::setClockDivider_noInline(uint32_t clk)
  366. {
  367. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  368. uint32_t ctar = SPI1_CTAR0;
  369. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  370. if (ctar & SPI_CTAR_CPHA) {
  371. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  372. }
  373. ctar |= clk;
  374. updateCTAR1(ctar);
  375. }
  376. uint8_t SPI1Class::pinIsChipSelect(uint8_t pin)
  377. {
  378. switch (pin) {
  379. case 6: return 0x01; // CS0
  380. case 31: return 0x01; // CS0
  381. #ifdef USE_SDCARD_PINS
  382. case 58: return 0x02; //CS1
  383. case 62: return 0x01; //CS0
  384. case 63: return 0x04; //CS2
  385. #endif
  386. }
  387. return 0;
  388. }
  389. bool SPI1Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  390. {
  391. uint8_t pin1_mask, pin2_mask;
  392. if ((pin1_mask = (uint8_t)pinIsChipSelect(pin1)) == 0) return false;
  393. if ((pin2_mask = (uint8_t)pinIsChipSelect(pin2)) == 0) return false;
  394. //Serial.printf("pinIsChipSelect %d %d %x %x\n\r", pin1, pin2, pin1_mask, pin2_mask);
  395. if ((pin1_mask & pin2_mask) != 0) return false;
  396. return true;
  397. }
  398. uint8_t SPI1Class::setCS(uint8_t pin)
  399. {
  400. switch (pin) {
  401. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(7); return 0x01; // PTD4
  402. case 31: CORE_PIN31_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD5
  403. #ifdef USE_SDCARD_PINS
  404. case 58: CORE_PIN58_CONFIG = PORT_PCR_MUX(2); return 0x02; //CS1
  405. case 62: CORE_PIN62_CONFIG = PORT_PCR_MUX(2); return 0x01; //CS0
  406. case 63: CORE_PIN63_CONFIG = PORT_PCR_MUX(2); return 0x04; //CS2
  407. #endif
  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. uint8_t SPI2Class::setCS(uint8_t pin)
  516. {
  517. switch (pin) {
  518. case 43: CORE_PIN43_CONFIG = PORT_PCR_MUX(2); return 0x01; // CS0
  519. case 54: CORE_PIN54_CONFIG = PORT_PCR_MUX(2); return 0x02; // CS1
  520. case 55: CORE_PIN55_CONFIG = PORT_PCR_MUX(2); return 0x01; // CS0
  521. }
  522. return 0;
  523. }
  524. #endif
  525. /**********************************************************/
  526. /* 32 bit Teensy-LC */
  527. /**********************************************************/
  528. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  529. SPIClass SPI;
  530. SPI1Class SPI1;
  531. uint32_t SPIClass::interruptMask = 0;
  532. uint32_t SPIClass::interruptSave = 0;
  533. uint32_t SPI1Class::interruptMask = 0;
  534. uint32_t SPI1Class::interruptSave = 0;
  535. #ifdef SPI_TRANSACTION_MISMATCH_LED
  536. uint8_t SPIClass::inTransactionFlag = 0;
  537. uint8_t SPI1Class::inTransactionFlag = 0;
  538. #endif
  539. void SPIClass::begin()
  540. {
  541. SIM_SCGC4 |= SIM_SCGC4_SPI0;
  542. SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  543. SPI0_C2 = 0;
  544. uint8_t tmp __attribute__((unused)) = SPI0_S;
  545. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  546. }
  547. void SPIClass::end() {
  548. SPCR.disable_pins();
  549. SPI0_C1 = 0;
  550. }
  551. const uint16_t SPISettings::br_div_table[30] = {
  552. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24,
  553. 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
  554. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768,
  555. };
  556. const uint8_t SPISettings::br_clock_table[30] = {
  557. SPI_BR_SPPR(0) | SPI_BR_SPR(0),
  558. SPI_BR_SPPR(1) | SPI_BR_SPR(0),
  559. SPI_BR_SPPR(2) | SPI_BR_SPR(0),
  560. SPI_BR_SPPR(3) | SPI_BR_SPR(0),
  561. SPI_BR_SPPR(4) | SPI_BR_SPR(0),
  562. SPI_BR_SPPR(5) | SPI_BR_SPR(0),
  563. SPI_BR_SPPR(6) | SPI_BR_SPR(0),
  564. SPI_BR_SPPR(7) | SPI_BR_SPR(0),
  565. SPI_BR_SPPR(4) | SPI_BR_SPR(1),
  566. SPI_BR_SPPR(5) | SPI_BR_SPR(1),
  567. SPI_BR_SPPR(6) | SPI_BR_SPR(1),
  568. SPI_BR_SPPR(7) | SPI_BR_SPR(1),
  569. SPI_BR_SPPR(4) | SPI_BR_SPR(2),
  570. SPI_BR_SPPR(5) | SPI_BR_SPR(2),
  571. SPI_BR_SPPR(6) | SPI_BR_SPR(2),
  572. SPI_BR_SPPR(7) | SPI_BR_SPR(2),
  573. SPI_BR_SPPR(4) | SPI_BR_SPR(3),
  574. SPI_BR_SPPR(5) | SPI_BR_SPR(3),
  575. SPI_BR_SPPR(6) | SPI_BR_SPR(3),
  576. SPI_BR_SPPR(7) | SPI_BR_SPR(3),
  577. SPI_BR_SPPR(4) | SPI_BR_SPR(4),
  578. SPI_BR_SPPR(5) | SPI_BR_SPR(4),
  579. SPI_BR_SPPR(6) | SPI_BR_SPR(4),
  580. SPI_BR_SPPR(7) | SPI_BR_SPR(4),
  581. SPI_BR_SPPR(4) | SPI_BR_SPR(5),
  582. SPI_BR_SPPR(5) | SPI_BR_SPR(5),
  583. SPI_BR_SPPR(6) | SPI_BR_SPR(5),
  584. SPI_BR_SPPR(7) | SPI_BR_SPR(5),
  585. SPI_BR_SPPR(4) | SPI_BR_SPR(6),
  586. SPI_BR_SPPR(5) | SPI_BR_SPR(6)
  587. };
  588. uint8_t SPIClass::setCS(uint8_t pin)
  589. {
  590. switch (pin) {
  591. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  592. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  593. }
  594. return 0;
  595. }
  596. void SPI1Class::begin()
  597. {
  598. SIM_SCGC4 |= SIM_SCGC4_SPI1;
  599. SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  600. SPI1_C2 = 0;
  601. uint8_t tmp __attribute__((unused)) = SPI1_S;
  602. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  603. }
  604. void SPI1Class::end() {
  605. SPCR1.disable_pins();
  606. SPI1_C1 = 0;
  607. }
  608. uint8_t SPI1Class::setCS(uint8_t pin)
  609. {
  610. switch (pin) {
  611. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
  612. }
  613. return 0;
  614. }
  615. /**********************************************************/
  616. /* 32 bit Arduino Due */
  617. /**********************************************************/
  618. #elif defined(__arm__) && defined(__SAM3X8E__)
  619. #include "SPI.h"
  620. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  621. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  622. {
  623. // Empty
  624. }
  625. void SPIClass::begin() {
  626. init();
  627. // NPCS control is left to the user
  628. // Default speed set to 4Mhz
  629. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  630. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  631. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  632. }
  633. void SPIClass::begin(uint8_t _pin) {
  634. init();
  635. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  636. PIO_Configure(
  637. g_APinDescription[spiPin].pPort,
  638. g_APinDescription[spiPin].ulPinType,
  639. g_APinDescription[spiPin].ulPin,
  640. g_APinDescription[spiPin].ulPinConfiguration);
  641. // Default speed set to 4Mhz
  642. setClockDivider(_pin, 21);
  643. setDataMode(_pin, SPI_MODE0);
  644. setBitOrder(_pin, MSBFIRST);
  645. }
  646. void SPIClass::init() {
  647. if (initialized)
  648. return;
  649. interruptMode = 0;
  650. interruptMask = 0;
  651. interruptSave = 0;
  652. initCb();
  653. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  654. SPI_Enable(spi);
  655. initialized = true;
  656. }
  657. #ifndef interruptsStatus
  658. #define interruptsStatus() __interruptsStatus()
  659. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  660. static inline unsigned char __interruptsStatus(void) {
  661. unsigned int primask;
  662. asm volatile ("mrs %0, primask" : "=r" (primask));
  663. if (primask) return 0;
  664. return 1;
  665. }
  666. #endif
  667. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  668. {
  669. uint8_t irestore;
  670. irestore = interruptsStatus();
  671. noInterrupts();
  672. if (interruptMode < 2) {
  673. if (interruptNumber > NUM_DIGITAL_PINS) {
  674. interruptMode = 2;
  675. } else {
  676. uint8_t imask = interruptMask;
  677. Pio *pio = g_APinDescription[interruptNumber].pPort;
  678. if (pio == PIOA) {
  679. imask |= 1;
  680. } else if (pio == PIOB) {
  681. imask |= 2;
  682. } else if (pio == PIOC) {
  683. imask |= 4;
  684. } else if (pio == PIOD) {
  685. imask |= 8;
  686. }
  687. interruptMask = imask;
  688. interruptMode = 1;
  689. }
  690. }
  691. if (irestore) interrupts();
  692. }
  693. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  694. {
  695. if (interruptMode > 0) {
  696. if (interruptMode == 1) {
  697. uint8_t imask = interruptMask;
  698. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  699. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  700. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  701. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  702. } else {
  703. interruptSave = interruptsStatus();
  704. noInterrupts();
  705. }
  706. }
  707. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  708. bitOrder[ch] = settings.border;
  709. SPI_ConfigureNPCS(spi, ch, settings.config);
  710. }
  711. void SPIClass::endTransaction(void)
  712. {
  713. if (interruptMode > 0) {
  714. if (interruptMode == 1) {
  715. uint8_t imask = interruptMask;
  716. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  717. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  718. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  719. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  720. } else {
  721. if (interruptSave) interrupts();
  722. }
  723. }
  724. }
  725. void SPIClass::end(uint8_t _pin) {
  726. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  727. // Setting the pin as INPUT will disconnect it from SPI peripheral
  728. pinMode(spiPin, INPUT);
  729. }
  730. void SPIClass::end() {
  731. SPI_Disable(spi);
  732. initialized = false;
  733. }
  734. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  735. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  736. bitOrder[ch] = _bitOrder;
  737. }
  738. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  739. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  740. mode[ch] = _mode | SPI_CSR_CSAAT;
  741. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  742. // transfer. Some device needs that for working properly.
  743. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  744. }
  745. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  746. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  747. divider[ch] = _divider;
  748. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  749. // transfer. Some device needs that for working properly.
  750. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  751. }
  752. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  753. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  754. // Reverse bit order
  755. if (bitOrder[ch] == LSBFIRST)
  756. _data = __REV(__RBIT(_data));
  757. uint32_t d = _data | SPI_PCS(ch);
  758. if (_mode == SPI_LAST)
  759. d |= SPI_TDR_LASTXFER;
  760. // SPI_Write(spi, _channel, _data);
  761. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  762. ;
  763. spi->SPI_TDR = d;
  764. // return SPI_Read(spi);
  765. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  766. ;
  767. d = spi->SPI_RDR;
  768. // Reverse bit order
  769. if (bitOrder[ch] == LSBFIRST)
  770. d = __REV(__RBIT(d));
  771. return d & 0xFF;
  772. }
  773. void SPIClass::attachInterrupt(void) {
  774. // Should be enableInterrupt()
  775. }
  776. void SPIClass::detachInterrupt(void) {
  777. // Should be disableInterrupt()
  778. }
  779. #if SPI_INTERFACES_COUNT > 0
  780. static void SPI_0_Init(void) {
  781. PIO_Configure(
  782. g_APinDescription[PIN_SPI_MOSI].pPort,
  783. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  784. g_APinDescription[PIN_SPI_MOSI].ulPin,
  785. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  786. PIO_Configure(
  787. g_APinDescription[PIN_SPI_MISO].pPort,
  788. g_APinDescription[PIN_SPI_MISO].ulPinType,
  789. g_APinDescription[PIN_SPI_MISO].ulPin,
  790. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  791. PIO_Configure(
  792. g_APinDescription[PIN_SPI_SCK].pPort,
  793. g_APinDescription[PIN_SPI_SCK].ulPinType,
  794. g_APinDescription[PIN_SPI_SCK].ulPin,
  795. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  796. }
  797. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  798. #endif
  799. #endif