Forum: Mikrocontroller und Digitale Elektronik Usb Hid mehrere Byte senden / empfangen


von Hannes (Gast)


Lesenswert?

Hallo
Ich habe gerade das erste mal einen Avr (M32) an den Usb angeschlossen 
und oh Wunder es klappt.

So jetzt verstehe ich aber leider den Usb noch so garnicht. Die Software 
erstellt einen Usb Hid (Ohne Treiber) und ich kann ein Byte senden und 
Empfangen.
Allerdings nur Empfangen wenn ich eins gesendet habe. Quasi als Antwort.
Jetzt währe es natürlich von Nutzen 1.Mehrere Byte zu senden und 
irgendwie deutet die software darauf hin das es möglich sein sollte. Und 
zum anderen Ohne anfrage vom Pc, vom Avr ein (Oder mehrere) Byte an den 
Pc zu senden.

Dann noch die frage was ist eigentlich Die Usb Report Id ?

Anbei habe ich mal das Programm (Bascom)
1
$regfile = "m32def.dat"
2
$baud = 9600
3
$noramclear
4
$crystal = 12000000
5
6
'Enable debugging output
7
'0 = disable
8
'1 = debugging messages
9
'2 = dbg frame/stack output
10
'Const Usbdebug = 0
11
12
$hwstack = 40
13
$swstack = 40
14
$framesize = 50
15
16
Config Portb = Output
17
Config Portd.6 = Input
18
Config Portd.5 = Input
19
Config Portd.4 = Input
20
Config Portd.3 = Input
21
Portd.6 = 1
22
Portd.5 = 1
23
Portd.4 = 1
24
Portd.3 = 1
25
26
27
$lib "swusb.lbx"
28
$external _swusb
29
30
Declare Sub Usb_reset()
31
Declare Sub Usb_processsetup(txstate As Byte)
32
Declare Sub Usb_send(txstate As Byte , Byval Count As Byte)
33
Declare Sub Usb_senddescriptor(txstate As Byte , Maxlen As Byte)
34
35
'*******************************************************************************
36
'*************************** Begin USB Configuration ***************************
37
'
38
'Set the following parameters to match your hardware configuration and USB
39
'device parameters.
40
41
'******************************* USB Connections *******************************
42
43
'Define the AVR port that the two USB pins are connected to
44
_usb_port Alias Portd
45
_usb_pin Alias Pind
46
_usb_ddr Alias Ddrd
47
48
'Define the D+ and D- pins. (put D+ on an interrupt pin)
49
Const _usb_dplus = 2                                        '4
50
Const _usb_dminus = 7                                       '5
51
52
'Configure the pins as inputs
53
Config Pind.2 = Input
54
Config Pind.7 = Input
55
56
'disable pullups
57
_usb_port._usb_dplus = 0
58
_usb_port._usb_dminus = 0
59
60
'*******************************************************************************
61
'************************* USB Configuration Constants *************************
62
63
'Use EEPROM or FLASH to store USB descriptors
64
'1 = EEPROM, 0 = FLASH.  Storing to EEPROM will reduce code size slightly.
65
Const _usb_use_eeprom = 0
66
67
'Don't wait for sent packets to be ACK'd by the host before marking the
68
'transmission as complete.  This option breaks the USB spec but improves
69
'throughput with faster polling speeds.
70
'This may cause reliability issues.  Should leave set to 0 to be safe.
71
Const _usb_assume_ack = 0
72
73
'  *************************** Device Descriptor *****************************
74
75
'USB Vendor ID and Product ID (Assigned by USB-IF)
76
Const _usb_vid = &HAAAA
77
Const _usb_pid = &HEF22
78
79
'USB Device Release Number (BCD)
80
Const _usb_devrel = &H0001
81
82
'USB Release Spec (BCD)
83
Const _usb_spec = &H0110
84
85
'USB Device Class, subclass, and protocol (assigned by USB-IF).
86
'&h00 = Class defined by interface. (HID is defined in the interface)
87
'&hFF = Vendor-defined class (You must write your own PC driver)
88
'See http://www.usb.org/developers/defined_class  for more information
89
Const _usb_devclass = 0
90
Const _usb_devsubclass = 0
91
Const _usb_devprot = 0
92
93
'These are _indexes_ to UNICODE string descriptors for the manufacturer,
94
'product name, and serial number.  0 means there is no descriptor.
95
Const _usb_imanufacturer = 1
96
Const _usb_iproduct = 2
97
Const _usb_iserial = 0
98
99
'Number of configurations for this device.  Don't change this unless
100
'you know what you are doing.  Ordinarily it should just be 1.
101
Const _usb_numconfigs = 1
102
103
'  *************************** Config Descriptor *****************************
104
105
'The number of interfaces for this device (Typically 1)
106
Const _usb_numifaces = 1
107
108
'Configuration Number (do not edit)
109
Const _usb_confignum = 1
110
111
'Index of UNICODE string descriptor that describes this config (0 = None)
112
Const _usb_iconfig = 2
113
114
'&H80 = device powered from USB bus.
115
'&HC0 = self-powered (has a power supply)
116
Const _usb_powered = &H80                                   '&HC0
117
118
'Required current in 2mA increments (500mA max)
119
Const _usb_maxpower = 150                                   '150 * 2mA = 300mA
120
121
'  ************************** Interface Descriptor ***************************
122
123
'Number of interfaces for this device (1 or 2)
124
Const _usb_ifaces = 1
125
126
'Interface number
127
Const _usb_ifaceaddr = 0
128
129
'Alternate index
130
Const _usb_alternate = 0
131
132
'Number of endpoints for this interface (excluding endp 0)
133
Const _usb_ifaceendpoints = 2
134
135
'USB Interface Class, subclass, and protocol (assigned by USB-IF).
136
'&h00 = RESERVED
137
'&hFF = Vendor-defined class (You must write your own PC driver)
138
'       Other values are USB interface device class. (such as HID)
139
'See http://www.usb.org/developers/defined_class  for more information
140
Const _usb_ifclass = 3
141
Const _usb_ifsubclass = 0
142
Const _usb_ifprotocol = 0
143
144
'Index to UNICODE string descriptor for this interface (0 = None)
145
Const _usb_iiface = 0
146
147
'  ************************* Optional HID Descriptor *************************
148
'HID class devices are things like keyboard, mouse, joystick.
149
'See http://www.usb.org/developers/hidpage/  for the specification,
150
'tools, and resources.
151
152
'Note that for a HID device, the device class, subclass, and protocol
153
'must be 0. The interface class must be 3 (HID).
154
'Interface subclass and protocol must be 0 unless you are making a
155
'keyboard or a mouse that supports the predefined boot protocol.
156
'See pages 8 and 9 of the HID 1.11 specification PDF.
157
158
'Number of HID descriptors (EXCLUDING report and physical)
159
'If you are not making a HID device, then set this constant to 0
160
Const _usb_hids = 1
161
162
'BCD HID releasenumber.  Current spec is 1.11
163
Const _usb_hid_release = &H0111
164
165
'Country code from page 23 of the HID 1.11 specifications.
166
'Usually the country code is 0 unless you are making a keyboard.
167
Const _usb_hid_country = 0
168
169
'The number of report and physical descriptors for this HID
170
'Must be at least 1! All HID devices have at least 1 report descriptor.
171
Const _usb_hid_numdescriptors = 1
172
173
'Use a software tool to create the report descriptor and $INCLUDE it.
174
175
176
'  ************************* Endpoint Descriptor(s) **************************
177
178
'Endpoint 0 is not included here.  These are only for optional
179
'endpoints.
180
'Note: HID devices require 1 interrupt IN endpoint
181
182
'Address of optional endpoints (Must be > 0. comment-out to not use)
183
Const _usb_endp2addr = 1
184
Const _usb_endp3addr = 2
185
186
'Valid types are 0 for control or 3 for interrupt
187
Const _usb_endp2type = 3
188
Const _usb_endp3type = 3
189
190
'Directions are: 0=Out, 1=In.  Ignored by control endpoints
191
Const _usb_endp2direction = 1
192
Const _usb_endp3direction = 0
193
194
'Polling interval (ms) for interrupt endpoints.  Ignored by control endpoints
195
' (Must be at least 10)
196
Const _usb_endp2interval = 200
197
Const _usb_endp3interval = 100
198
199
'*******************************************************************************
200
'The includes need to go right here--between the configuration constants above
201
'and the start of the program below.  The includes will automatically calculate
202
'constants based on the above configuration, dimension required variables, and
203
'allocate transmit and receive buffers.  Nothing inside the includes file needs
204
'to be modified.
205
$include "swusb-includes.bas"
206
'*******************************************************************************
207
208
'**************************** USB Interrupt And Init ***************************
209
'Set all the variables, flags, and sync bits to their initial states
210
Call Usb_reset()
211
212
213
214
Const _usb_intf = Intf0
215
Config Int0 = Rising
216
On Int0 Usb_isr Nosave
217
Enable Int0
218
Enable Interrupts
219
220
'*******************************************************************************
221
'*************************** End Of USB Configuration **************************
222
223
Dim Resetcounter As Word
224
Dim Idlemode As Byte
225
Dim Buttons_current As Byte
226
Dim Buttons_last As Byte
227
228
229
Config Portb = Output
230
Config Portc = Input
231
Dim Taste As Byte
232
Do
233
   Resetcounter = 0
