Browse Source

Switch to TMC2130 compatible hardware SPI & misc (#10299)

Bob-the-Kuhn 6 years ago
parent
commit
902c885782

+ 25
- 24
Marlin/src/HAL/HAL_DUE/HAL_Due.cpp View File

@@ -1,23 +1,21 @@
1
-/* **************************************************************************
2
-
3
- Marlin 3D Printer Firmware
4
- Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
- Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6
-
7
- This program is free software: you can redistribute it and/or modify
8
- it under the terms of the GNU General Public License as published by
9
- the Free Software Foundation, either version 3 of the License, or
10
- (at your option) any later version.
11
-
12
- This program is distributed in the hope that it will be useful,
13
- but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
- GNU General Public License for more details.
16
-
17
- You should have received a copy of the GNU General Public License
18
- along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
-****************************************************************************/
20
-
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
+ */
21 19
 
22 20
 /**
23 21
  * Description: HAL for Arduino Due and compatible (SAM3X8E)
@@ -77,6 +75,9 @@ uint16_t HAL_adc_result;
77 75
 // HAL initialization task
78 76
 void HAL_init(void) {
79 77
   // Initialize the USB stack
78
+  #if ENABLED(SDSUPPORT)
79
+    OUT_WRITE(SDSS, HIGH);  // Try to set SDSS inactive before any other SPI users start up
80
+  #endif
80 81
   usb_task_init();
81 82
 }
82 83
 
@@ -86,10 +87,10 @@ void HAL_idletask(void) {
86 87
   usb_task_idle();
87 88
 }
88 89
 
89
-// disable interrupts
90
+// Disable interrupts
90 91
 void cli(void) { noInterrupts(); }
91 92
 
92
-// enable interrupts
93
+// Enable interrupts
93 94
 void sei(void) { interrupts(); }
94 95
 
95 96
 void HAL_clear_reset_source(void) { }
@@ -106,7 +107,7 @@ uint8_t HAL_get_reset_source(void) {
106 107
 }
107 108
 
108 109
 void _delay_ms(const int delay_ms) {
109
-  // todo: port for Due?
110
+  // Todo: port for Due?
110 111
   delay(delay_ms);
111 112
 }
112 113
 
@@ -114,7 +115,7 @@ extern "C" {
114 115
   extern unsigned int _ebss; // end of bss section
115 116
 }
116 117
 
117
-// return free memory between end of heap (or end bss) and whatever is current
118
+// Return free memory between end of heap (or end bss) and whatever is current
118 119
 int freeMemory() {
119 120
   int free_memory, heap_end = (int)_sbrk(0);
120 121
   return (int)&free_memory - (heap_end ? heap_end : (int)&_ebss);

+ 22
- 24
Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp View File

@@ -486,7 +486,7 @@
486 486
 
487 487
   static void spiRxBlockX(uint8_t* buf, uint32_t todo) {
488 488
     do {
489
-      *buf++ = spiTransferRx(0xff);
489
+      *buf++ = spiTransferRx(0xFF);
490 490
     } while (--todo);
491 491
   }
492 492
 
@@ -629,7 +629,7 @@
629 629
     bool spiInitMaded = false;
630 630
 
631 631
     void spiBegin() {
632
-      if(spiInitMaded == false) {
632
+      if (spiInitMaded == false) {
633 633
         // Configure SPI pins
634 634
         PIO_Configure(
635 635
            g_APinDescription[SCK_PIN].pPort,
@@ -681,8 +681,8 @@
681 681
     }
682 682
 
683 683
     void spiInit(uint8_t spiRate) {
684
-      if(spiInitMaded == false) {
685
-        if(spiRate > 6) spiRate = 1;
684
+      if (spiInitMaded == false) {
685
+        if (spiRate > 6) spiRate = 1;
686 686
 
687 687
         #if MB(ALLIGATOR)
688 688
           // Set SPI mode 1, clock, select not active after transfer, with delay between transfers
@@ -789,7 +789,7 @@
789 789
     }
790 790
 
791 791
     // Read from SPI into buffer
792
-    void spiRead(uint8_t*buf, uint16_t nbyte) {
792
+    void spiRead(uint8_t* buf, uint16_t nbyte) {
793 793
       if (nbyte-- == 0) return;
794 794
 
795 795
       for (int i = 0; i < nbyte; i++) {
@@ -830,38 +830,37 @@
830 830
     #define SPI_MODE_2_DUE_HW 0
831 831
     #define SPI_MODE_3_DUE_HW 1
832 832
 
833
-    void spiInit(uint8_t spiRate = 6 ) {  // default to slowest rate if not specified)
833
+    void spiInit(uint8_t spiRate=6) {  // Default to slowest rate if not specified)
834 834
       // 8.4 MHz, 4 MHz, 2 MHz, 1 MHz, 0.5 MHz, 0.329 MHz, 0.329 MHz
835 835
       int spiDueDividors[] = { 10, 21, 42, 84, 168, 255, 255 };
836
-      if(spiRate > 6) spiRate = 1;
836
+      if (spiRate > 6) spiRate = 1;
837 837
 
838
-          /* enable PIOA and SPI0 */
838
+      // Enable PIOA and SPI0
839 839
       REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
840 840
 
841
-      /* disable PIO on A26 and A27 */
841
+      // Disable PIO on A26 and A27
842 842
       REG_PIOA_PDR = 0x0c000000;
843 843
       OUT_WRITE(SDSS, 1);
844 844
 
845
-      /* reset SPI0 (from sam lib) */
845
+      // Reset SPI0 (from sam lib)
846 846
       SPI0->SPI_CR = SPI_CR_SPIDIS;
847 847
       SPI0->SPI_CR = SPI_CR_SWRST;
848 848
       SPI0->SPI_CR = SPI_CR_SWRST;
849 849
       SPI0->SPI_CR = SPI_CR_SPIEN;
850 850
 
851
-
852
-      /* master mode, no fault detection, chip select 0 */
853
-      SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PCSDEC | SPI_MR_MODFDIS;
854
-
855
-      /* SPI mode 0, 8 Bit data transfer, baud rate */
856
-      SPI0->SPI_CSR[0] = SPI_CSR_SCBR(spiDueDividors[spiRate]) | SPI_MODE_0_DUE_HW;
851
+      // TMC2103 compatible setup
852
+      // Master mode, no fault detection, PCS bits in data written to TDR select CSR register
853
+      SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS;
854
+      // SPI mode 0, 8 Bit data transfer, baud rate
855
+      SPI0->SPI_CSR[3] = SPI_CSR_SCBR(spiDueDividors[spiRate]) | SPI_CSR_CSAAT | SPI_MODE_0_DUE_HW;  // use same CSR as TMC2130
857 856
     }
