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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <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. #include <SPI.h>
  48. // ------------------------
  49. // Public functions
  50. // ------------------------
  51. #if ENABLED(LPC_SOFTWARE_SPI)
  52. #include <SoftwareSPI.h>
  53. // Software SPI
  54. static uint8_t SPI_speed = 0;
  55. static uint8_t spiTransfer(uint8_t b) {
  56. return swSpiTransfer(b, SPI_speed, SCK_PIN, MISO_PIN, MOSI_PIN);
  57. }
  58. void spiBegin() {
  59. swSpiBegin(SCK_PIN, MISO_PIN, MOSI_PIN);
  60. }
  61. void spiInit(uint8_t spiRate) {
  62. SPI_speed = swSpiInit(spiRate, SCK_PIN, MOSI_PIN);
  63. }
  64. uint8_t spiRec() { return spiTransfer(0xFF); }
  65. void spiRead(uint8_t*buf, uint16_t nbyte) {
  66. for (int i = 0; i < nbyte; i++)
  67. buf[i] = spiTransfer(0xFF);
  68. }
  69. void spiSend(uint8_t b) { (void)spiTransfer(b); }
  70. void spiSend(const uint8_t* buf, size_t nbyte) {
  71. for (uint16_t i = 0; i < nbyte; i++)
  72. (void)spiTransfer(buf[i]);
  73. }
  74. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  75. (void)spiTransfer(token);
  76. for (uint16_t i = 0; i < 512; i++)
  77. (void)spiTransfer(buf[i]);
  78. }
  79. #else
  80. // Hardware SPI
  81. #include <lpc17xx_pinsel.h>
  82. #include <lpc17xx_ssp.h>
  83. #include <lpc17xx_clkpwr.h>
  84. // decide which HW SPI device to use
  85. #ifndef LPC_HW_SPI_DEV
  86. #if (SCK_PIN == P0_07 && MISO_PIN == P0_08 && MOSI_PIN == P0_09)
  87. #define LPC_HW_SPI_DEV 1
  88. #else
  89. #if (SCK_PIN == P0_15 && MISO_PIN == P0_17 && MOSI_PIN == P0_18)
  90. #define LPC_HW_SPI_DEV 0
  91. #else
  92. #error "Invalid pins selected for hardware SPI"
  93. #endif
  94. #endif
  95. #endif
  96. #if (LPC_HW_SPI_DEV == 0)
  97. #define LPC_SSPn LPC_SSP0
  98. #else
  99. #define LPC_SSPn LPC_SSP1
  100. #endif
  101. void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0
  102. PINSEL_CFG_Type PinCfg; // data structure to hold init values
  103. PinCfg.Funcnum = 2;
  104. PinCfg.OpenDrain = 0;
  105. PinCfg.Pinmode = 0;
  106. PinCfg.Pinnum = LPC176x::pin_bit(SCK_PIN);
  107. PinCfg.Portnum = LPC176x::pin_port(SCK_PIN);
  108. PINSEL_ConfigPin(&PinCfg);
  109. SET_OUTPUT(SCK_PIN);
  110. PinCfg.Pinnum = LPC176x::pin_bit(MISO_PIN);
  111. PinCfg.Portnum = LPC176x::pin_port(MISO_PIN);
  112. PINSEL_ConfigPin(&PinCfg);
  113. SET_INPUT(MISO_PIN);
  114. PinCfg.Pinnum = LPC176x::pin_bit(MOSI_PIN);
  115. PinCfg.Portnum = LPC176x::pin_port(MOSI_PIN);
  116. PINSEL_ConfigPin(&PinCfg);
  117. SET_OUTPUT(MOSI_PIN);
  118. // divide PCLK by 2 for SSP0
  119. CLKPWR_SetPCLKDiv(LPC_HW_SPI_DEV == 0 ? CLKPWR_PCLKSEL_SSP0 : CLKPWR_PCLKSEL_SSP1, CLKPWR_PCLKSEL_CCLK_DIV_2);
  120. spiInit(0);
  121. SSP_Cmd(LPC_SSPn, ENABLE); // start SSP running
  122. }
  123. void spiInit(uint8_t spiRate) {
  124. // table to convert Marlin spiRates (0-5 plus default) into bit rates
  125. uint32_t Marlin_speed[7]; // CPSR is always 2
  126. Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
  127. Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
  128. Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
  129. Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
  130. Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
  131. Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
  132. Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
  133. // setup for SPI mode
  134. SSP_CFG_Type HW_SPI_init; // data structure to hold init values
  135. SSP_ConfigStructInit(&HW_SPI_init); // set values for SPI mode
  136. HW_SPI_init.ClockRate = Marlin_speed[_MIN(spiRate, 6)]; // put in the specified bit rate
  137. HW_SPI_init.Mode |= SSP_CR1_SSP_EN;
  138. SSP_Init(LPC_SSPn, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers
  139. }
  140. static uint8_t doio(uint8_t b) {
  141. /* send and receive a single byte */
  142. SSP_SendData(LPC_SSPn, b & 0x00FF);
  143. while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY)); // wait for it to finish
  144. return SSP_ReceiveData(LPC_SSPn) & 0x00FF;
  145. }
  146. void spiSend(uint8_t b) { doio(b); }
  147. void spiSend(const uint8_t* buf, size_t nbyte) {
  148. for (uint16_t i = 0; i < nbyte; i++) doio(buf[i]);
  149. }
  150. void spiSend(uint32_t chan, byte b) {
  151. }
  152. void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {
  153. }
  154. // Read single byte from SPI
  155. uint8_t spiRec() { return doio(0xFF); }
  156. uint8_t spiRec(uint32_t chan) { return 0; }
  157. // Read from SPI into buffer
  158. void spiRead(uint8_t *buf, uint16_t nbyte) {
  159. for (uint16_t i = 0; i < nbyte; i++) buf[i] = doio(0xFF);
  160. }
  161. static uint8_t spiTransfer(uint8_t b) {
  162. return doio(b);
  163. }
  164. // Write from buffer to SPI
  165. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  166. (void)spiTransfer(token);
  167. for (uint16_t i = 0; i < 512; i++)
  168. (void)spiTransfer(buf[i]);
  169. }
  170. /** Begin SPI transaction, set clock, bit order, data mode */
  171. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  172. // TODO: to be implemented
  173. }
  174. #endif // ENABLED(LPC_SOFTWARE_SPI)
  175. void SPIClass::begin() { spiBegin(); }
  176. void SPIClass::beginTransaction(SPISettings cfg) {
  177. uint8_t spiRate;
  178. switch (cfg.spiRate()) {
  179. case 8000000: spiRate = 0; break;
  180. case 4000000: spiRate = 1; break;
  181. case 2000000: spiRate = 2; break;
  182. case 1000000: spiRate = 3; break;
  183. case 500000: spiRate = 4; break;
  184. case 250000: spiRate = 5; break;
  185. case 125000: spiRate = 6; break;
  186. default: spiRate = 2; break;
  187. }
  188. spiInit(spiRate);
  189. }
  190. uint8_t SPIClass::transfer(const uint8_t B) { return spiTransfer(B); }
  191. uint16_t SPIClass::transfer16(const uint16_t data) {
  192. return (transfer((data >> 8) & 0xFF) << 8)
  193. | (transfer(data & 0xFF) & 0xFF);
  194. }
  195. SPIClass SPI;
  196. #endif // TARGET_LPC1768