Selaa lähdekoodia

bugfix-2.0.x critical fix (#7401)

* Fix mistake in gitignore file and add in missing core files.

The missing leading slash on "lib" meant all folders names lib in the directory tree are ignored, rather than just the top level PlatformIO lib folder

* Add LiquidCrystal Library and associated headers modified to compile.
Chris Pepper 6 vuotta sitten
vanhempi
commit
a5cf3a190c
35 muutettua tiedostoa jossa 30689 lisäystä ja 1 poistoa
  1. 1
    1
      .gitignore
  2. 326
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.cpp
  3. 109
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h
  4. 356
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/Print.cpp
  5. 85
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/Print.h
  6. 41
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/Printable.h
  7. 534
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/binary.h
  8. 1143
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/LPC176x.h
  9. 109
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/diskio.h
  10. 6204
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ff.c
  11. 364
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ff.h
  12. 269
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ffconf.h
  13. 15566
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ffunicode.c
  14. 38
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/integer.h
  15. 745
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/mmc_ssp.c
  16. 91
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/rtc176x.c
  17. 22
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/rtc176x.h
  18. 36
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/Re-ARM_usb_driver.inf
  19. 254
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/cdc.h
  20. 258
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/cdcuser.cpp
  21. 62
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/cdcuser.h
  22. 116
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/msc.h
  23. 716
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/mscuser.cpp
  24. 69
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/mscuser.h
  25. 353
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usb.h
  26. 181
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbcfg.h
  27. 815
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbcore.cpp
  28. 52
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbcore.h
  29. 257
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbdesc.cpp
  30. 35
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbdesc.h
  31. 811
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbhw.cpp
  32. 114
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbhw.h
  33. 130
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbreg.h
  34. 353
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbuser.cpp
  35. 74
    0
      Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbuser.h

+ 1
- 1
.gitignore Näytä tiedosto

@@ -122,7 +122,7 @@ tags
122 122
 .piolibdeps
123 123
 .clang_complete
124 124
 .gcc-flags.json
125
-lib/
125
+/lib/
126 126
 
127 127
 #Visual Studio
128 128
 *.sln

+ 326
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.cpp Näytä tiedosto

@@ -0,0 +1,326 @@
1
+#include "LiquidCrystal.h"
2
+
3
+#include <stdio.h>
4
+#include <string.h>
5
+#include <inttypes.h>
6
+#include "../../../../src/HAL/HAL_LPC1768/arduino.h"
7
+
8
+// When the display powers up, it is configured as follows:
9
+//
10
+// 1. Display clear
11
+// 2. Function set: 
12
+//    DL = 1; 8-bit interface data 
13
+//    N = 0; 1-line display 
14
+//    F = 0; 5x8 dot character font 
15
+// 3. Display on/off control: 
16
+//    D = 0; Display off 
17
+//    C = 0; Cursor off 
18
+//    B = 0; Blinking off 
19
+// 4. Entry mode set: 
20
+//    I/D = 1; Increment by 1 
21
+//    S = 0; No shift 
22
+//
23
+// Note, however, that resetting the Arduino doesn't reset the LCD, so we
24
+// can't assume that its in that state when a sketch starts (and the
25
+// LiquidCrystal constructor is called).
26
+
27
+LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
28
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
29
+			     uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
30
+{
31
+  init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
32
+}
33
+
34
+LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
35
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
36
+			     uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
37
+{
38
+  init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
39
+}
40
+
41
+LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
42
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
43
+{
44
+  init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
45
+}
46
+
47
+LiquidCrystal::LiquidCrystal(uint8_t rs,  uint8_t enable,
48
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
49
+{
50
+  init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
51
+}
52
+
53
+void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
54
+			 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
55
+			 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
56
+{
57
+  _rs_pin = rs;
58
+  _rw_pin = rw;
59
+  _enable_pin = enable;
60
+  
61
+  _data_pins[0] = d0;
62
+  _data_pins[1] = d1;
63
+  _data_pins[2] = d2;
64
+  _data_pins[3] = d3; 
65
+  _data_pins[4] = d4;
66
+  _data_pins[5] = d5;
67
+  _data_pins[6] = d6;
68
+  _data_pins[7] = d7; 
69
+
70
+  if (fourbitmode)
71
+    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
72
+  else 
73
+    _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
74
+  
75
+  begin(16, 1);  
76
+}
77
+
78
+void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
79
+  if (lines > 1) {
80
+    _displayfunction |= LCD_2LINE;
81
+  }
82
+  _numlines = lines;
83
+
84
+  setRowOffsets(0x00, 0x40, 0x00 + cols, 0x40 + cols);  
85
+
86
+  // for some 1 line displays you can select a 10 pixel high font
87
+  if ((dotsize != LCD_5x8DOTS) && (lines == 1)) {
88
+    _displayfunction |= LCD_5x10DOTS;
89
+  }
90
+
91
+  pinMode(_rs_pin, OUTPUT);
92
+  // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
93
+  if (_rw_pin != 255) { 
94
+    pinMode(_rw_pin, OUTPUT);
95
+  }
96
+  pinMode(_enable_pin, OUTPUT);
97
+  
98
+  // Do these once, instead of every time a character is drawn for speed reasons.
99
+  for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i)
100
+  {
101
+    pinMode(_data_pins[i], OUTPUT);
102
+   } 
103
+
104
+  // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
105
+  // according to datasheet, we need at least 40ms after power rises above 2.7V
106
+  // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
107
+  delayMicroseconds(50000); 
108
+  // Now we pull both RS and R/W low to begin commands
109
+  digitalWrite(_rs_pin, LOW);
110
+  digitalWrite(_enable_pin, LOW);
111
+  if (_rw_pin != 255) { 
112
+    digitalWrite(_rw_pin, LOW);
113
+  }
114
+  
115
+  //put the LCD into 4 bit or 8 bit mode
116
+  if (! (_displayfunction & LCD_8BITMODE)) {
117
+    // this is according to the hitachi HD44780 datasheet
118
+    // figure 24, pg 46
119
+
120
+    // we start in 8bit mode, try to set 4 bit mode
121
+    write4bits(0x03);
122
+    delayMicroseconds(4500); // wait min 4.1ms
123
+
124
+    // second try
125
+    write4bits(0x03);
126
+    delayMicroseconds(4500); // wait min 4.1ms
127
+    
128
+    // third go!
129
+    write4bits(0x03); 
130
+    delayMicroseconds(150);
131
+
132
+    // finally, set to 4-bit interface
133
+    write4bits(0x02); 
134
+  } else {
135
+    // this is according to the hitachi HD44780 datasheet
136
+    // page 45 figure 23
137
+
138
+    // Send function set command sequence
139
+    command(LCD_FUNCTIONSET | _displayfunction);
140
+    delayMicroseconds(4500);  // wait more than 4.1ms
141
+
142
+    // second try
143
+    command(LCD_FUNCTIONSET | _displayfunction);
144
+    delayMicroseconds(150);
145
+
146
+    // third go
147
+    command(LCD_FUNCTIONSET | _displayfunction);
148
+  }
149
+
150
+  // finally, set # lines, font size, etc.
151
+  command(LCD_FUNCTIONSET | _displayfunction);  
152
+
153
+  // turn the display on with no cursor or blinking default
154
+  _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
155
+  display();
156
+
157
+  // clear it off
158
+  clear();
159
+
160
+  // Initialize to default text direction (for romance languages)
161
+  _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
162
+  // set the entry mode
163
+  command(LCD_ENTRYMODESET | _displaymode);
164
+
165
+}
166
+
167
+void LiquidCrystal::setRowOffsets(int row0, int row1, int row2, int row3)
168
+{
169
+  _row_offsets[0] = row0;
170
+  _row_offsets[1] = row1;
171
+  _row_offsets[2] = row2;
172
+  _row_offsets[3] = row3;
173
+}
174
+
175
+/********** high level commands, for the user! */
176
+void LiquidCrystal::clear()
177
+{
178
+  command(LCD_CLEARDISPLAY);  // clear display, set cursor position to zero
179
+  delayMicroseconds(2000);  // this command takes a long time!
180
+}
181
+
182
+void LiquidCrystal::home()
183
+{
184
+  command(LCD_RETURNHOME);  // set cursor position to zero
185
+  delayMicroseconds(2000);  // this command takes a long time!
186
+}
187
+
188
+void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
189
+{
190
+  const size_t max_lines = sizeof(_row_offsets) / sizeof(*_row_offsets);
191
+  if ( row >= max_lines ) {
192
+    row = max_lines - 1;    // we count rows starting w/0
193
+  }
194
+  if ( row >= _numlines ) {
195
+    row = _numlines - 1;    // we count rows starting w/0
196
+  }
197
+  
198
+  command(LCD_SETDDRAMADDR | (col + _row_offsets[row]));
199
+}
200
+
201
+// Turn the display on/off (quickly)
202
+void LiquidCrystal::noDisplay() {
203
+  _displaycontrol &= ~LCD_DISPLAYON;
204
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
205
+}
206
+void LiquidCrystal::display() {
207
+  _displaycontrol |= LCD_DISPLAYON;
208
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
209
+}
210
+
211
+// Turns the underline cursor on/off
212
+void LiquidCrystal::noCursor() {
213
+  _displaycontrol &= ~LCD_CURSORON;
214
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
215
+}
216
+void LiquidCrystal::cursor() {
217
+  _displaycontrol |= LCD_CURSORON;
218
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
219
+}
220
+
221
+// Turn on and off the blinking cursor
222
+void LiquidCrystal::noBlink() {
223
+  _displaycontrol &= ~LCD_BLINKON;
224
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
225
+}
226
+void LiquidCrystal::blink() {
227
+  _displaycontrol |= LCD_BLINKON;
228
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
229
+}
230
+
231
+// These commands scroll the display without changing the RAM
232
+void LiquidCrystal::scrollDisplayLeft(void) {
233
+  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
234
+}
235
+void LiquidCrystal::scrollDisplayRight(void) {
236
+  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
237
+}
238
+
239
+// This is for text that flows Left to Right
240
+void LiquidCrystal::leftToRight(void) {
241
+  _displaymode |= LCD_ENTRYLEFT;
242
+  command(LCD_ENTRYMODESET | _displaymode);
243
+}
244
+
245
+// This is for text that flows Right to Left
246
+void LiquidCrystal::rightToLeft(void) {
247
+  _displaymode &= ~LCD_ENTRYLEFT;
248
+  command(LCD_ENTRYMODESET | _displaymode);
249
+}
250
+
251
+// This will 'right justify' text from the cursor
252
+void LiquidCrystal::autoscroll(void) {
253
+  _displaymode |= LCD_ENTRYSHIFTINCREMENT;
254
+  command(LCD_ENTRYMODESET | _displaymode);
255
+}
256
+
257
+// This will 'left justify' text from the cursor
258
+void LiquidCrystal::noAutoscroll(void) {
259
+  _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
260
+  command(LCD_ENTRYMODESET | _displaymode);
261
+}
262
+
263
+// Allows us to fill the first 8 CGRAM locations
264
+// with custom characters
265
+void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
266
+  location &= 0x7; // we only have 8 locations 0-7
267
+  command(LCD_SETCGRAMADDR | (location << 3));
268
+  for (int i=0; i<8; i++) {
269
+    write(charmap[i]);
270
+  }
271
+}
272
+
273
+/*********** mid level commands, for sending data/cmds */
274
+
275
+inline void LiquidCrystal::command(uint8_t value) {
276
+  send(value, LOW);
277
+}
278
+
279
+inline size_t LiquidCrystal::write(uint8_t value) {
280
+  send(value, HIGH);
281
+  return 1; // assume sucess
282
+}
283
+
284
+/************ low level data pushing commands **********/
285
+
286
+// write either command or data, with automatic 4/8-bit selection
287
+void LiquidCrystal::send(uint8_t value, uint8_t mode) {
288
+  digitalWrite(_rs_pin, mode);
289
+
290
+  // if there is a RW pin indicated, set it low to Write
291
+  if (_rw_pin != 255) { 
292
+    digitalWrite(_rw_pin, LOW);
293
+  }
294
+  
295
+  if (_displayfunction & LCD_8BITMODE) {
296
+    write8bits(value); 
297
+  } else {
298
+    write4bits(value>>4);
299
+    write4bits(value);
300
+  }
301
+}
302
+
303
+void LiquidCrystal::pulseEnable(void) {
304
+  digitalWrite(_enable_pin, LOW);
305
+  delayMicroseconds(1);    
306
+  digitalWrite(_enable_pin, HIGH);
307
+  delayMicroseconds(1);    // enable pulse must be >450ns
308
+  digitalWrite(_enable_pin, LOW);
309
+  delayMicroseconds(100);   // commands need > 37us to settle
310
+}
311
+
312
+void LiquidCrystal::write4bits(uint8_t value) {
313
+  for (int i = 0; i < 4; i++) {
314
+    digitalWrite(_data_pins[i], (value >> i) & 0x01);
315
+  }
316
+
317
+  pulseEnable();
318
+}
319
+
320
+void LiquidCrystal::write8bits(uint8_t value) {
321
+  for (int i = 0; i < 8; i++) {
322
+    digitalWrite(_data_pins[i], (value >> i) & 0x01);
323
+  }
324
+  
325
+  pulseEnable();
326
+}

+ 109
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h Näytä tiedosto

@@ -0,0 +1,109 @@
1
+#ifndef LiquidCrystal_h
2
+#define LiquidCrystal_h
3
+
4
+#include <inttypes.h>
5
+#include "binary.h"
6
+#include "Print.h"
7
+
8
+// commands
9
+#define LCD_CLEARDISPLAY 0x01
10
+#define LCD_RETURNHOME 0x02
11
+#define LCD_ENTRYMODESET 0x04
12
+#define LCD_DISPLAYCONTROL 0x08
13
+#define LCD_CURSORSHIFT 0x10
14
+#define LCD_FUNCTIONSET 0x20
15
+#define LCD_SETCGRAMADDR 0x40
16
+#define LCD_SETDDRAMADDR 0x80
17
+
18
+// flags for display entry mode
19
+#define LCD_ENTRYRIGHT 0x00
20
+#define LCD_ENTRYLEFT 0x02
21
+#define LCD_ENTRYSHIFTINCREMENT 0x01
22
+#define LCD_ENTRYSHIFTDECREMENT 0x00
23
+
24
+// flags for display on/off control
25
+#define LCD_DISPLAYON 0x04
26
+#define LCD_DISPLAYOFF 0x00
27
+#define LCD_CURSORON 0x02
28
+#define LCD_CURSOROFF 0x00
29
+#define LCD_BLINKON 0x01
30
+#define LCD_BLINKOFF 0x00
31
+
32
+// flags for display/cursor shift
33
+#define LCD_DISPLAYMOVE 0x08
34
+#define LCD_CURSORMOVE 0x00
35
+#define LCD_MOVERIGHT 0x04
36
+#define LCD_MOVELEFT 0x00
37
+
38
+// flags for function set
39
+#define LCD_8BITMODE 0x10
40
+#define LCD_4BITMODE 0x00
41
+#define LCD_2LINE 0x08
42
+#define LCD_1LINE 0x00
43
+#define LCD_5x10DOTS 0x04
44
+#define LCD_5x8DOTS 0x00
45
+
46
+class LiquidCrystal : public Print {
47
+public:
48
+  LiquidCrystal(uint8_t rs, uint8_t enable,
49
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
50
+		uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
51
+  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
52
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
53
+		uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
54
+  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
55
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
56
+  LiquidCrystal(uint8_t rs, uint8_t enable,
57
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
58
+
59
+  void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
60
+	    uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
61
+	    uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
62
+    
63
+  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
64
+
65
+  void clear();
66
+  void home();
67
+
68
+  void noDisplay();
69
+  void display();
70
+  void noBlink();
71
+  void blink();
72
+  void noCursor();
73
+  void cursor();
74
+  void scrollDisplayLeft();
75
+  void scrollDisplayRight();
76
+  void leftToRight();
77
+  void rightToLeft();
78
+  void autoscroll();
79
+  void noAutoscroll();
80
+
81
+  void setRowOffsets(int row1, int row2, int row3, int row4);
82
+  void createChar(uint8_t, uint8_t[]);
83
+  void setCursor(uint8_t, uint8_t); 
84
+  virtual size_t write(uint8_t);
85
+  void command(uint8_t);
86
+  
87
+  using Print::write;
88
+private:
89
+  void send(uint8_t, uint8_t);
90
+  void write4bits(uint8_t);
91
+  void write8bits(uint8_t);
92
+  void pulseEnable();
93
+
94
+  uint8_t _rs_pin; // LOW: command.  HIGH: character.
95
+  uint8_t _rw_pin; // LOW: write to LCD.  HIGH: read from LCD.
96
+  uint8_t _enable_pin; // activated by a HIGH pulse.
97
+  uint8_t _data_pins[8];
98
+
99
+  uint8_t _displayfunction;
100
+  uint8_t _displaycontrol;
101
+  uint8_t _displaymode;
102
+
103
+  uint8_t _initialized;
104
+
105
+  uint8_t _numlines;
106
+  uint8_t _row_offsets[4];
107
+};
108
+
109
+#endif

+ 356
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/Print.cpp Näytä tiedosto

@@ -0,0 +1,356 @@
1
+/*
2
+ Print.cpp - Base class that provides print() and println()
3
+ Copyright (c) 2008 David A. Mellis.  All right reserved.
4
+ 
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+ 
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ Lesser General Public License for more details.
14
+ 
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+ 
19
+ Modified 23 November 2006 by David A. Mellis
20
+ Modified 03 August 2015 by Chuck Todd
21
+ */
22
+
23
+#include <stdlib.h>
24
+#include <stdio.h>
25
+#include <string.h>
26
+#include <math.h>
27
+#include <inttypes.h>
28
+#include "Print.h"
29
+#include <stdarg.h>
30
+
31
+#define PrintfEnable  0
32
+// Public Methods //////////////////////////////////////////////////////////////
33
+
34
+/* default implementation: may be overridden */
35
+size_t Print::write(const uint8_t *buffer, size_t size)
36
+{
37
+	
38
+  size_t n = 0;
39
+  while (size--) {
40
+    if (write(*buffer++)) n++;
41
+    else break;
42
+  }
43
+  return n;
44
+}
45
+
46
+
47
+size_t Print::print(const char str[])
48
+{
49
+	
50
+	//while(1);
51
+  return write(str);
52
+}
53
+
54
+size_t Print::print(char c)
55
+{
56
+  return write(c);
57
+}
58
+
59
+size_t Print::print(unsigned char b, int base)
60
+{
61
+  return print((unsigned long) b, base);
62
+}
63
+
64
+size_t Print::print(int n, int base)
65
+{
66
+  return print((long) n, base);
67
+}
68
+
69
+size_t Print::print(unsigned int n, int base)
70
+{
71
+  return print((unsigned long) n, base);
72
+}
73
+
74
+size_t Print::print(long n, int base)
75
+{
76
+  if (base == 0) {
77
+    return write(n);
78
+  } else if (base == 10) {
79
+    if (n < 0) {
80
+      int t = print('-');
81
+      n = -n;
82
+      return printNumber(n, 10) + t;
83
+    }
84
+    return printNumber(n, 10);
85
+  } else {
86
+    return printNumber(n, base);
87
+  }
88
+}
89
+
90
+size_t Print::print(unsigned long n, int base)
91
+{
92
+  if (base == 0) return write(n);
93
+  else return printNumber(n, base);
94
+}
95
+
96
+size_t Print::print(double n, int digits)
97
+{
98
+  return printFloat(n, digits);
99
+}
100
+
101
+size_t Print::print(const Printable& x)
102
+{
103
+  return x.printTo(*this);
104
+}
105
+
106
+size_t Print::println(void)
107
+{
108
+  return write("\r\n");
109
+}
110
+
111
+size_t Print::println(const char c[])
112
+{
113
+  size_t n = print(c);
114
+  n += println();
115
+  return n;
116
+}
117
+
118
+size_t Print::println(char c)
119
+{
120
+  size_t n = print(c);
121
+  n += println();
122
+  return n;
123
+}
124
+
125
+size_t Print::println(unsigned char b, int base)
126
+{
127
+  size_t n = print(b, base);
128
+  n += println();
129
+  return n;
130
+}
131
+
132
+size_t Print::println(int num, int base)
133
+{
134
+  size_t n = print(num, base);
135
+  n += println();
136
+  return n;
137
+}
138
+
139
+size_t Print::println(unsigned int num, int base)
140
+{
141
+  size_t n = print(num, base);
142
+  n += println();
143
+  return n;
144
+}
145
+
146
+size_t Print::println(long num, int base)
147
+{
148
+  size_t n = print(num, base);
149
+  n += println();
150
+  return n;
151
+}
152
+
153
+size_t Print::println(unsigned long num, int base)
154
+{
155
+  size_t n = print(num, base);
156
+  n += println();
157
+  return n;
158
+}
159
+
160
+size_t Print::println(double num, int digits)
161
+{
162
+  size_t n = print(num, digits);
163
+  n += println();
164
+  return n;
165
+}
166
+
167
+size_t Print::println(const Printable& x)
168
+{
169
+  size_t n = print(x);
170
+  n += println();
171
+  return n;
172
+}
173
+
174
+// Private Methods /////////////////////////////////////////////////////////////
175
+
176
+size_t Print::printNumber(unsigned long n, uint8_t base) {
177
+  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
178
+  char *str = &buf[sizeof(buf) - 1];
179
+
180
+  *str = '\0';
181
+
182
+  // prevent crash if called with base == 1
183
+  if (base < 2) base = 10;
184
+
185
+  do {
186
+    unsigned long m = n;
187
+    n /= base;
188
+    char c = m - base * n;
189
+    *--str = c < 10 ? c + '0' : c + 'A' - 10;
190
+  } while(n);
191
+
192
+  return write(str);
193
+}
194
+
195
+size_t Print::printFloat(double number, uint8_t digits) 
196
+{ 
197
+  size_t n = 0;
198
+  
199
+  if (isnan(number)) return print("nan");
200
+  if (isinf(number)) return print("inf");
201
+  if (number > 4294967040.0) return print ("ovf");  // constant determined empirically
202
+  if (number <-4294967040.0) return print ("ovf");  // constant determined empirically
203
+  
204
+  // Handle negative numbers
205
+  if (number < 0.0)
206
+  {
207
+     n += print('-');
208
+     number = -number;
209
+  }
210
+
211
+  // Round correctly so that print(1.999, 2) prints as "2.00"
212
+  double rounding = 0.5;
213
+  for (uint8_t i=0; i<digits; ++i)
214
+    rounding /= 10.0;
215
+  
216
+  number += rounding;
217
+
218
+  // Extract the integer part of the number and print it
219
+  unsigned long int_part = (unsigned long)number;
220
+  double remainder = number - (double)int_part;
221
+  n += print(int_part);
222
+
223
+  // Print the decimal point, but only if there are digits beyond
224
+  if (digits > 0) {
225
+    n += print("."); 
226
+  }
227
+
228
+  // Extract digits from the remainder one at a time
229
+  while (digits-- > 0)
230
+  {
231
+    remainder *= 10.0;
232
+    int toPrint = int(remainder);
233
+    n += print(toPrint);
234
+    remainder -= toPrint; 
235
+  } 
236
+  
237
+  return n;
238
+}
239
+
240
+
241
+#if (PrintfEnable == 1) 
242
+size_t Print::printf(const char *argList, ...)
243
+{
244
+    const char *ptr;
245
+    double floatNum_f32;
246
+    va_list argp;
247
+    sint16_t num_s16;
248
+    sint32_t num_s32;
249
+    uint16_t num_u16;
250
+    uint32_t num_u32;
251
+    char *str;
252
+    char  ch;
253
+    uint8_t numOfDigits;
254
+
255
+    va_start(argp, argList);
256
+
257
+    /* Loop through the list to extract all the input arguments */
258
+    for(ptr = argList; *ptr != '\0'; ptr++)
259
+    {
260
+
261
+        ch= *ptr;
262
+        if(ch == '%')         /*Check for '%' as there will be format specifier after it */
263
+        {
264
+            ptr++;
265
+            ch = *ptr;
266
+            if((ch>=0x30) && (ch<=0x39))
267
+            {
268
+                numOfDigits = 0;
269
+                while((ch>=0x30) && (ch<=0x39))
270
+                {
271
+                    numOfDigits = (numOfDigits * 10) + (ch-0x30);
272
+                    ptr++;
273
+                    ch = *ptr;
274
+                }
275
+            }
276
+            else
277
+            {
278
+                numOfDigits = 0xff;
279
+            }                
280
+
281
+
282
+            switch(ch)       /* Decode the type of the argument */
283
+            {
284
+
285
+            case 'C':
286
+            case 'c':     /* Argument type is of char, hence read char data from the argp */
287
+                ch = va_arg(argp, int);
288
+                print(ch);
289
+                break;
290
+
291
+
292
+
293
+            case 'd':    /* Argument type is of signed integer, hence read 16bit data from the argp */
294
+            case 'D':
295
+                num_s32 = va_arg(argp, int);
296
+                print(num_s32, 10);
297
+                break;  
298
+
299
+
300
+            case 'u':
301
+            case 'U':    /* Argument type is of integer, hence read 32bit unsigend data */
302
+                num_u32 = va_arg(argp, uint32_t);
303
+                print(num_u32, 10);               
304
+                break;            
305
+
306
+
307
+
308
+
309
+            case 'x':
310
+            case 'X':  /* Argument type is of hex, hence hexadecimal data from the argp */
311
+                num_u32 = va_arg(argp, uint32_t);
312
+                print(num_u32, 16);                 
313
+                break;
314
+
315
+
316
+            case 'b':
317
+            case 'B':  /* Argument type is of binary,Read int and convert to binary */
318
+                num_u32 = va_arg(argp, uint32_t); 
319
+                print(num_u32, 2);                 
320
+                break;
321
+
322
+
323
+
324
+            case 'F':
325
+            case 'f': /* Argument type is of float, hence read double data from the argp */
326
+                floatNum_f32 = va_arg(argp, double);              
327
+                printFloat(floatNum_f32,10);
328
+                break;
329
+
330
+
331
+
332
+            case 'S':
333
+            case 's': /* Argument type is of string, hence get the pointer to sting passed */
334
+                str = va_arg(argp, char *);
335
+                print(str);                
336
+                break;
337
+
338
+
339
+
340
+            case '%':
341
+                print('%');
342
+                break;
343
+            }
344
+        }
345
+        else
346
+        {
347
+            /* As '%' is not detected transmit the char passed */
348
+            print(ch);
349
+        }
350
+    }
351
+
352
+    va_end(argp);
353
+}
354
+
355
+
356
+#endif    

+ 85
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/Print.h Näytä tiedosto

@@ -0,0 +1,85 @@
1
+/*
2
+  Print.h - Base class that provides print() and println()
3
+  Copyright (c) 2008 David A. Mellis.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Print_h
21
+#define Print_h
22
+
23
+
24
+#include <inttypes.h>
25
+#include <stdio.h> // for size_t
26
+#include <string.h>
27
+
28
+
29
+#include "Printable.h"
30
+
31
+
32
+#define DEC 10
33
+#define HEX 16
34
+#define OCT 8
35
+#define BIN 2
36
+
37
+class Print
38
+{
39
+  private:
40
+    int write_error;
41
+    size_t printNumber(unsigned long, uint8_t);
42
+    size_t printFloat(double, uint8_t);
43
+  protected:
44
+    void setWriteError(int err = 1) { write_error = err; }
45
+  public:
46
+    Print() : write_error(0) {}
47
+  
48
+    int getWriteError() { return write_error; }
49
+    void clearWriteError() { setWriteError(0); }
50
+  
51
+    virtual size_t write(uint8_t) = 0;
52
+    size_t write(const char *str) {
53
+		
54
+      if (str == NULL) return 0;
55
+      return write((const uint8_t *)str, strlen(str));
56
+    }
57
+    virtual size_t write(const uint8_t *buffer, size_t size);
58
+    size_t write(const char *buffer, size_t size) {
59
+      return write((const uint8_t *)buffer, size);
60
+    }
61
+    
62
+    size_t print(const char[]);
63
+    size_t print(char);
64
+    size_t print(unsigned char, int = DEC);
65
+    size_t print(int, int = DEC);
66
+    size_t print(unsigned int, int = DEC);
67
+    size_t print(long, int = DEC);
68
+    size_t print(unsigned long, int = DEC);
69
+    size_t print(double, int = 2);
70
+    size_t print(const Printable&);
71
+
72
+    size_t println(const char[]);
73
+    size_t println(char);
74
+    size_t println(unsigned char, int = DEC);
75
+    size_t println(int, int = DEC);
76
+    size_t println(unsigned int, int = DEC);
77
+    size_t println(long, int = DEC);
78
+    size_t println(unsigned long, int = DEC);
79
+    size_t println(double, int = 2);
80
+    size_t println(const Printable&);
81
+    size_t println(void);
82
+    size_t printf(const char *argList, ...);
83
+};
84
+
85
+#endif

+ 41
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/Printable.h Näytä tiedosto

@@ -0,0 +1,41 @@
1
+/*
2
+  Printable.h - Interface class that allows printing of complex types
3
+  Copyright (c) 2011 Adrian McEwen.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Printable_h
21
+#define Printable_h
22
+
23
+#include <stdlib.h>
24
+#include <inttypes.h>
25
+
26
+class Print;
27
+
28
+/** The Printable class provides a way for new classes to allow themselves to be printed.
29
+    By deriving from Printable and implementing the printTo method, it will then be possible
30
+    for users to print out instances of this class by passing them into the usual
31
+    Print::print and Print::println methods.
32
+*/
33
+
34
+class Printable
35
+{
36
+  public:
37
+    virtual size_t printTo(Print& p) const = 0;
38
+};
39
+
40
+#endif
41
+

+ 534
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/binary.h Näytä tiedosto

@@ -0,0 +1,534 @@
1
+/*
2
+  binary.h - Definitions for binary constants
3
+  Copyright (c) 2006 David A. Mellis.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Binary_h
21
+#define Binary_h
22
+
23
+#define B0 0
24
+#define B00 0
25
+#define B000 0
26
+#define B0000 0
27
+#define B00000 0
28
+#define B000000 0
29
+#define B0000000 0
30
+#define B00000000 0
31
+#define B1 1
32
+#define B01 1
33
+#define B001 1
34
+#define B0001 1
35
+#define B00001 1
36
+#define B000001 1
37
+#define B0000001 1
38
+#define B00000001 1
39
+#define B10 2
40
+#define B010 2
41
+#define B0010 2
42
+#define B00010 2
43
+#define B000010 2
44
+#define B0000010 2
45
+#define B00000010 2
46
+#define B11 3
47
+#define B011 3
48
+#define B0011 3
49
+#define B00011 3
50
+#define B000011 3
51
+#define B0000011 3
52
+#define B00000011 3
53
+#define B100 4
54
+#define B0100 4
55
+#define B00100 4
56
+#define B000100 4
57
+#define B0000100 4
58
+#define B00000100 4
59
+#define B101 5
60
+#define B0101 5
61
+#define B00101 5
62
+#define B000101 5
63
+#define B0000101 5
64
+#define B00000101 5
65
+#define B110 6
66
+#define B0110 6
67
+#define B00110 6
68
+#define B000110 6
69
+#define B0000110 6
70
+#define B00000110 6
71
+#define B111 7
72
+#define B0111 7
73
+#define B00111 7
74
+#define B000111 7
75
+#define B0000111 7
76
+#define B00000111 7
77
+#define B1000 8
78
+#define B01000 8
79
+#define B001000 8
80
+#define B0001000 8
81
+#define B00001000 8
82
+#define B1001 9
83
+#define B01001 9
84
+#define B001001 9
85
+#define B0001001 9
86
+#define B00001001 9
87
+#define B1010 10
88
+#define B01010 10
89
+#define B001010 10
90
+#define B0001010 10
91
+#define B00001010 10
92
+#define B1011 11
93
+#define B01011 11
94
+#define B001011 11
95
+#define B0001011 11
96
+#define B00001011 11
97
+#define B1100 12
98
+#define B01100 12
99
+#define B001100 12
100
+#define B0001100 12
101
+#define B00001100 12
102
+#define B1101 13
103
+#define B01101 13
104
+#define B001101 13
105
+#define B0001101 13
106
+#define B00001101 13
107
+#define B1110 14
108
+#define B01110 14
109
+#define B001110 14
110
+#define B0001110 14
111
+#define B00001110 14
112
+#define B1111 15
113
+#define B01111 15
114
+#define B001111 15
115
+#define B0001111 15
116
+#define B00001111 15
117
+#define B10000 16
118
+#define B010000 16
119
+#define B0010000 16
120
+#define B00010000 16
121
+#define B10001 17
122
+#define B010001 17
123
+#define B0010001 17
124
+#define B00010001 17
125
+#define B10010 18
126
+#define B010010 18
127
+#define B0010010 18
128
+#define B00010010 18
129
+#define B10011 19
130
+#define B010011 19
131
+#define B0010011 19
132
+#define B00010011 19
133
+#define B10100 20
134
+#define B010100 20
135
+#define B0010100 20
136
+#define B00010100 20
137
+#define B10101 21
138
+#define B010101 21
139
+#define B0010101 21
140
+#define B00010101 21
141
+#define B10110 22
142
+#define B010110 22
143
+#define B0010110 22
144
+#define B00010110 22
145
+#define B10111 23
146
+#define B010111 23
147
+#define B0010111 23
148
+#define B00010111 23
149
+#define B11000 24
150
+#define B011000 24
151
+#define B0011000 24
152
+#define B00011000 24
153
+#define B11001 25
154
+#define B011001 25
155
+#define B0011001 25
156
+#define B00011001 25
157
+#define B11010 26
158
+#define B011010 26
159
+#define B0011010 26
160
+#define B00011010 26
161
+#define B11011 27
162
+#define B011011 27
163
+#define B0011011 27
164
+#define B00011011 27
165
+#define B11100 28
166
+#define B011100 28
167
+#define B0011100 28
168
+#define B00011100 28
169
+#define B11101 29
170
+#define B011101 29
171
+#define B0011101 29
172
+#define B00011101 29
173
+#define B11110 30
174
+#define B011110 30
175
+#define B0011110 30
176
+#define B00011110 30
177
+#define B11111 31
178
+#define B011111 31
179
+#define B0011111 31
180
+#define B00011111 31
181
+#define B100000 32
182
+#define B0100000 32
183
+#define B00100000 32
184
+#define B100001 33
185
+#define B0100001 33
186
+#define B00100001 33
187
+#define B100010 34
188
+#define B0100010 34
189
+#define B00100010 34
190
+#define B100011 35
191
+#define B0100011 35
192
+#define B00100011 35
193
+#define B100100 36
194
+#define B0100100 36
195
+#define B00100100 36
196
+#define B100101 37
197
+#define B0100101 37
198
+#define B00100101 37
199
+#define B100110 38
200
+#define B0100110 38
201
+#define B00100110 38
202
+#define B100111 39
203
+#define B0100111 39
204
+#define B00100111 39
205
+#define B101000 40
206
+#define B0101000 40
207
+#define B00101000 40
208
+#define B101001 41
209
+#define B0101001 41
210
+#define B00101001 41
211
+#define B101010 42
212
+#define B0101010 42
213
+#define B00101010 42
214
+#define B101011 43
215
+#define B0101011 43
216
+#define B00101011 43
217
+#define B101100 44
218
+#define B0101100 44
219
+#define B00101100 44
220
+#define B101101 45
221
+#define B0101101 45
222
+#define B00101101 45
223
+#define B101110 46
224
+#define B0101110 46
225
+#define B00101110 46
226
+#define B101111 47
227
+#define B0101111 47
228
+#define B00101111 47
229
+#define B110000 48
230
+#define B0110000 48
231
+#define B00110000 48
232
+#define B110001 49
233
+#define B0110001 49
234
+#define B00110001 49
235
+#define B110010 50
236
+#define B0110010 50
237
+#define B00110010 50
238
+#define B110011 51
239
+#define B0110011 51
240
+#define B00110011 51
241
+#define B110100 52
242
+#define B0110100 52
243
+#define B00110100 52
244
+#define B110101 53
245
+#define B0110101 53
246
+#define B00110101 53
247
+#define B110110 54
248
+#define B0110110 54
249
+#define B00110110 54
250
+#define B110111 55
251
+#define B0110111 55
252
+#define B00110111 55
253
+#define B111000 56
254
+#define B0111000 56
255
+#define B00111000 56
256
+#define B111001 57
257
+#define B0111001 57
258
+#define B00111001 57
259
+#define B111010 58
260
+#define B0111010 58
261
+#define B00111010 58
262
+#define B111011 59
263
+#define B0111011 59
264
+#define B00111011 59
265
+#define B111100 60
266
+#define B0111100 60
267
+#define B00111100 60
268
+#define B111101 61
269
+#define B0111101 61
270
+#define B00111101 61
271
+#define B111110 62
272
+#define B0111110 62
273
+#define B00111110 62
274
+#define B111111 63
275
+#define B0111111 63
276
+#define B00111111 63
277
+#define B1000000 64
278
+#define B01000000 64
279
+#define B1000001 65
280
+#define B01000001 65
281
+#define B1000010 66
282
+#define B01000010 66
283
+#define B1000011 67
284
+#define B01000011 67
285
+#define B1000100 68
286
+#define B01000100 68
287
+#define B1000101 69
288
+#define B01000101 69
289
+#define B1000110 70
290
+#define B01000110 70
291
+#define B1000111 71
292
+#define B01000111 71
293
+#define B1001000 72
294
+#define B01001000 72
295
+#define B1001001 73
296
+#define B01001001 73
297
+#define B1001010 74
298
+#define B01001010 74
299
+#define B1001011 75
300
+#define B01001011 75
301
+#define B1001100 76
302
+#define B01001100 76
303
+#define B1001101 77
304
+#define B01001101 77
305
+#define B1001110 78
306
+#define B01001110 78
307
+#define B1001111 79
308
+#define B01001111 79
309
+#define B1010000 80
310
+#define B01010000 80
311
+#define B1010001 81
312
+#define B01010001 81
313
+#define B1010010 82
314
+#define B01010010 82
315
+#define B1010011 83
316
+#define B01010011 83
317
+#define B1010100 84
318
+#define B01010100 84
319
+#define B1010101 85
320
+#define B01010101 85
321
+#define B1010110 86
322
+#define B01010110 86
323
+#define B1010111 87
324
+#define B01010111 87
325
+#define B1011000 88
326
+#define B01011000 88
327
+#define B1011001 89
328
+#define B01011001 89
329
+#define B1011010 90
330
+#define B01011010 90
331
+#define B1011011 91
332
+#define B01011011 91
333
+#define B1011100 92
334
+#define B01011100 92
335
+#define B1011101 93
336
+#define B01011101 93
337
+#define B1011110 94
338
+#define B01011110 94
339
+#define B1011111 95
340
+#define B01011111 95
341
+#define B1100000 96
342
+#define B01100000 96
343
+#define B1100001 97
344
+#define B01100001 97
345
+#define B1100010 98
346
+#define B01100010 98
347
+#define B1100011 99
348
+#define B01100011 99
349
+#define B1100100 100
350
+#define B01100100 100
351
+#define B1100101 101
352
+#define B01100101 101
353
+#define B1100110 102
354
+#define B01100110 102
355
+#define B1100111 103
356
+#define B01100111 103
357
+#define B1101000 104
358
+#define B01101000 104
359
+#define B1101001 105
360
+#define B01101001 105
361
+#define B1101010 106
362
+#define B01101010 106
363
+#define B1101011 107
364
+#define B01101011 107
365
+#define B1101100 108
366
+#define B01101100 108
367
+#define B1101101 109
368
+#define B01101101 109
369
+#define B1101110 110
370
+#define B01101110 110
371
+#define B1101111 111
372
+#define B01101111 111
373
+#define B1110000 112
374
+#define B01110000 112
375
+#define B1110001 113
376
+#define B01110001 113
377
+#define B1110010 114
378
+#define B01110010 114
379
+#define B1110011 115
380
+#define B01110011 115
381
+#define B1110100 116
382
+#define B01110100 116
383
+#define B1110101 117
384
+#define B01110101 117
385
+#define B1110110 118
386
+#define B01110110 118
387
+#define B1110111 119
388
+#define B01110111 119
389
+#define B1111000 120
390
+#define B01111000 120
391
+#define B1111001 121
392
+#define B01111001 121
393
+#define B1111010 122
394
+#define B01111010 122
395
+#define B1111011 123
396
+#define B01111011 123
397
+#define B1111100 124
398
+#define B01111100 124
399
+#define B1111101 125
400
+#define B01111101 125
401
+#define B1111110 126
402
+#define B01111110 126
403
+#define B1111111 127
404
+#define B01111111 127
405
+#define B10000000 128
406
+#define B10000001 129
407
+#define B10000010 130
408
+#define B10000011 131
409
+#define B10000100 132
410
+#define B10000101 133
411
+#define B10000110 134
412
+#define B10000111 135
413
+#define B10001000 136
414
+#define B10001001 137
415
+#define B10001010 138
416
+#define B10001011 139
417
+#define B10001100 140
418
+#define B10001101 141
419
+#define B10001110 142
420
+#define B10001111 143
421
+#define B10010000 144
422
+#define B10010001 145
423
+#define B10010010 146
424
+#define B10010011 147
425
+#define B10010100 148
426
+#define B10010101 149
427
+#define B10010110 150
428
+#define B10010111 151
429
+#define B10011000 152
430
+#define B10011001 153
431
+#define B10011010 154
432
+#define B10011011 155
433
+#define B10011100 156
434
+#define B10011101 157
435
+#define B10011110 158
436
+#define B10011111 159
437
+#define B10100000 160
438
+#define B10100001 161
439
+#define B10100010 162
440
+#define B10100011 163
441
+#define B10100100 164
442
+#define B10100101 165
443
+#define B10100110 166
444
+#define B10100111 167
445
+#define B10101000 168
446
+#define B10101001 169
447
+#define B10101010 170
448
+#define B10101011 171
449
+#define B10101100 172
450
+#define B10101101 173
451
+#define B10101110 174
452
+#define B10101111 175
453
+#define B10110000 176
454
+#define B10110001 177
455
+#define B10110010 178
456
+#define B10110011 179
457
+#define B10110100 180
458
+#define B10110101 181
459
+#define B10110110 182
460
+#define B10110111 183
461
+#define B10111000 184
462
+#define B10111001 185
463
+#define B10111010 186
464
+#define B10111011 187
465
+#define B10111100 188
466
+#define B10111101 189
467
+#define B10111110 190
468
+#define B10111111 191
469
+#define B11000000 192
470
+#define B11000001 193
471
+#define B11000010 194
472
+#define B11000011 195
473
+#define B11000100 196
474
+#define B11000101 197
475
+#define B11000110 198
476
+#define B11000111 199
477
+#define B11001000 200
478
+#define B11001001 201
479
+#define B11001010 202
480
+#define B11001011 203
481
+#define B11001100 204
482
+#define B11001101 205
483
+#define B11001110 206
484
+#define B11001111 207
485
+#define B11010000 208
486
+#define B11010001 209
487
+#define B11010010 210
488
+#define B11010011 211
489
+#define B11010100 212
490
+#define B11010101 213
491
+#define B11010110 214
492
+#define B11010111 215
493
+#define B11011000 216
494
+#define B11011001 217
495
+#define B11011010 218
496
+#define B11011011 219
497
+#define B11011100 220
498
+#define B11011101 221
499
+#define B11011110 222
500
+#define B11011111 223
501
+#define B11100000 224
502
+#define B11100001 225
503
+#define B11100010 226
504
+#define B11100011 227
505
+#define B11100100 228
506
+#define B11100101 229
507
+#define B11100110 230
508
+#define B11100111 231
509
+#define B11101000 232
510
+#define B11101001 233
511
+#define B11101010 234
512
+#define B11101011 235
513
+#define B11101100 236
514
+#define B11101101 237
515
+#define B11101110 238
516
+#define B11101111 239
517
+#define B11110000 240
518
+#define B11110001 241
519
+#define B11110010 242
520
+#define B11110011 243
521
+#define B11110100 244
522
+#define B11110101 245
523
+#define B11110110 246
524
+#define B11110111 247
525
+#define B11111000 248
526
+#define B11111001 249
527
+#define B11111010 250
528
+#define B11111011 251
529
+#define B11111100 252
530
+#define B11111101 253
531
+#define B11111110 254
532
+#define B11111111 255
533
+
534
+#endif

+ 1143
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/LPC176x.h
File diff suppressed because it is too large
Näytä tiedosto


+ 109
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/diskio.h Näytä tiedosto

@@ -0,0 +1,109 @@
1
+/*-----------------------------------------------------------------------
2
+/  Low level disk interface modlue include file   (C)ChaN, 2015
3
+/-----------------------------------------------------------------------*/
4
+
5
+#ifndef _DISKIO_DEFINED
6
+#define _DISKIO_DEFINED
7
+
8
+#ifdef __cplusplus
9
+extern "C" {
10
+#endif
11
+
12
+#define _DISKIO_WRITE	1	/* 1: Enable disk_write function */
13
+#define _DISKIO_IOCTL	1	/* 1: Enable disk_ioctl fucntion */
14
+#define _DISKIO_ISDIO	0	/* 1: Enable iSDIO control fucntion */
15
+
16
+#include "integer.h"
17
+
18
+
19
+/* Status of Disk Functions */
20
+typedef BYTE	DSTATUS;
21
+
22
+/* Results of Disk Functions */
23
+typedef enum {
24
+	RES_OK = 0,		/* 0: Successful */
25
+	RES_ERROR,		/* 1: R/W Error */
26
+	RES_WRPRT,		/* 2: Write Protected */
27
+	RES_NOTRDY,		/* 3: Not Ready */
28
+	RES_PARERR		/* 4: Invalid Parameter */
29
+} DRESULT;
30
+
31
+
32
+#if	_DISKIO_ISDIO
33
+/* Command structure for iSDIO ioctl command */
34
+typedef struct {
35
+	BYTE	func;	/* Function number: 0..7 */
36
+	WORD	ndata;	/* Number of bytes to transfer: 1..512, or mask + data */
37
+	DWORD	addr;	/* Register address: 0..0x1FFFF */
38
+	void*	data;	/* Pointer to the data (to be written | read buffer) */
39
+} SDIO_CMD;
40
+#endif
41
+
42
+
43
+/*---------------------------------------*/
44
+/* Prototypes for disk control functions */
45
+
46
+
47
+DSTATUS disk_initialize (BYTE pdrv);
48
+DSTATUS disk_status (BYTE pdrv);
49
+DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
50
+#if	_DISKIO_WRITE
51
+DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
52
+#endif
53
+#if	_DISKIO_IOCTL
54
+DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
55
+#endif
56
+
57
+
58
+/* Disk Status Bits (DSTATUS) */
59
+#define STA_NOINIT		0x01	/* Drive not initialized */
60
+#define STA_NODISK		0x02	/* No medium in the drive */
61
+#define STA_PROTECT		0x04	/* Write protected */
62
+
63
+
64
+/* Command code for disk_ioctrl fucntion */
65
+
66
+/* Generic command (Used by FatFs) */
67
+#define CTRL_SYNC			0	/* Complete pending write process (needed at _FS_READONLY == 0) */
68
+#define GET_SECTOR_COUNT	1	/* Get media size (needed at _USE_MKFS == 1) */
69
+#define GET_SECTOR_SIZE		2	/* Get sector size (needed at _MAX_SS != _MIN_SS) */
70
+#define GET_BLOCK_SIZE		3	/* Get erase block size (needed at _USE_MKFS == 1) */
71
+#define CTRL_TRIM			4	/* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */
72
+
73
+/* Generic command (Not used by FatFs) */
74
+#define CTRL_FORMAT			5	/* Create physical format on the media */
75
+#define CTRL_POWER_IDLE		6	/* Put the device idle state */
76
+#define CTRL_POWER_OFF		7	/* Put the device off state */
77
+#define CTRL_LOCK			8	/* Lock media removal */
78
+#define CTRL_UNLOCK			9	/* Unlock media removal */
79
+#define CTRL_EJECT			10	/* Eject media */
80
+
81
+/* MMC/SDC specific ioctl command (Not used by FatFs) */
82
+#define MMC_GET_TYPE		50	/* Get card type */
83
+#define MMC_GET_CSD			51	/* Get CSD */
84
+#define MMC_GET_CID			52	/* Get CID */
85
+#define MMC_GET_OCR			53	/* Get OCR */
86
+#define MMC_GET_SDSTAT		54	/* Get SD status */
87
+#define ISDIO_READ			55	/* Read data form SD iSDIO register */
88
+#define ISDIO_WRITE			56	/* Write data to SD iSDIO register */
89
+#define ISDIO_MRITE			57	/* Masked write data to SD iSDIO register */
90
+
91
+/* ATA/CF specific ioctl command (Not used by FatFs) */
92
+#define ATA_GET_REV			60	/* Get F/W revision */
93
+#define ATA_GET_MODEL		61	/* Get model name */
94
+#define ATA_GET_SN			62	/* Get serial number */
95
+
96
+
97
+/* MMC card type flags (MMC_GET_TYPE) */
98
+#define CT_MMC		0x01		/* MMC ver 3 */
99
+#define CT_SD1		0x02		/* SD ver 1 */
100
+#define CT_SD2		0x04		/* SD ver 2 */
101
+#define CT_SDC		(CT_SD1|CT_SD2)	/* SD */
102
+#define CT_BLOCK	0x08		/* Block addressing */
103
+
104
+
105
+#ifdef __cplusplus
106
+}
107
+#endif
108
+
109
+#endif

+ 6204
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ff.c
File diff suppressed because it is too large
Näytä tiedosto


+ 364
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ff.h Näytä tiedosto

@@ -0,0 +1,364 @@
1
+/*----------------------------------------------------------------------------/
2
+/  FatFs - Generic FAT Filesystem module  R0.13                               /
3
+/-----------------------------------------------------------------------------/
4
+/
5
+/ Copyright (C) 2017, ChaN, all right reserved.
6
+/
7
+/ FatFs module is an open source software. Redistribution and use of FatFs in
8
+/ source and binary forms, with or without modification, are permitted provided
9
+/ that the following condition is met:
10
+
11
+/ 1. Redistributions of source code must retain the above copyright notice,
12
+/    this condition and the following disclaimer.
13
+/
14
+/ This software is provided by the copyright holder and contributors "AS IS"
15
+/ and any warranties related to this software are DISCLAIMED.
16
+/ The copyright owner or contributors be NOT LIABLE for any damages caused
17
+/ by use of this software.
18
+/
19
+/----------------------------------------------------------------------------*/
20
+
21
+
22
+#ifndef FF_DEFINED
23
+#define FF_DEFINED	87030	/* Revision ID */
24
+
25
+#ifdef __cplusplus
26
+extern "C" {
27
+#endif
28
+
29
+#include "integer.h"	/* Basic integer types */
30
+#include "ffconf.h"		/* FatFs configuration options */
31
+
32
+#if FF_DEFINED != FFCONF_DEF
33
+#error Wrong configuration file (ffconf.h).
34
+#endif
35
+
36
+
37
+
38
+/* Definitions of volume management */
39
+
40
+#if FF_MULTI_PARTITION		/* Multiple partition configuration */
41
+typedef struct {
42
+	BYTE pd;	/* Physical drive number */
43
+	BYTE pt;	/* Partition: 0:Auto detect, 1-4:Forced partition) */
44
+} PARTITION;
45
+extern PARTITION VolToPart[];	/* Volume - Partition resolution table */
46
+#endif
47
+
48
+
49
+
50
+/* Type of path name strings on FatFs API */
51
+
52
+#if FF_LFN_UNICODE && FF_USE_LFN	/* Unicode (UTF-16) string */
53
+#ifndef _INC_TCHAR
54
+typedef WCHAR TCHAR;
55
+#define _T(x) L ## x
56
+#define _TEXT(x) L ## x
57
+#define _INC_TCHAR
58
+#endif
59
+#else						/* ANSI/OEM string */
60
+#ifndef _INC_TCHAR
61
+typedef char TCHAR;
62
+#define _T(x) x
63
+#define _TEXT(x) x
64
+#define _INC_TCHAR
65
+#endif
66
+#endif
67
+
68
+
69
+
70
+/* Type of file size variables */
71
+
72
+#if FF_FS_EXFAT
73
+#if !FF_USE_LFN
74
+#error LFN must be enabled when enable exFAT
75
+#endif
76
+typedef QWORD FSIZE_t;
77
+#else
78
+typedef DWORD FSIZE_t;
79
+#endif
80
+
81
+
82
+
83
+/* Filesystem object structure (FATFS) */
84
+
85
+typedef struct {
86
+	BYTE	fs_type;		/* Filesystem type (0:N/A) */
87
+	BYTE	pdrv;			/* Physical drive number */
88
+	BYTE	n_fats;			/* Number of FATs (1 or 2) */
89
+	BYTE	wflag;			/* win[] flag (b0:dirty) */
90
+	BYTE	fsi_flag;		/* FSINFO flags (b7:disabled, b0:dirty) */
91
+	WORD	id;				/* Volume mount ID */
92
+	WORD	n_rootdir;		/* Number of root directory entries (FAT12/16) */
93
+	WORD	csize;			/* Cluster size [sectors] */
94
+#if FF_MAX_SS != FF_MIN_SS
95
+	WORD	ssize;			/* Sector size (512, 1024, 2048 or 4096) */
96
+#endif
97
+#if FF_USE_LFN
98
+	WCHAR*	lfnbuf;			/* LFN working buffer */
99
+#endif
100
+#if FF_FS_EXFAT
101
+	BYTE*	dirbuf;			/* Directory entry block scratchpad buffer for exFAT */
102
+#endif
103
+#if FF_FS_REENTRANT
104
+	FF_SYNC_t	sobj;		/* Identifier of sync object */
105
+#endif
106
+#if !FF_FS_READONLY
107
+	DWORD	last_clst;		/* Last allocated cluster */
108
+	DWORD	free_clst;		/* Number of free clusters */
109
+#endif
110
+#if FF_FS_RPATH
111
+	DWORD	cdir;			/* Current directory start cluster (0:root) */
112
+#if FF_FS_EXFAT
113
+	DWORD	cdc_scl;		/* Containing directory start cluster (invalid when cdir is 0) */
114
+	DWORD	cdc_size;		/* b31-b8:Size of containing directory, b7-b0: Chain status */
115
+	DWORD	cdc_ofs;		/* Offset in the containing directory (invalid when cdir is 0) */
116
+#endif
117
+#endif
118
+	DWORD	n_fatent;		/* Number of FAT entries (number of clusters + 2) */
119
+	DWORD	fsize;			/* Size of an FAT [sectors] */
120
+	DWORD	volbase;		/* Volume base sector */
121
+	DWORD	fatbase;		/* FAT base sector */
122
+	DWORD	dirbase;		/* Root directory base sector/cluster */
123
+	DWORD	database;		/* Data base sector */
124
+	DWORD	winsect;		/* Current sector appearing in the win[] */
125
+	BYTE	win[FF_MAX_SS];	/* Disk access window for Directory, FAT (and file data at tiny cfg) */
126
+} FATFS;
127
+
128
+
129
+
130
+/* Object ID and allocation information (FFOBJID) */
131
+
132
+typedef struct {
133
+	FATFS*	fs;				/* Pointer to the hosting volume of this object */
134
+	WORD	id;				/* Hosting volume mount ID */
135
+	BYTE	attr;			/* Object attribute */
136
+	BYTE	stat;			/* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:flagmented in this session, b2:sub-directory stretched) */
137
+	DWORD	sclust;			/* Object data start cluster (0:no cluster or root directory) */
138
+	FSIZE_t	objsize;		/* Object size (valid when sclust != 0) */
139
+#if FF_FS_EXFAT
140
+	DWORD	n_cont;			/* Size of first fragment - 1 (valid when stat == 3) */
141
+	DWORD	n_frag;			/* Size of last fragment needs to be written to FAT (valid when not zero) */
142
+	DWORD	c_scl;			/* Containing directory start cluster (valid when sclust != 0) */
143
+	DWORD	c_size;			/* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
144
+	DWORD	c_ofs;			/* Offset in the containing directory (valid when file object and sclust != 0) */
145
+#endif
146
+#if FF_FS_LOCK
147
+	UINT	lockid;			/* File lock ID origin from 1 (index of file semaphore table Files[]) */
148
+#endif
149
+} FFOBJID;
150
+
151
+
152
+
153
+/* File object structure (FIL) */
154
+
155
+typedef struct {
156
+	FFOBJID	obj;			/* Object identifier (must be the 1st member to detect invalid object pointer) */
157
+	BYTE	flag;			/* File status flags */
158
+	BYTE	err;			/* Abort flag (error code) */
159
+	FSIZE_t	fptr;			/* File read/write pointer (Zeroed on file open) */
160
+	DWORD	clust;			/* Current cluster of fpter (invalid when fptr is 0) */
161
+	DWORD	sect;			/* Sector number appearing in buf[] (0:invalid) */
162
+#if !FF_FS_READONLY
163
+	DWORD	dir_sect;		/* Sector number containing the directory entry (not used at exFAT) */
164
+	BYTE*	dir_ptr;		/* Pointer to the directory entry in the win[] (not used at exFAT) */
165
+#endif
166
+#if FF_USE_FASTSEEK
167
+	DWORD*	cltbl;			/* Pointer to the cluster link map table (nulled on open, set by application) */
168
+#endif
169
+#if !FF_FS_TINY
170
+	BYTE	buf[FF_MAX_SS];	/* File private data read/write window */
171
+#endif
172
+} FIL;
173
+
174
+
175
+
176
+/* Directory object structure (DIR) */
177
+
178
+typedef struct {
179
+	FFOBJID	obj;			/* Object identifier */
180
+	DWORD	dptr;			/* Current read/write offset */
181
+	DWORD	clust;			/* Current cluster */
182
+	DWORD	sect;			/* Current sector (0:Read operation has terminated) */
183
+	BYTE*	dir;			/* Pointer to the directory item in the win[] */
184
+	BYTE	fn[12];			/* SFN (in/out) {body[8],ext[3],status[1]} */
185
+#if FF_USE_LFN
186
+	DWORD	blk_ofs;		/* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
187
+#endif
188
+#if FF_USE_FIND
189
+	const TCHAR* pat;		/* Pointer to the name matching pattern */
190
+#endif
191
+} DIR;
192
+
193
+
194
+
195
+/* File information structure (FILINFO) */
196
+
197
+typedef struct {
198
+	FSIZE_t	fsize;			/* File size */
199
+	WORD	fdate;			/* Modified date */
200
+	WORD	ftime;			/* Modified time */
201
+	BYTE	fattrib;		/* File attribute */
202
+#if FF_USE_LFN
203
+	TCHAR	altname[13];			/* Altenative file name */
204
+	TCHAR	fname[FF_MAX_LFN + 1];	/* Primary file name */
205
+#else
206
+	TCHAR	fname[13];		/* File name */
207
+#endif
208
+} FILINFO;
209
+
210
+
211
+
212
+/* File function return code (FRESULT) */
213
+
214
+typedef enum {
215
+	FR_OK = 0,				/* (0) Succeeded */
216
+	FR_DISK_ERR,			/* (1) A hard error occurred in the low level disk I/O layer */
217
+	FR_INT_ERR,				/* (2) Assertion failed */
218
+	FR_NOT_READY,			/* (3) The physical drive cannot work */
219
+	FR_NO_FILE,				/* (4) Could not find the file */
220
+	FR_NO_PATH,				/* (5) Could not find the path */
221
+	FR_INVALID_NAME,		/* (6) The path name format is invalid */
222
+	FR_DENIED,				/* (7) Access denied due to prohibited access or directory full */
223
+	FR_EXIST,				/* (8) Access denied due to prohibited access */
224
+	FR_INVALID_OBJECT,		/* (9) The file/directory object is invalid */
225
+	FR_WRITE_PROTECTED,		/* (10) The physical drive is write protected */
226
+	FR_INVALID_DRIVE,		/* (11) The logical drive number is invalid */
227
+	FR_NOT_ENABLED,			/* (12) The volume has no work area */
228
+	FR_NO_FILESYSTEM,		/* (13) There is no valid FAT volume */
229
+	FR_MKFS_ABORTED,		/* (14) The f_mkfs() aborted due to any problem */
230
+	FR_TIMEOUT,				/* (15) Could not get a grant to access the volume within defined period */
231
+	FR_LOCKED,				/* (16) The operation is rejected according to the file sharing policy */
232
+	FR_NOT_ENOUGH_CORE,		/* (17) LFN working buffer could not be allocated */
233
+	FR_TOO_MANY_OPEN_FILES,	/* (18) Number of open files > FF_FS_LOCK */
234
+	FR_INVALID_PARAMETER	/* (19) Given parameter is invalid */
235
+} FRESULT;
236
+
237
+
238
+
239
+/*--------------------------------------------------------------*/
240
+/* FatFs module application interface                           */
241
+
242
+FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode);				/* Open or create a file */
243
+FRESULT f_close (FIL* fp);											/* Close an open file object */
244
+FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);			/* Read data from the file */
245
+FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw);	/* Write data to the file */
246
+FRESULT f_lseek (FIL* fp, FSIZE_t ofs);								/* Move file pointer of the file object */
247
+FRESULT f_truncate (FIL* fp);										/* Truncate the file */
248
+FRESULT f_sync (FIL* fp);											/* Flush cached data of the writing file */
249
+FRESULT f_opendir (DIR* dp, const TCHAR* path);						/* Open a directory */
250
+FRESULT f_closedir (DIR* dp);										/* Close an open directory */
251
+FRESULT f_readdir (DIR* dp, FILINFO* fno);							/* Read a directory item */
252
+FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern);	/* Find first file */
253
+FRESULT f_findnext (DIR* dp, FILINFO* fno);							/* Find next file */
254
+FRESULT f_mkdir (const TCHAR* path);								/* Create a sub directory */
255
+FRESULT f_unlink (const TCHAR* path);								/* Delete an existing file or directory */
256
+FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new);	/* Rename/Move a file or directory */
257
+FRESULT f_stat (const TCHAR* path, FILINFO* fno);					/* Get file status */
258
+FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask);			/* Change attribute of a file/dir */
259
+FRESULT f_utime (const TCHAR* path, const FILINFO* fno);			/* Change timestamp of a file/dir */
260
+FRESULT f_chdir (const TCHAR* path);								/* Change current directory */
261
+FRESULT f_chdrive (const TCHAR* path);								/* Change current drive */
262
+FRESULT f_getcwd (TCHAR* buff, UINT len);							/* Get current directory */
263
+FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs);	/* Get number of free clusters on the drive */
264
+FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn);	/* Get volume label */
265
+FRESULT f_setlabel (const TCHAR* label);							/* Set volume label */
266
+FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf);	/* Forward data to the stream */
267
+FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt);					/* Allocate a contiguous block to the file */
268
+FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt);			/* Mount/Unmount a logical drive */
269
+FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len);	/* Create a FAT volume */
270
+FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work);			/* Divide a physical drive into some partitions */
271
+FRESULT f_setcp (WORD cp);											/* Set current code page */
272
+int f_putc (TCHAR c, FIL* fp);										/* Put a character to the file */
273
+int f_puts (const TCHAR* str, FIL* cp);								/* Put a string to the file */
274
+int f_printf (FIL* fp, const TCHAR* str, ...);						/* Put a formatted string to the file */
275
+TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);						/* Get a string from the file */
276
+
277
+#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
278
+#define f_error(fp) ((fp)->err)
279
+#define f_tell(fp) ((fp)->fptr)
280
+#define f_size(fp) ((fp)->obj.objsize)
281
+#define f_rewind(fp) f_lseek((fp), 0)
282
+#define f_rewinddir(dp) f_readdir((dp), 0)
283
+#define f_rmdir(path) f_unlink(path)
284
+#define f_unmount(path) f_mount(0, path, 0)
285
+
286
+#ifndef EOF
287
+#define EOF (-1)
288
+#endif
289
+
290
+
291
+
292
+
293
+/*--------------------------------------------------------------*/
294
+/* Additional user defined functions                            */
295
+
296
+/* RTC function */
297
+#if !FF_FS_READONLY && !FF_FS_NORTC
298
+DWORD get_fattime (void);
299
+#endif
300
+
301
+/* LFN support functions */
302
+#if FF_USE_LFN						/* Code conversion (defined in unicode.c) */
303
+WCHAR ff_oem2uni (WCHAR oem, WORD cp);	/* OEM code to Unicode conversion */
304
+WCHAR ff_uni2oem (WCHAR uni, WORD cp);	/* Unicode to OEM code conversion */
305
+WCHAR ff_wtoupper (WCHAR uni);			/* Unicode upper-case conversion */
306
+#endif
307
+#if FF_USE_LFN == 3						/* Dynamic memory allocation */
308
+void* ff_memalloc (UINT msize);			/* Allocate memory block */
309
+void ff_memfree (void* mblock);			/* Free memory block */
310
+#endif
311
+
312
+/* Sync functions */
313
+#if FF_FS_REENTRANT
314
+int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj);	/* Create a sync object */
315
+int ff_req_grant (FF_SYNC_t sobj);		/* Lock sync object */
316
+void ff_rel_grant (FF_SYNC_t sobj);		/* Unlock sync object */
317
+int ff_del_syncobj (FF_SYNC_t sobj);	/* Delete a sync object */
318
+#endif
319
+
320
+
321
+
322
+
323
+/*--------------------------------------------------------------*/
324
+/* Flags and offset address                                     */
325
+
326
+
327
+/* File access mode and open method flags (3rd argument of f_open) */
328
+#define	FA_READ				0x01
329
+#define	FA_WRITE			0x02
330
+#define	FA_OPEN_EXISTING	0x00
331
+#define	FA_CREATE_NEW		0x04
332
+#define	FA_CREATE_ALWAYS	0x08
333
+#define	FA_OPEN_ALWAYS		0x10
334
+#define	FA_OPEN_APPEND		0x30
335
+
336
+/* Fast seek controls (2nd argument of f_lseek) */
337
+#define CREATE_LINKMAP	((FSIZE_t)0 - 1)
338
+
339
+/* Format options (2nd argument of f_mkfs) */
340
+#define FM_FAT		0x01
341
+#define FM_FAT32	0x02
342
+#define FM_EXFAT	0x04
343
+#define FM_ANY		0x07
344
+#define FM_SFD		0x08
345
+
346
+/* Filesystem type (FATFS.fs_type) */
347
+#define FS_FAT12	1
348
+#define FS_FAT16	2
349
+#define FS_FAT32	3
350
+#define FS_EXFAT	4
351
+
352
+/* File attribute bits for directory entry (FILINFO.fattrib) */
353
+#define	AM_RDO	0x01	/* Read only */
354
+#define	AM_HID	0x02	/* Hidden */
355
+#define	AM_SYS	0x04	/* System */
356
+#define AM_DIR	0x10	/* Directory */
357
+#define AM_ARC	0x20	/* Archive */
358
+
359
+
360
+#ifdef __cplusplus
361
+}
362
+#endif
363
+
364
+#endif /* FF_DEFINED */

+ 269
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ffconf.h Näytä tiedosto

@@ -0,0 +1,269 @@
1
+/*---------------------------------------------------------------------------/
2
+/  FatFs - Configuration file
3
+/---------------------------------------------------------------------------*/
4
+
5
+#define FFCONF_DEF 87030	/* Revision ID */
6
+
7
+/*---------------------------------------------------------------------------/
8
+/ Function Configurations
9
+/---------------------------------------------------------------------------*/
10
+
11
+#define FF_FS_READONLY	0
12
+/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
13
+/  Read-only configuration removes writing API functions, f_write(), f_sync(),
14
+/  f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
15
+/  and optional writing functions as well. */
16
+
17
+
18
+#define FF_FS_MINIMIZE	0
19
+/* This option defines minimization level to remove some basic API functions.
20
+/
21
+/   0: All basic functions are enabled.
22
+/   1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
23
+/      are removed.
24
+/   2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
25
+/   3: f_lseek() function is removed in addition to 2. */
26
+
27
+
28
+#define FF_USE_STRFUNC	0
29
+/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
30
+/
31
+/  0: Disable string functions.
32
+/  1: Enable without LF-CRLF conversion.
33
+/  2: Enable with LF-CRLF conversion. */
34
+
35
+
36
+#define FF_USE_FIND		1
37
+/* This option switches filtered directory read functions, f_findfirst() and
38
+/  f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
39
+
40
+
41
+#define FF_USE_MKFS		1
42
+/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
43
+
44
+
45
+#define FF_USE_FASTSEEK	0
46
+/* This option switches fast seek function. (0:Disable or 1:Enable) */
47
+
48
+
49
+#define FF_USE_EXPAND	0
50
+/* This option switches f_expand function. (0:Disable or 1:Enable) */
51
+
52
+
53
+#define FF_USE_CHMOD	1
54
+/* This option switches attribute manipulation functions, f_chmod() and f_utime().
55
+/  (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
56
+
57
+
58
+#define FF_USE_LABEL	1
59
+/* This option switches volume label functions, f_getlabel() and f_setlabel().
60
+/  (0:Disable or 1:Enable) */
61
+
62
+
63
+#define FF_USE_FORWARD	0
64
+/* This option switches f_forward() function. (0:Disable or 1:Enable) */
65
+
66
+
67
+/*---------------------------------------------------------------------------/
68
+/ Locale and Namespace Configurations
69
+/---------------------------------------------------------------------------*/
70
+
71
+#define FF_CODE_PAGE	932
72
+/* This option specifies the OEM code page to be used on the target system.
73
+/  Incorrect code page setting can cause a file open failure.
74
+/
75
+/   437 - U.S.
76
+/   720 - Arabic
77
+/   737 - Greek
78
+/   771 - KBL
79
+/   775 - Baltic
80
+/   850 - Latin 1
81
+/   852 - Latin 2
82
+/   855 - Cyrillic
83
+/   857 - Turkish
84
+/   860 - Portuguese
85
+/   861 - Icelandic
86
+/   862 - Hebrew
87
+/   863 - Canadian French
88
+/   864 - Arabic
89
+/   865 - Nordic
90
+/   866 - Russian
91
+/   869 - Greek 2
92
+/   932 - Japanese (DBCS)
93
+/   936 - Simplified Chinese (DBCS)
94
+/   949 - Korean (DBCS)
95
+/   950 - Traditional Chinese (DBCS)
96
+/     0 - Include all code pages above and configured by f_setcp()
97
+*/
98
+
99
+
100
+#define FF_USE_LFN		1
101
+#define FF_MAX_LFN		255
102
+/* The FF_USE_LFN switches the support for LFN (long file name).
103
+/
104
+/   0: Disable LFN. FF_MAX_LFN has no effect.
105
+/   1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
106
+/   2: Enable LFN with dynamic working buffer on the STACK.
107
+/   3: Enable LFN with dynamic working buffer on the HEAP.
108
+/
109
+/  To enable the LFN, Unicode handling functions (option/unicode.c) must be added
110
+/  to the project. The working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
111
+/  additional 608 bytes at exFAT enabled. FF_MAX_LFN can be in range from 12 to 255.
112
+/  It should be set 255 to support full featured LFN operations.
113
+/  When use stack for the working buffer, take care on stack overflow. When use heap
114
+/  memory for the working buffer, memory management functions, ff_memalloc() and
115
+/  ff_memfree(), must be added to the project. */
116
+
117
+
118
+#define FF_LFN_UNICODE	0
119
+/* This option switches character encoding on the API, 0:ANSI/OEM or 1:UTF-16,
120
+/  when LFN is enabled. Also behavior of string I/O functions will be affected by
121
+/  this option. When LFN is not enabled, this option has no effect.
122
+*/
123
+
124
+
125
+#define FF_STRF_ENCODE	3
126
+/* When FF_LFN_UNICODE = 1 with LFN enabled, string I/O functions, f_gets(),
127
+/  f_putc(), f_puts and f_printf() convert the character encoding in it.
128
+/  This option selects assumption of character encoding ON THE FILE to be
129
+/  read/written via those functions.
130
+/
131
+/   0: ANSI/OEM
132
+/   1: UTF-16LE
133
+/   2: UTF-16BE
134
+/   3: UTF-8
135
+*/
136
+
137
+
138
+#define FF_FS_RPATH		2
139
+/* This option configures support for relative path.
140
+/
141
+/   0: Disable relative path and remove related functions.
142
+/   1: Enable relative path. f_chdir() and f_chdrive() are available.
143
+/   2: f_getcwd() function is available in addition to 1.
144
+*/
145
+
146
+
147
+/*---------------------------------------------------------------------------/
148
+/ Drive/Volume Configurations
149
+/---------------------------------------------------------------------------*/
150
+
151
+#define FF_VOLUMES		1
152
+/* Number of volumes (logical drives) to be used. (1-10) */
153
+
154
+
155
+#define FF_STR_VOLUME_ID	0
156
+#define FF_VOLUME_STRS		"RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
157
+/* FF_STR_VOLUME_ID switches string support for volume ID.
158
+/  When FF_STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive
159
+/  number in the path name. FF_VOLUME_STRS defines the drive ID strings for each
160
+/  logical drives. Number of items must be equal to FF_VOLUMES. Valid characters for
161
+/  the drive ID strings are: A-Z and 0-9. */
162
+
163
+
164
+#define FF_MULTI_PARTITION	0
165
+/* This option switches support for multiple volumes on the physical drive.
166
+/  By default (0), each logical drive number is bound to the same physical drive
167
+/  number and only an FAT volume found on the physical drive will be mounted.
168
+/  When this function is enabled (1), each logical drive number can be bound to
169
+/  arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
170
+/  funciton will be available. */
171
+
172
+
173
+#define FF_MIN_SS		512
174
+#define FF_MAX_SS		512
175
+/* This set of options configures the range of sector size to be supported. (512,
176
+/  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
177
+/  harddisk. But a larger value may be required for on-board flash memory and some
178
+/  type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
179
+/  for variable sector size mode and disk_ioctl() function needs to implement
180
+/  GET_SECTOR_SIZE command. */
181
+
182
+
183
+#define FF_USE_TRIM		0
184
+/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
185
+/  To enable Trim function, also CTRL_TRIM command should be implemented to the
186
+/  disk_ioctl() function. */
187
+
188
+
189
+#define FF_FS_NOFSINFO	0
190
+/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
191
+/  option, and f_getfree() function at first time after volume mount will force
192
+/  a full FAT scan. Bit 1 controls the use of last allocated cluster number.
193
+/
194
+/  bit0=0: Use free cluster count in the FSINFO if available.
195
+/  bit0=1: Do not trust free cluster count in the FSINFO.
196
+/  bit1=0: Use last allocated cluster number in the FSINFO if available.
197
+/  bit1=1: Do not trust last allocated cluster number in the FSINFO.
198
+*/
199
+
200
+
201
+
202
+/*---------------------------------------------------------------------------/
203
+/ System Configurations
204
+/---------------------------------------------------------------------------*/
205
+
206
+#define FF_FS_TINY		0
207
+/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
208
+/  At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
209
+/  Instead of private sector buffer eliminated from the file object, common sector
210
+/  buffer in the filesystem object (FATFS) is used for the file data transfer. */
211
+
212
+
213
+#define FF_FS_EXFAT		0
214
+/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
215
+/  When enable exFAT, also LFN needs to be enabled.
216
+/  Note that enabling exFAT discards ANSI C (C89) compatibility. */
217
+
218
+
219
+#define FF_FS_NORTC		0
220
+#define FF_NORTC_MON	1
221
+#define FF_NORTC_MDAY	1
222
+#define FF_NORTC_YEAR	2016
223
+/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
224
+/  any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
225
+/  the timestamp function. All objects modified by FatFs will have a fixed timestamp
226
+/  defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
227
+/  To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
228
+/  added to the project to read current time form real-time clock. FF_NORTC_MON,
229
+/  FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
230
+/  These options have no effect at read-only configuration (FF_FS_READONLY = 1). */
231
+
232
+
233
+#define FF_FS_LOCK		0
234
+/* The option FF_FS_LOCK switches file lock function to control duplicated file open
235
+/  and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
236
+/  is 1.
237
+/
238
+/  0:  Disable file lock function. To avoid volume corruption, application program
239
+/      should avoid illegal open, remove and rename to the open objects.
240
+/  >0: Enable file lock function. The value defines how many files/sub-directories
241
+/      can be opened simultaneously under file lock control. Note that the file
242
+/      lock control is independent of re-entrancy. */
243
+
244
+
245
+#define FF_FS_REENTRANT	0
246
+#define FF_FS_TIMEOUT	1000
247
+#define FF_SYNC_t		HANDLE
248
+/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
249
+/  module itself. Note that regardless of this option, file access to different
250
+/  volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
251
+/  and f_fdisk() function, are always not re-entrant. Only file/directory access
252
+/  to the same volume is under control of this function.
253
+/
254
+/   0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
255
+/   1: Enable re-entrancy. Also user provided synchronization handlers,
256
+/      ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
257
+/      function, must be added to the project. Samples are available in
258
+/      option/syscall.c.
259
+/
260
+/  The FF_FS_TIMEOUT defines timeout period in unit of time tick.
261
+/  The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
262
+/  SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
263
+/  included somewhere in the scope of ff.h. */
264
+
265
+/* #include <windows.h>	// O/S definitions  */
266
+
267
+
268
+
269
+/*--- End of configuration options ---*/

