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_L6470.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 L6470 SPI functions originally from Arduino Sd2Card Library
  24. * Copyright (c) 2009 by William Greiman
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if HAS_DRIVER(L6470)
  28. #include "Delay.h"
  29. #include "../../core/serial.h"
  30. #include "../../libs/L6470/L6470_Marlin.h"
  31. // Make sure GCC optimizes this file.
  32. // Note that this line triggers a bug in GCC which is fixed by casting.
  33. // See the note below.
  34. #pragma GCC optimize (3)
  35. // run at ~4Mhz
  36. uint8_t L6470_SpiTransfer_Mode_0(uint8_t b) { // using Mode 0
  37. for (uint8_t bits = 8; bits--;) {
  38. WRITE(L6470_CHAIN_MOSI_PIN, b & 0x80);
  39. b <<= 1; // little setup time
  40. WRITE(L6470_CHAIN_SCK_PIN, HIGH);
  41. DELAY_NS(125); // 10 cycles @ 84mhz
  42. b |= (READ(L6470_CHAIN_MISO_PIN) != 0);
  43. WRITE(L6470_CHAIN_SCK_PIN, LOW);
  44. DELAY_NS(125); // 10 cycles @ 84mhz
  45. }
  46. return b;
  47. }
  48. uint8_t L6470_SpiTransfer_Mode_3(uint8_t b) { // using Mode 3
  49. for (uint8_t bits = 8; bits--;) {
  50. WRITE(L6470_CHAIN_SCK_PIN, LOW);
  51. WRITE(L6470_CHAIN_MOSI_PIN, b & 0x80);
  52. DELAY_NS(125); // 10 cycles @ 84mhz
  53. WRITE(L6470_CHAIN_SCK_PIN, HIGH);
  54. b <<= 1; // little setup time
  55. b |= (READ(L6470_CHAIN_MISO_PIN) != 0);
  56. }
  57. DELAY_NS(125); // 10 cycles @ 84mhz
  58. return b;
  59. }
  60. /**
  61. * The following are weak-linked and defined as do-nothing
  62. * functions by the L6470-Arduino library. They must be
  63. * defined by the client (Marlin) to provide an SPI interface.
  64. */
  65. uint8_t L6470_transfer(uint8_t data, int16_t ss_pin, const uint8_t chain_position) {
  66. uint8_t data_out = 0;
  67. // first device in chain has data sent last
  68. extDigitalWrite(ss_pin, LOW);
  69. for (uint8_t i = L6470::chain[0]; (i >= 1) && !spi_abort; i--) { // stop sending data if spi_abort is active
  70. DISABLE_ISRS(); // disable interrupts during SPI transfer (can't allow partial command to chips)
  71. uint8_t temp = L6470_SpiTransfer_Mode_3(uint8_t(i == chain_position ? data : dSPIN_NOP));
  72. ENABLE_ISRS(); // enable interrupts
  73. if (i == chain_position) data_out = temp;
  74. }
  75. extDigitalWrite(ss_pin, HIGH);
  76. return data_out;
  77. }
  78. void L6470_transfer(uint8_t L6470_buf[], const uint8_t length) {
  79. // first device in chain has data sent last
  80. if (spi_active) { // interrupted SPI transfer so need to
  81. WRITE(L6470_CHAIN_SS_PIN, HIGH); // guarantee min high of 650nS
  82. DELAY_US(1);
  83. }
  84. WRITE(L6470_CHAIN_SS_PIN, LOW);
  85. for (uint8_t i = length; i >= 1; i--)
  86. L6470_SpiTransfer_Mode_3(uint8_t(L6470_buf[i]));
  87. WRITE(L6470_CHAIN_SS_PIN, HIGH);
  88. }
  89. void L6470_spi_init() {
  90. OUT_WRITE(L6470_CHAIN_SS_PIN, HIGH);
  91. OUT_WRITE(L6470_CHAIN_SCK_PIN, HIGH);
  92. OUT_WRITE(L6470_CHAIN_MOSI_PIN, HIGH);
  93. SET_INPUT(L6470_CHAIN_MISO_PIN);
  94. #if PIN_EXISTS(L6470_BUSY)
  95. SET_INPUT(L6470_BUSY_PIN);
  96. #endif
  97. OUT_WRITE(L6470_CHAIN_MOSI_PIN, HIGH);
  98. }
  99. #pragma GCC reset_options
  100. #endif // HAS_DRIVER(L6470)