234
235
   'Check for reset here
236
   While _usb_pin._usb_dminus = 0
237
      Incr Resetcounter
238
      If Resetcounter = 1000 Then
239
         Call Usb_reset()
240
      End If
241
   Wend
242
243
   'Check for received data
244
   If _usb_status._usb_rxc = 1 Then
245
'         Print Chr(_usb_rx_buffer(1)) ; Chr(_usb_rx_buffer(2)) ; Chr(_usb_rx_buffer(3)) ; Chr(_usb_rx_buffer(4)) ; Chr(_usb_rx_buffer(5)) ; Chr(_usb_rx_buffer(6))
246
      If _usb_status._usb_setup = 1 Then
247
         'Process a setup packet/Control message
248
         Call Usb_processsetup(_usb_tx_status)
249
      Elseif _usb_status._usb_endp1 = 1 Then
250
         ' Input data endpoints
251
         Portb = _usb_rx_buffer(2)                          ' Copy USB data in Portb
252
253
      End If
254
      'Reset the RXC bit and set the RTR bit (ready to receive a new packet)
255
      _usb_status._usb_rtr = 1
256
      _usb_status._usb_rxc = 0
257
   End If
258
259
260
      Taste.0 = Pind.6
261
      Taste.1 = Pind.5
262
      Taste.2 = Pind.4
