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.

774 lines
21KB

  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. }
  282. return 0;
  283. }
  284. /**********************************************************/
  285. /* 32 bit Teensy-3.4/3.5 */
  286. /**********************************************************/
  287. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  288. SPI1Class SPI1;
  289. uint8_t SPI1Class::interruptMasksUsed = 0;
  290. uint32_t SPI1Class::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  291. uint32_t SPI1Class::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  292. #ifdef SPI_TRANSACTION_MISMATCH_LED
  293. uint8_t SPI1Class::inTransactionFlag = 0;
  294. #endif
  295. void SPI1Class::begin()
  296. {
  297. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  298. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  299. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  300. SPI1_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  301. SPI1_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  302. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  303. }
  304. void SPI1Class::end() {
  305. SPCR1.disable_pins();
  306. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  307. }
  308. void SPI1Class::usingInterrupt(IRQ_NUMBER_t interruptName)
  309. {
  310. uint32_t n = (uint32_t)interruptName;
  311. if (n >= NVIC_NUM_INTERRUPTS) return;
  312. //Serial.print("usingInterrupt ");
  313. //Serial.println(n);
  314. interruptMasksUsed |= (1 << (n >> 5));
  315. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  316. //Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  317. //Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  318. //Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  319. //Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  320. }
  321. void SPI1Class::notUsingInterrupt(IRQ_NUMBER_t interruptName)
  322. {
  323. uint32_t n = (uint32_t)interruptName;
  324. if (n >= NVIC_NUM_INTERRUPTS) return;
  325. interruptMask[n >> 5] &= ~(1 << (n & 0x1F));
  326. if (interruptMask[n >> 5] == 0) {
  327. interruptMasksUsed &= ~(1 << (n >> 5));
  328. }
  329. }
  330. static void updateCTAR1(uint32_t ctar)
  331. {
  332. if (SPI1_CTAR0 != ctar) {
  333. uint32_t mcr = SPI1_MCR;
  334. if (mcr & SPI_MCR_MDIS) {
  335. SPI1_CTAR0 = ctar;
  336. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  337. } else {
  338. SPI1_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  339. SPI1_CTAR0 = ctar;
  340. SPI1_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  341. SPI1_MCR = mcr;
  342. }
  343. }
  344. }
  345. void SPI1Class::setBitOrder(uint8_t bitOrder)
  346. {
  347. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  348. uint32_t ctar = SPI1_CTAR0;
  349. if (bitOrder == LSBFIRST) {
  350. ctar |= SPI_CTAR_LSBFE;
  351. } else {
  352. ctar &= ~SPI_CTAR_LSBFE;
  353. }
  354. updateCTAR1(ctar);
  355. }
  356. void SPI1Class::setDataMode(uint8_t dataMode)
  357. {
  358. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  359. // TODO: implement with native code
  360. SPCR1 = (SPCR1 & ~SPI_MODE_MASK) | dataMode;
  361. }
  362. void SPI1Class::setClockDivider_noInline(uint32_t clk)
  363. {
  364. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  365. uint32_t ctar = SPI1_CTAR0;
  366. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  367. if (ctar & SPI_CTAR_CPHA) {
  368. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  369. }
  370. ctar |= clk;
  371. updateCTAR1(ctar);
  372. }
  373. bool SPI1Class::pinIsChipSelect(uint8_t pin)
  374. {
  375. if (pin == 6 || pin == 31) return true;
  376. return false;
  377. }
  378. bool SPI1Class::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  379. {
  380. return false; // only one CS bith 6 and 31 or logially the same.
  381. }
  382. uint8_t SPI1Class::setCS(uint8_t pin)
  383. {
  384. switch (pin) {
  385. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(7); return 0x01; // PTD4
  386. case 31: CORE_PIN31_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD5
  387. }
  388. return 0;
  389. }
  390. #endif
  391. /**********************************************************/
  392. /* 32 bit Teensy-LC */
  393. /**********************************************************/
  394. #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
  395. SPIClass SPI;
  396. SPI1Class SPI1;
  397. uint32_t SPIClass::interruptMask = 0;
  398. uint32_t SPIClass::interruptSave = 0;
  399. uint32_t SPI1Class::interruptMask = 0;
  400. uint32_t SPI1Class::interruptSave = 0;
  401. #ifdef SPI_TRANSACTION_MISMATCH_LED
  402. uint8_t SPIClass::inTransactionFlag = 0;
  403. uint8_t SPI1Class::inTransactionFlag = 0;
  404. #endif
  405. void SPIClass::begin()
  406. {
  407. SIM_SCGC4 |= SIM_SCGC4_SPI0;
  408. SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  409. SPI0_C2 = 0;
  410. uint8_t tmp __attribute__((unused)) = SPI0_S;
  411. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  412. }
  413. void SPIClass::end() {
  414. SPCR.disable_pins();
  415. SPI0_C1 = 0;
  416. }
  417. const uint16_t SPISettings::br_div_table[30] = {
  418. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24,
  419. 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
  420. 160, 192, 224, 256, 320, 384, 448, 512, 640, 768,
  421. };
  422. const uint8_t SPISettings::br_clock_table[30] = {
  423. SPI_BR_SPPR(0) | SPI_BR_SPR(0),
  424. SPI_BR_SPPR(1) | SPI_BR_SPR(0),
  425. SPI_BR_SPPR(2) | SPI_BR_SPR(0),
  426. SPI_BR_SPPR(3) | SPI_BR_SPR(0),
  427. SPI_BR_SPPR(4) | SPI_BR_SPR(0),
  428. SPI_BR_SPPR(5) | SPI_BR_SPR(0),
  429. SPI_BR_SPPR(6) | SPI_BR_SPR(0),
  430. SPI_BR_SPPR(7) | SPI_BR_SPR(0),
  431. SPI_BR_SPPR(4) | SPI_BR_SPR(1),
  432. SPI_BR_SPPR(5) | SPI_BR_SPR(1),
  433. SPI_BR_SPPR(6) | SPI_BR_SPR(1),
  434. SPI_BR_SPPR(7) | SPI_BR_SPR(1),
  435. SPI_BR_SPPR(4) | SPI_BR_SPR(2),
  436. SPI_BR_SPPR(5) | SPI_BR_SPR(2),
  437. SPI_BR_SPPR(6) | SPI_BR_SPR(2),
  438. SPI_BR_SPPR(7) | SPI_BR_SPR(2),
  439. SPI_BR_SPPR(4) | SPI_BR_SPR(3),
  440. SPI_BR_SPPR(5) | SPI_BR_SPR(3),
  441. SPI_BR_SPPR(6) | SPI_BR_SPR(3),
  442. SPI_BR_SPPR(7) | SPI_BR_SPR(3),
  443. SPI_BR_SPPR(4) | SPI_BR_SPR(4),
  444. SPI_BR_SPPR(5) | SPI_BR_SPR(4),
  445. SPI_BR_SPPR(6) | SPI_BR_SPR(4),
  446. SPI_BR_SPPR(7) | SPI_BR_SPR(4),
  447. SPI_BR_SPPR(4) | SPI_BR_SPR(5),
  448. SPI_BR_SPPR(5) | SPI_BR_SPR(5),
  449. SPI_BR_SPPR(6) | SPI_BR_SPR(5),
  450. SPI_BR_SPPR(7) | SPI_BR_SPR(5),
  451. SPI_BR_SPPR(4) | SPI_BR_SPR(6),
  452. SPI_BR_SPPR(5) | SPI_BR_SPR(6)
  453. };
  454. uint8_t SPIClass::setCS(uint8_t pin)
  455. {
  456. switch (pin) {
  457. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  458. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  459. }
  460. return 0;
  461. }
  462. void SPI1Class::begin()
  463. {
  464. SIM_SCGC4 |= SIM_SCGC4_SPI1;
  465. SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
  466. SPI1_C2 = 0;
  467. uint8_t tmp __attribute__((unused)) = SPI1_S;
  468. SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  469. }
  470. void SPI1Class::end() {
  471. SPCR1.disable_pins();
  472. SPI1_C1 = 0;
  473. }
  474. uint8_t SPI1Class::setCS(uint8_t pin)
  475. {
  476. switch (pin) {
  477. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
  478. }
  479. return 0;
  480. }
  481. /**********************************************************/
  482. /* 32 bit Arduino Due */
  483. /**********************************************************/
  484. #elif defined(__arm__) && defined(__SAM3X8E__)
  485. #include "SPI.h"
  486. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  487. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  488. {
  489. // Empty
  490. }
  491. void SPIClass::begin() {
  492. init();
  493. // NPCS control is left to the user
  494. // Default speed set to 4Mhz
  495. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  496. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  497. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  498. }
  499. void SPIClass::begin(uint8_t _pin) {
  500. init();
  501. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  502. PIO_Configure(
  503. g_APinDescription[spiPin].pPort,
  504. g_APinDescription[spiPin].ulPinType,
  505. g_APinDescription[spiPin].ulPin,
  506. g_APinDescription[spiPin].ulPinConfiguration);
  507. // Default speed set to 4Mhz
  508. setClockDivider(_pin, 21);
  509. setDataMode(_pin, SPI_MODE0);
  510. setBitOrder(_pin, MSBFIRST);
  511. }
  512. void SPIClass::init() {
  513. if (initialized)
  514. return;
  515. interruptMode = 0;
  516. interruptMask = 0;
  517. interruptSave = 0;
  518. initCb();
  519. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  520. SPI_Enable(spi);
  521. initialized = true;
  522. }
  523. #ifndef interruptsStatus
  524. #define interruptsStatus() __interruptsStatus()
  525. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  526. static inline unsigned char __interruptsStatus(void) {
  527. unsigned int primask;
  528. asm volatile ("mrs %0, primask" : "=r" (primask));
  529. if (primask) return 0;
  530. return 1;
  531. }
  532. #endif
  533. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  534. {
  535. uint8_t irestore;
  536. irestore = interruptsStatus();
  537. noInterrupts();
  538. if (interruptMode < 2) {
  539. if (interruptNumber > NUM_DIGITAL_PINS) {
  540. interruptMode = 2;
  541. } else {
  542. uint8_t imask = interruptMask;
  543. Pio *pio = g_APinDescription[interruptNumber].pPort;
  544. if (pio == PIOA) {
  545. imask |= 1;
  546. } else if (pio == PIOB) {
  547. imask |= 2;
  548. } else if (pio == PIOC) {
  549. imask |= 4;
  550. } else if (pio == PIOD) {
  551. imask |= 8;
  552. }
  553. interruptMask = imask;
  554. interruptMode = 1;
  555. }
  556. }
  557. if (irestore) interrupts();
  558. }
  559. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  560. {
  561. if (interruptMode > 0) {
  562. if (interruptMode == 1) {
  563. uint8_t imask = interruptMask;
  564. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  565. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  566. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  567. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  568. } else {
  569. interruptSave = interruptsStatus();
  570. noInterrupts();
  571. }
  572. }
  573. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  574. bitOrder[ch] = settings.border;
  575. SPI_ConfigureNPCS(spi, ch, settings.config);
  576. }
  577. void SPIClass::endTransaction(void)
  578. {
  579. if (interruptMode > 0) {
  580. if (interruptMode == 1) {
  581. uint8_t imask = interruptMask;
  582. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  583. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  584. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  585. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  586. } else {
  587. if (interruptSave) interrupts();
  588. }
  589. }
  590. }
  591. void SPIClass::end(uint8_t _pin) {
  592. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  593. // Setting the pin as INPUT will disconnect it from SPI peripheral
  594. pinMode(spiPin, INPUT);
  595. }
  596. void SPIClass::end() {
  597. SPI_Disable(spi);
  598. initialized = false;
  599. }
  600. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  601. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  602. bitOrder[ch] = _bitOrder;
  603. }
  604. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  605. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  606. mode[ch] = _mode | SPI_CSR_CSAAT;
  607. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  608. // transfer. Some device needs that for working properly.
  609. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  610. }
  611. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  612. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  613. divider[ch] = _divider;
  614. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  615. // transfer. Some device needs that for working properly.
  616. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  617. }
  618. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  619. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  620. // Reverse bit order
  621. if (bitOrder[ch] == LSBFIRST)
  622. _data = __REV(__RBIT(_data));
  623. uint32_t d = _data | SPI_PCS(ch);
  624. if (_mode == SPI_LAST)
  625. d |= SPI_TDR_LASTXFER;
  626. // SPI_Write(spi, _channel, _data);
  627. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  628. ;
  629. spi->SPI_TDR = d;
  630. // return SPI_Read(spi);
  631. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  632. ;
  633. d = spi->SPI_RDR;
  634. // Reverse bit order
  635. if (bitOrder[ch] == LSBFIRST)
  636. d = __REV(__RBIT(d));
  637. return d & 0xFF;
  638. }
  639. void SPIClass::attachInterrupt(void) {
  640. // Should be enableInterrupt()
  641. }
  642. void SPIClass::detachInterrupt(void) {
  643. // Should be disableInterrupt()
  644. }
  645. #if SPI_INTERFACES_COUNT > 0
  646. static void SPI_0_Init(void) {
  647. PIO_Configure(
  648. g_APinDescription[PIN_SPI_MOSI].pPort,
  649. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  650. g_APinDescription[PIN_SPI_MOSI].ulPin,
  651. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  652. PIO_Configure(
  653. g_APinDescription[PIN_SPI_MISO].pPort,
  654. g_APinDescription[PIN_SPI_MISO].ulPinType,
  655. g_APinDescription[PIN_SPI_MISO].ulPin,
  656. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  657. PIO_Configure(
  658. g_APinDescription[PIN_SPI_SCK].pPort,
  659. g_APinDescription[PIN_SPI_SCK].ulPinType,
  660. g_APinDescription[PIN_SPI_SCK].ulPin,
  661. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  662. }
  663. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  664. #endif
  665. #endif