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.

MarlinSPI.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(STM32H7xx)
  23. #include "MarlinSPI.h"
  24. static void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb, uint32_t dataSize) {
  25. spi_init(obj, speed, mode, msb);
  26. // spi_init set 8bit always
  27. // TODO: copy the code from spi_init and handle data size, to avoid double init always!!
  28. if (dataSize != SPI_DATASIZE_8BIT) {
  29. obj->handle.Init.DataSize = dataSize;
  30. HAL_SPI_Init(&obj->handle);
  31. __HAL_SPI_ENABLE(&obj->handle);
  32. }
  33. }
  34. void MarlinSPI::setClockDivider(uint8_t _div) {
  35. _speed = spi_getClkFreq(&_spi);// / _div;
  36. _clockDivider = _div;
  37. }
  38. void MarlinSPI::begin(void) {
  39. //TODO: only call spi_init if any parameter changed!!
  40. spi_init(&_spi, _speed, _dataMode, _bitOrder, _dataSize);
  41. }
  42. void MarlinSPI::setupDma(SPI_HandleTypeDef &_spiHandle, DMA_HandleTypeDef &_dmaHandle, uint32_t direction, bool minc) {
  43. _dmaHandle.Init.Direction = direction;
  44. _dmaHandle.Init.PeriphInc = DMA_PINC_DISABLE;
  45. _dmaHandle.Init.Mode = DMA_NORMAL;
  46. _dmaHandle.Init.Priority = DMA_PRIORITY_LOW;
  47. _dmaHandle.Init.MemInc = minc ? DMA_MINC_ENABLE : DMA_MINC_DISABLE;
  48. if (_dataSize == DATA_SIZE_8BIT) {
  49. _dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  50. _dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  51. }
  52. else {
  53. _dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  54. _dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  55. }
  56. #ifdef STM32F4xx
  57. _dmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  58. #endif
  59. // start DMA hardware
  60. // TODO: check if hardware is already enabled
  61. #ifdef SPI1_BASE
  62. if (_spiHandle.Instance == SPI1) {
  63. #ifdef STM32F1xx
  64. __HAL_RCC_DMA1_CLK_ENABLE();
  65. _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Channel3 : DMA1_Channel2;
  66. #elif defined(STM32F4xx)
  67. __HAL_RCC_DMA2_CLK_ENABLE();
  68. _dmaHandle.Init.Channel = DMA_CHANNEL_3;
  69. _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA2_Stream3 : DMA2_Stream0;
  70. #endif
  71. }
  72. #endif
  73. #ifdef SPI2_BASE
  74. if (_spiHandle.Instance == SPI2) {
  75. #ifdef STM32F1xx
  76. __HAL_RCC_DMA1_CLK_ENABLE();
  77. _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Channel5 : DMA1_Channel4;
  78. #elif defined(STM32F4xx)
  79. __HAL_RCC_DMA1_CLK_ENABLE();
  80. _dmaHandle.Init.Channel = DMA_CHANNEL_0;
  81. _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Stream4 : DMA1_Stream3;
  82. #endif
  83. }
  84. #endif
  85. #ifdef SPI3_BASE
  86. if (_spiHandle.Instance == SPI3) {
  87. #ifdef STM32F1xx
  88. __HAL_RCC_DMA2_CLK_ENABLE();
  89. _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA2_Channel2 : DMA2_Channel1;
  90. #elif defined(STM32F4xx)
  91. __HAL_RCC_DMA1_CLK_ENABLE();
  92. _dmaHandle.Init.Channel = DMA_CHANNEL_0;
  93. _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Stream5 : DMA1_Stream2;
  94. #endif
  95. }
  96. #endif
  97. HAL_DMA_Init(&_dmaHandle);
  98. }
  99. byte MarlinSPI::transfer(uint8_t _data) {
  100. uint8_t rxData = 0xFF;
  101. HAL_SPI_TransmitReceive(&_spi.handle, &_data, &rxData, 1, HAL_MAX_DELAY);
  102. return rxData;
  103. }
  104. uint8_t MarlinSPI::dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length) {
  105. const uint8_t ff = 0xFF;
  106. //if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) //only enable if disabled
  107. __HAL_SPI_ENABLE(&_spi.handle);
  108. if (receiveBuf) {
  109. setupDma(_spi.handle, _dmaRx, DMA_PERIPH_TO_MEMORY, true);
  110. HAL_DMA_Start(&_dmaRx, (uint32_t)&(_spi.handle.Instance->DR), (uint32_t)receiveBuf, length);
  111. SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_RXDMAEN); /* Enable Rx DMA Request */
  112. }
  113. // check for 2 lines transfer
  114. bool mincTransmit = true;
  115. if (transmitBuf == nullptr && _spi.handle.Init.Direction == SPI_DIRECTION_2LINES && _spi.handle.Init.Mode == SPI_MODE_MASTER) {
  116. transmitBuf = &ff;
  117. mincTransmit = false;
  118. }
  119. if (transmitBuf) {
  120. setupDma(_spi.handle, _dmaTx, DMA_MEMORY_TO_PERIPH, mincTransmit);
  121. HAL_DMA_Start(&_dmaTx, (uint32_t)transmitBuf, (uint32_t)&(_spi.handle.Instance->DR), length);
  122. SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_TXDMAEN); /* Enable Tx DMA Request */
  123. }
  124. if (transmitBuf) {
  125. HAL_DMA_PollForTransfer(&_dmaTx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
  126. HAL_DMA_Abort(&_dmaTx);
  127. HAL_DMA_DeInit(&_dmaTx);
  128. }
  129. // while ((_spi.handle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE) {}
  130. if (receiveBuf) {
  131. HAL_DMA_PollForTransfer(&_dmaRx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
  132. HAL_DMA_Abort(&_dmaRx);
  133. HAL_DMA_DeInit(&_dmaRx);
  134. }
  135. return 1;
  136. }
  137. uint8_t MarlinSPI::dmaSend(const void * transmitBuf, uint16_t length, bool minc) {
  138. setupDma(_spi.handle, _dmaTx, DMA_MEMORY_TO_PERIPH, minc);
  139. HAL_DMA_Start(&_dmaTx, (uint32_t)transmitBuf, (uint32_t)&(_spi.handle.Instance->DR), length);
  140. __HAL_SPI_ENABLE(&_spi.handle);
  141. SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_TXDMAEN); /* Enable Tx DMA Request */
  142. HAL_DMA_PollForTransfer(&_dmaTx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
  143. HAL_DMA_Abort(&_dmaTx);
  144. // DeInit objects
  145. HAL_DMA_DeInit(&_dmaTx);
  146. return 1;
  147. }
  148. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC