Browse Source

Adding USB MSD implementation to the HAL: Now, when you plug the Native USB port of the Arduino DUE to your PC, it will be detected as a composite USB device: One of the devices is a USB CDC (Serial port over USB) with native USB handshake (no more overflows!!) (set SERIAL_PORT to -1 to use it as main serial port) and also, a MSD (USB Mass Storage Device) will be detected, and the SD card present on the socket will be reflected as a Removable disk on your PC)

etagle 6 years ago
parent
commit
33a6fc70e4
43 changed files with 16226 additions and 8 deletions
  1. 14
    4
      Marlin/src/HAL/HAL_DUE/HAL_Due.h
  2. 254
    0
      Marlin/src/HAL/HAL_DUE/MarlinSerialUSB_Due.cpp
  3. 96
    0
      Marlin/src/HAL/HAL_DUE/MarlinSerialUSB_Due.h
  4. 4
    4
      Marlin/src/HAL/HAL_DUE/MarlinSerial_Due.h
  5. 103
    0
      Marlin/src/HAL/HAL_DUE/usb/arduino_due_x.h
  6. 1224
    0
      Marlin/src/HAL/HAL_DUE/usb/compiler.h
  7. 115
    0
      Marlin/src/HAL/HAL_DUE/usb/conf_access.h
  8. 100
    0
      Marlin/src/HAL/HAL_DUE/usb/conf_clock.h
  9. 287
    0
      Marlin/src/HAL/HAL_DUE/usb/conf_usb.h
  10. 646
    0
      Marlin/src/HAL/HAL_DUE/usb/ctrl_access.c
  11. 402
    0
      Marlin/src/HAL/HAL_DUE/usb/ctrl_access.h
  12. 278
    0
      Marlin/src/HAL/HAL_DUE/usb/genclk.h
  13. 339
    0
      Marlin/src/HAL/HAL_DUE/usb/mrepeat.h
  14. 261
    0
      Marlin/src/HAL/HAL_DUE/usb/osc.h
  15. 288
    0
      Marlin/src/HAL/HAL_DUE/usb/pll.h
  16. 55
    0
      Marlin/src/HAL/HAL_DUE/usb/preprocessor.h
  17. 173
    0
      Marlin/src/HAL/HAL_DUE/usb/sbc_protocol.h
  18. 135
    0
      Marlin/src/HAL/HAL_DUE/usb/sd_mmc_spi_mem.cpp
  19. 177
    0
      Marlin/src/HAL/HAL_DUE/usb/sd_mmc_spi_mem.h
  20. 337
    0
      Marlin/src/HAL/HAL_DUE/usb/spc_protocol.h
  21. 85
    0
      Marlin/src/HAL/HAL_DUE/usb/stringz.h
  22. 122
    0
      Marlin/src/HAL/HAL_DUE/usb/sysclk.c
  23. 229
    0
      Marlin/src/HAL/HAL_DUE/usb/sysclk.h
  24. 105
    0
      Marlin/src/HAL/HAL_DUE/usb/tpaste.h
  25. 1149
    0
      Marlin/src/HAL/HAL_DUE/usb/udc.c
  26. 697
    0
      Marlin/src/HAL/HAL_DUE/usb/udc.h
  27. 135
    0
      Marlin/src/HAL/HAL_DUE/usb/udc_desc.h
  28. 396
    0
      Marlin/src/HAL/HAL_DUE/usb/udd.h
  29. 133
    0
      Marlin/src/HAL/HAL_DUE/usb/udi.h
  30. 1155
    0
      Marlin/src/HAL/HAL_DUE/usb/udi_cdc.c
  31. 810
    0
      Marlin/src/HAL/HAL_DUE/usb/udi_cdc.h
  32. 188
    0
      Marlin/src/HAL/HAL_DUE/usb/udi_composite_desc.c
  33. 1128
    0
      Marlin/src/HAL/HAL_DUE/usb/udi_msc.c
  34. 376
    0
      Marlin/src/HAL/HAL_DUE/usb/udi_msc.h
  35. 2073
    0
      Marlin/src/HAL/HAL_DUE/usb/uotghs_device_due.c
  36. 663
    0
      Marlin/src/HAL/HAL_DUE/usb/uotghs_device_due.h
  37. 242
    0
      Marlin/src/HAL/HAL_DUE/usb/uotghs_otg.h
  38. 496
    0
      Marlin/src/HAL/HAL_DUE/usb/usb_protocol.h
  39. 318
    0
      Marlin/src/HAL/HAL_DUE/usb/usb_protocol_cdc.h
  40. 147
    0
      Marlin/src/HAL/HAL_DUE/usb/usb_protocol_msc.h
  41. 186
    0
      Marlin/src/HAL/HAL_DUE/usb/usb_task.c
  42. 101
    0
      Marlin/src/HAL/HAL_DUE/usb/usb_task.h
  43. 4
    0
      Marlin/src/Marlin.cpp

+ 14
- 4
Marlin/src/HAL/HAL_DUE/HAL_Due.h View File

@@ -40,15 +40,13 @@
40 40
 //
41 41
 // Defines
42 42
 //
43
-
44
-#if SERIAL_PORT == -1
45
-  #define MYSERIAL SerialUSB
46
-#elif SERIAL_PORT >= 0 && SERIAL_PORT <= 4
43
+#if SERIAL_PORT >= -1 && SERIAL_PORT <= 4
47 44
   #define MYSERIAL customizedSerial
48 45
 #endif
49 46
 
50 47
 // We need the previous define before the include, or compilation bombs...
51 48
 #include "MarlinSerial_Due.h"
49
+#include "MarlinSerialUSB_Due.h"
52 50
 
53 51
 #ifndef analogInputToDigitalPin
54 52
   #define analogInputToDigitalPin(p) ((p < 12u) ? (p) + 54u : -1)
@@ -153,4 +151,16 @@ void HAL_enable_AdcFreerun(void);
153 151
 #define GET_PIN_MAP_INDEX(pin) pin
154 152
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
155 153
 
154
+// Enable hooks into idle and setup for USB stack
155
+#define HAL_IDLETASK 1
156
+#define HAL_INIT 1
157
+#ifdef __cplusplus
158
+extern "C" {
159
+#endif
160
+void HAL_idletask(void);
161
+void HAL_init(void);
162
+#ifdef __cplusplus
163
+}
164
+#endif
165
+
156 166
 #endif // _HAL_DUE_H

+ 254
- 0
Marlin/src/HAL/HAL_DUE/MarlinSerialUSB_Due.cpp View File

@@ -0,0 +1,254 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ * MarlinSerial_Due.cpp - Hardware serial library for Arduino DUE
25
+ * Copyright (c) 2017 Eduardo José Tagle. All right reserved
26
+ * Based on MarlinSerial for AVR, copyright (c) 2006 Nicholas Zambetti.  All right reserved.
27
+ */
28
+#ifdef ARDUINO_ARCH_SAM
29
+
30
+#include "../../inc/MarlinConfig.h"
31
+
32
+#if SERIAL_PORT == -1
33
+
34
+#include "MarlinSerialUSB_Due.h"
35
+
36
+// Imports from Atmel USB Stack/CDC implementation
37
+extern "C" {
38
+  bool usb_task_cdc_isenabled(void);
39
+  bool udi_cdc_is_rx_ready(void);
40
+  int udi_cdc_getc(void);
41
+  bool udi_cdc_is_tx_ready(void);
42
+  int udi_cdc_putc(int value);
43
+};
44
+
45
+// Pending character
46
+static int pending_char = -1;
47
+
48
+// Public Methods
49
+void MarlinSerialUSB::begin(const long baud_setting) {
50
+}
51
+
52
+void MarlinSerialUSB::end() {
53
+}
54
+
55
+int MarlinSerialUSB::peek(void) {
56
+  if (pending_char >= 0)
57
+    return pending_char;
58
+
59
+  if (!usb_task_cdc_isenabled())
60
+    return -1;
61
+
62
+  if (!udi_cdc_is_rx_ready())
63
+    return -1;
64
+
65
+  pending_char = udi_cdc_getc();
66
+  return pending_char;
67
+}
68
+
69
+int MarlinSerialUSB::read(void) {
70
+  if (pending_char >= 0) {
71
+    int ret = pending_char;
72
+    pending_char = -1;
73
+    return ret;
74
+  }
75
+
76
+  if (!usb_task_cdc_isenabled())
77
+    return -1;
78
+
79
+  if (!udi_cdc_is_rx_ready())
80
+    return -1;
81
+
82
+  return udi_cdc_getc();
83
+}
84
+
85
+bool MarlinSerialUSB::available(void) {
86
+  return pending_char >= 0 ||
87
+      (usb_task_cdc_isenabled() && udi_cdc_is_rx_ready());
88
+}
89
+
90
+void MarlinSerialUSB::flush(void) {
91
+}
92
+
93
+void MarlinSerialUSB::write(const uint8_t c) {
94
+
95
+  while (usb_task_cdc_isenabled() &&
96
+        !udi_cdc_is_tx_ready()) {
97
+  };
98
+
99
+  if (!usb_task_cdc_isenabled())
100
+    return;
101
+
102
+  // Fifo full
103
+  //  udi_cdc_signal_overrun();
104
+  udi_cdc_putc(c);
105
+}
106
+
107
+/**
108
+* Imports from print.h
109
+*/
110
+
111
+void MarlinSerialUSB::print(char c, int base) {
112
+  print((long)c, base);
113
+}
114
+
115
+void MarlinSerialUSB::print(unsigned char b, int base) {
116
+  print((unsigned long)b, base);
117
+}
118
+
119
+void MarlinSerialUSB::print(int n, int base) {
120
+  print((long)n, base);
121
+}
122
+
123
+void MarlinSerialUSB::print(unsigned int n, int base) {
124
+  print((unsigned long)n, base);
125
+}
126
+
127
+void MarlinSerialUSB::print(long n, int base) {
128
+  if (base == 0)
129
+    write(n);
130
+  else if (base == 10) {
131
+    if (n < 0) {
132
+      print('-');
133
+      n = -n;
134
+    }
135
+    printNumber(n, 10);
136
+  }
137
+  else
138
+    printNumber(n, base);
139
+}
140
+
141
+void MarlinSerialUSB::print(unsigned long n, int base) {
142
+  if (base == 0) write(n);
143
+  else printNumber(n, base);
144
+}
145
+
146
+void MarlinSerialUSB::print(double n, int digits) {
147
+  printFloat(n, digits);
148
+}
149
+
150
+void MarlinSerialUSB::println(void) {
151
+  print('\r');
152
+  print('\n');
153
+}
154
+
155
+void MarlinSerialUSB::println(const String& s) {
156
+  print(s);
157
+  println();
158
+}
159
+
160
+void MarlinSerialUSB::println(const char c[]) {
161
+  print(c);
162
+  println();
163
+}
164
+
165
+void MarlinSerialUSB::println(char c, int base) {
166
+  print(c, base);
167
+  println();
168
+}
169
+
170
+void MarlinSerialUSB::println(unsigned char b, int base) {
171
+  print(b, base);
172
+  println();
173
+}
174
+
175
+void MarlinSerialUSB::println(int n, int base) {
176
+  print(n, base);
177
+  println();
178
+}
179
+
180
+void MarlinSerialUSB::println(unsigned int n, int base) {
181
+  print(n, base);
182
+  println();
183
+}
184
+
185
+void MarlinSerialUSB::println(long n, int base) {
186
+  print(n, base);
187
+  println();
188
+}
189
+
190
+void MarlinSerialUSB::println(unsigned long n, int base) {
191
+  print(n, base);
192
+  println();
193
+}
194
+
195
+void MarlinSerialUSB::println(double n, int digits) {
196
+  print(n, digits);
197
+  println();
198
+}
199
+
200
+// Private Methods
201
+
202
+void MarlinSerialUSB::printNumber(unsigned long n, uint8_t base) {
203
+  if (n) {
204
+    unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
205
+    int8_t i = 0;
206
+    while (n) {
207
+      buf[i++] = n % base;
208
+      n /= base;
209
+    }
210
+    while (i--)
211
+      print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
212
+  }
213
+  else
214
+    print('0');
215
+}
216
+
217
+void MarlinSerialUSB::printFloat(double number, uint8_t digits) {
218
+  // Handle negative numbers
219
+  if (number < 0.0) {
220
+    print('-');
221
+    number = -number;
222
+  }
223
+
224
+  // Round correctly so that print(1.999, 2) prints as "2.00"
225
+  double rounding = 0.5;
226
+  for (uint8_t i = 0; i < digits; ++i)
227
+    rounding *= 0.1;
228
+
229
+  number += rounding;
230
+
231
+  // Extract the integer part of the number and print it
232
+  unsigned long int_part = (unsigned long)number;
233
+  double remainder = number - (double)int_part;
234
+  print(int_part);
235
+
236
+  // Print the decimal point, but only if there are digits beyond
237
+  if (digits) {
238
+    print('.');
239
+    // Extract digits from the remainder one at a time
240
+    while (digits--) {
241
+      remainder *= 10.0;
242
+      int toPrint = int(remainder);
243
+      print(toPrint);
244
+      remainder -= toPrint;
245
+    }
246
+  }
247
+}
248
+
249
+// Preinstantiate
250
+MarlinSerialUSB customizedSerial;
251
+
252
+#endif // SERIAL_PORT == -1
253
+
254
+#endif // ARDUINO_ARCH_SAM

+ 96
- 0
Marlin/src/HAL/HAL_DUE/MarlinSerialUSB_Due.h View File

@@ -0,0 +1,96 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ * MarlinSerialUSB_Due.h - Hardware Serial over USB (CDC) library for Arduino DUE
25
+ * Copyright (c) 2017 Eduardo José Tagle. All right reserved
26
+ */
27
+
28
+#ifndef MARLINSERIALUSB_DUE_H
29
+#define MARLINSERIALUSB_DUE_H
30
+
31
+#include "../../inc/MarlinConfig.h"
32
+
33
+#if SERIAL_PORT == -1
34
+
35
+#include <WString.h>
36
+
37
+#define DEC 10
38
+#define HEX 16
39
+#define OCT 8
40
+#define BIN 2
41
+#define BYTE 0
42
+
43
+class MarlinSerialUSB {
44
+
45
+public:
46
+  MarlinSerialUSB() {};
47
+  static void begin(const long);
48
+  static void end();
49
+  static int peek(void);
50
+  static int read(void);
51
+  static void flush(void);
52
+  static bool available(void);
53
+  static void write(const uint8_t c);
54
+
55
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
56
+  FORCE_INLINE static uint32_t dropped() { return 0; }
57
+  #endif
58
+
59
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
60
+  FORCE_INLINE static int rxMaxEnqueued() { return 0; }
61
+  #endif
62
+
63
+  static FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
64
+  static FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
65
+  static FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
66
+  static FORCE_INLINE void print(const char* str) { write(str); }
67
+
68
+  static void print(char, int = BYTE);
69
+  static void print(unsigned char, int = BYTE);
70
+  static void print(int, int = DEC);
71
+  static void print(unsigned int, int = DEC);
72
+  static void print(long, int = DEC);
73
+  static void print(unsigned long, int = DEC);
74
+  static void print(double, int = 2);
75
+
76
+  static void println(const String& s);
77
+  static void println(const char[]);
78
+  static void println(char, int = BYTE);
79
+  static void println(unsigned char, int = BYTE);
80
+  static void println(int, int = DEC);
81
+  static void println(unsigned int, int = DEC);
82
+  static void println(long, int = DEC);
83
+  static void println(unsigned long, int = DEC);
84
+  static void println(double, int = 2);
85
+  static void println(void);
86
+  operator bool() { return true; }
87
+
88
+private:
89
+  static void printNumber(unsigned long, const uint8_t);
90
+  static void printFloat(double, uint8_t);
91
+};
92
+
93
+extern MarlinSerialUSB customizedSerial;
94
+
95
+#endif // SERIAL_PORT == -1
96
+#endif // MARLINSERIAL_DUE_H

+ 4
- 4
Marlin/src/HAL/HAL_DUE/MarlinSerial_Due.h View File

@@ -31,11 +31,9 @@
31 31
 
32 32
 #include "../../inc/MarlinConfig.h"
33 33
 
34
-#include <WString.h>
34
+#if SERIAL_PORT >= 0
35 35
 
36
-#ifndef SERIAL_PORT
37
-  #define SERIAL_PORT 0
38
-#endif
36
+#include <WString.h>
39 37
 
40 38
 #define DEC 10
41 39
 #define HEX 16
@@ -135,4 +133,6 @@ private:
135 133
 
136 134
 extern MarlinSerial customizedSerial;
137 135
 
136
+#endif // SERIAL_PORT >= 0
137
+
138 138
 #endif // MARLINSERIAL_DUE_H

+ 103
- 0
Marlin/src/HAL/HAL_DUE/usb/arduino_due_x.h View File

@@ -0,0 +1,103 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Arduino Due/X Board Definition.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef ARDUINO_DUE_X_H_INCLUDED
48
+#define ARDUINO_DUE_X_H_INCLUDED
49
+
50
+/* ------------------------------------------------------------------------ */
51
+
52
+/**
53
+ *  \page arduino_due_x_opfreq "Arduino Due/X - Operating frequencies"
54
+ *  This page lists several definition related to the board operating frequency
55
+ *
56
+ *  \section Definitions
57
+ *  - \ref BOARD_FREQ_*
58
+ *  - \ref BOARD_MCK
59
+ */
60
+
61
+/*! Board oscillator settings */
62
+#define BOARD_FREQ_SLCK_XTAL            (32768U)
63
+#define BOARD_FREQ_SLCK_BYPASS          (32768U)
64
+#define BOARD_FREQ_MAINCK_XTAL          (12000000U)
65
+#define BOARD_FREQ_MAINCK_BYPASS        (12000000U)
66
+
67
+/*! Master clock frequency */
68
+#define BOARD_MCK                       CHIP_FREQ_CPU_MAX
69
+#define BOARD_NO_32K_XTAL
70
+
71
+/** board main clock xtal startup time */
72
+#define BOARD_OSC_STARTUP_US   15625
73
+
74
+/* ------------------------------------------------------------------------ */
75
+
76
+/**
77
+ * \page arduino_due_x_board_info "Arduino Due/X - Board informations"
78
+ * This page lists several definition related to the board description.
79
+ *
80
+ */
81
+
82
+/* ------------------------------------------------------------------------ */
83
+/* USB                                                                      */
84
+/* ------------------------------------------------------------------------ */
85
+/*! USB OTG VBus On/Off: Bus Power Control Port. */
86
+#define PIN_UOTGHS_VBOF  { PIO_PB10, PIOB, ID_PIOB, PIO_PERIPH_A, PIO_PULLUP }
87
+/*! USB OTG Identification: Mini Connector Identification Port. */
88
+#define PIN_UOTGHS_ID    { PIO_PB11, PIOB, ID_PIOB, PIO_PERIPH_A, PIO_PULLUP }
89
+
90
+/*! Multiplexed pin used for USB_ID: */
91
+#define USB_ID                      PIO_PB11_IDX
92
+#define USB_ID_GPIO                 (PIO_PB11_IDX)
93
+#define USB_ID_FLAGS                (PIO_PERIPH_A | PIO_DEFAULT)
94
+/*! Multiplexed pin used for USB_VBOF: */
95
+#define USB_VBOF                    PIO_PB10_IDX
96
+#define USB_VBOF_GPIO               (PIO_PB10_IDX)
97
+#define USB_VBOF_FLAGS              (PIO_PERIPH_A | PIO_DEFAULT)
98
+/*! Active level of the USB_VBOF output pin. */
99
+#define USB_VBOF_ACTIVE_LEVEL       LOW
100
+/* ------------------------------------------------------------------------ */
101
+
102
+
103
+#endif /* ARDUINO_DUE_X_H_INCLUDED */

+ 1224
- 0
Marlin/src/HAL/HAL_DUE/usb/compiler.h
File diff suppressed because it is too large
View File


+ 115
- 0
Marlin/src/HAL/HAL_DUE/usb/conf_access.h View File

@@ -0,0 +1,115 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Memory access control configuration file.
5
+ *
6
+ * Copyright (c) 2012-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _CONF_ACCESS_H_
48
+#define _CONF_ACCESS_H_
49
+
50
+#include "compiler.h"
51
+
52
+
53
+/*! \name Activation of Logical Unit Numbers
54
+ */
55
+//! @{
56
+#define LUN_0                ENABLE    //!< SD/MMC Card over MCI Slot 0.
57
+#define LUN_1                DISABLE   
58
+#define LUN_2                DISABLE   
59
+#define LUN_3                DISABLE   
60
+#define LUN_4                DISABLE
61
+#define LUN_5                DISABLE
62
+#define LUN_6                DISABLE
63
+#define LUN_7                DISABLE
64
+#define LUN_USB              DISABLE   
65
+//! @}
66
+
67
+/*! \name LUN 0 Definitions
68
+ */
69
+//! @{
70
+#define SD_MMC_SPI_MEM                          LUN_0
71
+#define LUN_ID_SD_MMC_SPI_MEM                   LUN_ID_0
72
+#define LUN_0_INCLUDE                           "sd_mmc_spi_mem.h"
73
+#define Lun_0_test_unit_ready                   sd_mmc_spi_test_unit_ready
74
+#define Lun_0_read_capacity                     sd_mmc_spi_read_capacity
75
+#define Lun_0_unload                            sd_mmc_spi_unload
76
+#define Lun_0_wr_protect                        sd_mmc_spi_wr_protect
77
+#define Lun_0_removal                           sd_mmc_spi_removal
78
+#define Lun_0_usb_read_10                       sd_mmc_spi_usb_read_10
79
+#define Lun_0_usb_write_10                      sd_mmc_spi_usb_write_10
80
+#define LUN_0_NAME                              "\"SD/MMC Card\""
81
+//! @}
82
+
83
+
84
+/*! \name Actions Associated with Memory Accesses
85
+ *
86
+ * Write here the action to associate with each memory access.
87
+ *
88
+ * \warning Be careful not to waste time in order not to disturb the functions.
89
+ */
90
+//! @{
91
+#define memory_start_read_action(nb_sectors)    
92
+#define memory_stop_read_action()               
93
+#define memory_start_write_action(nb_sectors)   
94
+#define memory_stop_write_action()              
95
+//! @}
96
+
97
+/*! \name Activation of Interface Features
98
+ */
99
+//! @{
100
+#define ACCESS_USB           true    //!< MEM <-> USB interface.
101
+#define ACCESS_MEM_TO_RAM    false   //!< MEM <-> RAM interface.
102
+#define ACCESS_STREAM        false   //!< Streaming MEM <-> MEM interface.
103
+#define ACCESS_STREAM_RECORD false   //!< Streaming MEM <-> MEM interface in record mode.
104
+#define ACCESS_MEM_TO_MEM    false   //!< MEM <-> MEM interface.
105
+#define ACCESS_CODEC         false   //!< Codec interface.
106
+//! @}
107
+
108
+/*! \name Specific Options for Access Control
109
+ */
110
+//! @{
111
+#define GLOBAL_WR_PROTECT    false   //!< Management of a global write protection.
112
+//! @}
113
+
114
+
115
+#endif // _CONF_ACCESS_H_

+ 100
- 0
Marlin/src/HAL/HAL_DUE/usb/conf_clock.h View File

@@ -0,0 +1,100 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief SAM3X clock configuration.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef CONF_CLOCK_H_INCLUDED
48
+#define CONF_CLOCK_H_INCLUDED
49
+
50
+// ===== System Clock (MCK) Source Options
51
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_SLCK_RC
52
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_SLCK_XTAL
53
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_SLCK_BYPASS
54
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_MAINCK_4M_RC
55
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_MAINCK_8M_RC
56
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_MAINCK_12M_RC
57
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_MAINCK_XTAL
58
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_MAINCK_BYPASS
59
+#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_PLLACK
60
+//#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_UPLLCK
61
+
62
+// ===== System Clock (MCK) Prescaler Options   (Fmck = Fsys / (SYSCLK_PRES))
63
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_1
64
+#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_2
65
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_4
66
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_8
67
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_16
68
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_32
69
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_64
70
+//#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_3
71
+
72
+// ===== PLL0 (A) Options   (Fpll = (Fclk * PLL_mul) / PLL_div)
73
+// Use mul and div effective values here.
74
+#define CONFIG_PLL0_SOURCE          PLL_SRC_MAINCK_XTAL
75
+#define CONFIG_PLL0_MUL             14
76
+#define CONFIG_PLL0_DIV             1
77
+
78
+// ===== UPLL (UTMI) Hardware fixed at 480MHz.
79
+
80
+// ===== USB Clock Source Options   (Fusb = FpllX / USB_div)
81
+// Use div effective value here.
82
+//#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_PLL0
83
+#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_UPLL
84
+#define CONFIG_USBCLK_DIV           1
85
+
86
+// ===== Target frequency (System clock)
87
+// - XTAL frequency: 12MHz
88
+// - System clock source: PLLA
89
+// - System clock prescaler: 2 (divided by 2)
90
+// - PLLA source: XTAL
91
+// - PLLA output: XTAL * 14 / 1
92
+// - System clock is: 12 * 14 / 1 /2 = 84MHz
93
+// ===== Target frequency (USB Clock)
94
+// - USB clock source: UPLL
95
+// - USB clock divider: 1 (not divided)
96
+// - UPLL frequency: 480MHz
97
+// - USB clock: 480 / 1 = 480MHz
98
+
99
+
100
+#endif /* CONF_CLOCK_H_INCLUDED */

+ 287
- 0
Marlin/src/HAL/HAL_DUE/usb/conf_usb.h View File

@@ -0,0 +1,287 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB configuration file
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _CONF_USB_H_
48
+#define _CONF_USB_H_
49
+
50
+#include "compiler.h"
51
+
52
+/**
53
+ * USB Device Configuration
54
+ * @{
55
+ */
56
+
57
+//! Device definition (mandatory)
58
+#define  USB_DEVICE_VENDOR_ID             0x03EB /* ATMEL VID */
59
+#define  USB_DEVICE_PRODUCT_ID            0x2424 /* MSC / CDC */
60
+#define  USB_DEVICE_MAJOR_VERSION         1
61
+#define  USB_DEVICE_MINOR_VERSION         0
62
+#define  USB_DEVICE_POWER                 100 // Consumption on Vbus line (mA)
63
+#define  USB_DEVICE_ATTR                  \
64
+	(USB_CONFIG_ATTR_SELF_POWERED)
65
+// (USB_CONFIG_ATTR_BUS_POWERED)
66
+//	(USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_SELF_POWERED)
67
+//	(USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED)
68
+
69
+//! USB Device string definitions (Optional)
70
+#define  USB_DEVICE_MANUFACTURE_NAME      "MARLIN 3D"
71
+#define  USB_DEVICE_PRODUCT_NAME          "CDC and MSC"
72
+#define  USB_DEVICE_SERIAL_NAME           "123985739853" // Disk SN for MSC
73
+
74
+/**
75
+ * Device speeds support
76
+ * Low speed not supported by CDC and MSC
77
+ * @{
78
+ */
79
+
80
+//! To define a Low speed device
81
+//#define  USB_DEVICE_LOW_SPEED
82
+
83
+//! To authorize the High speed
84
+#if (UC3A3||UC3A4)
85
+#  define  USB_DEVICE_HS_SUPPORT
86
+#elif (SAM3XA||SAM3U)
87
+#  define  USB_DEVICE_HS_SUPPORT
88
+#endif
89
+//@}
90
+
91
+
92
+/**
93
+ * USB Device Callbacks definitions (Optional)
94
+ * @{
95
+ */
96
+#define  UDC_VBUS_EVENT(b_vbus_high)
97
+#define  UDC_SOF_EVENT()                  
98
+#define  UDC_SUSPEND_EVENT()              
99
+#define  UDC_RESUME_EVENT()               
100
+#define  UDC_GET_EXTRA_STRING()           usb_task_extra_string()
101
+//@}
102
+
103
+/**
104
+ * USB Device low level configuration
105
+ * When only one interface is used, these configurations are defined by the class module.
106
+ * For composite device, these configuration must be defined here
107
+ * @{
108
+ */
109
+//! Control endpoint size
110
+#define  USB_DEVICE_EP_CTRL_SIZE       64
111
+
112
+//! Two interfaces for this device (CDC COM + CDC DATA + MSC)
113
+#define  USB_DEVICE_NB_INTERFACE       3
114
+
115
+//! 5 endpoints used by CDC and MSC interfaces
116
+#if SAM3U
117
+// (3 | USB_EP_DIR_IN)  // CDC Notify endpoint
118
+// (6 | USB_EP_DIR_IN)  // CDC TX
119
+// (5 | USB_EP_DIR_OUT) // CDC RX
120
+// (1 | USB_EP_DIR_IN)  // MSC IN
121
+// (2 | USB_EP_DIR_OUT) // MSC OUT
122
+#  define  USB_DEVICE_MAX_EP           6
123
+#  if defined(USB_DEVICE_HS_SUPPORT)
124
+// In HS mode, size of bulk endpoints are 512
125
+// If CDC and MSC endpoints all uses 2 banks, DPRAM is not enough: 4 bulk
126
+// endpoints requires 4K bytes. So reduce the number of banks of CDC bulk
127
+// endpoints to use less DPRAM. Keep MSC setting to keep MSC performance.
128
+#     define  UDD_BULK_NB_BANK(ep) ((ep == 5 || ep== 6) ? 1 : 2)
129
+#endif
130
+#else
131
+// (3 | USB_EP_DIR_IN)  // CDC Notify endpoint
132
+// (4 | USB_EP_DIR_IN)  // CDC TX
133
+// (5 | USB_EP_DIR_OUT) // CDC RX
134
+// (1 | USB_EP_DIR_IN)  // MSC IN
135
+// (2 | USB_EP_DIR_OUT) // MSC OUT
136
+#  define  USB_DEVICE_MAX_EP           5
137
+#  if SAM3XA && defined(USB_DEVICE_HS_SUPPORT)
138
+// In HS mode, size of bulk endpoints are 512
139
+// If CDC and MSC endpoints all uses 2 banks, DPRAM is not enough: 4 bulk
140
+// endpoints requires 4K bytes. So reduce the number of banks of CDC bulk
141
+// endpoints to use less DPRAM. Keep MSC setting to keep MSC performance.
142
+#     define  UDD_BULK_NB_BANK(ep) ((ep == 4 || ep== 5) ? 1 : 2)
143
+#  endif
144
+#endif
145
+//@}
146
+
147
+//@}
148
+
149
+
150
+/**
151
+ * USB Interface Configuration
152
+ * @{
153
+ */
154
+/**
155
+ * Configuration of CDC interface
156
+ * @{
157
+ */
158
+
159
+//! Define one USB communication ports
160
+#define  UDI_CDC_PORT_NB 1
161
+
162
+//! Interface callback definition
163
+#define  UDI_CDC_ENABLE_EXT(port)         usb_task_cdc_enable(port)
164
+#define  UDI_CDC_DISABLE_EXT(port)        usb_task_cdc_disable(port)
165
+#define  UDI_CDC_RX_NOTIFY(port)          usb_task_cdc_rx_notify(port)
166
+#define  UDI_CDC_TX_EMPTY_NOTIFY(port)
167
+#define  UDI_CDC_SET_CODING_EXT(port,cfg) usb_task_cdc_config(port,cfg)
168
+#define  UDI_CDC_SET_DTR_EXT(port,set)    usb_task_cdc_set_dtr(port,set)
169
+#define  UDI_CDC_SET_RTS_EXT(port,set)
170
+
171
+//! Define it when the transfer CDC Device to Host is a low rate (<512000 bauds)
172
+//! to reduce CDC buffers size
173
+//#define  UDI_CDC_LOW_RATE
174
+
175
+//! Default configuration of communication port
176
+#define  UDI_CDC_DEFAULT_RATE             115200
177
+#define  UDI_CDC_DEFAULT_STOPBITS         CDC_STOP_BITS_1
178
+#define  UDI_CDC_DEFAULT_PARITY           CDC_PAR_NONE
179
+#define  UDI_CDC_DEFAULT_DATABITS         8
180
+
181
+//! Enable id string of interface to add an extra USB string
182
+#define  UDI_CDC_IAD_STRING_ID            4
183
+
184
+/**
185
+ * USB CDC low level configuration
186
+ * In standalone these configurations are defined by the CDC module.
187
+ * For composite device, these configuration must be defined here
188
+ * @{
189
+ */
190
+//! Endpoint numbers definition
191
+#if SAM3U
192
+#  define  UDI_CDC_COMM_EP_0             (3 | USB_EP_DIR_IN) // Notify endpoint
193
+#  define  UDI_CDC_DATA_EP_IN_0          (6 | USB_EP_DIR_IN) // TX
194
+#  define  UDI_CDC_DATA_EP_OUT_0         (5 | USB_EP_DIR_OUT)// RX
195
+#else
196
+#  define  UDI_CDC_COMM_EP_0             (3 | USB_EP_DIR_IN) // Notify endpoint
197
+#  define  UDI_CDC_DATA_EP_IN_0          (4 | USB_EP_DIR_IN) // TX
198
+#  define  UDI_CDC_DATA_EP_OUT_0         (5 | USB_EP_DIR_OUT)// RX
199
+#endif
200
+
201
+//! Interface numbers
202
+#define  UDI_CDC_COMM_IFACE_NUMBER_0   0
203
+#define  UDI_CDC_DATA_IFACE_NUMBER_0   1
204
+//@}
205
+//@}
206
+
207
+
208
+/**
209
+ * Configuration of MSC interface
210
+ * @{
211
+ */
212
+//! Vendor name and Product version of MSC interface
213
+#define UDI_MSC_GLOBAL_VENDOR_ID            \
214
+   'M', 'A', 'R', 'L', 'I', 'N', '3', 'D'
215
+#define UDI_MSC_GLOBAL_PRODUCT_VERSION            \
216
+   '1', '.', '0', '0'
217
+
218
+//! Interface callback definition
219
+#define  UDI_MSC_ENABLE_EXT()          usb_task_msc_enable()
220
+#define  UDI_MSC_DISABLE_EXT()         usb_task_msc_disable()
221
+
222
+//! Enable id string of interface to add an extra USB string
223
+#define  UDI_MSC_STRING_ID                5
224
+
225
+/**
226
+ * USB MSC low level configuration
227
+ * In standalone these configurations are defined by the MSC module.
228
+ * For composite device, these configuration must be defined here
229
+ * @{
230
+ */
231
+//! Endpoint numbers definition
232
+#define  UDI_MSC_EP_IN                 (1 | USB_EP_DIR_IN)
233
+#define  UDI_MSC_EP_OUT                (2 | USB_EP_DIR_OUT)
234
+
235
+//! Interface number
236
+#define  UDI_MSC_IFACE_NUMBER          2
237
+//@}
238
+//@}
239
+
240
+//@}
241
+
242
+
243
+/**
244
+ * Description of Composite Device
245
+ * @{
246
+ */
247
+//! USB Interfaces descriptor structure
248
+#define UDI_COMPOSITE_DESC_T \
249
+	usb_iad_desc_t       udi_cdc_iad; \
250
+	udi_cdc_comm_desc_t  udi_cdc_comm; \
251
+	udi_cdc_data_desc_t  udi_cdc_data; \
252
+	udi_msc_desc_t       udi_msc
253
+
254
+//! USB Interfaces descriptor value for Full Speed
255
+#define UDI_COMPOSITE_DESC_FS \
256
+	.udi_cdc_iad   = UDI_CDC_IAD_DESC_0, \
257
+	.udi_cdc_comm  = UDI_CDC_COMM_DESC_0, \
258
+	.udi_cdc_data  = UDI_CDC_DATA_DESC_0_FS, \
259
+	.udi_msc       = UDI_MSC_DESC_FS
260
+
261
+//! USB Interfaces descriptor value for High Speed
262
+#define UDI_COMPOSITE_DESC_HS \
263
+	.udi_cdc_iad   = UDI_CDC_IAD_DESC_0, \
264
+	.udi_cdc_comm  = UDI_CDC_COMM_DESC_0, \
265
+	.udi_cdc_data  = UDI_CDC_DATA_DESC_0_HS, \
266
+	.udi_msc       = UDI_MSC_DESC_HS
267
+
268
+//! USB Interface APIs
269
+#define UDI_COMPOSITE_API \
270
+	&udi_api_cdc_comm, \
271
+	&udi_api_cdc_data, \
272
+	&udi_api_msc
273
+//@}
274
+
275
+
276
+/**
277
+ * USB Device Driver Configuration
278
+ * @{
279
+ */
280
+//@}
281
+
282
+//! The includes of classes and other headers must be done at the end of this file to avoid compile error
283
+#include "udi_cdc.h"
284
+#include "udi_msc.h"
285
+#include "usb_task.h"
286
+
287
+#endif // _CONF_USB_H_

+ 646
- 0
Marlin/src/HAL/HAL_DUE/usb/ctrl_access.c View File

@@ -0,0 +1,646 @@
1
+/*****************************************************************************
2
+ *
3
+ * \file
4
+ *
5
+ * \brief Abstraction layer for memory interfaces.
6
+ *
7
+ * This module contains the interfaces:
8
+ *   - MEM <-> USB;
9
+ *   - MEM <-> RAM;
10
+ *   - MEM <-> MEM.
11
+ *
12
+ * This module may be configured and expanded to support the following features:
13
+ *   - write-protected globals;
14
+ *   - password-protected data;
15
+ *   - specific features;
16
+ *   - etc.
17
+ *
18
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
19
+ *
20
+ * \asf_license_start
21
+ *
22
+ * \page License
23
+ *
24
+ * Redistribution and use in source and binary forms, with or without
25
+ * modification, are permitted provided that the following conditions are met:
26
+ *
27
+ * 1. Redistributions of source code must retain the above copyright notice,
28
+ *    this list of conditions and the following disclaimer.
29
+ *
30
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
31
+ *    this list of conditions and the following disclaimer in the documentation
32
+ *    and/or other materials provided with the distribution.
33
+ *
34
+ * 3. The name of Atmel may not be used to endorse or promote products derived
35
+ *    from this software without specific prior written permission.
36
+ *
37
+ * 4. This software may only be redistributed and used in connection with an
38
+ *    Atmel microcontroller product.
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
41
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
43
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
44
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
49
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
+ * POSSIBILITY OF SUCH DAMAGE.
51
+ *
52
+ * \asf_license_stop
53
+ *
54
+ ******************************************************************************/
55
+/*
56
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
57
+ */
58
+
59
+#ifdef ARDUINO_ARCH_SAM
60
+
61
+//_____ I N C L U D E S ____________________________________________________
62
+
63
+#include "compiler.h"
64
+#include "preprocessor.h"
65
+#ifdef FREERTOS_USED
66
+#include "FreeRTOS.h"
67
+#include "semphr.h"
68
+#endif
69
+#include "ctrl_access.h"
70
+
71
+
72
+//_____ D E F I N I T I O N S ______________________________________________
73
+
74
+#ifdef FREERTOS_USED
75
+
76
+/*! \name LUN Access Protection Macros
77
+ */
78
+//! @{
79
+
80
+/*! \brief Locks accesses to LUNs.
81
+ *
82
+ * \return \c true if the access was successfully locked, else \c false.
83
+ */
84
+#define Ctrl_access_lock()    ctrl_access_lock()
85
+
86
+/*! \brief Unlocks accesses to LUNs.
87
+ */
88
+#define Ctrl_access_unlock()  xSemaphoreGive(ctrl_access_semphr)
89
+
90
+//! @}
91
+
92
+//! Handle to the semaphore protecting accesses to LUNs.
93
+static xSemaphoreHandle ctrl_access_semphr = NULL;
94
+
95
+#else
96
+
97
+/*! \name LUN Access Protection Macros
98
+ */
99
+//! @{
100
+
101
+/*! \brief Locks accesses to LUNs.
102
+ *
103
+ * \return \c true if the access was successfully locked, else \c false.
104
+ */
105
+#define Ctrl_access_lock()    true
106
+
107
+/*! \brief Unlocks accesses to LUNs.
108
+ */
109
+#define Ctrl_access_unlock()
110
+
111
+//! @}
112
+
113
+#endif  // FREERTOS_USED
114
+
115
+
116
+#if MAX_LUN
117
+
118
+/*! \brief Initializes an entry of the LUN descriptor table.
119
+ *
120
+ * \param lun Logical Unit Number.
121
+ *
122
+ * \return LUN descriptor table entry initializer.
123
+ */
124
+#if ACCESS_USB == true && ACCESS_MEM_TO_RAM == true
125
+#define Lun_desc_entry(lun) \
126
+  {\
127
+    TPASTE3(Lun_, lun, _test_unit_ready),\
128
+    TPASTE3(Lun_, lun, _read_capacity),\
129
+    TPASTE3(Lun_, lun, _unload),\
130
+    TPASTE3(Lun_, lun, _wr_protect),\
131
+    TPASTE3(Lun_, lun, _removal),\
132
+    TPASTE3(Lun_, lun, _usb_read_10),\
133
+    TPASTE3(Lun_, lun, _usb_write_10),\
134
+    TPASTE3(Lun_, lun, _mem_2_ram),\
135
+    TPASTE3(Lun_, lun, _ram_2_mem),\
136
+    TPASTE3(LUN_, lun, _NAME)\
137
+  }
138
+#elif ACCESS_USB == true
139
+#define Lun_desc_entry(lun) \
140
+  {\
141
+    TPASTE3(Lun_, lun, _test_unit_ready),\
142
+    TPASTE3(Lun_, lun, _read_capacity),\
143
+    TPASTE3(Lun_, lun, _unload),\
144
+    TPASTE3(Lun_, lun, _wr_protect),\
145
+    TPASTE3(Lun_, lun, _removal),\
146
+    TPASTE3(Lun_, lun, _usb_read_10),\
147
+    TPASTE3(Lun_, lun, _usb_write_10),\
148
+    TPASTE3(LUN_, lun, _NAME)\
149
+  }
150
+#elif ACCESS_MEM_TO_RAM == true
151
+#define Lun_desc_entry(lun) \
152
+  {\
153
+    TPASTE3(Lun_, lun, _test_unit_ready),\
154
+    TPASTE3(Lun_, lun, _read_capacity),\
155
+    TPASTE3(Lun_, lun, _unload),\
156
+    TPASTE3(Lun_, lun, _wr_protect),\
157
+    TPASTE3(Lun_, lun, _removal),\
158
+    TPASTE3(Lun_, lun, _mem_2_ram),\
159
+    TPASTE3(Lun_, lun, _ram_2_mem),\
160
+    TPASTE3(LUN_, lun, _NAME)\
161
+  }
162
+#else
163
+#define Lun_desc_entry(lun) \
164
+  {\
165
+    TPASTE3(Lun_, lun, _test_unit_ready),\
166
+    TPASTE3(Lun_, lun, _read_capacity),\
167
+    TPASTE3(Lun_, lun, _unload),\
168
+    TPASTE3(Lun_, lun, _wr_protect),\
169
+    TPASTE3(Lun_, lun, _removal),\
170
+    TPASTE3(LUN_, lun, _NAME)\
171
+  }
172
+#endif
173
+
174
+//! LUN descriptor table.
175
+static const struct
176
+{
177
+  Ctrl_status (*test_unit_ready)(void);
178
+  Ctrl_status (*read_capacity)(U32 *);
179
+  bool (*unload)(bool);
180
+  bool (*wr_protect)(void);
181
+  bool (*removal)(void);
182
+#if ACCESS_USB == true
183
+  Ctrl_status (*usb_read_10)(U32, U16);
184
+  Ctrl_status (*usb_write_10)(U32, U16);
185
+#endif
186
+#if ACCESS_MEM_TO_RAM == true
187
+  Ctrl_status (*mem_2_ram)(U32, void *);
188
+  Ctrl_status (*ram_2_mem)(U32, const void *);
189
+#endif
190
+  const char *name;
191
+} lun_desc[MAX_LUN] =
192
+{
193
+#if LUN_0 == ENABLE
194
+# ifndef Lun_0_unload
195
+#  define Lun_0_unload NULL
196
+# endif
197
+  Lun_desc_entry(0),
198
+#endif
199
+#if LUN_1 == ENABLE
200
+# ifndef Lun_1_unload
201
+#  define Lun_1_unload NULL
202
+# endif
203
+  Lun_desc_entry(1),
204
+#endif
205
+#if LUN_2 == ENABLE
206
+# ifndef Lun_2_unload
207
+#  define Lun_2_unload NULL
208
+# endif
209
+  Lun_desc_entry(2),
210
+#endif
211
+#if LUN_3 == ENABLE
212
+# ifndef Lun_3_unload
213
+#  define Lun_3_unload NULL
214
+# endif
215
+  Lun_desc_entry(3),
216
+#endif
217
+#if LUN_4 == ENABLE
218
+# ifndef Lun_4_unload
219
+#  define Lun_4_unload NULL
220
+# endif
221
+  Lun_desc_entry(4),
222
+#endif
223
+#if LUN_5 == ENABLE
224
+# ifndef Lun_5_unload
225
+#  define Lun_5_unload NULL
226
+# endif
227
+  Lun_desc_entry(5),
228
+#endif
229
+#if LUN_6 == ENABLE
230
+# ifndef Lun_6_unload
231
+#  define Lun_6_unload NULL
232
+# endif
233
+  Lun_desc_entry(6),
234
+#endif
235
+#if LUN_7 == ENABLE
236
+# ifndef Lun_7_unload
237
+#  define Lun_7_unload NULL
238
+# endif
239
+  Lun_desc_entry(7)
240
+#endif
241
+};
242
+
243
+#endif
244
+
245
+
246
+#if GLOBAL_WR_PROTECT == true
247
+bool g_wr_protect;
248
+#endif
249
+
250
+
251
+/*! \name Control Interface
252
+ */
253
+//! @{
254
+
255
+
256
+#ifdef FREERTOS_USED
257
+
258
+bool ctrl_access_init(void)
259
+{
260
+  // If the handle to the protecting semaphore is not valid,
261
+  if (!ctrl_access_semphr)
262
+  {
263
+    // try to create the semaphore.
264
+    vSemaphoreCreateBinary(ctrl_access_semphr);
265
+
266
+    // If the semaphore could not be created, there is no backup solution.
267
+    if (!ctrl_access_semphr) return false;
268
+  }
269
+
270
+  return true;
271
+}
272
+
273
+
274
+/*! \brief Locks accesses to LUNs.
275
+ *
276
+ * \return \c true if the access was successfully locked, else \c false.
277
+ */
278
+static bool ctrl_access_lock(void)
279
+{
280
+  // If the semaphore could not be created, there is no backup solution.
281
+  if (!ctrl_access_semphr) return false;
282
+
283
+  // Wait for the semaphore.
284
+  while (!xSemaphoreTake(ctrl_access_semphr, portMAX_DELAY));
285
+
286
+  return true;
287
+}
288
+
289
+#endif  // FREERTOS_USED
290
+
291
+
292
+U8 get_nb_lun(void)
293
+{
294
+#if MEM_USB == ENABLE
295
+#  ifndef Lun_usb_get_lun
296
+#    define Lun_usb_get_lun()  host_get_lun()
297
+#  endif
298
+  U8 nb_lun;
299
+
300
+  if (!Ctrl_access_lock()) return MAX_LUN;
301
+
302
+  nb_lun = MAX_LUN + Lun_usb_get_lun();
303
+
304
+  Ctrl_access_unlock();
305
+
306
+  return nb_lun;
307
+#else
308
+  return MAX_LUN;
309
+#endif
310
+}
311
+
312
+
313
+U8 get_cur_lun(void)
314
+{
315
+  return LUN_ID_0;
316
+}
317
+
318
+
319
+Ctrl_status mem_test_unit_ready(U8 lun)
320
+{
321
+  Ctrl_status status;
322
+
323
+  if (!Ctrl_access_lock()) return CTRL_FAIL;
324
+
325
+  status =
326
+#if MAX_LUN
327
+         (lun < MAX_LUN) ? lun_desc[lun].test_unit_ready() :
328
+#endif
329
+#if LUN_USB == ENABLE
330
+                             Lun_usb_test_unit_ready(lun - LUN_ID_USB);
331
+#else
332
+                             CTRL_FAIL;
333
+#endif
334
+
335
+  Ctrl_access_unlock();
336
+
337
+  return status;
338
+}
339
+
340
+
341
+Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector)
342
+{
343
+  Ctrl_status status;
344
+
345
+  if (!Ctrl_access_lock()) return CTRL_FAIL;
346
+
347
+  status =
348
+#if MAX_LUN
349
+         (lun < MAX_LUN) ? lun_desc[lun].read_capacity(u32_nb_sector) :
350
+#endif
351
+#if LUN_USB == ENABLE
352
+                             Lun_usb_read_capacity(lun - LUN_ID_USB, u32_nb_sector);
353
+#else
354
+                             CTRL_FAIL;
355
+#endif
356
+
357
+  Ctrl_access_unlock();
358
+
359
+  return status;
360
+}
361
+
362
+
363
+U8 mem_sector_size(U8 lun)
364
+{
365
+  U8 sector_size;
366
+
367
+  if (!Ctrl_access_lock()) return 0;
368
+
369
+  sector_size =
370
+#if MAX_LUN
371
+              (lun < MAX_LUN) ? 1 :
372
+#endif
373
+#if LUN_USB == ENABLE
374
+                                  Lun_usb_read_sector_size(lun - LUN_ID_USB);
375
+#else
376
+                                  0;
377
+#endif
378
+
379
+  Ctrl_access_unlock();
380
+
381
+  return sector_size;
382
+}
383
+
384
+
385
+bool mem_unload(U8 lun, bool unload)
386
+{
387
+  bool unloaded;
388
+#if !MAX_LUN || !defined(Lun_usb_unload)
389
+  UNUSED(lun);
390
+#endif
391
+
392
+  if (!Ctrl_access_lock()) return false;
393
+
394
+  unloaded =
395
+#if MAX_LUN
396
+          (lun < MAX_LUN) ?
397
+              (lun_desc[lun].unload ?
398
+                  lun_desc[lun].unload(unload) : !unload) :
399
+#endif
400
+#if LUN_USB == ENABLE
401
+# if defined(Lun_usb_unload)
402
+              Lun_usb_unload(lun - LUN_ID_USB, unload);
403
+# else
404
+              !unload; /* Can not unload: load success, unload fail */
405
+# endif
406
+#else
407
+              false; /* No mem, unload/load fail */
408
+#endif
409
+
410
+  Ctrl_access_unlock();
411
+
412
+  return unloaded;
413
+}
414
+
415
+bool mem_wr_protect(U8 lun)
416
+{
417
+  bool wr_protect;
418
+
419
+  if (!Ctrl_access_lock()) return true;
420
+
421
+  wr_protect =
422
+#if MAX_LUN
423
+             (lun < MAX_LUN) ? lun_desc[lun].wr_protect() :
424
+#endif
425
+#if LUN_USB == ENABLE
426
+                                 Lun_usb_wr_protect(lun - LUN_ID_USB);
427
+#else
428
+                                 true;
429
+#endif
430
+
431
+  Ctrl_access_unlock();
432
+
433
+  return wr_protect;
434
+}
435
+
436
+
437
+bool mem_removal(U8 lun)
438
+{
439
+  bool removal;
440
+#if MAX_LUN==0
441
+  UNUSED(lun);
442
+#endif
443
+
444
+  if (!Ctrl_access_lock()) return true;
445
+
446
+  removal =
447
+#if MAX_LUN
448
+          (lun < MAX_LUN) ? lun_desc[lun].removal() :
449
+#endif
450
+#if LUN_USB == ENABLE
451
+                              Lun_usb_removal();
452
+#else
453
+                              true;
454
+#endif
455
+
456
+  Ctrl_access_unlock();
457
+
458
+  return removal;
459
+}
460
+
461
+
462
+const char *mem_name(U8 lun)
463
+{
464
+#if MAX_LUN==0
465
+  UNUSED(lun);
466
+#endif
467
+  return
468
+#if MAX_LUN
469
+       (lun < MAX_LUN) ? lun_desc[lun].name :
470
+#endif
471
+#if LUN_USB == ENABLE
472
+                           LUN_USB_NAME;
473
+#else
474
+                           NULL;
475
+#endif
476
+}
477
+
478
+
479
+//! @}
480
+
481
+
482
+#if ACCESS_USB == true
483
+
484
+/*! \name MEM <-> USB Interface
485
+ */
486
+//! @{
487
+
488
+
489
+Ctrl_status memory_2_usb(U8 lun, U32 addr, U16 nb_sector)
490
+{
491
+  Ctrl_status status;
492
+
493
+  if (!Ctrl_access_lock()) return CTRL_FAIL;
494
+
495
+  memory_start_read_action(nb_sector);
496
+  status =
497
+#if MAX_LUN
498
+           (lun < MAX_LUN) ? lun_desc[lun].usb_read_10(addr, nb_sector) :
499
+#endif
500
+                             CTRL_FAIL;
501
+  memory_stop_read_action();
502
+
503
+  Ctrl_access_unlock();
504
+
505
+  return status;
506
+}
507
+
508
+
509
+Ctrl_status usb_2_memory(U8 lun, U32 addr, U16 nb_sector)
510
+{
511
+  Ctrl_status status;
512
+
513
+  if (!Ctrl_access_lock()) return CTRL_FAIL;
514
+
515
+  memory_start_write_action(nb_sector);
516
+  status =
517
+#if MAX_LUN
518
+           (lun < MAX_LUN) ? lun_desc[lun].usb_write_10(addr, nb_sector) :
519
+#endif
520
+                             CTRL_FAIL;
521
+  memory_stop_write_action();
522
+
523
+  Ctrl_access_unlock();
524
+
525
+  return status;
526
+}
527
+
528
+
529
+//! @}
530
+
531
+#endif  // ACCESS_USB == true
532
+
533
+
534
+#if ACCESS_MEM_TO_RAM == true
535
+
536
+/*! \name MEM <-> RAM Interface
537
+ */
538
+//! @{
539
+
540
+
541
+Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram)
542
+{
543
+  Ctrl_status status;
544
+#if MAX_LUN==0
545
+  UNUSED(lun);
546
+#endif
547
+
548
+  if (!Ctrl_access_lock()) return CTRL_FAIL;
549
+
550
+  memory_start_read_action(1);
551
+  status =
552
+#if MAX_LUN
553
+           (lun < MAX_LUN) ? lun_desc[lun].mem_2_ram(addr, ram) :
554
+#endif
555
+#if LUN_USB == ENABLE
556
+                             Lun_usb_mem_2_ram(addr, ram);
557
+#else
558
+                             CTRL_FAIL;
559
+#endif
560
+  memory_stop_read_action();
561
+
562
+  Ctrl_access_unlock();
563
+
564
+  return status;
565
+}
566
+
567
+
568
+Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram)
569
+{
570
+  Ctrl_status status;
571
+#if MAX_LUN==0
572
+  UNUSED(lun);
573
+#endif
574
+
575
+  if (!Ctrl_access_lock()) return CTRL_FAIL;
576
+
577
+  memory_start_write_action(1);
578
+  status =
579
+#if MAX_LUN
580
+           (lun < MAX_LUN) ? lun_desc[lun].ram_2_mem(addr, ram) :
581
+#endif
582
+#if LUN_USB == ENABLE
583
+                             Lun_usb_ram_2_mem(addr, ram);
584
+#else
585
+                             CTRL_FAIL;
586
+#endif
587
+  memory_stop_write_action();
588
+
589
+  Ctrl_access_unlock();
590
+
591
+  return status;
592
+}
593
+
594
+
595
+//! @}
596
+
597
+#endif  // ACCESS_MEM_TO_RAM == true
598
+
599
+
600
+#if ACCESS_STREAM == true
601
+
602
+/*! \name Streaming MEM <-> MEM Interface
603
+ */
604
+//! @{
605
+
606
+
607
+  #if ACCESS_MEM_TO_MEM == true
608
+
609
+#include "fat.h"
610
+
611
+Ctrl_status stream_mem_to_mem(U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector)
612
+{
613
+  COMPILER_ALIGNED(4)
614
+  static U8 sector_buf[FS_512B];
615
+  Ctrl_status status = CTRL_GOOD;
616
+
617
+  while (nb_sector--)
618
+  {
619
+    if ((status = memory_2_ram(src_lun, src_addr++, sector_buf)) != CTRL_GOOD) break;
620
+    if ((status = ram_2_memory(dest_lun, dest_addr++, sector_buf)) != CTRL_GOOD) break;
621
+  }
622
+
623
+  return status;
624
+}
625
+
626
+  #endif  // ACCESS_MEM_TO_MEM == true
627
+
628
+
629
+Ctrl_status stream_state(U8 id)
630
+{
631
+  UNUSED(id);
632
+  return CTRL_GOOD;
633
+}
634
+
635
+
636
+U16 stream_stop(U8 id)
637
+{
638
+  UNUSED(id);
639
+  return 0;
640
+}
641
+
642
+
643
+//! @}
644
+
645
+#endif  // ACCESS_STREAM == true
646
+#endif

+ 402
- 0
Marlin/src/HAL/HAL_DUE/usb/ctrl_access.h View File

@@ -0,0 +1,402 @@
1
+/*****************************************************************************
2
+ *
3
+ * \file
4
+ *
5
+ * \brief Abstraction layer for memory interfaces.
6
+ *
7
+ * This module contains the interfaces:
8
+ *   - MEM <-> USB;
9
+ *   - MEM <-> RAM;
10
+ *   - MEM <-> MEM.
11
+ *
12
+ * This module may be configured and expanded to support the following features:
13
+ *   - write-protected globals;
14
+ *   - password-protected data;
15
+ *   - specific features;
16
+ *   - etc.
17
+ *
18
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
19
+ *
20
+ * \asf_license_start
21
+ *
22
+ * \page License
23
+ *
24
+ * Redistribution and use in source and binary forms, with or without
25
+ * modification, are permitted provided that the following conditions are met:
26
+ *
27
+ * 1. Redistributions of source code must retain the above copyright notice,
28
+ *    this list of conditions and the following disclaimer.
29
+ *
30
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
31
+ *    this list of conditions and the following disclaimer in the documentation
32
+ *    and/or other materials provided with the distribution.
33
+ *
34
+ * 3. The name of Atmel may not be used to endorse or promote products derived
35
+ *    from this software without specific prior written permission.
36
+ *
37
+ * 4. This software may only be redistributed and used in connection with an
38
+ *    Atmel microcontroller product.
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
41
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
43
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
44
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
49
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
+ * POSSIBILITY OF SUCH DAMAGE.
51
+ *
52
+ * \asf_license_stop
53
+ *
54
+ ******************************************************************************/
55
+/*
56
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
57
+ */
58
+
59
+
60
+#ifndef _CTRL_ACCESS_H_
61
+#define _CTRL_ACCESS_H_
62
+
63
+#ifdef __cplusplus
64
+extern "C" {
65
+#endif
66
+
67
+/**
68
+ * \defgroup group_common_services_storage_ctrl_access Memory Control Access
69
+ *
70
+ * Common abstraction layer for memory interfaces. It provides interfaces between:
71
+ * Memory and USB, Memory and RAM, Memory and Memory. Common API for XMEGA and UC3.
72
+ *
73
+ * \{
74
+ */
75
+
76
+#include "compiler.h"
77
+#include "conf_access.h"
78
+
79
+#ifndef SECTOR_SIZE
80
+#define SECTOR_SIZE  512
81
+#endif
82
+
83
+//! Status returned by CTRL_ACCESS interfaces.
84
+typedef enum
85
+{
86
+  CTRL_GOOD       = PASS,     //!< Success, memory ready.
87
+  CTRL_FAIL       = FAIL,     //!< An error occurred.
88
+  CTRL_NO_PRESENT = FAIL + 1, //!< Memory unplugged.
89
+  CTRL_BUSY       = FAIL + 2  //!< Memory not initialized or changed.
90
+} Ctrl_status;
91
+
92
+
93
+// FYI: Each Logical Unit Number (LUN) corresponds to a memory.
94
+
95
+// Check LUN defines.
96
+#ifndef LUN_0
97
+  #error LUN_0 must be defined as ENABLE or DISABLE in conf_access.h
98
+#endif
99
+#ifndef LUN_1
100
+  #error LUN_1 must be defined as ENABLE or DISABLE in conf_access.h
101
+#endif
102
+#ifndef LUN_2
103
+  #error LUN_2 must be defined as ENABLE or DISABLE in conf_access.h
104
+#endif
105
+#ifndef LUN_3
106
+  #error LUN_3 must be defined as ENABLE or DISABLE in conf_access.h
107
+#endif
108
+#ifndef LUN_4
109
+  #error LUN_4 must be defined as ENABLE or DISABLE in conf_access.h
110
+#endif
111
+#ifndef LUN_5
112
+  #error LUN_5 must be defined as ENABLE or DISABLE in conf_access.h
113
+#endif
114
+#ifndef LUN_6
115
+  #error LUN_6 must be defined as ENABLE or DISABLE in conf_access.h
116
+#endif
117
+#ifndef LUN_7
118
+  #error LUN_7 must be defined as ENABLE or DISABLE in conf_access.h
119
+#endif
120
+#ifndef LUN_USB
121
+  #error LUN_USB must be defined as ENABLE or DISABLE in conf_access.h
122
+#endif
123
+
124
+/*! \name LUN IDs
125
+ */
126
+//! @{
127
+#define LUN_ID_0        (0)                 //!< First static LUN.
128
+#define LUN_ID_1        (LUN_ID_0 + LUN_0)
129
+#define LUN_ID_2        (LUN_ID_1 + LUN_1)
130
+#define LUN_ID_3        (LUN_ID_2 + LUN_2)
131
+#define LUN_ID_4        (LUN_ID_3 + LUN_3)
132
+#define LUN_ID_5        (LUN_ID_4 + LUN_4)
133
+#define LUN_ID_6        (LUN_ID_5 + LUN_5)
134
+#define LUN_ID_7        (LUN_ID_6 + LUN_6)
135
+#define MAX_LUN         (LUN_ID_7 + LUN_7)  //!< Number of static LUNs.
136
+#define LUN_ID_USB      (MAX_LUN)           //!< First dynamic LUN (USB host mass storage).
137
+//! @}
138
+
139
+
140
+// Include LUN header files.
141
+#if LUN_0 == ENABLE
142
+  #include LUN_0_INCLUDE
143
+#endif
144
+#if LUN_1 == ENABLE
145
+  #include LUN_1_INCLUDE
146
+#endif
147
+#if LUN_2 == ENABLE
148
+  #include LUN_2_INCLUDE
149
+#endif
150
+#if LUN_3 == ENABLE
151
+  #include LUN_3_INCLUDE
152
+#endif
153
+#if LUN_4 == ENABLE
154
+  #include LUN_4_INCLUDE
155
+#endif
156
+#if LUN_5 == ENABLE
157
+  #include LUN_5_INCLUDE
158
+#endif
159
+#if LUN_6 == ENABLE
160
+  #include LUN_6_INCLUDE
161
+#endif
162
+#if LUN_7 == ENABLE
163
+  #include LUN_7_INCLUDE
164
+#endif
165
+#if LUN_USB == ENABLE
166
+  #include LUN_USB_INCLUDE
167
+#endif
168
+
169
+
170
+// Check the configuration of write protection in conf_access.h.
171
+#ifndef GLOBAL_WR_PROTECT
172
+  #error GLOBAL_WR_PROTECT must be defined as true or false in conf_access.h
173
+#endif
174
+
175
+
176
+#if GLOBAL_WR_PROTECT == true
177
+
178
+//! Write protect.
179
+extern bool g_wr_protect;
180
+
181
+#endif
182
+
183
+
184
+/*! \name Control Interface
185
+ */
186
+//! @{
187
+
188
+#ifdef FREERTOS_USED
189
+
190
+/*! \brief Initializes the LUN access locker.
191
+ *
192
+ * \return \c true if the locker was successfully initialized, else \c false.
193
+ */
194
+extern bool ctrl_access_init(void);
195
+
196
+#endif  // FREERTOS_USED
197
+
198
+/*! \brief Returns the number of LUNs.
199
+ *
200
+ * \return Number of LUNs in the system.
201
+ */
202
+extern U8 get_nb_lun(void);
203
+
204
+/*! \brief Returns the current LUN.
205
+ *
206
+ * \return Current LUN.
207
+ *
208
+ * \todo Implement.
209
+ */
210
+extern U8 get_cur_lun(void);
211
+
212
+/*! \brief Tests the memory state and initializes the memory if required.
213
+ *
214
+ * The TEST UNIT READY SCSI primary command allows an application client to poll
215
+ * a LUN until it is ready without having to allocate memory for returned data.
216
+ *
217
+ * This command may be used to check the media status of LUNs with removable
218
+ * media.
219
+ *
220
+ * \param lun Logical Unit Number.
221
+ *
222
+ * \return Status.
223
+ */
224
+extern Ctrl_status mem_test_unit_ready(U8 lun);
225
+
226
+/*! \brief Returns the address of the last valid sector (512 bytes) in the
227
+ *         memory.
228
+ *
229
+ * \param lun           Logical Unit Number.
230
+ * \param u32_nb_sector Pointer to the address of the last valid sector.
231
+ *
232
+ * \return Status.
233
+ */
234
+extern Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector);
235
+
236
+/*! \brief Returns the size of the physical sector.
237
+ *
238
+ * \param lun Logical Unit Number.
239
+ *
240
+ * \return Sector size (unit: 512 bytes).
241
+ */
242
+extern U8 mem_sector_size(U8 lun);
243
+
244
+/*! \brief Unload/load the medium.
245
+ *
246
+ * \param lun Logical Unit Number.
247
+ * \param unload \c true to unload the medium, \c false to load the medium.
248
+ *
249
+ * \return \c true if unload/load success, else \c false.
250
+ */
251
+extern bool mem_unload(U8 lun, bool unload);
252
+
253
+/*! \brief Returns the write-protection state of the memory.
254
+ *
255
+ * \param lun Logical Unit Number.
256
+ *
257
+ * \return \c true if the memory is write-protected, else \c false.
258
+ *
259
+ * \note Only used by removable memories with hardware-specific write
260
+ *       protection.
261
+ */
262
+extern bool mem_wr_protect(U8 lun);
263
+
264
+/*! \brief Tells whether the memory is removable.
265
+ *
266
+ * \param lun Logical Unit Number.
267
+ *
268
+ * \return \c true if the memory is removable, else \c false.
269
+ */
270
+extern bool mem_removal(U8 lun);
271
+
272
+/*! \brief Returns a pointer to the LUN name.
273
+ *
274
+ * \param lun Logical Unit Number.
275
+ *
276
+ * \return Pointer to the LUN name string.
277
+ */
278
+extern const char *mem_name(U8 lun);
279
+
280
+//! @}
281
+
282
+
283
+#if ACCESS_USB == true
284
+
285
+/*! \name MEM <-> USB Interface
286
+ */
287
+//! @{
288
+
289
+/*! \brief Transfers data from the memory to USB.
290
+ *
291
+ * \param lun       Logical Unit Number.
292
+ * \param addr      Address of first memory sector to read.
293
+ * \param nb_sector Number of sectors to transfer.
294
+ *
295
+ * \return Status.
296
+ */
297
+extern Ctrl_status memory_2_usb(U8 lun, U32 addr, U16 nb_sector);
298
+
299
+/*! \brief Transfers data from USB to the memory.
300
+ *
301
+ * \param lun       Logical Unit Number.
302
+ * \param addr      Address of first memory sector to write.
303
+ * \param nb_sector Number of sectors to transfer.
304
+ *
305
+ * \return Status.
306
+ */
307
+extern Ctrl_status usb_2_memory(U8 lun, U32 addr, U16 nb_sector);
308
+
309
+//! @}
310
+
311
+#endif  // ACCESS_USB == true
312
+
313
+
314
+#if ACCESS_MEM_TO_RAM == true
315
+
316
+/*! \name MEM <-> RAM Interface
317
+ */
318
+//! @{
319
+
320
+/*! \brief Copies 1 data sector from the memory to RAM.
321
+ *
322
+ * \param lun   Logical Unit Number.
323
+ * \param addr  Address of first memory sector to read.
324
+ * \param ram   Pointer to RAM buffer to write.
325
+ *
326
+ * \return Status.
327
+ */
328
+extern Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram);
329
+
330
+/*! \brief Copies 1 data sector from RAM to the memory.
331
+ *
332
+ * \param lun   Logical Unit Number.
333
+ * \param addr  Address of first memory sector to write.
334
+ * \param ram   Pointer to RAM buffer to read.
335
+ *
336
+ * \return Status.
337
+ */
338
+extern Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram);
339
+
340
+//! @}
341
+
342
+#endif  // ACCESS_MEM_TO_RAM == true
343
+
344
+
345
+#if ACCESS_STREAM == true
346
+
347
+/*! \name Streaming MEM <-> MEM Interface
348
+ */
349
+//! @{
350
+
351
+//! Erroneous streaming data transfer ID.
352
+#define ID_STREAM_ERR         0xFF
353
+
354
+  #if ACCESS_MEM_TO_MEM == true
355
+
356
+/*! \brief Copies data from one memory to another.
357
+ *
358
+ * \param src_lun   Source Logical Unit Number.
359
+ * \param src_addr  Source address of first memory sector to read.
360
+ * \param dest_lun  Destination Logical Unit Number.
361
+ * \param dest_addr Destination address of first memory sector to write.
362
+ * \param nb_sector Number of sectors to copy.
363
+ *
364
+ * \return Status.
365
+ */
366
+extern Ctrl_status stream_mem_to_mem(U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector);
367
+
368
+  #endif  // ACCESS_MEM_TO_MEM == true
369
+
370
+/*! \brief Returns the state of a streaming data transfer.
371
+ *
372
+ * \param id  Transfer ID.
373
+ *
374
+ * \return Status.
375
+ *
376
+ * \todo Implement.
377
+ */
378
+extern Ctrl_status stream_state(U8 id);
379
+
380
+/*! \brief Stops a streaming data transfer.
381
+ *
382
+ * \param id  Transfer ID.
383
+ *
384
+ * \return Number of remaining sectors.
385
+ *
386
+ * \todo Implement.
387
+ */
388
+extern U16 stream_stop(U8 id);
389
+
390
+//! @}
391
+
392
+#endif  // ACCESS_STREAM == true
393
+
394
+/**
395
+ * \}
396
+ */
397
+
398
+#ifdef __cplusplus
399
+}
400
+#endif
401
+
402
+#endif  // _CTRL_ACCESS_H_

+ 278
- 0
Marlin/src/HAL/HAL_DUE/usb/genclk.h View File

@@ -0,0 +1,278 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Chip-specific generic clock management.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef CHIP_GENCLK_H_INCLUDED
48
+#define CHIP_GENCLK_H_INCLUDED
49
+
50
+#include <osc.h>
51
+#include <pll.h>
52
+
53
+/// @cond 0
54
+/**INDENT-OFF**/
55
+#ifdef __cplusplus
56
+extern "C" {
57
+#endif
58
+/**INDENT-ON**/
59
+/// @endcond
60
+
61
+/**
62
+ * \weakgroup genclk_group
63
+ * @{
64
+ */
65
+
66
+//! \name Programmable Clock Identifiers (PCK)
67
+//@{
68
+#define GENCLK_PCK_0      0 //!< PCK0 ID
69
+#define GENCLK_PCK_1      1 //!< PCK1 ID
70
+#define GENCLK_PCK_2      2 //!< PCK2 ID
71
+//@}
72
+
73
+//! \name Programmable Clock Sources (PCK)
74
+//@{
75
+
76
+enum genclk_source {
77
+	GENCLK_PCK_SRC_SLCK_RC       = 0, //!< Internal 32kHz RC oscillator as PCK source clock
78
+	GENCLK_PCK_SRC_SLCK_XTAL     = 1, //!< External 32kHz crystal oscillator as PCK source clock
79
+	GENCLK_PCK_SRC_SLCK_BYPASS   = 2, //!< External 32kHz bypass oscillator as PCK source clock
80
+	GENCLK_PCK_SRC_MAINCK_4M_RC  = 3, //!< Internal 4MHz RC oscillator as PCK source clock
81
+	GENCLK_PCK_SRC_MAINCK_8M_RC  = 4, //!< Internal 8MHz RC oscillator as PCK source clock
82
+	GENCLK_PCK_SRC_MAINCK_12M_RC = 5, //!< Internal 12MHz RC oscillator as PCK source clock
83
+	GENCLK_PCK_SRC_MAINCK_XTAL   = 6, //!< External crystal oscillator as PCK source clock
84
+	GENCLK_PCK_SRC_MAINCK_BYPASS = 7, //!< External bypass oscillator as PCK source clock
85
+	GENCLK_PCK_SRC_PLLACK        = 8, //!< Use PLLACK as PCK source clock
86
+	GENCLK_PCK_SRC_PLLBCK        = 9, //!< Use PLLBCK as PCK source clock
87
+	GENCLK_PCK_SRC_MCK           = 10, //!< Use Master Clk as PCK source clock
88
+};
89
+
90
+//@}
91
+
92
+//! \name Programmable Clock Prescalers (PCK)
93
+//@{
94
+
95
+enum genclk_divider {
96
+	GENCLK_PCK_PRES_1  = PMC_PCK_PRES_CLK_1, //!< Set PCK clock prescaler to 1
97
+	GENCLK_PCK_PRES_2  = PMC_PCK_PRES_CLK_2, //!< Set PCK clock prescaler to 2
98
+	GENCLK_PCK_PRES_4  = PMC_PCK_PRES_CLK_4, //!< Set PCK clock prescaler to 4
99
+	GENCLK_PCK_PRES_8  = PMC_PCK_PRES_CLK_8, //!< Set PCK clock prescaler to 8
100
+	GENCLK_PCK_PRES_16 = PMC_PCK_PRES_CLK_16, //!< Set PCK clock prescaler to 16
101
+	GENCLK_PCK_PRES_32 = PMC_PCK_PRES_CLK_32, //!< Set PCK clock prescaler to 32
102
+	GENCLK_PCK_PRES_64 = PMC_PCK_PRES_CLK_64, //!< Set PCK clock prescaler to 64
103
+};
104
+
105
+//@}
106
+
107
+struct genclk_config {
108
+	uint32_t ctrl;
109
+};
110
+
111
+static inline void genclk_config_defaults(struct genclk_config *p_cfg,
112
+		uint32_t ul_id)
113
+{
114
+	ul_id = ul_id;
115
+	p_cfg->ctrl = 0;
116
+}
117
+
118
+static inline void genclk_config_read(struct genclk_config *p_cfg,
119
+		uint32_t ul_id)
120
+{
121
+	p_cfg->ctrl = PMC->PMC_PCK[ul_id];
122
+}
123
+
124
+static inline void genclk_config_write(const struct genclk_config *p_cfg,
125
+		uint32_t ul_id)
126
+{
127
+	PMC->PMC_PCK[ul_id] = p_cfg->ctrl;
128
+}
129
+
130
+//! \name Programmable Clock Source and Prescaler configuration
131
+//@{
132
+
133
+static inline void genclk_config_set_source(struct genclk_config *p_cfg,
134
+		enum genclk_source e_src)
135
+{
136
+	p_cfg->ctrl &= (~PMC_PCK_CSS_Msk);
137
+
138
+	switch (e_src) {
139
+	case GENCLK_PCK_SRC_SLCK_RC:
140
+	case GENCLK_PCK_SRC_SLCK_XTAL:
141
+	case GENCLK_PCK_SRC_SLCK_BYPASS:
142
+		p_cfg->ctrl |= (PMC_PCK_CSS_SLOW_CLK);
143
+		break;
144
+
145
+	case GENCLK_PCK_SRC_MAINCK_4M_RC:
146
+	case GENCLK_PCK_SRC_MAINCK_8M_RC:
147
+	case GENCLK_PCK_SRC_MAINCK_12M_RC:
148
+	case GENCLK_PCK_SRC_MAINCK_XTAL:
149
+	case GENCLK_PCK_SRC_MAINCK_BYPASS:
150
+		p_cfg->ctrl |= (PMC_PCK_CSS_MAIN_CLK);
151
+		break;
152
+
153
+	case GENCLK_PCK_SRC_PLLACK:
154
+		p_cfg->ctrl |= (PMC_PCK_CSS_PLLA_CLK);
155
+		break;
156
+
157
+	case GENCLK_PCK_SRC_PLLBCK:
158
+		p_cfg->ctrl |= (PMC_PCK_CSS_UPLL_CLK);
159
+		break;
160
+
161
+	case GENCLK_PCK_SRC_MCK:
162
+		p_cfg->ctrl |= (PMC_PCK_CSS_MCK);
163
+		break;
164
+	}
165
+}
166
+
167
+static inline void genclk_config_set_divider(struct genclk_config *p_cfg,
168
+		uint32_t e_divider)
169
+{
170
+	p_cfg->ctrl &= ~PMC_PCK_PRES_Msk;
171
+	p_cfg->ctrl |= e_divider;
172
+}
173
+
174
+//@}
175
+
176
+static inline void genclk_enable(const struct genclk_config *p_cfg,
177
+		uint32_t ul_id)
178
+{
179
+	PMC->PMC_PCK[ul_id] = p_cfg->ctrl;
180
+	pmc_enable_pck(ul_id);
181
+}
182
+
183
+static inline void genclk_disable(uint32_t ul_id)
184
+{
185
+	pmc_disable_pck(ul_id);
186
+}
187
+
188
+static inline void genclk_enable_source(enum genclk_source e_src)
189
+{
190
+	switch (e_src) {
191
+	case GENCLK_PCK_SRC_SLCK_RC:
192
+		if (!osc_is_ready(OSC_SLCK_32K_RC)) {
193
+			osc_enable(OSC_SLCK_32K_RC);
194
+			osc_wait_ready(OSC_SLCK_32K_RC);
195
+		}
196
+		break;
197
+
198
+	case GENCLK_PCK_SRC_SLCK_XTAL:
199
+		if (!osc_is_ready(OSC_SLCK_32K_XTAL)) {
200
+			osc_enable(OSC_SLCK_32K_XTAL);
201
+			osc_wait_ready(OSC_SLCK_32K_XTAL);
202
+		}
203
+		break;
204
+
205
+	case GENCLK_PCK_SRC_SLCK_BYPASS:
206
+		if (!osc_is_ready(OSC_SLCK_32K_BYPASS)) {
207
+			osc_enable(OSC_SLCK_32K_BYPASS);
208
+			osc_wait_ready(OSC_SLCK_32K_BYPASS);
209
+		}
210
+		break;
211
+
212
+	case GENCLK_PCK_SRC_MAINCK_4M_RC:
213
+		if (!osc_is_ready(OSC_MAINCK_4M_RC)) {
214
+			osc_enable(OSC_MAINCK_4M_RC);
215
+			osc_wait_ready(OSC_MAINCK_4M_RC);
216
+		}
217
+		break;
218
+
219
+	case GENCLK_PCK_SRC_MAINCK_8M_RC:
220
+		if (!osc_is_ready(OSC_MAINCK_8M_RC)) {
221
+			osc_enable(OSC_MAINCK_8M_RC);
222
+			osc_wait_ready(OSC_MAINCK_8M_RC);
223
+		}
224
+		break;
225
+
226
+	case GENCLK_PCK_SRC_MAINCK_12M_RC:
227
+		if (!osc_is_ready(OSC_MAINCK_12M_RC)) {
228
+			osc_enable(OSC_MAINCK_12M_RC);
229
+			osc_wait_ready(OSC_MAINCK_12M_RC);
230
+		}
231
+		break;
232
+
233
+	case GENCLK_PCK_SRC_MAINCK_XTAL:
234
+		if (!osc_is_ready(OSC_MAINCK_XTAL)) {
235
+			osc_enable(OSC_MAINCK_XTAL);
236
+			osc_wait_ready(OSC_MAINCK_XTAL);
237
+		}
238
+		break;
239
+
240
+	case GENCLK_PCK_SRC_MAINCK_BYPASS:
241
+		if (!osc_is_ready(OSC_MAINCK_BYPASS)) {
242
+			osc_enable(OSC_MAINCK_BYPASS);
243
+			osc_wait_ready(OSC_MAINCK_BYPASS);
244
+		}
245
+		break;
246
+
247
+#ifdef CONFIG_PLL0_SOURCE
248
+	case GENCLK_PCK_SRC_PLLACK:
249
+		pll_enable_config_defaults(0);
250
+		break;
251
+#endif
252
+
253
+#ifdef CONFIG_PLL1_SOURCE
254
+	case GENCLK_PCK_SRC_PLLBCK:
255
+		pll_enable_config_defaults(1);
256
+		break;
257
+#endif
258
+
259
+	case GENCLK_PCK_SRC_MCK:
260
+		break;
261
+
262
+	default:
263
+		Assert(false);
264
+		break;
265
+	}
266
+}
267
+
268
+//! @}
269
+
270
+/// @cond 0
271
+/**INDENT-OFF**/
272
+#ifdef __cplusplus
273
+}
274
+#endif
275
+/**INDENT-ON**/
276
+/// @endcond
277
+
278
+#endif /* CHIP_GENCLK_H_INCLUDED */

+ 339
- 0
Marlin/src/HAL/HAL_DUE/usb/mrepeat.h View File

@@ -0,0 +1,339 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Preprocessor macro repeating utils.
5
+ *
6
+ * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _MREPEAT_H_
48
+#define _MREPEAT_H_
49
+
50
+/**
51
+ * \defgroup group_sam_utils_mrepeat Preprocessor - Macro Repeat
52
+ *
53
+ * \ingroup group_sam_utils
54
+ *
55
+ * \{
56
+ */
57
+
58
+#include "preprocessor.h"
59
+
60
+
61
+//! Maximal number of repetitions supported by MREPEAT.
62
+#define MREPEAT_LIMIT   256
63
+
64
+/*! \brief Macro repeat.
65
+ *
66
+ * This macro represents a horizontal repetition construct.
67
+ *
68
+ * \param count  The number of repetitious calls to macro. Valid values range from 0 to MREPEAT_LIMIT.
69
+ * \param macro  A binary operation of the form macro(n, data). This macro is expanded by MREPEAT with
70
+ *               the current repetition number and the auxiliary data argument.
71
+ * \param data   Auxiliary data passed to macro.
72
+ *
73
+ * \return       <tt>macro(0, data) macro(1, data) ... macro(count - 1, data)</tt>
74
+ */
75
+#define MREPEAT(count, macro, data)         TPASTE2(MREPEAT, count)(macro, data)
76
+
77
+#define MREPEAT0(  macro, data)
78
+#define MREPEAT1(  macro, data)       MREPEAT0(  macro, data)   macro(  0, data)
79
+#define MREPEAT2(  macro, data)       MREPEAT1(  macro, data)   macro(  1, data)
80
+#define MREPEAT3(  macro, data)       MREPEAT2(  macro, data)   macro(  2, data)
81
+#define MREPEAT4(  macro, data)       MREPEAT3(  macro, data)   macro(  3, data)
82
+#define MREPEAT5(  macro, data)       MREPEAT4(  macro, data)   macro(  4, data)
83
+#define MREPEAT6(  macro, data)       MREPEAT5(  macro, data)   macro(  5, data)
84
+#define MREPEAT7(  macro, data)       MREPEAT6(  macro, data)   macro(  6, data)
85
+#define MREPEAT8(  macro, data)       MREPEAT7(  macro, data)   macro(  7, data)
86
+#define MREPEAT9(  macro, data)       MREPEAT8(  macro, data)   macro(  8, data)
87
+#define MREPEAT10( macro, data)       MREPEAT9(  macro, data)   macro(  9, data)
88
+#define MREPEAT11( macro, data)       MREPEAT10( macro, data)   macro( 10, data)
89
+#define MREPEAT12( macro, data)       MREPEAT11( macro, data)   macro( 11, data)
90
+#define MREPEAT13( macro, data)       MREPEAT12( macro, data)   macro( 12, data)
91
+#define MREPEAT14( macro, data)       MREPEAT13( macro, data)   macro( 13, data)
92
+#define MREPEAT15( macro, data)       MREPEAT14( macro, data)   macro( 14, data)
93
+#define MREPEAT16( macro, data)       MREPEAT15( macro, data)   macro( 15, data)
94
+#define MREPEAT17( macro, data)       MREPEAT16( macro, data)   macro( 16, data)
95
+#define MREPEAT18( macro, data)       MREPEAT17( macro, data)   macro( 17, data)
96
+#define MREPEAT19( macro, data)       MREPEAT18( macro, data)   macro( 18, data)
97
+#define MREPEAT20( macro, data)       MREPEAT19( macro, data)   macro( 19, data)
98
+#define MREPEAT21( macro, data)       MREPEAT20( macro, data)   macro( 20, data)
99
+#define MREPEAT22( macro, data)       MREPEAT21( macro, data)   macro( 21, data)
100
+#define MREPEAT23( macro, data)       MREPEAT22( macro, data)   macro( 22, data)
101
+#define MREPEAT24( macro, data)       MREPEAT23( macro, data)   macro( 23, data)
102
+#define MREPEAT25( macro, data)       MREPEAT24( macro, data)   macro( 24, data)
103
+#define MREPEAT26( macro, data)       MREPEAT25( macro, data)   macro( 25, data)
104
+#define MREPEAT27( macro, data)       MREPEAT26( macro, data)   macro( 26, data)
105
+#define MREPEAT28( macro, data)       MREPEAT27( macro, data)   macro( 27, data)
106
+#define MREPEAT29( macro, data)       MREPEAT28( macro, data)   macro( 28, data)
107
+#define MREPEAT30( macro, data)       MREPEAT29( macro, data)   macro( 29, data)
108
+#define MREPEAT31( macro, data)       MREPEAT30( macro, data)   macro( 30, data)
109
+#define MREPEAT32( macro, data)       MREPEAT31( macro, data)   macro( 31, data)
110
+#define MREPEAT33( macro, data)       MREPEAT32( macro, data)   macro( 32, data)
111
+#define MREPEAT34( macro, data)       MREPEAT33( macro, data)   macro( 33, data)
112
+#define MREPEAT35( macro, data)       MREPEAT34( macro, data)   macro( 34, data)
113
+#define MREPEAT36( macro, data)       MREPEAT35( macro, data)   macro( 35, data)
114
+#define MREPEAT37( macro, data)       MREPEAT36( macro, data)   macro( 36, data)
115
+#define MREPEAT38( macro, data)       MREPEAT37( macro, data)   macro( 37, data)
116
+#define MREPEAT39( macro, data)       MREPEAT38( macro, data)   macro( 38, data)
117
+#define MREPEAT40( macro, data)       MREPEAT39( macro, data)   macro( 39, data)
118
+#define MREPEAT41( macro, data)       MREPEAT40( macro, data)   macro( 40, data)
119
+#define MREPEAT42( macro, data)       MREPEAT41( macro, data)   macro( 41, data)
120
+#define MREPEAT43( macro, data)       MREPEAT42( macro, data)   macro( 42, data)
121
+#define MREPEAT44( macro, data)       MREPEAT43( macro, data)   macro( 43, data)
122
+#define MREPEAT45( macro, data)       MREPEAT44( macro, data)   macro( 44, data)
123
+#define MREPEAT46( macro, data)       MREPEAT45( macro, data)   macro( 45, data)
124
+#define MREPEAT47( macro, data)       MREPEAT46( macro, data)   macro( 46, data)
125
+#define MREPEAT48( macro, data)       MREPEAT47( macro, data)   macro( 47, data)
126
+#define MREPEAT49( macro, data)       MREPEAT48( macro, data)   macro( 48, data)
127
+#define MREPEAT50( macro, data)       MREPEAT49( macro, data)   macro( 49, data)
128
+#define MREPEAT51( macro, data)       MREPEAT50( macro, data)   macro( 50, data)
129
+#define MREPEAT52( macro, data)       MREPEAT51( macro, data)   macro( 51, data)
130
+#define MREPEAT53( macro, data)       MREPEAT52( macro, data)   macro( 52, data)
131
+#define MREPEAT54( macro, data)       MREPEAT53( macro, data)   macro( 53, data)
132
+#define MREPEAT55( macro, data)       MREPEAT54( macro, data)   macro( 54, data)
133
+#define MREPEAT56( macro, data)       MREPEAT55( macro, data)   macro( 55, data)
134
+#define MREPEAT57( macro, data)       MREPEAT56( macro, data)   macro( 56, data)
135
+#define MREPEAT58( macro, data)       MREPEAT57( macro, data)   macro( 57, data)
136
+#define MREPEAT59( macro, data)       MREPEAT58( macro, data)   macro( 58, data)
137
+#define MREPEAT60( macro, data)       MREPEAT59( macro, data)   macro( 59, data)
138
+#define MREPEAT61( macro, data)       MREPEAT60( macro, data)   macro( 60, data)
139
+#define MREPEAT62( macro, data)       MREPEAT61( macro, data)   macro( 61, data)
140
+#define MREPEAT63( macro, data)       MREPEAT62( macro, data)   macro( 62, data)
141
+#define MREPEAT64( macro, data)       MREPEAT63( macro, data)   macro( 63, data)
142
+#define MREPEAT65( macro, data)       MREPEAT64( macro, data)   macro( 64, data)
143
+#define MREPEAT66( macro, data)       MREPEAT65( macro, data)   macro( 65, data)
144
+#define MREPEAT67( macro, data)       MREPEAT66( macro, data)   macro( 66, data)
145
+#define MREPEAT68( macro, data)       MREPEAT67( macro, data)   macro( 67, data)
146
+#define MREPEAT69( macro, data)       MREPEAT68( macro, data)   macro( 68, data)
147
+#define MREPEAT70( macro, data)       MREPEAT69( macro, data)   macro( 69, data)
148
+#define MREPEAT71( macro, data)       MREPEAT70( macro, data)   macro( 70, data)
149
+#define MREPEAT72( macro, data)       MREPEAT71( macro, data)   macro( 71, data)
150
+#define MREPEAT73( macro, data)       MREPEAT72( macro, data)   macro( 72, data)
151
+#define MREPEAT74( macro, data)       MREPEAT73( macro, data)   macro( 73, data)
152
+#define MREPEAT75( macro, data)       MREPEAT74( macro, data)   macro( 74, data)
153
+#define MREPEAT76( macro, data)       MREPEAT75( macro, data)   macro( 75, data)
154
+#define MREPEAT77( macro, data)       MREPEAT76( macro, data)   macro( 76, data)
155
+#define MREPEAT78( macro, data)       MREPEAT77( macro, data)   macro( 77, data)
156
+#define MREPEAT79( macro, data)       MREPEAT78( macro, data)   macro( 78, data)
157
+#define MREPEAT80( macro, data)       MREPEAT79( macro, data)   macro( 79, data)
158
+#define MREPEAT81( macro, data)       MREPEAT80( macro, data)   macro( 80, data)
159
+#define MREPEAT82( macro, data)       MREPEAT81( macro, data)   macro( 81, data)
160
+#define MREPEAT83( macro, data)       MREPEAT82( macro, data)   macro( 82, data)
161
+#define MREPEAT84( macro, data)       MREPEAT83( macro, data)   macro( 83, data)
162
+#define MREPEAT85( macro, data)       MREPEAT84( macro, data)   macro( 84, data)
163
+#define MREPEAT86( macro, data)       MREPEAT85( macro, data)   macro( 85, data)
164
+#define MREPEAT87( macro, data)       MREPEAT86( macro, data)   macro( 86, data)
165
+#define MREPEAT88( macro, data)       MREPEAT87( macro, data)   macro( 87, data)
166
+#define MREPEAT89( macro, data)       MREPEAT88( macro, data)   macro( 88, data)
167
+#define MREPEAT90( macro, data)       MREPEAT89( macro, data)   macro( 89, data)
168
+#define MREPEAT91( macro, data)       MREPEAT90( macro, data)   macro( 90, data)
169
+#define MREPEAT92( macro, data)       MREPEAT91( macro, data)   macro( 91, data)
170
+#define MREPEAT93( macro, data)       MREPEAT92( macro, data)   macro( 92, data)
171
+#define MREPEAT94( macro, data)       MREPEAT93( macro, data)   macro( 93, data)
172
+#define MREPEAT95( macro, data)       MREPEAT94( macro, data)   macro( 94, data)
173
+#define MREPEAT96( macro, data)       MREPEAT95( macro, data)   macro( 95, data)
174
+#define MREPEAT97( macro, data)       MREPEAT96( macro, data)   macro( 96, data)
175
+#define MREPEAT98( macro, data)       MREPEAT97( macro, data)   macro( 97, data)
176
+#define MREPEAT99( macro, data)       MREPEAT98( macro, data)   macro( 98, data)
177
+#define MREPEAT100(macro, data)       MREPEAT99( macro, data)   macro( 99, data)
178
+#define MREPEAT101(macro, data)       MREPEAT100(macro, data)   macro(100, data)
179
+#define MREPEAT102(macro, data)       MREPEAT101(macro, data)   macro(101, data)
180
+#define MREPEAT103(macro, data)       MREPEAT102(macro, data)   macro(102, data)
181
+#define MREPEAT104(macro, data)       MREPEAT103(macro, data)   macro(103, data)
182
+#define MREPEAT105(macro, data)       MREPEAT104(macro, data)   macro(104, data)
183
+#define MREPEAT106(macro, data)       MREPEAT105(macro, data)   macro(105, data)
184
+#define MREPEAT107(macro, data)       MREPEAT106(macro, data)   macro(106, data)
185
+#define MREPEAT108(macro, data)       MREPEAT107(macro, data)   macro(107, data)
186
+#define MREPEAT109(macro, data)       MREPEAT108(macro, data)   macro(108, data)
187
+#define MREPEAT110(macro, data)       MREPEAT109(macro, data)   macro(109, data)
188
+#define MREPEAT111(macro, data)       MREPEAT110(macro, data)   macro(110, data)
189
+#define MREPEAT112(macro, data)       MREPEAT111(macro, data)   macro(111, data)
190
+#define MREPEAT113(macro, data)       MREPEAT112(macro, data)   macro(112, data)
191
+#define MREPEAT114(macro, data)       MREPEAT113(macro, data)   macro(113, data)
192
+#define MREPEAT115(macro, data)       MREPEAT114(macro, data)   macro(114, data)
193
+#define MREPEAT116(macro, data)       MREPEAT115(macro, data)   macro(115, data)
194
+#define MREPEAT117(macro, data)       MREPEAT116(macro, data)   macro(116, data)
195
+#define MREPEAT118(macro, data)       MREPEAT117(macro, data)   macro(117, data)
196
+#define MREPEAT119(macro, data)       MREPEAT118(macro, data)   macro(118, data)
197
+#define MREPEAT120(macro, data)       MREPEAT119(macro, data)   macro(119, data)
198
+#define MREPEAT121(macro, data)       MREPEAT120(macro, data)   macro(120, data)
199
+#define MREPEAT122(macro, data)       MREPEAT121(macro, data)   macro(121, data)
200
+#define MREPEAT123(macro, data)       MREPEAT122(macro, data)   macro(122, data)
201
+#define MREPEAT124(macro, data)       MREPEAT123(macro, data)   macro(123, data)
202
+#define MREPEAT125(macro, data)       MREPEAT124(macro, data)   macro(124, data)
203
+#define MREPEAT126(macro, data)       MREPEAT125(macro, data)   macro(125, data)
204
+#define MREPEAT127(macro, data)       MREPEAT126(macro, data)   macro(126, data)
205
+#define MREPEAT128(macro, data)       MREPEAT127(macro, data)   macro(127, data)
206
+#define MREPEAT129(macro, data)       MREPEAT128(macro, data)   macro(128, data)
207
+#define MREPEAT130(macro, data)       MREPEAT129(macro, data)   macro(129, data)
208
+#define MREPEAT131(macro, data)       MREPEAT130(macro, data)   macro(130, data)
209
+#define MREPEAT132(macro, data)       MREPEAT131(macro, data)   macro(131, data)
210
+#define MREPEAT133(macro, data)       MREPEAT132(macro, data)   macro(132, data)
211
+#define MREPEAT134(macro, data)       MREPEAT133(macro, data)   macro(133, data)
212
+#define MREPEAT135(macro, data)       MREPEAT134(macro, data)   macro(134, data)
213
+#define MREPEAT136(macro, data)       MREPEAT135(macro, data)   macro(135, data)
214
+#define MREPEAT137(macro, data)       MREPEAT136(macro, data)   macro(136, data)
215
+#define MREPEAT138(macro, data)       MREPEAT137(macro, data)   macro(137, data)
216
+#define MREPEAT139(macro, data)       MREPEAT138(macro, data)   macro(138, data)
217
+#define MREPEAT140(macro, data)       MREPEAT139(macro, data)   macro(139, data)
218
+#define MREPEAT141(macro, data)       MREPEAT140(macro, data)   macro(140, data)
219
+#define MREPEAT142(macro, data)       MREPEAT141(macro, data)   macro(141, data)
220
+#define MREPEAT143(macro, data)       MREPEAT142(macro, data)   macro(142, data)
221
+#define MREPEAT144(macro, data)       MREPEAT143(macro, data)   macro(143, data)
222
+#define MREPEAT145(macro, data)       MREPEAT144(macro, data)   macro(144, data)
223
+#define MREPEAT146(macro, data)       MREPEAT145(macro, data)   macro(145, data)
224
+#define MREPEAT147(macro, data)       MREPEAT146(macro, data)   macro(146, data)
225
+#define MREPEAT148(macro, data)       MREPEAT147(macro, data)   macro(147, data)
226
+#define MREPEAT149(macro, data)       MREPEAT148(macro, data)   macro(148, data)
227
+#define MREPEAT150(macro, data)       MREPEAT149(macro, data)   macro(149, data)
228
+#define MREPEAT151(macro, data)       MREPEAT150(macro, data)   macro(150, data)
229
+#define MREPEAT152(macro, data)       MREPEAT151(macro, data)   macro(151, data)
230
+#define MREPEAT153(macro, data)       MREPEAT152(macro, data)   macro(152, data)
231
+#define MREPEAT154(macro, data)       MREPEAT153(macro, data)   macro(153, data)
232
+#define MREPEAT155(macro, data)       MREPEAT154(macro, data)   macro(154, data)
233
+#define MREPEAT156(macro, data)       MREPEAT155(macro, data)   macro(155, data)
234
+#define MREPEAT157(macro, data)       MREPEAT156(macro, data)   macro(156, data)
235
+#define MREPEAT158(macro, data)       MREPEAT157(macro, data)   macro(157, data)
236
+#define MREPEAT159(macro, data)       MREPEAT158(macro, data)   macro(158, data)
237
+#define MREPEAT160(macro, data)       MREPEAT159(macro, data)   macro(159, data)
238
+#define MREPEAT161(macro, data)       MREPEAT160(macro, data)   macro(160, data)
239
+#define MREPEAT162(macro, data)       MREPEAT161(macro, data)   macro(161, data)
240
+#define MREPEAT163(macro, data)       MREPEAT162(macro, data)   macro(162, data)
241
+#define MREPEAT164(macro, data)       MREPEAT163(macro, data)   macro(163, data)
242
+#define MREPEAT165(macro, data)       MREPEAT164(macro, data)   macro(164, data)
243
+#define MREPEAT166(macro, data)       MREPEAT165(macro, data)   macro(165, data)
244
+#define MREPEAT167(macro, data)       MREPEAT166(macro, data)   macro(166, data)
245
+#define MREPEAT168(macro, data)       MREPEAT167(macro, data)   macro(167, data)
246
+#define MREPEAT169(macro, data)       MREPEAT168(macro, data)   macro(168, data)
247
+#define MREPEAT170(macro, data)       MREPEAT169(macro, data)   macro(169, data)
248
+#define MREPEAT171(macro, data)       MREPEAT170(macro, data)   macro(170, data)
249
+#define MREPEAT172(macro, data)       MREPEAT171(macro, data)   macro(171, data)
250
+#define MREPEAT173(macro, data)       MREPEAT172(macro, data)   macro(172, data)
251
+#define MREPEAT174(macro, data)       MREPEAT173(macro, data)   macro(173, data)
252
+#define MREPEAT175(macro, data)       MREPEAT174(macro, data)   macro(174, data)
253
+#define MREPEAT176(macro, data)       MREPEAT175(macro, data)   macro(175, data)
254
+#define MREPEAT177(macro, data)       MREPEAT176(macro, data)   macro(176, data)
255
+#define MREPEAT178(macro, data)       MREPEAT177(macro, data)   macro(177, data)
256
+#define MREPEAT179(macro, data)       MREPEAT178(macro, data)   macro(178, data)
257
+#define MREPEAT180(macro, data)       MREPEAT179(macro, data)   macro(179, data)
258
+#define MREPEAT181(macro, data)       MREPEAT180(macro, data)   macro(180, data)
259
+#define MREPEAT182(macro, data)       MREPEAT181(macro, data)   macro(181, data)
260
+#define MREPEAT183(macro, data)       MREPEAT182(macro, data)   macro(182, data)
261
+#define MREPEAT184(macro, data)       MREPEAT183(macro, data)   macro(183, data)
262
+#define MREPEAT185(macro, data)       MREPEAT184(macro, data)   macro(184, data)
263
+#define MREPEAT186(macro, data)       MREPEAT185(macro, data)   macro(185, data)
264
+#define MREPEAT187(macro, data)       MREPEAT186(macro, data)   macro(186, data)
265
+#define MREPEAT188(macro, data)       MREPEAT187(macro, data)   macro(187, data)
266
+#define MREPEAT189(macro, data)       MREPEAT188(macro, data)   macro(188, data)
267
+#define MREPEAT190(macro, data)       MREPEAT189(macro, data)   macro(189, data)
268
+#define MREPEAT191(macro, data)       MREPEAT190(macro, data)   macro(190, data)
269
+#define MREPEAT192(macro, data)       MREPEAT191(macro, data)   macro(191, data)
270
+#define MREPEAT193(macro, data)       MREPEAT192(macro, data)   macro(192, data)
271
+#define MREPEAT194(macro, data)       MREPEAT193(macro, data)   macro(193, data)
272
+#define MREPEAT195(macro, data)       MREPEAT194(macro, data)   macro(194, data)
273
+#define MREPEAT196(macro, data)       MREPEAT195(macro, data)   macro(195, data)
274
+#define MREPEAT197(macro, data)       MREPEAT196(macro, data)   macro(196, data)
275
+#define MREPEAT198(macro, data)       MREPEAT197(macro, data)   macro(197, data)
276
+#define MREPEAT199(macro, data)       MREPEAT198(macro, data)   macro(198, data)
277
+#define MREPEAT200(macro, data)       MREPEAT199(macro, data)   macro(199, data)
278
+#define MREPEAT201(macro, data)       MREPEAT200(macro, data)   macro(200, data)
279
+#define MREPEAT202(macro, data)       MREPEAT201(macro, data)   macro(201, data)
280
+#define MREPEAT203(macro, data)       MREPEAT202(macro, data)   macro(202, data)
281
+#define MREPEAT204(macro, data)       MREPEAT203(macro, data)   macro(203, data)
282
+#define MREPEAT205(macro, data)       MREPEAT204(macro, data)   macro(204, data)
283
+#define MREPEAT206(macro, data)       MREPEAT205(macro, data)   macro(205, data)
284
+#define MREPEAT207(macro, data)       MREPEAT206(macro, data)   macro(206, data)
285
+#define MREPEAT208(macro, data)       MREPEAT207(macro, data)   macro(207, data)
286
+#define MREPEAT209(macro, data)       MREPEAT208(macro, data)   macro(208, data)
287
+#define MREPEAT210(macro, data)       MREPEAT209(macro, data)   macro(209, data)
288
+#define MREPEAT211(macro, data)       MREPEAT210(macro, data)   macro(210, data)
289
+#define MREPEAT212(macro, data)       MREPEAT211(macro, data)   macro(211, data)
290
+#define MREPEAT213(macro, data)       MREPEAT212(macro, data)   macro(212, data)
291
+#define MREPEAT214(macro, data)       MREPEAT213(macro, data)   macro(213, data)
292
+#define MREPEAT215(macro, data)       MREPEAT214(macro, data)   macro(214, data)
293
+#define MREPEAT216(macro, data)       MREPEAT215(macro, data)   macro(215, data)
294
+#define MREPEAT217(macro, data)       MREPEAT216(macro, data)   macro(216, data)
295
+#define MREPEAT218(macro, data)       MREPEAT217(macro, data)   macro(217, data)
296
+#define MREPEAT219(macro, data)       MREPEAT218(macro, data)   macro(218, data)
297
+#define MREPEAT220(macro, data)       MREPEAT219(macro, data)   macro(219, data)
298
+#define MREPEAT221(macro, data)       MREPEAT220(macro, data)   macro(220, data)
299
+#define MREPEAT222(macro, data)       MREPEAT221(macro, data)   macro(221, data)
300
+#define MREPEAT223(macro, data)       MREPEAT222(macro, data)   macro(222, data)
301
+#define MREPEAT224(macro, data)       MREPEAT223(macro, data)   macro(223, data)
302
+#define MREPEAT225(macro, data)       MREPEAT224(macro, data)   macro(224, data)
303
+#define MREPEAT226(macro, data)       MREPEAT225(macro, data)   macro(225, data)
304
+#define MREPEAT227(macro, data)       MREPEAT226(macro, data)   macro(226, data)
305
+#define MREPEAT228(macro, data)       MREPEAT227(macro, data)   macro(227, data)
306
+#define MREPEAT229(macro, data)       MREPEAT228(macro, data)   macro(228, data)
307
+#define MREPEAT230(macro, data)       MREPEAT229(macro, data)   macro(229, data)
308
+#define MREPEAT231(macro, data)       MREPEAT230(macro, data)   macro(230, data)
309
+#define MREPEAT232(macro, data)       MREPEAT231(macro, data)   macro(231, data)
310
+#define MREPEAT233(macro, data)       MREPEAT232(macro, data)   macro(232, data)
311
+#define MREPEAT234(macro, data)       MREPEAT233(macro, data)   macro(233, data)
312
+#define MREPEAT235(macro, data)       MREPEAT234(macro, data)   macro(234, data)
313
+#define MREPEAT236(macro, data)       MREPEAT235(macro, data)   macro(235, data)
314
+#define MREPEAT237(macro, data)       MREPEAT236(macro, data)   macro(236, data)
315
+#define MREPEAT238(macro, data)       MREPEAT237(macro, data)   macro(237, data)
316
+#define MREPEAT239(macro, data)       MREPEAT238(macro, data)   macro(238, data)
317
+#define MREPEAT240(macro, data)       MREPEAT239(macro, data)   macro(239, data)
318
+#define MREPEAT241(macro, data)       MREPEAT240(macro, data)   macro(240, data)
319
+#define MREPEAT242(macro, data)       MREPEAT241(macro, data)   macro(241, data)
320
+#define MREPEAT243(macro, data)       MREPEAT242(macro, data)   macro(242, data)
321
+#define MREPEAT244(macro, data)       MREPEAT243(macro, data)   macro(243, data)
322
+#define MREPEAT245(macro, data)       MREPEAT244(macro, data)   macro(244, data)
323
+#define MREPEAT246(macro, data)       MREPEAT245(macro, data)   macro(245, data)
324
+#define MREPEAT247(macro, data)       MREPEAT246(macro, data)   macro(246, data)
325
+#define MREPEAT248(macro, data)       MREPEAT247(macro, data)   macro(247, data)
326
+#define MREPEAT249(macro, data)       MREPEAT248(macro, data)   macro(248, data)
327
+#define MREPEAT250(macro, data)       MREPEAT249(macro, data)   macro(249, data)
328
+#define MREPEAT251(macro, data)       MREPEAT250(macro, data)   macro(250, data)
329
+#define MREPEAT252(macro, data)       MREPEAT251(macro, data)   macro(251, data)
330
+#define MREPEAT253(macro, data)       MREPEAT252(macro, data)   macro(252, data)
331
+#define MREPEAT254(macro, data)       MREPEAT253(macro, data)   macro(253, data)
332
+#define MREPEAT255(macro, data)       MREPEAT254(macro, data)   macro(254, data)
333
+#define MREPEAT256(macro, data)       MREPEAT255(macro, data)   macro(255, data)
334
+
335
+/**
336
+ * \}
337
+ */
338
+
339
+#endif  // _MREPEAT_H_

+ 261
- 0
Marlin/src/HAL/HAL_DUE/usb/osc.h View File

@@ -0,0 +1,261 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Chip-specific oscillator management functions.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef CHIP_OSC_H_INCLUDED
48
+#define CHIP_OSC_H_INCLUDED
49
+
50
+#include "compiler.h"
51
+
52
+/// @cond 0
53
+/**INDENT-OFF**/
54
+#ifdef __cplusplus
55
+extern "C" {
56
+#endif
57
+/**INDENT-ON**/
58
+/// @endcond
59
+
60
+/*
61
+ * Below BOARD_XXX macros are related to the specific board, and
62
+ * should be defined by the board code, otherwise default value are used.
63
+ */
64
+#if !defined(BOARD_FREQ_SLCK_XTAL)
65
+#  warning The board slow clock xtal frequency has not been defined.
66
+#  define BOARD_FREQ_SLCK_XTAL      (32768UL)
67
+#endif
68
+
69
+#if !defined(BOARD_FREQ_SLCK_BYPASS)
70
+#  warning The board slow clock bypass frequency has not been defined.
71
+#  define BOARD_FREQ_SLCK_BYPASS    (32768UL)
72
+#endif
73
+
74
+#if !defined(BOARD_FREQ_MAINCK_XTAL)
75
+#  warning The board main clock xtal frequency has not been defined.
76
+#  define BOARD_FREQ_MAINCK_XTAL    (12000000UL)
77
+#endif
78
+
79
+#if !defined(BOARD_FREQ_MAINCK_BYPASS)
80
+#  warning The board main clock bypass frequency has not been defined.
81
+#  define BOARD_FREQ_MAINCK_BYPASS  (12000000UL)
82
+#endif
83
+
84
+#if !defined(BOARD_OSC_STARTUP_US)
85
+#  warning The board main clock xtal startup time has not been defined.
86
+#  define BOARD_OSC_STARTUP_US      (15625UL)
87
+#endif
88
+
89
+/**
90
+ * \weakgroup osc_group
91
+ * @{
92
+ */
93
+
94
+//! \name Oscillator identifiers
95
+//@{
96
+#define OSC_SLCK_32K_RC             0    //!< Internal 32kHz RC oscillator.
97
+#define OSC_SLCK_32K_XTAL           1    //!< External 32kHz crystal oscillator.
98
+#define OSC_SLCK_32K_BYPASS         2    //!< External 32kHz bypass oscillator.
99
+#define OSC_MAINCK_4M_RC            3    //!< Internal 4MHz RC oscillator.
100
+#define OSC_MAINCK_8M_RC            4    //!< Internal 8MHz RC oscillator.
101
+#define OSC_MAINCK_12M_RC           5    //!< Internal 12MHz RC oscillator.
102
+#define OSC_MAINCK_XTAL             6    //!< External crystal oscillator.
103
+#define OSC_MAINCK_BYPASS           7    //!< External bypass oscillator.
104
+//@}
105
+
106
+//! \name Oscillator clock speed in hertz
107
+//@{
108
+#define OSC_SLCK_32K_RC_HZ          CHIP_FREQ_SLCK_RC               //!< Internal 32kHz RC oscillator.
109
+#define OSC_SLCK_32K_XTAL_HZ        BOARD_FREQ_SLCK_XTAL            //!< External 32kHz crystal oscillator.
110
+#define OSC_SLCK_32K_BYPASS_HZ      BOARD_FREQ_SLCK_BYPASS          //!< External 32kHz bypass oscillator.
111
+#define OSC_MAINCK_4M_RC_HZ         CHIP_FREQ_MAINCK_RC_4MHZ        //!< Internal 4MHz RC oscillator.
112
+#define OSC_MAINCK_8M_RC_HZ         CHIP_FREQ_MAINCK_RC_8MHZ        //!< Internal 8MHz RC oscillator.
113
+#define OSC_MAINCK_12M_RC_HZ        CHIP_FREQ_MAINCK_RC_12MHZ       //!< Internal 12MHz RC oscillator.
114
+#define OSC_MAINCK_XTAL_HZ          BOARD_FREQ_MAINCK_XTAL          //!< External crystal oscillator.
115
+#define OSC_MAINCK_BYPASS_HZ        BOARD_FREQ_MAINCK_BYPASS        //!< External bypass oscillator.
116
+//@}
117
+
118
+static inline void osc_enable(uint32_t ul_id)
119
+{
120
+	switch (ul_id) {
121
+	case OSC_SLCK_32K_RC:
122
+		break;
123
+
124
+	case OSC_SLCK_32K_XTAL:
125
+		pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
126
+		break;
127
+
128
+	case OSC_SLCK_32K_BYPASS:
129
+		pmc_switch_sclk_to_32kxtal(PMC_OSC_BYPASS);
130
+		break;
131
+
132
+
133
+	case OSC_MAINCK_4M_RC:
134
+		pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
135
+		break;
136
+
137
+	case OSC_MAINCK_8M_RC:
138
+		pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
139
+		break;
140
+
141
+	case OSC_MAINCK_12M_RC:
142
+		pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
143
+		break;
144
+
145
+
146
+	case OSC_MAINCK_XTAL:
147
+		pmc_switch_mainck_to_xtal(PMC_OSC_XTAL/*,
148
+			pmc_us_to_moscxtst(BOARD_OSC_STARTUP_US,
149
+				OSC_SLCK_32K_RC_HZ)*/);
150
+		break;
151
+
152
+	case OSC_MAINCK_BYPASS:
153
+		pmc_switch_mainck_to_xtal(PMC_OSC_BYPASS/*,
154
+			pmc_us_to_moscxtst(BOARD_OSC_STARTUP_US,
155
+				OSC_SLCK_32K_RC_HZ)*/);
156
+		break;
157
+	}
158
+}
159
+
160
+static inline void osc_disable(uint32_t ul_id)
161
+{
162
+	switch (ul_id) {
163
+	case OSC_SLCK_32K_RC:
164
+	case OSC_SLCK_32K_XTAL:
165
+	case OSC_SLCK_32K_BYPASS:
166
+		break;
167
+
168
+	case OSC_MAINCK_4M_RC:
169
+	case OSC_MAINCK_8M_RC:
170
+	case OSC_MAINCK_12M_RC:
171
+		pmc_osc_disable_fastrc();
172
+		break;
173
+
174
+	case OSC_MAINCK_XTAL:
175
+		pmc_osc_disable_xtal(PMC_OSC_XTAL);
176
+		break;
177
+
178
+	case OSC_MAINCK_BYPASS:
179
+		pmc_osc_disable_xtal(PMC_OSC_BYPASS);
180
+		break;
181
+	}
182
+}
183
+
184
+static inline bool osc_is_ready(uint32_t ul_id)
185
+{
186
+	switch (ul_id) {
187
+	case OSC_SLCK_32K_RC:
188
+		return 1;
189
+
190
+	case OSC_SLCK_32K_XTAL:
191
+	case OSC_SLCK_32K_BYPASS:
192
+		return pmc_osc_is_ready_32kxtal();
193
+
194
+	case OSC_MAINCK_4M_RC:
195
+	case OSC_MAINCK_8M_RC:
196
+	case OSC_MAINCK_12M_RC:
197
+	case OSC_MAINCK_XTAL:
198
+	case OSC_MAINCK_BYPASS:
199
+		return pmc_osc_is_ready_mainck();
200
+	}
201
+
202
+	return 0;
203
+}
204
+
205
+static inline uint32_t osc_get_rate(uint32_t ul_id)
206
+{
207
+	switch (ul_id) {
208
+	case OSC_SLCK_32K_RC:
209
+		return OSC_SLCK_32K_RC_HZ;
210
+
211
+	case OSC_SLCK_32K_XTAL:
212
+		return BOARD_FREQ_SLCK_XTAL;
213
+
214
+	case OSC_SLCK_32K_BYPASS:
215
+		return BOARD_FREQ_SLCK_BYPASS;
216
+
217
+	case OSC_MAINCK_4M_RC:
218
+		return OSC_MAINCK_4M_RC_HZ;
219
+
220
+	case OSC_MAINCK_8M_RC:
221
+		return OSC_MAINCK_8M_RC_HZ;
222
+
223
+	case OSC_MAINCK_12M_RC:
224
+		return OSC_MAINCK_12M_RC_HZ;
225
+
226
+	case OSC_MAINCK_XTAL:
227
+		return BOARD_FREQ_MAINCK_XTAL;
228
+
229
+	case OSC_MAINCK_BYPASS:
230
+		return BOARD_FREQ_MAINCK_BYPASS;
231
+	}
232
+
233
+	return 0;
234
+}
235
+
236
+/**
237
+ * \brief Wait until the oscillator identified by \a id is ready
238
+ *
239
+ * This function will busy-wait for the oscillator identified by \a id
240
+ * to become stable and ready to use as a clock source.
241
+ *
242
+ * \param id A number identifying the oscillator to wait for.
243
+ */
244
+static inline void osc_wait_ready(uint8_t id)
245
+{
246
+	while (!osc_is_ready(id)) {
247
+		/* Do nothing */
248
+	}
249
+} 
250
+
251
+//! @}
252
+
253
+/// @cond 0
254
+/**INDENT-OFF**/
255
+#ifdef __cplusplus
256
+}
257
+#endif
258
+/**INDENT-ON**/
259
+/// @endcond
260
+
261
+#endif /* CHIP_OSC_H_INCLUDED */

+ 288
- 0
Marlin/src/HAL/HAL_DUE/usb/pll.h View File

@@ -0,0 +1,288 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Chip-specific PLL definitions.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef CHIP_PLL_H_INCLUDED
48
+#define CHIP_PLL_H_INCLUDED
49
+
50
+#include "osc.h"
51
+
52
+/// @cond 0
53
+/**INDENT-OFF**/
54
+#ifdef __cplusplus
55
+extern "C" {
56
+#endif
57
+/**INDENT-ON**/
58
+/// @endcond
59
+
60
+/**
61
+ * \weakgroup pll_group
62
+ * @{
63
+ */
64
+
65
+#define PLL_OUTPUT_MIN_HZ   84000000
66
+#define PLL_OUTPUT_MAX_HZ   192000000
67
+
68
+#define PLL_INPUT_MIN_HZ    8000000
69
+#define PLL_INPUT_MAX_HZ    16000000
70
+
71
+#define NR_PLLS             2
72
+#define PLLA_ID             0
73
+#define UPLL_ID             1   //!< USB UTMI PLL.
74
+
75
+#define PLL_UPLL_HZ     480000000
76
+
77
+#define PLL_COUNT           0x3fU
78
+
79
+enum pll_source {
80
+	PLL_SRC_MAINCK_4M_RC        = OSC_MAINCK_4M_RC,     //!< Internal 4MHz RC oscillator.
81
+	PLL_SRC_MAINCK_8M_RC        = OSC_MAINCK_8M_RC,     //!< Internal 8MHz RC oscillator.
82
+	PLL_SRC_MAINCK_12M_RC       = OSC_MAINCK_12M_RC,    //!< Internal 12MHz RC oscillator.
83
+	PLL_SRC_MAINCK_XTAL         = OSC_MAINCK_XTAL,      //!< External crystal oscillator.
84
+	PLL_SRC_MAINCK_BYPASS       = OSC_MAINCK_BYPASS,    //!< External bypass oscillator.
85
+	PLL_NR_SOURCES,                                     //!< Number of PLL sources.
86
+};
87
+
88
+struct pll_config {
89
+	uint32_t ctrl;
90
+};
91
+
92
+#define pll_get_default_rate(pll_id)                                       \
93
+	((osc_get_rate(CONFIG_PLL##pll_id##_SOURCE)                            \
94
+			* CONFIG_PLL##pll_id##_MUL)                                    \
95
+			/ CONFIG_PLL##pll_id##_DIV)
96
+
97
+/* Force UTMI PLL parameters (Hardware defined) */
98
+#ifdef CONFIG_PLL1_SOURCE
99
+# undef CONFIG_PLL1_SOURCE
100
+#endif
101
+#ifdef CONFIG_PLL1_MUL
102
+# undef CONFIG_PLL1_MUL
103
+#endif
104
+#ifdef CONFIG_PLL1_DIV
105
+# undef CONFIG_PLL1_DIV
106
+#endif
107
+#define CONFIG_PLL1_SOURCE  PLL_SRC_MAINCK_XTAL
108
+#define CONFIG_PLL1_MUL     0
109
+#define CONFIG_PLL1_DIV     0
110
+
111
+/**
112
+ * \note The SAM3X PLL hardware interprets mul as mul+1. For readability the hardware mul+1
113
+ * is hidden in this implementation. Use mul as mul effective value.
114
+ */
115
+static inline void pll_config_init(struct pll_config *p_cfg,
116
+		enum pll_source e_src, uint32_t ul_div, uint32_t ul_mul)
117
+{
118
+	uint32_t vco_hz;
119
+
120
+	Assert(e_src < PLL_NR_SOURCES);
121
+
122
+	if (ul_div == 0 && ul_mul == 0) { /* Must only be true for UTMI PLL */
123
+		p_cfg->ctrl = CKGR_UCKR_UPLLCOUNT(PLL_COUNT);
124
+	} else { /* PLLA */
125
+		/* Calculate internal VCO frequency */
126
+		vco_hz = osc_get_rate(e_src) / ul_div;
127
+		Assert(vco_hz >= PLL_INPUT_MIN_HZ);
128
+		Assert(vco_hz <= PLL_INPUT_MAX_HZ);
129
+
130
+		vco_hz *= ul_mul;
131
+		Assert(vco_hz >= PLL_OUTPUT_MIN_HZ);
132
+		Assert(vco_hz <= PLL_OUTPUT_MAX_HZ);
133
+
134
+		/* PMC hardware will automatically make it mul+1 */
135
+		p_cfg->ctrl = CKGR_PLLAR_MULA(ul_mul - 1) | CKGR_PLLAR_DIVA(ul_div) | CKGR_PLLAR_PLLACOUNT(PLL_COUNT);
136
+	}
137
+}
138
+
139
+#define pll_config_defaults(cfg, pll_id)                                   \
140
+	pll_config_init(cfg,                                                   \
141
+			CONFIG_PLL##pll_id##_SOURCE,                                   \
142
+			CONFIG_PLL##pll_id##_DIV,                                      \
143
+			CONFIG_PLL##pll_id##_MUL)
144
+
145
+static inline void pll_config_read(struct pll_config *p_cfg, uint32_t ul_pll_id)
146
+{
147
+	Assert(ul_pll_id < NR_PLLS);
148
+
149
+	if (ul_pll_id == PLLA_ID) {
150
+		p_cfg->ctrl = PMC->CKGR_PLLAR;
151
+	} else {
152
+		p_cfg->ctrl = PMC->CKGR_UCKR;
153
+	}
154
+}
155
+
156
+static inline void pll_config_write(const struct pll_config *p_cfg, uint32_t ul_pll_id)
157
+{
158
+	Assert(ul_pll_id < NR_PLLS);
159
+
160
+	if (ul_pll_id == PLLA_ID) {
161
+		pmc_disable_pllack(); // Always stop PLL first!
162
+		PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | p_cfg->ctrl;
163
+	} else {
164
+		PMC->CKGR_UCKR = p_cfg->ctrl;
165
+	}
166
+}
167
+
168
+static inline void pll_enable(const struct pll_config *p_cfg, uint32_t ul_pll_id)
169
+{
170
+	Assert(ul_pll_id < NR_PLLS);
171
+
172
+	if (ul_pll_id == PLLA_ID) {
173
+		pmc_disable_pllack(); // Always stop PLL first!
174
+		PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | p_cfg->ctrl;
175
+	} else {
176
+		PMC->CKGR_UCKR = p_cfg->ctrl | CKGR_UCKR_UPLLEN;
177
+	}
178
+}
179
+
180
+/**
181
+ * \note This will only disable the selected PLL, not the underlying oscillator (mainck).
182
+ */
183
+static inline void pll_disable(uint32_t ul_pll_id)
184
+{
185
+	Assert(ul_pll_id < NR_PLLS);
186
+
187
+	if (ul_pll_id == PLLA_ID) {
188
+		pmc_disable_pllack();
189
+	} else {
190
+		PMC->CKGR_UCKR &= ~CKGR_UCKR_UPLLEN;
191
+	}
192
+}
193
+
194
+static inline uint32_t pll_is_locked(uint32_t ul_pll_id)
195
+{
196
+	Assert(ul_pll_id < NR_PLLS);
197
+
198
+	if (ul_pll_id == PLLA_ID) {
199
+		return pmc_is_locked_pllack();
200
+	} else {
201
+		return pmc_is_locked_upll();
202
+	}
203
+}
204
+
205
+static inline void pll_enable_source(enum pll_source e_src)
206
+{
207
+	switch (e_src) {
208
+	case PLL_SRC_MAINCK_4M_RC:
209
+	case PLL_SRC_MAINCK_8M_RC:
210
+	case PLL_SRC_MAINCK_12M_RC:
211
+	case PLL_SRC_MAINCK_XTAL:
212
+	case PLL_SRC_MAINCK_BYPASS:
213
+		osc_enable(e_src);
214
+		osc_wait_ready(e_src);
215
+		break;
216
+
217
+	default:
218
+		Assert(false);
219
+		break;
220
+	}
221
+}
222
+
223
+static inline void pll_enable_config_defaults(unsigned int ul_pll_id)
224
+{
225
+	struct pll_config pllcfg;
226
+
227
+	if (pll_is_locked(ul_pll_id)) {
228
+		return; // Pll already running
229
+	}
230
+	switch (ul_pll_id) {
231
+#ifdef CONFIG_PLL0_SOURCE
232
+	case 0:
233
+		pll_enable_source(CONFIG_PLL0_SOURCE);
234
+		pll_config_init(&pllcfg,
235
+				CONFIG_PLL0_SOURCE,
236
+				CONFIG_PLL0_DIV,
237
+				CONFIG_PLL0_MUL);
238
+		break;
239
+#endif
240
+#ifdef CONFIG_PLL1_SOURCE
241
+	case 1:
242
+		pll_enable_source(CONFIG_PLL1_SOURCE);
243
+		pll_config_init(&pllcfg,
244
+				CONFIG_PLL1_SOURCE,
245
+				CONFIG_PLL1_DIV,
246
+				CONFIG_PLL1_MUL);
247
+		break;
248
+#endif
249
+	default:
250
+		Assert(false);
251
+		break;
252
+	}
253
+	pll_enable(&pllcfg, ul_pll_id);
254
+	while (!pll_is_locked(ul_pll_id));
255
+}
256
+
257
+/**
258
+ * \brief Wait for PLL \a pll_id to become locked
259
+ *
260
+ * \todo Use a timeout to avoid waiting forever and hanging the system
261
+ *
262
+ * \param pll_id The ID of the PLL to wait for.
263
+ *
264
+ * \retval STATUS_OK The PLL is now locked.
265
+ * \retval ERR_TIMEOUT Timed out waiting for PLL to become locked.
266
+ */
267
+static inline int pll_wait_for_lock(unsigned int pll_id)
268
+{
269
+	Assert(pll_id < NR_PLLS);
270
+
271
+	while (!pll_is_locked(pll_id)) {
272
+		/* Do nothing */
273
+	}
274
+
275
+	return 0;
276
+} 
277
+ 
278
+//! @}
279
+
280
+/// @cond 0
281
+/**INDENT-OFF**/
282
+#ifdef __cplusplus
283
+}
284
+#endif
285
+/**INDENT-ON**/
286
+/// @endcond
287
+
288
+#endif /* CHIP_PLL_H_INCLUDED */

+ 55
- 0
Marlin/src/HAL/HAL_DUE/usb/preprocessor.h View File

@@ -0,0 +1,55 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Preprocessor utils.
5
+ *
6
+ * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _PREPROCESSOR_H_
48
+#define _PREPROCESSOR_H_
49
+
50
+#include "tpaste.h"
51
+#include "stringz.h"
52
+#include "mrepeat.h"
53
+
54
+
55
+#endif  // _PREPROCESSOR_H_

+ 173
- 0
Marlin/src/HAL/HAL_DUE/usb/sbc_protocol.h View File

@@ -0,0 +1,173 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief SCSI Block Commands
5
+ *
6
+ * This file contains definitions of some of the commands found in the
7
+ * SCSI SBC-2 standard.
8
+ *
9
+ * Note that the SBC specification depends on several commands defined
10
+ * by the SCSI Primary Commands (SPC) standard. Each version of the SBC
11
+ * standard is meant to be used in conjunction with a specific version
12
+ * of the SPC standard, as follows:
13
+ *   - SBC   depends on SPC
14
+ *   - SBC-2 depends on SPC-3
15
+ *   - SBC-3 depends on SPC-4
16
+ *
17
+ * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
18
+ *
19
+ * \asf_license_start
20
+ *
21
+ * \page License
22
+ *
23
+ * Redistribution and use in source and binary forms, with or without
24
+ * modification, are permitted provided that the following conditions are met:
25
+ *
26
+ * 1. Redistributions of source code must retain the above copyright notice,
27
+ *    this list of conditions and the following disclaimer.
28
+ *
29
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
30
+ *    this list of conditions and the following disclaimer in the documentation
31
+ *    and/or other materials provided with the distribution.
32
+ *
33
+ * 3. The name of Atmel may not be used to endorse or promote products derived
34
+ *    from this software without specific prior written permission.
35
+ *
36
+ * 4. This software may only be redistributed and used in connection with an
37
+ *    Atmel microcontroller product.
38
+ *
39
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
40
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
42
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
43
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
+ * POSSIBILITY OF SUCH DAMAGE.
50
+ *
51
+ * \asf_license_stop
52
+ *
53
+ */
54
+/*
55
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
56
+ */
57
+#ifndef _SBC_PROTOCOL_H_
58
+#define _SBC_PROTOCOL_H_
59
+
60
+
61
+/**
62
+ * \ingroup usb_msc_protocol
63
+ * \defgroup usb_sbc_protocol SCSI Block Commands protocol definitions
64
+ *
65
+ * @{
66
+ */
67
+
68
+//! \name SCSI commands defined by SBC-2
69
+//@{
70
+#define  SBC_FORMAT_UNIT         0x04
71
+#define  SBC_READ6               0x08
72
+#define  SBC_WRITE6              0x0A
73
+#define  SBC_START_STOP_UNIT     0x1B
74
+#define  SBC_READ_CAPACITY10     0x25
75
+#define  SBC_READ10              0x28
76
+#define  SBC_WRITE10             0x2A
77
+#define  SBC_VERIFY10            0x2F
78
+//@}
79
+
80
+//! \name SBC-2 Mode page definitions
81
+//@{
82
+
83
+enum scsi_sbc_mode {
84
+	SCSI_MS_MODE_RW_ERR_RECOV = 0x01,	//!< Read-Write Error Recovery mode page
85
+	SCSI_MS_MODE_FORMAT_DEVICE = 0x03,	//!< Format Device mode page
86
+	SCSI_MS_MODE_FLEXIBLE_DISK = 0x05,	//!< Flexible Disk mode page
87
+	SCSI_MS_MODE_CACHING = 0x08,	//!< Caching mode page
88
+};
89
+
90
+
91
+//! \name SBC-2 Device-Specific Parameter
92
+//@{
93
+#define SCSI_MS_SBC_WP              0x80	//!< Write Protected
94
+#define SCSI_MS_SBC_DPOFUA          0x10	//!< DPO and FUA supported
95
+//@}
96
+
97
+/**
98
+ * \brief SBC-2 Short LBA mode parameter block descriptor
99
+ */
100
+struct sbc_slba_block_desc {
101
+	be32_t nr_blocks;	//!< Number of Blocks
102
+	be32_t block_len;	//!< Block Length
103
+#define SBC_SLBA_BLOCK_LEN_MASK   0x00FFFFFFU	//!< Mask reserved bits
104
+};
105
+
106
+/**
107
+ * \brief SBC-2 Caching mode page
108
+ */
109
+struct sbc_caching_mode_page {
110
+	uint8_t page_code;
111
+	uint8_t page_length;
112
+	uint8_t flags2;
113
+#define  SBC_MP_CACHE_IC      (1 << 7)	//!< Initiator Control
114
+#define  SBC_MP_CACHE_ABPF    (1 << 6)	//!< Abort Pre-Fetch
115
+#define  SBC_MP_CACHE_CAP     (1 << 5)	//!< Catching Analysis Permitted
116
+#define  SBC_MP_CACHE_DISC    (1 << 4)	//!< Discontinuity
117
+#define  SBC_MP_CACHE_SIZE    (1 << 3)	//!< Size enable
118
+#define  SBC_MP_CACHE_WCE     (1 << 2)	//!< Write back Cache Enable
119
+#define  SBC_MP_CACHE_MF      (1 << 1)	//!< Multiplication Factor
120
+#define  SBC_MP_CACHE_RCD     (1 << 0)	//!< Read Cache Disable
121
+	uint8_t retention;
122
+	be16_t dis_pf_transfer_len;
123
+	be16_t min_prefetch;
124
+	be16_t max_prefetch;
125
+	be16_t max_prefetch_ceil;
126
+	uint8_t flags12;
127
+#define  SBC_MP_CACHE_FSW     (1 << 7)	//!< Force Sequential Write
128
+#define  SBC_MP_CACHE_LBCSS   (1 << 6)	//!< Logical Blk Cache Seg Sz
129
+#define  SBC_MP_CACHE_DRA     (1 << 5)	//!< Disable Read-Ahead
130
+#define  SBC_MP_CACHE_NV_DIS  (1 << 0)	//!< Non-Volatile Cache Disable
131
+	uint8_t nr_cache_segments;
132
+	be16_t cache_segment_size;
133
+	uint8_t reserved[4];
134
+};
135
+
136
+/**
137
+ * \brief SBC-2 Read-Write Error Recovery mode page
138
+ */
139
+struct sbc_rdwr_error_recovery_mode_page {
140
+	uint8_t page_code;
141
+	uint8_t page_length;
142
+#define  SPC_MP_RW_ERR_RECOV_PAGE_LENGTH    0x0A
143
+	uint8_t flags1;
144
+#define  SBC_MP_RW_ERR_RECOV_AWRE   (1 << 7)
145
+#define  SBC_MP_RW_ERR_RECOV_ARRE   (1 << 6)
146
+#define  SBC_MP_RW_ERR_RECOV_TB     (1 << 5)
147
+#define  SBC_MP_RW_ERR_RECOV_RC     (1 << 4)
148
+#define  SBC_MP_RW_ERR_RECOV_ERR    (1 << 3)
149
+#define  SBC_MP_RW_ERR_RECOV_PER    (1 << 2)
150
+#define  SBC_MP_RW_ERR_RECOV_DTE    (1 << 1)
151
+#define  SBC_MP_RW_ERR_RECOV_DCR    (1 << 0)
152
+	uint8_t read_retry_count;
153
+	uint8_t correction_span;
154
+	uint8_t head_offset_count;
155
+	uint8_t data_strobe_offset_count;
156
+	uint8_t flags2;
157
+	uint8_t write_retry_count;
158
+	uint8_t flags3;
159
+	be16_t recovery_time_limit;
160
+};
161
+//@}
162
+
163
+/**
164
+ * \brief SBC-2 READ CAPACITY (10) parameter data
165
+ */
166
+struct sbc_read_capacity10_data {
167
+	be32_t max_lba;	//!< LBA of last logical block
168
+	be32_t block_len;	//!< Number of bytes in the last logical block
169
+};
170
+
171
+//@}
172
+
173
+#endif // _SBC_PROTOCOL_H_

+ 135
- 0
Marlin/src/HAL/HAL_DUE/usb/sd_mmc_spi_mem.cpp View File

@@ -0,0 +1,135 @@
1
+/**
2
+ * Interface from Atmel USB MSD to Marlin SD card
3
+ */
4
+
5
+#ifdef ARDUINO_ARCH_SAM
6
+
7
+#include "../../../inc/MarlinConfig.h"
8
+#include "../../../sd/cardreader.h"
9
+extern "C" {
10
+#include "sd_mmc_spi_mem.h"
11
+}
12
+
13
+#define SD_MMC_BLOCK_SIZE 512
14
+
15
+void sd_mmc_spi_mem_init(void)
16
+{
17
+}
18
+
19
+Ctrl_status sd_mmc_spi_test_unit_ready(void)
20
+{
21
+  if (!IS_SD_INSERTED || IS_SD_PRINTING || IS_SD_FILE_OPEN || !card.cardOK)
22
+    return CTRL_NO_PRESENT;
23
+  return CTRL_GOOD;
24
+}
25
+
26
+Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector)
27
+{
28
+  if (!IS_SD_INSERTED || IS_SD_PRINTING || IS_SD_FILE_OPEN || !card.cardOK)
29
+    return CTRL_NO_PRESENT;
30
+  *nb_sector = card.getSd2Card().cardSize();
31
+  return CTRL_GOOD;
32
+}
33
+
34
+bool sd_mmc_spi_unload(bool unload)
35
+{
36
+  return true;
37
+}
38
+
39
+bool sd_mmc_spi_wr_protect(void)
40
+{
41
+  return false;
42
+}
43
+
44
+bool sd_mmc_spi_removal(void)
45
+{
46
+  if (!IS_SD_INSERTED || IS_SD_PRINTING || IS_SD_FILE_OPEN || !card.cardOK)
47
+    return true;
48
+  return false;
49
+}
50
+
51
+#if ACCESS_USB == true
52
+/**
53
+ * \name MEM <-> USB Interface
54
+ * @{
55
+ */
56
+
57
+#include "udi_msc.h"
58
+
59
+COMPILER_WORD_ALIGNED
60
+uint8_t sector_buf[SD_MMC_BLOCK_SIZE];
61
+
62
+// #define DEBUG_MMC
63
+
64
+Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector)
65
+{
66
+  if (!IS_SD_INSERTED || IS_SD_PRINTING || IS_SD_FILE_OPEN || !card.cardOK)
67
+    return CTRL_NO_PRESENT;
68
+
69
+#ifdef DEBUG_MMC
70
+  char buffer[80];
71
+  sprintf(buffer, "SDRD: %d @ 0x%08x\n", nb_sector, addr);
72
+  MYSERIAL.print(buffer);
73
+#endif
74
+
75
+  // Start reading
76
+  if (!card.getSd2Card().readStart(addr))
77
+    return CTRL_FAIL;
78
+
79
+  // For each specified sector
80
+  while (nb_sector--) {
81
+
82
+    // Read a sector
83
+    card.getSd2Card().readData(sector_buf);
84
+
85
+    // RAM -> USB
86
+    if (!udi_msc_trans_block(true, sector_buf, SD_MMC_BLOCK_SIZE, NULL)) {
87
+      card.getSd2Card().readStop();
88
+      return CTRL_FAIL;
89
+    }
90
+  }
91
+
92
+  // Stop reading
93
+  card.getSd2Card().readStop();
94
+
95
+  // Done
96
+  return CTRL_GOOD;
97
+}
98
+
99
+Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector)
100
+{
101
+  if (!IS_SD_INSERTED || IS_SD_PRINTING || IS_SD_FILE_OPEN || !card.cardOK)
102
+    return CTRL_NO_PRESENT;
103
+
104
+#ifdef DEBUG_MMC
105
+  char buffer[80];
106
+  sprintf(buffer, "SDWR: %d @ 0x%08x\n", nb_sector, addr);
107
+  MYSERIAL.print(buffer);
108
+#endif
109
+
110
+  if (!card.getSd2Card().writeStart(addr, nb_sector))
111
+    return CTRL_FAIL;
112
+
113
+  // For each specified sector
114
+  while (nb_sector--) {
115
+
116
+    // USB -> RAM
117
+    if (!udi_msc_trans_block(false, sector_buf, SD_MMC_BLOCK_SIZE, NULL)) {
118
+      card.getSd2Card().writeStop();
119
+      return CTRL_FAIL;
120
+    }
121
+
122
+    // Write a sector
123
+    card.getSd2Card().writeData(sector_buf);
124
+  }
125
+
126
+  // Stop writing
127
+  card.getSd2Card().writeStop();
128
+
129
+  // Done
130
+  return CTRL_GOOD;
131
+}
132
+
133
+#endif // ACCESS_USB == true
134
+
135
+#endif

+ 177
- 0
Marlin/src/HAL/HAL_DUE/usb/sd_mmc_spi_mem.h View File

@@ -0,0 +1,177 @@
1
+/*****************************************************************************
2
+ *
3
+ * \file
4
+ *
5
+ * \brief CTRL_ACCESS interface for SD/MMC card.
6
+ *
7
+ * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
8
+ *
9
+ * \asf_license_start
10
+ *
11
+ * \page License
12
+ *
13
+ * Redistribution and use in source and binary forms, with or without
14
+ * modification, are permitted provided that the following conditions are met:
15
+ *
16
+ * 1. Redistributions of source code must retain the above copyright notice,
17
+ *    this list of conditions and the following disclaimer.
18
+ *
19
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
20
+ *    this list of conditions and the following disclaimer in the documentation
21
+ *    and/or other materials provided with the distribution.
22
+ *
23
+ * 3. The name of Atmel may not be used to endorse or promote products derived
24
+ *    from this software without specific prior written permission.
25
+ *
26
+ * 4. This software may only be redistributed and used in connection with an
27
+ *    Atmel microcontroller product.
28
+ *
29
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
30
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
32
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
33
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39
+ * POSSIBILITY OF SUCH DAMAGE.
40
+ *
41
+ * \asf_license_stop
42
+ *
43
+ ******************************************************************************/
44
+/*
45
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
46
+ */
47
+
48
+
49
+#ifndef _SD_MMC_SPI_MEM_H_
50
+#define _SD_MMC_SPI_MEM_H_
51
+
52
+/**
53
+ * \defgroup group_avr32_components_memory_sd_mmc_sd_mmc_spi_mem SD/MMC SPI Memory
54
+ *
55
+ * \ingroup group_avr32_components_memory_sd_mmc_sd_mmc_spi
56
+ *
57
+ * \{
58
+ */
59
+
60
+#include "conf_access.h"
61
+
62
+#if SD_MMC_SPI_MEM == DISABLE
63
+  #error sd_mmc_spi_mem.h is #included although SD_MMC_SPI_MEM is disabled
64
+#endif
65
+
66
+
67
+#include "ctrl_access.h"
68
+
69
+
70
+//_____ D E F I N I T I O N S ______________________________________________
71
+
72
+#define   SD_MMC_REMOVED       0
73
+#define   SD_MMC_INSERTED      1
74
+#define   SD_MMC_REMOVING      2
75
+
76
+
77
+//---- CONTROL FONCTIONS ----
78
+//!
79
+//! @brief This function initializes the hw/sw resources required to drive the SD_MMC_SPI.
80
+//!/
81
+extern void           sd_mmc_spi_mem_init(void);
82
+
83
+//!
84
+//! @brief This function tests the state of the SD_MMC memory and sends it to the Host.
85
+//!        For a PC, this device is seen as a removable media
86
+//!        Before indicating any modification of the status of the media (GOOD->NO_PRESENT or vice-versa),
87
+//!         the function must return the BUSY data to make the PC accepting the change
88
+//!
89
+//! @return                Ctrl_status
90
+//!   Media is ready       ->    CTRL_GOOD
91
+//!   Media not present    ->    CTRL_NO_PRESENT
92
+//!   Media has changed    ->    CTRL_BUSY
93
+//!/
94
+extern Ctrl_status    sd_mmc_spi_test_unit_ready(void);
95
+
96
+//!
97
+//! @brief This function gives the address of the last valid sector.
98
+//!
99
+//! @param *nb_sector  number of sector (sector = 512B). OUT
100
+//!
101
+//! @return                Ctrl_status
102
+//!   Media ready          ->  CTRL_GOOD
103
+//!   Media not present    ->  CTRL_NO_PRESENT
104
+//!/
105
+extern Ctrl_status    sd_mmc_spi_read_capacity(uint32_t *nb_sector);
106
+
107
+/*! \brief Unload/Load the SD/MMC card selected
108
+ *
109
+ * The START STOP UNIT SCSI optional command allows an application client to
110
+ * eject the removable medium on a LUN.
111
+ *
112
+ * \param unload \c true to unload the medium, \c false to load the medium.
113
+ *
114
+ * \return \c true if unload/load done success.
115
+ */
116
+extern bool sd_mmc_spi_unload(bool unload); 
117
+
118
+//!
119
+//! @brief This function returns the write protected status of the memory.
120
+//!
121
+//! Only used by memory removal with a HARDWARE SPECIFIC write protected detection
122
+//! ! The user must unplug the memory to change this write protected status,
123
+//! which cannot be for a SD_MMC.
124
+//!
125
+//! @return false  -> the memory is not write-protected (always)
126
+//!/
127
+extern bool           sd_mmc_spi_wr_protect(void);
128
+
129
+//!
130
+//! @brief This function tells if the memory has been removed or not.
131
+//!
132
+//! @return false  -> The memory isn't removed
133
+//!
134
+extern bool           sd_mmc_spi_removal(void);
135
+
136
+
137
+//---- ACCESS DATA FONCTIONS ----
138
+
139
+#if ACCESS_USB == true
140
+// Standard functions for open in read/write mode the device
141
+
142
+//!
143
+//! @brief This function performs a read operation of n sectors from a given address on.
144
+//! (sector = 512B)
145
+//!
146
+//!         DATA FLOW is: SD_MMC => USB
147
+//!
148
+//! @param addr         Sector address to start the read from
149
+//! @param nb_sector    Number of sectors to transfer
150
+//!
151
+//! @return                Ctrl_status
152
+//!   It is ready    ->    CTRL_GOOD
153
+//!   A error occur  ->    CTRL_FAIL
154
+//!
155
+extern Ctrl_status    sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector);
156
+
157
+//! This function initializes the SD/MMC memory for a write operation
158
+//!
159
+//!         DATA FLOW is: USB => SD_MMC
160
+//!
161
+//! (sector = 512B)
162
+//! @param addr         Sector address to start write
163
+//! @param nb_sector    Number of sectors to transfer
164
+//!
165
+//! @return                Ctrl_status
166
+//!   It is ready    ->    CTRL_GOOD
167
+//!   An error occurs  ->    CTRL_FAIL
168
+//!
169
+extern Ctrl_status    sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector);
170
+
171
+#endif // #if ACCESS_USB == true
172
+
173
+/**
174
+ * \}
175
+ */
176
+
177
+#endif  // _SD_MMC_SPI_MEM_H_

+ 337
- 0
Marlin/src/HAL/HAL_DUE/usb/spc_protocol.h View File

@@ -0,0 +1,337 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief SCSI Primary Commands
5
+ *
6
+ * This file contains definitions of some of the commands found in the
7
+ * SPC-2 standard.
8
+ *
9
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
10
+ *
11
+ * \asf_license_start
12
+ *
13
+ * \page License
14
+ *
15
+ * Redistribution and use in source and binary forms, with or without
16
+ * modification, are permitted provided that the following conditions are met:
17
+ *
18
+ * 1. Redistributions of source code must retain the above copyright notice,
19
+ *    this list of conditions and the following disclaimer.
20
+ *
21
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
22
+ *    this list of conditions and the following disclaimer in the documentation
23
+ *    and/or other materials provided with the distribution.
24
+ *
25
+ * 3. The name of Atmel may not be used to endorse or promote products derived
26
+ *    from this software without specific prior written permission.
27
+ *
28
+ * 4. This software may only be redistributed and used in connection with an
29
+ *    Atmel microcontroller product.
30
+ *
31
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
32
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
34
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
35
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41
+ * POSSIBILITY OF SUCH DAMAGE.
42
+ *
43
+ * \asf_license_stop
44
+ *
45
+ */
46
+/*
47
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
48
+ */
49
+#ifndef  _SPC_PROTOCOL_H_
50
+#define  _SPC_PROTOCOL_H_
51
+
52
+
53
+/**
54
+ * \ingroup usb_msc_protocol
55
+ * \defgroup usb_spc_protocol SCSI Primary Commands protocol definitions
56
+ *
57
+ * @{
58
+ */
59
+
60
+//! \name SCSI commands defined by SPC-2
61
+//@{
62
+#define  SPC_TEST_UNIT_READY              0x00
63
+#define  SPC_REQUEST_SENSE                0x03
64
+#define  SPC_INQUIRY                      0x12
65
+#define  SPC_MODE_SELECT6                 0x15
66
+#define  SPC_MODE_SENSE6                  0x1A
67
+#define  SPC_SEND_DIAGNOSTIC              0x1D
68
+#define  SPC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1E
69
+#define  SPC_MODE_SENSE10                 0x5A
70
+#define  SPC_REPORT_LUNS                  0xA0
71
+//@}
72
+
73
+//! \brief May be set in byte 0 of the INQUIRY CDB
74
+//@{
75
+//! Enable Vital Product Data
76
+#define  SCSI_INQ_REQ_EVPD    0x01
77
+//! Command Support Data specified by the PAGE OR OPERATION CODE field
78
+#define  SCSI_INQ_REQ_CMDT    0x02
79
+//@}
80
+
81
+COMPILER_PACK_SET(1)
82
+
83
+/**
84
+ * \brief SCSI Standard Inquiry data structure
85
+ */
86
+struct scsi_inquiry_data {
87
+	uint8_t pq_pdt; //!< Peripheral Qual / Peripheral Dev Type
88
+#define  SCSI_INQ_PQ_CONNECTED   0x00   //!< Peripheral connected
89
+#define  SCSI_INQ_PQ_NOT_CONN    0x20   //!< Peripheral not connected
90
+#define  SCSI_INQ_PQ_NOT_SUPP    0x60   //!< Peripheral not supported
91
+#define  SCSI_INQ_DT_DIR_ACCESS  0x00   //!< Direct Access (SBC)
92
+#define  SCSI_INQ_DT_SEQ_ACCESS  0x01   //!< Sequential Access
93
+#define  SCSI_INQ_DT_PRINTER     0x02   //!< Printer
94
+#define  SCSI_INQ_DT_PROCESSOR   0x03   //!< Processor device
95
+#define  SCSI_INQ_DT_WRITE_ONCE  0x04   //!< Write-once device
96
+#define  SCSI_INQ_DT_CD_DVD      0x05   //!< CD/DVD device
97
+#define  SCSI_INQ_DT_OPTICAL     0x07   //!< Optical Memory
98
+#define  SCSI_INQ_DT_MC          0x08   //!< Medium Changer
99
+#define  SCSI_INQ_DT_ARRAY       0x0c   //!< Storage Array Controller
100
+#define  SCSI_INQ_DT_ENCLOSURE   0x0d   //!< Enclosure Services
101
+#define  SCSI_INQ_DT_RBC         0x0e   //!< Simplified Direct Access
102
+#define  SCSI_INQ_DT_OCRW        0x0f   //!< Optical card reader/writer
103
+#define  SCSI_INQ_DT_BCC         0x10   //!< Bridge Controller Commands
104
+#define  SCSI_INQ_DT_OSD         0x11   //!< Object-based Storage
105
+#define  SCSI_INQ_DT_NONE        0x1f   //!< No Peripheral
106
+	uint8_t flags1; //!< Flags (byte 1)
107
+#define  SCSI_INQ_RMB            0x80   //!< Removable Medium
108
+	uint8_t version; //!< Version
109
+#define  SCSI_INQ_VER_NONE       0x00   //!< No standards conformance
110
+#define  SCSI_INQ_VER_SPC        0x03   //!< SCSI Primary Commands     (link to SBC)
111
+#define  SCSI_INQ_VER_SPC2       0x04   //!< SCSI Primary Commands - 2 (link to SBC-2)
112
+#define  SCSI_INQ_VER_SPC3       0x05   //!< SCSI Primary Commands - 3 (link to SBC-2)
113
+#define  SCSI_INQ_VER_SPC4       0x06   //!< SCSI Primary Commands - 4 (link to SBC-3)
114
+	uint8_t flags3; //!< Flags (byte 3)
115
+#define  SCSI_INQ_NORMACA        0x20   //!< Normal ACA Supported
116
+#define  SCSI_INQ_HISUP          0x10   //!< Hierarchal LUN addressing
117
+#define  SCSI_INQ_RSP_SPC2       0x02   //!< SPC-2 / SPC-3 response format
118
+	uint8_t addl_len; //!< Additional Length (n-4)
119
+#define  SCSI_INQ_ADDL_LEN(tot)  ((tot)-5) //!< Total length is \a tot
120
+	uint8_t flags5; //!< Flags (byte 5)
121
+#define  SCSI_INQ_SCCS           0x80
122
+	uint8_t flags6; //!< Flags (byte 6)
123
+#define  SCSI_INQ_BQUE           0x80
124
+#define  SCSI_INQ_ENCSERV        0x40
125
+#define  SCSI_INQ_MULTIP         0x10
126
+#define  SCSI_INQ_MCHGR          0x08
127
+#define  SCSI_INQ_ADDR16         0x01
128
+	uint8_t flags7; //!< Flags (byte 7)
129
+#define  SCSI_INQ_WBUS16         0x20
130
+#define  SCSI_INQ_SYNC           0x10
131
+#define  SCSI_INQ_LINKED         0x08
132
+#define  SCSI_INQ_CMDQUE         0x02
133
+	uint8_t vendor_id[8];   //!< T10 Vendor Identification
134
+	uint8_t product_id[16]; //!< Product Identification
135
+	uint8_t product_rev[4]; //!< Product Revision Level
136
+};
137
+
138
+/**
139
+ * \brief SCSI Standard Request sense data structure
140
+ */
141
+struct scsi_request_sense_data {
142
+	/* 1st byte: REQUEST SENSE response flags*/
143
+	uint8_t valid_reponse_code;
144
+#define  SCSI_SENSE_VALID              0x80 //!< Indicates the INFORMATION field contains valid information
145
+#define  SCSI_SENSE_RESPONSE_CODE_MASK 0x7F
146
+#define  SCSI_SENSE_CURRENT            0x70 //!< Response code 70h (current errors)
147
+#define  SCSI_SENSE_DEFERRED           0x71
148
+
149
+	/* 2nd byte */
150
+	uint8_t obsolete;
151
+
152
+	/* 3rd byte */
153
+	uint8_t sense_flag_key;
154
+#define  SCSI_SENSE_FILEMARK        0x80 //!< Indicates that the current command has read a filemark or setmark.
155
+#define  SCSI_SENSE_EOM             0x40 //!< Indicates that an end-of-medium condition exists.
156
+#define  SCSI_SENSE_ILI             0x20 //!< Indicates that the requested logical block length did not match the logical block length of the data on the medium.
157
+#define  SCSI_SENSE_RESERVED        0x10 //!< Reserved
158
+#define  SCSI_SENSE_KEY(x)          (x&0x0F) //!< Sense Key
159
+
160
+	/* 4th to 7th bytes - INFORMATION field */
161
+	uint8_t information[4];
162
+
163
+	/* 8th byte  - ADDITIONAL SENSE LENGTH field */
164
+	uint8_t AddSenseLen;
165
+#define  SCSI_SENSE_ADDL_LEN(total_len)   ((total_len) - 8)
166
+
167
+	/* 9th to 12th byte  - COMMAND-SPECIFIC INFORMATION field */
168
+	uint8_t CmdSpecINFO[4];
169
+
170
+	/* 13th byte  - ADDITIONAL SENSE CODE field */
171
+	uint8_t AddSenseCode;
172
+
173
+	/* 14th byte  - ADDITIONAL SENSE CODE QUALIFIER field */
174
+	uint8_t AddSnsCodeQlfr;
175
+
176
+	/* 15th byte  - FIELD REPLACEABLE UNIT CODE field */
177
+	uint8_t FldReplUnitCode;
178
+
179
+	/* 16th byte */
180
+	uint8_t SenseKeySpec[3];
181
+#define  SCSI_SENSE_SKSV            0x80 //!< Indicates the SENSE-KEY SPECIFIC field contains valid information
182
+};
183
+
184
+COMPILER_PACK_RESET()
185
+
186
+/* Vital Product Data page codes */
187
+enum scsi_vpd_page_code {
188
+	SCSI_VPD_SUPPORTED_PAGES = 0x00,
189
+	SCSI_VPD_UNIT_SERIAL_NUMBER = 0x80,
190
+	SCSI_VPD_DEVICE_IDENTIFICATION = 0x83,
191
+};
192
+#define  SCSI_VPD_HEADER_SIZE       4
193
+
194
+/* Constants associated with the Device Identification VPD page */
195
+#define  SCSI_VPD_ID_HEADER_SIZE    4
196
+
197
+#define  SCSI_VPD_CODE_SET_BINARY   1
198
+#define  SCSI_VPD_CODE_SET_ASCII    2
199
+#define  SCSI_VPD_CODE_SET_UTF8     3
200
+
201
+#define  SCSI_VPD_ID_TYPE_T10       1
202
+
203
+
204
+/* Sense keys */
205
+enum scsi_sense_key {
206
+	SCSI_SK_NO_SENSE = 0x0,
207
+	SCSI_SK_RECOVERED_ERROR = 0x1,
208
+	SCSI_SK_NOT_READY = 0x2,
209
+	SCSI_SK_MEDIUM_ERROR = 0x3,
210
+	SCSI_SK_HARDWARE_ERROR = 0x4,
211
+	SCSI_SK_ILLEGAL_REQUEST = 0x5,
212
+	SCSI_SK_UNIT_ATTENTION = 0x6,
213
+	SCSI_SK_DATA_PROTECT = 0x7,
214
+	SCSI_SK_BLANK_CHECK = 0x8,
215
+	SCSI_SK_VENDOR_SPECIFIC = 0x9,
216
+	SCSI_SK_COPY_ABORTED = 0xa,
217
+	SCSI_SK_ABORTED_COMMAND = 0xb,
218
+	SCSI_SK_VOLUME_OVERFLOW = 0xd,
219
+	SCSI_SK_MISCOMPARE = 0xe,
220
+};
221
+
222
+/* Additional Sense Code / Additional Sense Code Qualifier pairs */
223
+enum scsi_asc_ascq {
224
+	SCSI_ASC_NO_ADDITIONAL_SENSE_INFO = 0x0000,
225
+	SCSI_ASC_LU_NOT_READY_REBUILD_IN_PROGRESS = 0x0405,
226
+	SCSI_ASC_WRITE_ERROR = 0x0c00,
227
+	SCSI_ASC_UNRECOVERED_READ_ERROR = 0x1100,
228
+	SCSI_ASC_INVALID_COMMAND_OPERATION_CODE = 0x2000,
229
+	SCSI_ASC_INVALID_FIELD_IN_CDB = 0x2400,
230
+	SCSI_ASC_WRITE_PROTECTED = 0x2700,
231
+	SCSI_ASC_NOT_READY_TO_READY_CHANGE = 0x2800,
232
+	SCSI_ASC_MEDIUM_NOT_PRESENT = 0x3A00,
233
+	SCSI_ASC_INTERNAL_TARGET_FAILURE = 0x4400,
234
+};
235
+
236
+/**
237
+ * \brief SPC-2 Mode parameter
238
+ * This subclause describes the block descriptors and the pages
239
+ * used with MODE SELECT and MODE SENSE commands
240
+ * that are applicable to all SCSI devices.
241
+ */
242
+enum scsi_spc_mode {
243
+	SCSI_MS_MODE_VENDOR_SPEC = 0x00,
244
+	SCSI_MS_MODE_INFEXP = 0x1C,    // Informational exceptions control page
245
+	SCSI_MS_MODE_ALL = 0x3f,
246
+};
247
+
248
+/**
249
+ * \brief SPC-2 Informational exceptions control page
250
+ * See chapter 8.3.8
251
+ */
252
+struct spc_control_page_info_execpt {
253
+	uint8_t page_code;
254
+	uint8_t page_length;
255
+#define  SPC_MP_INFEXP_PAGE_LENGTH     0x0A
256
+	uint8_t flags1;
257
+#define  SPC_MP_INFEXP_PERF            (1<<7)   //!< Initiator Control
258
+#define  SPC_MP_INFEXP_EBF             (1<<5)   //!< Caching Analysis Permitted
259
+#define  SPC_MP_INFEXP_EWASC           (1<<4)   //!< Discontinuity
260
+#define  SPC_MP_INFEXP_DEXCPT          (1<<3)   //!< Size enable
261
+#define  SPC_MP_INFEXP_TEST            (1<<2)   //!< Writeback Cache Enable
262
+#define  SPC_MP_INFEXP_LOGERR          (1<<0)   //!< Log errors bit
263
+	uint8_t mrie;
264
+#define  SPC_MP_INFEXP_MRIE_NO_REPORT           0x00
265
+#define  SPC_MP_INFEXP_MRIE_ASYNC_EVENT         0x01
266
+#define  SPC_MP_INFEXP_MRIE_GEN_UNIT            0x02
267
+#define  SPC_MP_INFEXP_MRIE_COND_RECOV_ERROR    0x03
268
+#define  SPC_MP_INFEXP_MRIE_UNCOND_RECOV_ERROR  0x04
269
+#define  SPC_MP_INFEXP_MRIE_NO_SENSE            0x05
270
+#define  SPC_MP_INFEXP_MRIE_ONLY_REPORT         0x06
271
+	be32_t interval_timer;
272
+	be32_t report_count;
273
+};
274
+
275
+
276
+enum scsi_spc_mode_sense_pc {
277
+	SCSI_MS_SENSE_PC_CURRENT = 0,
278
+	SCSI_MS_SENSE_PC_CHANGEABLE = 1,
279
+	SCSI_MS_SENSE_PC_DEFAULT = 2,
280
+	SCSI_MS_SENSE_PC_SAVED = 3,
281
+};
282
+
283
+
284
+
285
+static inline bool scsi_mode_sense_dbd_is_set(const uint8_t * cdb)
286
+{
287
+	return (cdb[1] >> 3) & 1;
288
+}
289
+
290
+static inline uint8_t scsi_mode_sense_get_page_code(const uint8_t * cdb)
291
+{
292
+	return cdb[2] & 0x3f;
293
+}
294
+
295
+static inline uint8_t scsi_mode_sense_get_pc(const uint8_t * cdb)
296
+{
297
+	return cdb[2] >> 6;
298
+}
299
+
300
+/**
301
+ * \brief SCSI Mode Parameter Header used by MODE SELECT(6) and MODE
302
+ * SENSE(6)
303
+ */
304
+struct scsi_mode_param_header6 {
305
+	uint8_t mode_data_length;	//!< Number of bytes after this
306
+	uint8_t medium_type;	//!< Medium Type
307
+	uint8_t device_specific_parameter;	//!< Defined by command set
308
+	uint8_t block_descriptor_length;	//!< Length of block descriptors
309
+};
310
+
311
+/**
312
+ * \brief SCSI Mode Parameter Header used by MODE SELECT(10) and MODE
313
+ * SENSE(10)
314
+ */
315
+struct scsi_mode_param_header10 {
316
+	be16_t mode_data_length;	//!< Number of bytes after this
317
+	uint8_t medium_type;	//!< Medium Type
318
+	uint8_t device_specific_parameter;	//!< Defined by command set
319
+	uint8_t flags4;	//!< LONGLBA in bit 0
320
+	uint8_t reserved;
321
+	be16_t block_descriptor_length;	//!< Length of block descriptors
322
+};
323
+
324
+/**
325
+ * \brief SCSI Page_0 Mode Page header (SPF not set)
326
+ */
327
+struct scsi_mode_page_0_header {
328
+	uint8_t page_code;
329
+#define  SCSI_PAGE_CODE_PS          (1 << 7)	//!< Parameters Saveable
330
+#define  SCSI_PAGE_CODE_SPF         (1 << 6)	//!< SubPage Format
331
+	uint8_t page_length;	//!< Number of bytes after this
332
+#define  SCSI_MS_PAGE_LEN(total)   ((total) - 2)
333
+};
334
+
335
+//@}
336
+
337
+#endif // SPC_PROTOCOL_H_

+ 85
- 0
Marlin/src/HAL/HAL_DUE/usb/stringz.h View File

@@ -0,0 +1,85 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Preprocessor stringizing utils.
5
+ *
6
+ * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _STRINGZ_H_
48
+#define _STRINGZ_H_
49
+
50
+/**
51
+ * \defgroup group_sam_utils_stringz Preprocessor - Stringize
52
+ *
53
+ * \ingroup group_sam_utils
54
+ *
55
+ * \{
56
+ */
57
+
58
+/*! \brief Stringize.
59
+ *
60
+ * Stringize a preprocessing token, this token being allowed to be \#defined.
61
+ *
62
+ * May be used only within macros with the token passed as an argument if the token is \#defined.
63
+ *
64
+ * For example, writing STRINGZ(PIN) within a macro \#defined by PIN_NAME(PIN)
65
+ * and invoked as PIN_NAME(PIN0) with PIN0 \#defined as A0 is equivalent to
66
+ * writing "A0".
67
+ */
68
+#define STRINGZ(x)                                #x
69
+
70
+/*! \brief Absolute stringize.
71
+ *
72
+ * Stringize a preprocessing token, this token being allowed to be \#defined.
73
+ *
74
+ * No restriction of use if the token is \#defined.
75
+ *
76
+ * For example, writing ASTRINGZ(PIN0) anywhere with PIN0 \#defined as A0 is
77
+ * equivalent to writing "A0".
78
+ */
79
+#define ASTRINGZ(x)                               STRINGZ(x)
80
+
81
+/**
82
+ * \}
83
+ */
84
+
85
+#endif  // _STRINGZ_H_

+ 122
- 0
Marlin/src/HAL/HAL_DUE/usb/sysclk.c View File

@@ -0,0 +1,122 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Chip-specific system clock management functions.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifdef ARDUINO_ARCH_SAM
48
+ 
49
+#include "sysclk.h"
50
+
51
+/// @cond 0
52
+/**INDENT-OFF**/
53
+#ifdef __cplusplus
54
+extern "C" {
55
+#endif
56
+/**INDENT-ON**/
57
+/// @endcond
58
+
59
+/**
60
+ * \weakgroup sysclk_group
61
+ * @{
62
+ */
63
+
64
+#if defined(CONFIG_USBCLK_SOURCE) || defined(__DOXYGEN__)
65
+/**
66
+ * \brief Enable full speed USB clock.
67
+ *
68
+ * \note The SAM3X PMC hardware interprets div as div+1. For readability the hardware div+1
69
+ * is hidden in this implementation. Use div as div effective value.
70
+ *
71
+ * \param pll_id Source of the USB clock.
72
+ * \param div Actual clock divisor. Must be superior to 0.
73
+ */
74
+void sysclk_enable_usb(void)
75
+{
76
+	Assert(CONFIG_USBCLK_DIV > 0);
77
+
78
+#ifdef CONFIG_PLL0_SOURCE
79
+	if (CONFIG_USBCLK_SOURCE == USBCLK_SRC_PLL0) {
80
+		struct pll_config pllcfg;
81
+
82
+		pll_enable_source(CONFIG_PLL0_SOURCE);
83
+		pll_config_defaults(&pllcfg, 0);
84
+		pll_enable(&pllcfg, 0);
85
+		pll_wait_for_lock(0);
86
+		pmc_switch_udpck_to_pllack(CONFIG_USBCLK_DIV - 1);
87
+		pmc_enable_udpck();
88
+		return;
89
+	}
90
+#endif
91
+
92
+	if (CONFIG_USBCLK_SOURCE == USBCLK_SRC_UPLL) {
93
+
94
+		pmc_enable_upll_clock();
95
+		pmc_switch_udpck_to_upllck(CONFIG_USBCLK_DIV - 1);
96
+		pmc_enable_udpck();
97
+		return;
98
+	}
99
+}
100
+
101
+/**
102
+ * \brief Disable full speed USB clock.
103
+ *
104
+ * \note This implementation does not switch off the PLL, it just turns off the USB clock.
105
+ */
106
+void sysclk_disable_usb(void)
107
+{
108
+	pmc_disable_udpck();
109
+}
110
+#endif // CONFIG_USBCLK_SOURCE
111
+
112
+//! @}
113
+
114
+/// @cond 0
115
+/**INDENT-OFF**/
116
+#ifdef __cplusplus
117
+}
118
+#endif
119
+/**INDENT-ON**/
120
+/// @endcond
121
+
122
+#endif

+ 229
- 0
Marlin/src/HAL/HAL_DUE/usb/sysclk.h View File

@@ -0,0 +1,229 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Chip-specific system clock management functions.
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef CHIP_SYSCLK_H_INCLUDED
48
+#define CHIP_SYSCLK_H_INCLUDED
49
+
50
+#include "osc.h"
51
+#include "pll.h"
52
+
53
+/**
54
+ * \page sysclk_quickstart Quick Start Guide for the System Clock Management service (SAM3A)
55
+ *
56
+ * This is the quick start guide for the \ref sysclk_group "System Clock Management"
57
+ * service, with step-by-step instructions on how to configure and use the service for
58
+ * specific use cases.
59
+ *
60
+ * \section sysclk_quickstart_usecases System Clock Management use cases
61
+ * - \ref sysclk_quickstart_basic
62
+ *
63
+ * \section sysclk_quickstart_basic Basic usage of the System Clock Management service
64
+ * This section will present a basic use case for the System Clock Management service.
65
+ * This use case will configure the main system clock to 84MHz, using an internal PLL
66
+ * module to multiply the frequency of a crystal attached to the microcontroller.
67
+ *
68
+ * \subsection sysclk_quickstart_use_case_1_prereq Prerequisites
69
+ *  - None
70
+ *
71
+ * \subsection sysclk_quickstart_use_case_1_setup_steps Initialization code
72
+ * Add to the application initialization code:
73
+ * \code
74
+	sysclk_init();
75
+\endcode
76
+ *
77
+ * \subsection sysclk_quickstart_use_case_1_setup_steps_workflow Workflow
78
+ * -# Configure the system clocks according to the settings in conf_clock.h:
79
+ *    \code sysclk_init(); \endcode
80
+ *
81
+ * \subsection sysclk_quickstart_use_case_1_example_code Example code
82
+ *   Add or uncomment the following in your conf_clock.h header file, commenting out all other
83
+ *   definitions of the same symbol(s):
84
+ *   \code
85
+	   #define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_PLLACK
86
+
87
+	   // Fpll0 = (Fclk * PLL_mul) / PLL_div
88
+	   #define CONFIG_PLL0_SOURCE          PLL_SRC_MAINCK_XTAL
89
+	   #define CONFIG_PLL0_MUL             (84000000UL / BOARD_FREQ_MAINCK_XTAL)
90
+	   #define CONFIG_PLL0_DIV             1
91
+
92
+	   // Fbus = Fsys / BUS_div
93
+	   #define CONFIG_SYSCLK_PRES          SYSCLK_PRES_1
94
+\endcode
95
+ *
96
+ * \subsection sysclk_quickstart_use_case_1_example_workflow Workflow
97
+ *  -# Configure the main system clock to use the output of the PLL module as its source:
98
+ *   \code #define CONFIG_SYSCLK_SOURCE          SYSCLK_SRC_PLLACK \endcode
99
+ *  -# Configure the PLL module to use the fast external fast crystal oscillator as its source:
100
+ *   \code #define CONFIG_PLL0_SOURCE            PLL_SRC_MAINCK_XTAL \endcode
101
+ *  -# Configure the PLL module to multiply the external fast crystal oscillator frequency up to 84MHz:
102
+ *   \code
103
+	#define CONFIG_PLL0_MUL             (84000000UL / BOARD_FREQ_MAINCK_XTAL)
104
+	#define CONFIG_PLL0_DIV             1
105
+\endcode
106
+ *   \note For user boards, \c BOARD_FREQ_MAINCK_XTAL should be defined in the board \c conf_board.h configuration
107
+ *         file as the frequency of the fast crystal attached to the microcontroller.
108
+ *  -# Configure the main clock to run at the full 84MHz, disable scaling of the main system clock speed:
109
+ *    \code
110
+	#define CONFIG_SYSCLK_PRES         SYSCLK_PRES_1
111
+\endcode
112
+ *    \note Some dividers are powers of two, while others are integer division factors. Refer to the
113
+ *          formulas in the conf_clock.h template commented above each division define.
114
+ */
115
+
116
+/// @cond 0
117
+/**INDENT-OFF**/
118
+#ifdef __cplusplus
119
+extern "C" {
120
+#endif
121
+/**INDENT-ON**/
122
+/// @endcond
123
+
124
+/**
125
+ * \weakgroup sysclk_group
126
+ * @{
127
+ */
128
+
129
+//! \name Configuration Symbols
130
+//@{
131
+/**
132
+ * \def CONFIG_SYSCLK_SOURCE
133
+ * \brief Initial/static main system clock source
134
+ *
135
+ * The main system clock will be configured to use this clock during
136
+ * initialization.
137
+ */
138
+#ifndef CONFIG_SYSCLK_SOURCE
139
+# define CONFIG_SYSCLK_SOURCE   SYSCLK_SRC_MAINCK_4M_RC
140
+#endif
141
+/**
142
+ * \def CONFIG_SYSCLK_PRES
143
+ * \brief Initial CPU clock divider (mck)
144
+ *
145
+ * The MCK will run at
146
+ * \f[
147
+ *   f_{MCK} = \frac{f_{sys}}{\mathrm{CONFIG\_SYSCLK\_PRES}}\,\mbox{Hz}
148
+ * \f]
149
+ * after initialization.
150
+ */
151
+#ifndef CONFIG_SYSCLK_PRES
152
+# define CONFIG_SYSCLK_PRES  0
153
+#endif
154
+
155
+//@}
156
+
157
+//! \name Master Clock Sources (MCK)
158
+//@{
159
+#define SYSCLK_SRC_SLCK_RC              0       //!< Internal 32kHz RC oscillator as master source clock
160
+#define SYSCLK_SRC_SLCK_XTAL            1       //!< External 32kHz crystal oscillator as master source clock
161
+#define SYSCLK_SRC_SLCK_BYPASS          2       //!< External 32kHz bypass oscillator as master source clock
162
+#define SYSCLK_SRC_MAINCK_4M_RC         3       //!< Internal 4MHz RC oscillator as master source clock
163
+#define SYSCLK_SRC_MAINCK_8M_RC         4       //!< Internal 8MHz RC oscillator as master source clock
164
+#define SYSCLK_SRC_MAINCK_12M_RC        5       //!< Internal 12MHz RC oscillator as master source clock
165
+#define SYSCLK_SRC_MAINCK_XTAL          6       //!< External crystal oscillator as master source clock
166
+#define SYSCLK_SRC_MAINCK_BYPASS        7       //!< External bypass oscillator as master source clock
167
+#define SYSCLK_SRC_PLLACK               8       //!< Use PLLACK as master source clock
168
+#define SYSCLK_SRC_UPLLCK               9       //!< Use UPLLCK as master source clock
169
+//@}
170
+
171
+//! \name Master Clock Prescalers (MCK)
172
+//@{
173
+#define SYSCLK_PRES_1           PMC_MCKR_PRES_CLK_1     //!< Set master clock prescaler to 1
174
+#define SYSCLK_PRES_2           PMC_MCKR_PRES_CLK_2     //!< Set master clock prescaler to 2
175
+#define SYSCLK_PRES_4           PMC_MCKR_PRES_CLK_4     //!< Set master clock prescaler to 4
176
+#define SYSCLK_PRES_8           PMC_MCKR_PRES_CLK_8     //!< Set master clock prescaler to 8
177
+#define SYSCLK_PRES_16          PMC_MCKR_PRES_CLK_16    //!< Set master clock prescaler to 16
178
+#define SYSCLK_PRES_32          PMC_MCKR_PRES_CLK_32    //!< Set master clock prescaler to 32
179
+#define SYSCLK_PRES_64          PMC_MCKR_PRES_CLK_64    //!< Set master clock prescaler to 64
180
+#define SYSCLK_PRES_3           PMC_MCKR_PRES_CLK_3     //!< Set master clock prescaler to 3
181
+//@}
182
+
183
+//! \name USB Clock Sources
184
+//@{
185
+#define USBCLK_SRC_PLL0       0     //!< Use PLLA
186
+#define USBCLK_SRC_UPLL       1     //!< Use UPLL
187
+//@}
188
+
189
+/**
190
+ * \def CONFIG_USBCLK_SOURCE
191
+ * \brief Configuration symbol for the USB generic clock source
192
+ *
193
+ * Sets the clock source to use for the USB. The source must also be properly
194
+ * configured.
195
+ *
196
+ * Define this to one of the \c USBCLK_SRC_xxx settings. Leave it undefined if
197
+ * USB is not required.
198
+ */
199
+#ifdef __DOXYGEN__
200
+# define CONFIG_USBCLK_SOURCE
201
+#endif
202
+
203
+/**
204
+ * \def CONFIG_USBCLK_DIV
205
+ * \brief Configuration symbol for the USB generic clock divider setting
206
+ *
207
+ * Sets the clock division for the USB generic clock. If a USB clock source is
208
+ * selected with CONFIG_USBCLK_SOURCE, this configuration symbol must also be
209
+ * defined.
210
+ */
211
+#ifdef __DOXYGEN__
212
+# define CONFIG_USBCLK_DIV
213
+#endif
214
+
215
+
216
+extern void sysclk_enable_usb(void);
217
+extern void sysclk_disable_usb(void);
218
+
219
+//! @}
220
+
221
+/// @cond 0
222
+/**INDENT-OFF**/
223
+#ifdef __cplusplus
224
+}
225
+#endif
226
+/**INDENT-ON**/
227
+/// @endcond
228
+
229
+#endif /* CHIP_SYSCLK_H_INCLUDED */

+ 105
- 0
Marlin/src/HAL/HAL_DUE/usb/tpaste.h View File

@@ -0,0 +1,105 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Preprocessor token pasting utils.
5
+ *
6
+ * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _TPASTE_H_
48
+#define _TPASTE_H_
49
+
50
+/**
51
+ * \defgroup group_sam_utils_tpaste Preprocessor - Token Paste
52
+ *
53
+ * \ingroup group_sam_utils
54
+ *
55
+ * \{
56
+ */
57
+
58
+/*! \name Token Paste
59
+ *
60
+ * Paste N preprocessing tokens together, these tokens being allowed to be \#defined.
61
+ *
62
+ * May be used only within macros with the tokens passed as arguments if the tokens are \#defined.
63
+ *
64
+ * For example, writing TPASTE2(U, WIDTH) within a macro \#defined by
65
+ * UTYPE(WIDTH) and invoked as UTYPE(UL_WIDTH) with UL_WIDTH \#defined as 32 is
66
+ * equivalent to writing U32.
67
+ */
68
+//! @{
69
+#define TPASTE2( a, b)                            a##b
70
+#define TPASTE3( a, b, c)                         a##b##c
71
+#define TPASTE4( a, b, c, d)                      a##b##c##d
72
+#define TPASTE5( a, b, c, d, e)                   a##b##c##d##e
73
+#define TPASTE6( a, b, c, d, e, f)                a##b##c##d##e##f
74
+#define TPASTE7( a, b, c, d, e, f, g)             a##b##c##d##e##f##g
75
+#define TPASTE8( a, b, c, d, e, f, g, h)          a##b##c##d##e##f##g##h
76
+#define TPASTE9( a, b, c, d, e, f, g, h, i)       a##b##c##d##e##f##g##h##i
77
+#define TPASTE10(a, b, c, d, e, f, g, h, i, j)    a##b##c##d##e##f##g##h##i##j
78
+//! @}
79
+
80
+/*! \name Absolute Token Paste
81
+ *
82
+ * Paste N preprocessing tokens together, these tokens being allowed to be \#defined.
83
+ *
84
+ * No restriction of use if the tokens are \#defined.
85
+ *
86
+ * For example, writing ATPASTE2(U, UL_WIDTH) anywhere with UL_WIDTH \#defined
87
+ * as 32 is equivalent to writing U32.
88
+ */
89
+//! @{
90
+#define ATPASTE2( a, b)                           TPASTE2( a, b)
91
+#define ATPASTE3( a, b, c)                        TPASTE3( a, b, c)
92
+#define ATPASTE4( a, b, c, d)                     TPASTE4( a, b, c, d)
93
+#define ATPASTE5( a, b, c, d, e)                  TPASTE5( a, b, c, d, e)
94
+#define ATPASTE6( a, b, c, d, e, f)               TPASTE6( a, b, c, d, e, f)
95
+#define ATPASTE7( a, b, c, d, e, f, g)            TPASTE7( a, b, c, d, e, f, g)
96
+#define ATPASTE8( a, b, c, d, e, f, g, h)         TPASTE8( a, b, c, d, e, f, g, h)
97
+#define ATPASTE9( a, b, c, d, e, f, g, h, i)      TPASTE9( a, b, c, d, e, f, g, h, i)
98
+#define ATPASTE10(a, b, c, d, e, f, g, h, i, j)   TPASTE10(a, b, c, d, e, f, g, h, i, j)
99
+//! @}
100
+
101
+/**
102
+ * \}
103
+ */
104
+
105
+#endif  // _TPASTE_H_

+ 1149
- 0
Marlin/src/HAL/HAL_DUE/usb/udc.c
File diff suppressed because it is too large
View File


+ 697
- 0
Marlin/src/HAL/HAL_DUE/usb/udc.h View File

@@ -0,0 +1,697 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Interface of the USB Device Controller (UDC)
5
+ *
6
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _UDC_H_
48
+#define _UDC_H_
49
+
50
+#include "conf_usb.h"
51
+#include "usb_protocol.h"
52
+#include "udc_desc.h"
53
+#include "udd.h"
54
+
55
+#if USB_DEVICE_VENDOR_ID == 0
56
+#   error USB_DEVICE_VENDOR_ID cannot be equal to 0
57
+#endif
58
+
59
+#if USB_DEVICE_PRODUCT_ID == 0
60
+#   error USB_DEVICE_PRODUCT_ID cannot be equal to 0
61
+#endif
62
+
63
+#ifdef __cplusplus
64
+extern "C" {
65
+#endif
66
+
67
+/**
68
+ * \ingroup usb_device_group
69
+ * \defgroup udc_group USB Device Controller (UDC)
70
+ *
71
+ * The UDC provides a high-level abstraction of the usb device.
72
+ * You can use these functions to control the main device state
73
+ * (start/attach/wakeup).
74
+ *
75
+ * \section USB_DEVICE_CONF USB Device Custom configuration
76
+ * The following USB Device configuration must be included in the conf_usb.h
77
+ * file of the application.
78
+ *
79
+ * USB_DEVICE_VENDOR_ID (Word)<br>
80
+ * Vendor ID provided by USB org (ATMEL 0x03EB).
81
+ *
82
+ * USB_DEVICE_PRODUCT_ID (Word)<br>
83
+ * Product ID (Referenced in usb_atmel.h).
84
+ *
85
+ * USB_DEVICE_MAJOR_VERSION (Byte)<br>
86
+ * Major version of the device
87
+ *
88
+ * USB_DEVICE_MINOR_VERSION (Byte)<br>
89
+ * Minor version of the device
90
+ *
91
+ * USB_DEVICE_MANUFACTURE_NAME (string)<br>
92
+ * ASCII name for the manufacture
93
+ *
94
+ * USB_DEVICE_PRODUCT_NAME (string)<br>
95
+ * ASCII name for the product
96
+ *
97
+ * USB_DEVICE_SERIAL_NAME (string)<br>
98
+ * ASCII name to enable and set a serial number
99
+ *
100
+ * USB_DEVICE_POWER (Numeric)<br>
101
+ * (unit mA) Maximum device power
102
+ *
103
+ * USB_DEVICE_ATTR (Byte)<br>
104
+ * USB attributes available:
105
+ *  - USB_CONFIG_ATTR_SELF_POWERED
106
+ *  - USB_CONFIG_ATTR_REMOTE_WAKEUP
107
+ *  Note: if remote wake enabled then defines remotewakeup callbacks,
108
+ * see Table 5-2. External API from UDC - Callback
109
+ *
110
+ * USB_DEVICE_LOW_SPEED (Only defined)<br>
111
+ * Force the USB Device to run in low speed
112
+ *
113
+ * USB_DEVICE_HS_SUPPORT (Only defined)<br>
114
+ * Authorize the USB Device to run in high speed
115
+ *
116
+ * USB_DEVICE_MAX_EP (Byte)<br>
117
+ * Define the maximum endpoint number used by the USB Device.<br>
118
+ * This one is already defined in UDI default configuration.
119
+ * Ex:
120
+ * - When endpoint control 0x00, endpoint 0x01 and
121
+ *   endpoint 0x82 is used then USB_DEVICE_MAX_EP=2
122
+ * - When only endpoint control 0x00 is used then USB_DEVICE_MAX_EP=0
123
+ * - When endpoint 0x01 and endpoint 0x81 is used then USB_DEVICE_MAX_EP=1<br>
124
+ *   (configuration not possible on USBB interface)
125
+ * @{
126
+ */
127
+
128
+/**
129
+ * \brief Authorizes the VBUS event
130
+ *
131
+ * \return true, if the VBUS monitoring is possible.
132
+ *
133
+ * \section udc_vbus_monitoring VBus monitoring used cases
134
+ *
135
+ * The VBus monitoring is used only for USB SELF Power application.
136
+ *
137
+ * - By default the USB device is automatically attached when Vbus is high
138
+ * or when USB is start for devices without internal Vbus monitoring.
139
+ * conf_usb.h file does not contains define USB_DEVICE_ATTACH_AUTO_DISABLE.
140
+ * \code //#define USB_DEVICE_ATTACH_AUTO_DISABLE \endcode
141
+ *
142
+ * - Add custom VBUS monitoring. conf_usb.h file contains define
143
+ * USB_DEVICE_ATTACH_AUTO_DISABLE:
144
+ * \code #define USB_DEVICE_ATTACH_AUTO_DISABLE \endcode
145
+ * User C file contains:
146
+ * \code  
147
+	// Authorize VBUS monitoring
148
+	if (!udc_include_vbus_monitoring()) {
149
+	  // Implement custom VBUS monitoring via GPIO or other
150
+	}
151
+	Event_VBUS_present() // VBUS interrupt or GPIO interrupt or other
152
+	{
153
+	  // Attach USB Device
154
+	  udc_attach();
155
+	}
156
+\endcode
157
+ *
158
+ * - Case of battery charging. conf_usb.h file contains define
159
+ * USB_DEVICE_ATTACH_AUTO_DISABLE:
160
+ * \code #define USB_DEVICE_ATTACH_AUTO_DISABLE \endcode
161
+ * User C file contains:
162
+ * \code  
163
+	Event VBUS present() // VBUS interrupt or GPIO interrupt or ..
164
+	{
165
+	  // Authorize battery charging, but wait key press to start USB.
166
+	}
167
+	Event Key press()
168
+	{
169
+	  // Stop batteries charging
170
+	  // Start USB
171
+	  udc_attach();
172
+	}
173
+\endcode
174
+ */
175
+static inline bool udc_include_vbus_monitoring(void)
176
+{
177
+	return udd_include_vbus_monitoring();
178
+}
179
+
180
+/*! \brief Start the USB Device stack
181
+ */
182
+void udc_start(void);
183
+
184
+/*! \brief Stop the USB Device stack
185
+ */
186
+void udc_stop(void);
187
+
188
+/**
189
+ * \brief Attach device to the bus when possible
190
+ *
191
+ * \warning If a VBus control is included in driver,
192
+ * then it will attach device when an acceptable Vbus
193
+ * level from the host is detected.
194
+ */
195
+static inline void udc_attach(void)
196
+{
197
+	udd_attach();
198
+}
199
+
200
+
201
+/**
202
+ * \brief Detaches the device from the bus
203
+ *
204
+ * The driver must remove pull-up on USB line D- or D+.
205
+ */
206
+static inline void udc_detach(void)
207
+{
208
+	udd_detach();
209
+}
210
+
211
+
212
+/*! \brief The USB driver sends a resume signal called \e "Upstream Resume"
213
+ * This is authorized only when the remote wakeup feature is enabled by host.
214
+ */
215
+static inline void udc_remotewakeup(void)
216
+{
217
+	udd_send_remotewakeup();
218
+}
219
+
220
+
221
+/**
222
+ * \brief Returns a pointer on the current interface descriptor
223
+ *
224
+ * \return pointer on the current interface descriptor.
225
+ */
226
+usb_iface_desc_t UDC_DESC_STORAGE *udc_get_interface_desc(void);
227
+
228
+//@}
229
+
230
+/**
231
+ * \ingroup usb_group
232
+ * \defgroup usb_device_group USB Stack Device
233
+ *
234
+ * This module includes USB Stack Device implementation.
235
+ * The stack is divided in three parts:
236
+ * - USB Device Controller (UDC) provides USB chapter 9 compliance
237
+ * - USB Device Interface (UDI) provides USB Class compliance
238
+ * - USB Device Driver (UDD) provides USB Driver for each Atmel MCU
239
+
240
+ * Many USB Device applications can be implemented on Atmel MCU.
241
+ * Atmel provides many application notes for different applications:
242
+ * - AVR4900, provides general information about Device Stack
243
+ * - AVR4901, explains how to create a new class
244
+ * - AVR4902, explains how to create a composite device
245
+ * - AVR49xx, all device classes provided in ASF have an application note
246
+ *
247
+ * A basic USB knowledge is required to understand the USB Device
248
+ * Class application notes (HID,MS,CDC,PHDC,...).
249
+ * Then, to create an USB device with
250
+ * only one class provided by ASF, refer directly to the application note
251
+ * corresponding to this USB class. The USB Device application note for
252
+ * New Class and Composite is dedicated to advanced USB users.
253
+ *
254
+ * @{
255
+ */
256
+
257
+//! @}
258
+
259
+#ifdef __cplusplus
260
+}
261
+#endif
262
+
263
+/**
264
+ * \ingroup udc_group
265
+ * \defgroup udc_basic_use_case_setup_prereq USB Device Controller (UDC) - Prerequisites
266
+ * Common prerequisites for all USB devices.
267
+ *
268
+ * This module is based on USB device stack full interrupt driven, and supporting
269
+ * \ref sleepmgr_group sleepmgr. For AVR and SAM3/4 devices the \ref clk_group clock services
270
+ * is supported. For SAMD devices the \ref asfdoc_sam0_system_clock_group clock driver is supported.
271
+ *
272
+ * The following procedure must be executed to setup the project correctly:
273
+ * - Specify the clock configuration:
274
+ *   - XMEGA USB devices need 48MHz clock input.\n
275
+ *     XMEGA USB devices need CPU frequency higher than 12MHz.\n
276
+ *     You can use either an internal RC48MHz auto calibrated by Start of Frames
277
+ *     or an external OSC.
278
+ *   - UC3 and SAM3/4 devices without USB high speed support need 48MHz clock input.\n
279
+ *     You must use a PLL and an external OSC.
280
+ *   - UC3 and SAM3/4 devices with USB high speed support need 12MHz clock input.\n
281
+ *     You must use an external OSC.
282
+ *   - UC3 devices with USBC hardware need CPU frequency higher than 25MHz.
283
+ *   - SAMD devices without USB high speed support need 48MHz clock input.\n
284
+ *     You should use DFLL with USBCRM.
285
+ * - In conf_board.h, the define CONF_BOARD_USB_PORT must be added to enable USB lines.
286
+ * (Not mandatory for all boards)
287
+ * - Enable interrupts
288
+ * - Initialize the clock service
289
+ *
290
+ * The usage of \ref sleepmgr_group sleepmgr service is optional, but recommended to reduce power
291
+ * consumption:
292
+ * - Initialize the sleep manager service
293
+ * - Activate sleep mode when the application is in IDLE state
294
+ *
295
+ * \subpage udc_conf_clock.
296
+ *
297
+ * for AVR and SAM3/4 devices, add to the initialization code:
298
+ * \code
299
+	sysclk_init();
300
+	irq_initialize_vectors();
301
+	cpu_irq_enable();
302
+	board_init();
303
+	sleepmgr_init(); // Optional
304
+\endcode
305
+ *
306
+ * For SAMD devices, add to the initialization code:
307
+ * \code
308
+	system_init();
309
+	irq_initialize_vectors();
310
+	cpu_irq_enable();
311
+	sleepmgr_init(); // Optional
312
+\endcode
313
+ * Add to the main IDLE loop:
314
+ * \code
315
+	sleepmgr_enter_sleep(); // Optional
316
+\endcode
317
+ *
318
+ */
319
+
320
+/**
321
+ * \ingroup udc_group
322
+ * \defgroup udc_basic_use_case_setup_code USB Device Controller (UDC) - Example code
323
+ * Common example code for all USB devices.
324
+ *
325
+ * Content of conf_usb.h:
326
+ * \code
327
+	#define USB_DEVICE_VENDOR_ID 0x03EB
328
+	#define USB_DEVICE_PRODUCT_ID 0xXXXX
329
+	#define USB_DEVICE_MAJOR_VERSION 1
330
+	#define USB_DEVICE_MINOR_VERSION 0
331
+	#define USB_DEVICE_POWER 100
332
+	#define USB_DEVICE_ATTR USB_CONFIG_ATTR_BUS_POWERED
333
+\endcode
334
+ *
335
+ * Add to application C-file:
336
+ * \code
337
+	void usb_init(void)
338
+	{
339
+	  udc_start();
340
+	}
341
+\endcode
342
+ */
343
+
344
+/**
345
+ * \ingroup udc_group
346
+ * \defgroup udc_basic_use_case_setup_flow USB Device Controller (UDC) - Workflow
347
+ * Common workflow for all USB devices.
348
+ *
349
+ * -# Ensure that conf_usb.h is available and contains the following configuration
350
+ * which is the main USB device configuration:
351
+ *   - \code // Vendor ID provided by USB org (ATMEL 0x03EB)
352
+	#define USB_DEVICE_VENDOR_ID 0x03EB // Type Word
353
+	// Product ID (Atmel PID referenced in usb_atmel.h)
354
+	#define USB_DEVICE_PRODUCT_ID 0xXXXX // Type Word
355
+	// Major version of the device
356
+	#define USB_DEVICE_MAJOR_VERSION 1 // Type Byte
357
+	// Minor version of the device
358
+	#define USB_DEVICE_MINOR_VERSION 0 // Type Byte
359
+	// Maximum device power (mA)
360
+	#define USB_DEVICE_POWER 100 // Type 9-bits
361
+	// USB attributes to enable features
362
+	#define USB_DEVICE_ATTR USB_CONFIG_ATTR_BUS_POWERED // Flags \endcode
363
+ * -# Call the USB device stack start function to enable stack and start USB:
364
+ *   - \code udc_start(); \endcode
365
+ *     \note In case of USB dual roles (Device and Host) managed through USB OTG connector
366
+ * (USB ID pin), the call of udc_start() must be removed and replaced by uhc_start().
367
+ * SeRefer to "AVR4950 section 6.1 Dual roles" for further information about dual roles.
368
+ */
369
+
370
+/**
371
+ * \page udc_conf_clock conf_clock.h examples with USB support
372
+ *
373
+ * Content of XMEGA conf_clock.h:
374
+ * \code
375
+	// Configuration based on internal RC:
376
+	// USB clock need of 48Mhz
377
+	#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_RCOSC
378
+	#define CONFIG_OSC_RC32_CAL         48000000UL
379
+	#define CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC  OSC_ID_USBSOF
380
+	// CPU clock need of clock > 12MHz to run with USB (Here 24MHz)
381
+	#define CONFIG_SYSCLK_SOURCE     SYSCLK_SRC_RC32MHZ
382
+	#define CONFIG_SYSCLK_PSADIV     SYSCLK_PSADIV_2
383
+	#define CONFIG_SYSCLK_PSBCDIV    SYSCLK_PSBCDIV_1_1
384
+\endcode
385
+ *
386
+ * Content of conf_clock.h for AT32UC3A0, AT32UC3A1, AT32UC3B devices (USBB):
387
+ * \code
388
+	// Configuration based on 12MHz external OSC:
389
+	#define CONFIG_PLL1_SOURCE          PLL_SRC_OSC0
390
+	#define CONFIG_PLL1_MUL             8
391
+	#define CONFIG_PLL1_DIV             2
392
+	#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_PLL1
393
+	#define CONFIG_USBCLK_DIV           1 // Fusb = Fsys/(2 ^ USB_div)
394
+\endcode
395
+ *
396
+ * Content of conf_clock.h for AT32UC3A3, AT32UC3A4 devices (USBB with high speed support):
397
+ * \code
398
+	// Configuration based on 12MHz external OSC:
399
+	#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_OSC0
400
+	#define CONFIG_USBCLK_DIV           1 // Fusb = Fsys/(2 ^ USB_div)
401
+\endcode
402
+ *
403
+ * Content of conf_clock.h for AT32UC3C, ATUCXXD, ATUCXXL3U, ATUCXXL4U devices (USBC):
404
+ * \code
405
+	// Configuration based on 12MHz external OSC:
406
+	#define CONFIG_PLL1_SOURCE          PLL_SRC_OSC0
407
+	#define CONFIG_PLL1_MUL             8
408
+	#define CONFIG_PLL1_DIV             2
409
+	#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_PLL1
410
+	#define CONFIG_USBCLK_DIV           1 // Fusb = Fsys/(2 ^ USB_div)
411
+	// CPU clock need of clock > 25MHz to run with USBC
412
+	#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_PLL1
413
+\endcode
414
+ *
415
+ * Content of conf_clock.h for SAM3S, SAM3SD, SAM4S devices (UPD: USB Peripheral Device):
416
+ * \code
417
+	// PLL1 (B) Options   (Fpll = (Fclk * PLL_mul) / PLL_div)
418
+	#define CONFIG_PLL1_SOURCE          PLL_SRC_MAINCK_XTAL
419
+	#define CONFIG_PLL1_MUL             16
420
+	#define CONFIG_PLL1_DIV             2
421
+	// USB Clock Source Options   (Fusb = FpllX / USB_div)
422
+	#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_PLL1
423
+	#define CONFIG_USBCLK_DIV           2
424
+\endcode
425
+ *
426
+ * Content of conf_clock.h for SAM3U device (UPDHS: USB Peripheral Device High Speed):
427
+ * \code
428
+	// USB Clock Source fixed at UPLL.
429
+\endcode
430
+ *
431
+ * Content of conf_clock.h for SAM3X, SAM3A devices (UOTGHS: USB OTG High Speed):
432
+ * \code
433
+	// USB Clock Source fixed at UPLL.
434
+	#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_UPLL
435
+	#define CONFIG_USBCLK_DIV           1
436
+\endcode
437
+ *
438
+ * Content of conf_clocks.h for SAMD devices (USB):
439
+ * \code
440
+	// System clock bus configuration
441
+	#  define CONF_CLOCK_FLASH_WAIT_STATES            2
442
+
443
+	// USB Clock Source fixed at DFLL.
444
+	// SYSTEM_CLOCK_SOURCE_DFLL configuration - Digital Frequency Locked Loop
445
+	#  define CONF_CLOCK_DFLL_ENABLE                  true
446
+	#  define CONF_CLOCK_DFLL_LOOP_MODE               SYSTEM_CLOCK_DFLL_LOOP_MODE_USB_RECOVERY
447
+	#  define CONF_CLOCK_DFLL_ON_DEMAND               true
448
+
449
+	// Set this to true to configure the GCLK when running clocks_init. 
450
+	// If set to false, none of the GCLK generators will be configured in clocks_init().
451
+	#  define CONF_CLOCK_CONFIGURE_GCLK               true
452
+
453
+	// Configure GCLK generator 0 (Main Clock)
454
+	#  define CONF_CLOCK_GCLK_0_ENABLE                true
455
+	#  define CONF_CLOCK_GCLK_0_RUN_IN_STANDBY        true
456
+	#  define CONF_CLOCK_GCLK_0_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_DFLL
457
+	#  define CONF_CLOCK_GCLK_0_PRESCALER             1
458
+	#  define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE         false
459
+\endcode
460
+ */
461
+
462
+/**
463
+ * \page udc_use_case_1 Change USB speed
464
+ *
465
+ * In this use case, the USB device is used with different USB speeds.
466
+ *
467
+ * \section udc_use_case_1_setup Setup steps
468
+ *
469
+ * Prior to implement this use case, be sure to have already
470
+ * apply the UDI module "basic use case".
471
+ *
472
+ * \section udc_use_case_1_usage Usage steps
473
+ *
474
+ * \subsection udc_use_case_1_usage_code Example code
475
+ * Content of conf_usb.h:
476
+ * \code
477
+	 #if // Low speed
478
+	 #define USB_DEVICE_LOW_SPEED
479
+	 // #define USB_DEVICE_HS_SUPPORT
480
+
481
+	 #elif // Full speed
482
+	 // #define USB_DEVICE_LOW_SPEED
483
+	 // #define USB_DEVICE_HS_SUPPORT
484
+
485
+	 #elif // High speed
486
+	 // #define USB_DEVICE_LOW_SPEED
487
+	 #define USB_DEVICE_HS_SUPPORT
488
+
489
+	 #endif
490
+\endcode
491
+ *
492
+ * \subsection udc_use_case_1_usage_flow Workflow
493
+ * -# Ensure that conf_usb.h is available and contains the following parameters
494
+ * required for a USB device low speed (1.5Mbit/s):
495
+ *   - \code #define USB_DEVICE_LOW_SPEED
496
+	 //#define  USB_DEVICE_HS_SUPPORT \endcode
497
+ * -# Ensure that conf_usb.h contains the following parameters
498
+ * required for a USB device full speed (12Mbit/s):
499
+ *   - \code //#define USB_DEVICE_LOW_SPEED
500
+	 //#define  USB_DEVICE_HS_SUPPORT \endcode
501
+ * -# Ensure that conf_usb.h contains the following parameters
502
+ * required for a USB device high speed (480Mbit/s):
503
+ *   - \code //#define USB_DEVICE_LOW_SPEED
504
+	 #define  USB_DEVICE_HS_SUPPORT \endcode
505
+ */
506
+
507
+/**
508
+ * \page udc_use_case_2 Use USB strings
509
+ *
510
+ * In this use case, the usual USB strings is added in the USB device.
511
+ *
512
+ * \section udc_use_case_2_setup Setup steps
513
+ * Prior to implement this use case, be sure to have already
514
+ * apply the UDI module "basic use case".
515
+ *
516
+ * \section udc_use_case_2_usage Usage steps
517
+ *
518
+ * \subsection udc_use_case_2_usage_code Example code
519
+ * Content of conf_usb.h:
520
+ * \code
521
+	#define  USB_DEVICE_MANUFACTURE_NAME      "Manufacture name"
522
+	#define  USB_DEVICE_PRODUCT_NAME          "Product name"
523
+	#define  USB_DEVICE_SERIAL_NAME           "12...EF"
524
+\endcode
525
+ *
526
+ * \subsection udc_use_case_2_usage_flow Workflow
527
+ * -# Ensure that conf_usb.h is available and contains the following parameters
528
+ * required to enable different USB strings:
529
+ *   - \code // Static ASCII name for the manufacture
530
+	#define  USB_DEVICE_MANUFACTURE_NAME "Manufacture name" \endcode
531
+ *   - \code // Static ASCII name for the product
532
+	#define  USB_DEVICE_PRODUCT_NAME "Product name" \endcode
533
+ *   - \code // Static ASCII name to enable and set a serial number
534
+	#define  USB_DEVICE_SERIAL_NAME "12...EF" \endcode
535
+ */
536
+
537
+/**
538
+ * \page udc_use_case_3 Use USB remote wakeup feature
539
+ *
540
+ * In this use case, the USB remote wakeup feature is enabled.
541
+ *
542
+ * \section udc_use_case_3_setup Setup steps
543
+ * Prior to implement this use case, be sure to have already
544
+ * apply the UDI module "basic use case".
545
+ *
546
+ * \section udc_use_case_3_usage Usage steps
547
+ *
548
+ * \subsection udc_use_case_3_usage_code Example code
549
+ * Content of conf_usb.h:
550
+ * \code
551
+	#define  USB_DEVICE_ATTR \
552
+	  (USB_CONFIG_ATTR_REMOTE_WAKEUP | USB_CONFIG_ATTR_..._POWERED)
553
+	#define UDC_REMOTEWAKEUP_ENABLE() my_callback_remotewakeup_enable()
554
+	extern void my_callback_remotewakeup_enable(void);
555
+	#define UDC_REMOTEWAKEUP_DISABLE() my_callback_remotewakeup_disable()
556
+	extern void my_callback_remotewakeup_disable(void);
557
+\endcode
558
+ *
559
+ * Add to application C-file:
560
+ * \code
561
+	 void my_callback_remotewakeup_enable(void)
562
+	 {
563
+	    // Enable application wakeup events (e.g. enable GPIO interrupt)
564
+	 }
565
+	 void my_callback_remotewakeup_disable(void)
566
+	 {
567
+	    // Disable application wakeup events (e.g. disable GPIO interrupt)
568
+	 }
569
+
570
+	 void my_interrupt_event(void)
571
+	 {
572
+	    udc_remotewakeup();
573
+	 }
574
+\endcode
575
+ *
576
+ * \subsection udc_use_case_3_usage_flow Workflow
577
+ * -# Ensure that conf_usb.h is available and contains the following parameters
578
+ * required to enable remote wakeup feature:
579
+ *   - \code // Authorizes the remote wakeup feature
580
+	     #define  USB_DEVICE_ATTR (USB_CONFIG_ATTR_REMOTE_WAKEUP | USB_CONFIG_ATTR_..._POWERED) \endcode
581
+ *   - \code // Define callback called when the host enables the remotewakeup feature
582
+	#define UDC_REMOTEWAKEUP_ENABLE() my_callback_remotewakeup_enable()
583
+	extern void my_callback_remotewakeup_enable(void); \endcode
584
+ *   - \code // Define callback called when the host disables the remotewakeup feature
585
+	#define UDC_REMOTEWAKEUP_DISABLE() my_callback_remotewakeup_disable()
586
+	extern void my_callback_remotewakeup_disable(void); \endcode
587
+ * -# Send a remote wakeup (USB upstream):
588
+ *   - \code udc_remotewakeup(); \endcode
589
+ */
590
+
591
+/**
592
+ * \page udc_use_case_5 Bus power application recommendations
593
+ *
594
+ * In this use case, the USB device BUS power feature is enabled.
595
+ * This feature requires a correct power consumption management.
596
+ *
597
+ * \section udc_use_case_5_setup Setup steps
598
+ * Prior to implement this use case, be sure to have already
599
+ * apply the UDI module "basic use case".
600
+ *
601
+ * \section udc_use_case_5_usage Usage steps
602
+ *
603
+ * \subsection udc_use_case_5_usage_code Example code
604
+ * Content of conf_usb.h:
605
+ * \code
606
+	#define  USB_DEVICE_ATTR (USB_CONFIG_ATTR_BUS_POWERED)
607
+	#define  UDC_SUSPEND_EVENT()         user_callback_suspend_action()
608
+	extern void user_callback_suspend_action(void)
609
+	#define  UDC_RESUME_EVENT()          user_callback_resume_action()
610
+	extern void user_callback_resume_action(void)
611
+\endcode
612
+ *
613
+ * Add to application C-file:
614
+ * \code
615
+	void user_callback_suspend_action(void)
616
+	{
617
+	   // Disable hardware component to reduce power consumption
618
+	}
619
+	void user_callback_resume_action(void)
620
+	{
621
+	   // Re-enable hardware component
622
+	}
623
+\endcode
624
+ *
625
+ * \subsection udc_use_case_5_usage_flow Workflow
626
+ * -# Ensure that conf_usb.h is available and contains the following parameters:
627
+ *   - \code // Authorizes the BUS power feature
628
+	#define  USB_DEVICE_ATTR (USB_CONFIG_ATTR_BUS_POWERED) \endcode
629
+ *   - \code // Define callback called when the host suspend the USB line
630
+	#define UDC_SUSPEND_EVENT() user_callback_suspend_action()
631
+	extern void user_callback_suspend_action(void); \endcode
632
+ *   - \code // Define callback called when the host or device resume the USB line
633
+	#define UDC_RESUME_EVENT() user_callback_resume_action()
634
+	extern void user_callback_resume_action(void); \endcode
635
+ * -# Reduce power consumption in suspend mode (max. 2.5mA on Vbus):
636
+ *   - \code void user_callback_suspend_action(void)
637
+	{
638
+	turn_off_components();
639
+	} \endcode
640
+ */
641
+
642
+/**
643
+ * \page udc_use_case_6 USB dynamic serial number
644
+ *
645
+ * In this use case, the USB serial strings is dynamic.
646
+ * For a static serial string refer to \ref udc_use_case_2.
647
+ *
648
+ * \section udc_use_case_6_setup Setup steps
649
+ * Prior to implement this use case, be sure to have already
650
+ * apply the UDI module "basic use case".
651
+ *
652
+ * \section udc_use_case_6_usage Usage steps
653
+ *
654
+ * \subsection udc_use_case_6_usage_code Example code
655
+ * Content of conf_usb.h:
656
+ * \code
657
+	#define  USB_DEVICE_SERIAL_NAME
658
+	#define  USB_DEVICE_GET_SERIAL_NAME_POINTER serial_number
659
+	#define  USB_DEVICE_GET_SERIAL_NAME_LENGTH  12
660
+	extern uint8_t serial_number[];
661
+\endcode
662
+ *
663
+ * Add to application C-file:
664
+ * \code
665
+	 uint8_t serial_number[USB_DEVICE_GET_SERIAL_NAME_LENGTH];
666
+
667
+	 void init_build_usb_serial_number(void)
668
+	 {
669
+	 serial_number[0] = 'A';
670
+	 serial_number[1] = 'B';
671
+	 ...
672
+	 serial_number[USB_DEVICE_GET_SERIAL_NAME_LENGTH-1] = 'C';
673
+	 } \endcode
674
+ *
675
+ * \subsection udc_use_case_6_usage_flow Workflow
676
+ * -# Ensure that conf_usb.h is available and contains the following parameters
677
+ * required to enable a USB serial number strings dynamically:
678
+ *   - \code #define  USB_DEVICE_SERIAL_NAME // Define this empty
679
+	#define  USB_DEVICE_GET_SERIAL_NAME_POINTER serial_number // Give serial array pointer
680
+	#define  USB_DEVICE_GET_SERIAL_NAME_LENGTH  12 // Give size of serial array
681
+	extern uint8_t serial_number[]; // Declare external serial array \endcode
682
+ * -# Before start USB stack, initialize the serial array
683
+ *   - \code
684
+	 uint8_t serial_number[USB_DEVICE_GET_SERIAL_NAME_LENGTH];
685
+
686
+	 void init_build_usb_serial_number(void)
687
+	 {
688
+	 serial_number[0] = 'A';
689
+	 serial_number[1] = 'B';
690
+	 ...
691
+	 serial_number[USB_DEVICE_GET_SERIAL_NAME_LENGTH-1] = 'C';
692
+	 } \endcode
693
+ */
694
+
695
+
696
+
697
+#endif // _UDC_H_

+ 135
- 0
Marlin/src/HAL/HAL_DUE/usb/udc_desc.h View File

@@ -0,0 +1,135 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Common API for USB Device Interface
5
+ *
6
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _UDC_DESC_H_
48
+#define _UDC_DESC_H_
49
+
50
+#include "conf_usb.h"
51
+#include "usb_protocol.h"
52
+#include "udi.h"
53
+
54
+#ifdef __cplusplus
55
+extern "C" {
56
+#endif
57
+
58
+/**
59
+ * \ingroup udc_group
60
+ * \defgroup udc_desc_group USB Device Descriptor
61
+ *
62
+ * @{
63
+ */
64
+
65
+/**
66
+ * \brief Defines the memory's location of USB descriptors
67
+ *
68
+ * By default the Descriptor is stored in RAM
69
+ * (UDC_DESC_STORAGE is defined empty).
70
+ *
71
+ * If you have need to free RAM space,
72
+ * it is possible to put descriptor in flash in following case:
73
+ * - USB driver authorize flash transfer (USBB on UC3 and USB on Mega)
74
+ * - USB Device is not high speed (UDC no need to change USB descriptors)
75
+ *
76
+ * For UC3 application used "const".
77
+ *
78
+ * For Mega application used "code".
79
+ */
80
+#define  UDC_DESC_STORAGE
81
+	// Descriptor storage in internal RAM
82
+#if (defined UDC_DATA_USE_HRAM_SUPPORT)
83
+#	if defined(__GNUC__)
84
+#		define UDC_DATA(x)              COMPILER_WORD_ALIGNED __attribute__((__section__(".data_hram0")))
85
+#		define UDC_BSS(x)               COMPILER_ALIGNED(x)   __attribute__((__section__(".bss_hram0")))
86
+#	elif defined(__ICCAVR32__)
87
+#		define UDC_DATA(x)              COMPILER_ALIGNED(x)   __data32
88
+#		define UDC_BSS(x)               COMPILER_ALIGNED(x)   __data32
89
+#	endif
90
+#else
91
+#	define UDC_DATA(x)              COMPILER_ALIGNED(x)
92
+#	define UDC_BSS(x)               COMPILER_ALIGNED(x)
93
+#endif
94
+
95
+
96
+
97
+/**
98
+ * \brief Configuration descriptor and UDI link for one USB speed
99
+ */
100
+typedef struct {
101
+	//! USB configuration descriptor
102
+	usb_conf_desc_t UDC_DESC_STORAGE *desc;
103
+	//! Array of UDI API pointer
104
+	udi_api_t UDC_DESC_STORAGE *UDC_DESC_STORAGE * udi_apis;
105
+} udc_config_speed_t;
106
+
107
+
108
+/**
109
+ * \brief All information about the USB Device
110
+ */
111
+typedef struct {
112
+	//! USB device descriptor for low or full speed
113
+	usb_dev_desc_t UDC_DESC_STORAGE *confdev_lsfs;
114
+	//! USB configuration descriptor and UDI API pointers for low or full speed
115
+	udc_config_speed_t UDC_DESC_STORAGE *conf_lsfs;
116
+#ifdef USB_DEVICE_HS_SUPPORT
117
+	//! USB device descriptor for high speed
118
+	usb_dev_desc_t UDC_DESC_STORAGE *confdev_hs;
119
+	//! USB device qualifier, only use in high speed mode
120
+	usb_dev_qual_desc_t UDC_DESC_STORAGE *qualifier;
121
+	//! USB configuration descriptor and UDI API pointers for high speed
122
+	udc_config_speed_t UDC_DESC_STORAGE *conf_hs;
123
+#endif
124
+	usb_dev_bos_desc_t UDC_DESC_STORAGE *conf_bos;
125
+} udc_config_t;
126
+
127
+//! Global variables of USB Device Descriptor and UDI links
128
+extern UDC_DESC_STORAGE udc_config_t udc_config;
129
+
130
+//@}
131
+
132
+#ifdef __cplusplus
133
+}
134
+#endif
135
+#endif // _UDC_DESC_H_

+ 396
- 0
Marlin/src/HAL/HAL_DUE/usb/udd.h View File

@@ -0,0 +1,396 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Common API for USB Device Drivers (UDD)
5
+ *
6
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _UDD_H_
48
+#define _UDD_H_
49
+
50
+#include "usb_protocol.h"
51
+#include "udc_desc.h"
52
+
53
+#ifdef __cplusplus
54
+extern "C" {
55
+#endif
56
+
57
+/**
58
+ * \ingroup usb_device_group
59
+ * \defgroup udd_group USB Device Driver (UDD)
60
+ *
61
+ * The UDD driver provides a low-level abstraction of the device
62
+ * controller hardware. Most events coming from the hardware such as
63
+ * interrupts, which may cause the UDD to call into the UDC and UDI.
64
+ *
65
+ * @{
66
+ */
67
+
68
+//! \brief Endpoint identifier
69
+typedef uint8_t udd_ep_id_t;
70
+
71
+//! \brief Endpoint transfer status
72
+//! Returned in parameters of callback register via udd_ep_run routine.
73
+typedef enum {
74
+	UDD_EP_TRANSFER_OK = 0,
75
+	UDD_EP_TRANSFER_ABORT = 1,
76
+} udd_ep_status_t;
77
+
78
+/**
79
+ * \brief Global variable to give and record information of the setup request management
80
+ *
81
+ * This global variable allows to decode and response a setup request.
82
+ * It can be updated by udc_process_setup() from UDC or *setup() from UDIs.
83
+ */
84
+typedef struct {
85
+	//! Data received in USB SETUP packet
86
+	//! Note: The swap of "req.wValues" from uin16_t to le16_t is done by UDD.
87
+	usb_setup_req_t req;
88
+
89
+	//! Point to buffer to send or fill with data following SETUP packet
90
+	//! This buffer must be word align for DATA IN phase (use prefix COMPILER_WORD_ALIGNED for buffer)
91
+	uint8_t *payload;
92
+
93
+	//! Size of buffer to send or fill, and content the number of byte transfered
94
+	uint16_t payload_size;
95
+
96
+	//! Callback called after reception of ZLP from setup request
97
+	void (*callback) (void);
98
+
99
+	//! Callback called when the buffer given (.payload) is full or empty.
100
+	//! This one return false to abort data transfer, or true with a new buffer in .payload.
101
+	bool(*over_under_run) (void);
102
+} udd_ctrl_request_t;
103
+extern udd_ctrl_request_t udd_g_ctrlreq;
104
+
105
+//! Return true if the setup request \a udd_g_ctrlreq indicates IN data transfer
106
+#define  Udd_setup_is_in()       \
107
+      (USB_REQ_DIR_IN == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK))
108
+
109
+//! Return true if the setup request \a udd_g_ctrlreq indicates OUT data transfer
110
+#define  Udd_setup_is_out()      \
111
+      (USB_REQ_DIR_OUT == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK))
112
+
113
+//! Return the type of the SETUP request \a udd_g_ctrlreq. \see usb_reqtype.
114
+#define  Udd_setup_type()        \
115
+      (udd_g_ctrlreq.req.bmRequestType & USB_REQ_TYPE_MASK)
116
+
117
+//! Return the recipient of the SETUP request \a udd_g_ctrlreq. \see usb_recipient
118
+#define  Udd_setup_recipient()   \
119
+      (udd_g_ctrlreq.req.bmRequestType & USB_REQ_RECIP_MASK)
120
+
121
+/**
122
+ * \brief End of halt callback function type.
123
+ * Registered by routine udd_ep_wait_stall_clear()
124
+ * Callback called when endpoint stall is cleared.
125
+ */
126
+typedef void (*udd_callback_halt_cleared_t) (void);
127
+
128
+/**
129
+ * \brief End of transfer callback function type.
130
+ * Registered by routine udd_ep_run()
131
+ * Callback called by USB interrupt after data transfer or abort (reset,...).
132
+ *
133
+ * \param status     UDD_EP_TRANSFER_OK, if transfer is complete
134
+ * \param status     UDD_EP_TRANSFER_ABORT, if transfer is aborted
135
+ * \param n          number of data transfered
136
+ */
137
+typedef void (*udd_callback_trans_t) (udd_ep_status_t status,
138
+		iram_size_t nb_transfered, udd_ep_id_t ep);
139
+
140
+/**
141
+ * \brief Authorizes the VBUS event
142
+ *
143
+ * \return true, if the VBUS monitoring is possible.
144
+ */
145
+bool udd_include_vbus_monitoring(void);
146
+
147
+/**
148
+ * \brief Enables the USB Device mode
149
+ */
150
+void udd_enable(void);
151
+
152
+/**
153
+ * \brief Disables the USB Device mode
154
+ */
155
+void udd_disable(void);
156
+
157
+/**
158
+ * \brief Attach device to the bus when possible
159
+ *
160
+ * \warning If a VBus control is included in driver,
161
+ * then it will attach device when an acceptable Vbus
162
+ * level from the host is detected.
163
+ */
164
+void udd_attach(void);
165
+
166
+/**
167
+ * \brief Detaches the device from the bus
168
+ *
169
+ * The driver must remove pull-up on USB line D- or D+.
170
+ */
171
+void udd_detach(void);
172
+
173
+/**
174
+ * \brief Test whether the USB Device Controller is running at high
175
+ * speed or not.
176
+ *
177
+ * \return \c true if the Device is running at high speed mode, otherwise \c false.
178
+ */
179
+bool udd_is_high_speed(void);
180
+
181
+/**
182
+ * \brief Changes the USB address of device
183
+ *
184
+ * \param address    New USB address
185
+ */
186
+void udd_set_address(uint8_t address);
187
+
188
+/**
189
+ * \brief Returns the USB address of device
190
+ *
191
+ * \return USB address
192
+ */
193
+uint8_t udd_getaddress(void);
194
+
195
+/**
196
+ * \brief Returns the current start of frame number
197
+ *
198
+ * \return current start of frame number.
199
+ */
200
+uint16_t udd_get_frame_number(void);
201
+
202
+/**
203
+ * \brief Returns the current micro start of frame number
204
+ *
205
+ * \return current micro start of frame number required in high speed mode.
206
+ */
207
+uint16_t udd_get_micro_frame_number(void);
208
+
209
+/*! \brief The USB driver sends a resume signal called Upstream Resume
210
+ */
211
+void udd_send_remotewakeup(void);
212
+
213
+/**
214
+ * \brief Load setup payload
215
+ *
216
+ * \param payload       Pointer on payload
217
+ * \param payload_size  Size of payload
218
+ */
219
+void udd_set_setup_payload( uint8_t *payload, uint16_t payload_size );
220
+
221
+
222
+/**
223
+ * \name Endpoint Management
224
+ *
225
+ * The following functions allow drivers to create and remove
226
+ * endpoints, as well as set, clear and query their "halted" and
227
+ * "wedged" states.
228
+ */
229
+//@{
230
+
231
+#if (USB_DEVICE_MAX_EP != 0)
232
+
233
+/**
234
+ * \brief Configures and enables an endpoint
235
+ *
236
+ * \param ep               Endpoint number including direction (USB_EP_DIR_IN/USB_EP_DIR_OUT).
237
+ * \param bmAttributes     Attributes of endpoint declared in the descriptor.
238
+ * \param MaxEndpointSize  Endpoint maximum size
239
+ *
240
+ * \return \c 1 if the endpoint is enabled, otherwise \c 0.
241
+ */
242
+bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes,
243
+		uint16_t MaxEndpointSize);
244
+
245
+/**
246
+ * \brief Disables an endpoint
247
+ *
248
+ * \param ep               Endpoint number including direction (USB_EP_DIR_IN/USB_EP_DIR_OUT).
249
+ */
250
+void udd_ep_free(udd_ep_id_t ep);
251
+
252
+/**
253
+ * \brief Check if the endpoint \a ep is halted.
254
+ *
255
+ * \param ep The ID of the endpoint to check.
256
+ *
257
+ * \return \c 1 if \a ep is halted, otherwise \c 0.
258
+ */
259
+bool udd_ep_is_halted(udd_ep_id_t ep);
260
+
261
+/**
262
+ * \brief Set the halted state of the endpoint \a ep
263
+ *
264
+ * After calling this function, any transaction on \a ep will result
265
+ * in a STALL handshake being sent. Any pending transactions will be
266
+ * performed first, however.
267
+ *
268
+ * \param ep The ID of the endpoint to be halted
269
+ *
270
+ * \return \c 1 if \a ep is halted, otherwise \c 0.
271
+ */
272
+bool udd_ep_set_halt(udd_ep_id_t ep);
273
+
274
+/**
275
+ * \brief Clear the halted state of the endpoint \a ep
276
+ *
277
+ * After calling this function, any transaction on \a ep will
278
+ * be handled normally, i.e. a STALL handshake will not be sent, and
279
+ * the data toggle sequence will start at DATA0.
280
+ *
281
+ * \param ep The ID of the endpoint to be un-halted
282
+ *
283
+ * \return \c 1 if function was successfully done, otherwise \c 0.
284
+ */
285
+bool udd_ep_clear_halt(udd_ep_id_t ep);
286
+
287
+/**
288
+ * \brief Registers a callback to call when endpoint halt is cleared
289
+ *
290
+ * \param ep            The ID of the endpoint to use
291
+ * \param callback      NULL or function to call when endpoint halt is cleared
292
+ *
293
+ * \warning if the endpoint is not halted then the \a callback is called immediately.
294
+ *
295
+ * \return \c 1 if the register is accepted, otherwise \c 0.
296
+ */
297
+bool udd_ep_wait_stall_clear(udd_ep_id_t ep,
298
+		udd_callback_halt_cleared_t callback);
299
+
300
+/**
301
+ * \brief Allows to receive or send data on an endpoint
302
+ *
303
+ * The driver uses a specific DMA USB to transfer data
304
+ * from internal RAM to endpoint, if this one is available.
305
+ * When the transfer is finished or aborted (stall, reset, ...), the \a callback is called.
306
+ * The \a callback returns the transfer status and eventually the number of byte transfered.
307
+ * Note: The control endpoint is not authorized.
308
+ *
309
+ * \param ep            The ID of the endpoint to use
310
+ * \param b_shortpacket Enabled automatic short packet
311
+ * \param buf           Buffer on Internal RAM to send or fill.
312
+ *                      It must be align, then use COMPILER_WORD_ALIGNED.
313
+ * \param buf_size      Buffer size to send or fill
314
+ * \param callback      NULL or function to call at the end of transfer
315
+ *
316
+ * \warning About \a b_shortpacket, for IN endpoint it means that a short packet
317
+ * (or a Zero Length Packet) will be sent to the USB line to properly close the usb
318
+ * transfer at the end of the data transfer.
319
+ * For Bulk and Interrupt OUT endpoint, it will automatically stop the transfer
320
+ * at the end of the data transfer (received short packet).
321
+ *
322
+ * \return \c 1 if function was successfully done, otherwise \c 0.
323
+ */
324
+bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket,
325
+		uint8_t * buf, iram_size_t buf_size,
326
+		udd_callback_trans_t callback);
327
+/**
328
+ * \brief Aborts transfer on going on endpoint
329
+ *
330
+ * If a transfer is on going, then it is stopped and
331
+ * the callback registered is called to signal the end of transfer.
332
+ * Note: The control endpoint is not authorized.
333
+ *
334
+ * \param ep            Endpoint to abort
335
+ */
336
+void udd_ep_abort(udd_ep_id_t ep);
337
+
338
+#endif
339
+
340
+//@}
341
+
342
+
343
+/**
344
+ * \name High speed test mode management
345
+ *
346
+ * The following functions allow the device to jump to a specific test mode required in high speed mode.
347
+ */
348
+//@{
349
+void udd_test_mode_j(void);
350
+void udd_test_mode_k(void);
351
+void udd_test_mode_se0_nak(void);
352
+void udd_test_mode_packet(void);
353
+//@}
354
+
355
+
356
+/**
357
+ * \name UDC callbacks to provide for UDD
358
+ *
359
+ * The following callbacks are used by UDD.
360
+ */
361
+//@{
362
+
363
+/**
364
+ * \brief Decodes and manages a setup request
365
+ *
366
+ * The driver call it when a SETUP packet is received.
367
+ * The \c udd_g_ctrlreq contains the data of SETUP packet.
368
+ * If this callback accepts the setup request then it must
369
+ * return \c 1 and eventually update \c udd_g_ctrlreq to send or receive data.
370
+ *
371
+ * \return \c 1 if the request is accepted, otherwise \c 0.
372
+ */
373
+extern bool udc_process_setup(void);
374
+
375
+/**
376
+ * \brief Reset the UDC
377
+ *
378
+ * The UDC must reset all configuration.
379
+ */
380
+extern void udc_reset(void);
381
+
382
+/**
383
+ * \brief To signal that a SOF is occurred
384
+ *
385
+ * The UDC must send the signal to all UDIs enabled
386
+ */
387
+extern void udc_sof_notify(void);
388
+
389
+//@}
390
+
391
+//@}
392
+
393
+#ifdef __cplusplus
394
+}
395
+#endif
396
+#endif // _UDD_H_

+ 133
- 0
Marlin/src/HAL/HAL_DUE/usb/udi.h View File

@@ -0,0 +1,133 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Common API for USB Device Interface
5
+ *
6
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _UDI_H_
48
+#define _UDI_H_
49
+
50
+#include "conf_usb.h"
51
+#include "usb_protocol.h"
52
+
53
+#ifdef __cplusplus
54
+extern "C" {
55
+#endif
56
+
57
+/**
58
+ * \ingroup usb_device_group
59
+ * \defgroup udi_group USB Device Interface (UDI)
60
+ * The UDI provides a common API for all classes,
61
+ * and this is used by UDC for the main control of USB Device interface.
62
+ * @{
63
+ */
64
+
65
+/**
66
+ * \brief UDI API.
67
+ *
68
+ * The callbacks within this structure are called only by
69
+ * USB Device Controller (UDC)
70
+ *
71
+ * The udc_get_interface_desc() can be use by UDI to know the interface descriptor
72
+ * selected by UDC.
73
+ */
74
+typedef struct {
75
+	/**
76
+	 * \brief Enable the interface.
77
+	 *
78
+	 * This function is called when the host selects a configuration
79
+	 * to which this interface belongs through a Set Configuration
80
+	 * request, and when the host selects an alternate setting of
81
+	 * this interface through a Set Interface request.
82
+	 *
83
+	 * \return \c 1 if function was successfully done, otherwise \c 0.
84
+	 */
85
+	bool(*enable) (void);
86
+
87
+	/**
88
+	 * \brief Disable the interface.
89
+	 *
90
+	 * This function is called when this interface is currently
91
+	 * active, and
92
+	 * - the host selects any configuration through a Set
93
+	 *   Configuration request, or
94
+	 * - the host issues a USB reset, or
95
+	 * - the device is detached from the host (i.e. Vbus is no
96
+	 *   longer present)
97
+	 */
98
+	void (*disable) (void);
99
+
100
+	/**
101
+	 * \brief Handle a control request directed at an interface.
102
+	 *
103
+	 * This function is called when this interface is currently
104
+	 * active and the host sends a SETUP request
105
+	 * with this interface as the recipient.
106
+	 *
107
+	 * Use udd_g_ctrlreq to decode and response to SETUP request.
108
+	 *
109
+	 * \return \c 1 if this interface supports the SETUP request, otherwise \c 0.
110
+	 */
111
+	bool(*setup) (void);
112
+
113
+	/**
114
+	 * \brief Returns the current setting of the selected interface.
115
+	 *
116
+	 * This function is called when UDC when know alternate setting of selected interface.
117
+	 *
118
+	 * \return alternate setting of selected interface
119
+	 */
120
+	uint8_t(*getsetting) (void);
121
+
122
+	/**
123
+	 * \brief To signal that a SOF is occurred
124
+	 */
125
+	void(*sof_notify) (void);
126
+} udi_api_t;
127
+
128
+//@}
129
+
130
+#ifdef __cplusplus
131
+}
132
+#endif
133
+#endif // _UDI_H_

+ 1155
- 0
Marlin/src/HAL/HAL_DUE/usb/udi_cdc.c
File diff suppressed because it is too large
View File


+ 810
- 0
Marlin/src/HAL/HAL_DUE/usb/udi_cdc.h View File

@@ -0,0 +1,810 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB Device Communication Device Class (CDC) interface definitions.
5
+ *
6
+ * Copyright (c) 2009-2016 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _UDI_CDC_H_
48
+#define _UDI_CDC_H_
49
+
50
+#include "conf_usb.h"
51
+#include "usb_protocol.h"
52
+#include "usb_protocol_cdc.h"
53
+#include "udd.h"
54
+#include "udc_desc.h"
55
+#include "udi.h"
56
+
57
+// Check the number of port
58
+#ifndef  UDI_CDC_PORT_NB
59
+# define  UDI_CDC_PORT_NB 1
60
+#endif
61
+#if (UDI_CDC_PORT_NB < 1) || (UDI_CDC_PORT_NB > 7)
62
+# error UDI_CDC_PORT_NB must be between 1 and 7
63
+#endif
64
+
65
+#ifdef __cplusplus
66
+extern "C" {
67
+#endif
68
+
69
+/**
70
+ * \addtogroup udi_cdc_group_udc
71
+ * @{
72
+ */
73
+
74
+//! Global structure which contains standard UDI API for UDC
75
+extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_comm;
76
+extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_data;
77
+//@}
78
+
79
+/**
80
+ * \ingroup udi_cdc_group
81
+ * \defgroup udi_cdc_group_desc USB interface descriptors
82
+ *
83
+ * The following structures provide predefined USB interface descriptors.
84
+ * It must be used to define the final USB descriptors.
85
+ */
86
+//@{
87
+
88
+/**
89
+ * \brief Communication Class interface descriptor
90
+ *
91
+ * Interface descriptor with associated functional and endpoint
92
+ * descriptors for the CDC Communication Class interface.
93
+ */
94
+typedef struct {
95
+	//! Standard interface descriptor
96
+	usb_iface_desc_t iface;
97
+	//! CDC Header functional descriptor
98
+	usb_cdc_hdr_desc_t header;
99
+	//! CDC Abstract Control Model functional descriptor
100
+	usb_cdc_acm_desc_t acm;
101
+	//! CDC Union functional descriptor
102
+	usb_cdc_union_desc_t union_desc;
103
+	//! CDC Call Management functional descriptor
104
+	usb_cdc_call_mgmt_desc_t call_mgmt;
105
+	//! Notification endpoint descriptor
106
+	usb_ep_desc_t ep_notify;
107
+} udi_cdc_comm_desc_t;
108
+
109
+
110
+/**
111
+ * \brief Data Class interface descriptor
112
+ *
113
+ * Interface descriptor with associated endpoint descriptors for the
114
+ * CDC Data Class interface.
115
+ */
116
+typedef struct {
117
+	//! Standard interface descriptor
118
+	usb_iface_desc_t iface;
119
+	//! Data IN/OUT endpoint descriptors
120
+	usb_ep_desc_t ep_in;
121
+	usb_ep_desc_t ep_out;
122
+} udi_cdc_data_desc_t;
123
+
124
+
125
+//! CDC communication endpoints size for all speeds
126
+#define UDI_CDC_COMM_EP_SIZE        64
127
+//! CDC data endpoints size for FS speed (8B, 16B, 32B, 64B)
128
+#define UDI_CDC_DATA_EPS_FS_SIZE    64
129
+//! CDC data endpoints size for HS speed (512B only)
130
+#define UDI_CDC_DATA_EPS_HS_SIZE    512
131
+
132
+/**
133
+ * \name Content of interface descriptors
134
+ * Up to 7 CDC interfaces can be implemented on a USB device.
135
+ */
136
+//@{
137
+//! By default no string associated to these interfaces
138
+#ifndef UDI_CDC_IAD_STRING_ID_0
139
+#define UDI_CDC_IAD_STRING_ID_0   0
140
+#endif
141
+#ifndef UDI_CDC_COMM_STRING_ID_0
142
+#define UDI_CDC_COMM_STRING_ID_0   0
143
+#endif
144
+#ifndef UDI_CDC_DATA_STRING_ID_0
145
+#define UDI_CDC_DATA_STRING_ID_0   0
146
+#endif
147
+#define UDI_CDC_IAD_DESC_0      UDI_CDC_IAD_DESC(0)
148
+#define UDI_CDC_COMM_DESC_0     UDI_CDC_COMM_DESC(0)
149
+#define UDI_CDC_DATA_DESC_0_FS  UDI_CDC_DATA_DESC_FS(0)
150
+#define UDI_CDC_DATA_DESC_0_HS  UDI_CDC_DATA_DESC_HS(0)
151
+
152
+//! By default no string associated to these interfaces
153
+#ifndef UDI_CDC_IAD_STRING_ID_1
154
+#define UDI_CDC_IAD_STRING_ID_1  0
155
+#endif
156
+#ifndef UDI_CDC_COMM_STRING_ID_1
157
+#define UDI_CDC_COMM_STRING_ID_1 0
158
+#endif
159
+#ifndef UDI_CDC_DATA_STRING_ID_1
160
+#define UDI_CDC_DATA_STRING_ID_1 0
161
+#endif
162
+#define UDI_CDC_IAD_DESC_1      UDI_CDC_IAD_DESC(1)
163
+#define UDI_CDC_COMM_DESC_1     UDI_CDC_COMM_DESC(1)
164
+#define UDI_CDC_DATA_DESC_1_FS  UDI_CDC_DATA_DESC_FS(1)
165
+#define UDI_CDC_DATA_DESC_1_HS  UDI_CDC_DATA_DESC_HS(1)
166
+
167
+//! By default no string associated to these interfaces
168
+#ifndef UDI_CDC_IAD_STRING_ID_2
169
+#define UDI_CDC_IAD_STRING_ID_2   0
170
+#endif
171
+#ifndef UDI_CDC_COMM_STRING_ID_2
172
+#define UDI_CDC_COMM_STRING_ID_2   0
173
+#endif
174
+#ifndef UDI_CDC_DATA_STRING_ID_2
175
+#define UDI_CDC_DATA_STRING_ID_2   0
176
+#endif
177
+#define UDI_CDC_IAD_DESC_2      UDI_CDC_IAD_DESC(2)
178
+#define UDI_CDC_COMM_DESC_2     UDI_CDC_COMM_DESC(2)
179
+#define UDI_CDC_DATA_DESC_2_FS  UDI_CDC_DATA_DESC_FS(2)
180
+#define UDI_CDC_DATA_DESC_2_HS  UDI_CDC_DATA_DESC_HS(2)
181
+
182
+//! By default no string associated to these interfaces
183
+#ifndef UDI_CDC_IAD_STRING_ID_3
184
+#define UDI_CDC_IAD_STRING_ID_3   0
185
+#endif
186
+#ifndef UDI_CDC_COMM_STRING_ID_3
187
+#define UDI_CDC_COMM_STRING_ID_3   0
188
+#endif
189
+#ifndef UDI_CDC_DATA_STRING_ID_3
190
+#define UDI_CDC_DATA_STRING_ID_3   0
191
+#endif
192
+#define UDI_CDC_IAD_DESC_3      UDI_CDC_IAD_DESC(3)
193
+#define UDI_CDC_COMM_DESC_3     UDI_CDC_COMM_DESC(3)
194
+#define UDI_CDC_DATA_DESC_3_FS  UDI_CDC_DATA_DESC_FS(3)
195
+#define UDI_CDC_DATA_DESC_3_HS  UDI_CDC_DATA_DESC_HS(3)
196
+
197
+//! By default no string associated to these interfaces
198
+#ifndef UDI_CDC_IAD_STRING_ID_4
199
+#define UDI_CDC_IAD_STRING_ID_4   0
200
+#endif
201
+#ifndef UDI_CDC_COMM_STRING_ID_4
202
+#define UDI_CDC_COMM_STRING_ID_4   0
203
+#endif
204
+#ifndef UDI_CDC_DATA_STRING_ID_4
205
+#define UDI_CDC_DATA_STRING_ID_4   0
206
+#endif
207
+#define UDI_CDC_IAD_DESC_4      UDI_CDC_IAD_DESC(4)
208
+#define UDI_CDC_COMM_DESC_4     UDI_CDC_COMM_DESC(4)
209
+#define UDI_CDC_DATA_DESC_4_FS  UDI_CDC_DATA_DESC_FS(4)
210
+#define UDI_CDC_DATA_DESC_4_HS  UDI_CDC_DATA_DESC_HS(4)
211
+
212
+//! By default no string associated to these interfaces
213
+#ifndef UDI_CDC_IAD_STRING_ID_5
214
+#define UDI_CDC_IAD_STRING_ID_5   0
215
+#endif
216
+#ifndef UDI_CDC_COMM_STRING_ID_5
217
+#define UDI_CDC_COMM_STRING_ID_5   0
218
+#endif
219
+#ifndef UDI_CDC_DATA_STRING_ID_5
220
+#define UDI_CDC_DATA_STRING_ID_5   0
221
+#endif
222
+#define UDI_CDC_IAD_DESC_5      UDI_CDC_IAD_DESC(5)
223
+#define UDI_CDC_COMM_DESC_5     UDI_CDC_COMM_DESC(5)
224
+#define UDI_CDC_DATA_DESC_5_FS  UDI_CDC_DATA_DESC_FS(5)
225
+#define UDI_CDC_DATA_DESC_5_HS  UDI_CDC_DATA_DESC_HS(5)
226
+
227
+//! By default no string associated to these interfaces
228
+#ifndef UDI_CDC_IAD_STRING_ID_6
229
+#define UDI_CDC_IAD_STRING_ID_6   0
230
+#endif
231
+#ifndef UDI_CDC_COMM_STRING_ID_6
232
+#define UDI_CDC_COMM_STRING_ID_6   0
233
+#endif
234
+#ifndef UDI_CDC_DATA_STRING_ID_6
235
+#define UDI_CDC_DATA_STRING_ID_6   0
236
+#endif
237
+#define UDI_CDC_IAD_DESC_6      UDI_CDC_IAD_DESC(6)
238
+#define UDI_CDC_COMM_DESC_6     UDI_CDC_COMM_DESC(6)
239
+#define UDI_CDC_DATA_DESC_6_FS  UDI_CDC_DATA_DESC_FS(6)
240
+#define UDI_CDC_DATA_DESC_6_HS  UDI_CDC_DATA_DESC_HS(6)
241
+//@}
242
+
243
+
244
+//! Content of CDC IAD interface descriptor for all speeds
245
+#define UDI_CDC_IAD_DESC(port) { \
246
+   .bLength                      = sizeof(usb_iad_desc_t),\
247
+   .bDescriptorType              = USB_DT_IAD,\
248
+   .bInterfaceCount              = 2,\
249
+   .bFunctionClass               = CDC_CLASS_COMM,\
250
+   .bFunctionSubClass            = CDC_SUBCLASS_ACM,\
251
+   .bFunctionProtocol            = CDC_PROTOCOL_V25TER,\
252
+   .bFirstInterface              = UDI_CDC_COMM_IFACE_NUMBER_##port,\
253
+   .iFunction                    = UDI_CDC_IAD_STRING_ID_##port,\
254
+   }
255
+
256
+//! Content of CDC COMM interface descriptor for all speeds
257
+#define UDI_CDC_COMM_DESC(port) { \
258
+   .iface.bLength                = sizeof(usb_iface_desc_t),\
259
+   .iface.bDescriptorType        = USB_DT_INTERFACE,\
260
+   .iface.bAlternateSetting      = 0,\
261
+   .iface.bNumEndpoints          = 1,\
262
+   .iface.bInterfaceClass        = CDC_CLASS_COMM,\
263
+   .iface.bInterfaceSubClass     = CDC_SUBCLASS_ACM,\
264
+   .iface.bInterfaceProtocol     = CDC_PROTOCOL_V25TER,\
265
+   .header.bFunctionLength       = sizeof(usb_cdc_hdr_desc_t),\
266
+   .header.bDescriptorType       = CDC_CS_INTERFACE,\
267
+   .header.bDescriptorSubtype    = CDC_SCS_HEADER,\
268
+   .header.bcdCDC                = LE16(0x0110),\
269
+   .call_mgmt.bFunctionLength    = sizeof(usb_cdc_call_mgmt_desc_t),\
270
+   .call_mgmt.bDescriptorType    = CDC_CS_INTERFACE,\
271
+   .call_mgmt.bDescriptorSubtype = CDC_SCS_CALL_MGMT,\
272
+   .call_mgmt.bmCapabilities     = \
273
+			CDC_CALL_MGMT_SUPPORTED | CDC_CALL_MGMT_OVER_DCI,\
274
+   .acm.bFunctionLength          = sizeof(usb_cdc_acm_desc_t),\
275
+   .acm.bDescriptorType          = CDC_CS_INTERFACE,\
276
+   .acm.bDescriptorSubtype       = CDC_SCS_ACM,\
277
+   .acm.bmCapabilities           = CDC_ACM_SUPPORT_LINE_REQUESTS,\
278
+   .union_desc.bFunctionLength   = sizeof(usb_cdc_union_desc_t),\
279
+   .union_desc.bDescriptorType   = CDC_CS_INTERFACE,\
280
+   .union_desc.bDescriptorSubtype= CDC_SCS_UNION,\
281
+   .ep_notify.bLength            = sizeof(usb_ep_desc_t),\
282
+   .ep_notify.bDescriptorType    = USB_DT_ENDPOINT,\
283
+   .ep_notify.bmAttributes       = USB_EP_TYPE_INTERRUPT,\
284
+   .ep_notify.wMaxPacketSize     = LE16(UDI_CDC_COMM_EP_SIZE),\
285
+   .ep_notify.bInterval          = 0x10,\
286
+   .ep_notify.bEndpointAddress   = UDI_CDC_COMM_EP_##port,\
287
+   .iface.bInterfaceNumber       = UDI_CDC_COMM_IFACE_NUMBER_##port,\
288
+   .call_mgmt.bDataInterface     = UDI_CDC_DATA_IFACE_NUMBER_##port,\
289
+   .union_desc.bMasterInterface  = UDI_CDC_COMM_IFACE_NUMBER_##port,\
290
+   .union_desc.bSlaveInterface0  = UDI_CDC_DATA_IFACE_NUMBER_##port,\
291
+   .iface.iInterface             = UDI_CDC_COMM_STRING_ID_##port,\
292
+   }
293
+
294
+//! Content of CDC DATA interface descriptors
295
+#define UDI_CDC_DATA_DESC_COMMON \
296
+   .iface.bLength                = sizeof(usb_iface_desc_t),\
297
+   .iface.bDescriptorType        = USB_DT_INTERFACE,\
298
+   .iface.bAlternateSetting      = 0,\
299
+   .iface.bNumEndpoints          = 2,\
300
+   .iface.bInterfaceClass        = CDC_CLASS_DATA,\
301
+   .iface.bInterfaceSubClass     = 0,\
302
+   .iface.bInterfaceProtocol     = 0,\
303
+   .ep_in.bLength                = sizeof(usb_ep_desc_t),\
304
+   .ep_in.bDescriptorType        = USB_DT_ENDPOINT,\
305
+   .ep_in.bmAttributes           = USB_EP_TYPE_BULK,\
306
+   .ep_in.bInterval              = 0,\
307
+   .ep_out.bLength               = sizeof(usb_ep_desc_t),\
308
+   .ep_out.bDescriptorType       = USB_DT_ENDPOINT,\
309
+   .ep_out.bmAttributes          = USB_EP_TYPE_BULK,\
310
+   .ep_out.bInterval             = 0,
311
+
312
+#define UDI_CDC_DATA_DESC_FS(port) { \
313
+   UDI_CDC_DATA_DESC_COMMON \
314
+   .ep_in.wMaxPacketSize         = LE16(UDI_CDC_DATA_EPS_FS_SIZE),\
315
+   .ep_out.wMaxPacketSize        = LE16(UDI_CDC_DATA_EPS_FS_SIZE),\
316
+   .ep_in.bEndpointAddress       = UDI_CDC_DATA_EP_IN_##port,\
317
+   .ep_out.bEndpointAddress      = UDI_CDC_DATA_EP_OUT_##port,\
318
+   .iface.bInterfaceNumber       = UDI_CDC_DATA_IFACE_NUMBER_##port,\
319
+   .iface.iInterface             = UDI_CDC_DATA_STRING_ID_##port,\
320
+   }
321
+
322
+#define UDI_CDC_DATA_DESC_HS(port) { \
323
+   UDI_CDC_DATA_DESC_COMMON \
324
+   .ep_in.wMaxPacketSize         = LE16(UDI_CDC_DATA_EPS_HS_SIZE),\
325
+   .ep_out.wMaxPacketSize        = LE16(UDI_CDC_DATA_EPS_HS_SIZE),\
326
+   .ep_in.bEndpointAddress       = UDI_CDC_DATA_EP_IN_##port,\
327
+   .ep_out.bEndpointAddress      = UDI_CDC_DATA_EP_OUT_##port,\
328
+   .iface.bInterfaceNumber       = UDI_CDC_DATA_IFACE_NUMBER_##port,\
329
+   .iface.iInterface             = UDI_CDC_DATA_STRING_ID_##port,\
330
+   }
331
+
332
+//@}
333
+
334
+/**
335
+ * \ingroup udi_group
336
+ * \defgroup udi_cdc_group USB Device Interface (UDI) for Communication Class Device (CDC)
337
+ *
338
+ * Common APIs used by high level application to use this USB class.
339
+ *
340
+ * These routines are used to transfer and control data
341
+ * to/from USB CDC endpoint.
342
+ *
343
+ * See \ref udi_cdc_quickstart.
344
+ * @{
345
+ */
346
+
347
+/**
348
+ * \name Interface for application with single CDC interface support
349
+ */
350
+//@{
351
+
352
+/**
353
+ * \brief Notify a state change of DCD signal
354
+ *
355
+ * \param b_set      DCD is enabled if true, else disabled
356
+ */
357
+void udi_cdc_ctrl_signal_dcd(bool b_set);
358
+
359
+/**
360
+ * \brief Notify a state change of DSR signal
361
+ *
362
+ * \param b_set      DSR is enabled if true, else disabled
363
+ */
364
+void udi_cdc_ctrl_signal_dsr(bool b_set);
365
+
366
+/**
367
+ * \brief Notify a framing error
368
+ */
369
+void udi_cdc_signal_framing_error(void);
370
+
371
+/**
372
+ * \brief Notify a parity error
373
+ */
374
+void udi_cdc_signal_parity_error(void);
375
+
376
+/**
377
+ * \brief Notify a overrun
378
+ */
379
+void udi_cdc_signal_overrun(void);
380
+
381
+/**
382
+ * \brief Gets the number of byte received
383
+ *
384
+ * \return the number of data available
385
+ */
386
+iram_size_t udi_cdc_get_nb_received_data(void);
387
+
388
+/**
389
+ * \brief This function checks if a character has been received on the CDC line
390
+ *
391
+ * \return \c 1 if a byte is ready to be read.
392
+ */
393
+bool udi_cdc_is_rx_ready(void);
394
+
395
+/**
396
+ * \brief Waits and gets a value on CDC line
397
+ *
398
+ * \return value read on CDC line
399
+ */
400
+int udi_cdc_getc(void);
401
+
402
+/**
403
+ * \brief Reads a RAM buffer on CDC line
404
+ *
405
+ * \param buf       Values read
406
+ * \param size      Number of value read
407
+ *
408
+ * \return the number of data remaining
409
+ */
410
+iram_size_t udi_cdc_read_buf(void* buf, iram_size_t size);
411
+
412
+/**
413
+ * \brief Non polling reads of a up to 'size' data from CDC line
414
+ *
415
+ * \param port      Communication port number to manage
416
+ * \param buf       Buffer where to store read data
417
+ * \param size      Maximum number of data to read (size of buffer)
418
+ *
419
+ * \return the number of data effectively read
420
+ */
421
+iram_size_t udi_cdc_read_no_polling(void* buf, iram_size_t size);
422
+
423
+/**
424
+ * \brief Gets the number of free byte in TX buffer
425
+ *
426
+ * \return the number of free byte in TX buffer
427
+ */
428
+iram_size_t udi_cdc_get_free_tx_buffer(void);
429
+
430
+/**
431
+ * \brief This function checks if a new character sent is possible
432
+ * The type int is used to support scanf redirection from compiler LIB.
433
+ *
434
+ * \return \c 1 if a new character can be sent
435
+ */
436
+bool udi_cdc_is_tx_ready(void);
437
+
438
+/**
439
+ * \brief Puts a byte on CDC line
440
+ * The type int is used to support printf redirection from compiler LIB.
441
+ *
442
+ * \param value      Value to put
443
+ *
444
+ * \return \c 1 if function was successfully done, otherwise \c 0.
445
+ */
446
+int udi_cdc_putc(int value);
447
+
448
+/**
449
+ * \brief Writes a RAM buffer on CDC line
450
+ *
451
+ * \param buf       Values to write
452
+ * \param size      Number of value to write
453
+ *
454
+ * \return the number of data remaining
455
+ */
456
+iram_size_t udi_cdc_write_buf(const void* buf, iram_size_t size);
457
+//@}
458
+
459
+/**
460
+ * \name Interface for application with multi CDC interfaces support
461
+ */
462
+//@{
463
+
464
+/**
465
+ * \brief Notify a state change of DCD signal
466
+ *
467
+ * \param port       Communication port number to manage
468
+ * \param b_set      DCD is enabled if true, else disabled
469
+ */
470
+void udi_cdc_multi_ctrl_signal_dcd(uint8_t port, bool b_set);
471
+
472
+/**
473
+ * \brief Notify a state change of DSR signal
474
+ *
475
+ * \param port       Communication port number to manage
476
+ * \param b_set      DSR is enabled if true, else disabled
477
+ */
478
+void udi_cdc_multi_ctrl_signal_dsr(uint8_t port, bool b_set);
479
+
480
+/**
481
+ * \brief Notify a framing error
482
+ *
483
+ * \param port       Communication port number to manage
484
+ */
485
+void udi_cdc_multi_signal_framing_error(uint8_t port);
486
+
487
+/**
488
+ * \brief Notify a parity error
489
+ *
490
+ * \param port       Communication port number to manage
491
+ */
492
+void udi_cdc_multi_signal_parity_error(uint8_t port);
493
+
494
+/**
495
+ * \brief Notify a overrun
496
+ *
497
+ * \param port       Communication port number to manage
498
+ */
499
+void udi_cdc_multi_signal_overrun(uint8_t port);
500
+
501
+/**
502
+ * \brief Gets the number of byte received
503
+ *
504
+ * \param port       Communication port number to manage
505
+ *
506
+ * \return the number of data available
507
+ */
508
+iram_size_t udi_cdc_multi_get_nb_received_data(uint8_t port);
509
+
510
+/**
511
+ * \brief This function checks if a character has been received on the CDC line
512
+ *
513
+ * \param port       Communication port number to manage
514
+ *
515
+ * \return \c 1 if a byte is ready to be read.
516
+ */
517
+bool udi_cdc_multi_is_rx_ready(uint8_t port);
518
+
519
+/**
520
+ * \brief Waits and gets a value on CDC line
521
+ *
522
+ * \param port       Communication port number to manage
523
+ *
524
+ * \return value read on CDC line
525
+ */
526
+int udi_cdc_multi_getc(uint8_t port);
527
+
528
+/**
529
+ * \brief Reads a RAM buffer on CDC line
530
+ *
531
+ * \param port       Communication port number to manage
532
+ * \param buf       Values read
533
+ * \param size      Number of values read
534
+ *
535
+ * \return the number of data remaining
536
+ */
537
+iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size);
538
+
539
+/**
540
+ * \brief Gets the number of free byte in TX buffer
541
+ *
542
+ * \param port       Communication port number to manage
543
+ *
544
+ * \return the number of free byte in TX buffer
545
+ */
546
+iram_size_t udi_cdc_multi_get_free_tx_buffer(uint8_t port);
547
+
548
+/**
549
+ * \brief This function checks if a new character sent is possible
550
+ * The type int is used to support scanf redirection from compiler LIB.
551
+ *
552
+ * \param port       Communication port number to manage
553
+ *
554
+ * \return \c 1 if a new character can be sent
555
+ */
556
+bool udi_cdc_multi_is_tx_ready(uint8_t port);
557
+
558
+/**
559
+ * \brief Puts a byte on CDC line
560
+ * The type int is used to support printf redirection from compiler LIB.
561
+ *
562
+ * \param port       Communication port number to manage
563
+ * \param value      Value to put
564
+ *
565
+ * \return \c 1 if function was successfully done, otherwise \c 0.
566
+ */
567
+int udi_cdc_multi_putc(uint8_t port, int value);
568
+
569
+/**
570
+ * \brief Writes a RAM buffer on CDC line
571
+ *
572
+ * \param port       Communication port number to manage
573
+ * \param buf       Values to write
574
+ * \param size      Number of value to write
575
+ *
576
+ * \return the number of data remaining
577
+ */
578
+iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t size);
579
+//@}
580
+
581
+//@}
582
+
583
+/**
584
+ * \page udi_cdc_quickstart Quick start guide for USB device Communication Class Device module (UDI CDC)
585
+ *
586
+ * This is the quick start guide for the \ref udi_cdc_group
587
+ * "USB device interface CDC module (UDI CDC)" with step-by-step instructions on
588
+ * how to configure and use the modules in a selection of use cases.
589
+ *
590
+ * The use cases contain several code fragments. The code fragments in the
591
+ * steps for setup can be copied into a custom initialization function, while
592
+ * the steps for usage can be copied into, e.g., the main application function.
593
+ *
594
+ * \section udi_cdc_basic_use_case Basic use case
595
+ * In this basic use case, the "USB CDC (Single Interface Device)" module is used
596
+ * with only one communication port.
597
+ * The "USB CDC (Composite Device)" module usage is described in \ref udi_cdc_use_cases
598
+ * "Advanced use cases".
599
+ *
600
+ * \section udi_cdc_basic_use_case_setup Setup steps
601
+ * \subsection udi_cdc_basic_use_case_setup_prereq Prerequisites
602
+ * \copydetails udc_basic_use_case_setup_prereq
603
+ * \subsection udi_cdc_basic_use_case_setup_code Example code
604
+ * \copydetails udc_basic_use_case_setup_code
605
+ * \subsection udi_cdc_basic_use_case_setup_flow Workflow
606
+ * \copydetails udc_basic_use_case_setup_flow
607
+ *
608
+ * \section udi_cdc_basic_use_case_usage Usage steps
609
+ *
610
+ * \subsection udi_cdc_basic_use_case_usage_code Example code
611
+ * Content of conf_usb.h:
612
+ * \code
613
+	 #define UDI_CDC_ENABLE_EXT(port) my_callback_cdc_enable()
614
+	 extern bool my_callback_cdc_enable(void);
615
+	 #define UDI_CDC_DISABLE_EXT(port) my_callback_cdc_disable()
616
+	 extern void my_callback_cdc_disable(void);
617
+	 #define  UDI_CDC_LOW_RATE
618
+
619
+	 #define  UDI_CDC_DEFAULT_RATE             115200
620
+	 #define  UDI_CDC_DEFAULT_STOPBITS         CDC_STOP_BITS_1
621
+	 #define  UDI_CDC_DEFAULT_PARITY           CDC_PAR_NONE
622
+	 #define  UDI_CDC_DEFAULT_DATABITS         8
623
+
624
+	 #include "udi_cdc_conf.h" // At the end of conf_usb.h file
625
+\endcode
626
+ *
627
+ * Add to application C-file:
628
+ * \code
629
+	 static bool my_flag_autorize_cdc_transfert = false;
630
+	 bool my_callback_cdc_enable(void)
631
+	 {
632
+	    my_flag_autorize_cdc_transfert = true;
633
+	    return true;
634
+	 }
635
+	 void my_callback_cdc_disable(void)
636
+	 {
637
+	    my_flag_autorize_cdc_transfert = false;
638
+	 }
639
+
640
+	 void task(void)
641
+	 {
642
+	    if (my_flag_autorize_cdc_transfert) {
643
+	        udi_cdc_putc('A');
644
+	        udi_cdc_getc();
645
+	    }
646
+	 }
647
+\endcode
648
+ *
649
+ * \subsection udi_cdc_basic_use_case_setup_flow Workflow
650
+ * -# Ensure that conf_usb.h is available and contains the following configuration,
651
+ * which is the USB device CDC configuration:
652
+ *   - \code #define USB_DEVICE_SERIAL_NAME  "12...EF" // Disk SN for CDC \endcode
653
+ *     \note The USB serial number is mandatory when a CDC interface is used.
654
+ *   - \code #define UDI_CDC_ENABLE_EXT(port) my_callback_cdc_enable()
655
+	 extern bool my_callback_cdc_enable(void); \endcode
656
+ *     \note After the device enumeration (detecting and identifying USB devices),
657
+ *     the USB host starts the device configuration. When the USB CDC interface
658
+ *     from the device is accepted by the host, the USB host enables this interface and the
659
+ *     UDI_CDC_ENABLE_EXT() callback function is called and return true.
660
+ *     Thus, when this event is received, the data transfer on CDC interface are authorized.
661
+ *   - \code #define UDI_CDC_DISABLE_EXT(port) my_callback_cdc_disable()
662
+	 extern void my_callback_cdc_disable(void); \endcode
663
+ *     \note When the USB device is unplugged or is reset by the USB host, the USB
664
+ *     interface is disabled and the UDI_CDC_DISABLE_EXT() callback function
665
+ *     is called. Thus, the data transfer must be stopped on CDC interface.
666
+ *   - \code #define  UDI_CDC_LOW_RATE \endcode
667
+ *     \note  Define it when the transfer CDC Device to Host is a low rate
668
+ *     (<512000 bauds) to reduce CDC buffers size.
669
+ *   - \code #define  UDI_CDC_DEFAULT_RATE             115200
670
+	#define  UDI_CDC_DEFAULT_STOPBITS         CDC_STOP_BITS_1
671
+	#define  UDI_CDC_DEFAULT_PARITY           CDC_PAR_NONE
672
+	#define  UDI_CDC_DEFAULT_DATABITS         8 \endcode
673
+ *     \note Default configuration of communication port at startup.
674
+ * -# Send or wait data on CDC line:
675
+ *   - \code // Waits and gets a value on CDC line
676
+	int udi_cdc_getc(void);
677
+	// Reads a RAM buffer on CDC line
678
+	iram_size_t udi_cdc_read_buf(int* buf, iram_size_t size);
679
+	// Puts a byte on CDC line
680
+	int udi_cdc_putc(int value);
681
+	// Writes a RAM buffer on CDC line
682
+	iram_size_t udi_cdc_write_buf(const int* buf, iram_size_t size); \endcode
683
+ *
684
+ * \section udi_cdc_use_cases Advanced use cases
685
+ * For more advanced use of the UDI CDC module, see the following use cases:
686
+ * - \subpage udi_cdc_use_case_composite
687
+ * - \subpage udc_use_case_1
688
+ * - \subpage udc_use_case_2
689
+ * - \subpage udc_use_case_3
690
+ * - \subpage udc_use_case_4
691
+ * - \subpage udc_use_case_5
692
+ * - \subpage udc_use_case_6
693
+ */
694
+
695
+/**
696
+ * \page udi_cdc_use_case_composite CDC in a composite device
697
+ *
698
+ * A USB Composite Device is a USB Device which uses more than one USB class.
699
+ * In this use case, the "USB CDC (Composite Device)" module is used to
700
+ * create a USB composite device. Thus, this USB module can be associated with
701
+ * another "Composite Device" module, like "USB HID Mouse (Composite Device)".
702
+ *
703
+ * Also, you can refer to application note
704
+ * <A href="http://www.atmel.com/dyn/resources/prod_documents/doc8445.pdf">
705
+ * AVR4902 ASF - USB Composite Device</A>.
706
+ *
707
+ * \section udi_cdc_use_case_composite_setup Setup steps
708
+ * For the setup code of this use case to work, the
709
+ * \ref udi_cdc_basic_use_case "basic use case" must be followed.
710
+ *
711
+ * \section udi_cdc_use_case_composite_usage Usage steps
712
+ *
713
+ * \subsection udi_cdc_use_case_composite_usage_code Example code
714
+ * Content of conf_usb.h:
715
+ * \code
716
+	 #define USB_DEVICE_EP_CTRL_SIZE  64
717
+	 #define USB_DEVICE_NB_INTERFACE (X+2)
718
+	 #define USB_DEVICE_MAX_EP (X+3)
719
+
720
+	 #define  UDI_CDC_DATA_EP_IN_0          (1 | USB_EP_DIR_IN)  // TX
721
+	 #define  UDI_CDC_DATA_EP_OUT_0         (2 | USB_EP_DIR_OUT) // RX
722
+	 #define  UDI_CDC_COMM_EP_0             (3 | USB_EP_DIR_IN)  // Notify endpoint
723
+	 #define  UDI_CDC_COMM_IFACE_NUMBER_0   X+0
724
+	 #define  UDI_CDC_DATA_IFACE_NUMBER_0   X+1
725
+
726
+	 #define UDI_COMPOSITE_DESC_T \
727
+	    usb_iad_desc_t udi_cdc_iad; \
728
+	    udi_cdc_comm_desc_t udi_cdc_comm; \
729
+	    udi_cdc_data_desc_t udi_cdc_data; \
730
+	    ...
731
+	 #define UDI_COMPOSITE_DESC_FS \
732
+	    .udi_cdc_iad               = UDI_CDC_IAD_DESC_0, \
733
+	    .udi_cdc_comm              = UDI_CDC_COMM_DESC_0, \
734
+	    .udi_cdc_data              = UDI_CDC_DATA_DESC_0_FS, \
735
+	    ...
736
+	 #define UDI_COMPOSITE_DESC_HS \
737
+	    .udi_cdc_iad               = UDI_CDC_IAD_DESC_0, \
738
+	    .udi_cdc_comm              = UDI_CDC_COMM_DESC_0, \
739
+	    .udi_cdc_data              = UDI_CDC_DATA_DESC_0_HS, \
740
+	    ...
741
+	 #define UDI_COMPOSITE_API \
742
+	    &udi_api_cdc_comm,       \
743
+	    &udi_api_cdc_data,       \
744
+	    ...
745
+\endcode
746
+ *
747
+ * \subsection udi_cdc_use_case_composite_usage_flow Workflow
748
+ * -# Ensure that conf_usb.h is available and contains the following parameters
749
+ * required for a USB composite device configuration:
750
+ *   - \code // Endpoint control size, This must be:
751
+	// - 8, 16, 32 or 64 for full speed device (8 is recommended to save RAM)
752
+	// - 64 for a high speed device
753
+	#define USB_DEVICE_EP_CTRL_SIZE  64
754
+	// Total Number of interfaces on this USB device.
755
+	// Add 2 for CDC.
756
+	#define USB_DEVICE_NB_INTERFACE (X+2)
757
+	// Total number of endpoints on this USB device.
758
+	// This must include each endpoint for each interface.
759
+	// Add 3 for CDC.
760
+	#define USB_DEVICE_MAX_EP (X+3) \endcode
761
+ * -# Ensure that conf_usb.h contains the description of
762
+ * composite device:
763
+ *   - \code // The endpoint numbers chosen by you for the CDC.
764
+	// The endpoint numbers starting from 1.
765
+	#define  UDI_CDC_DATA_EP_IN_0            (1 | USB_EP_DIR_IN)  // TX
766
+	#define  UDI_CDC_DATA_EP_OUT_0           (2 | USB_EP_DIR_OUT) // RX
767
+	#define  UDI_CDC_COMM_EP_0               (3 | USB_EP_DIR_IN)  // Notify endpoint
768
+	// The interface index of an interface starting from 0
769
+	#define  UDI_CDC_COMM_IFACE_NUMBER_0     X+0
770
+	#define  UDI_CDC_DATA_IFACE_NUMBER_0     X+1 \endcode
771
+ * -# Ensure that conf_usb.h contains the following parameters
772
+ * required for a USB composite device configuration:
773
+ *   - \code // USB Interfaces descriptor structure
774
+	#define UDI_COMPOSITE_DESC_T \
775
+	   ...
776
+	   usb_iad_desc_t udi_cdc_iad; \
777
+	   udi_cdc_comm_desc_t udi_cdc_comm; \
778
+	   udi_cdc_data_desc_t udi_cdc_data; \
779
+	   ...
780
+	// USB Interfaces descriptor value for Full Speed
781
+	#define UDI_COMPOSITE_DESC_FS \
782
+	   ...
783
+	   .udi_cdc_iad               = UDI_CDC_IAD_DESC_0, \
784
+	   .udi_cdc_comm              = UDI_CDC_COMM_DESC_0, \
785
+	   .udi_cdc_data              = UDI_CDC_DATA_DESC_0_FS, \
786
+	   ...
787
+	// USB Interfaces descriptor value for High Speed
788
+	#define UDI_COMPOSITE_DESC_HS \
789
+	   ...
790
+	   .udi_cdc_iad               = UDI_CDC_IAD_DESC_0, \
791
+	   .udi_cdc_comm              = UDI_CDC_COMM_DESC_0, \
792
+	   .udi_cdc_data              = UDI_CDC_DATA_DESC_0_HS, \
793
+	   ...
794
+	// USB Interface APIs
795
+	#define UDI_COMPOSITE_API \
796
+	   ...
797
+	   &udi_api_cdc_comm,       \
798
+	   &udi_api_cdc_data,       \
799
+	   ... \endcode
800
+ *   - \note The descriptors order given in the four lists above must be the
801
+ *     same as the order defined by all interface indexes. The interface index
802
+ *     orders are defined through UDI_X_IFACE_NUMBER defines.\n
803
+ *     Also, the CDC requires a USB Interface Association Descriptor (IAD) for
804
+ *     composite device.
805
+ */
806
+
807
+#ifdef __cplusplus
808
+}
809
+#endif
810
+#endif // _UDI_CDC_H_

+ 188
- 0
Marlin/src/HAL/HAL_DUE/usb/udi_composite_desc.c View File

@@ -0,0 +1,188 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Descriptors for an USB Composite Device
5
+ *
6
+ * Copyright (c) 2009-2016 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+ 
47
+#ifdef ARDUINO_ARCH_SAM 
48
+
49
+#include "conf_usb.h"
50
+#include "udd.h"
51
+#include "udc_desc.h"
52
+
53
+/**
54
+ * \defgroup udi_group_desc Descriptors for a USB Device
55
+ * composite
56
+ *
57
+ * @{
58
+ */
59
+
60
+/**INDENT-OFF**/
61
+
62
+//! USB Device Descriptor
63
+COMPILER_WORD_ALIGNED
64
+UDC_DESC_STORAGE usb_dev_desc_t udc_device_desc = {
65
+	.bLength                   = sizeof(usb_dev_desc_t),
66
+	.bDescriptorType           = USB_DT_DEVICE,
67
+	.bcdUSB                    = LE16(USB_V2_0),
68
+	.bDeviceClass              = 0,
69
+	.bDeviceSubClass           = 0,
70
+	.bDeviceProtocol           = 0,
71
+	.bMaxPacketSize0           = USB_DEVICE_EP_CTRL_SIZE,
72
+	.idVendor                  = LE16(USB_DEVICE_VENDOR_ID),
73
+	.idProduct                 = LE16(USB_DEVICE_PRODUCT_ID),
74
+	.bcdDevice                 = LE16((USB_DEVICE_MAJOR_VERSION << 8)
75
+		| USB_DEVICE_MINOR_VERSION),
76
+#ifdef USB_DEVICE_MANUFACTURE_NAME
77
+	.iManufacturer             = 1,
78
+#else
79
+	.iManufacturer             = 0,  // No manufacture string
80
+#endif
81
+#ifdef USB_DEVICE_PRODUCT_NAME
82
+	.iProduct                  = 2,
83
+#else
84
+	.iProduct                  = 0,  // No product string
85
+#endif
86
+#if (defined USB_DEVICE_SERIAL_NAME || defined USB_DEVICE_GET_SERIAL_NAME_POINTER)
87
+	.iSerialNumber             = 3,
88
+#else
89
+	.iSerialNumber             = 0,  // No serial string
90
+#endif
91
+	.bNumConfigurations        = 1
92
+};
93
+
94
+
95
+#ifdef USB_DEVICE_HS_SUPPORT
96
+//! USB Device Qualifier Descriptor for HS
97
+COMPILER_WORD_ALIGNED
98
+UDC_DESC_STORAGE usb_dev_qual_desc_t udc_device_qual = {
99
+	.bLength                   = sizeof(usb_dev_qual_desc_t),
100
+	.bDescriptorType           = USB_DT_DEVICE_QUALIFIER,
101
+	.bcdUSB                    = LE16(USB_V2_0),
102
+	.bDeviceClass              = 0,
103
+	.bDeviceSubClass           = 0,
104
+	.bDeviceProtocol           = 0,
105
+	.bMaxPacketSize0           = USB_DEVICE_EP_CTRL_SIZE,
106
+	.bNumConfigurations        = 1
107
+};
108
+#endif
109
+
110
+//! Structure for USB Device Configuration Descriptor
111
+COMPILER_PACK_SET(1)
112
+typedef struct {
113
+	usb_conf_desc_t conf;
114
+	UDI_COMPOSITE_DESC_T;
115
+} udc_desc_t;
116
+COMPILER_PACK_RESET()
117
+
118
+//! USB Device Configuration Descriptor filled for FS
119
+COMPILER_WORD_ALIGNED
120
+UDC_DESC_STORAGE udc_desc_t udc_desc_fs = {
121
+	.conf.bLength              = sizeof(usb_conf_desc_t),
122
+	.conf.bDescriptorType      = USB_DT_CONFIGURATION,
123
+	.conf.wTotalLength         = LE16(sizeof(udc_desc_t)),
124
+	.conf.bNumInterfaces       = USB_DEVICE_NB_INTERFACE,
125
+	.conf.bConfigurationValue  = 1,
126
+	.conf.iConfiguration       = 0,
127
+	.conf.bmAttributes         = USB_CONFIG_ATTR_MUST_SET | USB_DEVICE_ATTR,
128
+	.conf.bMaxPower            = USB_CONFIG_MAX_POWER(USB_DEVICE_POWER),
129
+	UDI_COMPOSITE_DESC_FS
130
+};
131
+
132
+#ifdef USB_DEVICE_HS_SUPPORT
133
+//! USB Device Configuration Descriptor filled for HS
134
+COMPILER_WORD_ALIGNED
135
+UDC_DESC_STORAGE udc_desc_t udc_desc_hs = {
136
+	.conf.bLength              = sizeof(usb_conf_desc_t),
137
+	.conf.bDescriptorType      = USB_DT_CONFIGURATION,
138
+	.conf.wTotalLength         = LE16(sizeof(udc_desc_t)),
139
+	.conf.bNumInterfaces       = USB_DEVICE_NB_INTERFACE,
140
+	.conf.bConfigurationValue  = 1,
141
+	.conf.iConfiguration       = 0,
142
+	.conf.bmAttributes         = USB_CONFIG_ATTR_MUST_SET | USB_DEVICE_ATTR,
143
+	.conf.bMaxPower            = USB_CONFIG_MAX_POWER(USB_DEVICE_POWER),
144
+	UDI_COMPOSITE_DESC_HS
145
+};
146
+#endif
147
+
148
+
149
+/**
150
+ * \name UDC structures which contains all USB Device definitions
151
+ */
152
+//@{
153
+
154
+//! Associate an UDI for each USB interface
155
+UDC_DESC_STORAGE udi_api_t *udi_apis[USB_DEVICE_NB_INTERFACE] = {
156
+	UDI_COMPOSITE_API
157
+};
158
+
159
+//! Add UDI with USB Descriptors FS
160
+UDC_DESC_STORAGE udc_config_speed_t   udc_config_lsfs[1] = {{
161
+	.desc          = (usb_conf_desc_t UDC_DESC_STORAGE*)&udc_desc_fs,
162
+	.udi_apis      = udi_apis,
163
+}};
164
+
165
+#ifdef USB_DEVICE_HS_SUPPORT
166
+//! Add UDI with USB Descriptors HS
167
+UDC_DESC_STORAGE udc_config_speed_t   udc_config_hs[1] = {{
168
+	.desc          = (usb_conf_desc_t UDC_DESC_STORAGE*)&udc_desc_hs,
169
+	.udi_apis      = udi_apis,
170
+}};
171
+#endif
172
+
173
+//! Add all information about USB Device in global structure for UDC
174
+UDC_DESC_STORAGE udc_config_t udc_config = {
175
+	.confdev_lsfs = &udc_device_desc,
176
+	.conf_lsfs = udc_config_lsfs,
177
+#ifdef USB_DEVICE_HS_SUPPORT
178
+	.confdev_hs = &udc_device_desc,
179
+	.qualifier = &udc_device_qual,
180
+	.conf_hs = udc_config_hs,
181
+#endif
182
+};
183
+
184
+//@}
185
+/**INDENT-ON**/
186
+//@}
187
+
188
+#endif

+ 1128
- 0
Marlin/src/HAL/HAL_DUE/usb/udi_msc.c
File diff suppressed because it is too large
View File


+ 376
- 0
Marlin/src/HAL/HAL_DUE/usb/udi_msc.h View File

@@ -0,0 +1,376 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB Device Mass Storage Class (MSC) interface definitions.
5
+ *
6
+ * Copyright (c) 2009-2016 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _UDI_MSC_H_
48
+#define _UDI_MSC_H_
49
+
50
+#include "conf_usb.h"
51
+#include "usb_protocol.h"
52
+#include "usb_protocol_msc.h"
53
+#include "udd.h"
54
+#include "udc_desc.h"
55
+#include "udi.h"
56
+
57
+#ifdef __cplusplus
58
+extern "C" {
59
+#endif
60
+
61
+/**
62
+ * \addtogroup udi_msc_group_udc
63
+ * @{
64
+ */
65
+//! Global structure which contains standard UDI interface for UDC
66
+extern UDC_DESC_STORAGE udi_api_t udi_api_msc;
67
+//@}
68
+
69
+/**
70
+ * \ingroup udi_msc_group
71
+ * \defgroup udi_msc_group USB interface descriptors
72
+ *
73
+ * The following structures provide predefined USB interface descriptors.
74
+ * It must be used to define the final USB descriptors.
75
+ */
76
+//@{
77
+
78
+//! Interface descriptor structure for MSC
79
+typedef struct {
80
+	usb_iface_desc_t iface;
81
+	usb_ep_desc_t ep_in;
82
+	usb_ep_desc_t ep_out;
83
+} udi_msc_desc_t;
84
+
85
+//! By default no string associated to this interface
86
+#ifndef UDI_MSC_STRING_ID
87
+#define UDI_MSC_STRING_ID     0
88
+#endif
89
+
90
+//! MSC endpoints size for full speed
91
+#define UDI_MSC_EPS_SIZE_FS   64
92
+//! MSC endpoints size for high speed
93
+#define UDI_MSC_EPS_SIZE_HS   512
94
+
95
+//! Content of MSC interface descriptor for all speeds
96
+#define UDI_MSC_DESC      \
97
+   .iface.bLength             = sizeof(usb_iface_desc_t),\
98
+   .iface.bDescriptorType     = USB_DT_INTERFACE,\
99
+   .iface.bInterfaceNumber    = UDI_MSC_IFACE_NUMBER,\
100
+   .iface.bAlternateSetting   = 0,\
101
+   .iface.bNumEndpoints       = 2,\
102
+   .iface.bInterfaceClass     = MSC_CLASS,\
103
+   .iface.bInterfaceSubClass  = MSC_SUBCLASS_TRANSPARENT,\
104
+   .iface.bInterfaceProtocol  = MSC_PROTOCOL_BULK,\
105
+   .iface.iInterface          = UDI_MSC_STRING_ID,\
106
+   .ep_in.bLength             = sizeof(usb_ep_desc_t),\
107
+   .ep_in.bDescriptorType     = USB_DT_ENDPOINT,\
108
+   .ep_in.bEndpointAddress    = UDI_MSC_EP_IN,\
109
+   .ep_in.bmAttributes        = USB_EP_TYPE_BULK,\
110
+   .ep_in.bInterval           = 0,\
111
+   .ep_out.bLength            = sizeof(usb_ep_desc_t),\
112
+   .ep_out.bDescriptorType    = USB_DT_ENDPOINT,\
113
+   .ep_out.bEndpointAddress   = UDI_MSC_EP_OUT,\
114
+   .ep_out.bmAttributes       = USB_EP_TYPE_BULK,\
115
+   .ep_out.bInterval          = 0,
116
+
117
+//! Content of MSC interface descriptor for full speed only
118
+#define UDI_MSC_DESC_FS   {\
119
+   UDI_MSC_DESC \
120
+   .ep_in.wMaxPacketSize      = LE16(UDI_MSC_EPS_SIZE_FS),\
121
+   .ep_out.wMaxPacketSize     = LE16(UDI_MSC_EPS_SIZE_FS),\
122
+   }
123
+
124
+//! Content of MSC interface descriptor for high speed only
125
+#define UDI_MSC_DESC_HS   {\
126
+   UDI_MSC_DESC \
127
+   .ep_in.wMaxPacketSize      = LE16(UDI_MSC_EPS_SIZE_HS),\
128
+   .ep_out.wMaxPacketSize     = LE16(UDI_MSC_EPS_SIZE_HS),\
129
+   }
130
+//@}
131
+
132
+
133
+/**
134
+ * \ingroup udi_group
135
+ * \defgroup udi_msc_group USB Device Interface (UDI) for Mass Storage Class (MSC)
136
+ *
137
+ * Common APIs used by high level application to use this USB class.
138
+ *
139
+ * These routines are used by memory to transfer its data
140
+ * to/from USB MSC endpoints.
141
+ *
142
+ * See \ref udi_msc_quickstart.
143
+ * @{
144
+ */
145
+
146
+/**
147
+ * \brief Process the background read/write commands
148
+ *
149
+ * Routine called by the main loop
150
+ */
151
+bool udi_msc_process_trans(void);
152
+
153
+/**
154
+ * \brief Transfers data to/from USB MSC endpoints
155
+ *
156
+ *
157
+ * \param b_read        Memory to USB, if true
158
+ * \param block         Buffer on Internal RAM to send or fill
159
+ * \param block_size    Buffer size to send or fill
160
+ * \param callback      Function to call at the end of transfer.
161
+ *                      If NULL then the routine exit when transfer is finish.
162
+ *
163
+ * \return \c 1 if function was successfully done, otherwise \c 0.
164
+ */
165
+bool udi_msc_trans_block(bool b_read, uint8_t * block, iram_size_t block_size,
166
+		void (*callback) (udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep));
167
+//@}
168
+
169
+#ifdef __cplusplus
170
+}
171
+#endif
172
+
173
+
174
+/**
175
+ * \page udi_msc_quickstart Quick start guide for USB device Mass Storage module (UDI MSC)
176
+ *
177
+ * This is the quick start guide for the \ref udi_msc_group 
178
+ * "USB device interface MSC module (UDI MSC)" with step-by-step instructions on 
179
+ * how to configure and use the modules in a selection of use cases.
180
+ *
181
+ * The use cases contain several code fragments. The code fragments in the
182
+ * steps for setup can be copied into a custom initialization function, while
183
+ * the steps for usage can be copied into, e.g., the main application function.
184
+ * 
185
+ * \section udi_msc_basic_use_case Basic use case
186
+ * In this basic use case, the "USB MSC (Single Interface Device)" module is used.
187
+ * The "USB MSC (Composite Device)" module usage is described in \ref udi_msc_use_cases
188
+ * "Advanced use cases".
189
+ *
190
+ * \section udi_msc_basic_use_case_setup Setup steps
191
+ * \subsection udi_msc_basic_use_case_setup_prereq Prerequisites
192
+ * \copydetails udc_basic_use_case_setup_prereq
193
+ * \subsection udi_msc_basic_use_case_setup_code Example code
194
+ * \copydetails udc_basic_use_case_setup_code
195
+ * \subsection udi_msc_basic_use_case_setup_flow Workflow
196
+ * \copydetails udc_basic_use_case_setup_flow
197
+ *
198
+ * \section udi_msc_basic_use_case_usage Usage steps
199
+ *
200
+ * \subsection udi_msc_basic_use_case_usage_code Example code
201
+ * Content of conf_usb.h:
202
+ * \code
203
+	#define  USB_DEVICE_SERIAL_NAME  "12...EF" // Disk SN for MSC
204
+	#define UDI_MSC_GLOBAL_VENDOR_ID \
205
+	   'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
206
+	#define UDI_MSC_GLOBAL_PRODUCT_VERSION \
207
+	   '1', '.', '0', '0'
208
+	#define UDI_MSC_ENABLE_EXT() my_callback_msc_enable()
209
+	extern bool my_callback_msc_enable(void);
210
+	#define UDI_MSC_DISABLE_EXT() my_callback_msc_disable()
211
+	extern void my_callback_msc_disable(void);
212
+	#include "udi_msc_conf.h" // At the end of conf_usb.h file
213
+\endcode
214
+ *
215
+ * Add to application C-file:
216
+ * \code
217
+	 static bool my_flag_autorize_msc_transfert = false;
218
+	 bool my_callback_msc_enable(void)
219
+	 {
220
+	    my_flag_autorize_msc_transfert = true;
221
+	    return true;
222
+	 }
223
+	 void my_callback_msc_disable(void)
224
+	 {
225
+	    my_flag_autorize_msc_transfert = false;
226
+	 }
227
+
228
+	 void task(void)
229
+	 {
230
+	    udi_msc_process_trans();
231
+	 }
232
+\endcode
233
+ *
234
+ * \subsection udi_msc_basic_use_case_setup_flow Workflow
235
+ * -# Ensure that conf_usb.h is available and contains the following configuration,
236
+ * which is the USB device MSC configuration:
237
+ *   - \code #define USB_DEVICE_SERIAL_NAME  "12...EF" // Disk SN for MSC \endcode
238
+ *     \note The USB serial number is mandatory when a MSC interface is used.
239
+ *   - \code //! Vendor name and Product version of MSC interface
240
+	#define UDI_MSC_GLOBAL_VENDOR_ID \
241
+	   'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
242
+	#define UDI_MSC_GLOBAL_PRODUCT_VERSION \
243
+	   '1', '.', '0', '0' \endcode
244
+ *     \note The USB MSC interface requires a vendor ID (8 ASCII characters)
245
+ *     and a product version (4 ASCII characters).
246
+ *   - \code #define UDI_MSC_ENABLE_EXT() my_callback_msc_enable()
247
+	extern bool my_callback_msc_enable(void); \endcode
248
+ *     \note After the device enumeration (detecting and identifying USB devices),
249
+ *     the USB host starts the device configuration. When the USB MSC interface 
250
+ *     from the device is accepted by the host, the USB host enables this interface and the
251
+ *     UDI_MSC_ENABLE_EXT() callback function is called and return true.
252
+ *     Thus, when this event is received, the tasks which call
253
+ *     udi_msc_process_trans() must be enabled.
254
+ *   - \code #define UDI_MSC_DISABLE_EXT() my_callback_msc_disable()
255
+	extern void my_callback_msc_disable(void); \endcode
256
+ *     \note When the USB device is unplugged or is reset by the USB host, the USB
257
+ *     interface is disabled and the UDI_MSC_DISABLE_EXT() callback function
258
+ *     is called. Thus, it is recommended to disable the task which is called udi_msc_process_trans().
259
+ * -# The MSC is automatically linked with memory control access component 
260
+ * which provides the memories interfaces. However, the memory data transfers
261
+ * must be done outside USB interrupt routine. This is done in the MSC process
262
+ * ("udi_msc_process_trans()") called by main loop:
263
+ *   - \code  * void task(void) {
264
+	udi_msc_process_trans();
265
+	} \endcode
266
+ * -# The MSC speed depends on task periodicity. To get the best speed
267
+ * the notification callback "UDI_MSC_NOTIFY_TRANS_EXT" can be used to wakeup
268
+ * this task (Example, through a mutex):
269
+ *   - \code #define  UDI_MSC_NOTIFY_TRANS_EXT()    msc_notify_trans()
270
+	void msc_notify_trans(void) {
271
+	wakeup_my_task();
272
+	} \endcode
273
+ *
274
+ * \section udi_msc_use_cases Advanced use cases
275
+ * For more advanced use of the UDI MSC module, see the following use cases:
276
+ * - \subpage udi_msc_use_case_composite
277
+ * - \subpage udc_use_case_1
278
+ * - \subpage udc_use_case_2
279
+ * - \subpage udc_use_case_3
280
+ * - \subpage udc_use_case_5
281
+ * - \subpage udc_use_case_6
282
+ */
283
+
284
+/**
285
+ * \page udi_msc_use_case_composite MSC in a composite device
286
+ *
287
+ * A USB Composite Device is a USB Device which uses more than one USB class.
288
+ * In this use case, the "USB MSC (Composite Device)" module is used to
289
+ * create a USB composite device. Thus, this USB module can be associated with
290
+ * another "Composite Device" module, like "USB HID Mouse (Composite Device)".
291
+ * 
292
+ * Also, you can refer to application note
293
+ * <A href="http://www.atmel.com/dyn/resources/prod_documents/doc8445.pdf">
294
+ * AVR4902 ASF - USB Composite Device</A>.
295
+ *
296
+ * \section udi_msc_use_case_composite_setup Setup steps
297
+ * For the setup code of this use case to work, the
298
+ * \ref udi_msc_basic_use_case "basic use case" must be followed.
299
+ *
300
+ * \section udi_msc_use_case_composite_usage Usage steps
301
+ *
302
+ * \subsection udi_msc_use_case_composite_usage_code Example code
303
+ * Content of conf_usb.h:
304
+ * \code
305
+	 #define USB_DEVICE_EP_CTRL_SIZE  64
306
+	 #define USB_DEVICE_NB_INTERFACE (X+1)
307
+	 #define USB_DEVICE_MAX_EP (X+2)
308
+
309
+	 #define UDI_MSC_EP_IN  (X | USB_EP_DIR_IN)
310
+	 #define UDI_MSC_EP_OUT (Y | USB_EP_DIR_OUT)
311
+	 #define UDI_MSC_IFACE_NUMBER  X
312
+
313
+	 #define UDI_COMPOSITE_DESC_T \
314
+	    udi_msc_desc_t udi_msc; \
315
+	    ...
316
+	 #define UDI_COMPOSITE_DESC_FS \
317
+	    .udi_msc = UDI_MSC_DESC, \
318
+	    ...
319
+	 #define UDI_COMPOSITE_DESC_HS \
320
+	    .udi_msc = UDI_MSC_DESC, \
321
+	    ...
322
+	 #define UDI_COMPOSITE_API \
323
+	    &udi_api_msc, \
324
+	    ...
325
+\endcode
326
+ *
327
+ * \subsection udi_msc_use_case_composite_usage_flow Workflow
328
+ * -# Ensure that conf_usb.h is available and contains the following parameters
329
+ * required for a USB composite device configuration:
330
+ *   - \code // Endpoint control size, This must be:
331
+	// - 8, 16, 32 or 64 for full speed device (8 is recommended to save RAM)
332
+	// - 64 for a high speed device
333
+	#define USB_DEVICE_EP_CTRL_SIZE  64
334
+	// Total Number of interfaces on this USB device.
335
+	// Add 1 for MSC.
336
+	#define USB_DEVICE_NB_INTERFACE (X+1)
337
+	// Total number of endpoints on this USB device.
338
+	// This must include each endpoint for each interface.
339
+	// Add 2 for MSC.
340
+	#define USB_DEVICE_MAX_EP (X+2) \endcode
341
+ * -# Ensure that conf_usb.h contains the description of
342
+ * composite device:
343
+ *   - \code // The endpoint numbers chosen by you for the MSC.
344
+	// The endpoint numbers starting from 1.
345
+	#define UDI_MSC_EP_IN  (X | USB_EP_DIR_IN)
346
+	#define UDI_MSC_EP_OUT (Y | USB_EP_DIR_OUT)
347
+	// The interface index of an interface starting from 0
348
+	#define UDI_MSC_IFACE_NUMBER  X \endcode
349
+ * -# Ensure that conf_usb.h contains the following parameters
350
+ * required for a USB composite device configuration:
351
+ *   - \code // USB Interfaces descriptor structure
352
+	#define UDI_COMPOSITE_DESC_T \
353
+	   ...
354
+	   udi_msc_desc_t udi_msc; \
355
+	   ...
356
+	// USB Interfaces descriptor value for Full Speed
357
+	#define UDI_COMPOSITE_DESC_FS \
358
+	   ...
359
+	   .udi_msc = UDI_MSC_DESC_FS, \
360
+	   ...
361
+	// USB Interfaces descriptor value for High Speed
362
+	#define UDI_COMPOSITE_DESC_HS \
363
+	   ...
364
+	   .udi_msc = UDI_MSC_DESC_HS, \
365
+	   ...
366
+	// USB Interface APIs
367
+	#define UDI_COMPOSITE_API \
368
+	   ...
369
+	   &udi_api_msc, \
370
+	   ... \endcode
371
+ *   - \note The descriptors order given in the four lists above must be the
372
+ *     same as the order defined by all interface indexes. The interface index
373
+ *     orders are defined through UDI_X_IFACE_NUMBER defines.
374
+ */
375
+
376
+#endif // _UDI_MSC_H_

+ 2073
- 0
Marlin/src/HAL/HAL_DUE/usb/uotghs_device_due.c
File diff suppressed because it is too large
View File


+ 663
- 0
Marlin/src/HAL/HAL_DUE/usb/uotghs_device_due.h View File

@@ -0,0 +1,663 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB Device Driver for UOTGHS. Compliant with common UDD driver.
5
+ *
6
+ * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef UOTGHS_DEVICE_DUE_H_INCLUDED
48
+#define UOTGHS_DEVICE_DUE_H_INCLUDED
49
+
50
+//#include "compiler.h"
51
+
52
+/// @cond 0
53
+/**INDENT-OFF**/
54
+#ifdef __cplusplus
55
+extern "C" {
56
+#endif
57
+/**INDENT-ON**/
58
+/// @endcond
59
+
60
+//! \ingroup udd_group
61
+//! \defgroup udd_udphs_group USB On-The-Go High-Speed Port for device mode (UOTGHS)
62
+//! UOTGHS low-level driver for USB device mode
63
+//!
64
+//! @{
65
+
66
+#ifndef UOTGHS_DEVEPTCFG_EPDIR_Pos
67
+// Bit pos is not defined in SAM header file but we need it.
68
+# define UOTGHS_DEVEPTCFG_EPDIR_Pos 8
69
+#endif
70
+
71
+//! @name UOTGHS Device IP properties
72
+//! These macros give access to IP properties
73
+//! @{
74
+  //! Get maximal number of endpoints
75
+#define udd_get_endpoint_max_nbr()             (9)
76
+#define UDD_MAX_PEP_NB                         (udd_get_endpoint_max_nbr() + 1)
77
+  //! Get maximal number of banks of endpoints
78
+#define udd_get_endpoint_bank_max_nbr(ep)      ((ep == 0) ? 1 : (( ep <= 2) ? 3 : 2))
79
+  //! Get maximal size of endpoint (3X, 1024/64)
80
+#define udd_get_endpoint_size_max(ep)          (((ep) == 0) ? 64 : 1024)
81
+  //! Get DMA support of endpoints
82
+#define Is_udd_endpoint_dma_supported(ep)      ((((ep) >= 1) && ((ep) <= 6)) ? true : false)
83
+  //! Get High Band Width support of endpoints
84
+#define Is_udd_endpoint_high_bw_supported(ep)  (((ep) >= 2) ? true : false)
85
+//! @}
86
+
87
+//! @name UOTGHS Device speeds management
88
+//! @{
89
+  //! Enable/disable device low-speed mode
90
+#define udd_low_speed_enable()               (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_LS))
91
+#define udd_low_speed_disable()              (Clr_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_LS))
92
+  //! Test if device low-speed mode is forced
93
+#define Is_udd_low_speed_enable()            (Tst_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_LS))
94
+
95
+#ifdef UOTGHS_DEVCTRL_SPDCONF_HIGH_SPEED
96
+  //! Enable high speed mode
97
+# define udd_high_speed_enable()          (Wr_bitfield(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_SPDCONF_Msk, 0))
98
+  //! Disable high speed mode
99
+# define udd_high_speed_disable()         (Wr_bitfield(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_SPDCONF_Msk, 3))
100
+  //! Test if controller is in full speed mode
101
+# define Is_udd_full_speed_mode()         (Rd_bitfield(UOTGHS->UOTGHS_SR, UOTGHS_SR_SPEED_Msk) == UOTGHS_SR_SPEED_FULL_SPEED)
102
+#else
103
+# define udd_high_speed_enable()          do { } while (0)
104
+# define udd_high_speed_disable()         do { } while (0)
105
+# define Is_udd_full_speed_mode()         true
106
+#endif
107
+//! @}
108
+
109
+//! @name UOTGHS Device HS test mode management
110
+//! @{
111
+#ifdef UOTGHS_DEVCTRL_SPDCONF_HIGH_SPEED
112
+  //! Enable high speed test mode
113
+# define udd_enable_hs_test_mode()        (Wr_bitfield(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_SPDCONF_Msk, 2))
114
+# define udd_enable_hs_test_mode_j()      (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_TSTJ))
115
+# define udd_enable_hs_test_mode_k()      (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_TSTK))
116
+# define udd_enable_hs_test_mode_packet() (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_TSTPCKT))
117
+#endif
118
+//! @}
119
+
120
+//! @name UOTGHS Device vbus management
121
+//! @{
122
+#define udd_enable_vbus_interrupt()       (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_VBUSTE))
123
+#define udd_disable_vbus_interrupt()      (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_VBUSTE))
124
+#define Is_udd_vbus_interrupt_enabled()   (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_VBUSTE))
125
+#define Is_udd_vbus_high()                (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_VBUS))
126
+#define Is_udd_vbus_low()                 (!Is_udd_vbus_high())
127
+#define udd_ack_vbus_transition()         (UOTGHS->UOTGHS_SCR = UOTGHS_SCR_VBUSTIC)
128
+#define udd_raise_vbus_transition()       (UOTGHS->UOTGHS_SFR = UOTGHS_SFR_VBUSTIS)
129
+#define Is_udd_vbus_transition()          (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_VBUSTI))
130
+//! @}
131
+
132
+
133
+//! @name UOTGHS device attach control
134
+//! These macros manage the UOTGHS Device attach.
135
+//! @{
136
+  //! Detaches from USB bus
137
+#define udd_detach_device()               (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_DETACH))
138
+  //! Attaches to USB bus
139
+#define udd_attach_device()               (Clr_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_DETACH))
140
+  //! Test if the device is detached
141
+#define Is_udd_detached()                 (Tst_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_DETACH))
142
+//! @}
143
+
144
+
145
+//! @name UOTGHS device bus events control
146
+//! These macros manage the UOTGHS Device bus events.
147
+//! @{
148
+
149
+//! Initiates a remote wake-up event
150
+//! @{
151
+#define udd_initiate_remote_wake_up()     (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_RMWKUP))
152
+#define Is_udd_pending_remote_wake_up()   (Tst_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_RMWKUP))
153
+//! @}
154
+
155
+//! Manage upstream resume event (=remote wakeup)
156
+//! The USB driver sends a resume signal called "Upstream Resume"
157
+//! @{
158
+#define udd_enable_remote_wake_up_interrupt()     (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_UPRSMES)
159
+#define udd_disable_remote_wake_up_interrupt()    (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_UPRSMEC)
160
+#define Is_udd_remote_wake_up_interrupt_enabled() (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_UPRSME))
161
+#define udd_ack_remote_wake_up_start()            (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_UPRSMC)
162
+#define udd_raise_remote_wake_up_start()          (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_UPRSMS)
163
+#define Is_udd_remote_wake_up_start()             (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_UPRSM))
164
+//! @}
165
+
166
+//! Manage downstream resume event (=remote wakeup from host)
167
+//! The USB controller detects a valid "End of Resume" signal initiated by the host
168
+//! @{
169
+#define udd_enable_resume_interrupt()             (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_EORSMES)
170
+#define udd_disable_resume_interrupt()            (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_EORSMEC)
171
+#define Is_udd_resume_interrupt_enabled()         (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_EORSME))
172
+#define udd_ack_resume()                          (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_EORSMC)
173
+#define udd_raise_resume()                        (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_EORSMS)
174
+#define Is_udd_resume()                           (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_EORSM))
175
+//! @}
176
+
177
+//! Manage wake-up event (=usb line activity)
178
+//! The USB controller is reactivated by a filtered non-idle signal from the lines
179
+//! @{
180
+#define udd_enable_wake_up_interrupt()            (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_WAKEUPES)
181
+#define udd_disable_wake_up_interrupt()           (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_WAKEUPEC)
182
+#define Is_udd_wake_up_interrupt_enabled()        (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_WAKEUPE))
183
+#define udd_ack_wake_up()                         (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_WAKEUPC)
184
+#define udd_raise_wake_up()                       (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_WAKEUPS)
185
+#define Is_udd_wake_up()                          (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_WAKEUP))
186
+//! @}
187
+
188
+//! Manage reset event
189
+//! Set when a USB "End of Reset" has been detected
190
+//! @{
191
+#define udd_enable_reset_interrupt()              (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_EORSTES)
192
+#define udd_disable_reset_interrupt()             (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_EORSTEC)
193
+#define Is_udd_reset_interrupt_enabled()          (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_EORSTE))
194
+#define udd_ack_reset()                           (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_EORSTC)
195
+#define udd_raise_reset()                         (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_EORSTS)
196
+#define Is_udd_reset()                            (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_EORST))
197
+//! @}
198
+
199
+//! Manage start of frame event
200
+//! @{
201
+#define udd_enable_sof_interrupt()                (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_SOFES)
202
+#define udd_disable_sof_interrupt()               (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_SOFEC)
203
+#define Is_udd_sof_interrupt_enabled()            (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_SOFE))
204
+#define udd_ack_sof()                             (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_SOFC)
205
+#define udd_raise_sof()                           (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_SOFS)
206
+#define Is_udd_sof()                              (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_SOF))
207
+#define udd_frame_number()                        (Rd_bitfield(UOTGHS->UOTGHS_DEVFNUM, UOTGHS_DEVFNUM_FNUM_Msk))
208
+#define Is_udd_frame_number_crc_error()           (Tst_bits(UOTGHS->UOTGHS_DEVFNUM, UOTGHS_DEVFNUM_FNCERR))
209
+//! @}
210
+
211
+//! Manage Micro start of frame event (High Speed Only)
212
+//! @{
213
+#define udd_enable_msof_interrupt()               (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_MSOFES)
214
+#define udd_disable_msof_interrupt()              (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_MSOFEC)
215
+#define Is_udd_msof_interrupt_enabled()           (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_MSOFE))
216
+#define udd_ack_msof()                            (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVIMR_MSOFE)
217
+#define udd_raise_msof()                          (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_MSOFS)
218
+#define Is_udd_msof()                             (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_MSOF))
219
+#define udd_micro_frame_number()                  \
220
+	(Rd_bitfield(UOTGHS->UOTGHS_DEVFNUM, (UOTGHS_DEVFNUM_FNUM_Msk|UOTGHS_DEVFNUM_MFNUM_Msk)))
221
+//! @}
222
+
223
+//! Manage suspend event
224
+//! @{
225
+#define udd_enable_suspend_interrupt()            (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_SUSPES)
226
+#define udd_disable_suspend_interrupt()           (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_SUSPEC)
227
+#define Is_udd_suspend_interrupt_enabled()        (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_SUSPE))
228
+#define udd_ack_suspend()                         (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_SUSPC)
229
+#define udd_raise_suspend()                       (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_SUSPS)
230
+#define Is_udd_suspend()                          (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_SUSP))
231
+//! @}
232
+
233
+//! @}
234
+
235
+//! @name UOTGHS device address control
236
+//! These macros manage the UOTGHS Device address.
237
+//! @{
238
+  //! enables USB device address
239
+#define udd_enable_address()                      (Set_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_ADDEN))
240
+  //! disables USB device address
241
+#define udd_disable_address()                     (Clr_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_ADDEN))
242
+#define Is_udd_address_enabled()                  (Tst_bits(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_ADDEN))
243
+  //! configures the USB device address
244
+#define udd_configure_address(addr)               (Wr_bitfield(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_UADD_Msk, addr))
245
+  //! gets the currently configured USB device address
246
+#define udd_get_configured_address()              (Rd_bitfield(UOTGHS->UOTGHS_DEVCTRL, UOTGHS_DEVCTRL_UADD_Msk))
247
+//! @}
248
+
249
+
250
+//! @name UOTGHS Device endpoint drivers
251
+//! These macros manage the common features of the endpoints.
252
+//! @{
253
+
254
+//! Generic macro for UOTGHS registers that can be arrayed
255
+//! @{
256
+#define UOTGHS_ARRAY(reg,index)                   ((&(UOTGHS->reg))[(index)])
257
+//! @}
258
+
259
+//! @name UOTGHS Device endpoint configuration
260
+//! @{
261
+  //! enables the selected endpoint
262
+#define udd_enable_endpoint(ep)                   (Set_bits(UOTGHS->UOTGHS_DEVEPT, UOTGHS_DEVEPT_EPEN0 << (ep)))
263
+  //! disables the selected endpoint
264
+#define udd_disable_endpoint(ep)                  (Clr_bits(UOTGHS->UOTGHS_DEVEPT, UOTGHS_DEVEPT_EPEN0 << (ep)))
265
+  //! tests if the selected endpoint is enabled
266
+#define Is_udd_endpoint_enabled(ep)               (Tst_bits(UOTGHS->UOTGHS_DEVEPT, UOTGHS_DEVEPT_EPEN0 << (ep)))
267
+  //! resets the selected endpoint
268
+#define udd_reset_endpoint(ep)                                         \
269
+	do {                                                               \
270
+		Set_bits(UOTGHS->UOTGHS_DEVEPT, UOTGHS_DEVEPT_EPRST0 << (ep)); \
271
+		Clr_bits(UOTGHS->UOTGHS_DEVEPT, UOTGHS_DEVEPT_EPRST0 << (ep)); \
272
+	} while (0)
273
+  //! Tests if the selected endpoint is being reset
274
+#define Is_udd_resetting_endpoint(ep)             (Tst_bits(UOTGHS->UOTGHS_DEVEPT, UOTGHS_DEVEPT_EPRST0 << (ep)))
275
+
276
+  //! Configures the selected endpoint type
277
+#define udd_configure_endpoint_type(ep, type)     (Wr_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPTYPE_Msk, type))
278
+  //! Gets the configured selected endpoint type
279
+#define udd_get_endpoint_type(ep)                 (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPTYPE_Msk))
280
+  //! Enables the bank autoswitch for the selected endpoint
281
+#define udd_enable_endpoint_bank_autoswitch(ep)   (Set_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_AUTOSW))
282
+  //! Disables the bank autoswitch for the selected endpoint
283
+#define udd_disable_endpoint_bank_autoswitch(ep)    (Clr_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_AUTOSW))
284
+#define Is_udd_endpoint_bank_autoswitch_enabled(ep) (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_AUTOSW))
285
+  //! Configures the selected endpoint direction
286
+#define udd_configure_endpoint_direction(ep, dir) (Wr_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPDIR, dir))
287
+  //! Gets the configured selected endpoint direction
288
+#define udd_get_endpoint_direction(ep)            (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPDIR))
289
+#define Is_udd_endpoint_in(ep)                    (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPDIR))
290
+  //! Bounds given integer size to allowed range and rounds it up to the nearest
291
+  //! available greater size, then applies register format of UOTGHS controller
292
+  //! for endpoint size bit-field.
293
+#define udd_format_endpoint_size(size)            (32 - clz(((uint32_t)min(max(size, 8), 1024) << 1) - 1) - 1 - 3)
294
+  //! Configures the selected endpoint size
295
+#define udd_configure_endpoint_size(ep, size)     (Wr_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPSIZE_Msk, udd_format_endpoint_size(size)))
296
+  //! Gets the configured selected endpoint size
297
+#define udd_get_endpoint_size(ep)                 (8 << Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPSIZE_Msk))
298
+  //! Configures the selected endpoint number of banks
299
+#define udd_configure_endpoint_bank(ep, bank)     (Wr_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPBK_Msk, bank))
300
+  //! Gets the configured selected endpoint number of banks
301
+#define udd_get_endpoint_bank(ep)                 (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPBK_Msk)+1)
302
+  //! Allocates the configuration selected endpoint in DPRAM memory
303
+#define udd_allocate_memory(ep)                   (Set_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_ALLOC))
304
+  //! un-allocates the configuration selected endpoint in DPRAM memory
305
+#define udd_unallocate_memory(ep)                 (Clr_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_ALLOC))
306
+#define Is_udd_memory_allocated(ep)               (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_ALLOC))
307
+
308
+  //! Configures selected endpoint in one step
309
+#define udd_configure_endpoint(ep, type, dir, size, bank) (\
310
+	Wr_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTCFG[0], ep), UOTGHS_DEVEPTCFG_EPTYPE_Msk |\
311
+			UOTGHS_DEVEPTCFG_EPDIR  |\
312
+			UOTGHS_DEVEPTCFG_EPSIZE_Msk |\
313
+			UOTGHS_DEVEPTCFG_EPBK_Msk ,   \
314
+			(((uint32_t)(type) << UOTGHS_DEVEPTCFG_EPTYPE_Pos) & UOTGHS_DEVEPTCFG_EPTYPE_Msk) |\
315
+			(((uint32_t)(dir ) << UOTGHS_DEVEPTCFG_EPDIR_Pos ) & UOTGHS_DEVEPTCFG_EPDIR) |\
316
+			( (uint32_t)udd_format_endpoint_size(size) << UOTGHS_DEVEPTCFG_EPSIZE_Pos) |\
317
+			(((uint32_t)(bank) << UOTGHS_DEVEPTCFG_EPBK_Pos) & UOTGHS_DEVEPTCFG_EPBK_Msk))\
318
+)
319
+  //! Tests if current endpoint is configured
320
+#define Is_udd_endpoint_configured(ep)            (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_CFGOK))
321
+  //! Returns the control direction
322
+#define udd_control_direction()                   (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], EP_CONTROL), UOTGHS_DEVEPTISR_CTRLDIR))
323
+
324
+  //! Resets the data toggle sequence
325
+#define udd_reset_data_toggle(ep)                 (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_RSTDTS)
326
+  //! Tests if the data toggle sequence is being reset
327
+#define Is_udd_data_toggle_reset(ep)              (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_RSTDT))
328
+  //! Returns data toggle
329
+#define udd_data_toggle(ep)                       (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_DTSEQ_Msk))
330
+//! @}
331
+
332
+
333
+//! @name UOTGHS Device control endpoint
334
+//! These macros control the endpoints.
335
+//! @{
336
+
337
+//! @name UOTGHS Device control endpoint interrupts
338
+//! These macros control the endpoints interrupts.
339
+//! @{
340
+  //! Enables the selected endpoint interrupt
341
+#define udd_enable_endpoint_interrupt(ep)         (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_PEP_0 << (ep))
342
+  //! Disables the selected endpoint interrupt
343
+#define udd_disable_endpoint_interrupt(ep)        (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_PEP_0 << (ep))
344
+  //! Tests if the selected endpoint interrupt is enabled
345
+#define Is_udd_endpoint_interrupt_enabled(ep)     (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_PEP_0 << (ep)))
346
+  //! Tests if an interrupt is triggered by the selected endpoint
347
+#define Is_udd_endpoint_interrupt(ep)             (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_PEP_0 << (ep)))
348
+  //! Returns the lowest endpoint number generating an endpoint interrupt or MAX_PEP_NB if none
349
+#define udd_get_interrupt_endpoint_number()       (ctz(((UOTGHS->UOTGHS_DEVISR >> UOTGHS_DEVISR_PEP_Pos) & \
350
+                                                   (UOTGHS->UOTGHS_DEVIMR >> UOTGHS_DEVIMR_PEP_Pos)) |     \
351
+                                                   (1 << MAX_PEP_NB)))
352
+#define UOTGHS_DEVISR_PEP_Pos   12
353
+#define UOTGHS_DEVIMR_PEP_Pos   12
354
+//! @}
355
+
356
+//! @name UOTGHS Device control endpoint errors
357
+//! These macros control the endpoint errors.
358
+//! @{
359
+  //! Enables the STALL handshake
360
+#define udd_enable_stall_handshake(ep)            (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_STALLRQS)
361
+  //! Disables the STALL handshake
362
+#define udd_disable_stall_handshake(ep)           (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_STALLRQC)
363
+  //! Tests if STALL handshake request is running
364
+#define Is_udd_endpoint_stall_requested(ep)       (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_STALLRQ))
365
+  //! Tests if STALL sent
366
+#define Is_udd_stall(ep)                          (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_STALLEDI))
367
+  //! ACKs STALL sent
368
+#define udd_ack_stall(ep)                         (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_STALLEDIC)
369
+  //! Raises STALL sent
370
+#define udd_raise_stall(ep)                       (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_STALLEDIS)
371
+  //! Enables STALL sent interrupt
372
+#define udd_enable_stall_interrupt(ep)            (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_STALLEDES)
373
+  //! Disables STALL sent interrupt
374
+#define udd_disable_stall_interrupt(ep)           (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_STALLEDEC)
375
+  //! Tests if STALL sent interrupt is enabled
376
+#define Is_udd_stall_interrupt_enabled(ep)        (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_STALLEDE))
377
+
378
+  //! Tests if NAK OUT received
379
+#define Is_udd_nak_out(ep)                        (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_NAKOUTI))
380
+  //! ACKs NAK OUT received
381
+#define udd_ack_nak_out(ep)                       (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_NAKOUTIC)
382
+  //! Raises NAK OUT received
383
+#define udd_raise_nak_out(ep)                     (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_NAKOUTIS)
384
+  //! Enables NAK OUT interrupt
385
+#define udd_enable_nak_out_interrupt(ep)          (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_NAKOUTES)
386
+  //! Disables NAK OUT interrupt
387
+#define udd_disable_nak_out_interrupt(ep)         (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_NAKOUTEC)
388
+  //! Tests if NAK OUT interrupt is enabled
389
+#define Is_udd_nak_out_interrupt_enabled(ep)      (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_NAKOUTE))
390
+
391
+  //! Tests if NAK IN received
392
+#define Is_udd_nak_in(ep)                         (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_NAKINI))
393
+  //! ACKs NAK IN received
394
+#define udd_ack_nak_in(ep)                        (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_NAKINIC)
395
+  //! Raises NAK IN received
396
+#define udd_raise_nak_in(ep)                      (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_NAKINIS)
397
+  //! Enables NAK IN interrupt
398
+#define udd_enable_nak_in_interrupt(ep)           (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_NAKINES)
399
+  //! Disables NAK IN interrupt
400
+#define udd_disable_nak_in_interrupt(ep)          (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_NAKINEC)
401
+  //! Tests if NAK IN interrupt is enabled
402
+#define Is_udd_nak_in_interrupt_enabled(ep)       (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_NAKINE))
403
+
404
+  //! ACKs endpoint isochronous overflow interrupt
405
+#define udd_ack_overflow_interrupt(ep)            (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_OVERFIC)
406
+  //! Raises endpoint isochronous overflow interrupt
407
+#define udd_raise_overflow_interrupt(ep)          (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_OVERFIS)
408
+  //! Tests if an overflow occurs
409
+#define Is_udd_overflow(ep)                       (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_OVERFI))
410
+  //! Enables overflow interrupt
411
+#define udd_enable_overflow_interrupt(ep)         (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_OVERFES)
412
+  //! Disables overflow interrupt
413
+#define udd_disable_overflow_interrupt(ep)        (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_OVERFEC)
414
+  //! Tests if overflow interrupt is enabled
415
+#define Is_udd_overflow_interrupt_enabled(ep)     (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_OVERFE))
416
+
417
+  //! ACKs endpoint isochronous underflow interrupt
418
+#define udd_ack_underflow_interrupt(ep)           (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_UNDERFIC)
419
+  //! Raises endpoint isochronous underflow interrupt
420
+#define udd_raise_underflow_interrupt(ep)         (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_UNDERFIS)
421
+  //! Tests if an underflow occurs
422
+#define Is_udd_underflow(ep)                      (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_UNDERFI))
423
+  //! Enables underflow interrupt
424
+#define udd_enable_underflow_interrupt(ep)        (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_UNDERFES)
425
+  //! Disables underflow interrupt
426
+#define udd_disable_underflow_interrupt(ep)       (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_UNDERFEC)
427
+  //! Tests if underflow interrupt is enabled
428
+#define Is_udd_underflow_interrupt_enabled(ep)    (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_UNDERFE))
429
+
430
+  //! Tests if CRC ERROR ISO OUT detected
431
+#define Is_udd_crc_error(ep)                      (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_CRCERRI))
432
+  //! ACKs CRC ERROR ISO OUT detected
433
+#define udd_ack_crc_error(ep)                     (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_CRCERRIC)
434
+  //! Raises CRC ERROR ISO OUT detected
435
+#define udd_raise_crc_error(ep)                   (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_CRCERRIS)
436
+  //! Enables CRC ERROR ISO OUT detected interrupt
437
+#define udd_enable_crc_error_interrupt(ep)        (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_CRCERRES)
438
+  //! Disables CRC ERROR ISO OUT detected interrupt
439
+#define udd_disable_crc_error_interrupt(ep)       (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_CRCERREC)
440
+  //! Tests if CRC ERROR ISO OUT detected interrupt is enabled
441
+#define Is_udd_crc_error_interrupt_enabled(ep)    (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_CRCERRE))
442
+//! @}
443
+
444
+//! @name UOTGHS Device control endpoint transfer
445
+//! These macros control the endpoint transfer.
446
+//! @{
447
+
448
+  //! Tests if endpoint read allowed
449
+#define Is_udd_read_enabled(ep)                   (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_RWALL))
450
+  //! Tests if endpoint write allowed
451
+#define Is_udd_write_enabled(ep)                  (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_RWALL))
452
+
453
+  //! Returns the byte count
454
+#define udd_byte_count(ep)                        (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_BYCT_Msk))
455
+  //! Clears FIFOCON bit
456
+#define udd_ack_fifocon(ep)                       (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_FIFOCONC)
457
+  //! Tests if FIFOCON bit set
458
+#define Is_udd_fifocon(ep)                        (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_FIFOCON))
459
+
460
+  //! Returns the number of busy banks
461
+#define udd_nb_busy_bank(ep)                      (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_NBUSYBK_Msk))
462
+  //! Returns the number of the current bank
463
+#define udd_current_bank(ep)                      (Rd_bitfield(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_CURRBK_Msk))
464
+  //! Kills last bank
465
+#define udd_kill_last_in_bank(ep)                 (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_KILLBKS)
466
+#define Is_udd_kill_last(ep)                      (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_KILLBK))
467
+  //! Tests if last bank killed
468
+#define Is_udd_last_in_bank_killed(ep)            (!Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_KILLBK))
469
+  //! Forces all banks full (OUT) or free (IN) interrupt
470
+#define udd_force_bank_interrupt(ep)              (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_NBUSYBKS)
471
+  //! Unforces all banks full (OUT) or free (IN) interrupt
472
+#define udd_unforce_bank_interrupt(ep)            (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_NBUSYBKS)
473
+  //! Enables all banks full (OUT) or free (IN) interrupt
474
+#define udd_enable_bank_interrupt(ep)             (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_NBUSYBKES)
475
+  //! Disables all banks full (OUT) or free (IN) interrupt
476
+#define udd_disable_bank_interrupt(ep)            (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_NBUSYBKEC)
477
+  //! Tests if all banks full (OUT) or free (IN) interrupt enabled
478
+#define Is_udd_bank_interrupt_enabled(ep)         (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_NBUSYBKE))
479
+
480
+  //! Tests if SHORT PACKET received
481
+#define Is_udd_short_packet(ep)                   (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_SHORTPACKET))
482
+  //! ACKs SHORT PACKET received
483
+#define udd_ack_short_packet(ep)                  (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_SHORTPACKETC)
484
+  //! Raises SHORT PACKET received
485
+#define udd_raise_short_packet(ep)                (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_SHORTPACKETS)
486
+  //! Enables SHORT PACKET received interrupt
487
+#define udd_enable_short_packet_interrupt(ep)     (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_SHORTPACKETES)
488
+  //! Disables SHORT PACKET received interrupt
489
+#define udd_disable_short_packet_interrupt(ep)    (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_SHORTPACKETEC)
490
+  //! Tests if SHORT PACKET received interrupt is enabled
491
+#define Is_udd_short_packet_interrupt_enabled(ep) (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_SHORTPACKETE))
492
+
493
+  //! Tests if SETUP received
494
+#define Is_udd_setup_received(ep)                    (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_RXSTPI))
495
+  //! ACKs SETUP received
496
+#define udd_ack_setup_received(ep)                   (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_RXSTPIC)
497
+  //! Raises SETUP received
498
+#define udd_raise_setup_received(ep)                 (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_RXSTPIS)
499
+  //! Enables SETUP received interrupt
500
+#define udd_enable_setup_received_interrupt(ep)      (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_RXSTPES)
501
+  //! Disables SETUP received interrupt
502
+#define udd_disable_setup_received_interrupt(ep)     (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_RXSTPEC)
503
+  //! Tests if SETUP received interrupt is enabled
504
+#define Is_udd_setup_received_interrupt_enabled(ep)  (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_RXSTPE))
505
+
506
+  //! Tests if OUT received
507
+#define Is_udd_out_received(ep)                   (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_RXOUTI))
508
+  //! ACKs OUT received
509
+#define udd_ack_out_received(ep)                  (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_RXOUTIC)
510
+  //! Raises OUT received
511
+#define udd_raise_out_received(ep)                (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_RXOUTIS)
512
+  //! Enables OUT received interrupt
513
+#define udd_enable_out_received_interrupt(ep)     (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_RXOUTES)
514
+  //! Disables OUT received interrupt
515
+#define udd_disable_out_received_interrupt(ep)    (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_RXOUTEC)
516
+  //! Tests if OUT received interrupt is enabled
517
+#define Is_udd_out_received_interrupt_enabled(ep) (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_RXOUTE))
518
+
519
+  //! Tests if IN sending
520
+#define Is_udd_in_send(ep)                        (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], ep), UOTGHS_DEVEPTISR_TXINI))
521
+  //! ACKs IN sending
522
+#define udd_ack_in_send(ep)                       (UOTGHS_ARRAY(UOTGHS_DEVEPTICR[0], ep) = UOTGHS_DEVEPTICR_TXINIC)
523
+  //! Raises IN sending
524
+#define udd_raise_in_send(ep)                     (UOTGHS_ARRAY(UOTGHS_DEVEPTIFR[0], ep) = UOTGHS_DEVEPTIFR_TXINIS)
525
+  //! Enables IN sending interrupt
526
+#define udd_enable_in_send_interrupt(ep)          (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0], ep) = UOTGHS_DEVEPTIER_TXINES)
527
+  //! Disables IN sending interrupt
528
+#define udd_disable_in_send_interrupt(ep)         (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0], ep) = UOTGHS_DEVEPTIDR_TXINEC)
529
+  //! Tests if IN sending interrupt is enabled
530
+#define Is_udd_in_send_interrupt_enabled(ep)      (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0], ep), UOTGHS_DEVEPTIMR_TXINE))
531
+
532
+
533
+  //! Get 64-, 32-, 16- or 8-bit access to FIFO data register of selected endpoint.
534
+  //! @param ep Endpoint of which to access FIFO data register
535
+  //! @param scale Data scale in bits: 64, 32, 16 or 8
536
+  //! @return Volatile 64-, 32-, 16- or 8-bit data pointer to FIFO data register
537
+  //! @warning It is up to the user of this macro to make sure that all accesses
538
+  //! are aligned with their natural boundaries except 64-bit accesses which
539
+  //! require only 32-bit alignment.
540
+  //! @warning It is up to the user of this macro to make sure that used HSB
541
+  //! addresses are identical to the DPRAM internal pointer modulo 32 bits.
542
+#define udd_get_endpoint_fifo_access(ep, scale) \
543
+		(((volatile TPASTE2(U, scale) (*)[0x8000 / ((scale) / 8)])UOTGHS_RAM_ADDR)[(ep)])
544
+
545
+//! @name UOTGHS endpoint DMA drivers
546
+//! These macros manage the common features of the endpoint DMA channels.
547
+//! @{
548
+
549
+  //! Maximum transfer size on USB DMA
550
+#define UDD_ENDPOINT_MAX_TRANS 0x10000
551
+  //! Enables the disabling of HDMA requests by endpoint interrupts
552
+#define udd_enable_endpoint_int_dis_hdma_req(ep)     (UOTGHS_ARRAY(UOTGHS_DEVEPTIER[0](ep) = UOTGHS_DEVEPTIER_EPDISHDMAS)
553
+  //! Disables the disabling of HDMA requests by endpoint interrupts
554
+#define udd_disable_endpoint_int_dis_hdma_req(ep)    (UOTGHS_ARRAY(UOTGHS_DEVEPTIDR[0](ep) = UOTGHS_DEVEPTIDR_EPDISHDMAC)
555
+  //! Tests if the disabling of HDMA requests by endpoint interrupts is enabled
556
+#define Is_udd_endpoint_int_dis_hdma_req_enabled(ep) (Tst_bits(UOTGHS_ARRAY(UOTGHS_DEVEPTIMR[0](ep), UOTGHS_DEVEPTIMR_EPDISHDMA))
557
+
558
+  //! Raises the selected endpoint DMA channel interrupt
559
+#define udd_raise_endpoint_dma_interrupt(ep)         (UOTGHS->UOTGHS_DEVIFR = UOTGHS_DEVIFR_DMA_1 << ((ep) - 1))
560
+  //! Raises the selected endpoint DMA channel interrupt
561
+#define udd_clear_endpoint_dma_interrupt(ep)         (UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVISR_DMA_1 << ((ep) - 1))
562
+  //! Tests if an interrupt is triggered by the selected endpoint DMA channel
563
+#define Is_udd_endpoint_dma_interrupt(ep)            (Tst_bits(UOTGHS->UOTGHS_DEVISR, UOTGHS_DEVISR_DMA_1 << ((ep) - 1)))
564
+  //! Enables the selected endpoint DMA channel interrupt
565
+#define udd_enable_endpoint_dma_interrupt(ep)        (UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_DMA_1 << ((ep) - 1))
566
+  //! Disables the selected endpoint DMA channel interrupt
567
+#define udd_disable_endpoint_dma_interrupt(ep)       (UOTGHS->UOTGHS_DEVIDR = UOTGHS_DEVIDR_DMA_1 << ((ep) - 1))
568
+  //! Tests if the selected endpoint DMA channel interrupt is enabled
569
+#define Is_udd_endpoint_dma_interrupt_enabled(ep)    (Tst_bits(UOTGHS->UOTGHS_DEVIMR, UOTGHS_DEVIMR_DMA_1 << ((ep) - 1)))
570
+
571
+  //! Access points to the UOTGHS device DMA memory map with arrayed registers
572
+  //! @{
573
+      //! Structure for DMA next descriptor register
574
+typedef struct {
575
+	uint32_t *NXT_DSC_ADD;
576
+} uotghs_dma_nextdesc_t;
577
+      //! Structure for DMA control register
578
+typedef struct {
579
+	uint32_t CHANN_ENB:1,
580
+		LDNXT_DSC:1,
581
+		END_TR_EN:1,
582
+		END_B_EN:1,
583
+		END_TR_IT:1,
584
+		END_BUFFIT:1,
585
+		DESC_LD_IT:1,
586
+		BUST_LCK:1,
587
+		reserved:8,
588
+		BUFF_LENGTH:16;
589
+} uotghs_dma_control_t;
590
+      //! Structure for DMA status register
591
+typedef struct {
592
+	uint32_t CHANN_ENB:1,
593
+		CHANN_ACT:1,
594
+		reserved0:2,
595
+		END_TR_ST:1,
596
+		END_BF_ST:1,
597
+		DESC_LDST:1,
598
+		reserved1:9,
599
+		BUFF_COUNT:16;
600
+} uotghs_dma_status_t;
601
+      //! Structure for DMA descriptor
602
+typedef struct {
603
+	union {
604
+		uint32_t nextdesc;
605
+		uotghs_dma_nextdesc_t NEXTDESC;
606
+	};
607
+	uint32_t addr;
608
+	union {
609
+		uint32_t control;
610
+		uotghs_dma_control_t CONTROL;
611
+	};
612
+	uint32_t reserved;
613
+} sam_uotghs_dmadesc_t, uotghs_dmadesc_t;
614
+      //! Structure for DMA registers in a channel
615
+typedef struct {
616
+	union {
617
+		uint32_t nextdesc;
618
+		uotghs_dma_nextdesc_t NEXTDESC;
619
+	};
620
+	uint32_t addr;
621
+	union {
622
+		uint32_t control;
623
+		uotghs_dma_control_t CONTROL;
624
+	};
625
+	union {
626
+		unsigned long status;
627
+		uotghs_dma_status_t STATUS;
628
+	};
629
+} sam_uotghs_dmach_t, uotghs_dmach_t;
630
+      //! DMA channel control command
631
+#define UDD_ENDPOINT_DMA_STOP_NOW                 (0)
632
+#define UDD_ENDPOINT_DMA_RUN_AND_STOP             (UOTGHS_DEVDMACONTROL_CHANN_ENB)
633
+#define UDD_ENDPOINT_DMA_LOAD_NEXT_DESC           (UOTGHS_DEVDMACONTROL_LDNXT_DSC)
634
+#define UDD_ENDPOINT_DMA_RUN_AND_LINK             (UOTGHS_DEVDMACONTROL_CHANN_ENB|UOTGHS_DEVDMACONTROL_LDNXT_DSC)
635
+      //! Structure for DMA registers
636
+#define UOTGHS_UDDMA_ARRAY(ep)                    (((volatile uotghs_dmach_t *)UOTGHS->UOTGHS_DEVDMA)[(ep) - 1])
637
+
638
+      //! Set control desc to selected endpoint DMA channel
639
+#define udd_endpoint_dma_set_control(ep,desc)     (UOTGHS_UDDMA_ARRAY(ep).control = desc)
640
+      //! Get control desc to selected endpoint DMA channel
641
+#define udd_endpoint_dma_get_control(ep)          (UOTGHS_UDDMA_ARRAY(ep).control)
642
+      //! Set RAM address to selected endpoint DMA channel
643
+#define udd_endpoint_dma_set_addr(ep,add)         (UOTGHS_UDDMA_ARRAY(ep).addr = add)
644
+      //! Get status to selected endpoint DMA channel
645
+#define udd_endpoint_dma_get_status(ep)           (UOTGHS_UDDMA_ARRAY(ep).status)
646
+   //! @}
647
+//! @}
648
+
649
+//! @}
650
+//! @}
651
+//! @}
652
+//! @}
653
+
654
+
655
+/// @cond 0
656
+/**INDENT-OFF**/
657
+#ifdef __cplusplus
658
+}
659
+#endif
660
+/**INDENT-ON**/
661
+/// @endcond
662
+
663
+#endif /* UOTGHS_DEVICE_H_INCLUDED */

+ 242
- 0
Marlin/src/HAL/HAL_DUE/usb/uotghs_otg.h View File

@@ -0,0 +1,242 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB OTG Driver for UOTGHS.
5
+ *
6
+ * Copyright (c) 2012-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef UOTGHS_OTG_H_INCLUDED
48
+#define UOTGHS_OTG_H_INCLUDED
49
+
50
+#include "compiler.h"
51
+
52
+#ifdef __cplusplus
53
+extern "C" {
54
+#endif
55
+
56
+
57
+//! \ingroup usb_group
58
+//! \defgroup otg_group UOTGHS OTG Driver
59
+//! UOTGHS low-level driver for OTG features
60
+//!
61
+//! @{
62
+
63
+/**
64
+ * \brief Initialize the dual role
65
+ * This function is implemented in uotghs_host.c file.
66
+ *
67
+ * \return \c true if the ID pin management has been started, otherwise \c false.
68
+ */
69
+bool otg_dual_enable(void);
70
+
71
+/**
72
+ * \brief Uninitialize the dual role
73
+ * This function is implemented in uotghs_host.c file.
74
+ */
75
+void otg_dual_disable(void);
76
+
77
+
78
+//! @name UOTGHS OTG ID pin management
79
+//! The ID pin come from the USB OTG connector (A and B receptable) and
80
+//! allows to select the USB mode host or device.
81
+//! The UOTGHS hardware can manage it automatically. This feature is optional.
82
+//! When USB_ID_GPIO is defined (in board.h), this feature is enabled.
83
+//!
84
+//! @{
85
+   //! Enable external OTG_ID pin (listened to by USB)
86
+#define otg_enable_id_pin()                 (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIDE))
87
+   //! Disable external OTG_ID pin (ignored by USB)
88
+#define otg_disable_id_pin()                (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIDE))
89
+   //! Test if external OTG_ID pin enabled (listened to by USB)
90
+#define Is_otg_id_pin_enabled()             (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIDE))
91
+   //! Disable external OTG_ID pin and force device mode
92
+#define otg_force_device_mode()             (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIMOD), otg_disable_id_pin())
93
+   //! Test if device mode is forced
94
+#define Is_otg_device_mode_forced()         (!Is_otg_id_pin_enabled() && Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIMOD))
95
+   //! Disable external OTG_ID pin and force host mode
96
+#define otg_force_host_mode()               (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIMOD), otg_disable_id_pin())
97
+   //! Test if host mode is forced
98
+#define Is_otg_host_mode_forced()           (!Is_otg_id_pin_enabled() && !Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UIMOD))
99
+
100
+//! @name UOTGHS OTG ID pin interrupt management
101
+//! These macros manage the ID pin interrupt
102
+//! @{
103
+#define otg_enable_id_interrupt()           (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_IDTE))
104
+#define otg_disable_id_interrupt()          (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_IDTE))
105
+#define Is_otg_id_interrupt_enabled()       (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_IDTE))
106
+#define Is_otg_id_device()                  (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_ID))
107
+#define Is_otg_id_host()                    (!Is_otg_id_device())
108
+#define otg_ack_id_transition()             (UOTGHS->UOTGHS_SCR = UOTGHS_SCR_IDTIC)
109
+#define otg_raise_id_transition()           (UOTGHS->UOTGHS_SFR = UOTGHS_SFR_IDTIS)
110
+#define Is_otg_id_transition()              (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_IDTI))
111
+//! @}
112
+//! @}
113
+
114
+//! @name OTG Vbus management
115
+//! @{
116
+#define otg_enable_vbus_interrupt()         (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_VBUSTE))
117
+#define otg_disable_vbus_interrupt()        (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_VBUSTE))
118
+#define Is_otg_vbus_interrupt_enabled()     (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_VBUSTE))
119
+#define Is_otg_vbus_high()                  (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_VBUS))
120
+#define Is_otg_vbus_low()                   (!Is_otg_vbus_high())
121
+#define otg_ack_vbus_transition()           (UOTGHS->UOTGHS_SCR = UOTGHS_SCR_VBUSTIC)
122
+#define otg_raise_vbus_transition()         (UOTGHS->UOTGHS_SFR = UOTGHS_SFR_VBUSTIS)
123
+#define Is_otg_vbus_transition()            (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_VBUSTI))
124
+//! @}
125
+
126
+//! @name UOTGHS OTG main management
127
+//! These macros allows to enable/disable pad and UOTGHS hardware
128
+//! @{
129
+  //! Reset USB macro
130
+#define otg_reset()                         \
131
+	do {                                    \
132
+		UOTGHS->UOTGHS_CTRL = 0;            \
133
+		while( UOTGHS->UOTGHS_SR & 0x3FFF) {\
134
+			UOTGHS->UOTGHS_SCR = 0xFFFFFFFF;\
135
+		}                                   \
136
+	} while (0)
137
+  //! Enable USB macro
138
+#define otg_enable()                        (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_USBE))
139
+  //! Disable USB macro                     
140
+#define otg_disable()                       (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_USBE))
141
+#define Is_otg_enabled()                    (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_USBE))
142
+
143
+  //! Enable OTG pad                        
144
+#define otg_enable_pad()                    (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_OTGPADE))
145
+  //! Disable OTG pad                       
146
+#define otg_disable_pad()                   (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_OTGPADE))
147
+#define Is_otg_pad_enabled()                (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_OTGPADE))
148
+
149
+  //! Check Clock Usable               
150
+  //! For parts with HS feature, this one corresponding at UTMI clock
151
+#define Is_otg_clock_usable()               (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_CLKUSABLE))
152
+
153
+  //! Stop (freeze) internal USB clock
154
+#define otg_freeze_clock()                  (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_FRZCLK))
155
+#define otg_unfreeze_clock()                (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_FRZCLK))
156
+#define Is_otg_clock_frozen()               (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_FRZCLK))
157
+
158
+  //! Configure time-out of specified OTG timer
159
+#define otg_configure_timeout(timer, timeout) (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UNLOCK),\
160
+		Wr_bitfield(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_TIMPAGE_Msk, timer),\
161
+		Wr_bitfield(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_TIMVALUE_Msk, timeout),\
162
+		Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UNLOCK))
163
+  //! Get configured time-out of specified OTG timer
164
+#define otg_get_timeout(timer)              (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UNLOCK),\
165
+		Wr_bitfield(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_TIMPAGE_Msk, timer),\
166
+		Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_UNLOCK),\
167
+		Rd_bitfield(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_TIMVALUE_Msk))
168
+
169
+
170
+  //! Get the dual-role device state of the internal USB finite state machine of the UOTGHS controller
171
+#define otg_get_fsm_drd_state()             (Rd_bitfield(UOTGHS->UOTGHS_FSM, UOTGHS_FSM_DRDSTATE_Msk))
172
+#define Is_otg_a_suspend()                  (4==otg_get_fsm_drd_state())
173
+#define Is_otg_a_wait_vrise()               (1==otg_get_fsm_drd_state())
174
+//! @}
175
+
176
+//! @name UOTGHS OTG hardware protocol
177
+//! These macros manages the hardware OTG protocol
178
+//! @{
179
+  //! Initiates a Host negotiation Protocol
180
+#define otg_device_initiate_hnp()             (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPREQ))
181
+  //! Accepts a Host negotiation Protocol
182
+#define otg_host_accept_hnp()                 (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPREQ))
183
+  //! Rejects a Host negotiation Protocol
184
+#define otg_host_reject_hnp()                 (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPREQ))
185
+  //! initiates a Session Request Protocol
186
+#define otg_device_initiate_srp()             (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPREQ))
187
+  //! Selects VBus as SRP method
188
+#define otg_select_vbus_srp_method()          (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPSEL))
189
+#define Is_otg_vbus_srp_method_selected()     (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPSEL))
190
+  //! Selects data line as SRP method
191
+#define otg_select_data_srp_method()          (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPSEL))
192
+#define Is_otg_data_srp_method_selected()     (!Is_otg_vbus_srp_method_selected())
193
+  //! Tests if a HNP occurs
194
+#define Is_otg_hnp()                          (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPREQ))
195
+  //! Tests if a SRP from device occurs
196
+#define Is_otg_device_srp()                   (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPREQ))
197
+
198
+  //! Enables HNP error interrupt
199
+#define otg_enable_hnp_error_interrupt()      (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPERRE))
200
+  //! Disables HNP error interrupt
201
+#define otg_disable_hnp_error_interrupt()     (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPERRE))
202
+#define Is_otg_hnp_error_interrupt_enabled()  (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_HNPERRE))
203
+  //! ACKs HNP error interrupt
204
+#define otg_ack_hnp_error_interrupt()         (UOTGHS->UOTGHS_SCR = UOTGHS_SCR_HNPERRIC)
205
+  //! Raises HNP error interrupt
206
+#define otg_raise_hnp_error_interrupt()       (UOTGHS->UOTGHS_SFR = UOTGHS_SFR_HNPERRIS)
207
+  //! Tests if a HNP error occurs
208
+#define Is_otg_hnp_error_interrupt()          (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_HNPERRI))
209
+
210
+  //! Enables role exchange interrupt
211
+#define otg_enable_role_exchange_interrupt()      (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_ROLEEXE))
212
+  //! Disables role exchange interrupt
213
+#define otg_disable_role_exchange_interrupt()     (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_ROLEEXE))
214
+#define Is_otg_role_exchange_interrupt_enabled()  (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_ROLEEXE))
215
+  //! ACKs role exchange interrupt
216
+#define otg_ack_role_exchange_interrupt()         (UOTGHS->UOTGHS_SCR = UOTGHS_SCR_ROLEEXIC)
217
+  //! Raises role exchange interrupt
218
+#define otg_raise_role_exchange_interrupt()       (UOTGHS->UOTGHS_SFR = UOTGHS_SFR_ROLEEXIS)
219
+  //! Tests if a role exchange occurs
220
+#define Is_otg_role_exchange_interrupt()          (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_ROLEEXI))
221
+
222
+  //! Enables SRP interrupt
223
+#define otg_enable_srp_interrupt()          (Set_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPE))
224
+  //! Disables SRP interrupt
225
+#define otg_disable_srp_interrupt()         (Clr_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPE))
226
+#define Is_otg_srp_interrupt_enabled()      (Tst_bits(UOTGHS->UOTGHS_CTRL, UOTGHS_CTRL_SRPE))
227
+  //! ACKs SRP interrupt
228
+#define otg_ack_srp_interrupt()             (UOTGHS->UOTGHS_SCR = UOTGHS_SCR_SRPIC)
229
+  //! Raises SRP interrupt
230
+#define otg_raise_srp_interrupt()           (UOTGHS->UOTGHS_SFR = UOTGHS_SFR_SRPIS)
231
+  //! Tests if a SRP occurs
232
+#define Is_otg_srp_interrupt()              (Tst_bits(UOTGHS->UOTGHS_SR, UOTGHS_SR_SRPI))
233
+//! @}
234
+
235
+//! @}
236
+
237
+#ifdef __cplusplus
238
+}
239
+#endif
240
+
241
+
242
+#endif /* UOTGHS_OTG_H_INCLUDED */

+ 496
- 0
Marlin/src/HAL/HAL_DUE/usb/usb_protocol.h View File

@@ -0,0 +1,496 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB protocol definitions.
5
+ *
6
+ * This file contains the USB definitions and data structures provided by the
7
+ * USB 2.0 specification.
8
+ *
9
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
10
+ *
11
+ * \asf_license_start
12
+ *
13
+ * \page License
14
+ *
15
+ * Redistribution and use in source and binary forms, with or without
16
+ * modification, are permitted provided that the following conditions are met:
17
+ *
18
+ * 1. Redistributions of source code must retain the above copyright notice,
19
+ *    this list of conditions and the following disclaimer.
20
+ *
21
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
22
+ *    this list of conditions and the following disclaimer in the documentation
23
+ *    and/or other materials provided with the distribution.
24
+ *
25
+ * 3. The name of Atmel may not be used to endorse or promote products derived
26
+ *    from this software without specific prior written permission.
27
+ *
28
+ * 4. This software may only be redistributed and used in connection with an
29
+ *    Atmel microcontroller product.
30
+ *
31
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
32
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
34
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
35
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41
+ * POSSIBILITY OF SUCH DAMAGE.
42
+ *
43
+ * \asf_license_stop
44
+ *
45
+ */
46
+/*
47
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
48
+ */
49
+
50
+#ifndef _USB_PROTOCOL_H_
51
+#define _USB_PROTOCOL_H_
52
+
53
+/**
54
+ * \ingroup usb_group
55
+ * \defgroup usb_protocol_group USB Protocol Definitions
56
+ *
57
+ * This module defines constants and data structures provided by the USB
58
+ * 2.0 specification.
59
+ *
60
+ * @{
61
+ */
62
+
63
+//! Value for field bcdUSB
64
+#define  USB_V2_0    0x0200 //!< USB Specification version 2.00
65
+#define  USB_V2_1    0x0201 //!< USB Specification version 2.01
66
+
67
+/*! \name Generic definitions (Class, subclass and protocol)
68
+ */
69
+//! @{
70
+#define  NO_CLASS                0x00
71
+#define  CLASS_VENDOR_SPECIFIC   0xFF
72
+#define  NO_SUBCLASS             0x00
73
+#define  NO_PROTOCOL             0x00
74
+//! @}
75
+
76
+//! \name IAD (Interface Association Descriptor) constants
77
+//! @{
78
+#define  CLASS_IAD               0xEF
79
+#define  SUB_CLASS_IAD           0x02
80
+#define  PROTOCOL_IAD            0x01
81
+//! @}
82
+
83
+/**
84
+ * \brief USB request data transfer direction (bmRequestType)
85
+ */
86
+#define  USB_REQ_DIR_OUT         (0<<7) //!< Host to device
87
+#define  USB_REQ_DIR_IN          (1<<7) //!< Device to host
88
+#define  USB_REQ_DIR_MASK        (1<<7) //!< Mask
89
+
90
+/**
91
+ * \brief USB request types (bmRequestType)
92
+ */
93
+#define  USB_REQ_TYPE_STANDARD   (0<<5) //!< Standard request
94
+#define  USB_REQ_TYPE_CLASS      (1<<5) //!< Class-specific request
95
+#define  USB_REQ_TYPE_VENDOR     (2<<5) //!< Vendor-specific request
96
+#define  USB_REQ_TYPE_MASK       (3<<5) //!< Mask
97
+
98
+/**
99
+ * \brief USB recipient codes (bmRequestType)
100
+ */
101
+#define  USB_REQ_RECIP_DEVICE    (0<<0) //!< Recipient device
102
+#define  USB_REQ_RECIP_INTERFACE (1<<0) //!< Recipient interface
103
+#define  USB_REQ_RECIP_ENDPOINT  (2<<0) //!< Recipient endpoint
104
+#define  USB_REQ_RECIP_OTHER     (3<<0) //!< Recipient other
105
+#define  USB_REQ_RECIP_MASK      (0x1F) //!< Mask
106
+
107
+/**
108
+ * \brief Standard USB requests (bRequest)
109
+ */
110
+enum usb_reqid {
111
+	USB_REQ_GET_STATUS = 0,
112
+	USB_REQ_CLEAR_FEATURE = 1,
113
+	USB_REQ_SET_FEATURE = 3,
114
+	USB_REQ_SET_ADDRESS = 5,
115
+	USB_REQ_GET_DESCRIPTOR = 6,
116
+	USB_REQ_SET_DESCRIPTOR = 7,
117
+	USB_REQ_GET_CONFIGURATION = 8,
118
+	USB_REQ_SET_CONFIGURATION = 9,
119
+	USB_REQ_GET_INTERFACE = 10,
120
+	USB_REQ_SET_INTERFACE = 11,
121
+	USB_REQ_SYNCH_FRAME = 12,
122
+};
123
+
124
+/**
125
+ * \brief Standard USB device status flags
126
+ *
127
+ */
128
+enum usb_device_status {
129
+	USB_DEV_STATUS_BUS_POWERED = 0,
130
+	USB_DEV_STATUS_SELF_POWERED = 1,
131
+	USB_DEV_STATUS_REMOTEWAKEUP = 2
132
+};
133
+
134
+/**
135
+ * \brief Standard USB Interface status flags
136
+ *
137
+ */
138
+enum usb_interface_status {
139
+	USB_IFACE_STATUS_RESERVED = 0
140
+};
141
+
142
+/**
143
+ * \brief Standard USB endpoint status flags
144
+ *
145
+ */
146
+enum usb_endpoint_status {
147
+	USB_EP_STATUS_HALTED = 1,
148
+};
149
+
150
+/**
151
+ * \brief Standard USB device feature flags
152
+ *
153
+ * \note valid for SetFeature request.
154
+ */
155
+enum usb_device_feature {
156
+	USB_DEV_FEATURE_REMOTE_WAKEUP = 1, //!< Remote wakeup enabled
157
+	USB_DEV_FEATURE_TEST_MODE = 2,     //!< USB test mode
158
+	USB_DEV_FEATURE_OTG_B_HNP_ENABLE = 3,
159
+	USB_DEV_FEATURE_OTG_A_HNP_SUPPORT = 4,
160
+	USB_DEV_FEATURE_OTG_A_ALT_HNP_SUPPORT = 5
161
+};
162
+
163
+/**
164
+ * \brief Test Mode possible on HS USB device
165
+ *
166
+ * \note valid for USB_DEV_FEATURE_TEST_MODE request.
167
+ */
168
+enum usb_device_hs_test_mode {
169
+	USB_DEV_TEST_MODE_J = 1,
170
+	USB_DEV_TEST_MODE_K = 2,
171
+	USB_DEV_TEST_MODE_SE0_NAK = 3,
172
+	USB_DEV_TEST_MODE_PACKET = 4,
173
+	USB_DEV_TEST_MODE_FORCE_ENABLE = 5,
174
+};
175
+
176
+/**
177
+ * \brief Standard USB endpoint feature/status flags
178
+ */
179
+enum usb_endpoint_feature {
180
+	USB_EP_FEATURE_HALT = 0,
181
+};
182
+
183
+/**
184
+ * \brief Standard USB Test Mode Selectors
185
+ */
186
+enum usb_test_mode_selector {
187
+	USB_TEST_J = 0x01,
188
+	USB_TEST_K = 0x02,
189
+	USB_TEST_SE0_NAK = 0x03,
190
+	USB_TEST_PACKET = 0x04,
191
+	USB_TEST_FORCE_ENABLE = 0x05,
192
+};
193
+
194
+/**
195
+ * \brief Standard USB descriptor types
196
+ */
197
+enum usb_descriptor_type {
198
+	USB_DT_DEVICE = 1,
199
+	USB_DT_CONFIGURATION = 2,
200
+	USB_DT_STRING = 3,
201
+	USB_DT_INTERFACE = 4,
202
+	USB_DT_ENDPOINT = 5,
203
+	USB_DT_DEVICE_QUALIFIER = 6,
204
+	USB_DT_OTHER_SPEED_CONFIGURATION = 7,
205
+	USB_DT_INTERFACE_POWER = 8,
206
+	USB_DT_OTG = 9,
207
+	USB_DT_IAD = 0x0B,
208
+	USB_DT_BOS = 0x0F,
209
+	USB_DT_DEVICE_CAPABILITY = 0x10,
210
+};
211
+
212
+/**
213
+ * \brief USB Device Capability types
214
+ */
215
+enum usb_capability_type {
216
+	USB_DC_USB20_EXTENSION = 0x02,
217
+};
218
+
219
+/**
220
+ * \brief USB Device Capability - USB 2.0 Extension
221
+ * To fill bmAttributes field of usb_capa_ext_desc_t structure.
222
+ */
223
+enum usb_capability_extension_attr {
224
+	USB_DC_EXT_LPM  = 0x00000002,
225
+};
226
+
227
+#define HIRD_50_US    0
228
+#define HIRD_125_US   1
229
+#define HIRD_200_US   2
230
+#define HIRD_275_US   3
231
+#define HIRD_350_US   4
232
+#define HIRD_425_US   5
233
+#define HIRD_500_US   6
234
+#define HIRD_575_US  7
235
+#define HIRD_650_US  8
236
+#define HIRD_725_US  9
237
+#define HIRD_800_US  10
238
+#define HIRD_875_US  11
239
+#define HIRD_950_US  12
240
+#define HIRD_1025_US  13
241
+#define HIRD_1100_US  14
242
+#define HIRD_1175_US  15
243
+
244
+/** Fields definition from a LPM TOKEN  */
245
+#define  USB_LPM_ATTRIBUT_BLINKSTATE_MASK      (0xF << 0)
246
+#define  USB_LPM_ATTRIBUT_FIRD_MASK            (0xF << 4)
247
+#define  USB_LPM_ATTRIBUT_REMOTEWAKE_MASK      (1 << 8)
248
+#define  USB_LPM_ATTRIBUT_BLINKSTATE(value)    ((value & 0xF) << 0)
249
+#define  USB_LPM_ATTRIBUT_FIRD(value)          ((value & 0xF) << 4)
250
+#define  USB_LPM_ATTRIBUT_REMOTEWAKE(value)    ((value & 1) << 8)
251
+#define  USB_LPM_ATTRIBUT_BLINKSTATE_L1        USB_LPM_ATTRIBUT_BLINKSTATE(1)
252
+
253
+/**
254
+ * \brief Standard USB endpoint transfer types
255
+ */
256
+enum usb_ep_type {
257
+	USB_EP_TYPE_CONTROL = 0x00,
258
+	USB_EP_TYPE_ISOCHRONOUS = 0x01,
259
+	USB_EP_TYPE_BULK = 0x02,
260
+	USB_EP_TYPE_INTERRUPT = 0x03,
261
+	USB_EP_TYPE_MASK = 0x03,
262
+};
263
+
264
+/**
265
+ * \brief Standard USB language IDs for string descriptors
266
+ */
267
+enum usb_langid {
268
+	USB_LANGID_EN_US = 0x0409, //!< English (United States)
269
+};
270
+
271
+/**
272
+ * \brief Mask selecting the index part of an endpoint address
273
+ */
274
+#define  USB_EP_ADDR_MASK     0x0f
275
+
276
+//! \brief USB address identifier
277
+typedef uint8_t usb_add_t;
278
+
279
+/**
280
+ * \brief Endpoint transfer direction is IN
281
+ */
282
+#define  USB_EP_DIR_IN        0x80
283
+
284
+/**
285
+ * \brief Endpoint transfer direction is OUT
286
+ */
287
+#define  USB_EP_DIR_OUT       0x00
288
+
289
+//! \brief Endpoint identifier
290
+typedef uint8_t usb_ep_t;
291
+
292
+/**
293
+ * \brief Maximum length in bytes of a USB descriptor
294
+ *
295
+ * The maximum length of a USB descriptor is limited by the 8-bit
296
+ * bLength field.
297
+ */
298
+#define  USB_MAX_DESC_LEN     255
299
+
300
+/*
301
+ * 2-byte alignment requested for all USB structures.
302
+ */
303
+COMPILER_PACK_SET(1)
304
+
305
+/**
306
+ * \brief A USB Device SETUP request
307
+ *
308
+ * The data payload of SETUP packets always follows this structure.
309
+ */
310
+typedef struct {
311
+	uint8_t bmRequestType;
312
+	uint8_t bRequest;
313
+	le16_t wValue;
314
+	le16_t wIndex;
315
+	le16_t wLength;
316
+} usb_setup_req_t;
317
+
318
+/**
319
+ * \brief Standard USB device descriptor structure
320
+ */
321
+typedef struct {
322
+	uint8_t bLength;
323
+	uint8_t bDescriptorType;
324
+	le16_t bcdUSB;
325
+	uint8_t bDeviceClass;
326
+	uint8_t bDeviceSubClass;
327
+	uint8_t bDeviceProtocol;
328
+	uint8_t bMaxPacketSize0;
329
+	le16_t idVendor;
330
+	le16_t idProduct;
331
+	le16_t bcdDevice;
332
+	uint8_t iManufacturer;
333
+	uint8_t iProduct;
334
+	uint8_t iSerialNumber;
335
+	uint8_t bNumConfigurations;
336
+} usb_dev_desc_t;
337
+
338
+/**
339
+ * \brief Standard USB device qualifier descriptor structure
340
+ *
341
+ * This descriptor contains information about the device when running at
342
+ * the "other" speed (i.e. if the device is currently operating at high
343
+ * speed, this descriptor can be used to determine what would change if
344
+ * the device was operating at full speed.)
345
+ */
346
+typedef struct {
347
+	uint8_t bLength;
348
+	uint8_t bDescriptorType;
349
+	le16_t bcdUSB;
350
+	uint8_t bDeviceClass;
351
+	uint8_t bDeviceSubClass;
352
+	uint8_t bDeviceProtocol;
353
+	uint8_t bMaxPacketSize0;
354
+	uint8_t bNumConfigurations;
355
+	uint8_t bReserved;
356
+} usb_dev_qual_desc_t;
357
+
358
+/**
359
+ * \brief USB Device BOS descriptor structure
360
+ *
361
+ * The BOS descriptor (Binary device Object Store) defines a root
362
+ * descriptor that is similar to the configuration descriptor, and is
363
+ * the base descriptor for accessing a family of related descriptors.
364
+ * A host can read a BOS descriptor and learn from the wTotalLength field
365
+ * the entire size of the device-level descriptor set, or it can read in
366
+ * the entire BOS descriptor set of device capabilities.
367
+ * The host accesses this descriptor using the GetDescriptor() request.
368
+ * The descriptor type in the GetDescriptor() request is set to BOS.
369
+ */
370
+typedef struct {
371
+	uint8_t bLength;
372
+	uint8_t bDescriptorType;
373
+	le16_t  wTotalLength;
374
+	uint8_t bNumDeviceCaps;
375
+} usb_dev_bos_desc_t;
376
+
377
+
378
+/**
379
+ * \brief USB Device Capabilities - USB 2.0 Extension Descriptor structure
380
+ *
381
+ * Defines the set of USB 1.1-specific device level capabilities.
382
+ */
383
+typedef struct {
384
+	uint8_t bLength;
385
+	uint8_t bDescriptorType;
386
+	uint8_t bDevCapabilityType;
387
+	le32_t  bmAttributes;
388
+} usb_dev_capa_ext_desc_t;
389
+
390
+/**
391
+ * \brief USB Device LPM Descriptor structure
392
+ *
393
+ * The BOS descriptor and capabilities descriptors for LPM.
394
+ */
395
+typedef struct {
396
+	usb_dev_bos_desc_t bos;
397
+	usb_dev_capa_ext_desc_t capa_ext;
398
+} usb_dev_lpm_desc_t;
399
+
400
+/**
401
+ * \brief Standard USB Interface Association Descriptor structure
402
+ */
403
+typedef struct {
404
+	uint8_t bLength;          //!< size of this descriptor in bytes
405
+	uint8_t bDescriptorType;  //!< INTERFACE descriptor type
406
+	uint8_t bFirstInterface;  //!< Number of interface
407
+	uint8_t bInterfaceCount;  //!< value to select alternate setting
408
+	uint8_t bFunctionClass;   //!< Class code assigned by the USB
409
+	uint8_t bFunctionSubClass;//!< Sub-class code assigned by the USB
410
+	uint8_t bFunctionProtocol;//!< Protocol code assigned by the USB
411
+	uint8_t iFunction;        //!< Index of string descriptor
412
+} usb_association_desc_t;
413
+
414
+
415
+/**
416
+ * \brief Standard USB configuration descriptor structure
417
+ */
418
+typedef struct {
419
+	uint8_t bLength;
420
+	uint8_t bDescriptorType;
421
+	le16_t wTotalLength;
422
+	uint8_t bNumInterfaces;
423
+	uint8_t bConfigurationValue;
424
+	uint8_t iConfiguration;
425
+	uint8_t bmAttributes;
426
+	uint8_t bMaxPower;
427
+} usb_conf_desc_t;
428
+
429
+
430
+#define  USB_CONFIG_ATTR_MUST_SET         (1 << 7) //!< Must always be set
431
+#define  USB_CONFIG_ATTR_BUS_POWERED      (0 << 6) //!< Bus-powered
432
+#define  USB_CONFIG_ATTR_SELF_POWERED     (1 << 6) //!< Self-powered
433
+#define  USB_CONFIG_ATTR_REMOTE_WAKEUP    (1 << 5) //!< remote wakeup supported
434
+
435
+#define  USB_CONFIG_MAX_POWER(ma)         (((ma) + 1) / 2) //!< Max power in mA
436
+
437
+/**
438
+ * \brief Standard USB association descriptor structure
439
+ */
440
+typedef struct {
441
+	uint8_t bLength;              //!< Size of this descriptor in bytes
442
+	uint8_t bDescriptorType;      //!< Interface descriptor type
443
+	uint8_t bFirstInterface;      //!< Number of interface
444
+	uint8_t bInterfaceCount;      //!< value to select alternate setting
445
+	uint8_t bFunctionClass;       //!< Class code assigned by the USB
446
+	uint8_t bFunctionSubClass;    //!< Sub-class code assigned by the USB
447
+	uint8_t bFunctionProtocol;    //!< Protocol code assigned by the USB
448
+	uint8_t iFunction;            //!< Index of string descriptor
449
+} usb_iad_desc_t;
450
+
451
+/**
452
+ * \brief Standard USB interface descriptor structure
453
+ */
454
+typedef struct {
455
+	uint8_t bLength;
456
+	uint8_t bDescriptorType;
457
+	uint8_t bInterfaceNumber;
458
+	uint8_t bAlternateSetting;
459
+	uint8_t bNumEndpoints;
460
+	uint8_t bInterfaceClass;
461
+	uint8_t bInterfaceSubClass;
462
+	uint8_t bInterfaceProtocol;
463
+	uint8_t iInterface;
464
+} usb_iface_desc_t;
465
+
466
+/**
467
+ * \brief Standard USB endpoint descriptor structure
468
+ */
469
+typedef struct {
470
+	uint8_t bLength;
471
+	uint8_t bDescriptorType;
472
+	uint8_t bEndpointAddress;
473
+	uint8_t bmAttributes;
474
+	le16_t wMaxPacketSize;
475
+	uint8_t bInterval;
476
+} usb_ep_desc_t;
477
+
478
+
479
+/**
480
+ * \brief A standard USB string descriptor structure
481
+ */
482
+typedef struct {
483
+	uint8_t bLength;
484
+	uint8_t bDescriptorType;
485
+} usb_str_desc_t;
486
+
487
+typedef struct {
488
+	usb_str_desc_t desc;
489
+	le16_t string[1];
490
+} usb_str_lgid_desc_t;
491
+
492
+COMPILER_PACK_RESET()
493
+
494
+//! @}
495
+
496
+#endif /* _USB_PROTOCOL_H_ */

+ 318
- 0
Marlin/src/HAL/HAL_DUE/usb/usb_protocol_cdc.h View File

@@ -0,0 +1,318 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB Communication Device Class (CDC) protocol definitions
5
+ *
6
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+#ifndef _USB_PROTOCOL_CDC_H_
47
+#define _USB_PROTOCOL_CDC_H_
48
+
49
+#include "compiler.h"
50
+
51
+/**
52
+ * \ingroup usb_protocol_group
53
+ * \defgroup cdc_protocol_group Communication Device Class Definitions
54
+ * @{
55
+ */
56
+
57
+/**
58
+ * \name Possible values of class
59
+ */
60
+//@{
61
+#define  CDC_CLASS_DEVICE     0x02	//!< USB Communication Device Class
62
+#define  CDC_CLASS_COMM       0x02	//!< CDC Communication Class Interface
63
+#define  CDC_CLASS_DATA       0x0A	//!< CDC Data Class Interface
64
+//@}
65
+
66
+//! \name USB CDC Subclass IDs
67
+//@{
68
+#define  CDC_SUBCLASS_DLCM    0x01	//!< Direct Line Control Model
69
+#define  CDC_SUBCLASS_ACM     0x02	//!< Abstract Control Model
70
+#define  CDC_SUBCLASS_TCM     0x03	//!< Telephone Control Model
71
+#define  CDC_SUBCLASS_MCCM    0x04	//!< Multi-Channel Control Model
72
+#define  CDC_SUBCLASS_CCM     0x05	//!< CAPI Control Model
73
+#define  CDC_SUBCLASS_ETH     0x06	//!< Ethernet Networking Control Model
74
+#define  CDC_SUBCLASS_ATM     0x07	//!< ATM Networking Control Model
75
+//@}
76
+
77
+//! \name USB CDC Communication Interface Protocol IDs
78
+//@{
79
+#define  CDC_PROTOCOL_V25TER  0x01	//!< Common AT commands
80
+//@}
81
+
82
+//! \name USB CDC Data Interface Protocol IDs
83
+//@{
84
+#define  CDC_PROTOCOL_I430    0x30	//!< ISDN BRI
85
+#define  CDC_PROTOCOL_HDLC    0x31	//!< HDLC
86
+#define  CDC_PROTOCOL_TRANS   0x32	//!< Transparent
87
+#define  CDC_PROTOCOL_Q921M   0x50	//!< Q.921 management protocol
88
+#define  CDC_PROTOCOL_Q921    0x51	//!< Q.931 [sic] Data link protocol
89
+#define  CDC_PROTOCOL_Q921TM  0x52	//!< Q.921 TEI-multiplexor
90
+#define  CDC_PROTOCOL_V42BIS  0x90	//!< Data compression procedures
91
+#define  CDC_PROTOCOL_Q931    0x91	//!< Euro-ISDN protocol control
92
+#define  CDC_PROTOCOL_V120    0x92	//!< V.24 rate adaption to ISDN
93
+#define  CDC_PROTOCOL_CAPI20  0x93	//!< CAPI Commands
94
+#define  CDC_PROTOCOL_HOST    0xFD	//!< Host based driver
95
+/**
96
+ * \brief Describes the Protocol Unit Functional Descriptors [sic]
97
+ * on Communication Class Interface
98
+ */
99
+#define  CDC_PROTOCOL_PUFD    0xFE
100
+//@}
101
+
102
+//! \name USB CDC Functional Descriptor Types
103
+//@{
104
+#define  CDC_CS_INTERFACE     0x24	//!< Interface Functional Descriptor
105
+#define  CDC_CS_ENDPOINT      0x25	//!< Endpoint Functional Descriptor
106
+//@}
107
+
108
+//! \name USB CDC Functional Descriptor Subtypes
109
+//@{
110
+#define  CDC_SCS_HEADER       0x00	//!< Header Functional Descriptor
111
+#define  CDC_SCS_CALL_MGMT    0x01	//!< Call Management
112
+#define  CDC_SCS_ACM          0x02	//!< Abstract Control Management
113
+#define  CDC_SCS_UNION        0x06	//!< Union Functional Descriptor
114
+//@}
115
+
116
+//! \name USB CDC Request IDs
117
+//@{
118
+#define  USB_REQ_CDC_SEND_ENCAPSULATED_COMMAND                   0x00
119
+#define  USB_REQ_CDC_GET_ENCAPSULATED_RESPONSE                   0x01
120
+#define  USB_REQ_CDC_SET_COMM_FEATURE                            0x02
121
+#define  USB_REQ_CDC_GET_COMM_FEATURE                            0x03
122
+#define  USB_REQ_CDC_CLEAR_COMM_FEATURE                          0x04
123
+#define  USB_REQ_CDC_SET_AUX_LINE_STATE                          0x10
124
+#define  USB_REQ_CDC_SET_HOOK_STATE                              0x11
125
+#define  USB_REQ_CDC_PULSE_SETUP                                 0x12
126
+#define  USB_REQ_CDC_SEND_PULSE                                  0x13
127
+#define  USB_REQ_CDC_SET_PULSE_TIME                              0x14
128
+#define  USB_REQ_CDC_RING_AUX_JACK                               0x15
129
+#define  USB_REQ_CDC_SET_LINE_CODING                             0x20
130
+#define  USB_REQ_CDC_GET_LINE_CODING                             0x21
131
+#define  USB_REQ_CDC_SET_CONTROL_LINE_STATE                      0x22
132
+#define  USB_REQ_CDC_SEND_BREAK                                  0x23
133
+#define  USB_REQ_CDC_SET_RINGER_PARMS                            0x30
134
+#define  USB_REQ_CDC_GET_RINGER_PARMS                            0x31
135
+#define  USB_REQ_CDC_SET_OPERATION_PARMS                         0x32
136
+#define  USB_REQ_CDC_GET_OPERATION_PARMS                         0x33
137
+#define  USB_REQ_CDC_SET_LINE_PARMS                              0x34
138
+#define  USB_REQ_CDC_GET_LINE_PARMS                              0x35
139
+#define  USB_REQ_CDC_DIAL_DIGITS                                 0x36
140
+#define  USB_REQ_CDC_SET_UNIT_PARAMETER                          0x37
141
+#define  USB_REQ_CDC_GET_UNIT_PARAMETER                          0x38
142
+#define  USB_REQ_CDC_CLEAR_UNIT_PARAMETER                        0x39
143
+#define  USB_REQ_CDC_GET_PROFILE                                 0x3A
144
+#define  USB_REQ_CDC_SET_ETHERNET_MULTICAST_FILTERS              0x40
145
+#define  USB_REQ_CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERNFILTER 0x41
146
+#define  USB_REQ_CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERNFILTER 0x42
147
+#define  USB_REQ_CDC_SET_ETHERNET_PACKET_FILTER                  0x43
148
+#define  USB_REQ_CDC_GET_ETHERNET_STATISTIC                      0x44
149
+#define  USB_REQ_CDC_SET_ATM_DATA_FORMAT                         0x50
150
+#define  USB_REQ_CDC_GET_ATM_DEVICE_STATISTICS                   0x51
151
+#define  USB_REQ_CDC_SET_ATM_DEFAULT_VC                          0x52
152
+#define  USB_REQ_CDC_GET_ATM_VC_STATISTICS                       0x53
153
+// Added bNotification codes according cdc spec 1.1 chapter 6.3
154
+#define  USB_REQ_CDC_NOTIFY_RING_DETECT                          0x09
155
+#define  USB_REQ_CDC_NOTIFY_SERIAL_STATE                         0x20
156
+#define  USB_REQ_CDC_NOTIFY_CALL_STATE_CHANGE                    0x28
157
+#define  USB_REQ_CDC_NOTIFY_LINE_STATE_CHANGE                    0x29
158
+//@}
159
+
160
+/*
161
+ * Need to pack structures tightly, or the compiler might insert padding
162
+ * and violate the spec-mandated layout.
163
+ */
164
+COMPILER_PACK_SET(1)
165
+
166
+//! \name USB CDC Descriptors
167
+//@{
168
+
169
+
170
+//! CDC Header Functional Descriptor
171
+typedef struct {
172
+	uint8_t bFunctionLength;
173
+	uint8_t bDescriptorType;
174
+	uint8_t bDescriptorSubtype;
175
+	le16_t bcdCDC;
176
+} usb_cdc_hdr_desc_t;
177
+
178
+//! CDC Call Management Functional Descriptor
179
+typedef struct {
180
+	uint8_t bFunctionLength;
181
+	uint8_t bDescriptorType;
182
+	uint8_t bDescriptorSubtype;
183
+	uint8_t bmCapabilities;
184
+	uint8_t bDataInterface;
185
+} usb_cdc_call_mgmt_desc_t;
186
+
187
+//! CDC ACM Functional Descriptor
188
+typedef struct {
189
+	uint8_t bFunctionLength;
190
+	uint8_t bDescriptorType;
191
+	uint8_t bDescriptorSubtype;
192
+	uint8_t bmCapabilities;
193
+} usb_cdc_acm_desc_t;
194
+
195
+//! CDC Union Functional Descriptor
196
+typedef struct {
197
+	uint8_t bFunctionLength;
198
+	uint8_t bDescriptorType;
199
+	uint8_t bDescriptorSubtype;
200
+	uint8_t bMasterInterface;
201
+	uint8_t bSlaveInterface0;
202
+} usb_cdc_union_desc_t;
203
+
204
+
205
+//! \name USB CDC Call Management Capabilities
206
+//@{
207
+//! Device handles call management itself
208
+#define  CDC_CALL_MGMT_SUPPORTED             (1 << 0)
209
+//! Device can send/receive call management info over a Data Class interface
210
+#define  CDC_CALL_MGMT_OVER_DCI              (1 << 1)
211
+//@}
212
+
213
+//! \name USB CDC ACM Capabilities
214
+//@{
215
+//! Device supports the request combination of
216
+//! Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature.
217
+#define  CDC_ACM_SUPPORT_FEATURE_REQUESTS    (1 << 0)
218
+//! Device supports the request combination of
219
+//! Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding,
220
+//! and the notification Serial_State.
221
+#define  CDC_ACM_SUPPORT_LINE_REQUESTS       (1 << 1)
222
+//! Device supports the request Send_Break
223
+#define  CDC_ACM_SUPPORT_SENDBREAK_REQUESTS  (1 << 2)
224
+//! Device supports the notification Network_Connection.
225
+#define  CDC_ACM_SUPPORT_NOTIFY_REQUESTS     (1 << 3)
226
+//@}
227
+//@}
228
+
229
+//! \name USB CDC line control
230
+//@{
231
+
232
+//! \name USB CDC line coding
233
+//@{
234
+//! Line Coding structure
235
+typedef struct {
236
+	le32_t dwDTERate;
237
+	uint8_t bCharFormat;
238
+	uint8_t bParityType;
239
+	uint8_t bDataBits;
240
+} usb_cdc_line_coding_t;
241
+//! Possible values of bCharFormat
242
+enum cdc_char_format {
243
+	CDC_STOP_BITS_1 = 0,	//!< 1 stop bit
244
+	CDC_STOP_BITS_1_5 = 1,	//!< 1.5 stop bits
245
+	CDC_STOP_BITS_2 = 2,	//!< 2 stop bits
246
+};
247
+//! Possible values of bParityType
248
+enum cdc_parity {
249
+	CDC_PAR_NONE = 0,	//!< No parity
250
+	CDC_PAR_ODD = 1,	//!< Odd parity
251
+	CDC_PAR_EVEN = 2,	//!< Even parity
252
+	CDC_PAR_MARK = 3,	//!< Parity forced to 0 (space)
253
+	CDC_PAR_SPACE = 4,	//!< Parity forced to 1 (mark)
254
+};
255
+//@}
256
+
257
+//! \name USB CDC control signals
258
+//! spec 1.1 chapter 6.2.14
259
+//@{
260
+
261
+//! Control signal structure
262
+typedef struct {
263
+	uint16_t value;
264
+} usb_cdc_control_signal_t;
265
+
266
+//! \name Possible values in usb_cdc_control_signal_t
267
+//@{
268
+//! Carrier control for half duplex modems.
269
+//! This signal corresponds to V.24 signal 105 and RS-232 signal RTS.
270
+//! The device ignores the value of this bit
271
+//! when operating in full duplex mode.
272
+#define  CDC_CTRL_SIGNAL_ACTIVATE_CARRIER    (1 << 1)
273
+//! Indicates to DCE if DTE is present or not.
274
+//! This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR.
275
+#define  CDC_CTRL_SIGNAL_DTE_PRESENT         (1 << 0)
276
+//@}
277
+//@}
278
+
279
+
280
+//! \name USB CDC notification message
281
+//@{
282
+
283
+typedef struct {
284
+	uint8_t bmRequestType;
285
+	uint8_t bNotification;
286
+	le16_t wValue;
287
+	le16_t wIndex;
288
+	le16_t wLength;
289
+} usb_cdc_notify_msg_t;
290
+
291
+//! \name USB CDC serial state
292
+//@{*
293
+
294
+//! Hardware handshake support (cdc spec 1.1 chapter 6.3.5)
295
+typedef struct {
296
+	usb_cdc_notify_msg_t header;
297
+	le16_t value;
298
+} usb_cdc_notify_serial_state_t;
299
+
300
+//! \name Possible values in usb_cdc_notify_serial_state_t
301
+//@{
302
+#define  CDC_SERIAL_STATE_DCD       CPU_TO_LE16((1<<0))
303
+#define  CDC_SERIAL_STATE_DSR       CPU_TO_LE16((1<<1))
304
+#define  CDC_SERIAL_STATE_BREAK     CPU_TO_LE16((1<<2))
305
+#define  CDC_SERIAL_STATE_RING      CPU_TO_LE16((1<<3))
306
+#define  CDC_SERIAL_STATE_FRAMING   CPU_TO_LE16((1<<4))
307
+#define  CDC_SERIAL_STATE_PARITY    CPU_TO_LE16((1<<5))
308
+#define  CDC_SERIAL_STATE_OVERRUN   CPU_TO_LE16((1<<6))
309
+//@}
310
+//! @}
311
+
312
+//! @}
313
+
314
+COMPILER_PACK_RESET()
315
+
316
+//! @}
317
+
318
+#endif // _USB_PROTOCOL_CDC_H_

+ 147
- 0
Marlin/src/HAL/HAL_DUE/usb/usb_protocol_msc.h View File

@@ -0,0 +1,147 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief USB Mass Storage Class (MSC) protocol definitions.
5
+ *
6
+ * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _USB_PROTOCOL_MSC_H_
48
+#define _USB_PROTOCOL_MSC_H_
49
+
50
+
51
+/**
52
+ * \ingroup usb_protocol_group
53
+ * \defgroup usb_msc_protocol USB Mass Storage Class (MSC) protocol definitions
54
+ *
55
+ * @{
56
+ */
57
+
58
+/**
59
+ * \name Possible Class value
60
+ */
61
+//@{
62
+#define  MSC_CLASS                  0x08
63
+//@}
64
+
65
+/**
66
+ * \name Possible SubClass value
67
+ * \note In practise, most devices should use
68
+ * #MSC_SUBCLASS_TRANSPARENT and specify the actual command set in
69
+ * the standard INQUIRY data block, even if the MSC spec indicates
70
+ * otherwise. In particular, RBC is not supported by certain major
71
+ * operating systems like Windows XP.
72
+ */
73
+//@{
74
+#define  MSC_SUBCLASS_RBC           0x01	//!< Reduced Block Commands
75
+#define  MSC_SUBCLASS_ATAPI         0x02	//!< CD/DVD devices
76
+#define  MSC_SUBCLASS_QIC_157       0x03	//!< Tape devices
77
+#define  MSC_SUBCLASS_UFI           0x04	//!< Floppy disk drives
78
+#define  MSC_SUBCLASS_SFF_8070I     0x05	//!< Floppy disk drives
79
+#define  MSC_SUBCLASS_TRANSPARENT   0x06	//!< Determined by INQUIRY
80
+//@}
81
+
82
+/**
83
+ * \name Possible protocol value
84
+ * \note Only the BULK protocol should be used in new designs.
85
+ */
86
+//@{
87
+#define  MSC_PROTOCOL_CBI           0x00	//!< Command/Bulk/Interrupt
88
+#define  MSC_PROTOCOL_CBI_ALT       0x01	//!< W/o command completion
89
+#define  MSC_PROTOCOL_BULK          0x50	//!< Bulk-only
90
+//@}
91
+
92
+
93
+/**
94
+ * \brief MSC USB requests (bRequest)
95
+ */
96
+enum usb_reqid_msc {
97
+	USB_REQ_MSC_BULK_RESET = 0xFF,	//!< Mass Storage Reset
98
+	USB_REQ_MSC_GET_MAX_LUN = 0xFE,	//!< Get Max LUN
99
+};
100
+
101
+
102
+COMPILER_PACK_SET(1)
103
+
104
+/**
105
+ * \name A Command Block Wrapper (CBW).
106
+ */
107
+//@{
108
+struct usb_msc_cbw {
109
+	le32_t dCBWSignature;	//!< Must contain 'USBC'
110
+	le32_t dCBWTag;	//!< Unique command ID
111
+	le32_t dCBWDataTransferLength;	//!< Number of bytes to transfer
112
+	uint8_t bmCBWFlags;	//!< Direction in bit 7
113
+	uint8_t bCBWLUN;	//!< Logical Unit Number
114
+	uint8_t bCBWCBLength;	//!< Number of valid CDB bytes
115
+	uint8_t CDB[16];	//!< SCSI Command Descriptor Block
116
+};
117
+
118
+#define  USB_CBW_SIGNATURE          0x55534243	//!< dCBWSignature value
119
+#define  USB_CBW_DIRECTION_IN       (1<<7)	//!< Data from device to host
120
+#define  USB_CBW_DIRECTION_OUT      (0<<7)	//!< Data from host to device
121
+#define  USB_CBW_LUN_MASK           0x0F	//!< Valid bits in bCBWLUN
122
+#define  USB_CBW_LEN_MASK           0x1F	//!< Valid bits in bCBWCBLength
123
+//@}
124
+
125
+
126
+/**
127
+ * \name A Command Status Wrapper (CSW).
128
+ */
129
+//@{
130
+struct usb_msc_csw {
131
+	le32_t dCSWSignature;	//!< Must contain 'USBS'
132
+	le32_t dCSWTag;	//!< Same as dCBWTag
133
+	le32_t dCSWDataResidue;	//!< Number of bytes not transfered
134
+	uint8_t bCSWStatus;	//!< Status code
135
+};
136
+
137
+#define  USB_CSW_SIGNATURE          0x55534253	//!< dCSWSignature value
138
+#define  USB_CSW_STATUS_PASS        0x00	//!< Command Passed
139
+#define  USB_CSW_STATUS_FAIL        0x01	//!< Command Failed
140
+#define  USB_CSW_STATUS_PE          0x02	//!< Phase Error
141
+//@}
142
+
143
+COMPILER_PACK_RESET()
144
+
145
+//@}
146
+
147
+#endif // _USB_PROTOCOL_MSC_H_

+ 186
- 0
Marlin/src/HAL/HAL_DUE/usb/usb_task.c View File

@@ -0,0 +1,186 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Main functions for USB composite example
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifdef ARDUINO_ARCH_SAM
48
+
49
+#include "conf_usb.h"
50
+#include "udc.h"
51
+
52
+static volatile bool main_b_msc_enable = false;
53
+static volatile bool main_b_cdc_enable = false;
54
+
55
+void HAL_init(void) {
56
+	udd_disable();
57
+	UDD_SetStack(&USBD_ISR);
58
+
59
+	// Start USB stack to authorize VBus monitoring
60
+	udc_start();
61
+}
62
+
63
+void HAL_idletask(void)
64
+{
65
+
66
+	// Attend SD card access from the USB MSD -- Priotize access to improve speed
67
+	int delay = 2;
68
+	while (main_b_msc_enable && --delay > 0 ) {
69
+		if (udi_msc_process_trans()) {
70
+			delay = 10000;
71
+		}
72
+		
73
+		/* Reset the watchdog, just to be sure */
74
+		REG_WDT_CR = WDT_CR_WDRSTT | WDT_CR_KEY(0xA5);
75
+	}
76
+}
77
+
78
+/*! \brief Example of extra USB string management
79
+ * This feature is available for single or composite device
80
+ * which want implement additional USB string than
81
+ * Manufacture, Product and serial number ID.
82
+ *
83
+ * return true, if the string ID requested is know and managed by this functions
84
+ */
85
+bool usb_task_extra_string(void)
86
+{
87
+	static uint8_t udi_cdc_name[] = "CDC interface";
88
+	static uint8_t udi_msc_name[] = "MSC interface";
89
+
90
+	struct extra_strings_desc_t{
91
+		usb_str_desc_t header;
92
+		le16_t string[Max(sizeof(udi_cdc_name)-1, sizeof(udi_msc_name)-1)];
93
+	};
94
+	static UDC_DESC_STORAGE struct extra_strings_desc_t extra_strings_desc = {
95
+		.header.bDescriptorType = USB_DT_STRING
96
+	};
97
+
98
+	uint8_t i;
99
+	uint8_t *str;
100
+	uint8_t str_lgt=0;
101
+
102
+	// Link payload pointer to the string corresponding at request
103
+	switch (udd_g_ctrlreq.req.wValue & 0xff) {
104
+	case UDI_CDC_IAD_STRING_ID:
105
+		str_lgt = sizeof(udi_cdc_name)-1;
106
+		str = udi_cdc_name;
107
+		break;
108
+	case UDI_MSC_STRING_ID:
109
+		str_lgt = sizeof(udi_msc_name)-1;
110
+		str = udi_msc_name;
111
+		break;
112
+	default:
113
+		return false;
114
+	}
115
+
116
+	if (str_lgt!=0) {
117
+		for( i=0; i<str_lgt; i++) {
118
+			extra_strings_desc.string[i] = cpu_to_le16((le16_t)str[i]);
119
+		}
120
+		extra_strings_desc.header.bLength = 2+ (str_lgt)*2;
121
+		udd_g_ctrlreq.payload_size = extra_strings_desc.header.bLength;
122
+		udd_g_ctrlreq.payload = (uint8_t *) &extra_strings_desc;
123
+	}
124
+
125
+	// if the string is larger than request length, then cut it
126
+	if (udd_g_ctrlreq.payload_size > udd_g_ctrlreq.req.wLength) {
127
+		udd_g_ctrlreq.payload_size = udd_g_ctrlreq.req.wLength;
128
+	}
129
+	return true;
130
+}
131
+
132
+bool usb_task_msc_enable(void)
133
+{
134
+	main_b_msc_enable = true;
135
+	return true;
136
+}
137
+
138
+void usb_task_msc_disable(void)
139
+{
140
+	main_b_msc_enable = false;
141
+}
142
+
143
+bool usb_task_msc_isenabled(void)
144
+{
145
+	return main_b_msc_enable;
146
+}
147
+
148
+bool usb_task_cdc_enable(uint8_t port)
149
+{
150
+	main_b_cdc_enable = true;
151
+	return true;
152
+}
153
+
154
+void usb_task_cdc_disable(uint8_t port)
155
+{
156
+	main_b_cdc_enable = false;
157
+}
158
+
159
+bool usb_task_cdc_isenabled(void)
160
+{
161
+	return main_b_cdc_enable;
162
+}
163
+
164
+/*! \brief Called by CDC interface
165
+ * Callback running when CDC device have received data
166
+ */
167
+void usb_task_cdc_rx_notify(uint8_t port)
168
+{
169
+}
170
+
171
+/*! \brief Configures communication line
172
+ *
173
+ * \param cfg      line configuration
174
+ */
175
+void usb_task_cdc_config(uint8_t port, usb_cdc_line_coding_t * cfg)
176
+{
177
+}
178
+
179
+void usb_task_cdc_set_dtr(uint8_t port, bool b_enable)
180
+{
181
+	if (b_enable) {
182
+	} else {
183
+	}
184
+}
185
+
186
+#endif

+ 101
- 0
Marlin/src/HAL/HAL_DUE/usb/usb_task.h View File

@@ -0,0 +1,101 @@
1
+/**
2
+ * \file
3
+ *
4
+ * \brief Declaration of main function used by Composite example 4
5
+ *
6
+ * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7
+ *
8
+ * \asf_license_start
9
+ *
10
+ * \page License
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * 1. Redistributions of source code must retain the above copyright notice,
16
+ *    this list of conditions and the following disclaimer.
17
+ *
18
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
19
+ *    this list of conditions and the following disclaimer in the documentation
20
+ *    and/or other materials provided with the distribution.
21
+ *
22
+ * 3. The name of Atmel may not be used to endorse or promote products derived
23
+ *    from this software without specific prior written permission.
24
+ *
25
+ * 4. This software may only be redistributed and used in connection with an
26
+ *    Atmel microcontroller product.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31
+ * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
+ * POSSIBILITY OF SUCH DAMAGE.
39
+ *
40
+ * \asf_license_stop
41
+ *
42
+ */
43
+/*
44
+ * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45
+ */
46
+
47
+#ifndef _USB_TASK_H_
48
+#define _USB_TASK_H_
49
+
50
+#include "usb_protocol_cdc.h"
51
+
52
+/*! \brief Called by MSC interface
53
+ * Callback running when USB Host enable MSC interface
54
+ *
55
+ * \retval true if MSC startup is ok
56
+ */
57
+bool usb_task_msc_enable(void);
58
+
59
+/*! \brief Called by MSC interface
60
+ * Callback running when USB Host disable MSC interface
61
+ */
62
+void usb_task_msc_disable(void);
63
+
64
+/*! \brief Opens the communication port
65
+ * This is called by CDC interface when USB Host enable it.
66
+ *
67
+ * \retval true if cdc startup is successfully done
68
+ */
69
+bool usb_task_cdc_enable(uint8_t port);
70
+
71
+/*! \brief Closes the communication port
72
+ * This is called by CDC interface when USB Host disable it.
73
+ */
74
+void usb_task_cdc_disable(uint8_t port);
75
+
76
+/*! \brief Save new DTR state to change led behavior.
77
+ * The DTR notify that the terminal have open or close the communication port.
78
+ */
79
+void usb_task_cdc_set_dtr(uint8_t port, bool b_enable);
80
+
81
+/*! \brief Called by UDC when USB Host request a extra string different
82
+ * of this specified in USB device descriptor
83
+ */
84
+bool usb_task_extra_string(void);
85
+
86
+/*! \brief Called by CDC interface
87
+ * Callback running when CDC device have received data
88
+ */
89
+void usb_task_cdc_rx_notify(uint8_t port);
90
+
91
+/*! \brief Configures communication line
92
+ *
93
+ * \param cfg      line configuration
94
+ */
95
+void usb_task_cdc_config(uint8_t port, usb_cdc_line_coding_t * cfg); 
96
+
97
+/* The USB device interrupt
98
+ */
99
+void USBD_ISR(void);
100
+
101
+#endif // _MAIN_H_

+ 4
- 0
Marlin/src/Marlin.cpp View File

@@ -637,6 +637,10 @@ void stop() {
637 637
  */
638 638
 void setup() {
639 639
 
640
+  #ifdef HAL_INIT
641
+    HAL_init();
642
+  #endif
643
+
640 644
   #if ENABLED(MAX7219_DEBUG)
641 645
     Max7219_init();
642 646
   #endif

Loading…
Cancel
Save