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.

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