+ 15566
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/ffunicode.c
File diff suppressed because it is too large
Näytä tiedosto


+ 38
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/integer.h Näytä tiedosto

@@ -0,0 +1,38 @@
1
+/*-------------------------------------------*/
2
+/* Integer type definitions for FatFs module */
3
+/*-------------------------------------------*/
4
+
5
+#ifndef _FF_INTEGER
6
+#define _FF_INTEGER
7
+
8
+#ifdef _WIN32	/* FatFs development platform */
9
+
10
+#include <windows.h>
11
+#include <tchar.h>
12
+typedef unsigned __int64 QWORD;
13
+
14
+
15
+#else			/* Embedded platform */
16
+
17
+/* These types MUST be 16-bit or 32-bit */
18
+typedef int				INT;
19
+typedef unsigned int	UINT;
20
+
21
+/* This type MUST be 8-bit */
22
+typedef unsigned char	BYTE;
23
+
24
+/* These types MUST be 16-bit */
25
+typedef short			SHORT;
26
+typedef unsigned short	WORD;
27
+typedef unsigned short	WCHAR;
28
+
29
+/* These types MUST be 32-bit */
30
+typedef long			LONG;
31
+typedef unsigned long	DWORD;
32
+
33
+/* This type MUST be 64-bit (Remove this for C89 compatibility) */
34
+typedef unsigned long long QWORD;
35
+
36
+#endif
37
+
38
+#endif

+ 745
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/mmc_ssp.c Näytä tiedosto

@@ -0,0 +1,745 @@
1
+/*------------------------------------------------------------------------*/
2
+/* LPCXpresso176x: MMCv3/SDv1/SDv2 (SPI mode) control module              */
3
+/*------------------------------------------------------------------------*/
4
+/*
5
+/  Copyright (C) 2015, ChaN, all right reserved.
6
+/
7
+/ * This software is a free software and there is NO WARRANTY.
8
+/ * No restriction on use. You can use, modify and redistribute it for
9
+/   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
10
+/ * Redistributions of source code must retain the above copyright notice.
11
+/
12
+/-------------------------------------------------------------------------*/
13
+
14
+#define SSP_CH	1	/* SSP channel to use (0:SSP0, 1:SSP1) */
15
+
16
+#define	CCLK		100000000UL	/* cclk frequency [Hz] */
17
+#define PCLK_SSP	50000000UL	/* PCLK frequency to be supplied for SSP [Hz] */
18
+#define SCLK_FAST	25000000UL	/* SCLK frequency under normal operation [Hz] */
19
+#define	SCLK_SLOW	400000UL	/* SCLK frequency under initialization [Hz] */
20
+
21
+//#define	MMC_CD		(!(FIO2PIN1 & _BV(1)))	/* Card detect (yes:true, no:false, default:true) */
22
+#define	MMC_WP		0						/* Write protected (yes:true, no:false, default:false) */
23
+
24
+#if SSP_CH == 0
25
+#define	SSPxDR		SSP0DR
26
+#define	SSPxSR		SSP0SR
27
+#define	SSPxCR0		SSP0CR0
28
+#define	SSPxCR1		SSP0CR1
29
+#define	SSPxCPSR	SSP0CPSR
30
+#define	CS_LOW()	{FIO0CLR2 = _BV(0);}	/* Set P0.16 low */
31
+#define	CS_HIGH()	{FIO0SET2 = _BV(0);}	/* Set P0.16 high */
32
+#define PCSSPx		PCSSP0
33
+#define	PCLKSSPx	PCLK_SSP0
34
+#define ATTACH_SSP() {\
35
+		__set_PINSEL(0, 15, 2);	/* SCK0 */\
36
+		__set_PINSEL(0, 17, 2);	/* MISO0 */\
37
+		__set_PINSEL(0, 18, 2);	/* MOSI0 */\
38
+		FIO0DIR |= _BV(16);		/* CS# (P0.16) */\
39
+		}
40
+#elif SSP_CH == 1
41
+#define	SSPxDR		SSP1DR
42
+#define	SSPxSR		SSP1SR
43
+#define	SSPxCR0		SSP1CR0
44
+#define	SSPxCR1		SSP1CR1
45
+#define	SSPxCPSR	SSP1CPSR
46
+#define	CS_LOW()	{FIO0CLR0 = _BV(6);}	/* Set P0.6 low */
47
+#define	CS_HIGH()	{FIO0SET0 = _BV(6);}	/* Set P0.6 high */
48
+#define PCSSPx		PCSSP1
49
+#define	PCLKSSPx	PCLK_SSP1
50
+#define ATTACH_SSP() {\
51
+		__set_PINSEL(0, 7, 2);	/* SCK1 */\
52
+		__set_PINSEL(0, 8, 2);	/* MISO1 */\
53
+		__set_PINSEL(0, 9, 2);	/* MOSI1 */\
54
+		FIO0DIR |= _BV(6);		/* CS# (P0.6) */\
55
+		}
56
+#endif
57
+
58
+#if PCLK_SSP * 1 == CCLK
59
+#define PCLKDIV_SSP	PCLKDIV_1
60
+#elif PCLK_SSP * 2 == CCLK
61
+#define PCLKDIV_SSP	PCLKDIV_2
62
+#elif PCLK_SSP * 4 == CCLK
63
+#define PCLKDIV_SSP	PCLKDIV_4
64
+#elif PCLK_SSP * 8 == CCLK
65
+#define PCLKDIV_SSP	PCLKDIV_8
66
+#else
67
+#error Invalid CCLK:PCLK_SSP combination.
68
+#endif
69
+
70
+
71
+#define FCLK_FAST() { SSPxCR0 = (SSPxCR0 & 0x00FF) | ((PCLK_SSP / 2 / SCLK_FAST) - 1) << 8; }
72
+#define FCLK_SLOW() { SSPxCR0 = (SSPxCR0 & 0x00FF) | ((PCLK_SSP / 2 / SCLK_SLOW) - 1) << 8; }
73
+
74
+
75
+
76
+/*--------------------------------------------------------------------------
77
+
78
+   Module Private Functions
79
+
80
+---------------------------------------------------------------------------*/
81
+
82
+#include "LPC176x.h"
83
+#include "diskio.h"
84
+
85
+
86
+/* MMC/SD command */
87
+#define CMD0	(0)			/* GO_IDLE_STATE */
88
+#define CMD1	(1)			/* SEND_OP_COND (MMC) */
89
+#define	ACMD41	(0x80+41)	/* SEND_OP_COND (SDC) */
90
+#define CMD8	(8)			/* SEND_IF_COND */
91
+#define CMD9	(9)			/* SEND_CSD */
92
+#define CMD10	(10)		/* SEND_CID */
93
+#define CMD12	(12)		/* STOP_TRANSMISSION */
94
+#define ACMD13	(0x80+13)	/* SD_STATUS (SDC) */
95
+#define CMD16	(16)		/* SET_BLOCKLEN */
96
+#define CMD17	(17)		/* READ_SINGLE_BLOCK */
97
+#define CMD18	(18)		/* READ_MULTIPLE_BLOCK */
98
+#define CMD23	(23)		/* SET_BLOCK_COUNT (MMC) */
99
+#define	ACMD23	(0x80+23)	/* SET_WR_BLK_ERASE_COUNT (SDC) */
100
+#define CMD24	(24)		/* WRITE_BLOCK */
101
+#define CMD25	(25)		/* WRITE_MULTIPLE_BLOCK */
102
+#define CMD32	(32)		/* ERASE_ER_BLK_START */
103
+#define CMD33	(33)		/* ERASE_ER_BLK_END */
104
+#define CMD38	(38)		/* ERASE */
105
+#define	CMD48	(48)		/* READ_EXTR_SINGLE */
106
+#define	CMD49	(49)		/* WRITE_EXTR_SINGLE */
107
+#define CMD55	(55)		/* APP_CMD */
108
+#define CMD58	(58)		/* READ_OCR */
109
+
110
+
111
+static volatile
112
+DSTATUS Stat = STA_NOINIT;	/* Physical drive status */
113
+
114
+static volatile
115
+UINT Timer1, Timer2;	/* 1kHz decrement timer stopped at zero (disk_timerproc()) */
116
+
117
+static
118
+BYTE CardType;			/* Card type flags */
119
+
120
+
121
+
122
+/*-----------------------------------------------------------------------*/
123
+/* Send/Receive data to the MMC  (Platform dependent)                    */
124
+/*-----------------------------------------------------------------------*/
125
+
126
+/* Exchange a byte */
127
+static
128
+BYTE xchg_spi (
129
+	BYTE dat	/* Data to send */
130
+)
131
+{
132
+	SSPxDR = dat;
133
+	while (SSPxSR & 0x10) ;
134
+	return SSPxDR;
135
+}
136
+
137
+
138
+/* Receive multiple byte */
139
+static
140
+void rcvr_spi_multi (
141
+	BYTE *buff,		/* Pointer to data buffer */
142
+	UINT btr		/* Number of bytes to receive (16, 64 or 512) */
143
+)
144
+{
145
+	UINT n;
146
+	WORD d;
147
+
148
+
149
+	SSPxCR0 |= 0x000F;	/* Select 16-bit mode */
150
+
151
+	for (n = 0; n < 8; n++)			/* Push 8 frames into pipeline  */
152
+		SSPxDR = 0xFFFF;
153
+	btr -= 16;
154
+
155
+	while (btr >= 2) {				/* Receive the data block into buffer */
156
+		btr -= 2;
157
+		while (!(SSPxSR & _BV(2))) ;	/* Wait for any data in receive FIFO */
158
+		d = SSPxDR;
159
+		SSPxDR = 0xFFFF;
160
+		*buff++ = d >> 8;
161
+		*buff++ = d;
162
+	}
163
+
164
+	for (n = 0; n < 8; n++) {		/* Pop remaining frames from pipeline */
165
+		while (!(SSPxSR & _BV(2))) ;
166
+		d = SSPxDR;
167
+		*buff++ = d >> 8;
168
+		*buff++ = d;
169
+	}
170
+
171
+	SSPxCR0 &= 0xFFF7;				/* Select 8-bit mode */
172
+}
173
+
174
+
175
+#if _DISKIO_WRITE
176
+/* Send multiple byte */
177
+static
178
+void xmit_spi_multi (
179
+	const BYTE *buff,	/* Pointer to the data */
180
+	UINT btx			/* Number of bytes to send (multiple of 16) */
181
+)
182
+{
183
+	UINT n;
184
+	WORD d;
185
+
186
+
187
+	SSPxCR0 |= 0x000F;			/* Select 16-bit mode */
188
+
189
+	for (n = 0; n < 8; n++) {	/* Push 8 frames into pipeline  */
190
+		d = *buff++;
191
+		d = d << 8 | *buff++;
192
+		SSPxDR = d;
193
+	}
194
+	btx -= 16;
195
+
196
+	while (btx >= 2) {			/* Transmit data block */
197
+		btx -= 2;
198
+		d = *buff++;
199
+		d = d << 8 | *buff++;
200
+		while (!(SSPxSR & _BV(2))) ;	/* Wait for any data in receive FIFO */
201
+		SSPxDR; SSPxDR = d;
202
+	}
203
+
204
+	for (n = 0; n < 8; n++) {	/* Flush pipeline */
205
+		while (!(SSPxSR & _BV(2))) ;
206
+		SSPxDR;
207
+	}
208
+
209
+	SSPxCR0 &= 0xFFF7;			/* Select 8-bit mode */
210
+}
211
+#endif
212
+
213
+
214
+/*-----------------------------------------------------------------------*/
215
+/* Wait for card ready                                                   */
216
+/*-----------------------------------------------------------------------*/
217
+
218
+static
219
+int wait_ready (	/* 1:Ready, 0:Timeout */
220
+	UINT wt			/* Timeout [ms] */
221
+)
222
+{
223
+	BYTE d;
224
+
225
+
226
+	Timer2 = wt;
227
+	do {
228
+		d = xchg_spi(0xFF);
229
+
230
+		/* This loop takes a time. Insert rot_rdq() here for multitask envilonment. */
231
+
232
+	} while (d != 0xFF && Timer2);	/* Wait for card goes ready or timeout */
233
+
234
+	return (d == 0xFF) ? 1 : 0;
235
+}
236
+
237
+
238
+
239
+/*-----------------------------------------------------------------------*/
240
+/* Deselect card and release SPI                                         */
241
+/*-----------------------------------------------------------------------*/
242
+
243
+static
244
+void deselect (void)
245
+{
246
+	CS_HIGH();		/* CS = H */
247
+	xchg_spi(0xFF);	/* Dummy clock (force DO hi-z for multiple slave SPI) */
248
+}
249
+
250
+
251
+
252
+/*-----------------------------------------------------------------------*/
253
+/* Select card and wait for ready                                        */
254
+/*-----------------------------------------------------------------------*/
255
+
256
+static
257
+int select (void)	/* 1:OK, 0:Timeout */
258
+{
259
+	CS_LOW();		/* CS = L */
260
+	xchg_spi(0xFF);	/* Dummy clock (force DO enabled) */
261
+
262
+	if (wait_ready(500)) return 1;	/* Leading busy check: Wait for card ready */
263
+
264
+	deselect();		/* Timeout */
265
+	return 0;
266
+}
267
+
268
+
269
+
270
+/*-----------------------------------------------------------------------*/
271
+/* Control SPI module (Platform dependent)                               */
272
+/*-----------------------------------------------------------------------*/
273
+
274
+static
275
+void power_on (void)	/* Enable SSP module and attach it to I/O pads */
276
+{
277
+	__set_PCONP(PCSSPx, 1);	/* Enable SSP module */
278
+	__set_PCLKSEL(PCLKSSPx, PCLKDIV_SSP);	/* Select PCLK frequency for SSP */
279
+	SSPxCPSR = 2;			/* CPSDVSR=2 */
280
+	SSPxCR0 = 0x0007;		/* Set mode: SPI mode 0, 8-bit */
281
+	SSPxCR1 = 0x2;			/* Enable SSP with Master */
282
+	ATTACH_SSP();			/* Attach SSP module to I/O pads */
283
+	CS_HIGH();				/* Set CS# high */
284
+
285
+	for (Timer1 = 10; Timer1; ) ;	/* 10ms */
286
+}
287
+
288
+
289
+static
290
+void power_off (void)		/* Disable SPI function */
291
+{
292
+	select();				/* Wait for card ready */
293
+	deselect();
294
+}
295
+
296
+
297
+
298
+/*-----------------------------------------------------------------------*/
299
+/* Receive a data packet from the MMC                                    */
300
+/*-----------------------------------------------------------------------*/
301
+
302
+static
303
+int rcvr_datablock (	/* 1:OK, 0:Error */
304
+	BYTE *buff,			/* Data buffer */
305
+	UINT btr			/* Data block length (byte) */
306
+)
307
+{
308
+	BYTE token;
309
+
310
+
311
+	Timer1 = 200;
312
+	do {							/* Wait for DataStart token in timeout of 200ms */
313
+		token = xchg_spi(0xFF);
314
+
315
+		/* This loop will take a time. Insert rot_rdq() here for multitask envilonment. */
316
+
317
+	} while ((token == 0xFF) && Timer1);
318
+	if(token != 0xFE) return 0;		/* Function fails if invalid DataStart token or timeout */
319
+
320
+	rcvr_spi_multi(buff, btr);		/* Store trailing data to the buffer */
321
+	xchg_spi(0xFF); xchg_spi(0xFF);	/* Discard CRC */
322
+
323
+	return 1;						/* Function succeeded */
324
+}
325
+
326
+
327
+
328
+/*-----------------------------------------------------------------------*/
329
+/* Send a data packet to the MMC                                         */
330
+/*-----------------------------------------------------------------------*/
331
+
332
+#if _DISKIO_WRITE
333
+static
334
+int xmit_datablock (	/* 1:OK, 0:Failed */
335
+	const BYTE *buff,	/* Ponter to 512 byte data to be sent */
336
+	BYTE token			/* Token */
337
+)
338
+{
339
+	BYTE resp;
340
+
341
+
342
+	if (!wait_ready(500)) return 0;		/* Leading busy check: Wait for card ready to accept data block */
343
+
344
+	xchg_spi(token);					/* Send token */
345
+	if (token == 0xFD) return 1;		/* Do not send data if token is StopTran */
346
+
347
+	xmit_spi_multi(buff, 512);			/* Data */
348
+	xchg_spi(0xFF); xchg_spi(0xFF);		/* Dummy CRC */
349
+
350
+	resp = xchg_spi(0xFF);				/* Receive data resp */
351
+
352
+	return (resp & 0x1F) == 0x05 ? 1 : 0;	/* Data was accepted or not */
353
+
354
+	/* Busy check is done at next transmission */
355
+}
356
+#endif
357
+
358
+
359
+/*-----------------------------------------------------------------------*/
360
+/* Send a command packet to the MMC                                      */
361
+/*-----------------------------------------------------------------------*/
362
+
363
+static
364
+BYTE send_cmd (		/* Return value: R1 resp (bit7==1:Failed to send) */
365
+	BYTE cmd,		/* Command index */
366
+	DWORD arg		/* Argument */
367
+)
368
+{
369
+	BYTE n, res;
370
+
371
+
372
+	if (cmd & 0x80) {	/* Send a CMD55 prior to ACMD<n> */
373
+		cmd &= 0x7F;
374
+		res = send_cmd(CMD55, 0);
375
+		if (res > 1) return res;
376
+	}
377
+
378
+	/* Select the card and wait for ready except to stop multiple block read */
379
+	if (cmd != CMD12) {
380
+		deselect();
381
+		if (!select()) return 0xFF;
382
+	}
383
+
384
+	/* Send command packet */
385
+	xchg_spi(0x40 | cmd);				/* Start + command index */
386
+	xchg_spi((BYTE)(arg >> 24));		/* Argument[31..24] */
387
+	xchg_spi((BYTE)(arg >> 16));		/* Argument[23..16] */
388
+	xchg_spi((BYTE)(arg >> 8));			/* Argument[15..8] */
389
+	xchg_spi((BYTE)arg);				/* Argument[7..0] */
390
+	n = 0x01;							/* Dummy CRC + Stop */
391
+	if (cmd == CMD0) n = 0x95;			/* Valid CRC for CMD0(0) */
392
+	if (cmd == CMD8) n = 0x87;			/* Valid CRC for CMD8(0x1AA) */
393
+	xchg_spi(n);
394
+
395
+	/* Receive command resp */
396
+	if (cmd == CMD12) xchg_spi(0xFF);	/* Diacard following one byte when CMD12 */
397
+	n = 10;								/* Wait for response (10 bytes max) */
398
+	do
399
+		res = xchg_spi(0xFF);
400
+	while ((res & 0x80) && --n);
401
+
402
+	return res;							/* Return received response */
403
+}
404
+
405
+
406
+
407
+/*--------------------------------------------------------------------------
408
+
409
+   Public Functions
410
+
411
+---------------------------------------------------------------------------*/
412
+
413
+
414
+/*-----------------------------------------------------------------------*/
415
+/* Initialize disk drive                                                 */
416
+/*-----------------------------------------------------------------------*/
417
+
418
+DSTATUS disk_initialize (
419
+	BYTE drv		/* Physical drive number (0) */
420
+)
421
+{
422
+	BYTE n, cmd, ty, ocr[4];
423
+
424
+
425
+	if (drv) return STA_NOINIT;			/* Supports only drive 0 */
426
+	power_on();							/* Initialize SPI */
427
+
428
+	if (Stat & STA_NODISK) return Stat;	/* Is a card existing in the soket? */
429
+
430
+	FCLK_SLOW();
431
+	for (n = 10; n; n--) xchg_spi(0xFF);	/* Send 80 dummy clocks */
432
+
433
+	ty = 0;
434
+	if (send_cmd(CMD0, 0) == 1) {			/* Put the card SPI state */
435
+		Timer1 = 1000;						/* Initialization timeout = 1 sec */
436
+		if (send_cmd(CMD8, 0x1AA) == 1) {	/* Is the catd SDv2? */
437
+			for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);	/* Get 32 bit return value of R7 resp */
438
+			if (ocr[2] == 0x01 && ocr[3] == 0xAA) {				/* Does the card support 2.7-3.6V? */
439
+				while (Timer1 && send_cmd(ACMD41, 1UL << 30)) ;	/* Wait for end of initialization with ACMD41(HCS) */
440
+				if (Timer1 && send_cmd(CMD58, 0) == 0) {		/* Check CCS bit in the OCR */
441
+					for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
442
+					ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;	/* Check if the card is SDv2 */
443
+				}
444
+			}
445
+		} else {	/* Not an SDv2 card */
446
+			if (send_cmd(ACMD41, 0) <= 1) 	{	/* SDv1 or MMCv3? */
447
+				ty = CT_SD1; cmd = ACMD41;	/* SDv1 (ACMD41(0)) */
448
+			} else {
449
+				ty = CT_MMC; cmd = CMD1;	/* MMCv3 (CMD1(0)) */
450
+			}
451
+			while (Timer1 && send_cmd(cmd, 0)) ;		/* Wait for the card leaves idle state */
452
+			if (!Timer1 || send_cmd(CMD16, 512) != 0)	/* Set block length: 512 */
453
+				ty = 0;
454
+		}
455
+	}
456
+	CardType = ty;	/* Card type */
457
+	deselect();
458
+
459
+	if (ty) {		/* OK */
460
+		FCLK_FAST();			/* Set fast clock */
461
+		Stat &= ~STA_NOINIT;	/* Clear STA_NOINIT flag */
462
+	} else {		/* Failed */
463
+		power_off();
464
+		Stat = STA_NOINIT;
465
+	}
466
+
467
+	return Stat;
468
+}
469
+
470
+
471
+
472
+/*-----------------------------------------------------------------------*/
473
+/* Get disk status                                                       */
474
+/*-----------------------------------------------------------------------*/
475
+
476
+DSTATUS disk_status (
477
+	BYTE drv		/* Physical drive number (0) */
478
+)
479
+{
480
+	if (drv) return STA_NOINIT;		/* Supports only drive 0 */
481
+
482
+	return Stat;	/* Return disk status */
483
+}
484
+
485
+
486
+
487
+/*-----------------------------------------------------------------------*/
488
+/* Read sector(s)                                                        */
489
+/*-----------------------------------------------------------------------*/
490
+
491
+DRESULT disk_read (
492
+	BYTE drv,		/* Physical drive number (0) */
493
+	BYTE *buff,		/* Pointer to the data buffer to store read data */
494
+	DWORD sector,	/* Start sector number (LBA) */
495
+	UINT count		/* Number of sectors to read (1..128) */
496
+)
497
+{
498
+	BYTE cmd;
499
+
500
+
501
+	if (drv || !count) return RES_PARERR;		/* Check parameter */
502
+	if (Stat & STA_NOINIT) return RES_NOTRDY;	/* Check if drive is ready */
503
+	if (!(CardType & CT_BLOCK)) sector *= 512;	/* LBA ot BA conversion (byte addressing cards) */
504
+
505
+	cmd = count > 1 ? CMD18 : CMD17;			/*  READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
506
+	if (send_cmd(cmd, sector) == 0) {
507
+		do {
508
+			if (!rcvr_datablock(buff, 512)) break;
509
+			buff += 512;
510
+		} while (--count);
511
+		if (cmd == CMD18) send_cmd(CMD12, 0);	/* STOP_TRANSMISSION */
512
+	}
513
+	deselect();
514
+
515
+	return count ? RES_ERROR : RES_OK;	/* Return result */
516
+}
517
+
518
+
519
+
520
+/*-----------------------------------------------------------------------*/
521
+/* Write sector(s)                                                       */
522
+/*-----------------------------------------------------------------------*/
523
+
524
+#if _DISKIO_WRITE
525
+DRESULT disk_write (
526
+	BYTE drv,			/* Physical drive number (0) */
527
+	const BYTE *buff,	/* Ponter to the data to write */
528
+	DWORD sector,		/* Start sector number (LBA) */
529
+	UINT count			/* Number of sectors to write (1..128) */
530
+)
531
+{
532
+	if (drv || !count) return RES_PARERR;		/* Check parameter */
533
+	if (Stat & STA_NOINIT) return RES_NOTRDY;	/* Check drive status */
534
+	if (Stat & STA_PROTECT) return RES_WRPRT;	/* Check write protect */
535
+
536
+	if (!(CardType & CT_BLOCK)) sector *= 512;	/* LBA ==> BA conversion (byte addressing cards) */
537
+
538
+	if (count == 1) {	/* Single sector write */
539
+		if ((send_cmd(CMD24, sector) == 0)	/* WRITE_BLOCK */
540
+			&& xmit_datablock(buff, 0xFE)) {
541
+			count = 0;
542
+		}
543
+	}
544
+	else {				/* Multiple sector write */
545
+		if (CardType & CT_SDC) send_cmd(ACMD23, count);	/* Predefine number of sectors */
546
+		if (send_cmd(CMD25, sector) == 0) {	/* WRITE_MULTIPLE_BLOCK */
547
+			do {
548
+				if (!xmit_datablock(buff, 0xFC)) break;
549
+				buff += 512;
550
+			} while (--count);
551
+			if (!xmit_datablock(0, 0xFD)) count = 1;	/* STOP_TRAN token */
552
+		}
553
+	}
554
+	deselect();
555
+
556
+	return count ? RES_ERROR : RES_OK;	/* Return result */
557
+}
558
+#endif
559
+
560
+
561
+/*-----------------------------------------------------------------------*/
562
+/* Miscellaneous drive controls other than data read/write               */
563
+/*-----------------------------------------------------------------------*/
564
+
565
+#if _DISKIO_IOCTL
566
+
567
+DRESULT disk_ioctl (
568
+	BYTE drv,		/* Physical drive number (0) */
569
+	BYTE cmd,		/* Control command code */
570
+	void *buff		/* Pointer to the conrtol data */
571
+)
572
+{
573
+	DRESULT res;
574
+	BYTE n, csd[16], *ptr = buff;
575
+	DWORD *dp, st, ed, csize;
576
+#if _DISKIO_ISDIO
577
+	SDIO_CMD *sdio = buff;
578
+	BYTE rc, *buf;
579
+	UINT dc;
580
+#endif
581
+
582
+	if (drv) return RES_PARERR;					/* Check parameter */
583
+	if (Stat & STA_NOINIT) return RES_NOTRDY;	/* Check if drive is ready */
584
+
585
+	res = RES_ERROR;
586
+
587
+	switch (cmd) {
588
+	case CTRL_SYNC:			/* Wait for end of internal write process of the drive */
589
+		if (select()) res = RES_OK;
590
+		break;
591
+
592
+	case GET_SECTOR_COUNT:	/* Get drive capacity in unit of sector (DWORD) */
593
+		if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
594
+			if ((csd[0] >> 6) == 1) {	/* SDC ver 2.00 */
595
+				csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
596
+				*(DWORD*)buff = csize << 10;
597
+			} else {					/* SDC ver 1.XX or MMC ver 3 */
598
+				n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
599
+				csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
600
+				*(DWORD*)buff = csize << (n - 9);
601
+			}
602
+			res = RES_OK;
603
+		}
604
+		break;
605
+
606
+	case GET_BLOCK_SIZE:	/* Get erase block size in unit of sector (DWORD) */
607
+		if (CardType & CT_SD2) {	/* SDC ver 2.00 */
608
+			if (send_cmd(ACMD13, 0) == 0) {	/* Read SD status */
609
+				xchg_spi(0xFF);
610
+				if (rcvr_datablock(csd, 16)) {				/* Read partial block */
611
+					for (n = 64 - 16; n; n--) xchg_spi(0xFF);	/* Purge trailing data */
612
+					*(DWORD*)buff = 16UL << (csd[10] >> 4);
613
+					res = RES_OK;
614
+				}
615
+			}
616
+		} else {					/* SDC ver 1.XX or MMC */
617
+			if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {	/* Read CSD */
618
+				if (CardType & CT_SD1) {	/* SDC ver 1.XX */
619
+					*(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
620
+				} else {					/* MMC */
621
+					*(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
622
+				}
623
+				res = RES_OK;
624
+			}
625
+		}
626
+		break;
627
+
628
+	case CTRL_TRIM:		/* Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1) */
629
+		if (!(CardType & CT_SDC)) break;				/* Check if the card is SDC */
630
+		if (disk_ioctl(drv, MMC_GET_CSD, csd)) break;	/* Get CSD */
631
+		if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break;	/* Check if sector erase can be applied to the card */
632
+		dp = buff; st = dp[0]; ed = dp[1];				/* Load sector block */
633
+		if (!(CardType & CT_BLOCK)) {
634
+			st *= 512; ed *= 512;
635
+		}
636
+		if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) {	/* Erase sector block */
637
+			res = RES_OK;	/* FatFs does not check result of this command */
638
+		}
639
+		break;
640
+
641
+	/* Following commands are never used by FatFs module */
642
+
643
+	case MMC_GET_TYPE:		/* Get MMC/SDC type (BYTE) */
644
+		*ptr = CardType;
645
+		res = RES_OK;
646
+		break;
647
+
648
+	case MMC_GET_CSD:		/* Read CSD (16 bytes) */
649
+		if (send_cmd(CMD9, 0) == 0 && rcvr_datablock(ptr, 16)) {	/* READ_CSD */
650
+			res = RES_OK;
651
+		}
652
+		break;
653
+
654
+	case MMC_GET_CID:		/* Read CID (16 bytes) */
655
+		if (send_cmd(CMD10, 0) == 0 && rcvr_datablock(ptr, 16)) {	/* READ_CID */
656
+			res = RES_OK;
657
+		}
658
+		break;
659
+
660
+	case MMC_GET_OCR:		/* Read OCR (4 bytes) */
661
+		if (send_cmd(CMD58, 0) == 0) {	/* READ_OCR */
662
+			for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
663
+			res = RES_OK;
664
+		}
665
+		break;
666
+
667
+	case MMC_GET_SDSTAT:	/* Read SD status (64 bytes) */
668
+		if (send_cmd(ACMD13, 0) == 0) {	/* SD_STATUS */
669
+			xchg_spi(0xFF);
670
+			if (rcvr_datablock(ptr, 64)) res = RES_OK;
671
+		}
672
+		break;
673
+#if _DISKIO_ISDIO
674
+	case ISDIO_READ:
675
+		sdio = buff;
676
+		if (send_cmd(CMD48, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) {
677
+			for (Timer1 = 1000; (rc = xchg_spi(0xFF)) == 0xFF && Timer1; ) ;
678
+			if (rc == 0xFE) {
679
+				for (buf = sdio->data, dc = sdio->ndata; dc; dc--) *buf++ = xchg_spi(0xFF);
680
+				for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF);
681
+				res = RES_OK;
682
+			}
683
+		}
684
+		break;
685
+	case ISDIO_WRITE:
686
+		sdio = buff;
687
+		if (send_cmd(CMD49, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) {
688
+			xchg_spi(0xFF); xchg_spi(0xFE);
689
+			for (buf = sdio->data, dc = sdio->ndata; dc; dc--) xchg_spi(*buf++);
690
+			for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF);
691
+			if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK;
692
+		}
693
+		break;
694
+	case ISDIO_MRITE:
695
+		sdio = buff;
696
+		if (send_cmd(CMD49, 0x84000000 | sdio->func << 28 | sdio->addr << 9 | sdio->ndata >> 8) == 0) {
697
+			xchg_spi(0xFF); xchg_spi(0xFE);
698
+			xchg_spi(sdio->ndata);
699
+			for (dc = 513; dc; dc--) xchg_spi(0xFF);
700
+			if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK;
701
+		}
702
+		break;
703
+#endif
704
+	default:
705
+		res = RES_PARERR;
706
+	}
707
+
708
+	deselect();
709
+	return res;
710
+}
711
+#endif
712
+
713
+
714
+/*-----------------------------------------------------------------------*/
715
+/* Device timer function                                                 */
716
+/*-----------------------------------------------------------------------*/
717
+/* This function must be called from timer interrupt routine in period
718
+/  of 1 ms to generate card control timing.
719
+*/
720
+
721
+void disk_timerproc (void)
722
+{
723
+	WORD n;
724
+	BYTE s;
725
+
726
+
727
+	n = Timer1;						/* 1kHz decrement timer stopped at 0 */
728
+	if (n) Timer1 = --n;
729
+	n = Timer2;
730
+	if (n) Timer2 = --n;
731
+
732
+	s = Stat;
733
+	if (MMC_WP) {	/* Write protected */
734
+		s |= STA_PROTECT;
735
+	} else {		/* Write enabled */
736
+		s &= ~STA_PROTECT;
737
+	}
738
+	//if (MMC_CD) {	/* Card is in socket */
739
+		s &= ~STA_NODISK;
740
+	//} else {		/* Socket empty */
741
+	//	s |= (STA_NODISK | STA_NOINIT);
742
+	//}
743
+	Stat = s;
744
+}
745
+

+ 91
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/rtc176x.c Näytä tiedosto

@@ -0,0 +1,91 @@
1
+/*------------------------------------------------------------------------/
2
+/  LPC176x RTC control module
3
+/-------------------------------------------------------------------------/
4
+/
5
+/  Copyright (C) 2011, ChaN, all right reserved.
6
+/
7
+/ * This software is a free software and there is NO WARRANTY.
8
+/ * No restriction on use. You can use, modify and redistribute it for
9
+/   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
10
+/ * Redistributions of source code must retain the above copyright notice.
11
+/
12
+/-------------------------------------------------------------------------*/
13
+
14
+#include "rtc176x.h"
15
+
16
+
17
+int rtc_initialize (void)
18
+{
19
+	/* Enable PCLK to the RTC */
20
+	__set_PCONP(PCRTC, 1);
21
+
22
+	/* Start RTC with external XTAL */
23
+	RTC_CCR = 0x11;
24
+
25
+	return 1;
26
+}
27
+
28
+
29
+
30
+int rtc_gettime (RTC *rtc)	/* 1:RTC valid, 0:RTC volatiled */
31
+{
32
+	DWORD d, t;
33
+
34
+
35
+	do {
36
+		t = RTC_CTIME0;
37
+		d = RTC_CTIME1;
38
+	} while (t != RTC_CTIME0 || d != RTC_CTIME1);
39
+
40
+	if (RTC_AUX & _BV(4)) {	/* If power fail has been detected, return default time. */
41
+		rtc->sec = 0; rtc->min = 0; rtc->hour = 0;
42
+		rtc->wday = 0; rtc->mday = 1; rtc->month = 1; rtc->year = 2014;
43
+		return 0;
44
+	}
45
+
46
+	rtc->sec = t & 63;
47
+	rtc->min = (t >> 8) & 63;
48
+	rtc->hour = (t >> 16) & 31;
49
+	rtc->wday = (t >> 24) & 7;
50
+	rtc->mday = d & 31;
51
+	rtc->month = (d >> 8) & 15;
52
+	rtc->year = (d >> 16) & 4095;
53
+	return 1;
54
+}
55
+
56
+
57
+
58
+
59
+int rtc_settime (const RTC *rtc)
60
+{
61
+	RTC_CCR = 0x12;		/* Stop RTC */
62
+
63
+	/* Update RTC registers */
64
+	RTC_SEC = rtc->sec;
65
+	RTC_MIN = rtc->min;
66
+	RTC_HOUR = rtc->hour;
67
+	RTC_DOW = rtc->wday;
68
+	RTC_DOM = rtc->mday;
69
+	RTC_MONTH = rtc->month;
70
+	RTC_YEAR = rtc->year;
71
+
72
+	RTC_AUX = _BV(4);	/* Clear power fail flag */
73
+	RTC_CCR = 0x11;		/* Restart RTC, Disable calibration feature */
74
+
75
+	return 1;
76
+}
77
+
78
+DWORD get_fattime (void) {
79
+  RTC rtc;
80
+
81
+  /* Get local time */
82
+  rtc_gettime(&rtc);
83
+
84
+  /* Pack date and time into a DWORD variable */
85
+  return    ((DWORD)(rtc.year - 1980) << 25)
86
+      | ((DWORD)rtc.month << 21)
87
+      | ((DWORD)rtc.mday << 16)
88
+      | ((DWORD)rtc.hour << 11)
89
+      | ((DWORD)rtc.min << 5)
90
+      | ((DWORD)rtc.sec >> 1);
91
+}

