My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

SPI.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /******************************************************************************
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010 Perry Hung.
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *****************************************************************************/
  26. /**
  27. * @author Marti Bolivar <mbolivar@leaflabs.com>
  28. * @brief Wirish SPI implementation.
  29. */
  30. #ifdef __STM32F1__
  31. #include <SPI.h>
  32. #include <libmaple/timer.h>
  33. #include <libmaple/util.h>
  34. #include <libmaple/rcc.h>
  35. #include <boards.h>
  36. #include <wirish.h>
  37. /** Time in ms for DMA receive timeout */
  38. #define DMA_TIMEOUT 100
  39. #if CYCLES_PER_MICROSECOND != 72
  40. #warning "Unexpected clock speed; SPI frequency calculation will be incorrect"
  41. #endif
  42. struct spi_pins { uint8_t nss, sck, miso, mosi; };
  43. static const spi_pins* dev_to_spi_pins(spi_dev *dev);
  44. static void configure_gpios(spi_dev *dev, bool as_master);
  45. static spi_baud_rate determine_baud_rate(spi_dev *dev, uint32_t freq);
  46. #if BOARD_NR_SPI >= 3 && !defined(STM32_HIGH_DENSITY)
  47. #error "The SPI library is misconfigured: 3 SPI ports only available on high density STM32 devices"
  48. #endif
  49. static const spi_pins board_spi_pins[] __FLASH__ = {
  50. #if BOARD_NR_SPI >= 1
  51. { BOARD_SPI1_NSS_PIN,
  52. BOARD_SPI1_SCK_PIN,
  53. BOARD_SPI1_MISO_PIN,
  54. BOARD_SPI1_MOSI_PIN },
  55. #endif
  56. #if BOARD_NR_SPI >= 2
  57. { BOARD_SPI2_NSS_PIN,
  58. BOARD_SPI2_SCK_PIN,
  59. BOARD_SPI2_MISO_PIN,
  60. BOARD_SPI2_MOSI_PIN },
  61. #endif
  62. #if BOARD_NR_SPI >= 3
  63. { BOARD_SPI3_NSS_PIN,
  64. BOARD_SPI3_SCK_PIN,
  65. BOARD_SPI3_MISO_PIN,
  66. BOARD_SPI3_MOSI_PIN },
  67. #endif
  68. };
  69. #if BOARD_NR_SPI >= 1
  70. static void *_spi1_this;
  71. #endif
  72. #if BOARD_NR_SPI >= 2
  73. static void *_spi2_this;
  74. #endif
  75. #if BOARD_NR_SPI >= 3
  76. static void *_spi3_this;
  77. #endif
  78. /**
  79. * Constructor
  80. */
  81. SPIClass::SPIClass(uint32_t spi_num) {
  82. _currentSetting = &_settings[spi_num - 1]; // SPI channels are called 1 2 and 3 but the array is zero indexed
  83. switch (spi_num) {
  84. #if BOARD_NR_SPI >= 1
  85. case 1:
  86. _currentSetting->spi_d = SPI1;
  87. _spi1_this = (void*)this;
  88. break;
  89. #endif
  90. #if BOARD_NR_SPI >= 2
  91. case 2:
  92. _currentSetting->spi_d = SPI2;
  93. _spi2_this = (void*)this;
  94. break;
  95. #endif
  96. #if BOARD_NR_SPI >= 3
  97. case 3:
  98. _currentSetting->spi_d = SPI3;
  99. _spi3_this = (void*)this;
  100. break;
  101. #endif
  102. default: ASSERT(0);
  103. }
  104. // Init things specific to each SPI device
  105. // clock divider setup is a bit of hack, and needs to be improved at a later date.
  106. #if BOARD_NR_SPI >= 1
  107. _settings[0].spi_d = SPI1;
  108. _settings[0].clockDivider = determine_baud_rate(_settings[0].spi_d, _settings[0].clock);
  109. _settings[0].spiDmaDev = DMA1;
  110. _settings[0].spiTxDmaChannel = DMA_CH3;
  111. _settings[0].spiRxDmaChannel = DMA_CH2;
  112. #endif
  113. #if BOARD_NR_SPI >= 2
  114. _settings[1].spi_d = SPI2;
  115. _settings[1].clockDivider = determine_baud_rate(_settings[1].spi_d, _settings[1].clock);
  116. _settings[1].spiDmaDev = DMA1;
  117. _settings[1].spiTxDmaChannel = DMA_CH5;
  118. _settings[1].spiRxDmaChannel = DMA_CH4;
  119. #endif
  120. #if BOARD_NR_SPI >= 3
  121. _settings[2].spi_d = SPI3;
  122. _settings[2].clockDivider = determine_baud_rate(_settings[2].spi_d, _settings[2].clock);
  123. _settings[2].spiDmaDev = DMA2;
  124. _settings[2].spiTxDmaChannel = DMA_CH2;
  125. _settings[2].spiRxDmaChannel = DMA_CH1;
  126. #endif
  127. // added for DMA callbacks.
  128. _currentSetting->state = SPI_STATE_IDLE;
  129. }
  130. /**
  131. * Set up/tear down
  132. */
  133. void SPIClass::updateSettings() {
  134. uint32_t flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | _currentSetting->dataSize | SPI_SW_SLAVE | SPI_SOFT_SS);
  135. spi_master_enable(_currentSetting->spi_d, (spi_baud_rate)_currentSetting->clockDivider, (spi_mode)_currentSetting->dataMode, flags);
  136. }
  137. void SPIClass::begin() {
  138. spi_init(_currentSetting->spi_d);
  139. configure_gpios(_currentSetting->spi_d, 1);
  140. updateSettings();
  141. // added for DMA callbacks.
  142. _currentSetting->state = SPI_STATE_READY;
  143. }
  144. void SPIClass::beginSlave() {
  145. spi_init(_currentSetting->spi_d);
  146. configure_gpios(_currentSetting->spi_d, 0);
  147. uint32_t flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | _currentSetting->dataSize);
  148. spi_slave_enable(_currentSetting->spi_d, (spi_mode)_currentSetting->dataMode, flags);
  149. // added for DMA callbacks.
  150. _currentSetting->state = SPI_STATE_READY;
  151. }
  152. void SPIClass::end() {
  153. if (!spi_is_enabled(_currentSetting->spi_d)) return;
  154. // Follows RM0008's sequence for disabling a SPI in master/slave
  155. // full duplex mode.
  156. while (spi_is_rx_nonempty(_currentSetting->spi_d)) {
  157. // FIXME [0.1.0] remove this once you have an interrupt based driver
  158. volatile uint16_t rx __attribute__((unused)) = spi_rx_reg(_currentSetting->spi_d);
  159. }
  160. waitSpiTxEnd(_currentSetting->spi_d);
  161. spi_peripheral_disable(_currentSetting->spi_d);
  162. // added for DMA callbacks.
  163. // Need to add unsetting the callbacks for the DMA channels.
  164. _currentSetting->state = SPI_STATE_IDLE;
  165. }
  166. /* Roger Clark added 3 functions */
  167. void SPIClass::setClockDivider(uint32_t clockDivider) {
  168. _currentSetting->clockDivider = clockDivider;
  169. uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_BR);
  170. _currentSetting->spi_d->regs->CR1 = cr1 | (clockDivider & SPI_CR1_BR);
  171. }
  172. void SPIClass::setBitOrder(BitOrder bitOrder) {
  173. _currentSetting->bitOrder = bitOrder;
  174. uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_LSBFIRST);
  175. if (bitOrder == LSBFIRST) cr1 |= SPI_CR1_LSBFIRST;
  176. _currentSetting->spi_d->regs->CR1 = cr1;
  177. }
  178. /**
  179. * Victor Perez. Added to test changing datasize from 8 to 16 bit modes on the fly.
  180. * Input parameter should be SPI_CR1_DFF set to 0 or 1 on a 32bit word.
  181. */
  182. void SPIClass::setDataSize(uint32_t datasize) {
  183. _currentSetting->dataSize = datasize;
  184. uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_DFF);
  185. uint8_t en = spi_is_enabled(_currentSetting->spi_d);
  186. spi_peripheral_disable(_currentSetting->spi_d);
  187. _currentSetting->spi_d->regs->CR1 = cr1 | (datasize & SPI_CR1_DFF) | en;
  188. }
  189. void SPIClass::setDataMode(uint8_t dataMode) {
  190. /**
  191. * Notes:
  192. * As far as we know the AVR numbers for dataMode match the numbers required by the STM32.
  193. * From the AVR doc https://www.atmel.com/images/doc2585.pdf section 2.4
  194. *
  195. * SPI Mode CPOL CPHA Shift SCK-edge Capture SCK-edge
  196. * 0 0 0 Falling Rising
  197. * 1 0 1 Rising Falling
  198. * 2 1 0 Rising Falling
  199. * 3 1 1 Falling Rising
  200. *
  201. * On the STM32 it appears to be
  202. *
  203. * bit 1 - CPOL : Clock polarity
  204. * (This bit should not be changed when communication is ongoing)
  205. * 0 : CLK to 0 when idle
  206. * 1 : CLK to 1 when idle
  207. *
  208. * bit 0 - CPHA : Clock phase
  209. * (This bit should not be changed when communication is ongoing)
  210. * 0 : The first clock transition is the first data capture edge
  211. * 1 : The second clock transition is the first data capture edge
  212. *
  213. * If someone finds this is not the case or sees a logic error with this let me know ;-)
  214. */
  215. _currentSetting->dataMode = dataMode;
  216. uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_CPOL|SPI_CR1_CPHA);
  217. _currentSetting->spi_d->regs->CR1 = cr1 | (dataMode & (SPI_CR1_CPOL|SPI_CR1_CPHA));
  218. }
  219. void SPIClass::beginTransaction(uint8_t pin, const SPISettings &settings) {
  220. setBitOrder(settings.bitOrder);
  221. setDataMode(settings.dataMode);
  222. setDataSize(settings.dataSize);
  223. setClockDivider(determine_baud_rate(_currentSetting->spi_d, settings.clock));
  224. begin();
  225. }
  226. void SPIClass::beginTransactionSlave(const SPISettings &settings) {
  227. setBitOrder(settings.bitOrder);
  228. setDataMode(settings.dataMode);
  229. setDataSize(settings.dataSize);
  230. beginSlave();
  231. }
  232. void SPIClass::endTransaction() { }
  233. /**
  234. * I/O
  235. */
  236. uint16_t SPIClass::read() {
  237. while (!spi_is_rx_nonempty(_currentSetting->spi_d)) { /* nada */ }
  238. return (uint16_t)spi_rx_reg(_currentSetting->spi_d);
  239. }
  240. void SPIClass::read(uint8_t *buf, uint32_t len) {
  241. if (len == 0) return;
  242. spi_rx_reg(_currentSetting->spi_d); // clear the RX buffer in case a byte is waiting on it.
  243. spi_reg_map * regs = _currentSetting->spi_d->regs;
  244. // start sequence: write byte 0
  245. regs->DR = 0x00FF; // write the first byte
  246. // main loop
  247. while (--len) {
  248. while(!(regs->SR & SPI_SR_TXE)) { /* nada */ } // wait for TXE flag
  249. noInterrupts(); // go atomic level - avoid interrupts to surely get the previously received data
  250. regs->DR = 0x00FF; // write the next data item to be transmitted into the SPI_DR register. This clears the TXE flag.
  251. while (!(regs->SR & SPI_SR_RXNE)) { /* nada */ } // wait till data is available in the DR register
  252. *buf++ = (uint8)(regs->DR); // read and store the received byte. This clears the RXNE flag.
  253. interrupts(); // let systick do its job
  254. }
  255. // read remaining last byte
  256. while (!(regs->SR & SPI_SR_RXNE)) { /* nada */ } // wait till data is available in the Rx register
  257. *buf++ = (uint8)(regs->DR); // read and store the received byte
  258. }
  259. void SPIClass::write(uint16_t data) {
  260. /* Added for 16bit data Victor Perez. Roger Clark
  261. * Improved speed by just directly writing the single byte to the SPI data reg and wait for completion,
  262. * by taking the Tx code from transfer(byte)
  263. * This almost doubles the speed of this function.
  264. */
  265. spi_tx_reg(_currentSetting->spi_d, data); // write the data to be transmitted into the SPI_DR register (this clears the TXE flag)
  266. waitSpiTxEnd(_currentSetting->spi_d);
  267. }
  268. void SPIClass::write16(uint16_t data) {
  269. // Added by stevestrong: write two consecutive bytes in 8 bit mode (DFF=0)
  270. spi_tx_reg(_currentSetting->spi_d, data>>8); // write high byte
  271. while (!spi_is_tx_empty(_currentSetting->spi_d)) { /* nada */ } // Wait until TXE=1
  272. spi_tx_reg(_currentSetting->spi_d, data); // write low byte
  273. waitSpiTxEnd(_currentSetting->spi_d);
  274. }
  275. void SPIClass::write(uint16_t data, uint32_t n) {
  276. // Added by stevstrong: Repeatedly send same data by the specified number of times
  277. spi_reg_map * regs = _currentSetting->spi_d->regs;
  278. while (n--) {
  279. regs->DR = data; // write the data to be transmitted into the SPI_DR register (this clears the TXE flag)
  280. while (!(regs->SR & SPI_SR_TXE)) { /* nada */ } // wait till Tx empty
  281. }
  282. while (regs->SR & SPI_SR_BSY) { /* nada */ } // wait until BSY=0 before returning
  283. }
  284. void SPIClass::write(const void *data, uint32_t length) {
  285. spi_dev * spi_d = _currentSetting->spi_d;
  286. spi_tx(spi_d, data, length); // data can be array of bytes or words
  287. waitSpiTxEnd(spi_d);
  288. }
  289. uint8_t SPIClass::transfer(uint8_t byte) const {
  290. spi_dev * spi_d = _currentSetting->spi_d;
  291. spi_rx_reg(spi_d); // read any previous data
  292. spi_tx_reg(spi_d, byte); // Write the data item to be transmitted into the SPI_DR register
  293. waitSpiTxEnd(spi_d);
  294. return (uint8)spi_rx_reg(spi_d); // "... and read the last received data."
  295. }
  296. uint16_t SPIClass::transfer16(uint16_t data) const {
  297. // Modified by stevestrong: write & read two consecutive bytes in 8 bit mode (DFF=0)
  298. // This is more effective than two distinct byte transfers
  299. spi_dev * spi_d = _currentSetting->spi_d;
  300. spi_rx_reg(spi_d); // read any previous data
  301. spi_tx_reg(spi_d, data>>8); // write high byte
  302. waitSpiTxEnd(spi_d); // wait until TXE=1 and then wait until BSY=0
  303. uint16_t ret = spi_rx_reg(spi_d)<<8; // read and shift high byte
  304. spi_tx_reg(spi_d, data); // write low byte
  305. waitSpiTxEnd(spi_d); // wait until TXE=1 and then wait until BSY=0
  306. ret += spi_rx_reg(spi_d); // read low byte
  307. return ret;
  308. }
  309. /**
  310. * Roger Clark and Victor Perez, 2015
  311. * Performs a DMA SPI transfer with at least a receive buffer.
  312. * If a TX buffer is not provided, FF is sent over and over for the lenght of the transfer.
  313. * On exit TX buffer is not modified, and RX buffer cotains the received data.
  314. * Still in progress.
  315. */
  316. void SPIClass::dmaTransferSet(const void *transmitBuf, void *receiveBuf) {
  317. dma_init(_currentSetting->spiDmaDev);
  318. //spi_rx_dma_enable(_currentSetting->spi_d);
  319. //spi_tx_dma_enable(_currentSetting->spi_d);
  320. dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_16BIT) ? DMA_SIZE_16BITS : DMA_SIZE_8BITS;
  321. dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &_currentSetting->spi_d->regs->DR,
  322. dma_bit_size, receiveBuf, dma_bit_size, (DMA_MINC_MODE | DMA_TRNS_CMPLT ));// receive buffer DMA
  323. if (!transmitBuf) {
  324. transmitBuf = &ff;
  325. dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR,
  326. dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, (DMA_FROM_MEM));// Transmit FF repeatedly
  327. }
  328. else {
  329. dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR,
  330. dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, (DMA_MINC_MODE | DMA_FROM_MEM ));// Transmit buffer DMA
  331. }
  332. dma_set_priority(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, DMA_PRIORITY_LOW);
  333. dma_set_priority(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, DMA_PRIORITY_VERY_HIGH);
  334. }
  335. uint8_t SPIClass::dmaTransferRepeat(uint16_t length) {
  336. if (length == 0) return 0;
  337. if (spi_is_rx_nonempty(_currentSetting->spi_d) == 1) spi_rx_reg(_currentSetting->spi_d);
  338. _currentSetting->state = SPI_STATE_TRANSFER;
  339. dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, length);
  340. dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
  341. dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);// enable receive
  342. dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
  343. spi_rx_dma_enable(_currentSetting->spi_d);
  344. spi_tx_dma_enable(_currentSetting->spi_d);
  345. if (_currentSetting->receiveCallback)
  346. return 0;
  347. //uint32_t m = millis();
  348. uint8_t b = 0;
  349. uint32_t m = millis();
  350. while (!(dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & DMA_ISR_TCIF1)) {
  351. // Avoid interrupts and just loop waiting for the flag to be set.
  352. if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; }
  353. }
  354. waitSpiTxEnd(_currentSetting->spi_d); // until TXE=1 and BSY=0
  355. spi_tx_dma_disable(_currentSetting->spi_d);
  356. spi_rx_dma_disable(_currentSetting->spi_d);
  357. dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  358. dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);
  359. dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);
  360. dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  361. _currentSetting->state = SPI_STATE_READY;
  362. return b;
  363. }
  364. /**
  365. * Roger Clark and Victor Perez, 2015
  366. * Performs a DMA SPI transfer with at least a receive buffer.
  367. * If a TX buffer is not provided, FF is sent over and over for the length of the transfer.
  368. * On exit TX buffer is not modified, and RX buffer contains the received data.
  369. * Still in progress.
  370. */
  371. uint8_t SPIClass::dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length) {
  372. dmaTransferSet(transmitBuf, receiveBuf);
  373. return dmaTransferRepeat(length);
  374. }
  375. /**
  376. * Roger Clark and Victor Perez, 2015
  377. * Performs a DMA SPI send using a TX buffer.
  378. * On exit TX buffer is not modified.
  379. * Still in progress.
  380. * 2016 - stevstrong - reworked to automatically detect bit size from SPI setting
  381. */
  382. void SPIClass::dmaSendSet(const void * transmitBuf, bool minc) {
  383. uint32_t flags = ( (DMA_MINC_MODE*minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT);
  384. dma_init(_currentSetting->spiDmaDev);
  385. dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_16BIT) ? DMA_SIZE_16BITS : DMA_SIZE_8BITS;
  386. dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size,
  387. (volatile void*)transmitBuf, dma_bit_size, flags);// Transmit buffer DMA
  388. dma_set_priority(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, DMA_PRIORITY_LOW);
  389. }
  390. uint8_t SPIClass::dmaSendRepeat(uint16_t length) {
  391. if (length == 0) return 0;
  392. dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  393. dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
  394. _currentSetting->state = SPI_STATE_TRANSMIT;
  395. dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); // enable transmit
  396. spi_tx_dma_enable(_currentSetting->spi_d);
  397. if (_currentSetting->transmitCallback) return 0;
  398. uint32_t m = millis();
  399. uint8_t b = 0;
  400. while (!(dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & DMA_ISR_TCIF1)) {
  401. // Avoid interrupts and just loop waiting for the flag to be set.
  402. if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; }
  403. }
  404. waitSpiTxEnd(_currentSetting->spi_d); // until TXE=1 and BSY=0
  405. spi_tx_dma_disable(_currentSetting->spi_d);
  406. dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  407. dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  408. _currentSetting->state = SPI_STATE_READY;
  409. return b;
  410. }
  411. uint8_t SPIClass::dmaSend(const void * transmitBuf, uint16_t length, bool minc) {
  412. dmaSendSet(transmitBuf, minc);
  413. return dmaSendRepeat(length);
  414. }
  415. uint8_t SPIClass::dmaSendAsync(const void * transmitBuf, uint16_t length, bool minc) {
  416. uint8_t b = 0;
  417. if (_currentSetting->state != SPI_STATE_READY) {
  418. uint32_t m = millis();
  419. while (!(dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & DMA_ISR_TCIF1)) {
  420. //Avoid interrupts and just loop waiting for the flag to be set.
  421. //delayMicroseconds(10);
  422. if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; }
  423. }
  424. waitSpiTxEnd(_currentSetting->spi_d); // until TXE=1 and BSY=0
  425. spi_tx_dma_disable(_currentSetting->spi_d);
  426. dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  427. _currentSetting->state = SPI_STATE_READY;
  428. }
  429. if (length == 0) return 0;
  430. uint32_t flags = ( (DMA_MINC_MODE*minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT);
  431. dma_init(_currentSetting->spiDmaDev);
  432. // TX
  433. dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_16BIT) ? DMA_SIZE_16BITS : DMA_SIZE_8BITS;
  434. dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR,
  435. dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, flags);// Transmit buffer DMA
  436. dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
  437. dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  438. dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
  439. spi_tx_dma_enable(_currentSetting->spi_d);
  440. _currentSetting->state = SPI_STATE_TRANSMIT;
  441. return b;
  442. }
  443. /**
  444. * New functions added to manage callbacks.
  445. * Victor Perez 2017
  446. */
  447. void SPIClass::onReceive(void(*callback)()) {
  448. _currentSetting->receiveCallback = callback;
  449. if (callback) {
  450. switch (_currentSetting->spi_d->clk_id) {
  451. #if BOARD_NR_SPI >= 1
  452. case RCC_SPI1:
  453. dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &SPIClass::_spi1EventCallback);
  454. break;
  455. #endif
  456. #if BOARD_NR_SPI >= 2
  457. case RCC_SPI2:
  458. dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &SPIClass::_spi2EventCallback);
  459. break;
  460. #endif
  461. #if BOARD_NR_SPI >= 3
  462. case RCC_SPI3:
  463. dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &SPIClass::_spi3EventCallback);
  464. break;
  465. #endif
  466. default:
  467. ASSERT(0);
  468. }
  469. }
  470. else {
  471. dma_detach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);
  472. }
  473. }
  474. void SPIClass::onTransmit(void(*callback)()) {
  475. _currentSetting->transmitCallback = callback;
  476. if (callback) {
  477. switch (_currentSetting->spi_d->clk_id) {
  478. #if BOARD_NR_SPI >= 1
  479. case RCC_SPI1:
  480. dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &SPIClass::_spi1EventCallback);
  481. break;
  482. #endif
  483. #if BOARD_NR_SPI >= 2
  484. case RCC_SPI2:
  485. dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &SPIClass::_spi2EventCallback);
  486. break;
  487. #endif
  488. #if BOARD_NR_SPI >= 3
  489. case RCC_SPI3:
  490. dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &SPIClass::_spi3EventCallback);
  491. break;
  492. #endif
  493. default:
  494. ASSERT(0);
  495. }
  496. }
  497. else {
  498. dma_detach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  499. }
  500. }
  501. /**
  502. * TODO: check if better to first call the customer code, next disable the DMA requests.
  503. * Also see if we need to check whether callbacks are set or not, may be better to be checked
  504. * during the initial setup and only set the callback to EventCallback if they are set.
  505. */
  506. void SPIClass::EventCallback() {
  507. waitSpiTxEnd(_currentSetting->spi_d);
  508. switch (_currentSetting->state) {
  509. case SPI_STATE_TRANSFER:
  510. while (spi_is_rx_nonempty(_currentSetting->spi_d)) { /* nada */ }
  511. _currentSetting->state = SPI_STATE_READY;
  512. spi_tx_dma_disable(_currentSetting->spi_d);
  513. spi_rx_dma_disable(_currentSetting->spi_d);
  514. //dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  515. //dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);
  516. if (_currentSetting->receiveCallback)
  517. _currentSetting->receiveCallback();
  518. break;
  519. case SPI_STATE_TRANSMIT:
  520. _currentSetting->state = SPI_STATE_READY;
  521. spi_tx_dma_disable(_currentSetting->spi_d);
  522. //dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
  523. if (_currentSetting->transmitCallback)
  524. _currentSetting->transmitCallback();
  525. break;
  526. default:
  527. break;
  528. }
  529. }
  530. void SPIClass::attachInterrupt() {
  531. // Should be enableInterrupt()
  532. }
  533. void SPIClass::detachInterrupt() {
  534. // Should be disableInterrupt()
  535. }
  536. /**
  537. * Pin accessors
  538. */
  539. uint8_t SPIClass::misoPin() {
  540. return dev_to_spi_pins(_currentSetting->spi_d)->miso;
  541. }
  542. uint8_t SPIClass::mosiPin() {
  543. return dev_to_spi_pins(_currentSetting->spi_d)->mosi;
  544. }
  545. uint8_t SPIClass::sckPin() {
  546. return dev_to_spi_pins(_currentSetting->spi_d)->sck;
  547. }
  548. uint8_t SPIClass::nssPin() {
  549. return dev_to_spi_pins(_currentSetting->spi_d)->nss;
  550. }
  551. /**
  552. * Deprecated functions
  553. */
  554. uint8_t SPIClass::send(uint8_t data) { write(data); return 1; }
  555. uint8_t SPIClass::send(uint8_t *buf, uint32_t len) { write(buf, len); return len; }
  556. uint8_t SPIClass::recv() { return read(); }
  557. /**
  558. * DMA call back functions, one per port.
  559. */
  560. #if BOARD_NR_SPI >= 1
  561. void SPIClass::_spi1EventCallback() {
  562. reinterpret_cast<class SPIClass*>(_spi1_this)->EventCallback();
  563. }
  564. #endif
  565. #if BOARD_NR_SPI >= 2
  566. void SPIClass::_spi2EventCallback() {
  567. reinterpret_cast<class SPIClass*>(_spi2_this)->EventCallback();
  568. }
  569. #endif
  570. #if BOARD_NR_SPI >= 3
  571. void SPIClass::_spi3EventCallback() {
  572. reinterpret_cast<class SPIClass*>(_spi3_this)->EventCallback();
  573. }
  574. #endif
  575. /**
  576. * Auxiliary functions
  577. */
  578. static const spi_pins* dev_to_spi_pins(spi_dev *dev) {
  579. switch (dev->clk_id) {
  580. #if BOARD_NR_SPI >= 1
  581. case RCC_SPI1: return board_spi_pins;
  582. #endif
  583. #if BOARD_NR_SPI >= 2
  584. case RCC_SPI2: return board_spi_pins + 1;
  585. #endif
  586. #if BOARD_NR_SPI >= 3
  587. case RCC_SPI3: return board_spi_pins + 2;
  588. #endif
  589. default: return NULL;
  590. }
  591. }
  592. static void disable_pwm(const stm32_pin_info *i) {
  593. if (i->timer_device)
  594. timer_set_mode(i->timer_device, i->timer_channel, TIMER_DISABLED);
  595. }
  596. static void configure_gpios(spi_dev *dev, bool as_master) {
  597. const spi_pins *pins = dev_to_spi_pins(dev);
  598. if (!pins) return;
  599. const stm32_pin_info *nssi = &PIN_MAP[pins->nss],
  600. *scki = &PIN_MAP[pins->sck],
  601. *misoi = &PIN_MAP[pins->miso],
  602. *mosii = &PIN_MAP[pins->mosi];
  603. disable_pwm(nssi);
  604. disable_pwm(scki);
  605. disable_pwm(misoi);
  606. disable_pwm(mosii);
  607. spi_config_gpios(dev, as_master, nssi->gpio_device, nssi->gpio_bit,
  608. scki->gpio_device, scki->gpio_bit, misoi->gpio_bit,
  609. mosii->gpio_bit);
  610. }
  611. static const spi_baud_rate baud_rates[8] __FLASH__ = {
  612. SPI_BAUD_PCLK_DIV_2,
  613. SPI_BAUD_PCLK_DIV_4,
  614. SPI_BAUD_PCLK_DIV_8,
  615. SPI_BAUD_PCLK_DIV_16,
  616. SPI_BAUD_PCLK_DIV_32,
  617. SPI_BAUD_PCLK_DIV_64,
  618. SPI_BAUD_PCLK_DIV_128,
  619. SPI_BAUD_PCLK_DIV_256,
  620. };
  621. /**
  622. * Note: This assumes you're on a LeafLabs-style board
  623. * (CYCLES_PER_MICROSECOND == 72, APB2 at 72MHz, APB1 at 36MHz).
  624. */
  625. static spi_baud_rate determine_baud_rate(spi_dev *dev, uint32_t freq) {
  626. uint32_t clock = 0;
  627. switch (rcc_dev_clk(dev->clk_id)) {
  628. case RCC_AHB:
  629. case RCC_APB2: clock = STM32_PCLK2; break; // 72 Mhz
  630. case RCC_APB1: clock = STM32_PCLK1; break; // 36 Mhz
  631. }
  632. clock >>= 1;
  633. uint8_t i = 0;
  634. while (i < 7 && freq < clock) { clock >>= 1; i++; }
  635. return baud_rates[i];
  636. }
  637. SPIClass SPI(1);
  638. #endif // __STM32F1__