263
      Taste.3 = Pind.3
264
265
   Buttons_current = Pinc
266
    'Queue data to be sent on endpoint 2 (HID report)
267
268
     If _usb_tx_status2._usb_txc = 1 Then
269
       _usb_tx_buffer2(2) = Taste
270
       Call Usb_send(_usb_tx_status2 , 1)                   ' Send data in PC
271
     End If
272
 '  End If
273
274
275
276
Loop
277
278
End
279
280
'*******************************************************************************
281
'******************** Descriptors stored in EEPROM or FLASH ********************
282
'                  Do not change the order of the descriptors!
283
284
   $data
285
286
287
'Device Descriptor
288
_usb_devicedescriptor:
289
Data 18 , 18 , _usb_desc_device , _usb_specl , _usb_spech , _usb_devclass
290
Data _usb_devsubclass , _usb_devprot , 8 , _usb_vidl , _usb_vidh , _usb_pidl
291
Data _usb_pidh , _usb_devrell , _usb_devrelh , _usb_imanufacturer
292
Data _usb_iproduct , _usb_iserial , _usb_numconfigs
293
294
295
'Retrieving the configuration descriptor also gets all the interface and
296
'endpoint descriptors for that configuration.  It is not possible to retrieve
297
'only an interface or only an endpoint descriptor.  Consequently, this is a
298
'large transaction of variable size.
299
_usb_configdescriptor:
300
Data _usb_descr_total , 9 , _usb_desc_config , _usb_descr_totall
301
Data _usb_descr_totalh , _usb_numifaces , _usb_confignum , _usb_iconfig
302
Data _usb_powered , _usb_maxpower
303
304
'_usb_IFaceDescriptor
305
Data 9 , _usb_desc_iface , _usb_ifaceaddr , _usb_alternate
306
Data _usb_ifaceendpoints , _usb_ifclass , _usb_ifsubclass , _usb_ifprotocol
307
Data _usb_iiface
308
309
#if _usb_hids > 0
310
'_usb_HIDDescriptor
311
Data _usb_hid_descr_len , _usb_desc_hid , _usb_hid_releasel , _usb_hid_releaseh
312
Data _usb_hid_country , _usb_hid_numdescriptors
313
314
'Next follows a list of bType and wLength bytes/words for each report and
315
'physical descriptor.  There must be at least 1 report descriptor.  In practice,
316
'There are usually 0 physical descriptors and only 1 report descriptor.
317
Data _usb_desc_report
318
Data 33 , 0
319
'End of report/physical descriptor list
320
#endif
321
322
#if _usb_endpoints > 1
323
'_usb_EndpointDescriptor
324
Data 7 , _usb_desc_endpoint , _usb_endp2attr , _usb_endp2type , 8 , 0
325
Data _usb_endp2interval
326
#endif
327
328
#if _usb_endpoints > 2
329
'_usb_EndpointDescriptor
330
Data 7 , _usb_desc_endpoint , _usb_endp3attr , _usb_endp3type , 8 , 0
331
Data _usb_endp3interval
332
#endif
333
334
#if _usb_hids > 0
335
_usb_hid_reportdescriptor:
336
Data 33                                                     ' Length = 33 bytes
337
Data &H06 , &H00 , &HFF                                     ' Usage_page(vendor Defined Page 1)
338
Data &H09 , &H01                                            ' Usage(vendor Usage 1)
339
Data &HA1 , &H02                                            ' Collection(logical)
340
341
Data &H09 , &H01                                            ' Usage(pointer)
342
Data &H15 , &H00                                            ' Logical_minimum(0)
343
Data &H25 , &HFF                                            ' Logical_maximum(255)
344
Data &H75 , &H08                                            ' Report_size(8)
345
Data &H95 , &H01                                            ' Report_count(1)
346
Data &H81 , &H02                                            ' Input(data , Var , Abs)
347
348
Data &H09 , &H01                                            '1                                           ' Usage(pointer)
349
Data &H15 , &H00                                            ' Logical_minimum(0)
350
Data &H26 , &HFF , 0                                        ' Logical_maximum(255)
351
Data &H75 , &H08                                            ' Report_size(8)
352
Data &H95 , &H01                                            '1                                           ' Report_count(1)
353
Data &H91 , &H02                                            ' Output(data , Var , Abs)
354
355
Data &HC0                                                   ' End_collection
356
357
#endif
358
359
'*****************************String descriptors********************************
360
'Yes, they MUST be written like "t","e","s","t".  Doing so pads them with
361
'0's.  If you write it like "test," I promise you it won't work.
362
363
'Default language descriptor (index 0)
364
_usb_langdescriptor:
365
Data 4 , 4 , _usb_desc_string , 09 , 04                     '&h0409 = English
366
367
'Manufacturer Descriptor (unicode)
368
_usb_mandescriptor:
369
Data 14 , 14 , _usb_desc_string
370
Data "M" , "o" , "s" , "e" , "l" , "0" , "1"
371
372
'Product Descriptor (unicode)
373
_usb_proddescriptor:
374
Data 44 , 44 , _usb_desc_string
375
Data "K" , "D" , "R" , " " , " "
376
Data "v" , "1" , "." , "0"
377
378
379
380
'*******************************************************************************
381
382
383
384
'*******************************************************************************
385
'******************************** Subroutines **********************************
386
'*******************************************************************************
387
388
Sub Usb_processsetup(txstate As Byte)
389
Senddescriptor = 0
390
391
   'Control transfers reset the sync bits like so
