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

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