+ 22
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/chanfs/rtc176x.h Näytä tiedosto

@@ -0,0 +1,22 @@
1
+#ifndef _RTC_DEFINED
2
+#define _RTC_DEFINE
3
+
4
+#include "integer.h"
5
+#include "LPC176x.h"
6
+
7
+typedef struct {
8
+	WORD	year;	/* 1..4095 */
9
+	BYTE	month;	/* 1..12 */
10
+	BYTE	mday;	/* 1.. 31 */
11
+	BYTE	wday;	/* 1..7 */
12
+	BYTE	hour;	/* 0..23 */
13
+	BYTE	min;	/* 0..59 */
14
+	BYTE	sec;	/* 0..59 */
15
+} RTC;
16
+
17
+int rtc_initialize (void);		/* Initialize RTC */
18
+int rtc_gettime (RTC*);			/* Get time */
19
+int rtc_settime (const RTC*);	/* Set time */
20
+DWORD get_fattime (void);
21
+
22
+#endif

+ 36
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/Re-ARM_usb_driver.inf Näytä tiedosto

@@ -0,0 +1,36 @@
1
+[Version]
2
+Signature="$Windows NT$"
3
+Class=Ports
4
+ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
5
+Provider=%PROVIDER%
6
+DriverVer =04/14/2008, 5.1.2600.5512
7
+
8
+[Manufacturer]
9
+%PROVIDER%=DeviceList,ntamd64
10
+
11
+
12
+[DeviceList]
13
+%DESCRIPTION%=LPC1768USB, USB\VID_1D50&PID_6029&MI_00
14
+
15
+[DeviceList.ntamd64]
16
+%DESCRIPTION%=LPC1768USB, USB\VID_1D50&PID_6029&MI_00
17
+
18
+
19
+[LPC1768USB]
20
+include=mdmcpq.inf
21
+CopyFiles=FakeModemCopyFileSection
22
+AddReg=LowerFilterAddReg,SerialPropPageAddReg
23
+
24
+[LPC1768USB.Services]
25
+include=mdmcpq.inf
26
+AddService=usbser, 0x00000002, LowerFilter_Service_Inst
27
+
28
+[SerialPropPageAddReg]
29
+HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
30
+
31
+
32
+[Strings]
33
+PROVIDER   = "marlinfw.org"
34
+DRIVER.SVC = "Marlin USB Driver"
35
+DESCRIPTION= "Marlin USB Serial"
36
+COMPOSITE  = "Marlin USB VCOM"

+ 254
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/cdc.h Näytä tiedosto

@@ -0,0 +1,254 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ *      Name:    CDC.h
5
+ *      Purpose: USB Communication Device Class Definitions
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __CDC_H
20
+#define __CDC_H
21
+
22
+extern "C" {
23
+#include <lpc_types.h>
24
+}
25
+
26
+#if defined   (  __GNUC__  )
27
+#define __packed __attribute__((__packed__))
28
+#endif
29
+/*----------------------------------------------------------------------------
30
+ *      Definitions  based on usbcdc11.pdf (www.usb.org)
31
+ *---------------------------------------------------------------------------*/
32
+// Communication device class specification version 1.10
33
+#define CDC_V1_10                               0x0110
34
+
35
+// Communication interface class code
36
+// (usbcdc11.pdf, 4.2, Table 15)
37
+#define CDC_COMMUNICATION_INTERFACE_CLASS       0x02
38
+
39
+// Communication interface class subclass codes
40
+// (usbcdc11.pdf, 4.3, Table 16)
41
+#define CDC_DIRECT_LINE_CONTROL_MODEL           0x01
42
+#define CDC_ABSTRACT_CONTROL_MODEL              0x02
43
+#define CDC_TELEPHONE_CONTROL_MODEL             0x03
44
+#define CDC_MULTI_CHANNEL_CONTROL_MODEL         0x04
45
+#define CDC_CAPI_CONTROL_MODEL                  0x05
46
+#define CDC_ETHERNET_NETWORKING_CONTROL_MODEL   0x06
47
+#define CDC_ATM_NETWORKING_CONTROL_MODEL        0x07
48
+
49
+// Communication interface class control protocol codes
50
+// (usbcdc11.pdf, 4.4, Table 17)
51
+#define CDC_PROTOCOL_COMMON_AT_COMMANDS         0x01
52
+
53
+// Data interface class code
54
+// (usbcdc11.pdf, 4.5, Table 18)
55
+#define CDC_DATA_INTERFACE_CLASS                0x0A
56
+
57
+// Data interface class protocol codes
58
+// (usbcdc11.pdf, 4.7, Table 19)
59
+#define CDC_PROTOCOL_ISDN_BRI                   0x30
60
+#define CDC_PROTOCOL_HDLC                       0x31
61
+#define CDC_PROTOCOL_TRANSPARENT                0x32
62
+#define CDC_PROTOCOL_Q921_MANAGEMENT            0x50
63
+#define CDC_PROTOCOL_Q921_DATA_LINK             0x51
64
+#define CDC_PROTOCOL_Q921_MULTIPLEXOR           0x52
65
+#define CDC_PROTOCOL_V42                        0x90
66
+#define CDC_PROTOCOL_EURO_ISDN                  0x91
67
+#define CDC_PROTOCOL_V24_RATE_ADAPTATION        0x92
68
+#define CDC_PROTOCOL_CAPI                       0x93
69
+#define CDC_PROTOCOL_HOST_BASED_DRIVER          0xFD
70
+#define CDC_PROTOCOL_DESCRIBED_IN_PUFD          0xFE
71
+
72
+// Type values for bDescriptorType field of functional descriptors
73
+// (usbcdc11.pdf, 5.2.3, Table 24)
74
+#define CDC_CS_INTERFACE                        0x24
75
+#define CDC_CS_ENDPOINT                         0x25
76
+
77
+// Type values for bDescriptorSubtype field of functional descriptors
78
+// (usbcdc11.pdf, 5.2.3, Table 25)
79
+#define CDC_HEADER                              0x00
80
+#define CDC_CALL_MANAGEMENT                     0x01
81
+#define CDC_ABSTRACT_CONTROL_MANAGEMENT         0x02
82
+#define CDC_DIRECT_LINE_MANAGEMENT              0x03
83
+#define CDC_TELEPHONE_RINGER                    0x04
84
+#define CDC_REPORTING_CAPABILITIES              0x05
85
+#define CDC_UNION                               0x06
86
+#define CDC_COUNTRY_SELECTION                   0x07
87
+#define CDC_TELEPHONE_OPERATIONAL_MODES         0x08
88
+#define CDC_USB_TERMINAL                        0x09
89
+#define CDC_NETWORK_CHANNEL                     0x0A
90
+#define CDC_PROTOCOL_UNIT                       0x0B
91
+#define CDC_EXTENSION_UNIT                      0x0C
92
+#define CDC_MULTI_CHANNEL_MANAGEMENT            0x0D
93
+#define CDC_CAPI_CONTROL_MANAGEMENT             0x0E
94
+#define CDC_ETHERNET_NETWORKING                 0x0F
95
+#define CDC_ATM_NETWORKING                      0x10
96
+
97
+// CDC class-specific request codes
98
+// (usbcdc11.pdf, 6.2, Table 46)
99
+// see Table 45 for info about the specific requests.
100
+#define CDC_SEND_ENCAPSULATED_COMMAND           0x00
101
+#define CDC_GET_ENCAPSULATED_RESPONSE           0x01
102
+#define CDC_SET_COMM_FEATURE                    0x02
103
+#define CDC_GET_COMM_FEATURE                    0x03
104
+#define CDC_CLEAR_COMM_FEATURE                  0x04
105
+#define CDC_SET_AUX_LINE_STATE                  0x10
106
+#define CDC_SET_HOOK_STATE                      0x11
107
+#define CDC_PULSE_SETUP                         0x12
108
+#define CDC_SEND_PULSE                          0x13
109
+#define CDC_SET_PULSE_TIME                      0x14
110
+#define CDC_RING_AUX_JACK                       0x15
111
+#define CDC_SET_LINE_CODING                     0x20
112
+#define CDC_GET_LINE_CODING                     0x21
113
+#define CDC_SET_CONTROL_LINE_STATE              0x22
114
+#define CDC_SEND_BREAK                          0x23
115
+#define CDC_SET_RINGER_PARMS                    0x30
116
+#define CDC_GET_RINGER_PARMS                    0x31
117
+#define CDC_SET_OPERATION_PARMS                 0x32
118
+#define CDC_GET_OPERATION_PARMS                 0x33
119
+#define CDC_SET_LINE_PARMS                      0x34
120
+#define CDC_GET_LINE_PARMS                      0x35
121
+#define CDC_DIAL_DIGITS                         0x36
122
+#define CDC_SET_UNIT_PARAMETER                  0x37
123
+#define CDC_GET_UNIT_PARAMETER                  0x38
124
+#define CDC_CLEAR_UNIT_PARAMETER                0x39
125
+#define CDC_GET_PROFILE                         0x3A
126
+#define CDC_SET_ETHERNET_MULTICAST_FILTERS      0x40
127
+#define CDC_SET_ETHERNET_PMP_FILTER             0x41
128
+#define CDC_GET_ETHERNET_PMP_FILTER             0x42
129
+#define CDC_SET_ETHERNET_PACKET_FILTER          0x43
130
+#define CDC_GET_ETHERNET_STATISTIC              0x44
131
+#define CDC_SET_ATM_DATA_FORMAT                 0x50
132
+#define CDC_GET_ATM_DEVICE_STATISTICS           0x51
133
+#define CDC_SET_ATM_DEFAULT_VC                  0x52
134
+#define CDC_GET_ATM_VC_STATISTICS               0x53
135
+
136
+// Communication feature selector codes
137
+// (usbcdc11.pdf, 6.2.2..6.2.4, Table 47)
138
+#define CDC_ABSTRACT_STATE                      0x01
139
+#define CDC_COUNTRY_SETTING                     0x02
140
+
141
+// Feature Status returned for ABSTRACT_STATE Selector
142
+// (usbcdc11.pdf, 6.2.3, Table 48)
143
+#define CDC_IDLE_SETTING                        (1 << 0)
144
+#define CDC_DATA_MULTPLEXED_STATE               (1 << 1)
145
+
146
+
147
+// Control signal bitmap values for the SetControlLineState request
148
+// (usbcdc11.pdf, 6.2.14, Table 51)
149
+#define CDC_DTE_PRESENT                         (1 << 0)
150
+#define CDC_ACTIVATE_CARRIER                    (1 << 1)
151
+
152
+// CDC class-specific notification codes
153
+// (usbcdc11.pdf, 6.3, Table 68)
154
+// see Table 67 for Info about class-specific notifications
155
+#define CDC_NOTIFICATION_NETWORK_CONNECTION     0x00
156
+#define CDC_RESPONSE_AVAILABLE                  0x01
157
+#define CDC_AUX_JACK_HOOK_STATE                 0x08
158
+#define CDC_RING_DETECT                         0x09
159
+#define CDC_NOTIFICATION_SERIAL_STATE           0x20
160
+#define CDC_CALL_STATE_CHANGE                   0x28
161
+#define CDC_LINE_STATE_CHANGE                   0x29
162
+#define CDC_CONNECTION_SPEED_CHANGE             0x2A
163
+
164
+// UART state bitmap values (Serial state notification).
165
+// (usbcdc11.pdf, 6.3.5, Table 69)
166
+#define CDC_SERIAL_STATE_OVERRUN                (1 << 6)  // receive data overrun error has occurred
167
+#define CDC_SERIAL_STATE_PARITY                 (1 << 5)  // parity error has occurred
168
+#define CDC_SERIAL_STATE_FRAMING                (1 << 4)  // framing error has occurred
169
+#define CDC_SERIAL_STATE_RING                   (1 << 3)  // state of ring signal detection
170
+#define CDC_SERIAL_STATE_BREAK                  (1 << 2)  // state of break detection
171
+#define CDC_SERIAL_STATE_TX_CARRIER             (1 << 1)  // state of transmission carrier
172
+#define CDC_SERIAL_STATE_RX_CARRIER             (1 << 0)  // state of receiver carrier
173
+
174
+// capabilities callmgmt
175
+#define CDC_CALLMGMT_CAP_CALLMGMT 0x01
176
+#define CDC_CALLMGMT_CAP_DATAINTF 0x02
177
+
178
+// capabilities acm
179
+#define CDC_ACM_CAP_COMM  0x01
180
+#define CDC_ACM_CAP_LINE  0x02
181
+#define CDC_ACM_CAP_BRK   0x04
182
+#define CDC_ACM_CAP_NOTIFY  0x08
183
+
184
+
185
+
186
+/*----------------------------------------------------------------------------
187
+ *      Structures  based on usbcdc11.pdf (www.usb.org)
188
+ *---------------------------------------------------------------------------*/
189
+
190
+// Header functional descriptor
191
+// (usbcdc11.pdf, 5.2.3.1)
192
+// This header must precede any list of class-specific descriptors.
193
+typedef struct __packed  _CDC_HEADER_DESCRIPTOR{
194
+  uint8_t bFunctionLength;                     // size of this descriptor in bytes
195
+  uint8_t bDescriptorType;                     // CS_INTERFACE descriptor type
196
+  uint8_t bDescriptorSubtype;                  // Header functional descriptor subtype
197
+  uint16_t bcdCDC;                              // USB CDC specification release version
198
+} CDC_HEADER_DESCRIPTOR;
199
+
200
+//Call management functional descriptor
201
+// (usbcdc11.pdf, 5.2.3.2)
202
+// Describes the processing of calls for the communication class interface.
203
+typedef struct __packed  _CDC_CALL_MANAGEMENT_DESCRIPTOR{
204
+  uint8_t bFunctionLength;                     // size of this descriptor in bytes
205
+  uint8_t bDescriptorType;                     // CS_INTERFACE descriptor type
206
+  uint8_t bDescriptorSubtype;                  // call management functional descriptor subtype
207
+  uint8_t bmCapabilities;                      // capabilities that this configuration supports
208
+  uint8_t bDataInterface;                      // interface number of the data class interface used for call management (optional)
209
+} CDC_CALL_MANAGEMENT_DESCRIPTOR;
210
+
211
+// Abstract control management functional descriptor
212
+// (usbcdc11.pdf, 5.2.3.3)
213
+// Describes the command supported by the communication interface class with the Abstract Control Model subclass code.
214
+typedef struct __packed  _CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR{
215
+  uint8_t bFunctionLength;                     // size of this descriptor in bytes
216
+  uint8_t bDescriptorType;                     // CS_INTERFACE descriptor type
217
+  uint8_t bDescriptorSubtype;                  // abstract control management functional descriptor subtype
218
+  uint8_t bmCapabilities;                      // capabilities supported by this configuration
219
+} CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR;
220
+
221
+// Union functional descriptors
222
+// (usbcdc11.pdf, 5.2.3.8)
223
+// Describes the relationship between a group of interfaces that can be considered to form a functional unit.
224
+typedef struct __packed  _CDC_UNION_DESCRIPTOR{
225
+  uint8_t bFunctionLength;                     // size of this descriptor in bytes
226
+  uint8_t bDescriptorType;                     // CS_INTERFACE descriptor type
227
+  uint8_t bDescriptorSubtype;                  // union functional descriptor subtype
228
+  uint8_t bMasterInterface;                    // interface number designated as master
229
+} CDC_UNION_DESCRIPTOR;
230
+
231
+// Union functional descriptors with one slave interface
232
+// (usbcdc11.pdf, 5.2.3.8)
233
+typedef struct __packed  _CDC_UNION_1SLAVE_DESCRIPTOR{
234
+  CDC_UNION_DESCRIPTOR sUnion;              // Union functional descriptor
235
+  uint8_t                 bSlaveInterfaces[1]; // Slave interface 0
236
+} CDC_UNION_1SLAVE_DESCRIPTOR;
237
+
238
+//  Line coding structure
239
+//  Format of the data returned when a GetLineCoding request is received
240
+// (usbcdc11.pdf, 6.2.13)
241
+typedef struct __packed  _CDC_LINE_CODING{
242
+  uint32_t dwDTERate;                          // Data terminal rate in bits per second
243
+  uint8_t  bCharFormat;                        // Number of stop bits
244
+  uint8_t  bParityType;                        // Parity bit type
245
+  uint8_t  bDataBits;                          // Number of data bits
246
+} CDC_LINE_CODING;
247
+
248
+// Notification header
249
+// Data sent on the notification endpoint must follow this header.
250
+// see  USB_SETUP_PACKET in file usb.h
251
+typedef USB_SETUP_PACKET CDC_NOTIFICATION_HEADER;
252
+
253
+#endif /* __CDC_H */
254
+

+ 258
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/cdcuser.cpp Näytä tiedosto

@@ -0,0 +1,258 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ *      Name:    cdcuser.c
5
+ *      Purpose: USB Communication Device Class User module
6
+ *      Version: V1.10
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC microcontroller devices only. Nothing else
14
+ *      gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+extern "C" {
20
+#include <lpc_types.h>
21
+#include <debug_frmwrk.h>
22
+}
23
+
24
+#include "usb.h"
25
+#include "usbhw.h"
26
+#include "usbcfg.h"
27
+#include "usbcore.h"
28
+#include "cdc.h"
29
+#include "cdcuser.h"
30
+
31
+#include <HAL_LPC1768/serial.h>
32
+
33
+unsigned char BulkBufIn[USB_CDC_BUFSIZE];            // Buffer to store USB IN  packet
34
+unsigned char BulkBufOut[USB_CDC_BUFSIZE];            // Buffer to store USB OUT packet
35
+unsigned char NotificationBuf[10];
36
+
37
+CDC_LINE_CODING CDC_LineCoding = { 921600, 0, 0, 8 };
38
+unsigned short CDC_DepInEmpty = 1;                   // Data IN EP is empty
39
+unsigned short CDC_LineState = 0;
40
+unsigned short CDC_SerialState = 0;
41
+
42
+
43
+extern HalSerial usb_serial;
44
+/*----------------------------------------------------------------------------
45
+ write data to CDC_OutBuf
46
+ *---------------------------------------------------------------------------*/
47
+uint32_t CDC_WrOutBuf(const char *buffer, uint32_t *length) {
48
+  uint32_t bytesToWrite, bytesWritten;
49
+
50
+  // Write *length bytes
51
+  bytesToWrite = *length;
52
+  bytesWritten = bytesToWrite;
53
+
54
+  while (bytesToWrite) {
55
+    usb_serial.receive_buffer.write(*buffer++);           // Copy Data to buffer
56
+    bytesToWrite--;
57
+  }
58
+
59
+  return (bytesWritten);
60
+}
61
+
62
+/*----------------------------------------------------------------------------
63
+ check if character(s) are available at CDC_OutBuf
64
+ *---------------------------------------------------------------------------*/
65
+uint32_t CDC_OutBufAvailChar(uint32_t *availChar) {
66
+  *availChar = usb_serial.transmit_buffer.available();
67
+  return (0);
68
+}
69
+/* end Buffer handling */
70
+
71
+/*----------------------------------------------------------------------------
72
+ CDC Initialisation
73
+ Initializes the data structures and serial port
74
+ Parameters:   None
75
+ Return Value: None
76
+ *---------------------------------------------------------------------------*/
77
+void CDC_Init() {
78
+  CDC_DepInEmpty = 1;
79
+}
80
+
81
+/*----------------------------------------------------------------------------
82
+ CDC SendEncapsulatedCommand Request Callback
83
+ Called automatically on CDC SEND_ENCAPSULATED_COMMAND Request
84
+ Parameters:   None                          (global SetupPacket and EP0Buf)
85
+ Return Value: TRUE - Success, FALSE - Error
86
+ *---------------------------------------------------------------------------*/
87
+uint32_t CDC_SendEncapsulatedCommand(void) {
88
+
89
+  return (TRUE);
90
+}
91
+
92
+/*----------------------------------------------------------------------------
93
+ CDC GetEncapsulatedResponse Request Callback
94
+ Called automatically on CDC Get_ENCAPSULATED_RESPONSE Request
95
+ Parameters:   None                          (global SetupPacket and EP0Buf)
96
+ Return Value: TRUE - Success, FALSE - Error
97
+ *---------------------------------------------------------------------------*/
98
+uint32_t CDC_GetEncapsulatedResponse(void) {
99
+
100
+  /* ... add code to handle request */
101
+  return (TRUE);
102
+}
103
+
104
+/*----------------------------------------------------------------------------
105
+ CDC SetCommFeature Request Callback
106
+ Called automatically on CDC Set_COMM_FATURE Request
107
+ Parameters:   FeatureSelector
108
+ Return Value: TRUE - Success, FALSE - Error
109
+ *---------------------------------------------------------------------------*/
110
+uint32_t CDC_SetCommFeature(unsigned short wFeatureSelector) {
111
+
112
+  /* ... add code to handle request */
113
+  return (TRUE);
114
+}
115
+
116
+/*----------------------------------------------------------------------------
117
+ CDC GetCommFeature Request Callback
118
+ Called automatically on CDC Get_COMM_FATURE Request
119
+ Parameters:   FeatureSelector
120
+ Return Value: TRUE - Success, FALSE - Error
121
+ *---------------------------------------------------------------------------*/
122
+uint32_t CDC_GetCommFeature(unsigned short wFeatureSelector) {
123
+
124
+  /* ... add code to handle request */
125
+  return (TRUE);
126
+}
127
+
128
+/*----------------------------------------------------------------------------
129
+ CDC ClearCommFeature Request Callback
130
+ Called automatically on CDC CLEAR_COMM_FATURE Request
131
+ Parameters:   FeatureSelector
132
+ Return Value: TRUE - Success, FALSE - Error
133
+ *---------------------------------------------------------------------------*/
134
+uint32_t CDC_ClearCommFeature(unsigned short wFeatureSelector) {
135
+
136
+  /* ... add code to handle request */
137
+  return (TRUE);
138
+}
139
+
140
+/*----------------------------------------------------------------------------
141
+ CDC SetLineCoding Request Callback
142
+ Called automatically on CDC SET_LINE_CODING Request
143
+ Parameters:   none                    (global SetupPacket and EP0Buf)
144
+ Return Value: TRUE - Success, FALSE - Error
145
+ *---------------------------------------------------------------------------*/
146
+uint32_t CDC_SetLineCoding(void) {
147
+
148
+  CDC_LineCoding.dwDTERate = (EP0Buf[0] << 0) | (EP0Buf[1] << 8) | (EP0Buf[2] << 16) | (EP0Buf[3] << 24);
149
+  CDC_LineCoding.bCharFormat = EP0Buf[4];
150
+  CDC_LineCoding.bParityType = EP0Buf[5];
151
+  CDC_LineCoding.bDataBits = EP0Buf[6];
152
+
153
+  return (TRUE);
154
+}
155
+
156
+/*----------------------------------------------------------------------------
157
+ CDC GetLineCoding Request Callback
158
+ Called automatically on CDC GET_LINE_CODING Request
159
+ Parameters:   None                         (global SetupPacket and EP0Buf)
160
+ Return Value: TRUE - Success, FALSE - Error
161
+ *---------------------------------------------------------------------------*/
162
+uint32_t CDC_GetLineCoding(void) {
163
+
164
+  EP0Buf[0] = (CDC_LineCoding.dwDTERate >> 0) & 0xFF;
165
+  EP0Buf[1] = (CDC_LineCoding.dwDTERate >> 8) & 0xFF;
166
+  EP0Buf[2] = (CDC_LineCoding.dwDTERate >> 16) & 0xFF;
167
+  EP0Buf[3] = (CDC_LineCoding.dwDTERate >> 24) & 0xFF;
168
+  EP0Buf[4] = CDC_LineCoding.bCharFormat;
169
+  EP0Buf[5] = CDC_LineCoding.bParityType;
170
+  EP0Buf[6] = CDC_LineCoding.bDataBits;
171
+
172
+  return (TRUE);
173
+}
174
+
175
+/*----------------------------------------------------------------------------
176
+ CDC SetControlLineState Request Callback
177
+ Called automatically on CDC SET_CONTROL_LINE_STATE Request
178
+ Parameters:   ControlSignalBitmap
179
+ Return Value: TRUE - Success, FALSE - Error
180
+ *---------------------------------------------------------------------------*/
181
+uint32_t CDC_SetControlLineState(unsigned short wControlSignalBitmap) {
182
+  CDC_LineState = wControlSignalBitmap;
183
+  usb_serial.host_connected = wControlSignalBitmap > 0 ? true : false;
184
+  return true;
185
+}
186
+
187
+/*----------------------------------------------------------------------------
188
+ CDC SendBreak Request Callback
189
+ Called automatically on CDC Set_COMM_FATURE Request
190
+ Parameters:   0xFFFF  start of Break
191
+ 0x0000  stop  of Break
192
+ 0x####  Duration of Break
193
+ Return Value: TRUE - Success, FALSE - Error
194
+ *---------------------------------------------------------------------------*/
195
+uint32_t CDC_SendBreak(unsigned short wDurationOfBreak) {
196
+
197
+  /* ... add code to handle request */
198
+  return (TRUE);
199
+}
200
+
201
+/*----------------------------------------------------------------------------
202
+ CDC_BulkIn call on DataIn Request
203
+ Parameters:   none
204
+ Return Value: none
205
+ *---------------------------------------------------------------------------*/
206
+void CDC_BulkIn(void) {
207
+  uint32_t numBytesAvail = usb_serial.transmit_buffer.available();
208
+
209
+  if (numBytesAvail > 0) {
210
+    numBytesAvail = numBytesAvail > (USB_CDC_BUFSIZE - 1) ? (USB_CDC_BUFSIZE - 1) : numBytesAvail;
211
+    for(uint32_t i = 0; i < numBytesAvail; ++i) {
212
+      BulkBufIn[i] = usb_serial.transmit_buffer.read(); //todo: optimise
213
+    }
214
+    USB_WriteEP(CDC_DEP_IN, &BulkBufIn[0], numBytesAvail);
215
+  } else {
216
+    CDC_DepInEmpty = 1;
217
+  }
218
+}
219
+
220
+/*----------------------------------------------------------------------------
221
+ CDC_BulkOut call on DataOut Request
222
+ Parameters:   none
223
+ Return Value: none
224
+ *---------------------------------------------------------------------------*/
225
+void CDC_BulkOut(void) {
226
+  uint32_t numBytesRead = USB_ReadEP(CDC_DEP_OUT, &BulkBufOut[0]);
227
+  CDC_WrOutBuf((char *) &BulkBufOut[0], &numBytesRead);
228
+}
229
+
230
+/*----------------------------------------------------------------------------
231
+ Get the SERIAL_STATE as defined in usbcdc11.pdf, 6.3.5, Table 69.
232
+ Parameters:   none
233
+ Return Value: SerialState as defined in usbcdc11.pdf
234
+ *---------------------------------------------------------------------------*/
235
+unsigned short CDC_GetSerialState(void) {
236
+  CDC_SerialState = CDC_LineState;
237
+  //todo: detect buffer overrun
238
+  return (CDC_SerialState);
239
+}
240
+
241
+/*----------------------------------------------------------------------------
242
+ Send the SERIAL_STATE notification as defined in usbcdc11.pdf, 6.3.5.
243
+ *---------------------------------------------------------------------------*/
244
+void CDC_NotificationIn(void) {
245
+
246
+  NotificationBuf[0] = 0xA1;                           // bmRequestType
247
+  NotificationBuf[1] = CDC_NOTIFICATION_SERIAL_STATE;     // bNotification (SERIAL_STATE)
248
+  NotificationBuf[2] = 0x00;                           // wValue
249
+  NotificationBuf[3] = 0x00;
250
+  NotificationBuf[4] = 0x00;                           // wIndex (Interface #, LSB first)
251
+  NotificationBuf[5] = 0x00;
252
+  NotificationBuf[6] = 0x02;                           // wLength (Data length = 2 bytes, LSB first)
253
+  NotificationBuf[7] = 0x00;
254
+  NotificationBuf[8] = (CDC_SerialState >> 0) & 0xFF;     // UART State Bitmap (16bits, LSB first)
255
+  NotificationBuf[9] = (CDC_SerialState >> 8) & 0xFF;
256
+
257
+  USB_WriteEP(CDC_CEP_IN, &NotificationBuf[0], 10);     // send notification
258
+}

+ 62
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/cdcuser.h Näytä tiedosto

@@ -0,0 +1,62 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ *      Name:    cdcuser.h
5
+ *      Purpose: USB Communication Device Class User module Definitions
6
+ *      Version: V1.10
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC microcontroller devices only. Nothing else 
14
+ *      gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __CDCUSER_H__
20
+#define __CDCUSER_H__
21
+
22
+/* CDC buffer handling */
23
+extern uint32_t CDC_RdOutBuf(char *buffer, const uint32_t *length);
24
+extern uint32_t CDC_WrOutBuf(const char *buffer, uint32_t *length);
25
+extern uint32_t CDC_OutBufAvailChar(uint32_t *availChar);
26
+
27
+/* CDC Data In/Out Endpoint Address */
28
+#define CDC_DEP_IN       0x82
29
+#define CDC_DEP_OUT      0x02
30
+
31
+/* CDC Communication In Endpoint Address */
32
+#define CDC_CEP_IN       0x81
33
+
34
+/* CDC Requests Callback Functions */
35
+extern uint32_t CDC_SendEncapsulatedCommand(void);
36
+extern uint32_t CDC_GetEncapsulatedResponse(void);
37
+extern uint32_t CDC_SetCommFeature(unsigned short wFeatureSelector);
38
+extern uint32_t CDC_GetCommFeature(unsigned short wFeatureSelector);
39
+extern uint32_t CDC_ClearCommFeature(unsigned short wFeatureSelector);
40
+extern uint32_t CDC_GetLineCoding(void);
41
+extern uint32_t CDC_SetLineCoding(void);
42
+extern uint32_t CDC_SetControlLineState(unsigned short wControlSignalBitmap);
43
+extern uint32_t CDC_SendBreak(unsigned short wDurationOfBreak);
44
+
45
+/* CDC Bulk Callback Functions */
46
+extern void CDC_BulkIn(void);
47
+extern void CDC_BulkOut(void);
48
+
49
+/* CDC Notification Callback Function */
50
+extern void CDC_NotificationIn(void);
51
+
52
+/* CDC Initializtion Function */
53
+extern void CDC_Init();
54
+
55
+/* CDC prepare the SERAIAL_STATE */
56
+extern unsigned short CDC_GetSerialState(void);
57
+
58
+/* flow control */
59
+extern unsigned short CDC_DepInEmpty;         // DataEndPoint IN empty
60
+
61
+#endif  /* __CDCUSER_H__ */
62
+

+ 116
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/msc.h Näytä tiedosto

