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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016, 2017 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. *
  28. * For TARGET_LPC1768
  29. */
  30. /**
  31. * Hardware SPI and a software SPI implementations are included in this file.
  32. * The hardware SPI runs faster and has higher throughput but is not compatible
  33. * with some LCD interfaces/adapters.
  34. *
  35. * Control of the slave select pin(s) is handled by the calling routines.
  36. *
  37. * Some of the LCD interfaces/adapters result in the LCD SPI and the SD card
  38. * SPI sharing pins. The SCK, MOSI & MISO pins can NOT be set/cleared with
  39. * WRITE nor digitalWrite when the hardware SPI module within the LPC17xx is
  40. * active. If any of these pins are shared then the software SPI must be used.
  41. *
  42. * A more sophisticated hardware SPI can be found at the following link. This
  43. * implementation has not been fully debugged.
  44. * https://github.com/MarlinFirmware/Marlin/tree/071c7a78f27078fd4aee9a3ef365fcf5e143531e
  45. */
  46. #ifdef TARGET_LPC1768
  47. // --------------------------------------------------------------------------
  48. // Includes
  49. // --------------------------------------------------------------------------
  50. //#include "../../../MarlinConfig.h" //works except in U8g
  51. #include "spi_pins.h"
  52. #include "fastio.h"
  53. // --------------------------------------------------------------------------
  54. // Public Variables
  55. // --------------------------------------------------------------------------
  56. // --------------------------------------------------------------------------
  57. // Public functions
  58. // --------------------------------------------------------------------------
  59. #if ENABLED(LPC_SOFTWARE_SPI)
  60. // --------------------------------------------------------------------------
  61. // software SPI
  62. // --------------------------------------------------------------------------
  63. /**
  64. * This software SPI runs at three rates. The SD software provides an index
  65. * (spiRate) of 0-6. The mapping is:
  66. * 0-1 - about 5 MHz peak
  67. * 2-3 - about 2 MHz peak
  68. * all others - about 250 KHz
  69. */
  70. static uint8_t SPI_speed = 0;
  71. static uint8_t spiTransfer(uint8_t b) {
  72. if (!SPI_speed) { // fastest - about 5 MHz peak
  73. for (int bits = 0; bits < 8; bits++) {
  74. if (b & 0x80) {
  75. WRITE(MOSI_PIN, HIGH);
  76. WRITE(MOSI_PIN, HIGH); // delay to (hopefully) guarantee setup time
  77. }
  78. else {
  79. WRITE(MOSI_PIN, LOW);
  80. WRITE(MOSI_PIN, LOW); // delay to (hopefully) guarantee setup time
  81. }
  82. b <<= 1;
  83. WRITE(SCK_PIN, HIGH);
  84. if (READ(MISO_PIN)) {
  85. b |= 1;
  86. }
  87. WRITE(SCK_PIN, LOW);
  88. }
  89. }
  90. else if (SPI_speed == 1) { // medium - about 1 MHz
  91. for (int bits = 0; bits < 8; bits++) {
  92. if (b & 0x80) {
  93. for (uint8_t i = 0; i < 8; i++) WRITE(MOSI_PIN, HIGH);
  94. }
  95. else {
  96. for (uint8_t i = 0; i < 8; i++) WRITE(MOSI_PIN, LOW);
  97. }
  98. b <<= 1;
  99. for (uint8_t i = 0; i < 6; i++) WRITE(SCK_PIN, HIGH);
  100. if (READ(MISO_PIN)) {
  101. b |= 1;
  102. }
  103. WRITE(SCK_PIN, LOW);
  104. }
  105. }
  106. else { // slow - about 250 KHz
  107. for (int bits = 0; bits < 8; bits++) {
  108. if (b & 0x80) {
  109. WRITE(MOSI_PIN, HIGH);
  110. }
  111. else {
  112. WRITE(MOSI_PIN, LOW);
  113. }
  114. b <<= 1;
  115. delayMicroseconds(1U);
  116. WRITE(SCK_PIN, HIGH);
  117. delayMicroseconds(2U);
  118. if (READ(MISO_PIN)) {
  119. b |= 1;
  120. }
  121. WRITE(SCK_PIN, LOW);
  122. delayMicroseconds(1U);
  123. }
  124. }
  125. return b;
  126. }
  127. void spiBegin() {
  128. SET_OUTPUT(SCK_PIN);
  129. SET_INPUT(MISO_PIN);
  130. SET_OUTPUT(MOSI_PIN);
  131. }
  132. void spiInit(uint8_t spiRate) {
  133. SPI_speed = spiRate >> 1;
  134. WRITE(MOSI_PIN, HIGH);
  135. WRITE(SCK_PIN, LOW);
  136. }
  137. uint8_t spiRec() {
  138. uint8_t b = spiTransfer(0xff);
  139. return b;
  140. }
  141. void spiRead(uint8_t*buf, uint16_t nbyte) {
  142. if (nbyte == 0) return;
  143. for (int i = 0; i < nbyte; i++) {
  144. buf[i] = spiTransfer(0xff);
  145. }
  146. }
  147. void spiSend(uint8_t b) {
  148. uint8_t response = spiTransfer(b);
  149. UNUSED(response);
  150. }
  151. static void spiSend(const uint8_t* buf, size_t n) {
  152. uint8_t response;
  153. if (n == 0) return;
  154. for (uint16_t i = 0; i < n; i++) {
  155. response = spiTransfer(buf[i]);
  156. }
  157. UNUSED(response);
  158. }
  159. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  160. uint8_t response;
  161. response = spiTransfer(token);
  162. for (uint16_t i = 0; i < 512; i++) {
  163. response = spiTransfer(buf[i]);
  164. }
  165. UNUSED(response);
  166. WRITE(SS_PIN, HIGH);
  167. }
  168. #else
  169. // hardware SPI
  170. #include <lpc17xx_pinsel.h>
  171. #include <lpc17xx_ssp.h>
  172. #include <lpc17xx_clkpwr.h>
  173. void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0
  174. PINSEL_CFG_Type PinCfg; // data structure to hold init values
  175. PinCfg.Funcnum = 2;
  176. PinCfg.OpenDrain = 0;
  177. PinCfg.Pinmode = 0;
  178. PinCfg.Pinnum = pin_map[SCK_PIN].pin;
  179. PinCfg.Portnum = pin_map[SCK_PIN].port;
  180. PINSEL_ConfigPin(&PinCfg);
  181. SET_OUTPUT(SCK_PIN);
  182. PinCfg.Pinnum = pin_map[MISO_PIN].pin;
  183. PinCfg.Portnum = pin_map[MISO_PIN].port;
  184. PINSEL_ConfigPin(&PinCfg);
  185. SET_INPUT(MISO_PIN);
  186. PinCfg.Pinnum = pin_map[MOSI_PIN].pin;
  187. PinCfg.Portnum = pin_map[MOSI_PIN].port;
  188. PINSEL_ConfigPin(&PinCfg);
  189. SET_OUTPUT(MOSI_PIN);
  190. }
  191. void spiInit(uint8_t spiRate) {
  192. // table to convert Marlin spiRates (0-5 plus default) into bit rates
  193. uint32_t Marlin_speed[7]; // CPSR is always 2
  194. Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
  195. Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
  196. Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
  197. Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
  198. Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
  199. Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
  200. Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
  201. // select 50MHz PCLK for SSP0
  202. CLKPWR_SetPCLKDiv(CLKPWR_PCLKSEL_SSP0, CLKPWR_PCLKSEL_CCLK_DIV_2);
  203. // setup for SPI mode
  204. SSP_CFG_Type HW_SPI_init; // data structure to hold init values
  205. SSP_ConfigStructInit(&HW_SPI_init); // set values for SPI mode
  206. HW_SPI_init.ClockRate = Marlin_speed[MIN(spiRate, 6)]; // put in the specified bit rate
  207. SSP_Init(LPC_SSP0, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers
  208. SSP_Cmd(LPC_SSP0, ENABLE); // start SSP0 running
  209. }
  210. void spiSend(uint8_t b) {
  211. while (!SSP_GetStatus(LPC_SSP0, SSP_STAT_TXFIFO_NOTFULL)); // wait for room in the buffer
  212. SSP_SendData(LPC_SSP0, b & 0x00FF);
  213. while (SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY)); // wait for it to finish
  214. }
  215. void spiSend(const uint8_t* buf, size_t n) {
  216. if (n == 0) return;
  217. for (uint16_t i = 0; i < n; i++) {
  218. while (!SSP_GetStatus(LPC_SSP0, SSP_STAT_TXFIFO_NOTFULL)); // wait for room in the buffer
  219. SSP_SendData(LPC_SSP0, buf[i] & 0x00FF);
  220. }
  221. while (SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY)); // wait for it to finish
  222. }
  223. void spiSend(uint32_t chan, byte b) {
  224. }
  225. void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
  226. }
  227. uint8_t get_one_byte() {
  228. // send a dummy byte so can clock in receive data
  229. SSP_SendData(LPC_SSP0,0x00FF);
  230. while (SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY)); // wait for it to finish
  231. return SSP_ReceiveData(LPC_SSP0) & 0x00FF;
  232. }
  233. // Read single byte from SPI
  234. uint8_t spiRec() {
  235. while (SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY) || SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY)) SSP_ReceiveData(LPC_SSP0); //flush the receive buffer
  236. return get_one_byte();
  237. }
  238. uint8_t spiRec(uint32_t chan) {
  239. return 0;
  240. }
  241. // Read from SPI into buffer
  242. void spiRead(uint8_t*buf, uint16_t nbyte) {
  243. while (SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY) || SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY)) SSP_ReceiveData(LPC_SSP0); //flush the receive buffer
  244. if (nbyte == 0) return;
  245. for (int i = 0; i < nbyte; i++) {
  246. buf[i] = get_one_byte();
  247. }
  248. }
  249. // Write from buffer to SPI
  250. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  251. }
  252. #endif // ENABLED(LPC_SOFTWARE_SPI)
  253. #endif // TARGET_LPC1768