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

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