@@ -0,0 +1,116 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ *      Name:    MSC.H
5
+ *      Purpose: USB Mass Storage Class Definitions
6
+ *      Version: V1.10
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ *      Copyright (c) 2005-2009 Keil Software.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __MSC_H__
20
+#define __MSC_H__
21
+
22
+
23
+#if defined   (  __GNUC__  )
24
+#define __packed __attribute__((__packed__))
25
+#endif
26
+
27
+
28
+/* MSC Subclass Codes */
29
+#define MSC_SUBCLASS_RBC                0x01
30
+#define MSC_SUBCLASS_SFF8020I_MMC2      0x02
31
+#define MSC_SUBCLASS_QIC157             0x03
32
+#define MSC_SUBCLASS_UFI                0x04
33
+#define MSC_SUBCLASS_SFF8070I           0x05
34
+#define MSC_SUBCLASS_SCSI               0x06
35
+
36
+/* MSC Protocol Codes */
37
+#define MSC_PROTOCOL_CBI_INT            0x00
38
+#define MSC_PROTOCOL_CBI_NOINT          0x01
39
+#define MSC_PROTOCOL_BULK_ONLY          0x50
40
+
41
+
42
+/* MSC Request Codes */
43
+#define MSC_REQUEST_RESET               0xFF
44
+#define MSC_REQUEST_GET_MAX_LUN         0xFE
45
+
46
+
47
+/* MSC Bulk-only Stage */
48
+#define MSC_BS_CBW                      0       /* Command Block Wrapper */
49
+#define MSC_BS_DATA_OUT                 1       /* Data Out Phase */
50
+#define MSC_BS_DATA_IN                  2       /* Data In Phase */
51
+#define MSC_BS_DATA_IN_LAST             3       /* Data In Last Phase */
52
+#define MSC_BS_DATA_IN_LAST_STALL       4       /* Data In Last Phase with Stall */
53
+#define MSC_BS_CSW                      5       /* Command Status Wrapper */
54
+#define MSC_BS_ERROR                    6       /* Error */
55
+
56
+
57
+/* Bulk-only Command Block Wrapper */
58
+#if defined     (  __CC_ARM  )
59
+typedef __packed struct _MSC_CBW {
60
+#elif defined   (  __GNUC__  )
61
+typedef struct __packed _MSC_CBW {
62
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
63
+typedef __packed struct _MSC_CBW {
64
+#endif
65
+  uint32_t dSignature;
66
+  uint32_t dTag;
67
+  uint32_t dDataLength;
68
+  uint8_t  bmFlags;
69
+  uint8_t  bLUN;
70
+  uint8_t  bCBLength;
71
+  uint8_t  CB[16];
72
+} MSC_CBW;
73
+
74
+/* Bulk-only Command Status Wrapper */
75
+#if defined     (  __CC_ARM  )
76
+typedef __packed struct _MSC_CSW {
77
+#elif defined   (  __GNUC__  )
78
+typedef struct __packed _MSC_CSW {
79
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
80
+typedef __packed struct _MSC_CSW {
81
+#endif
82
+  uint32_t dSignature;
83
+  uint32_t dTag;
84
+  uint32_t dDataResidue;
85
+  uint8_t  bStatus;
86
+} MSC_CSW;
87
+
88
+#define MSC_CBW_Signature               0x43425355
89
+#define MSC_CSW_Signature               0x53425355
90
+
91
+
92
+/* CSW Status Definitions */
93
+#define CSW_CMD_PASSED                  0x00
94
+#define CSW_CMD_FAILED                  0x01
95
+#define CSW_PHASE_ERROR                 0x02
96
+
97
+
98
+/* SCSI Commands */
99
+#define SCSI_TEST_UNIT_READY            0x00
100
+#define SCSI_REQUEST_SENSE              0x03
101
+#define SCSI_FORMAT_UNIT                0x04
102
+#define SCSI_INQUIRY                    0x12
103
+#define SCSI_MODE_SELECT6               0x15
104
+#define SCSI_MODE_SENSE6                0x1A
105
+#define SCSI_START_STOP_UNIT            0x1B
106
+#define SCSI_MEDIA_REMOVAL              0x1E
107
+#define SCSI_READ_FORMAT_CAPACITIES     0x23
108
+#define SCSI_READ_CAPACITY              0x25
109
+#define SCSI_READ10                     0x28
110
+#define SCSI_WRITE10                    0x2A
111
+#define SCSI_VERIFY10                   0x2F
112
+#define SCSI_MODE_SELECT10              0x55
113
+#define SCSI_MODE_SENSE10               0x5A
114
+
115
+
116
+#endif  /* __MSC_H__ */

+ 716
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/mscuser.cpp Näytä tiedosto

@@ -0,0 +1,716 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ *      Name:    MSCUSER.C
5
+ *      Purpose: Mass Storage Class Custom User Module
6
+ *      Version: V1.10
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ *      Copyright (c) 2005-2009 Keil Software.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+extern "C" {
20
+#include "LPC17xx.h"
21
+#include "lpc_types.h"
22
+}
23
+#include "usb.h"
24
+#include "msc.h"
25
+#include "usbcfg.h"
26
+#include "usbhw.h"
27
+#include "usbcore.h"
28
+#include "mscuser.h"
29
+
30
+#include "../chanfs/diskio.h"
31
+#include <debug_frmwrk.h>
32
+
33
+DWORD MSC_BlockCount = 0;
34
+
35
+uint32_t MemOK;                   /* Memory OK */
36
+
37
+DWORD lba;                        /* start block */
38
+DWORD transfer_count;             /* blocks to transfer */
39
+DWORD length;
40
+uint32_t block_offset;            /* current block offset*/
41
+
42
+uint8_t  BulkStage;               /* Bulk Stage */
43
+
44
+uint8_t  BulkBuf[MSC_MAX_PACKET]; /* Bulk In/Out Buffer */
45
+uint8_t  block_cache[MSC_BLOCK_SIZE];
46
+uint8_t  BulkLen;                 /* Bulk In/Out Length */
47
+
48
+MSC_CBW CBW;                   /* Command Block Wrapper */
49
+MSC_CSW CSW;                   /* Command Status Wrapper */
50
+uint8_t media_lock = 0;
51
+
52
+uint32_t MSC_SD_Lock() {
53
+  media_lock = CBW.CB[4]; //0x1 - lock, 0x0 - unlock
54
+  // logical_unit = CBW.CB[1] & 0xE0;
55
+  CSW.bStatus = CSW_CMD_PASSED;
56
+  MSC_SetCSW();
57
+  return 0;
58
+}
59
+
60
+uint32_t MSC_SD_Release(uint8_t pdrv) {
61
+  MSC_BlockCount = 0;
62
+  return 0;
63
+}
64
+
65
+uint32_t MSC_SD_Init(uint8_t pdrv) {
66
+  DSTATUS ret = disk_initialize(pdrv);
67
+  if(ret) return ret;
68
+  if(disk_ioctl (pdrv, GET_SECTOR_COUNT, (void *)(&MSC_BlockCount))) return 1;
69
+  return 0;
70
+}
71
+
72
+#define STARTSTOP_STOPMOTOR        0x0
73
+#define STARTSTOP_STARTMOTOR       0x1
74
+#define STARTSTOP_EJECT            0x2
75
+#define STARTSTOP_LOAD             0x3
76
+
77
+void MSC_StartStopUnit() {
78
+  switch (CBW.CB[4] & 0x03) {
79
+    case STARTSTOP_EJECT:
80
+      MSC_SD_Release(0);
81
+      break;
82
+    case STARTSTOP_LOAD:
83
+      if(MSC_BlockCount == 0) {
84
+        if(MSC_SD_Init(0) != 0) {
85
+          CSW.bStatus = CSW_CMD_FAILED;
86
+          MSC_SetCSW();
87
+          return;
88
+        }
89
+      }
90
+      break;
91
+    default:
92
+      _DBG("MSC_StartStopUnit unknown startstopunit sub command\n");
93
+  }
94
+  CSW.bStatus = CSW_CMD_PASSED;
95
+  MSC_SetCSW();
96
+}
97
+
98
+/*
99
+ *  MSC Mass Storage Reset Request Callback
100
+ *   Called automatically on Mass Storage Reset Request
101
+ *    Parameters:      None (global SetupPacket and EP0Buf)
102
+ *    Return Value:    TRUE - Success, FALSE - Error
103
+ */
104
+
105
+uint32_t MSC_Reset (void) {
106
+  BulkStage = MSC_BS_CBW;
107
+  return (TRUE);
108
+}
109
+
110
+
111
+/*
112
+ *  MSC Get Max LUN Request Callback
113
+ *   Called automatically on Get Max LUN Request
114
+ *    Parameters:      None (global SetupPacket and EP0Buf)
115
+ *    Return Value:    TRUE - Success, FALSE - Error
116
+ */
117
+
118
+uint32_t MSC_GetMaxLUN (void) {
119
+
120
+  EP0Buf[0] = 0;               /* No LUN associated with this device */
121
+  return (TRUE);
122
+}
123
+
124
+
125
+/*
126
+ *  MSC Memory Read Callback
127
+ *   Called automatically on Memory Read Event
128
+ *    Parameters:      None (global variables)
129
+ *    Return Value:    None
130
+ */
131
+
132
+void MSC_MemoryRead (void) {
133
+  uint32_t n = (length > MSC_MAX_PACKET) ? MSC_MAX_PACKET : length;
134
+
135
+  if (lba > MSC_BlockCount) {
136
+      n = (MSC_BlockCount - lba) * MSC_BLOCK_SIZE + block_offset;
137
+      BulkStage = MSC_BS_ERROR;
138
+  }
139
+
140
+  if(block_offset == 0) {
141
+    disk_read (0, block_cache, lba, 1);
142
+  }
143
+
144
+  USB_WriteEP(MSC_EP_IN, &block_cache[block_offset], n);
145
+
146
+  block_offset += n;
147
+  length -= n;
148
+  CSW.dDataResidue -= n;
149
+
150
+  if(block_offset >= MSC_BLOCK_SIZE) {
151
+    block_offset = 0;
152
+    ++lba;
153
+  }
154
+
155
+  if (length == 0) {
156
+    BulkStage = MSC_BS_DATA_IN_LAST;
157
+  }
158
+
159
+  if (BulkStage != MSC_BS_DATA_IN) {
160
+    CSW.bStatus = CSW_CMD_PASSED;
161
+  }
162
+}
163
+
164
+
165
+/*
166
+ *  MSC Memory Write Callback
167
+ *   Called automatically on Memory Write Event
168
+ *    Parameters:      None (global variables)
169
+ *    Return Value:    None
170
+ */
171
+
172
+void MSC_MemoryWrite (void) {
173
+
174
+  for (uint32_t n = 0; n < BulkLen; n++) {
175
+    block_cache[block_offset + n] = BulkBuf[n];
176
+  }
177
+
178
+  if(block_offset + BulkLen >= MSC_BLOCK_SIZE) {
179
+    if(!(disk_status(0) & STA_PROTECT)){
180
+      disk_write(0, block_cache, lba, 1);
181
+    }
182
+  }
183
+
184
+  block_offset += BulkLen;
185
+  length -= BulkLen;
186
+  CSW.dDataResidue -= BulkLen;
187
+
188
+  if(block_offset >= MSC_BLOCK_SIZE) {
189
+    block_offset = 0;
190
+    ++lba;
191
+  }
192
+
193
+  if ((length == 0) || (BulkStage == MSC_BS_CSW)) {
194
+    CSW.bStatus = CSW_CMD_PASSED;
195
+    MSC_SetCSW();
196
+  }
197
+}
198
+
199
+
200
+/*
201
+ *  MSC Memory Verify Callback
202
+ *   Called automatically on Memory Verify Event
203
+ *    Parameters:      None (global variables)
204
+ *    Return Value:    None
205
+ */
206
+
207
+void MSC_MemoryVerify (void) {
208
+
209
+  if(!block_offset) {
210
+    disk_read(0, block_cache, lba, 1);
211
+  }
212
+
213
+  for (uint32_t n = 0; n < BulkLen; n++) {
214
+    if (block_cache[block_offset + n] != BulkBuf[n]) {
215
+      MemOK = FALSE;
216
+      break;
217
+    }
218
+  }
219
+
220
+  block_offset += BulkLen;
221
+  length -= BulkLen;
222
+  CSW.dDataResidue -= BulkLen;
223
+
224
+  if ((length == 0) || (BulkStage == MSC_BS_CSW)) {
225
+    CSW.bStatus = (MemOK) ? CSW_CMD_PASSED : CSW_CMD_FAILED;
226
+    MSC_SetCSW();
227
+  }
228
+}
229
+
230
+
231
+/*
232
+ *  MSC SCSI Read/Write Setup Callback
233
+ *    Parameters:      None (global variables)
234
+ *    Return Value:    TRUE - Success, FALSE - Error
235
+ */
236
+
237
+uint32_t MSC_RWSetup (void) {
238
+  uint32_t n;
239
+
240
+  /* Logical Block Address of First Block */
241
+  lba = (CBW.CB[2] << 24) |
242
+        (CBW.CB[3] << 16) |
243
+        (CBW.CB[4] <<  8) |
244
+        (CBW.CB[5] <<  0);
245
+
246
+  /* Number of Blocks to transfer */
247
+  transfer_count = (CBW.CB[7] <<  8) |
248
+                   (CBW.CB[8] <<  0);
249
+
250
+  block_offset = 0;
251
+  length = transfer_count * MSC_BLOCK_SIZE;
252
+
253
+  if (CBW.dDataLength != (transfer_count * MSC_BLOCK_SIZE)) {
254
+    USB_SetStallEP(MSC_EP_IN);
255
+    USB_SetStallEP(MSC_EP_OUT);
256
+    CSW.bStatus = CSW_PHASE_ERROR;
257
+    MSC_SetCSW();
258
+    return (FALSE);
259
+  }
260
+
261
+  return (TRUE);
262
+}
263
+
264
+
265
+/*
266
+ *  Check Data IN Format
267
+ *    Parameters:      None (global variables)
268
+ *    Return Value:    TRUE - Success, FALSE - Error
269
+ */
270
+
271
+uint32_t DataInFormat (void) {
272
+
273
+  if (CBW.dDataLength == 0) {
274
+    CSW.bStatus = CSW_PHASE_ERROR;
275
+    MSC_SetCSW();
276
+    return (FALSE);
277
+  }
278
+  if ((CBW.bmFlags & 0x80) == 0) {
279
+    USB_SetStallEP(MSC_EP_OUT);
280
+    CSW.bStatus = CSW_PHASE_ERROR;
281
+    MSC_SetCSW();
282
+    return (FALSE);
283
+  }
284
+  return (TRUE);
285
+}
286
+
287
+
288
+/*
289
+ *  Perform Data IN Transfer
290
+ *    Parameters:      None (global variables)
291
+ *    Return Value:    TRUE - Success, FALSE - Error
292
+ */
293
+
294
+void DataInTransfer (void) {
295
+
296
+  if (BulkLen > CBW.dDataLength) {
297
+    BulkLen = CBW.dDataLength;
298
+  }
299
+
300
+  USB_WriteEP(MSC_EP_IN, BulkBuf, BulkLen);
301
+  BulkStage = MSC_BS_DATA_IN_LAST;
302
+
303
+  CSW.dDataResidue -= BulkLen;
304
+  CSW.bStatus = CSW_CMD_PASSED;
305
+}
306
+
307
+
308
+/*
309
+ *  MSC SCSI Test Unit Ready Callback
310
+ *    Parameters:      None (global variables)
311
+ *    Return Value:    None
312
+ */
313
+
314
+void MSC_TestUnitReady (void) {
315
+
316
+  if (CBW.dDataLength != 0) {
317
+    if ((CBW.bmFlags & 0x80) != 0) {
318
+      USB_SetStallEP(MSC_EP_IN);
319
+    } else {
320
+      USB_SetStallEP(MSC_EP_OUT);
321
+    }
322
+  }
323
+
324
+  if(MSC_BlockCount > 0) {
325
+    CSW.bStatus = CSW_CMD_PASSED;
326
+  } else {
327
+    CSW.bStatus = CSW_CMD_FAILED;
328
+  }
329
+
330
+  MSC_SetCSW();
331
+}
332
+
333
+
334
+/*
335
+ *  MSC SCSI Request Sense Callback
336
+ *    Parameters:      None (global variables)
337
+ *    Return Value:    None
338
+ */
339
+
340
+void MSC_RequestSense (void) {
341
+
342
+  if (!DataInFormat()) return;
343
+
344
+  BulkBuf[ 0] = 0x70;          /* Response Code */
345
+  BulkBuf[ 1] = 0x00;
346
+  BulkBuf[ 2] = static_cast<uint8_t>(Sense_KEY::ILLEGAL_REQUEST);
347
+  BulkBuf[ 3] = 0x00;
348
+  BulkBuf[ 4] = 0x00;
349
+  BulkBuf[ 5] = 0x00;
350
+  BulkBuf[ 6] = 0x00;
351
+  BulkBuf[ 7] = 0x0A;           /* Additional Length */
352
+
353
+  BulkBuf[ 8] = 0x00;
354
+  BulkBuf[ 9] = 0x00;
355
+  BulkBuf[10] = 0x00;
356
+  BulkBuf[11] = 0x00;
357
+  BulkBuf[12] = static_cast<uint8_t>(Sense_ASC::CANNOT_READ_MEDIUM);
358
+  BulkBuf[13] = static_cast<uint8_t>(Sense_ASCQ::UNKNOWN_FORMAT);
359
+  BulkBuf[14] = 0x00;
360
+  BulkBuf[15] = 0x00;
361
+  BulkBuf[16] = 0x00;
362
+  BulkBuf[17] = 0x00;
363
+
364
+  if (MSC_BlockCount == 0) {
365
+    BulkBuf[ 2] = static_cast<uint8_t>(Sense_KEY::NOT_READY);
366
+    BulkBuf[12] = static_cast<uint8_t>(Sense_ASC::MEDIUM_NOT_PRESENT);
367
+    BulkBuf[13] = static_cast<uint8_t>(Sense_ASCQ::LOADABLE);
368
+  }
369
+
370
+  BulkLen = 18;
371
+  DataInTransfer();
372
+}
373
+
374
+
375
+/*
376
+ *  MSC SCSI Inquiry Callback
377
+ *  Parameters:      None (global variables)
378
+ *  Return Value:    None
379
+ */
380
+
381
+void MSC_Inquiry (void) {
382
+
383
+  if (!DataInFormat()) return;
384
+
385
+  BulkBuf[ 0] = 0x00;          /* Direct Access Device */
386
+  BulkBuf[ 1] = 0x80;          /* RMB = 1: Removable Medium */
387
+  BulkBuf[ 2] = 0x00;          /* Version: No conformance claim to standard */
388
+  BulkBuf[ 3] = 0x01;
389
+
390
+  BulkBuf[ 4] = 36-4;          /* Additional Length */
391
+  BulkBuf[ 5] = 0x80;          /* SCCS = 1: Storage Controller Component */
392
+  BulkBuf[ 6] = 0x00;
393
+  BulkBuf[ 7] = 0x00;
394
+
395
+  BulkBuf[ 8] = 'M';           /* Vendor Identification */
396
+  BulkBuf[ 9] = 'a';
397
+  BulkBuf[10] = 'r';
398
+  BulkBuf[11] = 'l';
399
+  BulkBuf[12] = 'i';
400
+  BulkBuf[13] = 'n';
401
+  BulkBuf[14] = ' ';
402
+  BulkBuf[15] = ' ';
403
+
404
+  BulkBuf[16] = 'R';           /* Product Identification */
405
+  BulkBuf[17] = 'e';
406
+  BulkBuf[18] = '-';
407
+  BulkBuf[19] = 'A';
408
+  BulkBuf[20] = 'R';
409
+  BulkBuf[21] = 'M';
410
+  BulkBuf[22] = ' ';
411
+  BulkBuf[23] = 'S';
412
+  BulkBuf[24] = 'D';
413
+  BulkBuf[25] = 'C';
414
+  BulkBuf[26] = 'a';
415
+  BulkBuf[27] = 'r';
416
+  BulkBuf[28] = 'd';
417
+  BulkBuf[29] = ' ';
418
+  BulkBuf[30] = '0';
419
+  BulkBuf[31] = '1';
420
+
421
+  BulkBuf[32] = '1';           /* Product Revision Level */
422
+  BulkBuf[33] = '.';
423
+  BulkBuf[34] = '0';
424
+  BulkBuf[35] = ' ';
425
+
426
+  if(MSC_BlockCount == 0) {
427
+    BulkBuf[0] = 0x20; // Direct Access Device usually available but not currently
428
+  }
429
+
430
+  BulkLen = 36;
431
+  DataInTransfer();
432
+}
433
+
434
+
435
+/*
436
+ *  MSC SCSI Mode Sense (6-Byte) Callback
437
+ *    Parameters:      None (global variables)
438
+ *    Return Value:    None
439
+ */
440
+
441
+void MSC_ModeSense6 (void) {
442
+
443
+  if (!DataInFormat()) return;
444
+
445
+  BulkBuf[ 0] = 0x03;
446
+  BulkBuf[ 1] = 0x00;
447
+  BulkBuf[ 2] = 0x00;
448
+  BulkBuf[ 3] = 0x00;
449
+
450
+  BulkLen = 4;
451
+  DataInTransfer();
452
+}
453
+
454
+
455
+/*
456
+ *  MSC SCSI Mode Sense (10-Byte) Callback
457
+ *    Parameters:      None (global variables)
458
+ *    Return Value:    None
459
+ */
460
+
461
+void MSC_ModeSense10 (void) {
462
+
463
+  if (!DataInFormat()) return;
464
+
465
+  BulkBuf[ 0] = 0x00;
466
+  BulkBuf[ 1] = 0x06;
467
+  BulkBuf[ 2] = 0x00;
468
+  BulkBuf[ 3] = 0x00;
469
+  BulkBuf[ 4] = 0x00;
470
+  BulkBuf[ 5] = 0x00;
471
+  BulkBuf[ 6] = 0x00;
472
+  BulkBuf[ 7] = 0x00;
473
+
474
+  BulkLen = 8;
475
+  DataInTransfer();
476
+}
477
+
478
+
479
+/*
480
+ *  MSC SCSI Read Capacity Callback
481
+ *    Parameters:      None (global variables)
482
+ *    Return Value:    None
483
+ */
484
+
485
+void MSC_ReadCapacity (void) {
486
+
487
+  if (!DataInFormat()) return;
488
+
489
+  /* Last Logical Block */
490
+  BulkBuf[ 0] = ((MSC_BlockCount - 1) >> 24) & 0xFF;
491
+  BulkBuf[ 1] = ((MSC_BlockCount - 1) >> 16) & 0xFF;
492
+  BulkBuf[ 2] = ((MSC_BlockCount - 1) >>  8) & 0xFF;
493
+  BulkBuf[ 3] = ((MSC_BlockCount - 1) >>  0) & 0xFF;
494
+
495
+  /* Block Length */
496
+  BulkBuf[ 4] = (MSC_BLOCK_SIZE >> 24) & 0xFF;
497
+  BulkBuf[ 5] = (MSC_BLOCK_SIZE >> 16) & 0xFF;
498
+  BulkBuf[ 6] = (MSC_BLOCK_SIZE >>  8) & 0xFF;
499
+  BulkBuf[ 7] = (MSC_BLOCK_SIZE >>  0) & 0xFF;
500
+
501
+  BulkLen = 8;
502
+  DataInTransfer();
503
+}
504
+
505
+
506
+/*
507
+ *  MSC SCSI Read Format Capacity Callback
508
+ *    Parameters:      None (global variables)
509
+ *    Return Value:    None
510
+ */
511
+
512
+void MSC_ReadFormatCapacity (void) {
513
+
514
+  if (!DataInFormat()) return;
515
+
516
+  BulkBuf[ 0] = 0x00;
517
+  BulkBuf[ 1] = 0x00;
518
+  BulkBuf[ 2] = 0x00;
519
+  BulkBuf[ 3] = 0x08;          /* Capacity List Length */
520
+
521
+  /* Block Count */
522
+  BulkBuf[ 4] = (MSC_BlockCount >> 24) & 0xFF;
523
+  BulkBuf[ 5] = (MSC_BlockCount >> 16) & 0xFF;
524
+  BulkBuf[ 6] = (MSC_BlockCount >>  8) & 0xFF;
525
+  BulkBuf[ 7] = (MSC_BlockCount >>  0) & 0xFF;
526
+
527
+  /* Block Length */
528
+  BulkBuf[ 8] = 0x02;          /* Descriptor Code: Formatted Media */
529
+  BulkBuf[ 9] = (MSC_BLOCK_SIZE >> 16) & 0xFF;
530
+  BulkBuf[10] = (MSC_BLOCK_SIZE >>  8) & 0xFF;
531
+  BulkBuf[11] = (MSC_BLOCK_SIZE >>  0) & 0xFF;
532
+
533
+  BulkLen = 12;
534
+  DataInTransfer();
535
+}
536
+
537
+
538
+/*
539
+ *  MSC Get Command Block Wrapper Callback
540
+ *    Parameters:      None (global variables)
541
+ *    Return Value:    None
542
+ */
543
+
544
+void MSC_GetCBW (void) {
545
+  uint32_t n;
546
+
547
+  for (n = 0; n < BulkLen; n++) {
548
+    *((uint8_t *)&CBW + n) = BulkBuf[n];
549
+  }
550
+  if ((BulkLen == sizeof(CBW)) && (CBW.dSignature == MSC_CBW_Signature)) {
551
+    /* Valid CBW */
552
+    CSW.dTag = CBW.dTag;
553
+    CSW.dDataResidue = CBW.dDataLength;
554
+    if ((CBW.bLUN != 0) || (CBW.bCBLength < 1) || CBW.bCBLength > 16) {
555
+fail: CSW.bStatus = CSW_CMD_FAILED;
556
+      MSC_SetCSW();
557
+      _DBG("Failed SCSI OP code ");
558
+      _DBH(CBW.CB[0]);
559
+      _DBG("\n");
560
+    } else {
561
+      switch (CBW.CB[0]) {
562
+        case SCSI_TEST_UNIT_READY:
563
+          MSC_TestUnitReady();
564
+          break;
565
+        case SCSI_REQUEST_SENSE:
566
+          MSC_RequestSense();
567
+          break;
568
+        case SCSI_FORMAT_UNIT:
569
+          goto fail;
570
+        case SCSI_INQUIRY:
571
+          MSC_Inquiry();
572
+          break;
573
+        case SCSI_START_STOP_UNIT:
574
+          MSC_StartStopUnit();
575
+          break;
576
+        case SCSI_MEDIA_REMOVAL:
577
+          MSC_SD_Lock();
578
+          break;
579
+        case SCSI_MODE_SELECT6:
580
+          goto fail;
581
+        case SCSI_MODE_SENSE6:
582
+          MSC_ModeSense6();
583
+          break;
584
+        case SCSI_MODE_SELECT10:
585
+          goto fail;
586
+        case SCSI_MODE_SENSE10:
587
+          MSC_ModeSense10();
588
+          break;
589
+        case SCSI_READ_FORMAT_CAPACITIES:
590
+          MSC_ReadFormatCapacity();
591
+          break;
592
+        case SCSI_READ_CAPACITY:
593
+          MSC_ReadCapacity();
594
+          break;
595
+        case SCSI_READ10:
596
+          if (MSC_RWSetup()) {
597
+            if ((CBW.bmFlags & 0x80) != 0) {
598
+              BulkStage = MSC_BS_DATA_IN;
599
+              MSC_MemoryRead();
600
+            } else {
601
+              USB_SetStallEP(MSC_EP_OUT);
602
+              CSW.bStatus = CSW_PHASE_ERROR;
603
+              MSC_SetCSW();
604
+            }
605
+          }
606
+          break;
607
+        case SCSI_WRITE10:
608
+          if (MSC_RWSetup()) {
609
+            if ((CBW.bmFlags & 0x80) == 0) {
610
+              BulkStage = MSC_BS_DATA_OUT;
611
+            } else {
612
+              USB_SetStallEP(MSC_EP_IN);
613
+              CSW.bStatus = CSW_PHASE_ERROR;
614
+              MSC_SetCSW();
615
+            }
616
+          }
617
+          break;
618
+        case SCSI_VERIFY10:
619
+          if (MSC_RWSetup()) {
620
+            if ((CBW.bmFlags & 0x80) == 0) {
621
+              BulkStage = MSC_BS_DATA_OUT;
622
+              MemOK = TRUE;
623
+            } else {
624
+              USB_SetStallEP(MSC_EP_IN);
625
+              CSW.bStatus = CSW_PHASE_ERROR;
626
+              MSC_SetCSW();
627
+            }
628
+          }
629
+          break;
630
+        default:
631
+          goto fail;
632
+      }
633
+    }
634
+  } else {
635
+    /* Invalid CBW */
636
+    USB_SetStallEP(MSC_EP_IN);
637
+    USB_SetStallEP(MSC_EP_OUT);
638
+    BulkStage = MSC_BS_ERROR;
639
+  }
640
+}
641
+
642
+
643
+/*
644
+ *  MSC Set Command Status Wrapper Callback
645
+ *    Parameters:      None (global variables)
646
+ *    Return Value:    None
647
+ */
648
+
649
+void MSC_SetCSW (void) {
650
+
651
+  CSW.dSignature = MSC_CSW_Signature;
652
+  USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
653
+  BulkStage = MSC_BS_CSW;
654
+}
655
+
656
+
657
+/*
658
+ *  MSC Bulk In Callback
659
+ *    Parameters:      None (global variables)
660
+ *    Return Value:    None
661
+ */
662
+
663
+void MSC_BulkIn (void) {
664
+
665
+  switch (BulkStage) {
666
+    case MSC_BS_DATA_IN:
667
+      switch (CBW.CB[0]) {
668
+        case SCSI_READ10:
669
+          MSC_MemoryRead();
670
+          break;
671
+      }
672
+      break;
673
+    case MSC_BS_DATA_IN_LAST:
674
+      MSC_SetCSW();
675
+      break;
676
+    case MSC_BS_DATA_IN_LAST_STALL:
677
+      USB_SetStallEP(MSC_EP_IN);
678
+      MSC_SetCSW();
679
+      break;
680
+    case MSC_BS_CSW:
681
+      BulkStage = MSC_BS_CBW;
682
+      break;
683
+  }
684
+}
685
+
686
+
687
+/*
688
+ *  MSC Bulk Out Callback
689
+ *    Parameters:      None (global variables)
690
+ *    Return Value:    None
691
+ */
692
+
693
+void MSC_BulkOut (void) {
694
+
695
+  BulkLen = (uint8_t)USB_ReadEP(MSC_EP_OUT, BulkBuf);
696
+  switch (BulkStage) {
697
+    case MSC_BS_CBW:
698
+      MSC_GetCBW();
699
+      break;
700
+    case MSC_BS_DATA_OUT:
701
+      switch (CBW.CB[0]) {
702
+        case SCSI_WRITE10:
703
+          MSC_MemoryWrite();
704
+          break;
705
+        case SCSI_VERIFY10:
706
+          MSC_MemoryVerify();
707
+          break;
708
+      }
709
+      break;
710
+    default:
711
+      USB_SetStallEP(MSC_EP_OUT);
712
+      CSW.bStatus = CSW_PHASE_ERROR;
713
+      MSC_SetCSW();
714
+      break;
715
+  }
716
+}

+ 69
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/mscuser.h Näytä tiedosto

@@ -0,0 +1,69 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ *      Name:    MSCUSER.H
5
+ *      Purpose: Mass Storage Class Custom User Definitions
6
+ *      Version: V1.10
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing 
14
+ *      else gives you the right to use this software.
15
+ *
16
+ *      Copyright (c) 2005-2009 Keil Software.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __MSCUSER_H__
20
+#define __MSCUSER_H__
21
+
22
+
23
+/* Max In/Out Packet Size */
24
+#define MSC_MAX_PACKET  64
25
+#define MSC_BLOCK_SIZE 512
26
+
27
+/* MSC In/Out Endpoint Address */
28
+#define MSC_EP_IN       0x85
29
+#define MSC_EP_OUT      0x05
30
+
31
+/* MSC Requests Callback Functions */
32
+extern uint32_t MSC_Reset     (void);
33
+extern uint32_t MSC_GetMaxLUN (void);
34
+
35
+/* MSC Bulk Callback Functions */
36
+extern void MSC_GetCBW (void);
37
+extern void MSC_SetCSW (void);
38
+extern void MSC_BulkIn (void);
39
+extern void MSC_BulkOut(void);
40
+
41
+enum class Sense_KEY : uint8_t {
42
+  NO_SENSE,
43
+  RECOVERED_ERROR,
44
+  NOT_READY,
45
+  MEDIUM_ERROR,
46
+  HARDWARE_ERROR,
47
+  ILLEGAL_REQUEST,
48
+  UNIT_ATTENTION,
49
+  DATA_PROTECT
50
+};
51
+
52
+enum class Sense_ASC : uint8_t {
53
+  CANNOT_READ_MEDIUM = 0x30,
54
+  MEDIUM_NOT_PRESENT = 0x3A
55
+};
56
+
57
+enum class Sense_ASCQ : uint8_t {
58
+  // CANNOT_READ_MEDIUM
59
+  UNKNOWN_FORMAT = 0x01,
60
+  // MEDIUM_NOT_PRESENT
61
+  REASON_UNKNOWN = 0x00,
62
+  TRAY_CLOSED,
63
+  TRAY_OPEN,
64
+  LOADABLE,
65
+  AUXILIARY_MEMORY_ACCESSIBLE
66
+};
67
+
68
+
69
+#endif  /* __MSCUSER_H__ */

+ 353
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usb.h Näytä tiedosto

@@ -0,0 +1,353 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usb.h
5
+ * Purpose: USB Definitions
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __USB_H__
20
+#define __USB_H__
21
+extern "C" {
22
+#include "lpc_types.h"
23
+}
24
+#if defined   (  __GNUC__  )
25
+#define __packed __attribute__((__packed__))
26
+#endif
27
+
28
+#if defined     (  __CC_ARM  )
29
+typedef __packed union {
30
+#elif defined   (  __GNUC__  )
31
+typedef union __packed {
32
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
33
+#pragma pack(1)
34
+typedef union {
35
+#endif
36
+  uint16_t W;
37
+#if defined     (  __CC_ARM  )
38
+  __packed struct {
39
+#elif defined   (  __GNUC__  )
40
+  struct __packed {
41
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
42
+#pragma pack(1)
43
+  struct {
44
+#endif
45
+    uint8_t L;
46
+    uint8_t H;
47
+  } WB;
48
+#ifdef __IAR_SYSTEMS_ICC__
49
+#pragma pack()
50
+#endif
51
+} WORD_BYTE;
52
+#ifdef __IAR_SYSTEMS_ICC__
53
+#pragma pack()
54
+#endif
55
+
56
+
57
+/* bmRequestType.Dir */
58
+#define REQUEST_HOST_TO_DEVICE     0
59
+#define REQUEST_DEVICE_TO_HOST     1
60
+
61
+/* bmRequestType.Type */
62
+#define REQUEST_STANDARD           0
63
+#define REQUEST_CLASS              1
64
+#define REQUEST_VENDOR             2
65
+#define REQUEST_RESERVED           3
66
+
67
+/* bmRequestType.Recipient */
68
+#define REQUEST_TO_DEVICE          0
69
+#define REQUEST_TO_INTERFACE       1
70
+#define REQUEST_TO_ENDPOINT        2
71
+#define REQUEST_TO_OTHER           3
72
+
73
+/* bmRequestType Definition */
74
+#if defined     (  __CC_ARM  )
75
+typedef __packed union _REQUEST_TYPE {
76
+#elif defined   (  __GNUC__  )
77
+typedef union __packed _REQUEST_TYPE {
78
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
79
+#pragma pack(1)
80
+typedef union _REQUEST_TYPE {
81
+#endif
82
+#if defined     (  __CC_ARM  )
83
+  __packed struct _BM {
84
+#elif defined   (  __GNUC__  )
85
+  struct __packed _BM {
86
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
87
+#pragma pack(1)
88
+  struct _BM {
89
+#endif
90
+    uint8_t Recipient : 5;
91
+    uint8_t Type      : 2;
92
+    uint8_t Dir       : 1;
93
+  } BM;
94
+#ifdef __IAR_SYSTEMS_ICC__
95
+#pragma pack()
96
+#endif
97
+  uint8_t B;
98
+} REQUEST_TYPE;
99
+#ifdef __IAR_SYSTEMS_ICC__
100
+#pragma pack()
101
+#endif
102
+
103
+/* USB Standard Request Codes */
104
+#define USB_REQUEST_GET_STATUS                 0
105
+#define USB_REQUEST_CLEAR_FEATURE              1
106
+#define USB_REQUEST_SET_FEATURE                3
107
+#define USB_REQUEST_SET_ADDRESS                5
108
+#define USB_REQUEST_GET_DESCRIPTOR             6
109
+#define USB_REQUEST_SET_DESCRIPTOR             7
110
+#define USB_REQUEST_GET_CONFIGURATION          8
111
+#define USB_REQUEST_SET_CONFIGURATION          9
112
+#define USB_REQUEST_GET_INTERFACE              10
113
+#define USB_REQUEST_SET_INTERFACE              11
114
+#define USB_REQUEST_SYNC_FRAME                 12
115
+
116
+/* USB GET_STATUS Bit Values */
117
+#define USB_GETSTATUS_SELF_POWERED             0x01
118
+#define USB_GETSTATUS_REMOTE_WAKEUP            0x02
119
+#define USB_GETSTATUS_ENDPOINT_STALL           0x01
120
+
121
+/* USB Standard Feature selectors */
122
+#define USB_FEATURE_ENDPOINT_STALL             0
123
+#define USB_FEATURE_REMOTE_WAKEUP              1
124
+
125
+/* USB Default Control Pipe Setup Packet */
126
+#if defined     (  __CC_ARM  )
127
+typedef __packed struct _USB_SETUP_PACKET {
128
+#elif defined   (  __GNUC__  )
129
+typedef struct __packed _USB_SETUP_PACKET {
130
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
131
+#pragma pack(1)
132
+typedef struct _USB_SETUP_PACKET {
133
+#endif
134
+  REQUEST_TYPE bmRequestType;
135
+  uint8_t         bRequest;
136
+  WORD_BYTE    wValue;
137
+  WORD_BYTE    wIndex;
138
+  uint16_t         wLength;
139
+} USB_SETUP_PACKET;
140
+#ifdef __IAR_SYSTEMS_ICC__
141
+#pragma pack()
142
+#endif
143
+
144
+
145
+/* USB Descriptor Types */
146
+#define USB_DEVICE_DESCRIPTOR_TYPE             1
147
+#define USB_CONFIGURATION_DESCRIPTOR_TYPE      2
148
+#define USB_STRING_DESCRIPTOR_TYPE             3
149
+#define USB_INTERFACE_DESCRIPTOR_TYPE          4
150
+#define USB_ENDPOINT_DESCRIPTOR_TYPE           5
151
+#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE   6
152
+#define USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE 7
153
+#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE    8
154
+#define USB_OTG_DESCRIPTOR_TYPE                     9
155
+#define USB_DEBUG_DESCRIPTOR_TYPE                  10
156
+#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE  11
157
+
158
+/* USB Device Classes */
159
+#define USB_DEVICE_CLASS_RESERVED              0x00
160
+#define USB_DEVICE_CLASS_AUDIO                 0x01
161
+#define USB_DEVICE_CLASS_COMMUNICATIONS        0x02
162
+#define USB_DEVICE_CLASS_HUMAN_INTERFACE       0x03
163
+#define USB_DEVICE_CLASS_MONITOR               0x04
164
+#define USB_DEVICE_CLASS_PHYSICAL_INTERFACE    0x05
165
+#define USB_DEVICE_CLASS_POWER                 0x06
166
+#define USB_DEVICE_CLASS_PRINTER               0x07
167
+#define USB_DEVICE_CLASS_STORAGE               0x08
168
+#define USB_DEVICE_CLASS_HUB                   0x09
169
+#define USB_DEVICE_CLASS_MISCELLANEOUS         0xEF
170
+#define USB_DEVICE_CLASS_VENDOR_SPECIFIC       0xFF
171
+
172
+/* bmAttributes in Configuration Descriptor */
173
+#define USB_CONFIG_POWERED_MASK                0x40
174
+#define USB_CONFIG_BUS_POWERED                 0x80
175
+#define USB_CONFIG_SELF_POWERED                0xC0
176
+#define USB_CONFIG_REMOTE_WAKEUP               0x20
177
+
178
+/* bMaxPower in Configuration Descriptor */
179
+#define USB_CONFIG_POWER_MA(mA)                ((mA)/2)
180
+
181
+/* bEndpointAddress in Endpoint Descriptor */
182
+#define USB_ENDPOINT_DIRECTION_MASK            0x80
183
+#define USB_ENDPOINT_OUT(addr)                 ((addr) | 0x00)
184
+#define USB_ENDPOINT_IN(addr)                  ((addr) | 0x80)
185
+
186
+/* bmAttributes in Endpoint Descriptor */
187
+#define USB_ENDPOINT_TYPE_MASK                 0x03
188
+#define USB_ENDPOINT_TYPE_CONTROL              0x00
189
+#define USB_ENDPOINT_TYPE_ISOCHRONOUS          0x01
190
+#define USB_ENDPOINT_TYPE_BULK                 0x02
191
+#define USB_ENDPOINT_TYPE_INTERRUPT            0x03
192
+#define USB_ENDPOINT_SYNC_MASK                 0x0C
193
+#define USB_ENDPOINT_SYNC_NO_SYNCHRONIZATION   0x00
194
+#define USB_ENDPOINT_SYNC_ASYNCHRONOUS         0x04
195
+#define USB_ENDPOINT_SYNC_ADAPTIVE             0x08
196
+#define USB_ENDPOINT_SYNC_SYNCHRONOUS          0x0C
197
+#define USB_ENDPOINT_USAGE_MASK                0x30
198
+#define USB_ENDPOINT_USAGE_DATA                0x00
199
+#define USB_ENDPOINT_USAGE_FEEDBACK            0x10
200
+#define USB_ENDPOINT_USAGE_IMPLICIT_FEEDBACK   0x20
201
+#define USB_ENDPOINT_USAGE_RESERVED            0x30
202
+
203
+/* USB Standard Device Descriptor */
204
+#if defined     (  __CC_ARM  )
205
+typedef __packed struct _USB_DEVICE_DESCRIPTOR {
206
+#elif defined   (  __GNUC__  )
207
+typedef struct __packed _USB_DEVICE_DESCRIPTOR {
208
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
209
+#pragma pack(1)
210
+typedef struct _USB_DEVICE_DESCRIPTOR {
211
+#endif
212
+  uint8_t  bLength;
213
+  uint8_t  bDescriptorType;
214
+  uint16_t  bcdUSB;
215
+  uint8_t  bDeviceClass;
216
+  uint8_t  bDeviceSubClass;
217
+  uint8_t  bDeviceProtocol;
218
+  uint8_t  bMaxPacketSize0;
219
+  uint16_t  idVendor;
220
+  uint16_t  idProduct;
221
+  uint16_t  bcdDevice;
222
+  uint8_t  iManufacturer;
223
+  uint8_t  iProduct;
224
+  uint8_t  iSerialNumber;
225
+  uint8_t  bNumConfigurations;
226
+} USB_DEVICE_DESCRIPTOR;
227
+#ifdef __IAR_SYSTEMS_ICC__
228
+#pragma pack()
229
+#endif
230
+
231
+/* USB 2.0 Device Qualifier Descriptor */
232
+#if defined     (  __CC_ARM  )
233
+typedef __packed struct _USB_DEVICE_QUALIFIER_DESCRIPTOR {
234
+#elif defined   (  __GNUC__  )
235
+typedef struct __packed _USB_DEVICE_QUALIFIER_DESCRIPTOR {
236
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
237
+#pragma pack(1)
238
+typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR {
239
+#endif
240
+  uint8_t  bLength;
241
+  uint8_t  bDescriptorType;
242
+  uint16_t  bcdUSB;
243
+  uint8_t  bDeviceClass;
244
+  uint8_t  bDeviceSubClass;
245
+  uint8_t  bDeviceProtocol;
246
+  uint8_t  bMaxPacketSize0;
247
+  uint8_t  bNumConfigurations;
248
+  uint8_t  bReserved;
249
+} USB_DEVICE_QUALIFIER_DESCRIPTOR;
250
+#ifdef __IAR_SYSTEMS_ICC__
251
+#pragma pack()
252
+#endif
253
+
254
+#if defined     (  __CC_ARM  )
255
+typedef __packed struct _USB_CONFIGURATION_DESCRIPTOR {
256
+#elif defined   (  __GNUC__  )
257
+typedef struct __packed _USB_CONFIGURATION_DESCRIPTOR {
258
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
259
+#pragma pack(1)
260
+typedef struct _USB_CONFIGURATION_DESCRIPTOR {
261
+#endif
262
+  uint8_t  bLength;
263
+  uint8_t  bDescriptorType;
264
+  uint16_t  wTotalLength;
265
+  uint8_t  bNumInterfaces;
266
+  uint8_t  bConfigurationValue;
267
+  uint8_t  iConfiguration;
268
+  uint8_t  bmAttributes;
269
+  uint8_t  bMaxPower;
270
+} USB_CONFIGURATION_DESCRIPTOR;
271
+#ifdef __IAR_SYSTEMS_ICC__
272
+#pragma pack()
273
+#endif
274
+
275
+/* USB Standard Interface Descriptor */
276
+#if defined     (  __CC_ARM  )
277
+typedef __packed struct _USB_INTERFACE_DESCRIPTOR {
278
+#elif defined   (  __GNUC__  )
279
+typedef struct __packed _USB_INTERFACE_DESCRIPTOR {
280
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
281
+#pragma pack(1)
282
+typedef struct _USB_INTERFACE_DESCRIPTOR {
283
+#endif
284
+  uint8_t  bLength;
285
+  uint8_t  bDescriptorType;
286
+  uint8_t  bInterfaceNumber;
287
+  uint8_t  bAlternateSetting;
288
+  uint8_t  bNumEndpoints;
289
+  uint8_t  bInterfaceClass;
290
+  uint8_t  bInterfaceSubClass;
291
+  uint8_t  bInterfaceProtocol;
292
+  uint8_t  iInterface;
293
+} USB_INTERFACE_DESCRIPTOR;
294
+#ifdef __IAR_SYSTEMS_ICC__
295
+#pragma pack()
296
+#endif
297
+
298
+/* USB Standard Endpoint Descriptor */
299
+#if defined     (  __CC_ARM  )
300
+typedef __packed struct _USB_ENDPOINT_DESCRIPTOR {
301
+#elif defined   (  __GNUC__  )
302
+typedef struct __packed _USB_ENDPOINT_DESCRIPTOR {
303
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
304
+#pragma pack(1)
305
+typedef struct _USB_ENDPOINT_DESCRIPTOR {
306
+#endif
307
+  uint8_t  bLength;
308
+  uint8_t  bDescriptorType;
309
+  uint8_t  bEndpointAddress;
310
+  uint8_t  bmAttributes;
311
+  uint16_t  wMaxPacketSize;
312
+  uint8_t  bInterval;
313
+} USB_ENDPOINT_DESCRIPTOR;
314
+#ifdef __IAR_SYSTEMS_ICC__
315
+#pragma pack()
316
+#endif
317
+
318
+/* USB String Descriptor */
319
+#if defined     (  __CC_ARM  )
320
+typedef __packed struct _USB_STRING_DESCRIPTOR {
321
+#elif defined   (  __GNUC__  )
322
+typedef struct __packed _USB_STRING_DESCRIPTOR {
323
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
324
+#pragma pack(1)
325
+typedef struct _USB_STRING_DESCRIPTOR {
326
+#endif
327
+  uint8_t  bLength;
328
+  uint8_t  bDescriptorType;
329
+  uint16_t  bString/*[]*/;
330
+} USB_STRING_DESCRIPTOR;
331
+#ifdef __IAR_SYSTEMS_ICC__
332
+#pragma pack()
333
+#endif
334
+
335
+/* USB Common Descriptor */
336
+#if defined     (  __CC_ARM  )
337
+typedef __packed struct _USB_COMMON_DESCRIPTOR {
338
+#elif defined   (  __GNUC__  )
339
+typedef struct __packed _USB_COMMON_DESCRIPTOR {
340
+#elif defined   (  __IAR_SYSTEMS_ICC__  )
341
+#pragma pack(1)
342
+typedef struct _USB_COMMON_DESCRIPTOR {
343
+#endif
344
+  uint8_t  bLength;
345
+  uint8_t  bDescriptorType;
346
+} USB_COMMON_DESCRIPTOR;
347
+#ifdef __IAR_SYSTEMS_ICC__
348
+#pragma pack()
349
+#endif
350
+
351
+
352
+
353
+#endif  /* __USB_H__ */

+ 181
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbcfg.h Näytä tiedosto

@@ -0,0 +1,181 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbcfg.h
5
+ * Purpose: USB Custom Configuration
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing 
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *----------------------------------------------------------------------------
18
+ * History:
19
+ *          V1.20 Added vendor specific support
20
+ *          V1.00 Initial Version
21
+ *---------------------------------------------------------------------------*/
22
+
23
+#ifndef __USBCFG_H__
24
+#define __USBCFG_H__
25
+
26
+
27
+//*** <<< Use Configuration Wizard in Context Menu >>> ***
28
+
29
+
30
+/*
31
+// <h> USB Configuration
32
+//   <o0> USB Power
33
+//        <i> Default Power Setting
34
+//        <0=> Bus-powered
35
+//        <1=> Self-powered
36
+//   <o1> Max Number of Interfaces <1-256>
37
+//   <o2> Max Number of Endpoints  <1-32>
38
+//   <o3> Max Endpoint 0 Packet Size
39
+//        <8=> 8 Bytes <16=> 16 Bytes <32=> 32 Bytes <64=> 64 Bytes
40
+//   <e4> DMA Transfer
41
+//     <i> Use DMA for selected Endpoints
42
+//     <o5.0>  Endpoint 0 Out
43
+//     <o5.1>  Endpoint 0 In
44
+//     <o5.2>  Endpoint 1 Out
45
+//     <o5.3>  Endpoint 1 In
46
+//     <o5.4>  Endpoint 2 Out
47
+//     <o5.5>  Endpoint 2 In
48
+//     <o5.6>  Endpoint 3 Out
49
+//     <o5.7>  Endpoint 3 In
50
+//     <o5.8>  Endpoint 4 Out
51
+//     <o5.9>  Endpoint 4 In
52
+//     <o5.10> Endpoint 5 Out
53
+//     <o5.11> Endpoint 5 In
54
+//     <o5.12> Endpoint 6 Out
55
+//     <o5.13> Endpoint 6 In
56
+//     <o5.14> Endpoint 7 Out
57
+//     <o5.15> Endpoint 7 In
58
+//     <o5.16> Endpoint 8 Out
59
+//     <o5.17> Endpoint 8 In
60
+//     <o5.18> Endpoint 9 Out
61
+//     <o5.19> Endpoint 9 In
62
+//     <o5.20> Endpoint 10 Out
63
+//     <o5.21> Endpoint 10 In
64
+//     <o5.22> Endpoint 11 Out
65
+//     <o5.23> Endpoint 11 In
66
+//     <o5.24> Endpoint 12 Out
67
+//     <o5.25> Endpoint 12 In
68
+//     <o5.26> Endpoint 13 Out
69
+//     <o5.27> Endpoint 13 In
70
+//     <o5.28> Endpoint 14 Out
71
+//     <o5.29> Endpoint 14 In
72
+//     <o5.30> Endpoint 15 Out
73
+//     <o5.31> Endpoint 15 In
74
+//   </e>
75
+// </h>
76
+*/
77
+
78
+#define USB_POWER           0
79
+#define USB_IF_NUM          4
80
+#define USB_EP_NUM          32
81
+#define USB_MAX_PACKET0     64
82
+#define USB_DMA             0
83
+#define USB_DMA_EP          0x00000000
84
+
85
+
86
+/*
87
+// <h> USB Event Handlers
88
+//   <h> Device Events
89
+//     <o0.0> Power Event
90
+//     <o1.0> Reset Event
91
+//     <o2.0> Suspend Event
92
+//     <o3.0> Resume Event
93
+//     <o4.0> Remote Wakeup Event
94
+//     <o5.0> Start of Frame Event
95
+//     <o6.0> Error Event
96
+//   </h>
97
+//   <h> Endpoint Events
98
+//     <o7.0>  Endpoint 0 Event
99
+//     <o7.1>  Endpoint 1 Event
100
+//     <o7.2>  Endpoint 2 Event
101
+//     <o7.3>  Endpoint 3 Event
102
+//     <o7.4>  Endpoint 4 Event
103
+//     <o7.5>  Endpoint 5 Event
104
+//     <o7.6>  Endpoint 6 Event
105
+//     <o7.7>  Endpoint 7 Event
106
+//     <o7.8>  Endpoint 8 Event
107
+//     <o7.9>  Endpoint 9 Event
108
+//     <o7.10> Endpoint 10 Event
109
+//     <o7.11> Endpoint 11 Event
110
+//     <o7.12> Endpoint 12 Event
111
+//     <o7.13> Endpoint 13 Event
112
+//     <o7.14> Endpoint 14 Event
113
+//     <o7.15> Endpoint 15 Event
114
+//   </h>
115
+//   <h> USB Core Events
116
+//     <o8.0>  Set Configuration Event
117
+//     <o9.0>  Set Interface Event
118
+//     <o10.0> Set/Clear Feature Event
119
+//   </h>
120
+// </h>
121
+*/
122
+
123
+#define USB_POWER_EVENT     0
124
+#define USB_RESET_EVENT     1
125
+#define USB_SUSPEND_EVENT   0
126
+#define USB_RESUME_EVENT    0
127
+#define USB_WAKEUP_EVENT    0
128
+#define USB_SOF_EVENT       0
129
+#define USB_ERROR_EVENT     0
130
+#define USB_EP_EVENT        0x0027
131
+#define USB_CONFIGURE_EVENT 1
132
+#define USB_INTERFACE_EVENT 0
133
+#define USB_FEATURE_EVENT   0
134
+
135
+
136
+/*
137
+// <e0> USB Class Support
138
+//   <i> enables USB Class specific Requests
139
+//   <e1> Human Interface Device (HID)
140
+//     <o2> Interface Number <0-255>
141
+//   </e>
142
+//   <e3> Mass Storage
143
+//     <o4> Interface Number <0-255>
144
+//   </e>
145
+//   <e5> Audio Device
146
+//     <o6> Control Interface Number <0-255>
147
+//     <o7> Streaming Interface 1 Number <0-255>
148
+//     <o8> Streaming Interface 2 Number <0-255>
149
+//   </e>
150
+//   <e9> Communication Device
151
+//     <o10> Control Interface Number <0-255>
152
+//     <o11> Bulk Interface Number <0-255>
153
+//     <o12> Max Communication Device Buffer Size
154
+//        <8=> 8 Bytes <16=> 16 Bytes <32=> 32 Bytes <64=> 64 Bytes 
155
+//   </e>
156
+// </e>
157
+*/
158
+
159
+#define USB_CLASS           1
160
+#define USB_HID             0
161
+#define USB_HID_IF_NUM      0
162
+#define USB_MSC             1
163
+#define USB_MSC_IF_NUM      2
164
+#define USB_AUDIO           0
165
+#define USB_ADC_CIF_NUM     0
166
+#define USB_ADC_SIF1_NUM    1
167
+#define USB_ADC_SIF2_NUM    2
168
+#define USB_CDC  			1
169
+#define USB_CDC_CIF_NUM     0
170
+#define USB_CDC_DIF_NUM     1
171
+#define USB_CDC_BUFSIZE     64
172
+
173
+/*
174
+// <e0> USB Vendor Support
175
+//   <i> enables USB Vendor specific Requests
176
+// </e>
177
+*/
178
+#define USB_VENDOR          0
179
+
180
+
181
+#endif  /* __USBCFG_H__ */

+ 815
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbcore.cpp Näytä tiedosto

@@ -0,0 +1,815 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbcore.c
5
+ * Purpose: USB Core Module
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *----------------------------------------------------------------------------
18
+ * History:
19
+ *          V1.20 Added vendor specific requests
20
+ *                Changed string descriptor handling
21
+ *                Reworked Endpoint0
22
+ *          V1.00 Initial Version
23
+ *----------------------------------------------------------------------------*/
24
+extern "C" {
25
+#include "lpc_types.h"
26
+}
27
+#include "usb.h"
28
+#include "usbcfg.h"
29
+#include "usbhw.h"
30
+#include "usbcore.h"
31
+#include "usbdesc.h"
32
+#include "usbuser.h"
33
+
34
+#include "msc.h"
35
+#include "mscuser.h"
36
+extern MSC_CSW CSW;
37
+
38
+#include "cdc.h"
39
+#include "cdcuser.h"
40
+
41
+#define __packed __attribute__((__packed__))
42
+
43
+uint16_t USB_DeviceStatus;
44
+uint8_t USB_DeviceAddress;
45
+uint8_t USB_Configuration;
46
+uint32_t USB_EndPointMask;
47
+uint32_t USB_EndPointHalt;
48
+uint32_t USB_EndPointStall; /* EP must stay stalled */
49
+uint8_t USB_NumInterfaces;
50
+uint8_t USB_AltSetting[USB_IF_NUM];
51
+
52
+uint8_t EP0Buf[USB_MAX_PACKET0];
53
+
54
+USB_EP_DATA EP0Data;
55
+
56
+USB_SETUP_PACKET SetupPacket;
57
+
58
+/*
59
+ *  Reset USB Core
60
+ *    Parameters:      None
61
+ *    Return Value:    None
62
+ */
63
+
64
+void USB_ResetCore(void) {
65
+
66
+  USB_DeviceStatus = USB_POWER;
67
+  USB_DeviceAddress = 0;
68
+  USB_Configuration = 0;
69
+  USB_EndPointMask = 0x00010001;
70
+  USB_EndPointHalt = 0x00000000;
71
+  USB_EndPointStall = 0x00000000;
72
+}
73
+
74
+/*
75
+ *  USB Request - Setup Stage
76
+ *    Parameters:      None (global SetupPacket)
77
+ *    Return Value:    None
78
+ */
79
+
80
+void USB_SetupStage(void) {
81
+  USB_ReadEP(0x00, (uint8_t *) &SetupPacket);
82
+}
83
+
84
+/*
85
+ *  USB Request - Data In Stage
86
+ *    Parameters:      None (global EP0Data)
87
+ *    Return Value:    None
88
+ */
89
+
90
+void USB_DataInStage(void) {
91
+  uint32_t cnt;
92
+
93
+  if (EP0Data.Count > USB_MAX_PACKET0) {
94
+    cnt = USB_MAX_PACKET0;
95
+  } else {
96
+    cnt = EP0Data.Count;
97
+  }
98
+  cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
99
+  EP0Data.pData += cnt;
100
+  EP0Data.Count -= cnt;
101
+}
102
+
103
+/*
104
+ *  USB Request - Data Out Stage
105
+ *    Parameters:      None (global EP0Data)
106
+ *    Return Value:    None
107
+ */
108
+
109
+void USB_DataOutStage(void) {
110
+  uint32_t cnt;
111
+
112
+  cnt = USB_ReadEP(0x00, EP0Data.pData);
113
+  EP0Data.pData += cnt;
114
+  EP0Data.Count -= cnt;
115
+}
116
+
117
+/*
118
+ *  USB Request - Status In Stage
119
+ *    Parameters:      None
120
+ *    Return Value:    None
121
+ */
122
+
123
+void USB_StatusInStage(void) {
124
+  USB_WriteEP(0x80, nullptr, 0);
125
+}
126
+
127
+/*
128
+ *  USB Request - Status Out Stage
129
+ *    Parameters:      None
130
+ *    Return Value:    None
131
+ */
132
+
133
+void USB_StatusOutStage(void) {
134
+  USB_ReadEP(0x00, EP0Buf);
135
+}
136
+
137
+/*
138
+ *  Get Status USB Request
139
+ *    Parameters:      None (global SetupPacket)
140
+ *    Return Value:    TRUE - Success, FALSE - Error
141
+ */
142
+
143
+__inline uint32_t USB_ReqGetStatus(void) {
144
+  uint32_t n, m;
145
+
146
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
147
+  case REQUEST_TO_DEVICE:
148
+    EP0Data.pData = (uint8_t *) &USB_DeviceStatus;
149
+    break;
150
+  case REQUEST_TO_INTERFACE:
151
+    if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
152
+      *((__packed uint16_t *) EP0Buf) = 0;
153
+      *((uint16_t *) EP0Buf) = 0;
154
+      EP0Data.pData = EP0Buf;
155
+    } else {
156
+      return (FALSE);
157
+    }
158
+    break;
159
+  case REQUEST_TO_ENDPOINT:
160
+    n = SetupPacket.wIndex.WB.L & 0x8F;
161
+    m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
162
+    if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
163
+      *((__packed uint16_t *) EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
164
+      *((uint16_t *) EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
165
+      EP0Data.pData = EP0Buf;
166
+    } else {
167
+      return (FALSE);
168
+    }
169
+    break;
170
+  default:
171
+    return (FALSE);
172
+  }
173
+  return (TRUE);
174
+}
175
+
176
+/*
177
+ *  Set/Clear Feature USB Request
178
+ *    Parameters:      sc:    0 - Clear, 1 - Set
179
+ *                            (global SetupPacket)
180
+ *    Return Value:    TRUE - Success, FALSE - Error
181
+ */
182
+
183
+__inline uint32_t USB_ReqSetClrFeature(uint32_t sc) {
184
+  uint32_t n, m;
185
+
186
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
187
+  case REQUEST_TO_DEVICE:
188
+    if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
189
+      if (sc) {
190
+        USB_WakeUpCfg(TRUE);
191
+        USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
192
+      } else {
193
+        USB_WakeUpCfg(FALSE);
194
+        USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
195
+      }
196
+    } else {
197
+      return (FALSE);
198
+    }
199
+    break;
200
+  case REQUEST_TO_INTERFACE:
201
+    return (FALSE);
202
+  case REQUEST_TO_ENDPOINT:
203
+    n = SetupPacket.wIndex.WB.L & 0x8F;
204
+    m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
205
+    if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
206
+      if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
207
+        if (sc) {
208
+          USB_SetStallEP(n);
209
+          USB_EndPointHalt |= m;
210
+        } else {
211
+          if ((USB_EndPointStall & m) != 0) {
212
+            return (TRUE);
213
+          }
214
+          USB_ClrStallEP(n);
215
+#if (USB_MSC)
216
+          if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
217
+            /* Compliance Test: rewrite CSW after unstall */
218
+            if (CSW.dSignature == MSC_CSW_Signature) {
219
+              USB_WriteEP(MSC_EP_IN, (uint8_t *) &CSW, sizeof(CSW));
220
+            }
221
+          }
222
+#endif
223
+          USB_EndPointHalt &= ~m;
224
+        }
225
+      } else {
226
+        return (FALSE);
227
+      }
228
+    } else {
229
+      return (FALSE);
230
+    }
231
+    break;
232
+  default:
233
+    return (FALSE);
234
+  }
235
+  return (TRUE);
236
+}
237
+
238
+/*
239
+ *  Set Address USB Request
240
+ *    Parameters:      None (global SetupPacket)
241
+ *    Return Value:    TRUE - Success, FALSE - Error
242
+ */
243
+
244
+__inline uint32_t USB_ReqSetAddress(void) {
245
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
246
+  case REQUEST_TO_DEVICE:
247
+    USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
248
+    break;
249
+  default:
250
+    return (FALSE);
251
+  }
252
+  return (TRUE);
253
+}
254
+
255
+/*
256
+ *  Get Descriptor USB Request
257
+ *    Parameters:      None (global SetupPacket)
258
+ *    Return Value:    TRUE - Success, FALSE - Error
259
+ */
260
+
261
+__inline uint32_t USB_ReqGetDescriptor(void) {
262
+  uint8_t *pD;
263
+  uint32_t len, n;
264
+
265
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
266
+  case REQUEST_TO_DEVICE:
267
+    switch (SetupPacket.wValue.WB.H) {
268
+    case USB_DEVICE_DESCRIPTOR_TYPE:
269
+      EP0Data.pData = (uint8_t *) USB_DeviceDescriptor;
270
+      len = USB_DEVICE_DESC_SIZE;
271
+      break;
272
+    case USB_CONFIGURATION_DESCRIPTOR_TYPE:
273
+      pD = (uint8_t *) USB_ConfigDescriptor;
274
+      for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
275
+        if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bLength != 0) {
276
+          pD += ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
277
+        }
278
+      }
279
+      if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bLength == 0) {
280
+        return (FALSE);
281
+      }
282
+      EP0Data.pData = pD;
283
+      len = ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
284
+      break;
285
+    case USB_STRING_DESCRIPTOR_TYPE:
286
+      pD = (uint8_t *) USB_StringDescriptor;
287
+      for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
288
+        if (((USB_STRING_DESCRIPTOR *) pD)->bLength != 0) {
289
+          pD += ((USB_STRING_DESCRIPTOR *) pD)->bLength;
290
+        }
291
+      }
292
+      if (((USB_STRING_DESCRIPTOR *) pD)->bLength == 0) {
293
+        return (FALSE);
294
+      }
295
+      EP0Data.pData = pD;
296
+      len = ((USB_STRING_DESCRIPTOR *) EP0Data.pData)->bLength;
297
+      break;
298
+    default:
299
+      return (FALSE);
300
+    }
301
+    break;
302
+  case REQUEST_TO_INTERFACE:
303
+    switch (SetupPacket.wValue.WB.H) {
304
+    default:
305
+      return (FALSE);
306
+    }
307
+//      break;
308
+  default:
309
+    return (FALSE);
310
+  }
311
+
312
+  if (EP0Data.Count > len) {
313
+    EP0Data.Count = len;
314
+  }
315
+
316
+  return (TRUE);
317
+}
318
+
319
+/*
320
+ *  Get Configuration USB Request
321
+ *    Parameters:      None (global SetupPacket)
322
+ *    Return Value:    TRUE - Success, FALSE - Error
323
+ */
324
+
325
+__inline uint32_t USB_ReqGetConfiguration(void) {
326
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
327
+  case REQUEST_TO_DEVICE:
328
+    EP0Data.pData = &USB_Configuration;
329
+    break;
330
+  default:
331
+    return (FALSE);
332
+  }
333
+  return (TRUE);
334
+}
335
+
336
+/*
337
+ *  Set Configuration USB Request
338
+ *    Parameters:      None (global SetupPacket)
339
+ *    Return Value:    TRUE - Success, FALSE - Error
340
+ */
341
+
342
+__inline uint32_t USB_ReqSetConfiguration(void) {
343
+  USB_COMMON_DESCRIPTOR *pD;
344
+  uint32_t alt = 0;
345
+  uint32_t n, m;
346
+  uint32_t tmp;
347
+
348
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
349
+  case REQUEST_TO_DEVICE:
350
+
351
+    if (SetupPacket.wValue.WB.L) {
352
+      pD = (USB_COMMON_DESCRIPTOR *) USB_ConfigDescriptor;
353
+      while (pD->bLength) {
354
+        switch (pD->bDescriptorType) {
355
+        case USB_CONFIGURATION_DESCRIPTOR_TYPE:
356
+          if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
357
+            USB_Configuration = SetupPacket.wValue.WB.L;
358
+            USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *) pD)->bNumInterfaces;
359
+            for (n = 0; n < USB_IF_NUM; n++) {
360
+              USB_AltSetting[n] = 0;
361
+            }
362
+            for (n = 1; n < 16; n++) {
363
+              if (USB_EndPointMask & (1 << n)) {
364
+                USB_DisableEP(n);
365
+              }
366
+              if (USB_EndPointMask & ((1 << 16) << n)) {
367
+                USB_DisableEP(n | 0x80);
368
+              }
369
+            }
370
+            USB_EndPointMask = 0x00010001;
371
+            USB_EndPointHalt = 0x00000000;
372
+            USB_EndPointStall = 0x00000000;
373
+            USB_Configure(TRUE);
374
+            if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
375
+              USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
376
+            } else {
377
+              USB_DeviceStatus &= ~(USB_GETSTATUS_SELF_POWERED);
378
+            }
379
+          } else {
380
+//                (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
381
+            tmp = (uint32_t) pD;
382
+            tmp += ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
383
+            pD = (USB_COMMON_DESCRIPTOR *) tmp;
384
+            continue;
385
+          }
386
+          break;
387
+        case USB_INTERFACE_DESCRIPTOR_TYPE:
388
+          alt = ((USB_INTERFACE_DESCRIPTOR *) pD)->bAlternateSetting;
389
+          break;
390
+        case USB_ENDPOINT_DESCRIPTOR_TYPE:
391
+          if (alt == 0) {
392
+            n = ((USB_ENDPOINT_DESCRIPTOR *) pD)->bEndpointAddress & 0x8F;
393
+            m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
394
+            USB_EndPointMask |= m;
395
+            USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *) pD);
396
+            USB_EnableEP(n);
397
+            USB_ResetEP(n);
398
+          }
399
+          break;
400
+        }
401
+//          (uint8_t *)pD += pD->bLength;
402
+        tmp = (uint32_t) pD;
403
+        tmp += pD->bLength;
404
+        pD = (USB_COMMON_DESCRIPTOR *) tmp;
405
+      }
406
+    } else {
407
+      USB_Configuration = 0;
408
+      for (n = 1; n < 16; n++) {
409
+        if (USB_EndPointMask & (1 << n)) {
410
+          USB_DisableEP(n);
411
+        }
412
+        if (USB_EndPointMask & ((1 << 16) << n)) {
413
+          USB_DisableEP(n | 0x80);
414
+        }
415
+      }
416
+      USB_EndPointMask = 0x00010001;
417
+      USB_EndPointHalt = 0x00000000;
418
+      USB_EndPointStall = 0x00000000;
419
+      USB_Configure(FALSE);
420
+    }
421
+
422
+    if (USB_Configuration != SetupPacket.wValue.WB.L) {
423
+      return (FALSE);
424
+    }
425
+    break;
426
+  default:
427
+    return (FALSE);
428
+  }
429
+  return (TRUE);
430
+}
431
+
432
+/*
433
+ *  Get Interface USB Request
434
+ *    Parameters:      None (global SetupPacket)
435
+ *    Return Value:    TRUE - Success, FALSE - Error
436
+ */
437
+
438
+__inline uint32_t USB_ReqGetInterface(void) {
439
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
440
+  case REQUEST_TO_INTERFACE:
441
+    if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
442
+      EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
443
+    } else {
444
+      return (FALSE);
445
+    }
446
+    break;
447
+  default:
448
+    return (FALSE);
449
+  }
450
+  return (TRUE);
451
+}
452
+
453
+/*
454
+ *  Set Interface USB Request
455
+ *    Parameters:      None (global SetupPacket)
456
+ *    Return Value:    TRUE - Success, FALSE - Error
457
+ */
458
+__inline uint32_t USB_ReqSetInterface(void) {
459
+  USB_COMMON_DESCRIPTOR *pD;
460
+  uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
461
+  uint32_t n, m;
462
+  uint32_t set;
463
+  uint32_t tmp;
464
+
465
+  switch (SetupPacket.bmRequestType.BM.Recipient) {
466
+  case REQUEST_TO_INTERFACE:
467
+    if (USB_Configuration == 0)
468
+      return (FALSE);
469
+    set = FALSE;
470
+    pD = (USB_COMMON_DESCRIPTOR *) USB_ConfigDescriptor;
471
+    while (pD->bLength) {
472
+      switch (pD->bDescriptorType) {
473
+      case USB_CONFIGURATION_DESCRIPTOR_TYPE:
474
+        if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bConfigurationValue != USB_Configuration) {
475
+//              (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
476
+          tmp = (uint32_t) pD;
477
+          tmp += ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
478
+          pD = (USB_COMMON_DESCRIPTOR *) tmp;
479
+
480
+          continue;
481
+        }
482
+        break;
483
+      case USB_INTERFACE_DESCRIPTOR_TYPE:
484
+        ifn = ((USB_INTERFACE_DESCRIPTOR *) pD)->bInterfaceNumber;
485
+        alt = ((USB_INTERFACE_DESCRIPTOR *) pD)->bAlternateSetting;
486
+        msk = 0;
487
+        if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
488
+          set = TRUE;
489
+          old = USB_AltSetting[ifn];
490
+          USB_AltSetting[ifn] = (uint8_t) alt;
491
+        }
492
+        break;
493
+      case USB_ENDPOINT_DESCRIPTOR_TYPE:
494
+        if (ifn == SetupPacket.wIndex.WB.L) {
495
+          n = ((USB_ENDPOINT_DESCRIPTOR *) pD)->bEndpointAddress & 0x8F;
496
+          m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
497
+          if (alt == SetupPacket.wValue.WB.L) {
498
+            USB_EndPointMask |= m;
499
+            USB_EndPointHalt &= ~m;
500
+            USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *) pD);
501
+            USB_EnableEP(n);
502
+            USB_ResetEP(n);
503
+            msk |= m;
504
+          } else if ((alt == old) && ((msk & m) == 0)) {
505
+            USB_EndPointMask &= ~m;
506
+            USB_EndPointHalt &= ~m;
507
+            USB_DisableEP(n);
508
+          }
509
+        }
510
+        break;
511
+      }
512
+//        (uint8_t *)pD += pD->bLength;
513
+      tmp = (uint32_t) pD;
514
+      tmp += pD->bLength;
515
+      pD = (USB_COMMON_DESCRIPTOR *) tmp;
516
+    }
517
+    break;
518
+  default:
519
+    return (FALSE);
520
+  }
521
+
522
+  return (set);
523
+}
524
+
525
+/*
526
+ *  USB Endpoint 0 Event Callback
527
+ *    Parameters:      event
528
+ *    Return Value:    none
529
+ */
530
+
531
+void USB_EndPoint0(uint32_t event) {
532
+
533
+  switch (event) {
534
+  case USB_EVT_SETUP:
535
+    USB_SetupStage();
536
+    USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
537
+    EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
538
+    switch (SetupPacket.bmRequestType.BM.Type) {
539
+
540
+    case REQUEST_STANDARD:
541
+      switch (SetupPacket.bRequest) {
542
+      case USB_REQUEST_GET_STATUS:
543
+        if (!USB_ReqGetStatus()) {
544
+          goto stall_i;
545
+        }
546
+        USB_DataInStage();
547
+        break;
548
+
549
+      case USB_REQUEST_CLEAR_FEATURE:
550
+        if (!USB_ReqSetClrFeature(0)) {
551
+          goto stall_i;
552
+        }
553
+        USB_StatusInStage();
554
+#if USB_FEATURE_EVENT
555
+        USB_Feature_Event();
556
+#endif
557
+        break;
558
+
559
+      case USB_REQUEST_SET_FEATURE:
560
+        if (!USB_ReqSetClrFeature(1)) {
561
+          goto stall_i;
562
+        }
563
+        USB_StatusInStage();
564
+#if USB_FEATURE_EVENT
565
+        USB_Feature_Event();
566
+#endif
567
+        break;
568
+
569
+      case USB_REQUEST_SET_ADDRESS:
570
+        if (!USB_ReqSetAddress()) {
571
+          goto stall_i;
572
+        }
573
+        USB_StatusInStage();
574
+        break;
575
+
576
+      case USB_REQUEST_GET_DESCRIPTOR:
577
+        if (!USB_ReqGetDescriptor()) {
578
+          goto stall_i;
579
+        }
580
+        USB_DataInStage();
581
+        break;
582
+
583
+      case USB_REQUEST_SET_DESCRIPTOR:
584
+        USB_SetStallEP(0x00);
585
+        EP0Data.Count = 0;
586
+        break;
587
+
588
+      case USB_REQUEST_GET_CONFIGURATION:
589
+        if (!USB_ReqGetConfiguration()) {
590
+          goto stall_i;
591
+        }
592
+        USB_DataInStage();
593
+        break;
594
+
595
+      case USB_REQUEST_SET_CONFIGURATION:
596
+        if (!USB_ReqSetConfiguration()) {
597
+          goto stall_i;
598
+        }
599
+        USB_StatusInStage();
600
+#if USB_CONFIGURE_EVENT
601
+        USB_Configure_Event();
602
+#endif
603
+        break;
604
+
605
+      case USB_REQUEST_GET_INTERFACE:
606
+        if (!USB_ReqGetInterface()) {
607
+          goto stall_i;
608
+        }
609
+        USB_DataInStage();
610
+        break;
611
+
612
+      case USB_REQUEST_SET_INTERFACE:
613
+        if (!USB_ReqSetInterface()) {
614
+          goto stall_i;
615
+        }
616
+        USB_StatusInStage();
617
+#if USB_INTERFACE_EVENT
618
+        USB_Interface_Event();
619
+#endif
620
+        break;
621
+
622
+      default:
623
+        goto stall_i;
624
+      }
625
+      break; /* end case REQUEST_STANDARD */
626
+
627
+    case REQUEST_CLASS:
628
+      switch (SetupPacket.bmRequestType.BM.Recipient) {
629
+
630
+      case REQUEST_TO_DEVICE:
631
+        goto stall_i;
632
+        /* not supported */
633
+
634
+      case REQUEST_TO_INTERFACE:
635
+        if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */
636
+          switch (SetupPacket.bRequest) {
637
+          case MSC_REQUEST_RESET:
638
+            if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */
639
+            (SetupPacket.wLength == 0)) {
640
+              if (MSC_Reset()) {
641
+                USB_StatusInStage();
642
+                goto setup_class_ok;
643
+              }
644
+            }
645
+            break;
646
+          case MSC_REQUEST_GET_MAX_LUN:
647
+            if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
648
+            (SetupPacket.wLength == 1)) {
649
+              if (MSC_GetMaxLUN()) {
650
+                EP0Data.pData = EP0Buf;
651
+                USB_DataInStage();
652
+                goto setup_class_ok;
653
+              }
654
+            }
655
+            break;
656
+          }
657
+        }
658
+        if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
659
+        (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
660
+          switch (SetupPacket.bRequest) {
661
+          case CDC_SEND_ENCAPSULATED_COMMAND:
662
+            EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
663
+            goto setup_class_ok;
664
+          case CDC_GET_ENCAPSULATED_RESPONSE:
665
+            if (CDC_GetEncapsulatedResponse()) {
666
+              EP0Data.pData = EP0Buf; /* point to data to be sent */
667
+              USB_DataInStage(); /* send requested data */
668
+              goto setup_class_ok;
669
+            }
670
+            break;
671
+          case CDC_SET_COMM_FEATURE:
672
+            EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
673
+            goto setup_class_ok;
674
+          case CDC_GET_COMM_FEATURE:
675
+            if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
676
+              EP0Data.pData = EP0Buf; /* point to data to be sent */
677
+              USB_DataInStage(); /* send requested data */
678
+              goto setup_class_ok;
679
+            }
680
+            break;
681
+          case CDC_CLEAR_COMM_FEATURE:
682
+            if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
683
+              USB_StatusInStage(); /* send Acknowledge */
684
+              goto setup_class_ok;
685
+            }
686
+            break;
687
+          case CDC_SET_LINE_CODING:
688
+            EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
689
+            goto setup_class_ok;
690
+          case CDC_GET_LINE_CODING:
691
+            if (CDC_GetLineCoding()) {
692
+              EP0Data.pData = EP0Buf; /* point to data to be sent */
693
+              USB_DataInStage(); /* send requested data */
694
+              goto setup_class_ok;
695
+            }
696
+            break;
697
+          case CDC_SET_CONTROL_LINE_STATE:
698
+            if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
699
+              USB_StatusInStage(); /* send Acknowledge */
700
+              goto setup_class_ok;
701
+            }
702
+            break;
703
+          case CDC_SEND_BREAK:
704
+            if (CDC_SendBreak(SetupPacket.wValue.W)) {
705
+              USB_StatusInStage(); /* send Acknowledge */
706
+              goto setup_class_ok;
707
+            }
708
+            break;
709
+          }
710
+        }
711
+        goto stall_i;
712
+        /* not supported */
713
+        /* end case REQUEST_TO_INTERFACE */
714
+
715
+      case REQUEST_TO_ENDPOINT:
716
+        goto stall_i;
717
+        /* end case REQUEST_TO_ENDPOINT */
718
+
719
+      default:
720
+        goto stall_i;
721
+      }
722
+      setup_class_ok: /* request finished successfully */
723
+      break; /* end case REQUEST_CLASS */
724
+
725
+    default:
726
+      stall_i: USB_SetStallEP(0x80);
727
+      EP0Data.Count = 0;
728
+      break;
729
+    }
730
+    break; /* end case USB_EVT_SETUP */
731
+
732
+  case USB_EVT_OUT:
733
+    if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
734
+      if (EP0Data.Count) { /* still data to receive ? */
735
+        USB_DataOutStage(); /* receive data */
736
+        if (EP0Data.Count == 0) { /* data complete ? */
737
+          switch (SetupPacket.bmRequestType.BM.Type) {
738
+
739
+          case REQUEST_STANDARD:
740
+            goto stall_i;
741
+            /* not supported */
742
+
743
+          case REQUEST_CLASS:
744
+            switch (SetupPacket.bmRequestType.BM.Recipient) {
745
+            case REQUEST_TO_DEVICE:
746
+              goto stall_i;
747
+              /* not supported */
748
+
749
+            case REQUEST_TO_INTERFACE:
750
+              if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
751
+              (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
752
+                switch (SetupPacket.bRequest) {
753
+                case CDC_SEND_ENCAPSULATED_COMMAND:
754
+                  if (CDC_SendEncapsulatedCommand()) {
755
+                    USB_StatusInStage(); /* send Acknowledge */
756
+                    goto out_class_ok;
757
+                  }
758
+                  break;
759
+                case CDC_SET_COMM_FEATURE:
760
+                  if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
761
+                    USB_StatusInStage(); /* send Acknowledge */
762
+                    goto out_class_ok;
763
+                  }
764
+                  break;
765
+                case CDC_SET_LINE_CODING:
766
+                  if (CDC_SetLineCoding()) {
767
+                    USB_StatusInStage(); /* send Acknowledge */
768
+                    goto out_class_ok;
769
+                  }
770
+                  break;
771
+                }
772
+              }
773
+              goto stall_i;
774
+              /* end case REQUEST_TO_INTERFACE */
775
+
776
+            case REQUEST_TO_ENDPOINT:
777
+              goto stall_i;
778
+              /* end case REQUEST_TO_ENDPOINT */
779
+
780
+            default:
781
+              goto stall_i;
782
+            }
783
+            out_class_ok: /* request finished successfully */
784
+            break; /* end case REQUEST_CLASS */
785
+          default:
786
+            goto stall_i;
787
+          }
788
+        }
789
+      }
790
+    } else {
791
+      USB_StatusOutStage(); /* receive Acknowledge */
792
+    }
793
+    break; /* end case USB_EVT_OUT */
794
+
795
+  case USB_EVT_IN:
796
+    if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
797
+      USB_DataInStage(); /* send data */
798
+    } else {
799
+      if (USB_DeviceAddress & 0x80) {
800
+        USB_DeviceAddress &= 0x7F;
801
+        USB_SetAddress(USB_DeviceAddress);
802
+      }
803
+    }
804
+    break; /* end case USB_EVT_IN */
805
+
806
+  case USB_EVT_OUT_STALL:
807
+    USB_ClrStallEP(0x00);
808
+    break;
809
+
810
+  case USB_EVT_IN_STALL:
811
+    USB_ClrStallEP(0x80);
812
+    break;
813
+
814
+  }
815
+}