392
   Txstate = _usb_setup_sync
393
394
   'These are the standard device, interface, and endpoint requests that the
395
   'USB spec requires that we support.
396
   Select Case _usb_rx_buffer(2)
397
      'Standard Device Requests
398
      Case &B10000000:
399
         Select Case _usb_rx_buffer(3)
400
'            CASE _usb_REQ_GET_STATUS:
401
            Case _usb_req_get_descriptor:
402
               Select Case _usb_rx_buffer(5)
403
                  Case _usb_desc_device:
404
405
                     'Send the device descriptor
406
407
                        Restore _usb_devicedescriptor
408
409
                     Senddescriptor = 1
410
                  Case _usb_desc_config:
411
412
                     'Send the configuration descriptor
413
414
                        Restore _usb_configdescriptor
415
416
                     Senddescriptor = 1
417
                  Case _usb_desc_string:
418
                     Select Case _usb_rx_buffer(4)
419
                        Case 0:
420
                           'Send the language descriptor
421
422
                              Restore _usb_langdescriptor
423
424
                           Senddescriptor = 1
425
                        Case 1:
426
                           'Send the manufacturer descriptor
427
428
                              Restore _usb_mandescriptor
429
430
                           Senddescriptor = 1
431
                        Case 2:
432
                           'Send the product descriptor
