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.

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