858 857
 
859 858
     static uint8_t spiTransfer(uint8_t data) {
860 859
 
861
-      /* wait until tx register is empty */
860
+      // Wait until tx register is empty
862 861
       while( (SPI0->SPI_SR & SPI_SR_TDRE) == 0 );
863
-      /* send data */
864
-      SPI0->SPI_TDR = (uint32_t)data; // | SPI_PCS(0xF);
862
+      // Send data
863
+      SPI0->SPI_TDR = (uint32_t)data | 0x00070000UL;  // Add TMC2130 PCS bits to every byte
865 864
 
866 865
       // wait for transmit register empty
867 866
       while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
@@ -877,15 +876,14 @@
877 876
     }
878 877
 
879 878
     uint8_t spiRec() {
880
-      uint8_t data = spiTransfer(0xff);
879
+      uint8_t data = spiTransfer(0xFF);
881 880
       return data;
882 881
     }
883 882
 
884
-    void spiRead(uint8_t*buf, uint16_t nbyte) {
883
+    void spiRead(uint8_t* buf, uint16_t nbyte) {
885 884
       if (nbyte == 0) return;
886
-      for (int i = 0; i < nbyte; i++) {
887
-        buf[i] = spiTransfer(0xff);
888
-      }
885
+      for (int i = 0; i < nbyte; i++)
886
+        buf[i] = spiTransfer(0xFF);
889 887
     }
890 888
 
891 889
     void spiSend(uint8_t data) {

+ 8
- 0
Marlin/src/HAL/HAL_DUE/SanityCheck_Due.h View File

@@ -27,3 +27,11 @@
27 27
 /**
28 28
  * Require gcc 4.7 or newer (first included with Arduino 1.6.8) for C++11 features.
29 29
  */
30
+
31
+#if ENABLED(SDSUPPORT) && ENABLED(HAVE_TMC2130)
32
+  #if ENABLED(DUE_SOFTWARE_SPI) && !ENABLED(TMC_USE_SW_SPI)
33
+    #error "DUE software SPI is incompatible with TMC2130 hardware SPI. Enable TMC_USE_SW_SPI to fix."
34
+  #elif !ENABLED(DUE_SOFTWARE_SPI) && ENABLED(TMC_USE_SW_SPI)
35
+    #error "DUE hardware SPI is incompatible with TMC2130 software SPI. Disable TMC_USE_SW_SPI to fix."
36
+  #endif
37
+#endif

+ 6
- 5
Marlin/src/Marlin.cpp View File

@@ -892,14 +892,15 @@ void setup() {
892 892
  *  - Call LCD update
893 893
  */
894 894
 void loop() {
895
-  if (commands_in_queue < BUFSIZE) get_available_commands();
896 895
 
897 896
   #if ENABLED(SDSUPPORT)
898 897
     card.checkautostart(false);
899 898
   #endif
900 899
 
901
-  advance_command_queue();
902
-
903
-  endstops.report_state();
904
-  idle();
900
+  for (;;) {
901
+    if (commands_in_queue < BUFSIZE) get_available_commands();
902
+    advance_command_queue();
903
+    endstops.report_state();
904
+    idle();
905
+  }
905 906
 }

+ 6
- 5
Marlin/src/sd/Sd2Card.cpp View File

@@ -249,15 +249,16 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
249 249
     watchdog_reset();
250 250
   #endif
251 251
 
252
-  // set pin modes
253
-  pinMode(chipSelectPin_, OUTPUT); // Solution for #8746 by @benlye
252
+  // Set pin modes
253
+  digitalWrite(chipSelectPin_, HIGH);  // For some CPUs pinMode can write the wrong data so init desired data value first
254
+  pinMode(chipSelectPin_, OUTPUT);     // Solution for #8746 by @benlye
254 255
   spiBegin();
255 256
 
256
-  // set SCK rate for initialization commands
257
+  // Set SCK rate for initialization commands
257 258
   spiRate_ = SPI_SD_INIT_RATE;
258 259
   spiInit(spiRate_);
259 260
 
260
-  // must supply min of 74 clock cycles with CS high.
261
+  // Must supply min of 74 clock cycles with CS high.
261 262
   for (uint8_t i = 0; i < 10; i++) spiSend(0xFF);
262 263
 
263 264
   // Initialization can cause the watchdog to timeout, so reinit it here
@@ -265,7 +266,7 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
265 266
     watchdog_reset();
266 267
   #endif
267 268
 
268
-  // command to go idle in SPI mode
269
+  // Command to go idle in SPI mode
269 270
   while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
270 271
     if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
271 272
       error(SD_CARD_ERROR_CMD0);

Loading…
Cancel
Save