433
434
                              Restore _usb_proddescriptor
435
436
                           Senddescriptor = 1
437
                     End Select
438
               End Select
439
440
         End Select
441
      Case &B00000000:
442
         Select Case _usb_rx_buffer(3)
443
444
            Case _usb_req_set_address:
445
446
               'USB status reporting for control writes
447
               Call Usb_send(txstate , 0)
448
               While Txstate._usb_txc = 0 : Wend
449
               'We are now addressed.
450
               _usb_deviceid = _usb_rx_buffer(4)
451
            Case _usb_req_set_config:
452
453
               'Have to do status reporting
454
               Call Usb_send(txstate , 0)
455
         End Select
456
      'Standard Interface Requests
457
      Case &B10000001:
458
         Select Case _usb_rx_buffer(3)
459
460
            Case _usb_req_get_descriptor
461
            '_usb_rx_buffer(4) is the descriptor index and (5) is the type
462
               Select Case _usb_rx_buffer(5)
463
                  Case _usb_desc_report:
464
465
466
                        Restore _usb_hid_reportdescriptor
467
468
                     Senddescriptor = 1
469
470
471
               End Select
472
         End Select
473
474
      Case &B00100001:
475
         'Class specific SET requests
476
         Select Case _usb_rx_buffer(3)
477
            'CASE _usb_REQ_SET_REPORT:
478
            Case _usb_req_set_idle:
479
               Idlemode = 1
480
               'Do status reporting
481
               Call Usb_send(txstate , 0)
482
            'CASE _usb_REQ_SET_PROTOCOL:
483
         End Select
484
   End Select
485
486
   If Senddescriptor = 1 Then
487
      Call Usb_senddescriptor(txstate , _usb_rx_buffer(8))
488
   End If
489
490
End Sub
491
492
Sub Usb_senddescriptor(txstate As Byte , Maxlen As Byte)
493
494
   'Break the descriptor into packets and send to TxState
495
   Local Size As Byte
496
   Local I As Byte
497
   Local J As Byte
498
   Local Timeout As Word
499
500
501
      Read Size
502
503
504
505
   If Maxlen < Size Then Size = Maxlen
506
507
   I = 2