+ 52
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbcore.h Näytä tiedosto

@@ -0,0 +1,52 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbcore.h
5
+ * Purpose: USB Core Definitions
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC microcontroller devices only. Nothing else 
14
+ *      gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __USBCORE_H__
20
+#define __USBCORE_H__
21
+
22
+
23
+/* USB Endpoint Data Structure */
24
+typedef struct _USB_EP_DATA {
25
+  uint8_t  *pData;
26
+  uint16_t Count;
27
+} USB_EP_DATA;
28
+
29
+/* USB Core Global Variables */
30
+extern uint16_t USB_DeviceStatus;
31
+extern uint8_t  USB_DeviceAddress;
32
+extern uint8_t  USB_Configuration;
33
+extern uint32_t USB_EndPointMask;
34
+extern uint32_t USB_EndPointHalt;
35
+extern uint32_t USB_EndPointStall;
36
+extern uint8_t  USB_AltSetting[USB_IF_NUM];
37
+
38
+/* USB Endpoint 0 Buffer */
39
+extern uint8_t  EP0Buf[USB_MAX_PACKET0];
40
+
41
+/* USB Endpoint 0 Data Info */
42
+extern USB_EP_DATA EP0Data;
43
+
44
+/* USB Setup Packet */
45
+extern USB_SETUP_PACKET SetupPacket;
46
+
47
+/* USB Core Functions */
48
+extern void USB_ResetCore (void);
49
+
50
+
51
+
52
+#endif  /* __USBCORE_H__ */

