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.

519 line
14KB

  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 mask;
  87. if (interruptMode > 1) return;
  88. noInterrupts();
  89. switch (interruptNumber) {
  90. #ifdef SPI_INT0_MASK
  91. case 0: mask = SPI_INT0_MASK; break;
  92. #endif
  93. #ifdef SPI_INT1_MASK
  94. case 1: mask = SPI_INT1_MASK; break;
  95. #endif
  96. #ifdef SPI_INT2_MASK
  97. case 2: mask = SPI_INT2_MASK; break;
  98. #endif
  99. #ifdef SPI_INT3_MASK
  100. case 3: mask = SPI_INT3_MASK; break;
  101. #endif
  102. #ifdef SPI_INT4_MASK
  103. case 4: mask = SPI_INT4_MASK; break;
  104. #endif
  105. #ifdef SPI_INT5_MASK
  106. case 5: mask = SPI_INT5_MASK; break;
  107. #endif
  108. #ifdef SPI_INT6_MASK
  109. case 6: mask = SPI_INT6_MASK; break;
  110. #endif
  111. #ifdef SPI_INT7_MASK
  112. case 7: mask = SPI_INT7_MASK; break;
  113. #endif
  114. default:
  115. interruptMode = 2;
  116. interrupts();
  117. return;
  118. }
  119. interruptMode = 1;
  120. interruptMask |= mask;
  121. interrupts();
  122. }
  123. /**********************************************************/
  124. /* 32 bit Teensy 3.0 and 3.1 */
  125. /**********************************************************/
  126. #elif defined(__arm__) && defined(TEENSYDUINO)
  127. SPIClass SPI;
  128. uint8_t SPIClass::interruptMasksUsed = 0;
  129. uint32_t SPIClass::interruptMask[(NVIC_NUM_INTERRUPTS+31)/32];
  130. uint32_t SPIClass::interruptSave[(NVIC_NUM_INTERRUPTS+31)/32];
  131. #ifdef SPI_TRANSACTION_MISMATCH_LED
  132. uint8_t SPIClass::inTransactionFlag = 0;
  133. #endif
  134. void SPIClass::begin()
  135. {
  136. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  137. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  138. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  139. SPI0_CTAR1 = SPI_CTAR_FMSZ(15) | SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  140. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F);
  141. SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
  142. }
  143. void SPIClass::end() {
  144. SPCR.disable_pins();
  145. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  146. }
  147. void SPIClass::usingInterrupt(IRQ_NUMBER_t interruptName)
  148. {
  149. uint32_t n = (uint32_t)interruptName;
  150. if (n >= NVIC_NUM_INTERRUPTS) return;
  151. Serial.print("usingInterrupt ");
  152. Serial.println(n);
  153. interruptMasksUsed |= (1 << (n >> 5));
  154. interruptMask[n >> 5] |= (1 << (n & 0x1F));
  155. Serial.printf("interruptMasksUsed = %d\n", interruptMasksUsed);
  156. Serial.printf("interruptMask[0] = %08X\n", interruptMask[0]);
  157. Serial.printf("interruptMask[1] = %08X\n", interruptMask[1]);
  158. Serial.printf("interruptMask[2] = %08X\n", interruptMask[2]);
  159. }
  160. const uint16_t SPISettings::ctar_div_table[23] = {
  161. 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40,
  162. 56, 64, 96, 128, 192, 256, 384, 512, 640, 768
  163. };
  164. const uint32_t SPISettings::ctar_clock_table[23] = {
  165. SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  166. SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  167. SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  168. SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR | SPI_CTAR_CSSCK(0),
  169. SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  170. SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1),
  171. SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0),
  172. SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1),
  173. SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  174. SPI_CTAR_PBR(2) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(0),
  175. SPI_CTAR_PBR(1) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  176. SPI_CTAR_PBR(0) | SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(3),
  177. SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  178. SPI_CTAR_PBR(3) | SPI_CTAR_BR(3) | SPI_CTAR_CSSCK(2),
  179. SPI_CTAR_PBR(0) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4),
  180. SPI_CTAR_PBR(1) | SPI_CTAR_BR(5) | SPI_CTAR_CSSCK(4),
  181. SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5),
  182. SPI_CTAR_PBR(1) | SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(5),
  183. SPI_CTAR_PBR(0) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  184. SPI_CTAR_PBR(1) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  185. SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7),
  186. SPI_CTAR_PBR(2) | SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(6),
  187. SPI_CTAR_PBR(1) | SPI_CTAR_BR(8) | SPI_CTAR_CSSCK(7)
  188. };
  189. static void updateCTAR(uint32_t ctar)
  190. {
  191. if (SPI0_CTAR0 != ctar) {
  192. uint32_t mcr = SPI0_MCR;
  193. if (mcr & SPI_MCR_MDIS) {
  194. SPI0_CTAR0 = ctar;
  195. SPI0_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  196. } else {
  197. SPI0_MCR = SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  198. SPI0_CTAR0 = ctar;
  199. SPI0_CTAR1 = ctar | SPI_CTAR_FMSZ(8);
  200. SPI0_MCR = mcr;
  201. }
  202. }
  203. }
  204. void SPIClass::setBitOrder(uint8_t bitOrder)
  205. {
  206. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  207. uint32_t ctar = SPI0_CTAR0;
  208. if (bitOrder == LSBFIRST) {
  209. ctar |= SPI_CTAR_LSBFE;
  210. } else {
  211. ctar &= ~SPI_CTAR_LSBFE;
  212. }
  213. updateCTAR(ctar);
  214. }
  215. void SPIClass::setDataMode(uint8_t dataMode)
  216. {
  217. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  218. // TODO: implement with native code
  219. SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
  220. }
  221. void SPIClass::setClockDivider_noInline(uint32_t clk)
  222. {
  223. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  224. uint32_t ctar = SPI0_CTAR0;
  225. ctar &= (SPI_CTAR_CPOL | SPI_CTAR_CPHA | SPI_CTAR_LSBFE);
  226. if (ctar & SPI_CTAR_CPHA) {
  227. clk = (clk & 0xFFFF0FFF) | ((clk & 0xF000) >> 4);
  228. }
  229. ctar |= clk;
  230. updateCTAR(ctar);
  231. }
  232. bool SPIClass::pinIsChipSelect(uint8_t pin)
  233. {
  234. if (pin == 10 || pin == 9 || pin == 6 || pin == 2 || pin == 15) return true;
  235. if (pin >= 20 && pin <= 23) return true;
  236. return false;
  237. }
  238. bool SPIClass::pinIsChipSelect(uint8_t pin1, uint8_t pin2)
  239. {
  240. if (!pinIsChipSelect(pin1) || !pinIsChipSelect(pin2)) return false;
  241. if ((pin1 == 2 && pin2 == 10) || (pin1 == 10 && pin2 == 2)) return false;
  242. if ((pin1 == 6 && pin2 == 9) || (pin1 == 9 && pin2 == 6)) return false;
  243. if ((pin1 == 20 && pin2 == 23) || (pin1 == 23 && pin2 == 20)) return false;
  244. if ((pin1 == 21 && pin2 == 22) || (pin1 == 22 && pin2 == 21)) return false;
  245. return true;
  246. }
  247. uint8_t SPIClass::setCS(uint8_t pin)
  248. {
  249. switch (pin) {
  250. case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
  251. case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
  252. case 9: CORE_PIN9_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTC3
  253. case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x02; // PTD4
  254. case 20: CORE_PIN20_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTD5
  255. case 23: CORE_PIN23_CONFIG = PORT_PCR_MUX(2); return 0x04; // PTC2
  256. case 21: CORE_PIN21_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTD6
  257. case 22: CORE_PIN22_CONFIG = PORT_PCR_MUX(2); return 0x08; // PTC1
  258. case 15: CORE_PIN15_CONFIG = PORT_PCR_MUX(2); return 0x10; // PTC0
  259. }
  260. return 0;
  261. }
  262. /**********************************************************/
  263. /* 32 bit Arduino Due */
  264. /**********************************************************/
  265. #elif defined(__arm__) && defined(__SAM3X8E__)
  266. #include "SPI.h"
  267. SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
  268. spi(_spi), id(_id), initCb(_initCb), initialized(false)
  269. {
  270. // Empty
  271. }
  272. void SPIClass::begin() {
  273. init();
  274. // NPCS control is left to the user
  275. // Default speed set to 4Mhz
  276. setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
  277. setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
  278. setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
  279. }
  280. void SPIClass::begin(uint8_t _pin) {
  281. init();
  282. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  283. PIO_Configure(
  284. g_APinDescription[spiPin].pPort,
  285. g_APinDescription[spiPin].ulPinType,
  286. g_APinDescription[spiPin].ulPin,
  287. g_APinDescription[spiPin].ulPinConfiguration);
  288. // Default speed set to 4Mhz
  289. setClockDivider(_pin, 21);
  290. setDataMode(_pin, SPI_MODE0);
  291. setBitOrder(_pin, MSBFIRST);
  292. }
  293. void SPIClass::init() {
  294. if (initialized)
  295. return;
  296. interruptMode = 0;
  297. interruptMask = 0;
  298. interruptSave = 0;
  299. initCb();
  300. SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
  301. SPI_Enable(spi);
  302. initialized = true;
  303. }
  304. #ifndef interruptsStatus
  305. #define interruptsStatus() __interruptsStatus()
  306. static inline unsigned char __interruptsStatus(void) __attribute__((always_inline, unused));
  307. static inline unsigned char __interruptsStatus(void) {
  308. unsigned int primask;
  309. asm volatile ("mrs %0, primask" : "=r" (primask));
  310. if (primask) return 0;
  311. return 1;
  312. }
  313. #endif
  314. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  315. {
  316. uint8_t irestore;
  317. irestore = interruptsStatus();
  318. noInterrupts();
  319. if (interruptMode < 2) {
  320. if (interruptNumber > NUM_DIGITAL_PINS) {
  321. interruptMode = 2;
  322. } else {
  323. uint8_t imask = interruptMask;
  324. Pio *pio = g_APinDescription[interruptNumber].pPort;
  325. if (pio == PIOA) {
  326. imask |= 1;
  327. } else if (pio == PIOB) {
  328. imask |= 2;
  329. } else if (pio == PIOC) {
  330. imask |= 4;
  331. } else if (pio == PIOD) {
  332. imask |= 8;
  333. }
  334. interruptMask = imask;
  335. interruptMode = 1;
  336. }
  337. }
  338. if (irestore) interrupts();
  339. }
  340. void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
  341. {
  342. if (interruptMode > 0) {
  343. if (interruptMode == 1) {
  344. uint8_t imask = interruptMask;
  345. if (imask & 1) NVIC_DisableIRQ(PIOA_IRQn);
  346. if (imask & 2) NVIC_DisableIRQ(PIOB_IRQn);
  347. if (imask & 4) NVIC_DisableIRQ(PIOC_IRQn);
  348. if (imask & 8) NVIC_DisableIRQ(PIOD_IRQn);
  349. } else {
  350. interruptSave = interruptsStatus();
  351. noInterrupts();
  352. }
  353. }
  354. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
  355. bitOrder[ch] = settings.border;
  356. SPI_ConfigureNPCS(spi, ch, settings.config);
  357. }
  358. void SPIClass::endTransaction(void)
  359. {
  360. if (interruptMode > 0) {
  361. if (interruptMode == 1) {
  362. uint8_t imask = interruptMask;
  363. if (imask & 1) NVIC_EnableIRQ(PIOA_IRQn);
  364. if (imask & 2) NVIC_EnableIRQ(PIOB_IRQn);
  365. if (imask & 4) NVIC_EnableIRQ(PIOC_IRQn);
  366. if (imask & 8) NVIC_EnableIRQ(PIOD_IRQn);
  367. } else {
  368. if (interruptSave) interrupts();
  369. }
  370. }
  371. }
  372. void SPIClass::end(uint8_t _pin) {
  373. uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
  374. // Setting the pin as INPUT will disconnect it from SPI peripheral
  375. pinMode(spiPin, INPUT);
  376. }
  377. void SPIClass::end() {
  378. SPI_Disable(spi);
  379. initialized = false;
  380. }
  381. void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
  382. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  383. bitOrder[ch] = _bitOrder;
  384. }
  385. void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
  386. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  387. mode[ch] = _mode | SPI_CSR_CSAAT;
  388. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  389. // transfer. Some device needs that for working properly.
  390. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  391. }
  392. void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
  393. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  394. divider[ch] = _divider;
  395. // SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
  396. // transfer. Some device needs that for working properly.
  397. SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
  398. }
  399. byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
  400. uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
  401. // Reverse bit order
  402. if (bitOrder[ch] == LSBFIRST)
  403. _data = __REV(__RBIT(_data));
  404. uint32_t d = _data | SPI_PCS(ch);
  405. if (_mode == SPI_LAST)
  406. d |= SPI_TDR_LASTXFER;
  407. // SPI_Write(spi, _channel, _data);
  408. while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
  409. ;
  410. spi->SPI_TDR = d;
  411. // return SPI_Read(spi);
  412. while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
  413. ;
  414. d = spi->SPI_RDR;
  415. // Reverse bit order
  416. if (bitOrder[ch] == LSBFIRST)
  417. d = __REV(__RBIT(d));
  418. return d & 0xFF;
  419. }
  420. void SPIClass::attachInterrupt(void) {
  421. // Should be enableInterrupt()
  422. }
  423. void SPIClass::detachInterrupt(void) {
  424. // Should be disableInterrupt()
  425. }
  426. #if SPI_INTERFACES_COUNT > 0
  427. static void SPI_0_Init(void) {
  428. PIO_Configure(
  429. g_APinDescription[PIN_SPI_MOSI].pPort,
  430. g_APinDescription[PIN_SPI_MOSI].ulPinType,
  431. g_APinDescription[PIN_SPI_MOSI].ulPin,
  432. g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  433. PIO_Configure(
  434. g_APinDescription[PIN_SPI_MISO].pPort,
  435. g_APinDescription[PIN_SPI_MISO].ulPinType,
  436. g_APinDescription[PIN_SPI_MISO].ulPin,
  437. g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  438. PIO_Configure(
  439. g_APinDescription[PIN_SPI_SCK].pPort,
  440. g_APinDescription[PIN_SPI_SCK].ulPinType,
  441. g_APinDescription[PIN_SPI_SCK].ulPin,
  442. g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  443. }
  444. SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
  445. #endif
  446. #endif