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.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #pragma once
  23. #include "../../shared/HAL_SPI.h"
  24. #include <stdint.h>
  25. #include <lpc17xx_ssp.h>
  26. #include <lpc17xx_gpdma.h>
  27. //#define MSBFIRST 1
  28. #define SPI_MODE0 0
  29. #define SPI_MODE1 1
  30. #define SPI_MODE2 2
  31. #define SPI_MODE3 3
  32. #define DATA_SIZE_8BIT SSP_DATABIT_8
  33. #define DATA_SIZE_16BIT SSP_DATABIT_16
  34. #define SPI_CLOCK_MAX_TFT 30000000UL
  35. #define SPI_CLOCK_DIV2 8333333 //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
  36. #define SPI_CLOCK_DIV4 4166667 //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
  37. #define SPI_CLOCK_DIV8 2083333 //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
  38. #define SPI_CLOCK_DIV16 1000000 //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
  39. #define SPI_CLOCK_DIV32 500000 //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
  40. #define SPI_CLOCK_DIV64 250000 //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
  41. #define SPI_CLOCK_DIV128 125000 //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
  42. #define SPI_CLOCK_MAX SPI_CLOCK_DIV2
  43. #define BOARD_NR_SPI 2
  44. //#define BOARD_SPI1_NSS_PIN PA4 ?!
  45. #define BOARD_SPI1_SCK_PIN P0_15
  46. #define BOARD_SPI1_MISO_PIN P0_17
  47. #define BOARD_SPI1_MOSI_PIN P0_18
  48. //#define BOARD_SPI2_NSS_PIN PB12 ?!
  49. #define BOARD_SPI2_SCK_PIN P0_07
  50. #define BOARD_SPI2_MISO_PIN P0_08
  51. #define BOARD_SPI2_MOSI_PIN P0_09
  52. class SPISettings {
  53. public:
  54. SPISettings(uint32_t spiRate, int inBitOrder, int inDataMode) {
  55. init_AlwaysInline(spiRate2Clock(spiRate), inBitOrder, inDataMode, DATA_SIZE_8BIT);
  56. }
  57. SPISettings(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
  58. if (__builtin_constant_p(inClock))
  59. init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize);
  60. else
  61. init_MightInline(inClock, inBitOrder, inDataMode, inDataSize);
  62. }
  63. SPISettings() {
  64. init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
  65. }
  66. //uint32_t spiRate() const { return spi_speed; }
  67. static inline uint32_t spiRate2Clock(uint32_t spiRate) {
  68. uint32_t Marlin_speed[7]; // CPSR is always 2
  69. Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
  70. Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
  71. Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
  72. Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
  73. Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
  74. Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
  75. Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
  76. return Marlin_speed[spiRate > 6 ? 6 : spiRate];
  77. }
  78. private:
  79. void init_MightInline(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
  80. init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize);
  81. }
  82. void init_AlwaysInline(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) __attribute__((__always_inline__)) {
  83. clock = inClock;
  84. bitOrder = inBitOrder;
  85. dataMode = inDataMode;
  86. dataSize = inDataSize;
  87. }
  88. //uint32_t spi_speed;
  89. uint32_t clock;
  90. uint32_t dataSize;
  91. //uint32_t clockDivider;
  92. uint8_t bitOrder;
  93. uint8_t dataMode;
  94. LPC_SSP_TypeDef *spi_d;
  95. friend class SPIClass;
  96. };
  97. /**
  98. * @brief Wirish SPI interface.
  99. *
  100. * This is the same interface is available across HAL
  101. *
  102. * This implementation uses software slave management, so the caller
  103. * is responsible for controlling the slave select line.
  104. */
  105. class SPIClass {
  106. public:
  107. /**
  108. * @param spiPortNumber Number of the SPI port to manage.
  109. */
  110. SPIClass(uint8_t spiPortNumber);
  111. /**
  112. * Init using pins
  113. */
  114. SPIClass(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel = (pin_t)-1);
  115. /**
  116. * Select and configure the current selected SPI device to use
  117. */
  118. void begin();
  119. /**
  120. * Disable the current SPI device
  121. */
  122. void end();
  123. void beginTransaction(const SPISettings&);
  124. void endTransaction() {}
  125. // Transfer using 1 "Data Size"
  126. uint8_t transfer(uint16_t data);
  127. // Transfer 2 bytes in 8 bit mode
  128. uint16_t transfer16(uint16_t data);
  129. void send(uint8_t data);
  130. uint16_t read();
  131. void read(uint8_t *buf, uint32_t len);
  132. void dmaSend(void *buf, uint16_t length, bool minc);
  133. /**
  134. * @brief Sets the number of the SPI peripheral to be used by
  135. * this HardwareSPI instance.
  136. *
  137. * @param spi_num Number of the SPI port. 1-2 in low density devices
  138. * or 1-3 in high density devices.
  139. */
  140. void setModule(uint8_t device);
  141. void setClock(uint32_t clock);
  142. void setBitOrder(uint8_t bitOrder);
  143. void setDataMode(uint8_t dataMode);
  144. void setDataSize(uint32_t ds);
  145. inline uint32_t getDataSize() { return _currentSetting->dataSize; }
  146. private:
  147. SPISettings _settings[BOARD_NR_SPI];
  148. SPISettings *_currentSetting;
  149. void updateSettings();
  150. };
  151. extern SPIClass SPI;