+ 257
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbdesc.cpp Näytä tiedosto

@@ -0,0 +1,257 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbdesc.c
5
+ * Purpose: USB Descriptors
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC microcontroller devices only. Nothing else
14
+ *      gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *----------------------------------------------------------------------------
18
+ * History:
19
+ *          V1.20 Changed string descriptor handling
20
+ *          V1.00 Initial Version
21
+ *---------------------------------------------------------------------------*/
22
+extern "C" {
23
+#include "lpc_types.h"
24
+}
25
+
26
+#include "usb.h"
27
+#include "cdc.h"
28
+#include "msc.h"
29
+#include "usbcfg.h"
30
+#include "usbdesc.h"
31
+
32
+
33
+/* USB Standard Device Descriptor */
34
+const uint8_t USB_DeviceDescriptor[] = {
35
+  USB_DEVICE_DESC_SIZE,              /* bLength */
36
+  USB_DEVICE_DESCRIPTOR_TYPE,        /* bDescriptorType */
37
+  WBVAL(0x0200), /* 2.0 */           /* bcdUSB */
38
+  USB_DEVICE_CLASS_MISCELLANEOUS,    /* bDeviceClass Composite*/
39
+  0x02,                              /* bDeviceSubClass */
40
+  0x01,                              /* bDeviceProtocol */
41
+  USB_MAX_PACKET0,                   /* bMaxPacketSize0 */
42
+  WBVAL(0x1d50),                     /* idVendor */
43
+  WBVAL(0x6029),                     /* idProduct */
44
+  WBVAL(0x0100), /* 1.00 */          /* bcdDevice */
45
+  0x01,                              /* iManufacturer */
46
+  0x02,                              /* iProduct */
47
+  0x03,                              /* iSerialNumber */
48
+  0x01                               /* bNumConfigurations: one possible configuration*/
49
+};
50
+
51
+/* USB Configuration Descriptor */
52
+/*   All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
53
+const uint8_t USB_ConfigDescriptor[] = {
54
+/* Configuration 1 */
55
+  USB_CONFIGUARTION_DESC_SIZE,       /* bLength */
56
+  USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */
57
+  WBVAL(                             /* wTotalLength */
58
+    1*USB_CONFIGUARTION_DESC_SIZE +
59
+    // Interface Association Descriptor
60
+    8 +
61
+    //CDC Control Interface
62
+    1*USB_INTERFACE_DESC_SIZE     +  /* communication interface */
63
+    0x0013                        +  /* CDC functions */
64
+    1*USB_ENDPOINT_DESC_SIZE      +  /* interrupt endpoint */
65
+    //CDC Data Interface
66
+    1*USB_INTERFACE_DESC_SIZE     +  /* data interface */
67
+    2*USB_ENDPOINT_DESC_SIZE      +  /* bulk endpoints */
68
+    //MSC Interface
69
+    1*USB_INTERFACE_DESC_SIZE     +
70
+    2*USB_ENDPOINT_DESC_SIZE
71
+    ),
72
+  0x03,                              /* bNumInterfaces */
73
+  0x01,                              /* bConfigurationValue: 0x01 is used to select this configuration */
74
+  0x00,                              /* iConfiguration: no string to describe this configuration */
75
+  USB_CONFIG_BUS_POWERED,            /* bmAttributes */
76
+  USB_CONFIG_POWER_MA(500),          /* bMaxPower, device power consumption is 500 mA */
77
+
78
+
79
+/* Interface Association Descriptor */
80
+  0x08,
81
+  0x0B, //DescriptorType : Interface Association
82
+  0x00, //FirstInterface
83
+  0x02, //InterfaceCount
84
+  0x02, //FunctionClass
85
+  0x02, //FunctionSubClass
86
+  0x01, //FunctionProtocol
87
+  0x00, //Function
88
+
89
+/* Interface 0, Alternate Setting 0, Communication class interface descriptor */
90
+  USB_INTERFACE_DESC_SIZE,           /* bLength */
91
+  USB_INTERFACE_DESCRIPTOR_TYPE,     /* bDescriptorType */
92
+  USB_CDC_CIF_NUM,                   /* bInterfaceNumber: Number of Interface */
93
+  0x00,                              /* bAlternateSetting: Alternate setting */
94
+  0x01,                              /* bNumEndpoints: One endpoint used */
95
+  CDC_COMMUNICATION_INTERFACE_CLASS, /* bInterfaceClass: Communication Interface Class */
96
+  CDC_ABSTRACT_CONTROL_MODEL,        /* bInterfaceSubClass: Abstract Control Model */
97
+  0x00,                              /* bInterfaceProtocol: no protocol used */
98
+  0x00,                              /* iInterface: */
99
+
100
+/*Header Functional Descriptor*/
101
+  0x05,                              /* bLength: Endpoint Descriptor size */
102
+  CDC_CS_INTERFACE,                  /* bDescriptorType: CS_INTERFACE */
103
+  CDC_HEADER,                        /* bDescriptorSubtype: Header Func Desc */
104
+  WBVAL(CDC_V1_10), /* 1.10 */       /* bcdCDC */
105
+
106
+/*Call Management Functional Descriptor*/
107
+  0x05,                              /* bFunctionLength */
108
+  CDC_CS_INTERFACE,                  /* bDescriptorType: CS_INTERFACE */
109
+  CDC_CALL_MANAGEMENT,               /* bDescriptorSubtype: Call Management Func Desc */
110
+  CDC_CALLMGMT_CAP_CALLMGMT | CDC_CALLMGMT_CAP_DATAINTF,  /* bmCapabilities: device handles call management */
111
+  0x01,                              /* bDataInterface: CDC data IF ID */
112
+
113
+/*Abstract Control Management Functional Descriptor*/
114
+  0x04,                              /* bFunctionLength */
115
+  CDC_CS_INTERFACE,                  /* bDescriptorType: CS_INTERFACE */
116
+  CDC_ABSTRACT_CONTROL_MANAGEMENT,   /* bDescriptorSubtype: Abstract Control Management desc */
117
+  CDC_ACM_CAP_LINE | CDC_ACM_CAP_BRK,/* bmCapabilities: SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported */
118
+
119
+/*Union Functional Descriptor*/
120
+  0x05,                              /* bFunctionLength */
121
+  CDC_CS_INTERFACE,                  /* bDescriptorType: CS_INTERFACE */
122
+  CDC_UNION,                         /* bDescriptorSubtype: Union func desc */
123
+  USB_CDC_CIF_NUM,                   /* bMasterInterface: Communication class interface is master */
124
+  USB_CDC_DIF_NUM,                   /* bSlaveInterface0: Data class interface is slave 0 */
125
+
126
+/*Endpoint 1 Descriptor*/            /* event notification (optional) */
127
+  USB_ENDPOINT_DESC_SIZE,            /* bLength */
128
+  USB_ENDPOINT_DESCRIPTOR_TYPE,      /* bDescriptorType */
129
+  USB_ENDPOINT_IN(1),                /* bEndpointAddress */
130
+  USB_ENDPOINT_TYPE_INTERRUPT,       /* bmAttributes */
131
+  WBVAL(0x0010),                     /* wMaxPacketSize */
132
+  0x10,                              /* bInterval */
133
+
134
+
135
+/* Interface 1, Alternate Setting 0, Data class interface descriptor*/
136
+  USB_INTERFACE_DESC_SIZE,           /* bLength */
137
+  USB_INTERFACE_DESCRIPTOR_TYPE,     /* bDescriptorType */
138
+  USB_CDC_DIF_NUM,                   /* bInterfaceNumber: Number of Interface */
139
+  0x00,                              /* bAlternateSetting: no alternate setting */
140
+  0x02,                              /* bNumEndpoints: two endpoints used */
141
+  CDC_DATA_INTERFACE_CLASS,          /* bInterfaceClass: Data Interface Class */
142
+  0x00,                              /* bInterfaceSubClass: no subclass available */
143
+  0x00,                              /* bInterfaceProtocol: no protocol used */
144
+  0x00,                              /* iInterface: */
145
+/* Endpoint, EP2 Bulk Out */
146
+  USB_ENDPOINT_DESC_SIZE,            /* bLength */
147
+  USB_ENDPOINT_DESCRIPTOR_TYPE,      /* bDescriptorType */
148
+  USB_ENDPOINT_OUT(2),               /* bEndpointAddress */
149
+  USB_ENDPOINT_TYPE_BULK,            /* bmAttributes */
150
+  WBVAL(USB_CDC_BUFSIZE),            /* wMaxPacketSize */
151
+  0x00,                              /* bInterval: ignore for Bulk transfer */
152
+/* Endpoint, EP2 Bulk In */
153
+  USB_ENDPOINT_DESC_SIZE,            /* bLength */
154
+  USB_ENDPOINT_DESCRIPTOR_TYPE,      /* bDescriptorType */
155
+  USB_ENDPOINT_IN(2),                /* bEndpointAddress */
156
+  USB_ENDPOINT_TYPE_BULK,            /* bmAttributes */
157
+  WBVAL(USB_CDC_BUFSIZE),            /* wMaxPacketSize */
158
+  0x00,                              /* bInterval: ignore for Bulk transfer */
159
+
160
+
161
+  /* MSC Interface */
162
+  /* Interface 2, Alternate Setting 0, Data class interface descriptor*/
163
+  USB_INTERFACE_DESC_SIZE,           /* bLength */
164
+  USB_INTERFACE_DESCRIPTOR_TYPE,     /* bDescriptorType */
165
+  0x02,                              /* bInterfaceNumber */
166
+  0x00,                              /* bAlternateSetting */
167
+  0x02,                              /* bNumEndpoints */
168
+  USB_DEVICE_CLASS_STORAGE,          /* bInterfaceClass */
169
+  MSC_SUBCLASS_SCSI,                 /* bInterfaceSubClass */
170
+  MSC_PROTOCOL_BULK_ONLY,            /* bInterfaceProtocol */
171
+  0x00,                              /* iInterface */
172
+  /* Bulk In Endpoint */
173
+  USB_ENDPOINT_DESC_SIZE,            /* bLength */
174
+  USB_ENDPOINT_DESCRIPTOR_TYPE,      /* bDescriptorType */
175
+  USB_ENDPOINT_IN(5),                /* bEndpointAddress */
176
+  USB_ENDPOINT_TYPE_BULK,            /* bmAttributes */
177
+  WBVAL(0x0040),                     /* wMaxPacketSize */
178
+  0x00,                                 /* bInterval */
179
+  /* Bulk Out Endpoint */
180
+  USB_ENDPOINT_DESC_SIZE,            /* bLength */
181
+  USB_ENDPOINT_DESCRIPTOR_TYPE,      /* bDescriptorType */
182
+  USB_ENDPOINT_OUT(5),               /* bEndpointAddress */
183
+  USB_ENDPOINT_TYPE_BULK,            /* bmAttributes */
184
+  WBVAL(0x0040),                     /* wMaxPacketSize */
185
+  0,
186
+  /* Terminator */
187
+  0                                  /* bLength */
188
+};
189
+
190
+
191
+
192
+
193
+/* USB String Descriptor (optional) */
194
+const uint8_t USB_StringDescriptor[] = {
195
+/* Index 0x00: LANGID Codes */
196
+  0x04,                              /* bLength */
197
+  USB_STRING_DESCRIPTOR_TYPE,        /* bDescriptorType */
198
+  WBVAL(0x0409), /* US English */    /* wLANGID */
199
+/* Index 0x01: Manufacturer */
200
+  (13*2 + 2),                        /* bLength (13 Char + Type + length) */
201
+  USB_STRING_DESCRIPTOR_TYPE,        /* bDescriptorType */
202
+  'm',0,
203
+  'a',0,
204
+  'r',0,
205
+  'l',0,
206
+  'i',0,
207
+  'n',0,
208
+  'f',0,
209
+  'w',0,
210
+  '.',0,
211
+  'o',0,
212
+  'r',0,
213
+  'g',0,
214
+  ' ',0,
215
+/* Index 0x02: Product */
216
+  (17*2 + 2),                        /* bLength ( 17 Char + Type + length) */
217
+  USB_STRING_DESCRIPTOR_TYPE,        /* bDescriptorType */
218
+  'M',0,
219
+  'a',0,
220
+  'r',0,
221
+  'l',0,
222
+  'i',0,
223
+  'n',0,
224
+  ' ',0,
225
+  'U',0,
226
+  'S',0,
227
+  'B',0,
228
+  ' ',0,
229
+  'D',0,
230
+  'e',0,
231
+  'v',0,
232
+  'i',0,
233
+  'c',0,
234
+  'e',0,
235
+/* Index 0x03: Serial Number */
236
+  (12*2 + 2),                        /* bLength (12 Char + Type + length) */
237
+  USB_STRING_DESCRIPTOR_TYPE,        /* bDescriptorType */
238
+  '1',0,
239
+  '.',0,
240
+  '0',0,
241
+  '0',0,
242
+  '0',0,
243
+  '0',0,
244
+  '0',0,
245
+  '0',0,
246
+  '0',0,
247
+  '0',0,
248
+  '0',0,
249
+  '0',0,
250
+/* Index 0x04: Interface 0, Alternate Setting 0 */
251
+  ( 4*2 + 2),                        /* bLength (4 Char + Type + length) */
252
+  USB_STRING_DESCRIPTOR_TYPE,        /* bDescriptorType */
253
+  'V',0,
254
+  'C',0,
255
+  'O',0,
256
+  'M',0,
257
+};

+ 35
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbdesc.h Näytä tiedosto

@@ -0,0 +1,35 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbdesc.h
5
+ * Purpose: USB Descriptors Definitions
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC microcontroller devices only. Nothing else 
14
+ *      gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __USBDESC_H__
20
+#define __USBDESC_H__
21
+
22
+
23
+#define WBVAL(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
24
+
25
+#define USB_DEVICE_DESC_SIZE        (sizeof(USB_DEVICE_DESCRIPTOR))
26
+#define USB_CONFIGUARTION_DESC_SIZE (sizeof(USB_CONFIGURATION_DESCRIPTOR))
27
+#define USB_INTERFACE_DESC_SIZE     (sizeof(USB_INTERFACE_DESCRIPTOR))
28
+#define USB_ENDPOINT_DESC_SIZE      (sizeof(USB_ENDPOINT_DESCRIPTOR))
29
+
30
+extern const uint8_t USB_DeviceDescriptor[];
31
+extern const uint8_t USB_ConfigDescriptor[];
32
+extern const uint8_t USB_StringDescriptor[];
33
+
34
+
35
+#endif  /* __USBDESC_H__ */

+ 811
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbhw.cpp Näytä tiedosto

@@ -0,0 +1,811 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbhw.c
5
+ * Purpose: USB Hardware Layer Module for NXP's LPC17xx MCU
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *----------------------------------------------------------------------------
18
+ * History:
19
+ *          V1.20 Added USB_ClearEPBuf
20
+ *          V1.00 Initial Version
21
+ *----------------------------------------------------------------------------*/
22
+extern "C" {
23
+#include "LPC17xx.h"                        /* LPC17xx definitions */
24
+}
25
+
26
+#include "usb.h"
27
+#include "usbcfg.h"
28
+#include "usbreg.h"
29
+#include "usbhw.h"
30
+#include "usbcore.h"
31
+#include "usbuser.h"
32
+
33
+#define EP_MSK_CTRL 0x0001      /* Control Endpoint Logical Address Mask */
34
+#define EP_MSK_BULK 0xC924      /* Bulk Endpoint Logical Address Mask */
35
+#define EP_MSK_INT  0x4492      /* Interrupt Endpoint Logical Address Mask */
36
+#define EP_MSK_ISO  0x1248      /* Isochronous Endpoint Logical Address Mask */
37
+
38
+#if USB_DMA
39
+uint32_t UDCA[USB_EP_NUM] __attribute__((section("USB_RAM")));        /* UDCA in USB RAM */
40
+uint32_t DD_NISO_Mem[4*DD_NISO_CNT] __attribute__((section("USB_RAM")));    /* Non-Iso DMA Descriptor Memory */
41
+uint32_t DD_ISO_Mem [5*DD_ISO_CNT] __attribute__((section("USB_RAM")));     /* Iso DMA Descriptor Memory */
42
+uint32_t udca[USB_EP_NUM];                                    /* UDCA saved values */
43
+uint32_t DDMemMap[2];
44
+#endif
45
+
46
+
47
+/*
48
+ *  Get Endpoint Physical Address
49
+ *    Parameters:      EPNum: Endpoint Number
50
+ *                       EPNum.0..3: Address
51
+ *                       EPNum.7:    Dir
52
+ *    Return Value:    Endpoint Physical Address
53
+ */
54
+
55
+uint32_t EPAdr (uint32_t EPNum) {
56
+  uint32_t val;
57
+
58
+  val = (EPNum & 0x0F) << 1;
59
+  if (EPNum & 0x80) {
60
+    val += 1;
61
+  }
62
+  return (val);
63
+}
64
+
65
+
66
+/*
67
+ *  Write Command
68
+ *    Parameters:      cmd:   Command
69
+ *    Return Value:    None
70
+ */
71
+
72
+void WrCmd (uint32_t cmd) {
73
+
74
+  LPC_USB->USBDevIntClr = CCEMTY_INT;
75
+  LPC_USB->USBCmdCode = cmd;
76
+  while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
77
+}
78
+
79
+
80
+/*
81
+ *  Write Command Data
82
+ *    Parameters:      cmd:   Command
83
+ *                     val:   Data
84
+ *    Return Value:    None
85
+ */
86
+
87
+void WrCmdDat (uint32_t cmd, uint32_t val) {
88
+
89
+  LPC_USB->USBDevIntClr = CCEMTY_INT;
90
+  LPC_USB->USBCmdCode = cmd;
91
+  while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
92
+  LPC_USB->USBDevIntClr = CCEMTY_INT;
93
+  LPC_USB->USBCmdCode = val;
94
+  while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
95
+}
96
+
97
+
98
+/*
99
+ *  Write Command to Endpoint
100
+ *    Parameters:      cmd:   Command
101
+ *                     val:   Data
102
+ *    Return Value:    None
103
+ */
104
+
105
+void WrCmdEP (uint32_t EPNum, uint32_t cmd){
106
+
107
+  LPC_USB->USBDevIntClr = CCEMTY_INT;
108
+  LPC_USB->USBCmdCode = CMD_SEL_EP(EPAdr(EPNum));
109
+  while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
110
+  LPC_USB->USBDevIntClr = CCEMTY_INT;
111
+  LPC_USB->USBCmdCode = cmd;
112
+  while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
113
+}
114
+
115
+
116
+/*
117
+ *  Read Command Data
118
+ *    Parameters:      cmd:   Command
119
+ *    Return Value:    Data Value
120
+ */
121
+
122
+uint32_t RdCmdDat (uint32_t cmd) {
123
+
124
+  LPC_USB->USBDevIntClr = CCEMTY_INT | CDFULL_INT;
125
+  LPC_USB->USBCmdCode = cmd;
126
+  while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
127
+  return (LPC_USB->USBCmdData);
128
+}
129
+
130
+
131
+/*
132
+ *  USB Initialize Function
133
+ *   Called by the User to initialize USB
134
+ *    Return Value:    None
135
+ */
136
+
137
+void USB_Init (void) {
138
+
139
+  LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28));   /* P0.29 D+, P0.30 D- */
140
+  LPC_PINCON->PINSEL1 |=  ((1<<26)|(1<<28));   /* PINSEL1 26.27, 28.29  = 01 */
141
+
142
+  //todo: VBUS not used by smoothieboard (though spec requires it for self powered devices), pin used for beeper
143
+  //todo: Goodlink used for servo4?
144
+  //LPC_PINCON->PINSEL3 &= ~((3<< 4)|(3<<28));   /* P1.18 GoodLink, P1.30 VBUS */
145
+  //LPC_PINCON->PINSEL3 |=  ((1<< 4)|(2<<28));   /* PINSEL3 4.5 = 01, 28.29 = 10 */
146
+
147
+  LPC_PINCON->PINSEL4 &= ~((3<<18)        );   /* P2.9 SoftConnect */
148
+  LPC_PINCON->PINSEL4 |=  ((1<<18)        );   /* PINSEL4 18.19 = 01 */
149
+
150
+  LPC_SC->PCONP |= (1UL<<31);                /* USB PCLK -> enable USB Per.       */
151
+
152
+  LPC_USB->USBClkCtrl = 0x1A;                /* Dev, PortSel, AHB clock enable */
153
+  while ((LPC_USB->USBClkSt & 0x1A) != 0x1A);
154
+
155
+  NVIC_EnableIRQ(USB_IRQn);               /* enable USB interrupt */
156
+
157
+  USB_Reset();
158
+  USB_SetAddress(0);
159
+}
160
+
161
+
162
+/*
163
+ *  USB Connect Function
164
+ *   Called by the User to Connect/Disconnect USB
165
+ *    Parameters:      con:   Connect/Disconnect
166
+ *    Return Value:    None
167
+ */
168
+
169
+void USB_Connect (uint32_t con) {
170
+  WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0));
171
+}
172
+
173
+
174
+/*
175
+ *  USB Reset Function
176
+ *   Called automatically on USB Reset
177
+ *    Return Value:    None
178
+ */
179
+
180
+void USB_Reset (void) {
181
+#if USB_DMA
182
+  uint32_t n;
183
+#endif
184
+
185
+  LPC_USB->USBEpInd = 0;
186
+  LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
187
+  LPC_USB->USBEpInd = 1;
188
+  LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
189
+  while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
190
+
191
+  LPC_USB->USBEpIntClr  = 0xFFFFFFFF;
192
+  LPC_USB->USBEpIntEn   = 0xFFFFFFFF ^ USB_DMA_EP;
193
+  LPC_USB->USBDevIntClr = 0xFFFFFFFF;
194
+  LPC_USB->USBDevIntEn  = DEV_STAT_INT    | EP_SLOW_INT    |
195
+               (USB_SOF_EVENT   ? FRAME_INT : 0) |
196
+               (USB_ERROR_EVENT ? ERR_INT   : 0);
197
+
198
+  WrCmdDat(CMD_SET_MODE, DAT_WR_BYTE(INAK_BI));
199
+
200
+
201
+#if USB_DMA
202
+  LPC_USB->USBUDCAH   = USB_RAM_ADR;
203
+  LPC_USB->USBDMARClr = 0xFFFFFFFF;
204
+  LPC_USB->USBEpDMADis  = 0xFFFFFFFF;
205
+  LPC_USB->USBEpDMAEn   = USB_DMA_EP;
206
+  LPC_USB->USBEoTIntClr = 0xFFFFFFFF;
207
+  LPC_USB->USBNDDRIntClr = 0xFFFFFFFF;
208
+  LPC_USB->USBSysErrIntClr = 0xFFFFFFFF;
209
+  LPC_USB->USBDMAIntEn  = 0x00000007;
210
+  DDMemMap[0] = 0x00000000;
211
+  DDMemMap[1] = 0x00000000;
212
+  for (n = 0; n < USB_EP_NUM; n++) {
213
+    udca[n] = 0;
214
+    UDCA[n] = 0;
215
+  }
216
+#endif
217
+}
218
+
219
+
220
+/*
221
+ *  USB Suspend Function
222
+ *   Called automatically on USB Suspend
223
+ *    Return Value:    None
224
+ */
225
+
226
+void USB_Suspend (void) {
227
+  /* Performed by Hardware */
228
+}
229
+
230
+
231
+/*
232
+ *  USB Resume Function
233
+ *   Called automatically on USB Resume
234
+ *    Return Value:    None
235
+ */
236
+
237
+void USB_Resume (void) {
238
+  /* Performed by Hardware */
239
+}
240
+
241
+
242
+/*
243
+ *  USB Remote Wakeup Function
244
+ *   Called automatically on USB Remote Wakeup
245
+ *    Return Value:    None
246
+ */
247
+
248
+void USB_WakeUp (void) {
249
+
250
+  if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) {
251
+    WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
252
+  }
253
+}
254
+
255
+
256
+/*
257
+ *  USB Remote Wakeup Configuration Function
258
+ *    Parameters:      cfg:   Enable/Disable
259
+ *    Return Value:    None
260
+ */
261
+
262
+void USB_WakeUpCfg (uint32_t cfg) {
263
+  /* Not needed */
264
+}
265
+
266
+
267
+/*
268
+ *  USB Set Address Function
269
+ *    Parameters:      adr:   USB Address
270
+ *    Return Value:    None
271
+ */
272
+
273
+void USB_SetAddress (uint32_t adr) {
274
+  WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */
275
+  WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /*  Setup Status Phase */
276
+}
277
+
278
+
279
+/*
280
+ *  USB Configure Function
281
+ *    Parameters:      cfg:   Configure/Deconfigure
282
+ *    Return Value:    None
283
+ */
284
+
285
+void USB_Configure (uint32_t cfg) {
286
+
287
+  WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0));
288
+
289
+  LPC_USB->USBReEp = 0x00000003;
290
+  while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
291
+  LPC_USB->USBDevIntClr = EP_RLZED_INT;
292
+}
293
+
294
+
295
+/*
296
+ *  Configure USB Endpoint according to Descriptor
297
+ *    Parameters:      pEPD:  Pointer to Endpoint Descriptor
298
+ *    Return Value:    None
299
+ */
300
+
301
+void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) {
302
+  uint32_t num;
303
+
304
+  num = EPAdr(pEPD->bEndpointAddress);
305
+  LPC_USB->USBReEp |= (1 << num);
306
+  LPC_USB->USBEpInd = num;
307
+  LPC_USB->USBMaxPSize = pEPD->wMaxPacketSize;
308
+  while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
309
+  LPC_USB->USBDevIntClr = EP_RLZED_INT;
310
+}
311
+
312
+
313
+/*
314
+ *  Set Direction for USB Control Endpoint
315
+ *    Parameters:      dir:   Out (dir == 0), In (dir <> 0)
316
+ *    Return Value:    None
317
+ */
318
+
319
+void USB_DirCtrlEP (uint32_t dir) {
320
+  /* Not needed */
321
+}
322
+
323
+
324
+/*
325
+ *  Enable USB Endpoint
326
+ *    Parameters:      EPNum: Endpoint Number
327
+ *                       EPNum.0..3: Address
328
+ *                       EPNum.7:    Dir
329
+ *    Return Value:    None
330
+ */
331
+
332
+void USB_EnableEP (uint32_t EPNum) {
333
+  WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
334
+}
335
+
336
+
337
+/*
338
+ *  Disable USB Endpoint
339
+ *    Parameters:      EPNum: Endpoint Number
340
+ *                       EPNum.0..3: Address
341
+ *                       EPNum.7:    Dir
342
+ *    Return Value:    None
343
+ */
344
+
345
+void USB_DisableEP (uint32_t EPNum) {
346
+  WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA));
347
+}
348
+
349
+
350
+/*
351
+ *  Reset USB Endpoint
352
+ *    Parameters:      EPNum: Endpoint Number
353
+ *                       EPNum.0..3: Address
354
+ *                       EPNum.7:    Dir
355
+ *    Return Value:    None
356
+ */
357
+
358
+void USB_ResetEP (uint32_t EPNum) {
359
+  WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
360
+}
361
+
362
+
363
+/*
364
+ *  Set Stall for USB Endpoint
365
+ *    Parameters:      EPNum: Endpoint Number
366
+ *                       EPNum.0..3: Address
367
+ *                       EPNum.7:    Dir
368
+ *    Return Value:    None
369
+ */
370
+
371
+void USB_SetStallEP (uint32_t EPNum) {
372
+  WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST));
373
+}
374
+
375
+
376
+/*
377
+ *  Clear Stall for USB Endpoint
378
+ *    Parameters:      EPNum: Endpoint Number
379
+ *                       EPNum.0..3: Address
380
+ *                       EPNum.7:    Dir
381
+ *    Return Value:    None
382
+ */
383
+
384
+void USB_ClrStallEP (uint32_t EPNum) {
385
+  WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
386
+}
387
+
388
+
389
+/*
390
+ *  Clear USB Endpoint Buffer
391
+ *    Parameters:      EPNum: Endpoint Number
392
+ *                       EPNum.0..3: Address
393
+ *                       EPNum.7:    Dir
394
+ *    Return Value:    None
395
+ */
396
+
397
+void USB_ClearEPBuf (uint32_t EPNum) {
398
+  WrCmdEP(EPNum, CMD_CLR_BUF);
399
+}
400
+
401
+
402
+/*
403
+ *  Read USB Endpoint Data
404
+ *    Parameters:      EPNum: Endpoint Number
405
+ *                       EPNum.0..3: Address
406
+ *                       EPNum.7:    Dir
407
+ *                     pData: Pointer to Data Buffer
408
+ *    Return Value:    Number of bytes read
409
+ */
410
+
411
+uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) {
412
+  uint32_t cnt, n;
413
+
414
+  LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN;
415
+
416
+  do {
417
+    cnt = LPC_USB->USBRxPLen;
418
+  } while ((cnt & PKT_RDY) == 0);
419
+  cnt &= PKT_LNGTH_MASK;
420
+
421
+  for (n = 0; n < (cnt + 3) / 4; n++) {
422
+    *((__packed uint32_t *)pData) = LPC_USB->USBRxData;
423
+    pData += 4;
424
+  }
425
+  LPC_USB->USBCtrl = 0;
426
+
427
+  if (((EP_MSK_ISO >> EPNum) & 1) == 0) {   /* Non-Isochronous Endpoint */
428
+    WrCmdEP(EPNum, CMD_CLR_BUF);
429
+  }
430
+
431
+  return (cnt);
432
+}
433
+
434
+
435
+/*
436
+ *  Write USB Endpoint Data
437
+ *    Parameters:      EPNum: Endpoint Number
438
+ *                       EPNum.0..3: Address
439
+ *                       EPNum.7:    Dir
440
+ *                     pData: Pointer to Data Buffer
441
+ *                     cnt:   Number of bytes to write
442
+ *    Return Value:    Number of bytes written
443
+ */
444
+
445
+uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) {
446
+  uint32_t n;
447
+
448
+  LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
449
+
450
+  LPC_USB->USBTxPLen = cnt;
451
+
452
+  for (n = 0; n < (cnt + 3) / 4; n++) {
453
+    LPC_USB->USBTxData = *((__packed uint32_t *)pData);
454
+    pData += 4;
455
+  }
456
+  LPC_USB->USBCtrl = 0;
457
+  WrCmdEP(EPNum, CMD_VALID_BUF);
458
+  return (cnt);
459
+}
460
+
461
+#if USB_DMA
462
+
463
+/* DMA Descriptor Memory Layout */
464
+const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR };
465
+const uint32_t DDSz [2] = { 16,          20         };
466
+
467
+
468
+/*
469
+ *  Setup USB DMA Transfer for selected Endpoint
470
+ *    Parameters:      EPNum: Endpoint Number
471
+ *                     pDD: Pointer to DMA Descriptor
472
+ *    Return Value:    TRUE - Success, FALSE - Error
473
+ */
474
+
475
+uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) {
476
+  uint32_t num, ptr, nxt, iso, n;
477
+
478
+  iso = pDD->Cfg.Type.IsoEP;                /* Iso or Non-Iso Descriptor */
479
+  num = EPAdr(EPNum);                       /* Endpoint's Physical Address */
480
+
481
+  ptr = 0;                                  /* Current Descriptor */
482
+  nxt = udca[num];                          /* Initial Descriptor */
483
+  while (nxt) {                             /* Go through Descriptor List */
484
+    ptr = nxt;                              /* Current Descriptor */
485
+    if (!pDD->Cfg.Type.Link) {              /* Check for Linked Descriptors */
486
+      n = (ptr - DDAdr[iso]) / DDSz[iso];   /* Descriptor Index */
487
+      DDMemMap[iso] &= ~(1 << n);           /* Unmark Memory Usage */
488
+    }
489
+    nxt = *((uint32_t *)ptr);                  /* Next Descriptor */
490
+  }
491
+
492
+  for (n = 0; n < 32; n++) {                /* Search for available Memory */
493
+    if ((DDMemMap[iso] & (1 << n)) == 0) {
494
+      break;                                /* Memory found */
495
+    }
496
+  }
497
+  if (n == 32) return (FALSE);              /* Memory not available */
498
+
499
+  DDMemMap[iso] |= 1 << n;                  /* Mark Memory Usage */
500
+  nxt = DDAdr[iso] + n * DDSz[iso];         /* Next Descriptor */
501
+
502
+  if (ptr && pDD->Cfg.Type.Link) {
503
+    *((uint32_t *)(ptr + 0))  = nxt;           /* Link in new Descriptor */
504
+    *((uint32_t *)(ptr + 4)) |= 0x00000004;    /* Next DD is Valid */
505
+  } else {
506
+    udca[num] = nxt;                        /* Save new Descriptor */
507
+    UDCA[num] = nxt;                        /* Update UDCA in USB */
508
+  }
509
+
510
+  uint32_t * nxt_ptr = (uint32_t *)nxt;
511
+  /* Fill in DMA Descriptor */
512
+  *nxt_ptr++ =  0;   /* Next DD Pointer */
513
+  *nxt_ptr++ = (pDD->Cfg.Type.ATLE) |
514
+               (pDD->Cfg.Type.IsoEP << 4) |
515
+               (pDD->MaxSize <<  5) |
516
+               (pDD->BufLen  << 16);
517
+  *nxt_ptr++ =  pDD->BufAdr;
518
+  *nxt_ptr++ =  pDD->Cfg.Type.LenPos << 8;
519
+  if (iso) {
520
+    *nxt_ptr =  pDD->InfoAdr;
521
+  }
522
+
523
+  return (TRUE); /* Success */
524
+}
525
+
526
+
527
+/*
528
+ *  Enable USB DMA Endpoint
529
+ *    Parameters:      EPNum: Endpoint Number
530
+ *                       EPNum.0..3: Address
531
+ *                       EPNum.7:    Dir
532
+ *    Return Value:    None
533
+ */
534
+
535
+void USB_DMA_Enable (uint32_t EPNum) {
536
+  LPC_USB->USBEpDMAEn = 1 << EPAdr(EPNum);
537
+}
538
+
539
+
540
+/*
541
+ *  Disable USB DMA Endpoint
542
+ *    Parameters:      EPNum: Endpoint Number
543
+ *                       EPNum.0..3: Address
544
+ *                       EPNum.7:    Dir
545
+ *    Return Value:    None
546
+ */
547
+
548
+void USB_DMA_Disable (uint32_t EPNum) {
549
+  LPC_USB->USBEpDMADis = 1 << EPAdr(EPNum);
550
+}
551
+
552
+
553
+/*
554
+ *  Get USB DMA Endpoint Status
555
+ *    Parameters:      EPNum: Endpoint Number
556
+ *                       EPNum.0..3: Address
557
+ *                       EPNum.7:    Dir
558
+ *    Return Value:    DMA Status
559
+ */
560
+
561
+uint32_t USB_DMA_Status (uint32_t EPNum) {
562
+  uint32_t ptr, val;
563
+
564
+  ptr = UDCA[EPAdr(EPNum)];                 /* Current Descriptor */
565
+  if (ptr == 0)
566
+	return (USB_DMA_INVALID);
567
+
568
+  val = *((uint32_t *)(ptr + 3*4));            /* Status Information */
569
+  switch ((val >> 1) & 0x0F) {
570
+    case 0x00:                              /* Not serviced */
571
+      return (USB_DMA_IDLE);
572
+    case 0x01:                              /* Being serviced */
573
+      return (USB_DMA_BUSY);
574
+    case 0x02:                              /* Normal Completition */
575
+      return (USB_DMA_DONE);
576
+    case 0x03:                              /* Data Under Run */
577
+      return (USB_DMA_UNDER_RUN);
578
+    case 0x08:                              /* Data Over Run */
579
+      return (USB_DMA_OVER_RUN);
580
+    case 0x09:                              /* System Error */
581
+      return (USB_DMA_ERROR);
582
+  }
583
+
584
+  return (USB_DMA_UNKNOWN);
585
+}
586
+
587
+
588
+/*
589
+ *  Get USB DMA Endpoint Current Buffer Address
590
+ *    Parameters:      EPNum: Endpoint Number
591
+ *                       EPNum.0..3: Address
592
+ *                       EPNum.7:    Dir
593
+ *    Return Value:    DMA Address (or -1 when DMA is Invalid)
594
+ */
595
+
596
+uint32_t USB_DMA_BufAdr (uint32_t EPNum) {
597
+  uint32_t ptr, val;
598
+
599
+  ptr = UDCA[EPAdr(EPNum)];                 /* Current Descriptor */
600
+  if (ptr == 0)
601
+  {
602
+	return ((uint32_t)(-1));                /* DMA Invalid */
603
+  }
604
+
605
+  val = *((uint32_t *)(ptr + 2*4));         /* Buffer Address */
606
+  return (val);                             /* Current Address */
607
+}
608
+
609
+
610
+/*
611
+ *  Get USB DMA Endpoint Current Buffer Count
612
+ *   Number of transfered Bytes or Iso Packets
613
+ *    Parameters:      EPNum: Endpoint Number
614
+ *                       EPNum.0..3: Address
615
+ *                       EPNum.7:    Dir
616
+ *    Return Value:    DMA Count (or -1 when DMA is Invalid)
617
+ */
618
+
619
+uint32_t USB_DMA_BufCnt (uint32_t EPNum) {
620
+  uint32_t ptr, val;
621
+
622
+  ptr = UDCA[EPAdr(EPNum)];                 /* Current Descriptor */
623
+  if (ptr == 0)
624
+  {
625
+	return ((uint32_t)(-1));                /* DMA Invalid */
626
+  }
627
+  val = *((uint32_t *)(ptr + 3*4));         /* Status Information */
628
+  return (val >> 16);                       /* Current Count */
629
+}
630
+
631
+
632
+#endif /* USB_DMA */
633
+
634
+
635
+/*
636
+ *  Get USB Last Frame Number
637
+ *    Parameters:      None
638
+ *    Return Value:    Frame Number
639
+ */
640
+
641
+uint32_t USB_GetFrame (void) {
642
+  uint32_t val;
643
+
644
+  WrCmd(CMD_RD_FRAME);
645
+  val = RdCmdDat(DAT_RD_FRAME);
646
+  val = val | (RdCmdDat(DAT_RD_FRAME) << 8);
647
+
648
+  return (val);
649
+}
650
+
651
+
652
+/*
653
+ *  USB Interrupt Service Routine
654
+ */
655
+
656
+void USB_IRQHandler (void) {
657
+  uint32_t disr, val, n, m;
658
+  uint32_t episr, episrCur;
659
+
660
+  disr = LPC_USB->USBDevIntSt;       /* Device Interrupt Status */
661
+
662
+  /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */
663
+  if (disr & DEV_STAT_INT) {
664
+    LPC_USB->USBDevIntClr = DEV_STAT_INT;
665
+    WrCmd(CMD_GET_DEV_STAT);
666
+    val = RdCmdDat(DAT_GET_DEV_STAT);       /* Device Status */
667
+    if (val & DEV_RST) {                    /* Reset */
668
+      USB_Reset();
669
+#if   USB_RESET_EVENT
670
+      USB_Reset_Event();
671
+#endif
672
+    }
673
+    if (val & DEV_CON_CH) {                 /* Connect change */
674
+#if   USB_POWER_EVENT
675
+      USB_Power_Event(val & DEV_CON);
676
+#endif
677
+    }
678
+    if (val & DEV_SUS_CH) {                 /* Suspend/Resume */
679
+      if (val & DEV_SUS) {                  /* Suspend */
680
+        USB_Suspend();
681
+#if     USB_SUSPEND_EVENT
682
+        USB_Suspend_Event();
683
+#endif
684
+      } else {                              /* Resume */
685
+        USB_Resume();
686
+#if     USB_RESUME_EVENT
687
+        USB_Resume_Event();
688
+#endif
689
+      }
690
+    }
691
+    goto isr_end;
692
+  }
693
+
694
+#if USB_SOF_EVENT
695
+  /* Start of Frame Interrupt */
696
+  if (disr & FRAME_INT) {
697
+	LPC_USB->USBDevIntClr = FRAME_INT;
698
+    USB_SOF_Event();
699
+  }
700
+#endif
701
+
702
+#if USB_ERROR_EVENT
703
+  /* Error Interrupt */
704
+  if (disr & ERR_INT) {
705
+	LPC_USB->USBDevIntClr = ERR_INT;
706
+    WrCmd(CMD_RD_ERR_STAT);
707
+    val = RdCmdDat(DAT_RD_ERR_STAT);
708
+    USB_Error_Event(val);
709
+  }
710
+#endif
711
+
712
+  /* Endpoint's Slow Interrupt */
713
+  if (disr & EP_SLOW_INT) {
714
+    episrCur = 0;
715
+    episr    = LPC_USB->USBEpIntSt;
716
+    for (n = 0; n < USB_EP_NUM; n++) {      /* Check All Endpoints */
717
+      if (episr == episrCur) break;         /* break if all EP interrupts handled */
718
+      if (episr & (1 << n)) {
719
+        episrCur |= (1 << n);
720
+        m = n >> 1;
721
+
722
+        LPC_USB->USBEpIntClr = (1 << n);
723
+        while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
724
+        val = LPC_USB->USBCmdData;
725
+
726
+        if ((n & 1) == 0) {                 /* OUT Endpoint */
727
+          if (n == 0) {                     /* Control OUT Endpoint */
728
+            if (val & EP_SEL_STP) {         /* Setup Packet */
729
+              if (USB_P_EP[0]) {
730
+                USB_P_EP[0](USB_EVT_SETUP);
731
+                continue;
732
+              }
733
+            }
734
+          }
735
+          if (USB_P_EP[m]) {
736
+            USB_P_EP[m](USB_EVT_OUT);
737
+          }
738
+        } else {                            /* IN Endpoint */
739
+          if (USB_P_EP[m]) {
740
+            USB_P_EP[m](USB_EVT_IN);
741
+          }
742
+        }
743
+      }
744
+    }
745
+    LPC_USB->USBDevIntClr = EP_SLOW_INT;
746
+  }
747
+
748
+#if USB_DMA
749
+
750
+  if (LPC_USB->USBDMAIntSt & 0x00000001) {          /* End of Transfer Interrupt */
751
+    val = LPC_USB->USBEoTIntSt;
752
+    for (n = 2; n < USB_EP_NUM; n++) {      /* Check All Endpoints */
753
+      if (val & (1 << n)) {
754
+        m = n >> 1;
755
+        if ((n & 1) == 0) {                 /* OUT Endpoint */
756
+          if (USB_P_EP[m]) {
757
+            USB_P_EP[m](USB_EVT_OUT_DMA_EOT);
758
+          }
759
+        } else {                            /* IN Endpoint */
760
+          if (USB_P_EP[m]) {
761
+            USB_P_EP[m](USB_EVT_IN_DMA_EOT);
762
+          }
763
+        }
764
+      }
765
+    }
766
+    LPC_USB->USBEoTIntClr = val;
767
+  }
768
+
769
+  if (LPC_USB->USBDMAIntSt & 0x00000002) {          /* New DD Request Interrupt */
770
+    val = LPC_USB->USBNDDRIntSt;
771
+    for (n = 2; n < USB_EP_NUM; n++) {      /* Check All Endpoints */
772
+      if (val & (1 << n)) {
773
+        m = n >> 1;
774
+        if ((n & 1) == 0) {                 /* OUT Endpoint */
775
+          if (USB_P_EP[m]) {
776
+            USB_P_EP[m](USB_EVT_OUT_DMA_NDR);
777
+          }
778
+        } else {                            /* IN Endpoint */
779
+          if (USB_P_EP[m]) {
780
+            USB_P_EP[m](USB_EVT_IN_DMA_NDR);
781
+          }
782
+        }
783
+      }
784
+    }
785
+    LPC_USB->USBNDDRIntClr = val;
786
+  }
787
+
788
+  if (LPC_USB->USBDMAIntSt & 0x00000004) {          /* System Error Interrupt */
789
+    val = LPC_USB->USBSysErrIntSt;
790
+    for (n = 2; n < USB_EP_NUM; n++) {      /* Check All Endpoints */
791
+      if (val & (1 << n)) {
792
+        m = n >> 1;
793
+        if ((n & 1) == 0) {                 /* OUT Endpoint */
794
+          if (USB_P_EP[m]) {
795
+            USB_P_EP[m](USB_EVT_OUT_DMA_ERR);
796
+          }
797
+        } else {                            /* IN Endpoint */
798
+          if (USB_P_EP[m]) {
799
+            USB_P_EP[m](USB_EVT_IN_DMA_ERR);
800
+          }
801
+        }
802
+      }
803
+    }
804
+    LPC_USB->USBSysErrIntClr = val;
805
+  }
806
+
807
+#endif /* USB_DMA */
808
+
809
+isr_end:
810
+  return;
811
+}

