Browse Source

HAL SPI pin init cleanup

Scott Lahteine 5 years ago
parent
commit
26de051e92

+ 22
- 29
Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp View File

@@ -21,12 +21,12 @@
21 21
  */
22 22
 
23 23
 /**
24
- * Originally from Arduino Sd2Card Library
24
+ * Adapted from Arduino Sd2Card Library
25 25
  * Copyright (C) 2009 by William Greiman
26 26
  */
27 27
 
28 28
 /**
29
- * Description: HAL for AVR - SPI functions
29
+ * HAL for AVR - SPI functions
30 30
  */
31 31
 
32 32
 #ifdef __AVR__
@@ -37,35 +37,24 @@
37 37
 
38 38
 #include "../../inc/MarlinConfig.h"
39 39
 
40
-// --------------------------------------------------------------------------
41
-// Public Variables
42
-// --------------------------------------------------------------------------
43
-
44
-
45
-// --------------------------------------------------------------------------
46
-// Public functions
47
-// --------------------------------------------------------------------------
48
-
49
-void spiBegin (void) {
50
-  SET_OUTPUT(SS_PIN);
51
-  WRITE(SS_PIN, HIGH);
40
+void spiBegin(void) {
41
+  OUT_WRITE(SS_PIN, HIGH);
52 42
   SET_OUTPUT(SCK_PIN);
53 43
   SET_INPUT(MISO_PIN);
54 44
   SET_OUTPUT(MOSI_PIN);
55 45
 
56 46
   #if DISABLED(SOFTWARE_SPI)
57 47
     // SS must be in output mode even it is not chip select
58
-    SET_OUTPUT(SS_PIN);
48
+    //SET_OUTPUT(SS_PIN);
59 49
     // set SS high - may be chip select for another SPI device
60
-    #if SET_SPI_SS_HIGH
61
-      WRITE(SS_PIN, HIGH);
62
-    #endif  // SET_SPI_SS_HIGH
50
+    //#if SET_SPI_SS_HIGH
51
+      //WRITE(SS_PIN, HIGH);
52
+    //#endif
63 53
     // set a default rate
64 54
     spiInit(1);
65
-  #endif  // SOFTWARE_SPI
55
+  #endif
66 56
 }
67 57
 
68
-
69 58
 #if DISABLED(SOFTWARE_SPI, FORCE_SOFT_SPI)
70 59
 
71 60
   //------------------------------------------------------------------------------
@@ -190,22 +179,26 @@ void spiBegin (void) {
190 179
   }
191 180
 
192 181
 
193
-#else
182
+#else // SOFTWARE_SPI || FORCE_SOFT_SPI
183
+
184
+  //------------------------------------------------------------------------------
185
+  // Software SPI
186
+  //------------------------------------------------------------------------------
194 187
 
195
-  /** nop to tune soft SPI timing */
188
+  // nop to tune soft SPI timing
196 189
   #define nop asm volatile ("\tnop\n")
197 190
 
198
-  /** Set SPI rate */
191
+  // Set SPI rate
199 192
   void spiInit(uint8_t spiRate) {
200 193
     UNUSED(spiRate);  // nothing to do
201 194
   }
202 195
 
203
-  /** Begin SPI transaction, set clock, bit order, data mode */
196
+  // Begin SPI transaction, set clock, bit order, data mode
204 197
   void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
205 198
     UNUSED(spiBeginTransaction);  // nothing to do
206 199
   }
207 200
 
