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

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