+ 114
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbhw.h Näytä tiedosto

@@ -0,0 +1,114 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbhw.h
5
+ * Purpose: USB Hardware Layer Definitions
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *----------------------------------------------------------------------------
18
+ * History:
19
+ *          V1.20 Added USB_ClearEPBuf
20
+ *          V1.00 Initial Version
21
+ *----------------------------------------------------------------------------*/
22
+
23
+#ifndef __USBHW_H__
24
+#define __USBHW_H__
25
+extern "C" {
26
+#include "lpc_types.h"
27
+}
28
+/* USB RAM Definitions */
29
+#define USB_RAM_ADR     0x2007C000  /* USB RAM Start Address */
30
+#define USB_RAM_SZ      0x00004000  /* USB RAM Size (16kB) */
31
+
32
+/* DMA Endpoint Descriptors */
33
+#define DD_NISO_CNT             16  /* Non-Iso EP DMA Descr. Count (max. 32) */
34
+#define DD_ISO_CNT               8  /* Iso EP DMA Descriptor Count (max. 32) */
35
+#define DD_NISO_SZ    (DD_NISO_CNT * 16)    /* Non-Iso DMA Descr. Size */
36
+#define DD_ISO_SZ     (DD_ISO_CNT  * 20)    /* Iso DMA Descriptor Size */
37
+#define DD_NISO_ADR   (USB_RAM_ADR + 128)   /* Non-Iso DMA Descr. Address */
38
+#define DD_ISO_ADR    (DD_NISO_ADR + DD_NISO_SZ) /* Iso DMA Descr. Address */
39
+#define DD_SZ                 (128 + DD_NISO_SZ + DD_ISO_SZ) /* Descr. Size */
40
+
41
+/* DMA Buffer Memory Definitions */
42
+#define DMA_BUF_ADR   (USB_RAM_ADR + DD_SZ) /* DMA Buffer Start Address */
43
+#define DMA_BUF_SZ    (USB_RAM_SZ  - DD_SZ) /* DMA Buffer Size */
44
+
45
+/* USB Error Codes */
46
+#define USB_ERR_PID         0x0001  /* PID Error */
47
+#define USB_ERR_UEPKT       0x0002  /* Unexpected Packet */
48
+#define USB_ERR_DCRC        0x0004  /* Data CRC Error */
49
+#define USB_ERR_TIMOUT      0x0008  /* Bus Time-out Error */
50
+#define USB_ERR_EOP         0x0010  /* End of Packet Error */
51
+#define USB_ERR_B_OVRN      0x0020  /* Buffer Overrun */
52
+#define USB_ERR_BTSTF       0x0040  /* Bit Stuff Error */
53
+#define USB_ERR_TGL         0x0080  /* Toggle Bit Error */
54
+
55
+/* USB DMA Status Codes */
56
+#define USB_DMA_INVALID     0x0000  /* DMA Invalid - Not Configured */
57
+#define USB_DMA_IDLE        0x0001  /* DMA Idle - Waiting for Trigger */
58
+#define USB_DMA_BUSY        0x0002  /* DMA Busy - Transfer in progress */
59
+#define USB_DMA_DONE        0x0003  /* DMA Transfer Done (no Errors)*/
60
+#define USB_DMA_OVER_RUN    0x0004  /* Data Over Run */
61
+#define USB_DMA_UNDER_RUN   0x0005  /* Data Under Run (Short Packet) */
62
+#define USB_DMA_ERROR       0x0006  /* Error */
63
+#define USB_DMA_UNKNOWN     0xFFFF  /* Unknown State */
64
+
65
+/* USB DMA Descriptor */
66
+typedef struct _USB_DMA_DESCRIPTOR {
67
+  uint32_t BufAdr;                     /* DMA Buffer Address */
68
+  uint16_t  BufLen;                     /* DMA Buffer Length */
69
+  uint16_t  MaxSize;                    /* Maximum Packet Size */
70
+  uint32_t InfoAdr;                    /* Packet Info Memory Address */
71
+  union {                           /* DMA Configuration */
72
+    struct {
73
+      uint32_t Link   : 1;             /* Link to existing Descriptors */
74
+      uint32_t IsoEP  : 1;             /* Isonchronous Endpoint */
75
+      uint32_t ATLE   : 1;             /* ATLE (Auto Transfer Length Extract) */
76
+      uint32_t Rsrvd  : 5;             /* Reserved */
77
+      uint32_t LenPos : 8;             /* Length Position (ATLE) */
78
+    } Type;
79
+    uint32_t Val;
80
+  } Cfg;
81
+} USB_DMA_DESCRIPTOR;
82
+
83
+extern "C" {
84
+/* USB Hardware Functions */
85
+extern void  USB_Init       (void);
86
+extern void  USB_Connect    (uint32_t  con);
87
+extern void  USB_Reset      (void);
88
+extern void  USB_Suspend    (void);
89
+extern void  USB_Resume     (void);
90
+extern void  USB_WakeUp     (void);
91
+extern void  USB_WakeUpCfg  (uint32_t  cfg);
92
+extern void  USB_SetAddress (uint32_t adr);
93
+extern void  USB_Configure  (uint32_t  cfg);
94
+extern void  USB_ConfigEP   (USB_ENDPOINT_DESCRIPTOR *pEPD);
95
+extern void  USB_DirCtrlEP  (uint32_t dir);
96
+extern void  USB_EnableEP   (uint32_t EPNum);
97
+extern void  USB_DisableEP  (uint32_t EPNum);
98
+extern void  USB_ResetEP    (uint32_t EPNum);
99
+extern void  USB_SetStallEP (uint32_t EPNum);
100
+extern void  USB_ClrStallEP (uint32_t EPNum);
101
+extern void USB_ClearEPBuf  (uint32_t  EPNum);
102
+extern uint32_t USB_ReadEP     (uint32_t EPNum, uint8_t *pData);
103
+extern uint32_t USB_WriteEP    (uint32_t EPNum, uint8_t *pData, uint32_t cnt);
104
+extern uint32_t  USB_DMA_Setup  (uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD);
105
+extern void  USB_DMA_Enable (uint32_t EPNum);
106
+extern void  USB_DMA_Disable(uint32_t EPNum);
107
+extern uint32_t USB_DMA_Status (uint32_t EPNum);
108
+extern uint32_t USB_DMA_BufAdr (uint32_t EPNum);
109
+extern uint32_t USB_DMA_BufCnt (uint32_t EPNum);
110
+extern uint32_t USB_GetFrame   (void);
111
+extern void  USB_IRQHandler (void);
112
+}
113
+
114
+#endif  /* __USBHW_H__ */

+ 130
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbreg.h Näytä tiedosto

@@ -0,0 +1,130 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbreg.h
5
+ * Purpose: USB Hardware Layer Definitions for NXP LPC Family MCUs
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing 
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __USBREG_H
20
+#define __USBREG_H
21
+
22
+/* Device Interrupt Bit Definitions */
23
+#define FRAME_INT           0x00000001
24
+#define EP_FAST_INT         0x00000002
25
+#define EP_SLOW_INT         0x00000004
26
+#define DEV_STAT_INT        0x00000008
27
+#define CCEMTY_INT          0x00000010
28
+#define CDFULL_INT          0x00000020
29
+#define RxENDPKT_INT        0x00000040
30
+#define TxENDPKT_INT        0x00000080
31
+#define EP_RLZED_INT        0x00000100
32
+#define ERR_INT             0x00000200
33
+
34
+/* Rx & Tx Packet Length Definitions */
35
+#define PKT_LNGTH_MASK      0x000003FF
36
+#define PKT_DV              0x00000400
37
+#define PKT_RDY             0x00000800
38
+
39
+/* USB Control Definitions */
40
+#define CTRL_RD_EN          0x00000001
41
+#define CTRL_WR_EN          0x00000002
42
+
43
+/* Command Codes */
44
+#define CMD_SET_ADDR        0x00D00500
45
+#define CMD_CFG_DEV         0x00D80500
46
+#define CMD_SET_MODE        0x00F30500
47
+#define CMD_RD_FRAME        0x00F50500
48
+#define DAT_RD_FRAME        0x00F50200
49
+#define CMD_RD_TEST         0x00FD0500
50
+#define DAT_RD_TEST         0x00FD0200
51
+#define CMD_SET_DEV_STAT    0x00FE0500
52
+#define CMD_GET_DEV_STAT    0x00FE0500
53
+#define DAT_GET_DEV_STAT    0x00FE0200
54
+#define CMD_GET_ERR_CODE    0x00FF0500
55
+#define DAT_GET_ERR_CODE    0x00FF0200
56
+#define CMD_RD_ERR_STAT     0x00FB0500
57
+#define DAT_RD_ERR_STAT     0x00FB0200
58
+#define DAT_WR_BYTE(x)     (0x00000100 | ((x) << 16))
59
+#define CMD_SEL_EP(x)      (0x00000500 | ((x) << 16))
60
+#define DAT_SEL_EP(x)      (0x00000200 | ((x) << 16))
61
+#define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
62
+#define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
63
+#define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
64
+#define CMD_CLR_BUF         0x00F20500
65
+#define DAT_CLR_BUF         0x00F20200
66
+#define CMD_VALID_BUF       0x00FA0500
67
+
68
+/* Device Address Register Definitions */
69
+#define DEV_ADDR_MASK       0x7F
70
+#define DEV_EN              0x80
71
+
72
+/* Device Configure Register Definitions */
73
+#define CONF_DVICE          0x01
74
+
75
+/* Device Mode Register Definitions */
76
+#define AP_CLK              0x01
77
+#define INAK_CI             0x02
78
+#define INAK_CO             0x04
79
+#define INAK_II             0x08
80
+#define INAK_IO             0x10
81
+#define INAK_BI             0x20
82
+#define INAK_BO             0x40
83
+
84
+/* Device Status Register Definitions */
85
+#define DEV_CON             0x01
86
+#define DEV_CON_CH          0x02
87
+#define DEV_SUS             0x04
88
+#define DEV_SUS_CH          0x08
89
+#define DEV_RST             0x10
90
+
91
+/* Error Code Register Definitions */
92
+#define ERR_EC_MASK         0x0F
93
+#define ERR_EA              0x10
94
+
95
+/* Error Status Register Definitions */
96
+#define ERR_PID             0x01
97
+#define ERR_UEPKT           0x02
98
+#define ERR_DCRC            0x04
99
+#define ERR_TIMOUT          0x08
100
+#define ERR_EOP             0x10
101
+#define ERR_B_OVRN          0x20
102
+#define ERR_BTSTF           0x40
103
+#define ERR_TGL             0x80
104
+
105
+/* Endpoint Select Register Definitions */
106
+#define EP_SEL_F            0x01
107
+#define EP_SEL_ST           0x02
108
+#define EP_SEL_STP          0x04
109
+#define EP_SEL_PO           0x08
110
+#define EP_SEL_EPN          0x10
111
+#define EP_SEL_B_1_FULL     0x20
112
+#define EP_SEL_B_2_FULL     0x40
113
+
114
+/* Endpoint Status Register Definitions */
115
+#define EP_STAT_ST          0x01
116
+#define EP_STAT_DA          0x20
117
+#define EP_STAT_RF_MO       0x40
118
+#define EP_STAT_CND_ST      0x80
119
+
120
+/* Clear Buffer Register Definitions */
121
+#define CLR_BUF_PO          0x01
122
+
123
+
124
+/* DMA Interrupt Bit Definitions */
125
+#define EOT_INT             0x01
126
+#define NDD_REQ_INT         0x02
127
+#define SYS_ERR_INT         0x04
128
+
129
+
130
+#endif  /* __USBREG_H */

+ 353
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbuser.cpp Näytä tiedosto

@@ -0,0 +1,353 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbuser.c
5
+ * Purpose: USB Custom User Module
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+extern "C" {
20
+#include "lpc_types.h"
21
+#include <debug_frmwrk.h>
22
+}
23
+
24
+#include "usb.h"
25
+#include "usbcfg.h"
26
+#include "usbhw.h"
27
+#include "usbcore.h"
28
+#include "usbuser.h"
29
+#include "cdcuser.h"
30
+#include "mscuser.h"
31
+
32
+
33
+/*
34
+ *  USB Power Event Callback
35
+ *   Called automatically on USB Power Event
36
+ *    Parameter:       power: On(TRUE)/Off(FALSE)
37
+ */
38
+
39
+#if USB_POWER_EVENT
40
+void USB_Power_Event (uint32_t  power) {
41
+}
42
+#endif
43
+
44
+
45
+/*
46
+ *  USB Reset Event Callback
47
+ *   Called automatically on USB Reset Event
48
+ */
49
+
50
+#if USB_RESET_EVENT
51
+void USB_Reset_Event (void) {
52
+  USB_ResetCore();
53
+}
54
+#endif
55
+
56
+
57
+/*
58
+ *  USB Suspend Event Callback
59
+ *   Called automatically on USB Suspend Event
60
+ */
61
+
62
+#if USB_SUSPEND_EVENT
63
+void USB_Suspend_Event (void) {
64
+}
65
+#endif
66
+
67
+
68
+/*
69
+ *  USB Resume Event Callback
70
+ *   Called automatically on USB Resume Event
71
+ */
72
+
73
+#if USB_RESUME_EVENT
74
+void USB_Resume_Event (void) {
75
+}
76
+#endif
77
+
78
+
79
+/*
80
+ *  USB Remote Wakeup Event Callback
81
+ *   Called automatically on USB Remote Wakeup Event
82
+ */
83
+
84
+#if USB_WAKEUP_EVENT
85
+void USB_WakeUp_Event (void) {
86
+}
87
+#endif
88
+
89
+
90
+/*
91
+ *  USB Start of Frame Event Callback
92
+ *   Called automatically on USB Start of Frame Event
93
+ */
94
+
95
+#if USB_SOF_EVENT
96
+void USB_SOF_Event (void) {
97
+}
98
+#endif
99
+
100
+
101
+/*
102
+ *  USB Error Event Callback
103
+ *   Called automatically on USB Error Event
104
+ *    Parameter:       error: Error Code
105
+ */
106
+
107
+#if USB_ERROR_EVENT
108
+void USB_Error_Event (uint32_t error) {
109
+}
110
+#endif
111
+
112
+
113
+/*
114
+ *  USB Set Configuration Event Callback
115
+ *   Called automatically on USB Set Configuration Request
116
+ */
117
+
118
+#if USB_CONFIGURE_EVENT
119
+void USB_Configure_Event (void) {
120
+
121
+  if (USB_Configuration) {                  /* Check if USB is configured */
122
+    /* add your code here */
123
+  }
124
+}
125
+#endif
126
+
127
+
128
+/*
129
+ *  USB Set Interface Event Callback
130
+ *   Called automatically on USB Set Interface Request
131
+ */
132
+
133
+#if USB_INTERFACE_EVENT
134
+void USB_Interface_Event (void) {
135
+}
136
+#endif
137
+
138
+
139
+/*
140
+ *  USB Set/Clear Feature Event Callback
141
+ *   Called automatically on USB Set/Clear Feature Request
142
+ */
143
+
144
+#if USB_FEATURE_EVENT
145
+void USB_Feature_Event (void) {
146
+}
147
+#endif
148
+
149
+
150
+#define P_EP(n) ((USB_EP_EVENT & (1 << (n))) ? USB_EndPoint##n : nullptr)
151
+
152
+/* USB Endpoint Events Callback Pointers */
153
+void (* const USB_P_EP[16]) (uint32_t event) = {
154
+  P_EP(0),
155
+  P_EP(1),
156
+  P_EP(2),
157
+  P_EP(3),
158
+  P_EP(4),
159
+  P_EP(5),
160
+  P_EP(6),
161
+  P_EP(7),
162
+  P_EP(8),
163
+  P_EP(9),
164
+  P_EP(10),
165
+  P_EP(11),
166
+  P_EP(12),
167
+  P_EP(13),
168
+  P_EP(14),
169
+  P_EP(15),
170
+};
171
+
172
+
173
+/*
174
+ *  USB Endpoint 1 Event Callback
175
+ *   Called automatically on USB Endpoint 1 Event
176
+ *    Parameter:       event
177
+ */
178
+
179
+void USB_EndPoint1 (uint32_t event) {
180
+  uint16_t temp;
181
+  static uint16_t serialState;
182
+
183
+  switch (event) {
184
+    case USB_EVT_IN:
185
+      temp = CDC_GetSerialState();
186
+      if (serialState != temp) {
187
+         serialState = temp;
188
+         CDC_NotificationIn();            /* send SERIAL_STATE notification */
189
+      }
190
+      break;
191
+    default:
192
+      _DBG("Unhandled EP1 event: ");
193
+      _DBH(event);
194
+      _DBG("\n");
195
+  }
196
+}
197
+
198
+
199
+/*
200
+ *  USB Endpoint 2 Event Callback
201
+ *   Called automatically on USB Endpoint 2 Event
202
+ *    Parameter:       event
203
+ */
204
+
205
+void USB_EndPoint2 (uint32_t event) {
206
+
207
+  switch (event) {
208
+    case USB_EVT_OUT:
209
+      CDC_BulkOut ();                /* data received from Host */
210
+      break;
211
+    case USB_EVT_IN:
212
+      CDC_BulkIn ();                 /* data expected from Host */
213
+      break;
214
+  }
215
+}
216
+
217
+
218
+/*
219
+ *  USB Endpoint 3 Event Callback
220
+ *   Called automatically on USB Endpoint 3 Event
221
+ *    Parameter:       event
222
+ */
223
+
224
+void USB_EndPoint3 (uint32_t event) {
225
+}
226
+
227
+
228
+/*
229
+ *  USB Endpoint 4 Event Callback
230
+ *   Called automatically on USB Endpoint 4 Event
231
+ *    Parameter:       event
232
+ */
233
+
234
+void USB_EndPoint4 (uint32_t event) {
235
+}
236
+
237
+
238
+/*
239
+ *  USB Endpoint 5 Event Callback
240
+ *   Called automatically on USB Endpoint 5 Event
241
+ *    Parameter:       event
242
+ */
243
+
244
+void USB_EndPoint5 (uint32_t event) {
245
+  switch (event) {
246
+    case USB_EVT_OUT:
247
+      MSC_BulkOut();
248
+      break;
249
+    case USB_EVT_IN:
250
+      MSC_BulkIn();
251
+      break;
252
+  }
253
+}
254
+
255
+
256
+/*
257
+ *  USB Endpoint 6 Event Callback
258
+ *   Called automatically on USB Endpoint 6 Event
259
+ *    Parameter:       event
260
+ */
261
+
262
+void USB_EndPoint6 (uint32_t event) {
263
+}
264
+
265
+
266
+/*
267
+ *  USB Endpoint 7 Event Callback
268
+ *   Called automatically on USB Endpoint 7 Event
269
+ *    Parameter:       event
270
+ */
271
+
272
+void USB_EndPoint7 (uint32_t event) {
273
+}
274
+
275
+
276
+/*
277
+ *  USB Endpoint 8 Event Callback
278
+ *   Called automatically on USB Endpoint 8 Event
279
+ *    Parameter:       event
280
+ */
281
+
282
+void USB_EndPoint8 (uint32_t event) {
283
+}
284
+
285
+
286
+/*
287
+ *  USB Endpoint 9 Event Callback
288
+ *   Called automatically on USB Endpoint 9 Event
289
+ *    Parameter:       event
290
+ */
291
+
292
+void USB_EndPoint9 (uint32_t event) {
293
+}
294
+
295
+
296
+/*
297
+ *  USB Endpoint 10 Event Callback
298
+ *   Called automatically on USB Endpoint 10 Event
299
+ *    Parameter:       event
300
+ */
301
+
302
+void USB_EndPoint10 (uint32_t event) {
303
+}
304
+
305
+
306
+/*
307
+ *  USB Endpoint 11 Event Callback
308
+ *   Called automatically on USB Endpoint 11 Event
309
+ *    Parameter:       event
310
+ */
311
+
312
+void USB_EndPoint11 (uint32_t event) {
313
+}
314
+
315
+
316
+/*
317
+ *  USB Endpoint 12 Event Callback
318
+ *   Called automatically on USB Endpoint 12 Event
319
+ *    Parameter:       event
320
+ */
321
+
322
+void USB_EndPoint12 (uint32_t event) {
323
+}
324
+
325
+
326
+/*
327
+ *  USB Endpoint 13 Event Callback
328
+ *   Called automatically on USB Endpoint 13 Event
329
+ *    Parameter:       event
330
+ */
331
+
332
+void USB_EndPoint13 (uint32_t event) {
333
+}
334
+
335
+
336
+/*
337
+ *  USB Endpoint 14 Event Callback
338
+ *   Called automatically on USB Endpoint 14 Event
339
+ *    Parameter:       event
340
+ */
341
+
342
+void USB_EndPoint14 (uint32_t event) {
343
+}
344
+
345
+
346
+/*
347
+ *  USB Endpoint 15 Event Callback
348
+ *   Called automatically on USB Endpoint 15 Event
349
+ *    Parameter:       event
350
+ */
351
+
352
+void USB_EndPoint15 (uint32_t event) {
353
+}

+ 74
- 0
Marlin/frameworks/CMSIS/LPC1768/lib/usb/usbuser.h Näytä tiedosto

@@ -0,0 +1,74 @@
1
+/*----------------------------------------------------------------------------
2
+ *      U S B  -  K e r n e l
3
+ *----------------------------------------------------------------------------
4
+ * Name:    usbuser.h
5
+ * Purpose: USB Custom User Definitions
6
+ * Version: V1.20
7
+ *----------------------------------------------------------------------------
8
+ *      This software is supplied "AS IS" without any warranties, express,
9
+ *      implied or statutory, including but not limited to the implied
10
+ *      warranties of fitness for purpose, satisfactory quality and
11
+ *      noninfringement. Keil extends you a royalty-free right to reproduce
12
+ *      and distribute executable files created using this software for use
13
+ *      on NXP Semiconductors LPC family microcontroller devices only. Nothing 
14
+ *      else gives you the right to use this software.
15
+ *
16
+ * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17
+ *---------------------------------------------------------------------------*/
18
+
19
+#ifndef __USBUSER_H__
20
+#define __USBUSER_H__
21
+
22
+
23
+/* USB Device Events Callback Functions */
24
+extern void USB_Power_Event     (uint32_t power);
25
+extern void USB_Reset_Event     (void);
26
+extern void USB_Suspend_Event   (void);
27
+extern void USB_Resume_Event    (void);
28
+extern void USB_WakeUp_Event    (void);
29
+extern void USB_SOF_Event       (void);
30
+extern void USB_Error_Event     (uint32_t error);
31
+
32
+/* USB Endpoint Callback Events */
33
+#define USB_EVT_SETUP       1   /* Setup Packet */
34
+#define USB_EVT_OUT         2   /* OUT Packet */
35
+#define USB_EVT_IN          3   /*  IN Packet */
36
+#define USB_EVT_OUT_NAK     4   /* OUT Packet - Not Acknowledged */
37
+#define USB_EVT_IN_NAK      5   /*  IN Packet - Not Acknowledged */
38
+#define USB_EVT_OUT_STALL   6   /* OUT Packet - Stalled */
39
+#define USB_EVT_IN_STALL    7   /*  IN Packet - Stalled */
40
+#define USB_EVT_OUT_DMA_EOT 8   /* DMA OUT EP - End of Transfer */
41
+#define USB_EVT_IN_DMA_EOT  9   /* DMA  IN EP - End of Transfer */
42
+#define USB_EVT_OUT_DMA_NDR 10  /* DMA OUT EP - New Descriptor Request */
43
+#define USB_EVT_IN_DMA_NDR  11  /* DMA  IN EP - New Descriptor Request */
44
+#define USB_EVT_OUT_DMA_ERR 12  /* DMA OUT EP - Error */
45
+#define USB_EVT_IN_DMA_ERR  13  /* DMA  IN EP - Error */
46
+
47
+/* USB Endpoint Events Callback Pointers */
48
+extern void (* const USB_P_EP[16])(uint32_t event);
49
+
50
+/* USB Endpoint Events Callback Functions */
51
+extern void USB_EndPoint0  (uint32_t event);
52
+extern void USB_EndPoint1  (uint32_t event);
53
+extern void USB_EndPoint2  (uint32_t event);
54
+extern void USB_EndPoint3  (uint32_t event);
55
+extern void USB_EndPoint4  (uint32_t event);
56
+extern void USB_EndPoint5  (uint32_t event);
57
+extern void USB_EndPoint6  (uint32_t event);
58
+extern void USB_EndPoint7  (uint32_t event);
59
+extern void USB_EndPoint8  (uint32_t event);
60
+extern void USB_EndPoint9  (uint32_t event);
61
+extern void USB_EndPoint10 (uint32_t event);
62
+extern void USB_EndPoint11 (uint32_t event);
63
+extern void USB_EndPoint12 (uint32_t event);
64
+extern void USB_EndPoint13 (uint32_t event);
65
+extern void USB_EndPoint14 (uint32_t event);
66
+extern void USB_EndPoint15 (uint32_t event);
67
+
68
+/* USB Core Events Callback Functions */
69
+extern void USB_Configure_Event (void);
70
+extern void USB_Interface_Event (void);
71
+extern void USB_Feature_Event   (void);
72
+
73
+
74
+#endif  /* __USBUSER_H__ */

Loading…
Peruuta
Tallenna