浏览代码

[2.0.x] LPC176x Serial cleanup (#11032)

Chris Pepper 6 年前
父节点
当前提交
0312c42f9d

+ 8
- 7
Marlin/src/HAL/HAL_LPC1768/HardwareSerial.cpp 查看文件

@@ -123,8 +123,8 @@ void HardwareSerial::begin(uint32_t baudrate) {
123 123
   Baudrate = baudrate;
124 124
 }
125 125
 
126
-int HardwareSerial::peek() {
127
-  int byte = -1;
126
+int16_t HardwareSerial::peek() {
127
+  int16_t byte = -1;
128 128
 
129 129
   // Temporarily lock out UART receive interrupts during this read so the UART receive
130 130
   // interrupt won't cause problems with the index values
@@ -139,8 +139,8 @@ int HardwareSerial::peek() {
139 139
   return byte;
140 140
 }
141 141
 
142
-int HardwareSerial::read() {
143
-  int byte = -1;
142
+int16_t HardwareSerial::read() {
143
+  int16_t byte = -1;
144 144
 
145 145
   // Temporarily lock out UART receive interrupts during this read so the UART receive
146 146
   // interrupt won't cause problems with the index values
@@ -201,7 +201,7 @@ size_t HardwareSerial::write(uint8_t send) {
201 201
   }
202 202
 #endif
203 203
 
204
-int HardwareSerial::available() {
204
+size_t HardwareSerial::available() {
205 205
   return (RxQueueWritePos + RX_BUFFER_SIZE - RxQueueReadPos) % RX_BUFFER_SIZE;
206 206
 }
207 207
 
@@ -210,16 +210,17 @@ void HardwareSerial::flush() {
210 210
   RxQueueReadPos = 0;
211 211
 }
212 212
 
213
-void HardwareSerial::printf(const char *format, ...) {
213
+size_t HardwareSerial::printf(const char *format, ...) {
214 214
   char RxBuffer[256];
215 215
   va_list vArgs;
216 216
   va_start(vArgs, format);
217 217
   int length = vsnprintf(RxBuffer, 256, format, vArgs);
218 218
   va_end(vArgs);
219 219
   if (length > 0 && length < 256) {
220
-    for (int i = 0; i < length; ++i)
220
+    for (size_t i = 0; i < (size_t)length; ++i)
221 221
       write(RxBuffer[i]);
222 222
   }
223
+  return length;
223 224
 }
224 225
 
225 226
 void HardwareSerial::IRQHandler() {

+ 4
- 104
Marlin/src/HAL/HAL_LPC1768/HardwareSerial.h 查看文件

@@ -72,120 +72,20 @@ public:
72 72
   }
73 73
 
74 74
   void begin(uint32_t baudrate);
75
-  int peek();
76
-  int read();
75
+  int16_t peek();
76
+  int16_t read();
77 77
   size_t write(uint8_t send);
78 78
   #if TX_BUFFER_SIZE > 0
79 79
     void flushTX();
80 80
   #endif
81
-  int available();
81
+  size_t available();
82 82
   void flush();
83
-  void printf(const char *format, ...);
83
+  size_t printf(const char *format, ...);
84 84
 
85 85
   operator bool() { return true; }
86 86
 
87 87
   void IRQHandler();
88 88
 
89
-  #define DEC 10
90
-  #define HEX 16
91
-  #define OCT 8
92
-  #define BIN 2
93
-
94
-  void print_bin(uint32_t value, uint8_t num_digits) {
95
-    uint32_t mask = 1 << (num_digits -1);
96
-    for (uint8_t i = 0; i < num_digits; i++) {
97
-      if (!(i % 4) && i)    printf(" ");
98
-      if (!(i % 16)  && i)  printf(" ");
99
-      if (value & mask)     printf("1");
100
-      else                  printf("0");
101
-      value <<= 1;
102
-    }
103
-  }
104
-
105
-  void print(const char value[]) {
106
-    printf("%s" , value);
107
-  }
108
-  void print(char value, int nbase = 0) {
109
-    if (nbase == BIN) print_bin(value,8);
110
-    else if (nbase == OCT) printf("%3o", value);
111
-    else if (nbase == HEX) printf("%2X", value);
112
-    else if (nbase == DEC ) printf("%d", value);
113
-    else printf("%c" , value);
114
-  }
115
-  void print(unsigned char value, int nbase = 0) {
116
-    if (nbase == BIN) print_bin(value,8);
117
-    else if (nbase == OCT) printf("%3o", value);
118
-    else if (nbase == HEX) printf("%2X", value);
119
-    else printf("%u" , value);
120
-  }
121
-  void print(int value, int nbase = 0) {
122
-    if (nbase == BIN) print_bin(value,16);
123
-    else if (nbase == OCT) printf("%6o", value);
124
-    else if (nbase == HEX) printf("%4X", value);
125
-    else printf("%d", value);
126
-  }
127
-  void print(unsigned int value, int nbase = 0) {
128
-    if (nbase == BIN) print_bin(value,16);
129
-    else if (nbase == OCT) printf("%6o", value);
130
-    else if (nbase == HEX) printf("%4X", value);
131
-    else printf("%u" , value);
132
-  }
133
-  void print(long value, int nbase = 0) {
134
-    if (nbase == BIN) print_bin(value,32);
135
-    else if (nbase == OCT) printf("%11o", value);
136
-    else if (nbase == HEX) printf("%8X", value);
137
-    else printf("%ld" , value);
138
-  }
139
-  void print(unsigned long value, int nbase = 0) {
140
-    if (nbase == BIN) print_bin(value,32);
141
-    else if (nbase == OCT) printf("%11o", value);
142
-    else if (nbase == HEX) printf("%8X", value);
143
-    else printf("%lu" , value);
144
-  }
145
-  void print(float value, int round = 6) {
146
-    printf("%f" , value);
147
-  }
148
-  void print(double value, int round = 6) {
149
-    printf("%f" , value );
150
-  }
151
-
152
-  void println(const char value[]) {
153
-    printf("%s\n" , value);
154
-  }
155
-  void println(char value, int nbase = 0) {
156
-    print(value, nbase);
157
-    println();
158
-  }
159
-  void println(unsigned char value, int nbase = 0) {
160
-    print(value, nbase);
161
-    println();
162
-  }
163
-  void println(int value, int nbase = 0) {
164
-    print(value, nbase);
165
-    println();
166
-  }
167
-  void println(unsigned int value, int nbase = 0) {
168
-    print(value, nbase);
169
-    println();
170
-  }
171
-  void println(long value, int nbase = 0) {
172
-    print(value, nbase);
173
-    println();
174
-  }
175
-  void println(unsigned long value, int nbase = 0) {
176
-    print(value, nbase);
177
-    println();
178
-  }
179
-  void println(float value, int round = 6) {
180
-    printf("%f\n" , value );
181
-  }
182
-  void println(double value, int round = 6) {
183
-    printf("%f\n" , value );
184
-  }
185
-  void println(void) {
186
-    print('\n');
187
-  }
188
-
189 89
 };
190 90
 
191 91
 #endif // MARLIN_SRC_HAL_HAL_SERIAL_H_

+ 5
- 2
Marlin/src/HAL/HAL_LPC1768/LPC1768_Servo.cpp 查看文件

@@ -60,9 +60,11 @@
60 60
  * unless DEACTIVATE_SERVOS_AFTER_MOVE is enabled and a MOVE command was issued.
61 61
  */
62 62
 
63
+#ifdef TARGET_LPC1768
64
+
63 65
 #include "../../inc/MarlinConfig.h"
64 66
 
65
-#if HAS_SERVOS && defined(TARGET_LPC1768)
67
+#if HAS_SERVOS
66 68
 
67 69
   #include "LPC1768_PWM.h"
68 70
   #include "LPC1768_Servo.h"
@@ -157,4 +159,5 @@
157 159
     }
158 160
   }
159 161
 
160
-#endif // HAS_SERVOS && TARGET_LPC1768
162
+#endif // HAS_SERVOS
163
+#endif // TARGET_LPC1768

+ 2
- 2
Marlin/src/HAL/HAL_LPC1768/SoftwareSPI.cpp 查看文件

@@ -29,10 +29,10 @@
29 29
  * For TARGET_LPC1768
30 30
  */
31 31
 
32
-#include "../../inc/MarlinConfig.h"
33
-
34 32
 #ifdef TARGET_LPC1768
35 33
 
34
+#include "../../inc/MarlinConfig.h"
35
+
36 36
 // --------------------------------------------------------------------------
37 37
 // Software SPI
38 38
 // --------------------------------------------------------------------------

+ 3
- 3
Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp 查看文件

@@ -253,7 +253,7 @@ void SoftwareSerial::end() {
253 253
 
254 254
 
255 255
 // Read data from buffer
256
-int SoftwareSerial::read() {
256
+int16_t SoftwareSerial::read() {
257 257
   if (!isListening()) return -1;
258 258
 
259 259
   // Empty buffer?
@@ -265,7 +265,7 @@ int SoftwareSerial::read() {
265 265
   return d;
266 266
 }
267 267
 
268
-int SoftwareSerial::available() {
268
+size_t SoftwareSerial::available() {
269 269
   if (!isListening()) return 0;
270 270
 
271 271
   return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
@@ -314,7 +314,7 @@ void SoftwareSerial::flush() {
314 314
   sei();
315 315
 }
316 316
 
317
-int SoftwareSerial::peek() {
317
+int16_t SoftwareSerial::peek() {
318 318
   if (!isListening())
319 319
     return -1;
320 320
 

+ 3
- 3
Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.h 查看文件

@@ -93,11 +93,11 @@ public:
93 93
   bool isListening() { return this == active_object; }
94 94
   bool stopListening();
95 95
   bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
96
-  int peek();
96
+  int16_t peek();
97 97
 
98 98
   virtual size_t write(uint8_t byte);
99
-  virtual int read();
100
-  virtual int available();
99
+  virtual int16_t read();
100
+  virtual size_t available();
101 101
   virtual void flush();
102 102
   operator bool() { return true; }
103 103
 

+ 3
- 0
Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c 查看文件

@@ -23,6 +23,8 @@
23 23
 // adapted from  I2C/master/master.c example
24 24
 //   https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
25 25
 
26
+#ifdef TARGET_LPC1768
27
+
26 28
 #include "../../../inc/MarlinConfigPre.h"
27 29
 
28 30
 #if MB(MKS_SBASE)
@@ -135,3 +137,4 @@ uint8_t digipot_mcp4451_send_byte(uint8_t data) {
135 137
 #endif
136 138
 
137 139
 #endif // MB(MKS_SBASE)
140
+#endif // TARGET_LPC1768

+ 73
- 119
Marlin/src/HAL/HAL_LPC1768/include/serial.h 查看文件

@@ -30,177 +30,131 @@
30 30
 
31 31
 #include <stdarg.h>
32 32
 #include <stdio.h>
33
+#include <Print.h>
33 34
 
34 35
 /**
35 36
  * Generic RingBuffer
36 37
  * T type of the buffer array
37 38
  * S size of the buffer (must be power of 2)
38
- *
39
- * todo: optimise
40 39
  */
40
+
41 41
 template <typename T, uint32_t S> class RingBuffer {
42 42
 public:
43
-  RingBuffer() { index_read = index_write = 0; }
44
-  uint32_t available() volatile { return buffer_mask & (index_write - index_read); }
45
-  uint32_t free() volatile      { return buffer_size - available(); }
46
-  bool empty() volatile         { return (buffer_mask & index_read) == (buffer_mask & index_write); }
47
-  bool full() volatile          { return index_read == buffer_mask & (index_write + 1); }
48
-  void clear() volatile         { index_read = index_write = 0; }
49
-  bool peek(T *value) volatile {
50
-    if (value == 0 || available() == 0)
51
-      return false;
52
-    *value = buffer[buffer_mask & index_read];
43
+  RingBuffer() {index_read = index_write = 0;}
44
+
45
+  uint32_t available() {return mask(index_write - index_read);}
46
+  uint32_t free() {return buffer_size - available();}
47
+  bool empty() {return index_read == index_write;}
48
+  bool full() {return next(index_write) == index_read;}
49
+  void clear() {index_read = index_write = 0;}
50
+
51
+  bool peek(T *const value) {
52
+    if (value == nullptr || empty()) return false;
53
+    *value = buffer[index_read];
53 54
     return true;
54 55
   }
55
-  int read() volatile {
56
-    if ((buffer_mask & index_read) == (buffer_mask & index_write)) return -1;
57
-    T val = buffer[buffer_mask & index_read];
58
-    ++index_read;
59
-    return val;
56
+
57
+  uint32_t read(T *const value) {
58
+    if (value == nullptr || empty()) return 0;
59
+    *value = buffer[index_read];
60
+    index_read = next(index_read);
61
+    return 1;
60 62
   }
61
-  bool write(T value) volatile {
62
-    uint32_t next_head = buffer_mask & (index_write + 1);
63
-    if (next_head != index_read) {
64
-      buffer[buffer_mask & index_write] = value;
65
-      index_write = next_head;
66
-      return true;
67
-    }
68
-    return false;
63
+
64
+  uint32_t write(T value) {
65
+    uint32_t next_head = next(index_write);
66
+    if (next_head == index_read) return 0;     // buffer full
67
+    buffer[index_write] = value;
68
+    index_write = next_head;
69
+    return 1;
69 70
   }
70 71
 
71 72
 private:
73
+  inline uint32_t mask(uint32_t val) {
74
+    return val & buffer_mask;
75
+  }
76
+
77
+  inline uint32_t next(uint32_t val) {
78
+    return mask(val + 1);
79
+  }
80
+
72 81
   static const uint32_t buffer_size = S;
73 82
   static const uint32_t buffer_mask = buffer_size - 1;
74
-  volatile T buffer[buffer_size];
83
+  T buffer[buffer_size];
75 84
   volatile uint32_t index_write;
76 85
   volatile uint32_t index_read;
77 86
 };
78 87
 
79
-class HalSerial {
88
+/**
89
+ *  Serial Interface Class
90
+ *  Data is injected directly into, and consumed from, the fifo buffers
91
+ */
92
+
93
+class HalSerial: public Print {
80 94
 public:
81 95
 
82 96
   #if ENABLED(EMERGENCY_PARSER)
83 97
     EmergencyParser::State emergency_state;
84 98
   #endif
85 99
 
86
-  HalSerial() { host_connected = false; }
100
+  HalSerial() : host_connected(false) { }
101
+  virtual ~HalSerial() { }
87 102
 
88
-  void begin(int32_t baud) {
89
-  }
103
+  operator bool() { return host_connected; }
104
+
105
+  void begin(int32_t baud) { }
90 106
 
91
-  int peek() {
107
+  int16_t peek() {
92 108
     uint8_t value;
93 109
     return receive_buffer.peek(&value) ? value : -1;
94 110
   }
95 111
 
96
-  int read() { return receive_buffer.read(); }
97
-
98
-  size_t write(char c) { return host_connected ? transmit_buffer.write((uint8_t)c) : 0; }
112
+  int16_t read() {
113
+    uint8_t value;
114
+    return receive_buffer.read(&value) ? value : -1;
115
+  }
99 116
 
100
-  operator bool() { return host_connected; }
117
+  size_t write(const uint8_t c) {
118
+    if (!host_connected) return 0;          // Do not fill buffer when host disconnected
119
+    while (transmit_buffer.write(c) == 0) { // Block until there is free room in buffer
120
+      if (!host_connected) return 0;        // Break infinite loop on host disconect
121
+    }
122
+    return 1;
123
+  }
101 124
 
102
-  uint16_t available() {
103
-    return (uint16_t)receive_buffer.available();
125
+  size_t available() {
126
+    return (size_t)receive_buffer.available();
104 127
   }
105 128
 
106
-  void flush() { receive_buffer.clear(); }
129
+  void flush() {
130
+    receive_buffer.clear();
131
+  }
107 132
 
108
-  uint8_t availableForWrite(void){
133
+  uint8_t availableForWrite(void) {
109 134
     return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free();
110 135
   }
111 136
 
112
-  void flushTX(void){
113
-    if (host_connected)
114
-      while (transmit_buffer.available()) { /* nada */ }
137
+  void flushTX(void) {
138
+    while (transmit_buffer.available() && host_connected) { /* nada */}
115 139
   }
116 140
 
117
-  void printf(const char *format, ...) {
141
+  size_t printf(const char *format, ...) {
118 142
     static char buffer[256];
119 143
     va_list vArgs;
120 144
     va_start(vArgs, format);
121 145
     int length = vsnprintf((char *) buffer, 256, (char const *) format, vArgs);
122 146
     va_end(vArgs);
147
+    size_t i = 0;
123 148
     if (length > 0 && length < 256) {
124
-      if (host_connected) {
125
-        for (int i = 0; i < length;) {
126
-          if (transmit_buffer.write(buffer[i])) {
127
-            ++i;
128
-          }
129
-        }
149
+      while (i < (size_t)length && host_connected) {
150
+        i += transmit_buffer.write(buffer[i]);
130 151
       }
131 152
     }
153
+    return i;
132 154
   }
133 155
 
134
-  #define DEC 10
135
-  #define HEX 16
136
-  #define OCT 8
137
-  #define BIN 2
138
-
139
-  void print_bin(uint32_t value, uint8_t num_digits) {
140
-    uint32_t mask = 1 << (num_digits -1);
141
-    for (uint8_t i = 0; i < num_digits; i++) {
142
-      if (!(i % 4) && i)    write(' ');
143
-      if (!(i % 16)  && i)  write(' ');
144
-      if (value & mask)     write('1');
145
-      else                  write('0');
146
-      value <<= 1;
147
-    }
148
-  }
149
-
150
-  void print(const char value[]) { printf("%s" , value); }
151
-  void print(char value, int nbase = 0) {
152
-    if (nbase == BIN) print_bin(value, 8);
153
-    else if (nbase == OCT) printf("%3o", value);
154
-    else if (nbase == HEX) printf("%2X", value);
155
-    else if (nbase == DEC ) printf("%d", value);
156
-    else printf("%c" , value);
157
-  }
158
-  void print(unsigned char value, int nbase = 0) {
159
-    if (nbase == BIN) print_bin(value, 8);
160
-    else if (nbase == OCT) printf("%3o", value);
161
-    else if (nbase == HEX) printf("%2X", value);
162
-    else printf("%u" , value);
163
-  }
164
-  void print(int value, int nbase = 0) {
165
-    if (nbase == BIN) print_bin(value, 16);
166
-    else if (nbase == OCT) printf("%6o", value);
167
-    else if (nbase == HEX) printf("%4X", value);
168
-    else printf("%d", value);
169
-  }
170
-  void print(unsigned int value, int nbase = 0) {
171
-    if (nbase == BIN) print_bin(value, 16);
172
-    else if (nbase == OCT) printf("%6o", value);
173
-    else if (nbase == HEX) printf("%4X", value);
174
-    else printf("%u" , value);
175
-  }
176
-  void print(long value, int nbase = 0) {
177
-    if (nbase == BIN) print_bin(value, 32);
178
-    else if (nbase == OCT) printf("%11o", value);
179
-    else if (nbase == HEX) printf("%8X", value);
180
-    else printf("%ld" , value);
181
-  }
182
-  void print(unsigned long value, int nbase = 0) {
183
-    if (nbase == BIN) print_bin(value, 32);
184
-    else if (nbase == OCT) printf("%11o", value);
185
-    else if (nbase == HEX) printf("%8X", value);
186
-    else printf("%lu" , value);
187
-  }
188
-  void print(float value, int round = 6)  { printf("%f" , value); }
189
-  void print(double value, int round = 6) { printf("%f" , value); }
190
-
191
-  void println(const char value[]) { printf("%s\n" , value); }
192
-  void println(char value, int nbase = 0) { print(value, nbase); println(); }
193
-  void println(unsigned char value, int nbase = 0) { print(value, nbase); println(); }
194
-  void println(int value, int nbase = 0) { print(value, nbase); println(); }
195
-  void println(unsigned int value, int nbase = 0) { print(value, nbase); println(); }
196
-  void println(long value, int nbase = 0) { print(value, nbase); println(); }
197
-  void println(unsigned long value, int nbase = 0) { print(value, nbase); println(); }
198
-  void println(float value, int round = 6) { printf("%f\n" , value); }
199
-  void println(double value, int round = 6) { printf("%f\n" , value); }
200
-  void println(void) { print('\n'); }
201
-
202
-  volatile RingBuffer<uint8_t, 128> receive_buffer;
203
-  volatile RingBuffer<uint8_t, 128> transmit_buffer;
156
+  RingBuffer<uint8_t, 128> receive_buffer;
157
+  RingBuffer<uint8_t, 128> transmit_buffer;
204 158
   volatile bool host_connected;
205 159
 };
206 160
 

+ 1
- 1
Marlin/src/HAL/HAL_LPC1768/lpc1768_flag_script.py 查看文件

@@ -49,7 +49,7 @@ else:
49 49
           "-fno-threadsafe-statics"
50 50
       ],
51 51
       LINKFLAGS=[
52
-          "-Wl,-Tframeworks/CMSIS/LPC1768/Re-ARM/LPC1768.ld,--gc-sections",
52
+          "-Wl,-Tframeworks/CMSIS/LPC1768/system/LPC1768.ld,--gc-sections",
53 53
           "-Os",
54 54
           "-mcpu=cortex-m3",
55 55
           "-mthumb",

+ 38
- 40
Marlin/src/HAL/HAL_LPC1768/main.cpp 查看文件

@@ -35,53 +35,52 @@ extern "C" {
35 35
 #include "LPC1768_PWM.h"
36 36
 
37 37
 static __INLINE uint32_t SysTick_Config(uint32_t ticks) {
38
-  if (ticks > SysTick_LOAD_RELOAD_Msk)
39
-    return (1); /* Reload value impossible */
38
+  if (ticks > SysTick_LOAD_RELOAD_Msk) return 1;
40 39
 
41
-  SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
42
-  NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(0, 0, 0)); /* set Priority for Cortex-M3 System Interrupts */
43
-  SysTick->VAL = 0; /* Load the SysTick Counter Value */
40
+  SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;        // Set reload register
41
+  SysTick->VAL  = 0;                                            // Load the SysTick Counter Value
44 42
   SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
45
-  SysTick_CTRL_TICKINT_Msk |
46
-  SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
47
-  return (0); /* Function successful */
43
+                  SysTick_CTRL_TICKINT_Msk   |
44
+                  SysTick_CTRL_ENABLE_Msk;                      // Enable SysTick IRQ and SysTick Timer
45
+
46
+  NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(0, 0, 0)); // Set Priority for Cortex-M3 System Interrupts
47
+  return 0;
48 48
 }
49 49
 
50 50
 extern "C" {
51
-extern void disk_timerproc(void);
52
-volatile uint32_t _millis;
53
-void SysTick_Handler(void) {
54
-  ++_millis;
55
-  disk_timerproc(); /* Disk timer process */
56
-}
57
-}
51
+  extern int isLPC1769();
52
+  extern void disk_timerproc(void);
53
+  volatile uint32_t _millis;
58 54
 
59
-// runs after clock init and before global static constructors
60
-extern "C" void SystemPostInit() {
61
-  _millis = 0;     // initialise the millisecond counter value;
62
-  SysTick_Config(SystemCoreClock / 1000);     // start millisecond global counter
63
-  GPIO_SetDir(4, 1UL << 28, 1);
64
-
65
-  for (int i = 0; i < 4; ++i) {
66
-    GPIO_SetValue(4, 1UL << 28);
67
-    delay(100);
68
-    GPIO_ClearValue(4, 1UL << 28);
69
-    delay(100);
55
+  void SysTick_Handler(void) {
56
+    ++_millis;
57
+    disk_timerproc();
70 58
   }
71
-}
72
-
73
-// detect 17x[4-8] (100MHz) or 17x9 (120MHz)
74
-static bool isLPC1769() {
75
-    #define IAP_LOCATION 0x1FFF1FF1
76
-    uint32_t command[1];
77
-    uint32_t result[5];
78
-    typedef void (*IAP)(uint32_t*, uint32_t*);
79
-    IAP iap = (IAP) IAP_LOCATION;
80 59
 
81
-    command[0] = 54;
82
-    iap(command, result);
60
+  // Runs after clock init and before global static constructors
61
+  void SystemPostInit() {
62
+    _millis = 0;                            // Initialise the millisecond counter value;
63
+    SysTick_Config(SystemCoreClock / 1000); // Start millisecond global counter
83 64
 
84
-    return ((result[1] & 0x00100000) != 0);
65
+    // Runs before setup() need to configure LED_PIN and use to indicate succsessful bootloader execution
66
+    #if PIN_EXISTS(LED)
67
+      SET_DIR_OUTPUT(LED_PIN);
68
+      WRITE_PIN_CLR(LED_PIN);
69
+
70
+      //MKS-SBASE has 3 other LEDS the bootloader uses during flashing, clear them
71
+      SET_DIR_OUTPUT(P1_19);
72
+      WRITE_PIN_CLR(P1_19);
73
+      SET_DIR_OUTPUT(P1_20);
74
+      WRITE_PIN_CLR(P1_20);
75
+      SET_DIR_OUTPUT(P1_21);
76
+      WRITE_PIN_CLR(P1_21);
77
+
78
+      for (int i = 0; i < 6; ++i) {
79
+        TOGGLE(LED_PIN);
80
+        delay(100);
81
+      }
82
+    #endif
83
+  }
85 84
 }
86 85
 
87 86
 extern uint32_t MSC_SD_Init(uint8_t pdrv);
@@ -96,7 +95,6 @@ int main(void) {
96 95
   const uint32_t usb_timeout = millis() + 2000;
97 96
   while (!USB_Configuration && PENDING(millis(), usb_timeout)) {
98 97
     delay(50);
99
-
100 98
     #if PIN_EXISTS(LED)
101 99
       TOGGLE(LED_PIN);     // Flash fast while USB initialisation completes
102 100
     #endif
@@ -107,7 +105,7 @@ int main(void) {
107 105
     #if NUM_SERIAL > 1
108 106
       MYSERIAL1.begin(BAUDRATE);
109 107
     #endif
110
-    SERIAL_PRINTF("\n\n%s (%dMhz) UART0 Initialised\n", isLPC1769() ? "LPC1769" : "LPC1768", SystemCoreClock / 1000000);
108
+    SERIAL_PRINTF("\n\necho:%s (%dMhz) Initialised\n", isLPC1769() ? "LPC1769" : "LPC1768", SystemCoreClock / 1000000);
111 109
     SERIAL_FLUSHTX();
112 110
   #endif
113 111
 

+ 2
- 0
Marlin/src/pins/pins_MKS_SBASE.h 查看文件

@@ -40,6 +40,8 @@
40 40
 #define PIN_P0_28          P0_28
41 41
 */
42 42
 
43
+#define LED_PIN           P1_18 // LED2 P1_19, LED3 P1_20, LED4 P1_21
44
+
43 45
 //
44 46
 // Servo pin
45 47
 //

+ 3
- 2
frameworks/CMSIS/LPC1768/lib/Print.h 查看文件

@@ -40,7 +40,8 @@ class Print {
40 40
     void setWriteError(const int err = 1) { write_error = err; }
41 41
   public:
42 42
     Print() : write_error(0) {}
43
-
43
+    virtual ~Print() {}
44
+    
44 45
     int getWriteError() { return write_error; }
45 46
     void clearWriteError() { setWriteError(0); }
46 47
 
@@ -74,7 +75,7 @@ class Print {
74 75
     size_t println(double, int = 2);
75 76
     size_t println(const Printable&);
76 77
     size_t println(void);
77
-    size_t printf(const char *argList, ...);
78
+    virtual size_t printf(const char *argList, ...);
78 79
 };
79 80
 
80 81
 #endif // _CMSIS_PRINT_H_

+ 3
- 3
frameworks/CMSIS/LPC1768/lib/Stream.h 查看文件

@@ -47,9 +47,9 @@ class Stream : public Print
47 47
     int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
48 48
 
49 49
   public:
50
-    virtual int available() = 0;
51
-    virtual int read() = 0;
52
-    virtual int peek() = 0;
50
+    virtual size_t available() = 0;
51
+    virtual int16_t read() = 0; // signed int required for error (empty buffer) value
52
+    virtual int16_t peek() = 0;
53 53
     virtual void flush() = 0;
54 54
 
55 55
     Stream() {_timeout=1000;}

+ 1
- 1
frameworks/CMSIS/LPC1768/lib/usb/cdcuser.cpp 查看文件

@@ -211,7 +211,7 @@ void CDC_BulkIn(void) {
211 211
   if (numBytesAvail > 0) {
212 212
     numBytesAvail = numBytesAvail > (USB_CDC_BUFSIZE - 1) ? (USB_CDC_BUFSIZE - 1) : numBytesAvail;
213 213
     for(uint32_t i = 0; i < numBytesAvail; ++i) {
214
-      BulkBufIn[i] = usb_serial.transmit_buffer.read(); //todo: optimise
214
+      usb_serial.transmit_buffer.read(&BulkBufIn[i]);
215 215
     }
216 216
     USB_WriteEP(CDC_DEP_IN, &BulkBufIn[0], numBytesAvail);
217 217
   } else {

frameworks/CMSIS/LPC1768/Re-ARM/LPC1768.ld → frameworks/CMSIS/LPC1768/system/LPC1768.ld 查看文件


frameworks/CMSIS/LPC1768/Re-ARM/startup_LPC17xx.S → frameworks/CMSIS/LPC1768/system/startup_LPC17xx.S 查看文件


frameworks/CMSIS/LPC1768/Re-ARM/system_LPC17xx.c → frameworks/CMSIS/LPC1768/system/system_LPC17xx.c 查看文件

@@ -499,7 +499,7 @@ void SystemCoreClockUpdate (void)            /* Get Core Clock Frequency      */
499 499
 }
500 500
 
501 501
 // detect 17x[4-8] (100MHz) or 17x9 (120MHz)
502
-static int can_120MHz() {
502
+int isLPC1769() {
503 503
   #define IAP_LOCATION 0x1FFF1FF1
504 504
   uint32_t command[1];
505 505
   uint32_t result[5];
@@ -558,7 +558,7 @@ void SystemInit (void)
558 558
 
559 559
   LPC_SC->CCLKCFG   = 0x00000002;       /* Setup CPU Clock Divider            */
560 560
 
561
-  if(can_120MHz()) {
561
+  if(isLPC1769()) {
562 562
     LPC_SC->PLL0CFG   = 0x0000000E;     /* configure PLL0                     */
563 563
     LPC_SC->PLL0FEED  = 0xAA;
564 564
     LPC_SC->PLL0FEED  = 0x55;

正在加载...
取消
保存