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.

HAL_spi.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Software SPI functions originally from Arduino Sd2Card Library
  24. * Copyright (C) 2009 by William Greiman
  25. */
  26. /**
  27. * For TARGET_LPC1768
  28. */
  29. /**
  30. * Hardware SPI and a software SPI implementations are included in this file.
  31. * The hardware SPI runs faster and has higher throughput but is not compatible
  32. * with some LCD interfaces/adapters.
  33. *
  34. * Control of the slave select pin(s) is handled by the calling routines.
  35. *
  36. * Some of the LCD interfaces/adapters result in the LCD SPI and the SD card
  37. * SPI sharing pins. The SCK, MOSI & MISO pins can NOT be set/cleared with
  38. * WRITE nor digitalWrite when the hardware SPI module within the LPC17xx is
  39. * active. If any of these pins are shared then the software SPI must be used.
  40. *
  41. * A more sophisticated hardware SPI can be found at the following link. This
  42. * implementation has not been fully debugged.
  43. * https://github.com/MarlinFirmware/Marlin/tree/071c7a78f27078fd4aee9a3ef365fcf5e143531e
  44. */
  45. #ifdef TARGET_LPC1768
  46. #include "../../inc/MarlinConfig.h"
  47. // --------------------------------------------------------------------------
  48. // Includes
  49. // --------------------------------------------------------------------------
  50. #include <SPI.h>
  51. // --------------------------------------------------------------------------
  52. // Public functions
  53. // --------------------------------------------------------------------------
  54. #if ENABLED(LPC_SOFTWARE_SPI)
  55. #include "SoftwareSPI.h"
  56. // Software SPI
  57. static uint8_t SPI_speed = 0;
  58. static uint8_t spiTransfer(uint8_t b) {
  59. return swSpiTransfer(b, SPI_speed, SCK_PIN, MISO_PIN, MOSI_PIN);
  60. }
  61. void spiBegin() {
  62. swSpiBegin(SCK_PIN, MISO_PIN, MOSI_PIN);
  63. }
  64. void spiInit(uint8_t spiRate) {
  65. SPI_speed = swSpiInit(spiRate, SCK_PIN, MOSI_PIN);
  66. }
  67. uint8_t spiRec() { return spiTransfer(0xFF); }
  68. void spiRead(uint8_t*buf, uint16_t nbyte) {
  69. if (nbyte)
  70. for (int i = 0; i < nbyte; i++)
  71. buf[i] = spiTransfer(0xFF);
  72. }
  73. void spiSend(uint8_t b) { (void)spiTransfer(b); }
  74. void spiSend(const uint8_t* buf, size_t n) {
  75. if (n)
  76. for (uint16_t i = 0; i < n; i++)
  77. (void)spiTransfer(buf[i]);
  78. }
  79. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  80. (void)spiTransfer(token);
  81. for (uint16_t i = 0; i < 512; i++)
  82. (void)spiTransfer(buf[i]);
  83. }
  84. #else
  85. // Hardware SPI
  86. #include <lpc17xx_pinsel.h>
  87. #include <lpc17xx_ssp.h>
  88. #include <lpc17xx_clkpwr.h>
  89. // decide which HW SPI device to use
  90. #ifndef LPC_HW_SPI_DEV
  91. #if (SCK_PIN == P0_07 && MISO_PIN == P0_08 && MOSI_PIN == P0_09)
  92. #define LPC_HW_SPI_DEV 1
  93. #else
  94. #if (SCK_PIN == P0_15 && MISO_PIN == P0_17 && MOSI_PIN == P0_18)
  95. #define LPC_HW_SPI_DEV 0
  96. #else
  97. #error "Invalid pins selected for hardware SPI"
  98. #endif
  99. #endif
  100. #endif
  101. #if (LPC_HW_SPI_DEV == 0)
  102. #define LPC_SSPn LPC_SSP0
  103. #else
  104. #define LPC_SSPn LPC_SSP1
  105. #endif
  106. void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0
  107. PINSEL_CFG_Type PinCfg; // data structure to hold init values
  108. PinCfg.Funcnum = 2;
  109. PinCfg.OpenDrain = 0;
  110. PinCfg.Pinmode = 0;
  111. PinCfg.Pinnum = LPC1768_PIN_PIN(SCK_PIN);
  112. PinCfg.Portnum = LPC1768_PIN_PORT(SCK_PIN);
  113. PINSEL_ConfigPin(&PinCfg);
  114. SET_OUTPUT(SCK_PIN);
  115. PinCfg.Pinnum = LPC1768_PIN_PIN(MISO_PIN);
  116. PinCfg.Portnum = LPC1768_PIN_PORT(MISO_PIN);
  117. PINSEL_ConfigPin(&PinCfg);
  118. SET_INPUT(MISO_PIN);
  119. PinCfg.Pinnum = LPC1768_PIN_PIN(MOSI_PIN);
  120. PinCfg.Portnum = LPC1768_PIN_PORT(MOSI_PIN);
  121. PINSEL_ConfigPin(&PinCfg);
  122. SET_OUTPUT(MOSI_PIN);
  123. // divide PCLK by 2 for SSP0
  124. CLKPWR_SetPCLKDiv(LPC_HW_SPI_DEV == 0 ? CLKPWR_PCLKSEL_SSP0 : CLKPWR_PCLKSEL_SSP1, CLKPWR_PCLKSEL_CCLK_DIV_2);
  125. spiInit(0);
  126. SSP_Cmd(LPC_SSPn, ENABLE); // start SSP running
  127. }
  128. void spiInit(uint8_t spiRate) {
  129. // table to convert Marlin spiRates (0-5 plus default) into bit rates
  130. uint32_t Marlin_speed[7]; // CPSR is always 2
  131. Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
  132. Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
  133. Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
  134. Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
  135. Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
  136. Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
  137. Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
  138. // setup for SPI mode
  139. SSP_CFG_Type HW_SPI_init; // data structure to hold init values
  140. SSP_ConfigStructInit(&HW_SPI_init); // set values for SPI mode
  141. HW_SPI_init.ClockRate = Marlin_speed[MIN(spiRate, 6)]; // put in the specified bit rate
  142. HW_SPI_init.Mode |= SSP_CR1_SSP_EN;
  143. SSP_Init(LPC_SSPn, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers
  144. }
  145. static uint8_t doio(uint8_t b) {
  146. /* send and receive a single byte */
  147. SSP_SendData(LPC_SSPn, b & 0x00FF);
  148. while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY)); // wait for it to finish
  149. return SSP_ReceiveData(LPC_SSPn) & 0x00FF;
  150. }
  151. void spiSend(uint8_t b) { doio(b); }
  152. void spiSend(const uint8_t* buf, size_t n) {
  153. if (n)
  154. for (uint16_t i = 0; i < n; i++) doio(buf[i]);
  155. }
  156. void spiSend(uint32_t chan, byte b) {
  157. }
  158. void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
  159. }
  160. // Read single byte from SPI
  161. uint8_t spiRec() { return doio(0xFF); }
  162. uint8_t spiRec(uint32_t chan) { return 0; }
  163. // Read from SPI into buffer
  164. void spiRead(uint8_t*buf, uint16_t nbyte) {
  165. if (nbyte)
  166. for (int i = 0; i < nbyte; i++) buf[i] = doio(0xff);
  167. }
  168. static uint8_t spiTransfer(uint8_t b) {
  169. return doio(b);
  170. }
  171. // Write from buffer to SPI
  172. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  173. (void)spiTransfer(token);
  174. for (uint16_t i = 0; i < 512; i++)
  175. (void)spiTransfer(buf[i]);
  176. }
  177. /** Begin SPI transaction, set clock, bit order, data mode */
  178. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  179. // TODO: to be implemented
  180. }
  181. #endif // ENABLED(LPC_SOFTWARE_SPI)
  182. void SPIClass::begin() { spiBegin(); }
  183. void SPIClass::beginTransaction(SPISettings cfg) {
  184. uint8_t spiRate;
  185. switch (cfg.spiRate()) {
  186. case 8000000: spiRate = 0; break;
  187. case 4000000: spiRate = 1; break;
  188. case 2000000: spiRate = 2; break;
  189. case 1000000: spiRate = 3; break;
  190. case 500000: spiRate = 4; break;
  191. case 250000: spiRate = 5; break;
  192. case 125000: spiRate = 6; break;
  193. default: spiRate = 2; break;
  194. }
  195. spiInit(spiRate);
  196. }
  197. uint8_t SPIClass::transfer(const uint8_t B) { return spiTransfer(B); }
  198. uint16_t SPIClass::transfer16(const uint16_t data) {
  199. return (transfer((data >> 8) & 0xFF) << 8)
  200. | (transfer(data & 0xFF) & 0xFF);
  201. }
  202. SPIClass SPI;
  203. #endif // TARGET_LPC1768