508
   For J = 1 To Size
509
      Incr I
510
511
         Read Txstate(i)
512
513
      If I = 10 Or J = Size Then
514
         I = I - 2
515
         Call Usb_send(txstate , I)
516
         While Txstate._usb_txc = 0
517
            Timeout = 0
518
            'To prevent an infinite loop, check for reset here
519
            While _usb_pin._usb_dminus = 0
520
               Incr Timeout
521
               If Timeout = 1000 Then                       '
522
                  Call Usb_reset()
523
                  Exit Sub
524
               End If
525
            Wend
526
         Wend
527
         I = 2
528
      End If
529
   Next
530
End Sub
531
532
Sub Usb_send(txstate As Byte , Byval Count As Byte)
533
534
535
536
537
   'Calculates and adds the CRC16,adds the DATAx PID,
538
   'and signals to the ISR that the data is ready to be sent.
539
   '
540
   '"Count" is the DATA payload size.  Range is 0 to 8. Do not exceed 8!
541
542
   'Reset all the flags except TxSync and RxSync
543
   Txstate = Txstate And _usb_syncmask
544
'   Print Txstate
545
   'Calculate the 16-bit CRC
546
   _usb_crc = 0
547
   If Count <> 0 Then
548
      _usb_crc = Crc16uni(txstate(3) , Count , &HFFFF , &H8005 , 1 , 1)
549
      Toggle _usb_crc
550
   End If
551
552
   'Bytes to transmit will be PID + DATA payload + CRC16
553
   Count = Count + 3
554
   Txstate = Txstate + Count
555
556
   Txstate(count) = Low(_usb_crc)
557
   Incr Count
558
   Txstate(count) = High(_usb_crc)
559
560
561
   'Add the appropriate DATAx PID
562
   Txstate(2) = _usb_pid_data1
563
   If Txstate._usb_txsync = 0 Then
564
      Txstate(2) = _usb_pid_data0
565
   End If
566
567
   'The last step is to signal that the packet is Ready To Transmit
568
   Txstate._usb_rtt = 1
569
   Txstate._usb_txc = 0
570
End Sub
571
572
Sub Usb_reset()
573
574
   'Reset the receive flags
575
   _usb_status._usb_rtr = 1
576
   _usb_status._usb_rxc = 0
577
578
   'Reset the transmit flags
579
   _usb_tx_status = _usb_endp_init
580
   #if Varexist( "_usb_Endp2Addr")
581
   _usb_tx_status2 = _usb_endp_init
582
   #endif
583
   #if Varexist( "_usb_Endp3Addr")
584
   _usb_tx_status3 = _usb_endp_init
585
   #endif
586
587
   'Reset the device ID to 0
588
   _usb_deviceid = 0
589
590
591
   Idlemode = 0
592
593
End Sub

von Marc Rupprath (Gast)


Lesenswert?

Hallo;

meine Erfahrung aus meiner damaligen Diplomarbeit:

Ohne Kenntnisse der USB Spezifikation wirst du vermutlich 
Schwierigkeiten
haben.

Ich empfehle dir zu diesem Thema die Bücher aus dem Franzis Verlag.
Hier wurde die Spezifikation in den wichtigsten Punkten gut erläutert.


Um deine Frage zu beantworten:

Die Größe der Daten welches ein HID (Human Interface Device) überträgt 
wird in sog: Device Descriptoren beschrieben.

Ich habe Seinerzeit für meine USB Tastaturmaus wechselweise 4 oder 6 
Byte übertragen.

Ist jedoch schon 10 Jahre her: Nochmals der Hinweis:

Spezifikationen der USB Organisation und Oben beschriebene Literatur
Gruß

von Hannes (Gast)


Lesenswert?

Descriptoren das war das stichwort :) vielen dank jetzt klappt es
senden und empfangen von mehreren bytes :)

Beitrag #5745714 wurde von einem Moderator gelöscht.
Beitrag #5745720 wurde von einem Moderator gelöscht.
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.