208
-  /** Soft SPI receive byte */
201
+  // Soft SPI receive byte
209 202
   uint8_t spiRec() {
210 203
     uint8_t data = 0;
211 204
     // no interrupts during byte receive - about 8µs
@@ -230,13 +223,13 @@ void spiBegin (void) {
230 223
     return data;
231 224
   }
232 225
 
233
-  /** Soft SPI read data */
226
+  // Soft SPI read data
234 227
   void spiRead(uint8_t* buf, uint16_t nbyte) {
235 228
     for (uint16_t i = 0; i < nbyte; i++)
236 229
       buf[i] = spiRec();
237 230
   }
238 231
 
239
-  /** Soft SPI send byte */
232
+  // Soft SPI send byte
240 233
   void spiSend(uint8_t data) {
241 234
     // no interrupts during byte send - about 8µs
242 235
     cli();
@@ -257,13 +250,13 @@ void spiBegin (void) {
257 250
     sei();
258 251
   }
259 252
 
260
-  /** Soft SPI send block */
253
+  // Soft SPI send block
261 254
   void spiSendBlock(uint8_t token, const uint8_t* buf) {
262 255
     spiSend(token);
263 256
     for (uint16_t i = 0; i < 512; i++)
264 257
       spiSend(buf[i]);
265 258
   }
266 259
 
267
-#endif // SOFTWARE_SPI, FORCE_SOFT_SPI
260
+#endif // SOFTWARE_SPI || FORCE_SOFT_SPI
268 261
 
269 262
 #endif // __AVR__

+ 2
- 4
Marlin/src/HAL/HAL_TEENSY31_32/HAL_spi_Teensy.cpp View File

@@ -38,14 +38,12 @@ void spiBegin(void) {
38 38
   #if !PIN_EXISTS(SS)
39 39
     #error "SS_PIN not defined!"
40 40
   #endif
41
-  SET_OUTPUT(SS_PIN);
42
-  WRITE(SS_PIN, HIGH);
41
+  OUT_WRITE(SS_PIN, HIGH);
43 42
   SET_OUTPUT(SCK_PIN);
44 43
   SET_INPUT(MISO_PIN);
45 44
   SET_OUTPUT(MOSI_PIN);
46 45
 
47
-  //#if DISABLED(SOFTWARE_SPI)
48
-  #if 0
46
+  #if 0 && DISABLED(SOFTWARE_SPI)
49 47
     // set SS high - may be chip select for another SPI device
50 48
     #if SET_SPI_SS_HIGH
51 49
       WRITE(SS_PIN, HIGH);

+ 2
- 2
Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.h View File

@@ -23,7 +23,7 @@
23 23
 
24 24
 #include <Servo.h>
25 25
 
26
-// Inherit and expand on the official library
26
+// Inherit and expand on core Servo library
27 27
 class libServo : public Servo {
28 28
   public:
29 29
     int8_t attach(const int pin);
@@ -32,5 +32,5 @@ class libServo : public Servo {
32 32
   private:
33 33
      uint16_t min_ticks;
34 34
      uint16_t max_ticks;
35
-     uint8_t servoIndex;               // index into the channel data for this servo
35
+     uint8_t servoIndex; // Index into the channel data for this servo
36 36
 };

+ 44
- 35
Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp View File

@@ -1,3 +1,24 @@
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
+ */
1 22
 #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
2 23
 
3 24
 #include "HAL.h"
@@ -8,32 +29,27 @@
8 29
 
9 30
 static SPISettings spiConfig;
10 31
 
11
-// Standard SPI functions
12
-/** Initialize SPI bus */
13 32
 void spiBegin(void) {
14 33
   #if !PIN_EXISTS(SS)
15 34
     #error SS_PIN not defined!
16 35
   #endif
17
-  SET_OUTPUT(SS_PIN);
18
-  WRITE(SS_PIN, HIGH);
36
+  OUT_WRITE(SS_PIN, HIGH);
19 37
   SET_OUTPUT(SCK_PIN);
20 38
   SET_INPUT(MISO_PIN);
21 39
   SET_OUTPUT(MOSI_PIN);
22 40
 
23
-  //#if DISABLED(SOFTWARE_SPI)
24
-  #if 0
41
+  #if 0 && DISABLED(SOFTWARE_SPI)
25 42
     // set SS high - may be chip select for another SPI device
26 43
     #if SET_SPI_SS_HIGH
27 44
       WRITE(SS_PIN, HIGH);
28
-    #endif  // SET_SPI_SS_HIGH
45
+    #endif
29 46
     // set a default rate
30 47
     spiInit(SPI_HALF_SPEED); // 1
31
-  #endif  // SOFTWARE_SPI
48
+  #endif
32 49
 }
33 50
 
34
-/** Configure SPI for specified SPI speed */
35 51
 void spiInit(uint8_t spiRate) {
36
-  // Use datarates Marlin uses
52
+  // Use Marlin data-rates
37 53
   uint32_t clock;
38 54
   switch (spiRate) {
39 55
   case SPI_FULL_SPEED:    clock = 10000000; break;
@@ -49,44 +65,39 @@ void spiInit(uint8_t spiRate) {
49 65
   SPI.begin();
50 66
 }
51 67
 
52
-//------------------------------------------------------------------------------
53
-/** SPI receive a byte */
54 68
 uint8_t spiRec(void) {
55 69
   SPI.beginTransaction(spiConfig);
56 70
   uint8_t returnByte = SPI.transfer(0xFF);
57 71
   SPI.endTransaction();
58 72
   return returnByte;
59
-//  SPDR = 0xFF;
60
-//  while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
61
-//  return SPDR;
73
+  //SPDR = 0xFF;
74
+  //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
75
+  //return SPDR;
62 76
 }
63
-//------------------------------------------------------------------------------
64
-/** SPI read data  */
77
+
65 78
 void spiRead(uint8_t* buf, uint16_t nbyte) {
66 79
   SPI.beginTransaction(spiConfig);
67 80
   SPI.transfer(buf, nbyte);
68 81
   SPI.endTransaction();
69
-//if (nbyte-- == 0) return;
70
-//  SPDR = 0xFF;
71
-//for (uint16_t i = 0; i < nbyte; i++) {
72
-//  while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
73
-//  buf[i] = SPDR;
74
-//  SPDR = 0xFF;
75
-//}
76
-//while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
77
-//buf[nbyte] = SPDR;
82
+  //if (nbyte-- == 0) return;
83
+  //  SPDR = 0xFF;
84
+  //for (uint16_t i = 0; i < nbyte; i++) {
85
+  //  while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
86
+  //  buf[i] = SPDR;
87
+  //  SPDR = 0xFF;
88
+  //}
89
+  //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
90
+  //buf[nbyte] = SPDR;
78 91
 }
79
-//------------------------------------------------------------------------------
80
-/** SPI send a byte */
92
+
81 93
 void spiSend(uint8_t b) {
82 94
   SPI.beginTransaction(spiConfig);
83 95
   SPI.transfer(b);
84 96
   SPI.endTransaction();
85
-//  SPDR = b;
86
-//  while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
97
+  //SPDR = b;
98
+  //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
87 99
 }
88
-//------------------------------------------------------------------------------
89
-/** SPI send block  */
100
+
90 101
 void spiSendBlock(uint8_t token, const uint8_t* buf) {
91 102
   SPI.beginTransaction(spiConfig);
92 103
   SPDR = token;
@@ -100,11 +111,9 @@ void spiSendBlock(uint8_t token, const uint8_t* buf) {
100 111
   SPI.endTransaction();
101 112
 }
102 113
 
103
-
104
-/** Begin SPI transaction, set clock, bit order, data mode */
114
+// Begin SPI transaction, set clock, bit order, data mode
105 115
 void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
106 116
   spiConfig = SPISettings(spiClock, bitOrder, dataMode);
107
-
108 117
   SPI.beginTransaction(spiConfig);
109 118
 }
110 119
 

+ 0
- 1
Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp View File

@@ -20,7 +20,6 @@
20 20
  *
21 21
  */
22 22
 
23
-
24 23
 /**
25 24
  * Teensy3.5 __MK64FX512__
26 25
  * Teensy3.6 __MK66FX1M0__

+ 1
- 2
Marlin/src/HAL/shared/SpiEeprom.cpp View File

@@ -115,5 +115,4 @@ void eeprom_update_block(const void* src, void* eeprom_address, size_t n) {
115 115
   delay(7);   // wait for page write to complete
116 116
 }
117 117
 
118
-
119
-#endif // ENABLED(SPI_EEPROM)
118
+#endif // SPI_EEPROM

Loading…
Cancel
Save