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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_L64XX
  28. #include "Delay.h"
  29. #include "../../core/serial.h"
  30. #include "../../libs/L64XX/L64XX_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. inline 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. inline 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. DELAY_NS(125); // Need more delay for fast CPUs
  55. b <<= 1; // little setup time
  56. b |= (READ(L6470_CHAIN_MISO_PIN) != 0);
  57. }
  58. DELAY_NS(125); // 10 cycles @ 84mhz
  59. return b;
  60. }
  61. /**
  62. * L64XX methods for SPI init and transfer
  63. */
  64. void L64XX_Marlin::spi_init() {
  65. OUT_WRITE(L6470_CHAIN_SS_PIN, HIGH);
  66. OUT_WRITE(L6470_CHAIN_SCK_PIN, HIGH);
  67. OUT_WRITE(L6470_CHAIN_MOSI_PIN, HIGH);
  68. SET_INPUT(L6470_CHAIN_MISO_PIN);
  69. #if PIN_EXISTS(L6470_BUSY)
  70. SET_INPUT(L6470_BUSY_PIN);
  71. #endif
  72. OUT_WRITE(L6470_CHAIN_MOSI_PIN, HIGH);
  73. }
  74. uint8_t L64XX_Marlin::transfer_single(uint8_t data, int16_t ss_pin) {
  75. // First device in chain has data sent last
  76. extDigitalWrite(ss_pin, LOW);
  77. DISABLE_ISRS(); // Disable interrupts during SPI transfer (can't allow partial command to chips)
  78. const uint8_t data_out = L6470_SpiTransfer_Mode_3(data);
  79. ENABLE_ISRS(); // Enable interrupts
  80. extDigitalWrite(ss_pin, HIGH);
  81. return data_out;
  82. }
  83. uint8_t L64XX_Marlin::transfer_chain(uint8_t data, int16_t ss_pin, uint8_t chain_position) {
  84. uint8_t data_out = 0;
  85. // first device in chain has data sent last
  86. extDigitalWrite(ss_pin, LOW);
  87. for (uint8_t i = L64XX::chain[0]; !L64xxManager.spi_abort && i >= 1; i--) { // Send data unless aborted
  88. DISABLE_ISRS(); // Disable interrupts during SPI transfer (can't allow partial command to chips)
  89. const uint8_t temp = L6470_SpiTransfer_Mode_3(uint8_t(i == chain_position ? data : dSPIN_NOP));
  90. ENABLE_ISRS(); // Enable interrupts
  91. if (i == chain_position) data_out = temp;
  92. }
  93. extDigitalWrite(ss_pin, HIGH);
  94. return data_out;
  95. }
  96. /**
  97. * Platform-supplied L6470 buffer transfer method
  98. */
  99. void L64XX_Marlin::transfer(uint8_t L6470_buf[], const uint8_t length) {
  100. // First device in chain has its data sent last
  101. if (spi_active) { // Interrupted SPI transfer so need to
  102. WRITE(L6470_CHAIN_SS_PIN, HIGH); // guarantee min high of 650ns
  103. DELAY_US(1);
  104. }
  105. WRITE(L6470_CHAIN_SS_PIN, LOW);
  106. for (uint8_t i = length; i >= 1; i--)
  107. L6470_SpiTransfer_Mode_3(uint8_t(L6470_buf[i]));
  108. WRITE(L6470_CHAIN_SS_PIN, HIGH);
  109. }
  110. #pragma GCC reset_options
  111. #endif // HAS_L64XX