Hanna
Sourcecode Kontrollplatine
usb_defaults.h
gehe zur Dokumentation dieser Datei
1 
16 #ifndef _USB_DEFAULTS_H
17 #define _USB_DEFAULTS_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <stddef.h>
23 
24 #include <avr/io.h>
25 #include <avr/interrupt.h>
26 #include <avr/pgmspace.h>
27 #include <util/delay.h>
28 
29 #define ARCH_HAS_EEPROM_ADDRESS_SPACE
30 #define ARCH_HAS_FLASH_ADDRESS_SPACE
31 #define ARCH_HAS_MULTI_ADDRESS_SPACE
32 #define ARCH_LITTLE_ENDIAN
33 
34 #define USB_DEF_VID 0x03EB // VID for USB
35 //#define USB_DEF_PID 0x204B // PID for USB
36 #define USB_DEF_PID 0x2404 // PID for USB
37 
38 #define USB_DEF_EP_NUM 3 // Number of Endpoints used (ep0, In, out, notification... )
39 #define USB_DEF_EP_MAX 16 // Number of max endpoints for this device (to allocate the endpoint-table)
40 #define USB_DEF_EP0_SIZE 64 // size of endpoint0 to handle controll-requests
41 
42 #define ENDPOINT_DIR_IN 0x80
43 #define ENDPOINT_DIR_MASK 0x80
44 #define ENDPOINT_DIR_OUT 0x00
45 
46 //#define USB_DEF_CONFIG_CLK 1 // if defined, the clk is configured at init
47 //#define USB_DEF_CONFIG_IRQ 1 // if defined, IRQlevle are configured
48 
49 #define CPU_TO_LE16(x) x
50 
51 #define VERSION_TENS(x) (int)((x) / 10)
52 #define VERSION_ONES(x) (int)((x) - (10 * VERSION_TENS(x)))
53 #define VERSION_TENTHS(x) (int)(((x) - (int)(x)) * 10)
54 #define VERSION_HUNDREDTHS(x) (int)((((x) - (int)(x)) * 100) - (10 * VERSION_TENTHS(x)))
55 
56 #define CONTROL_REQTYPE_DIRECTION 0x80
57 #define CONTROL_REQTYPE_TYPE 0x60
58 #define CONTROL_REQTYPE_RECIPIENT 0x1F
59 #define REQDIR_HOSTTODEVICE (0 << 7)
60 #define REQDIR_DEVICETOHOST (1 << 7)
61 #define REQTYPE_STANDARD (0 << 5)
62 #define REQTYPE_CLASS (1 << 5)
63 #define REQTYPE_VENDOR (2 << 5)
64 #define REQREC_DEVICE (0 << 0)
65 #define REQREC_INTERFACE (1 << 0)
66 #define REQREC_ENDPOINT (2 << 0)
67 #define REQREC_OTHER (3 << 0)
68 
69 #define NO_DESCRIPTOR 0
70 #define USB_CONFIG_POWER_MA(mA) ((mA) >> 1)
71 #define USB_STRING_LEN(UnicodeChars) (sizeof(USB_Descriptor_Header_t) + ((UnicodeChars) << 1))
72 #define VERSION_BCD(x) CPU_TO_LE16((((VERSION_TENS(x) << 4) | VERSION_ONES(x)) << 8) | ((VERSION_TENTHS(x) << 4) | VERSION_HUNDREDTHS(x)))
73 #define LANGUAGE_ID_ENG 0x0409
74 #define ENDPOINT_DESCRIPTOR_DIR_IN ENDPOINT_DIR_IN
75 #define ENDPOINT_DESCRIPTOR_DIR_OUT ENDPOINT_DIR_OUT
76 #define USB_CONFIG_ATTR_BUSPOWERED 0x80
77 #define USB_CONFIG_ATTR_SELFPOWERED 0x40
78 #define USB_CONFIG_ATTR_REMOTEWAKEUP 0x20
79 #define ENDPOINT_ATTR_NO_SYNC (0 << 2)
80 #define ENDPOINT_ATTR_ASYNC (1 << 2)
81 #define ENDPOINT_ATTR_ADAPTIVE (2 << 2)
82 #define ENDPOINT_ATTR_SYNC (3 << 2)
83 #define ENDPOINT_USAGE_DATA (0 << 4)
84 #define ENDPOINT_USAGE_FEEDBACK (1 << 4)
85 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
86 #define EP_TYPE_CONTROL 0x00
87 #define EP_TYPE_ISOCHRONOUS 0x01
88 #define EP_TYPE_BULK 0x02
89 #define EP_TYPE_INTERRUPT 0x03
90 
91 #define FEATURE_SELFPOWERED_ENABLED (1 << 0)
92 #define FEATURE_REMOTE_WAKEUP_ENABLED (1 << 1)
93 
94 #define USE_INTERNAL_SERIAL 0xDC
95 
96 #define INTERNAL_SERIAL_LENGTH_BITS (8 * (1 + (offsetof(NVM_PROD_SIGNATURES_t, COORDY1) - offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0))))
97 #define INTERNAL_SERIAL_START_ADDRESS offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0)
98 
99 typedef uint8_t uint_reg_t;
100 struct USB_Request_Header;
102 
104 // Load and Clear
105 #ifdef __GNUC__
106 #define LACR16(addr,msk) \
107  __asm__ __volatile__ ( \
108  "ldi r16, %1" "\n\t" \
109  ".dc.w 0x9306" "\n\t"\
110  ::"z" (addr), "M" (msk):"r16")
111 #else
112 #define LACR16(addr,msk) __lac((unsigned char)msk,(unsigned char*)addr)
113 #endif
114 
115 // Load and Set
116 #ifdef __GNUC__
117 #define LASR16(addr,msk) \
118  __asm__ __volatile__ ( \
119  "ldi r16, %1" "\n\t" \
120  ".dc.w 0x9305" "\n\t"\
121  ::"z" (addr), "M" (msk):"r16")
122 #else
123 #define LASR16(addr,msk) __las((unsigned char)msk,(unsigned char*)addr)
124 #endif
125 
126 // Exchange
127 #ifdef __GNUC__
128 #define XCHR16(addr,msk) \
129  __asm__ __volatile__ ( \
130  "ldi r16, %1" "\n\t" \
131  ".dc.w 0x9304" "\n\t"\
132  ::"z" (addr), "M" (msk):"r16")
133 #else
134 #define XCHR16(addr,msk) __xch(msk,addr)
135 #endif
136 
137 // Load and toggle
138 #ifdef __GNUC__
139 #define LATR16(addr,msk) \
140  __asm__ __volatile__ ( \
141  "ldi r16, %1" "\n\t" \
142  ".dc.w 0x9307" "\n\t"\
143  ::"z" (addr), "M" (msk):"r16")
144 #else
145 #define LATR16(addr,msk) __lat(msk,addr)
146 #endif
147 
148 #define MACROS do
149 #define MACROE while (0)
150 #if !defined(MAX)
151 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
152 #endif
153 
154 #if !defined(MIN)
155 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
156 #endif
157 
158 #if !defined(STRINGIFY)
159 #define STRINGIFY(x) #x
160 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
161 #endif
162 
163 #if defined(__GNUC__)
164 #define GCC_FORCE_POINTER_ACCESS(StructPtr) __asm__ __volatile__("" : "=b" (StructPtr) : "0" (StructPtr))
165 #define GCC_MEMORY_BARRIER() __asm__ __volatile__("" ::: "memory");
166 #define GCC_IS_COMPILE_CONST(x) __builtin_constant_p(x)
167 
169 #define GCC_ASSERT(e) ((void)sizeof(char[1 - 2*!(__builtin_constant_p(e) && (e))]))
170 
174 #define GCC_FORCE_ALIGN_2 __attribute__((section (".data,\"aw\",@progbits\n.p2align 1;")))
175 
176 #define likely(x) __builtin_expect((x),1)
177 #define unlikely(x) __builtin_expect((x),0)
178 #else
179 #define GCC_FORCE_POINTER_ACCESS(StructPtr)
180 #define GCC_MEMORY_BARRIER()
181 #define GCC_IS_COMPILE_CONST(x) 0
182 #define GCC_FORCE_ALIGN_2
183 #define likely(x) x
184 #define unlikely(x) x
185 #endif
186 
187 #define ATTR_NO_RETURN __attribute__ ((noreturn))
188 #define ATTR_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
189 #define ATTR_NON_NULL_PTR_ARG(...) __attribute__ ((nonnull (__VA_ARGS__)))
190 #define ATTR_NAKED __attribute__ ((naked))
191 #define ATTR_NO_INLINE __attribute__ ((noinline))
192 #define ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
193 #define ATTR_PURE __attribute__ ((pure))
194 #define ATTR_CONST __attribute__ ((const))
195 #define ATTR_DEPRECATED __attribute__ ((deprecated))
196 #define ATTR_WEAK __attribute__ ((weak))
197 #define ATTR_NO_INIT __attribute__ ((section (".noinit")))
198 #define ATTR_INIT_SECTION(SectionIndex) __attribute__ ((naked, section (".init" #SectionIndex )))
199 #define ATTR_ALIAS(Func) __attribute__ ((alias( #Func )))
200 #define ATTR_PACKED __attribute__ ((packed))
201 
211 
213 void EVENT_USB_Device_ControlOUT ( uint8_t* data, uint8_t len );
214 
217 void EVENT_USB_Device_ConfigurationChanged ( uint8_t config );
218 
221 bool EVENT_USB_Device_SetInterface ( uint8_t interface, uint8_t altsetting );
222 
224 void EVENT_USB_Device_Suspend ( void );
225 
227 void EVENT_USB_Device_WakeUp ( void );
228 
230 void EVENT_USB_Device_Reset ( void );
231 
233 void EVENT_USB_Device_StartOfFrame ( void );
234 
235 /* Enums: */
238 {
239  DTYPE_Device = 0x01,
241  DTYPE_String = 0x03,
243  DTYPE_Endpoint = 0x05,
245  DTYPE_Other = 0x07,
250 };
251 
254 {
282 };
283 
284 typedef struct
285 {
286  uint8_t Size;
287  uint8_t Type;
290 }ATTR_PACKED USB_Descriptor_Header_t;
291 
292 typedef struct
293 {
294  uint8_t bLength;
295  uint8_t bDescriptorType;
298 }ATTR_PACKED USB_StdDescriptor_Header_t;
299 
300 typedef struct
301 {
302  USB_Descriptor_Header_t Header;
304  uint16_t USBSpecification;
305  uint8_t Class;
306  uint8_t SubClass;
307  uint8_t Protocol;
309  uint8_t Endpoint0Size;
311  uint16_t VendorID;
312  uint16_t ProductID;
313  uint16_t ReleaseNumber;
321  uint8_t ProductStrIndex;
340 }ATTR_PACKED USB_Descriptor_Device_t;
341 
342 typedef struct
343 {
344  uint8_t bLength;
345  uint8_t bDescriptorType;
348  uint16_t bcdUSB;
349  uint8_t bDeviceClass;
350  uint8_t bDeviceSubClass;
351  uint8_t bDeviceProtocol;
352  uint8_t bMaxPacketSize0;
353  uint16_t idVendor;
354  uint16_t idProduct;
355  uint16_t bcdDevice;
356  uint8_t iManufacturer;
362  uint8_t iProduct;
366  uint8_t iSerialNumber;
381 }ATTR_PACKED USB_StdDescriptor_Device_t;
382 
383 typedef struct
384 {
385  USB_Descriptor_Header_t Header;
387  uint16_t USBSpecification;
388  uint8_t Class;
389  uint8_t SubClass;
390  uint8_t Protocol;
392  uint8_t Endpoint0Size;
393  uint8_t NumberOfConfigurations;
396  uint8_t Reserved;
397 }ATTR_PACKED USB_Descriptor_DeviceQualifier_t;
398 
399 typedef struct
400 {
401  uint8_t bLength;
402  uint8_t bDescriptorType;
405  uint16_t bcdUSB;
406  uint8_t bDeviceClass;
407  uint8_t bDeviceSubClass;
408  uint8_t bDeviceProtocol;
409  uint8_t bMaxPacketSize0;
410  uint8_t bNumConfigurations;
413  uint8_t bReserved;
414 }ATTR_PACKED USB_StdDescriptor_DeviceQualifier_t;
415 
416 typedef struct
417 {
418  USB_Descriptor_Header_t Header;
423  uint8_t TotalInterfaces;
436 }ATTR_PACKED USB_Descriptor_Configuration_Header_t;
437 
438 typedef struct
439 {
440  uint8_t bLength;
441  uint8_t bDescriptorType;
444  uint16_t wTotalLength;
447  uint8_t bNumInterfaces;
449  uint8_t iConfiguration;
450  uint8_t bmAttributes;
453  uint8_t bMaxPower;
457 }ATTR_PACKED USB_StdDescriptor_Configuration_Header_t;
458 
459 typedef struct
460 {
461  USB_Descriptor_Header_t Header;
463  uint8_t InterfaceNumber;
469  uint8_t TotalEndpoints;
471  uint8_t Class;
472  uint8_t SubClass;
473  uint8_t Protocol;
476 }ATTR_PACKED USB_Descriptor_Interface_t;
477 
478 typedef struct
479 {
480  uint8_t bLength;
481  uint8_t bDescriptorType;
490  uint8_t bNumEndpoints;
491  uint8_t bInterfaceClass;
494  uint8_t iInterface;
497 }ATTR_PACKED USB_StdDescriptor_Interface_t;
498 
499 typedef struct
500 {
501  USB_Descriptor_Header_t Header;
504  uint8_t TotalInterfaces;
506  uint8_t Class;
507  uint8_t SubClass;
508  uint8_t Protocol;
510  uint8_t IADStrIndex;
513 }ATTR_PACKED USB_Descriptor_Interface_Association_t;
514 
515 typedef struct
516 {
517  uint8_t bLength;
518  uint8_t bDescriptorType;
521  uint8_t bFirstInterface;
522  uint8_t bInterfaceCount;
523  uint8_t bFunctionClass;
526  uint8_t iFunction;
529 }ATTR_PACKED USB_StdDescriptor_Interface_Association_t;
530 
531 typedef struct
532 {
533  USB_Descriptor_Header_t Header;
535  uint8_t EndpointAddress;
538  uint8_t Attributes;
541  uint16_t EndpointSize;
547 }ATTR_PACKED USB_Descriptor_Endpoint_t;
548 
549 typedef struct
550 {
551  uint8_t bLength;
552  uint8_t bDescriptorType;
558  uint8_t bmAttributes;
561  uint16_t wMaxPacketSize;
564  uint8_t bInterval;
567 }ATTR_PACKED USB_StdDescriptor_Endpoint_t;
568 
569 typedef struct
570 {
571  USB_Descriptor_Header_t Header;
573  wchar_t UnicodeString[];
574 }ATTR_PACKED USB_Descriptor_String_t;
575 
576 typedef struct
577 {
578  uint8_t bLength;
579  uint8_t bDescriptorType;
582  uint16_t bString[];
591 }ATTR_PACKED USB_StdDescriptor_String_t;
592 
593 /* Macros: */
594 
595 typedef struct USB_Request_Header
596 {
597  uint8_t bmRequestType;
598  uint8_t bRequest;
599  uint16_t wValue;
600  uint16_t wIndex;
601  uint16_t wLength;
603 
605 {
646 };
647 
648 /* Private Interface - For use in library only: */
649 /* Macros: */
650 
651 /* Enums: */
660 {
685 };
686 
687 #endif
uint8_t bInterfaceClass
Definition: usb_defaults.h:491
uint8_t MaxPowerConsumption
Definition: usb_defaults.h:432
uint8_t bInterfaceCount
Definition: usb_defaults.h:522
uint8_t Class
Definition: usb_defaults.h:305
uint8_t SubClass
Definition: usb_defaults.h:306
uint8_t InterfaceNumber
Definition: usb_defaults.h:463
uint8_t Attributes
Definition: usb_defaults.h:538
void EVENT_USB_Device_StartOfFrame(void)
uint8_t iConfiguration
Definition: usb_defaults.h:449
uint8_t Size
Definition: usb_defaults.h:286
uint8_t bDeviceProtocol
Definition: usb_defaults.h:351
uint8_t bFunctionClass
Definition: usb_defaults.h:523
uint8_t TotalEndpoints
Definition: usb_defaults.h:469
void EVENT_USB_Device_ConfigurationChanged(uint8_t config)
Definition: usb_cdc.c:191
USB_Descriptor_ClassSubclassProtocol_t
Definition: usb_defaults.h:253
uint8_t iFunction
Definition: usb_defaults.h:526
uint8_t bmAttributes
Definition: usb_defaults.h:450
void EVENT_USB_Device_WakeUp(void)
Definition: usb_cdc.c:208
uint16_t ProductID
Definition: usb_defaults.h:312
uint8_t Type
Definition: usb_defaults.h:287
USB_DescriptorTypes_t
Definition: usb_defaults.h:237
bool EVENT_USB_Device_ControlRequest(struct USB_Request_Header *req)
Definition: usb_cdc.c:239
uint8_t bInterfaceProtocol
Definition: usb_defaults.h:493
uint16_t idVendor
Definition: usb_defaults.h:353
void EVENT_USB_Device_Suspend(void)
Definition: usb_cdc.c:201
uint8_t iProduct
Definition: usb_defaults.h:362
uint8_t bReserved
Definition: usb_defaults.h:413
uint16_t wTotalLength
Definition: usb_defaults.h:444
uint8_t bEndpointAddress
Definition: usb_defaults.h:555
uint8_t bMaxPacketSize0
Definition: usb_defaults.h:352
uint8_t SerialNumStrIndex
Definition: usb_defaults.h:325
uint8_t bInterfaceSubClass
Definition: usb_defaults.h:492
uint8_t bNumEndpoints
Definition: usb_defaults.h:490
uint8_t ConfigurationStrIndex
Definition: usb_defaults.h:426
uint8_t bDeviceSubClass
Definition: usb_defaults.h:350
uint8_t IADStrIndex
Definition: usb_defaults.h:510
uint16_t wMaxPacketSize
Definition: usb_defaults.h:561
uint8_t NumberOfConfigurations
Definition: usb_defaults.h:337
uint8_t iManufacturer
Definition: usb_defaults.h:356
uint8_t FirstInterfaceIndex
Definition: usb_defaults.h:503
uint8_t bInterval
Definition: usb_defaults.h:564
uint8_t bInterfaceNumber
Definition: usb_defaults.h:484
uint8_t ProductStrIndex
Definition: usb_defaults.h:321
uint16_t EndpointSize
Definition: usb_defaults.h:541
uint8_t InterfaceStrIndex
Definition: usb_defaults.h:475
uint16_t bcdDevice
Definition: usb_defaults.h:355
uint8_t ManufacturerStrIndex
Definition: usb_defaults.h:315
uint8_t bFunctionProtocol
Definition: usb_defaults.h:525
uint8_t Protocol
Definition: usb_defaults.h:307
uint8_t bFirstInterface
Definition: usb_defaults.h:521
uint8_t ConfigAttributes
Definition: usb_defaults.h:428
uint8_t PollingIntervalMS
Definition: usb_defaults.h:544
uint8_t AlternateSetting
Definition: usb_defaults.h:464
uint16_t TotalConfigurationSize
Definition: usb_defaults.h:420
uint8_t Reserved
Definition: usb_defaults.h:396
uint8_t iSerialNumber
Definition: usb_defaults.h:366
uint8_t ConfigurationNumber
Definition: usb_defaults.h:425
uint16_t bcdUSB
Definition: usb_defaults.h:348
uint8_t Endpoint0Size
Definition: usb_defaults.h:309
uint8_t bDeviceClass
Definition: usb_defaults.h:349
USB_Control_Request_t
Definition: usb_defaults.h:604
uint16_t ReleaseNumber
Definition: usb_defaults.h:313
uint16_t idProduct
Definition: usb_defaults.h:354
USB_Device_States_t
Definition: usb_defaults.h:659
uint8_t bMaxPower
Definition: usb_defaults.h:453
uint8_t bNumInterfaces
Definition: usb_defaults.h:447
uint8_t bConfigurationValue
Definition: usb_defaults.h:448
void EVENT_USB_Device_Reset(void)
Definition: usb_cdc.c:215
uint8_t EndpointAddress
Definition: usb_defaults.h:535
bool EVENT_USB_Device_SetInterface(uint8_t interface, uint8_t altsetting)
Definition: usb_cdc.c:183
uint8_t bFunctionSubClass
Definition: usb_defaults.h:524
uint8_t TotalInterfaces
Definition: usb_defaults.h:423
uint8_t bLength
Definition: usb_defaults.h:294
uint8_t iInterface
Definition: usb_defaults.h:494
uint8_t bAlternateSetting
Definition: usb_defaults.h:485
uint16_t VendorID
Definition: usb_defaults.h:311
void EVENT_USB_Device_ControlOUT(uint8_t *data, uint8_t len)
Definition: usb_cdc.c:221
uint8_t bNumConfigurations
Definition: usb_defaults.h:378
uint16_t USBSpecification
Definition: usb_defaults.h:304