Forum: Mikrocontroller und Digitale Elektronik Kommunikation vom Pc zum PIC18f4550/4553 mittels USB


von Alex (Gast)


Lesenswert?

Hallo
derzeit arbeite ich an einem Projekt, welches den PC mit einem 
PIC18f4553 mittels USB verbinden soll.

Dabei nutze ich unter Windows eine einfache Win32-Anwendung, in C++, um 
Zeichen zu empfangen und um Zeichen zu senden.

Der PIC wird in C und mit dem PCWHD Compiler Version 4.124 geflasht, 
außerdem wird ordnungsgemäß die Treibersoftware erkannt und es ist 
möglich mit dem Programm: USB View sämtliche Descritoren auszulesen.

Nun zu meinem Problem:
Die USB-Verbindung funktioniert nur einseitig, sprich ich kann vom PC 
aus senden, aber nichts am PC empfangen. Die Zeichen die gesendet 
werden, kommen im PIC richtig an.

Wenn ich aber vom PIC aus Zeichen senden will, kommt am PC nichts an. 
Ich habe auch schon versucht, mittels Sniffer-Programm, den Datenverkehr 
mitzuschneiden, aber leider war dieses erfolglos, da das Programm keinen 
ankommenden Datenstream anzeigte (Device Monitoring Studio 6.25).

Nun hoffe ich, dass mir irgendwer ein paar Tipps geben kann, damit der 
Datenverkehr auch in beide Richtungen funktioniert.

Danke schon mal im voraus. :)

Mit freundlichen Grüßen

Alex

Win32-Anwendung
Der Quellcode wurde von Microship verwendet und geändert.
Relevanteänderungen sind in der console.cpp in der Swtich-Anweisung 
gemacht worden (funktionen:writing(); reading(); dritter Codeblock)

MPUSBAPI Library Version 1.00
1
#ifndef _MPUSBAPI_H_
2
#define _MPUSBAPI_H_
3
4
#define  MPUSB_FAIL                  0
5
#define MPUSB_SUCCESS               1
6
7
#define MP_WRITE                    0
8
#define MP_READ                     1
9
10
// MAX_NUM_MPUSB_DEV is an abstract limitation.
11
// It is very unlikely that a computer system will have more
12
// then 127 USB devices attached to it. (single or multiple USB hosts)
13
#define MAX_NUM_MPUSB_DEV           127
14
15
DWORD (*MPUSBGetDLLVersion)(void);
16
17
DWORD (*MPUSBGetDeviceCount)(PCHAR pVID_PID);
18
19
HANDLE (*MPUSBOpen)(DWORD instance,         // Input
20
                 PCHAR pVID_PID,            // Input
21
                 PCHAR pEP,                 // Input
22
                 DWORD dwDir,               // Input
23
                 DWORD dwReserved);         // Input <Future Use>
24
25
DWORD (*MPUSBRead)(HANDLE handle,           // Input
26
                PVOID pData,                // Output
27
                DWORD dwLen,                // Input
28
                PDWORD pLength,             // Output
29
                DWORD dwMilliseconds);      // Input
30
31
DWORD (*MPUSBWrite)(HANDLE handle,          // Input
32
                 PVOID pData,               // Input
33
                 DWORD dwLen,               // Input
34
                 PDWORD pLength,            // Output
35
                 DWORD dwMilliseconds);     // Input
36
37
DWORD (*MPUSBReadInt)(HANDLE handle,        // Input
38
                   PVOID pData,             // Output
39
                   DWORD dwLen,             // Input
40
                   PDWORD pLength,          // Output
41
                   DWORD dwMilliseconds);   // Input
42
43
BOOL (*MPUSBClose)(HANDLE handle);
44
45
#endif


console.cpp
1
#include <stdio.h>
2
#include <windows.h>
3
#include <iostream>
4
5
using namespace std;
6
7
#pragma hdrstop
8
9
#include "mpusbapi.h"                   // MPUSBAPI Header File
10
11
//---------------------------------------------------------------------------
12
#pragma argsused
13
14
// Global Vars
15
char vid_pid[]= "vid_04d8&pid_000c";    // Default Demo Application Firmware
16
char out_pipe[]= "\\MCHP_EP1";
17
char in_pipe[]= "\\MCHP_EP1";
18
19
DWORD temp;
20
21
HINSTANCE libHandle;
22
HANDLE myOutPipe;
23
HANDLE myInPipe;
24
25
//---------------------------------------------------------------------------
26
// Prototypes
27
void GetSummary(void);
28
void LoadDLL(void);
29
void GetUSBDemoFWVersion(void);
30
DWORD SendReceivePacket(BYTE *SendData, DWORD SendLength, BYTE *ReceiveData,
31
                    DWORD *ReceiveLength, UINT SendDelay, UINT ReceiveDelay);
32
void CheckInvalidHandle(void);
33
34
35
36
int reading(void);
37
int writing(void);
38
//---------------------------------------------------------------------------
39
40
int main(int argc, char* argv[])
41
{
42
    BOOLEAN bQuit;
43
    DWORD selection;
44
    bQuit = false;
45
  int x11,x12;
46
    // Load DLL when it is necessary, i.e. on start-up!
47
    LoadDLL();
48
49
    // Always a good idea to initialize the handles
50
    myOutPipe= INVALID_HANDLE_VALUE;
51
  myInPipe = INVALID_HANDLE_VALUE;
52
53
54
    printf("Microchip Technology Inc., 2004\r\n");
55
    printf("===============================\r\n");
56
    while(!bQuit)
57
    {
58
        printf("Select an option\r\n");
59
        printf("[1] Get MPUSBAPI Version\r\n");
60
        printf("[2] Summarize Instances\r\n");
61
        printf("[3] Get USB Demo Firmware Version\r\n");
62
        printf("[4] Daten schreiben\r\n");
63
    printf("[5] Daten lesen\r\n");
64
    printf("[6] Quit\r\n>>");
65
        scanf("%d",&selection);
66
    
67
        switch(selection)
68
        {
69
            case 1:
70
                temp = MPUSBGetDLLVersion();
71
                printf("MPUSBAPI Version: %d.%d\r\n",x11=HIWORD(temp),x12=LOWORD(temp));
72
                break;
73
            case 2:
74
                GetSummary();
75
                break;
76
            case 3:
77
                GetUSBDemoFWVersion();
78
                break;
79
      case 4:
80
                writing();
81
                break;
82
            case 5:
83
                reading();
84
                break;
85
      case 6:
86
                bQuit = true;
87
                break;
88
89
            default:
90
                break;
91
        }// end switch
92
93
        fflush(stdin);printf("\r\n");
94
    }//end while
95
96
    // Always check to close all handles before exiting!
97
    if (myOutPipe != INVALID_HANDLE_VALUE) MPUSBClose(myOutPipe);
98
    if (myInPipe != INVALID_HANDLE_VALUE) MPUSBClose(myInPipe);
99
    myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
100
101
    // Always check to close the library too.
102
    if (libHandle != NULL) FreeLibrary(libHandle);
103
104
    return 0;
105
}//end main
106
107
//---------------------------------------------------------------------------
108
109
void GetSummary(void)
110
{
111
    HANDLE tempPipe = INVALID_HANDLE_VALUE;
112
    DWORD count = 0;
113
    DWORD max_count;
114
115
    max_count = MPUSBGetDeviceCount(vid_pid);
116
117
    printf("\r\n%d device(s) with %s currently attached\r\n",max_count,vid_pid);
118
119
    // Note:
120
    // The total number of devices using the generic driver could be
121
    // bigger than max_count. They could have different vid & pid numbers.
122
    // This means if max_count is 2, the valid instance index do not
123
    // necessary have to be '0' and '1'.
124
    //
125
    // Below is a sample code for searching for all valid instance indexes.
126
    // MAX_NUM_MPUSB_DEV is defined in _mpusbapi.h
127
128
    count = 0;
129
    for(int i = 0; i < MAX_NUM_MPUSB_DEV; i++)
130
    {
131
        tempPipe = MPUSBOpen(i,vid_pid,NULL,MP_READ,0);
132
        if(tempPipe != INVALID_HANDLE_VALUE)
133
        {
134
            printf("Instance Index # %d\r\n",i);
135
            MPUSBClose(tempPipe);
136
            count++;
137
        }
138
        if(count == max_count) break;
139
    }//end for
140
    printf("\r\n");
141
}//end GetSummary
142
143
//---------------------------------------------------------------------------
144
145
void LoadDLL(void)
146
{
147
    libHandle = NULL;
148
  
149
  libHandle = LoadLibrary(TEXT("mpusbapi.dll"));
150
    if(libHandle == NULL)
151
    {
152
        printf("Error loading mpusbapi.dll\r\n");
153
    }
154
    else
155
    {
156
        MPUSBGetDLLVersion=(DWORD(*)(void))\
157
                    GetProcAddress(libHandle,"_MPUSBGetDLLVersion");
158
        MPUSBGetDeviceCount=(DWORD(*)(PCHAR))\
159
                    GetProcAddress(libHandle,"_MPUSBGetDeviceCount");
160
        MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))\
161
                    GetProcAddress(libHandle,"_MPUSBOpen");
162
        MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
163
                    GetProcAddress(libHandle,"_MPUSBWrite");
164
        MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
165
                    GetProcAddress(libHandle,"_MPUSBRead");
166
        MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
167
                    GetProcAddress(libHandle,"_MPUSBReadInt");
168
        MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(libHandle,"_MPUSBClose");
169
170
        if((MPUSBGetDeviceCount == NULL) || (MPUSBOpen == NULL) ||
171
            (MPUSBWrite == NULL) || (MPUSBRead == NULL) ||
172
            (MPUSBClose == NULL) || (MPUSBGetDLLVersion == NULL) ||
173
            (MPUSBReadInt == NULL))
174
            printf("GetProcAddress Error\r\n");
175
    }//end if else
176
}//end LoadDLL
177
178
179
void GetUSBDemoFWVersion(void)
180
{
181
    // First we need to open data pipes...
182
    DWORD selection;
183
    fflush(stdin);
184
    printf("Enter a valid instance index to open a USB connection: ");
185
    scanf("%d",&selection);
186
187
    myOutPipe = MPUSBOpen(selection,vid_pid,out_pipe,MP_WRITE,0);
188
    myInPipe = MPUSBOpen(selection,vid_pid,NULL,MP_READ,0);//out_pipe,MP_READ,0);
189
    if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
190
    {
191
        printf("Failed to open data pipes.\r\n");
192
        return;
193
    }//end if
194
195
196
    // The default demo firmware application has a defined application
197
    // level protocol.
198
    // To read the firmware version, the host must send the READ_VERSION
199
    // command which is defined as 0x00, follows by the length of the
200
    // expected result, in this case is 2 bytes, major version, and minor
201
    // version.
202
    // i.e. <READ_VERSION><0x02>
203
    //
204
    // The response packet from the board has the following format:
205
    // <READ_VERSION><0x02><MINOR_VER><MAJOR_VER>
206
207
    // The receive buffer size must be equal to or larger than the maximum
208
    // endpoint size it is communicating with. In this case, 64 bytes.
209
210
    BYTE send_buf[64],receive_buf[64];
211
    DWORD RecvLength=4;
212
213
    #define READ_VERSION    0
214
    send_buf[0] = READ_VERSION;      // Command
215
    send_buf[1] = 0x02;              // Expected length of the result
216
217
    if(SendReceivePacket(send_buf,2,receive_buf,&RecvLength,1000,1000) == 1)
218
    {
219
        if(RecvLength == 4 && receive_buf[0] == READ_VERSION &&
220
            receive_buf[1] == 0x02)
221
        {
222
            printf("\nUSB Demo Firmware Version %d.%d\r\n",receive_buf[3],
223
                    receive_buf[2]);
224
        }
225
    }
226
    else
227
        printf("USB Operation Failed\r\n");
228
229
    // Let's close the data pipes since we have nothing left to do..
230
    MPUSBClose(myOutPipe);
231
    MPUSBClose(myInPipe);
232
    myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
233
    
234
}//end GetUSBDemoFWVersion
235
236
//---------------------------------------------------------------------------
237
238
/////////////////////////////////////////////////////////////////////////////
239
//
240
// A typical application would send a command to the target device and expect
241
// a response.
242
// SendReceivePacket is a wrapper function that facilitates the
243
// send command / read response paradigm
244
//
245
// SendData - pointer to data to be sent
246
// SendLength - length of data to be sent
247
// ReceiveData - Points to the buffer that receives the data read from the call
248
// ReceiveLength - Points to the number of bytes read
249
// SendDelay - time-out value for MPUSBWrite operation in milliseconds
250
// ReceiveDelay - time-out value for MPUSBRead operation in milliseconds
251
//
252
253
DWORD SendReceivePacket(BYTE *SendData, DWORD SendLength, BYTE *ReceiveData,
254
                    DWORD *ReceiveLength, UINT SendDelay, UINT ReceiveDelay)
255
{
256
    DWORD SentDataLength;
257
    DWORD ExpectedReceiveLength = *ReceiveLength;
258
259
    if(myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE)
260
    {
261
        if(MPUSBWrite(myOutPipe,SendData,SendLength,&SentDataLength,SendDelay))
262
        {
263
264
            if(MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength,
265
                        ReceiveLength,ReceiveDelay))
266
            {
267
                if(*ReceiveLength == ExpectedReceiveLength)
268
                {
269
                    return 1;   // Success!
270
                }
271
                else if(*ReceiveLength < ExpectedReceiveLength)
272
                {
273
                    return 2;   // Partially failed, incorrect receive length
274
                }//end if else
275
            }
276
            else
277
                CheckInvalidHandle();
278
        }
279
        else
280
            CheckInvalidHandle();
281
    }//end if
282
283
    return 0;  // Operation Failed
284
}//end SendReceivePacket
285
286
//---------------------------------------------------------------------------
287
288
void CheckInvalidHandle(void)
289
{
290
    if(GetLastError() == ERROR_INVALID_HANDLE)
291
    {
292
        // Most likely cause of the error is the board was disconnected.
293
        MPUSBClose(myOutPipe);
294
        MPUSBClose(myInPipe);
295
        myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
296
    }//end if
297
    else
298
        printf("Error Code \r\n",GetLastError());
299
}//end CheckInvalidHandle


writing(); reading()
1
//---------------------------------------------------------------------------
2
int writing(void)
3
{
4
  BYTE *SendData;
5
  DWORD SendLength=1; 
6
  BYTE *ReceiveData;
7
    DWORD ReceiveLength=1;
8
  UINT SendDelay=0;
9
  UINT ReceiveDelay=100;
10
  DWORD ExpectedReceiveLength=1;
11
  ReceiveData=0;
12
13
  DWORD SentDataLength;
14
15
  
16
  
17
  BYTE daten1[]="0";
18
  BYTE daten2[]="8";
19
  char daten[2]="7";
20
21
  myOutPipe=MPUSBOpen(0,vid_pid,out_pipe,MP_WRITE,0);
22
  myInPipe = MPUSBOpen(0,vid_pid,in_pipe,MP_READ,0);
23
24
  //SendReceivePacket(daten1, SendLength, daten2, &ReceiveLength, 1, 0);
25
  while(1)
26
  {
27
    std::cin>>daten1;
28
    MPUSBWrite(myOutPipe,daten1,1,&SentDataLength,0);
29
  if(daten1[0]==daten2[0])
30
  {
31
    break;
32
  }
33
  Sleep(150);
34
  }
35
  //MPUSBRead(myInPipe,daten,1,1,0);
36
  //MPUSBRead(myInPipe,daten, ExpectedReceiveLength,1000,1000);
37
  
38
  int temp=MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength,&ReceiveLength,ReceiveDelay);
39
  if(temp!=0)
40
  {
41
    cout<<"\n\n"<<ReceiveData<<"\n\n";
42
  }
43
44
  MPUSBClose(myOutPipe);
45
  MPUSBClose(myInPipe);
46
47
  return 0;
48
}
49
50
int reading(void)
51
{
52
  BYTE *SendData;
53
  DWORD SendLength=1; 
54
  BYTE *ReceiveData;
55
    DWORD ReceiveLength=1;
56
  UINT SendDelay=0;
57
  UINT ReceiveDelay=100;
58
  DWORD ExpectedReceiveLength=1;
59
  ReceiveData=0;
60
  BYTE test[10];
61
  DWORD SentDataLength;
62
  ReceiveData=&test[0];
63
  BYTE daten1[]="0";
64
  BYTE daten2[]="8";
65
  char daten[2]="7";
66
67
  myOutPipe=MPUSBOpen(0,vid_pid,out_pipe,MP_WRITE,0);
68
  myInPipe = MPUSBOpen(0,vid_pid,in_pipe,MP_READ,0);
69
  int temp=0;
70
  while(1)
71
  {
72
    temp=MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength,&ReceiveLength,ReceiveDelay);
73
    if(temp!=0)
74
    {
75
      cout<<"\n\n"<<ReceiveData<<"\n\n";
76
    }
77
  }
78
79
  MPUSBClose(myOutPipe);
80
  MPUSBClose(myInPipe);
81
82
83
  return 0;
84
}
85
86
//---------------------------------------------------------------------------

//---------------------------------------------------------------------- 
-

Controllerprogramm

main.c
1
#include <main.h>
2
3
#include <pic18_usb.h>
4
#include <usb_desc_scope2.h>
5
#include <usb.c>
6
7
int daten2[1]=0x30;
8
#int_RDA
9
void RDA_isr(void) 
10
{
11
   int Daten=RCREG;            //Daten  auslesen und zwischenspeichern
12
   TXREG=RCREG+1;              //Daten um eins erhöhen und zurück senden
13
14
   output_toggle(PIN_A1);
15
   
16
   usb_put_packet(0x81,daten2,1,USB_DTS_TOGGLE); 
17
        // usb_puts(0x82,daten2,1,500); 
18
   output_toggle(PIN_A0);
19
}
20
21
void main()
22
{   
23
SPBRG=25;               //9600Baud //EUSART Baud Rate Generator Register Low Byte
24
25
   int temp;
26
   int daten1[1]=0;
27
28
   delay_ms(250);
29
   output_high(PIN_D1);
30
31
   usb_init();
32
   usb_wait_for_enumeration();
33
34
   output_low(PIN_D1);
35
36
   setup_adc_ports(NO_ANALOGS);
37
38
   enable_interrupts(INT_RDA);
39
40
   enable_interrupts(GLOBAL);
41
42
   delay_ms(250);
43
   
44
   while(true)
45
   {
46
      output_toggle(PIN_D0);              //blinkende LED zeigt Aktivität
47
      usb_put_packet(0x81,daten2,1,USB_DTS_TOGGLE);
48
      delay_ms(333);
49
      
50
      if(usb_kbhit(1)==TRUE)              //Daten wurden empfangen
51
      {
52
         output_toggle(PIN_A0);           //LED blinkt 
53
         usb_get_packet(0x01,daten1,1);   //wurde mittels UART und Debugmodus kontrolliert 
54
      }
55
   }
56
}


main.h
1
#include <18F4553.h>
2
#device adc=16
3
#device ICD=TRUE
4
5
#FUSES NOWDT                    //No Watch Dog Timer
6
#FUSES PLL2                     //1.00   PLL1   No PLL PreScaler
7
#FUSES CPUDIV4                  //CPUDIV2   System Clock by 2
8
#FUSES XTPLL                    //XTPLL   Crystal/Resonator with PLL enabled
9
#FUSES NOBROWNOUT               //No brownout reset
10
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
11
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
12
#FUSES VREGEN                   //USB voltage regulator enabled      
13
#FUSES NOUSBDIV
14
15
#FUSES HSPLL
16
#FUSES ECPLL
17
#FUSES EC_IO
18
#FUSES VREGEN
19
20
#use delay(crystal=8000000,clock=8000000)
21
22
#use FIXED_IO( A_outputs=PIN_A3,PIN_A2,PIN_A1,PIN_A0 )
23
24
#use FIXED_IO( D_outputs=PIN_D3,PIN_D2,PIN_D1,PIN_D0 )
25
26
27
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
28
//funktioniert nur wenn man danach nochmal die Baudrate per hand setzt
29
//sonst wird diese doppelt so groß gesetzt
30
31
//++++++++++RS-232-Start++++++++++//
32
#byte RCSTA   =  0xFAB           //RECEIVE STATUS AND CONTROL REGISTER
33
#byte TXSTA   =  0xFAC           //TRANSMIT STATUS AND CONTROL REGISTER
34
#byte TXREG   =  0xFAD           //EUSART Transmit Register
35
#byte RCREG   =  0xFAE           //EUSART Receive Register
36
#byte SPBRG   =  0xFAF           //EUSART Baud Rate Generator Register Low Byte
37
#byte SPBRGH  =  0xFB0           //EUSART Baud Rate Generator Register High Byte
38
#byte BAUDCON =  0xFB8           //BAUD RATE CONTROL REGISTER
39
40
41
//#byte RCSTA   =  0xFAB         //RECEIVE STATUS AND CONTROL REGISTER
42
43
#bit RX9D     =  RCSTA.0         //9th Bit of Received Data 
44
#bit OERR     =  RCSTA.1         //Overrun Error bit 
45
#bit FERR     =  RCSTA.2         //Framing Error bit 
46
#bit ADDEN    =  RCSTA.3         //Address Detect Enable bit
47
#bit CREN     =  RCSTA.4         //Continuous Receive Enable bit
48
#bit SREN     =  RCSTA.5         //Single Receive Enable bit 
49
#bit RX9      =  RCSTA.6         //9-Bit Receive Enable bit 
50
#bit SPEN     =  RCSTA.7         //Serial Port Enable bit 
51
52
//#byte TXSTA   =  0xFAC         //TRANSMIT STATUS AND CONTROL REGISTER
53
54
#bit TX9D     =  TXSTA.0         //9th Bit of Transmit Data 
55
#bit TRMT     =  TXSTA.1         //Transmit Shift Register Status bit 
56
#bit BRGH     =  TXSTA.2         //High Baud Rate Select bit
57
#bit SENDB    =  TXSTA.3         //Send Break Character bit 
58
#bit SYNC     =  TXSTA.4         //EUSART Mode Select bit 
59
#bit TXEN     =  TXSTA.5         //Transmit Enable bit 
60
#bit TX9      =  TXSTA.6         //9-Bit Transmit Enable bit 
61
#bit CSRC     =  TXSTA.7         //Clock Source Select bit
62
63
//#byte BAUDCON =  0xFB8         //BAUD RATE CONTROL REGISTER
64
65
#bit ABDEN    =  BAUDCON.0       //Auto-Baud Detect Enable bit 
66
#bit WUE      =  BAUDCON.1       //Wake-up Enable bit 
67
#bit BRG16    =  BAUDCON.3       //16-Bit Baud Rate Register Enable bit 
68
#bit TXCKP    =  BAUDCON.4       //Clock and Data Polarity Select bit 
69
#bit RXDTP    =  BAUDCON.5       //Data/Receive Polarity Select bit 
70
#bit RCIDL    =  BAUDCON.6       //Receive Operation Idle Status bit 
71
#bit ABDOVF   =  BAUDCON.7       //Auto-Baud Acquisition Rollover Status bit 
72
73
//++++++++++RS-232-Ende++++++++++//




usb_desc_scope2.h   geänderte Decriptoren Quelle:Microship
1
///////////////////////////////////////////////////////////////////////////
2
////                         usb_desc_scope.h                          ////
3
////                                                                   ////
4
//// An example set of device / configuration descriptors for use with ////
5
//// the USB Bulk demo (see ex_usb_scope.c)                            ////
6
////                                                                   ////
7
//// This file is part of CCS's PIC USB driver code.  See USB.H        ////
8
//// for more documentation and a list of examples.                    ////
9
////                                                                   ////
10
///////////////////////////////////////////////////////////////////////////
11
////                                                                   ////
12
//// Version History:                                                  ////
13
////                                                                   ////
14
//// March 5th, 2009:                                                  ////
15
////   Cleanup for Wizard.                                             ////
16
////   PIC24 Initial release.                                          ////
17
////                                                                   ////
18
//// July 13th, 2005:                                                  ////
19
////   Endpoint descriptor works if USB_EP1_TX_SIZE is 16bits          ////
20
////   Endpoint descriptor works if USB_EP1_RX_SIZE is 16bits          ////
21
////                                                                   ////
22
//// June 20th, 2005:                                                  ////
23
////   18fxx5x Initial release.                                        ////
24
////                                                                   ////
25
//// March 21st, 2005:                                                 ////
26
////   EP 0x01 and EP 0x81 now use USB_EP1_TX_SIZE and USB_EP1_RX_SIZE ////
27
////      to define max packet size, to make it easier for dynamically ////
28
////      changed code.                                                ////
29
////                                                                   ////
30
//// June 24th, 2002: Cleanup                                          ////
31
////                                                                   ////
32
//// May 6th, 2003: Fixed non-HID descriptors pointing to faulty       ////
33
////                strings                                            ////
34
////                                                                   ////
35
//// August 2nd, 2002: Initial Public Release                          ////
36
////                                                                   ////
37
////                                                                   ////
38
///////////////////////////////////////////////////////////////////////////
39
////        (C) Copyright 1996,2009 Custom Computer Services           ////
40
//// This source code may only be used by licensed users of the CCS    ////
41
//// C compiler.  This source code may only be distributed to other    ////
42
//// licensed users of the CCS C compiler.  No other use,              ////
43
//// reproduction or distribution is permitted without written         ////
44
//// permission.  Derivative programs created using this software      ////
45
//// in object code form are not restricted in any way.                ////
46
///////////////////////////////////////////////////////////////////////////
47
48
#IFNDEF __USB_DESCRIPTORS__
49
#DEFINE __USB_DESCRIPTORS__
50
51
///////// config options, although it's best to leave alone for this demo /////
52
#define  USB_CONFIG_PID       0x000c//0x0003   //changing this value may make the driver incompatible
53
#define  USB_CONFIG_VID       0x04d8//0x0461   //changing this value may make the driver incompatible
54
#define  USB_CONFIG_BUS_POWER 250   //100mA  (range is 0..500)
55
#define  USB_CONFIG_VERSION   0x0100      //01.00  //range is 00.00 to 99.99
56
//////// end config ///////////////////////////////////////////////////////////
57
58
#DEFINE USB_HID_DEVICE     FALSE
59
#define USB_EP2_TX_ENABLE  USB_ENABLE_BULK  //turn on EP1 for IN bulk/interrupt transfers
60
#define USB_EP1_RX_ENABLE  USB_ENABLE_BULK  //turn on EP1 for OUT bulk/interrupt transfers
61
62
#ifndef USB_EP2_TX_SIZE
63
#define USB_EP2_TX_SIZE    64  //size to allocate for the tx endpoint 1 buffer
64
#endif
65
66
#ifndef USB_EP1_RX_SIZE
67
#define USB_EP1_RX_SIZE    8   //size to allocate for the rx endpoint 1 buffer
68
#endif
69
70
#include <usb.h>
71
72
//////////////////////////////////////////////////////////////////
73
///
74
///   start config descriptor
75
///   right now we only support one configuration descriptor.
76
///   the config, interface, class, and endpoint goes into this array.
77
///
78
//////////////////////////////////////////////////////////////////
79
80
   #DEFINE USB_TOTAL_CONFIG_LEN      32 //config+interface+class+endpoint
81
82
   //configuration descriptor
83
   char const USB_CONFIG_DESC[] = {
84
   //config_descriptor for config index 1
85
         USB_DESC_CONFIG_LEN,     //length of descriptor size
86
         USB_DESC_CONFIG_TYPE,         //constant CONFIGURATION (0x02)
87
         USB_TOTAL_CONFIG_LEN,0,  //size of all data returned for this config
88
         1,      //number of interfaces this device supports
89
         0x01,                //identifier for this configuration.  (IF we had more than one configurations)
90
         0x00,                //index of string descriptor for this configuration
91
        #if USB_CONFIG_BUS_POWER
92
         0x80, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't), bits 0-4 unused and bit7=1         ==7
93
        #else
94
         0xC0, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't), bits 0-4 unused and bit7=1         ==7
95
        #endif
96
         USB_CONFIG_BUS_POWER/2, //maximum bus power required (maximum milliamperes/2)  (0x32 = 100mA)   ==8
97
98
   //interface descriptor 0 alt 0
99
         USB_DESC_INTERFACE_LEN,  //length of descriptor
100
         USB_DESC_INTERFACE_TYPE,      //constant INTERFACE (0x04)
101
         0x00,                //number defining this interface (IF we had more than one interface)
102
         0x00,                //alternate setting
103
         2,       //number of endpoints, not counting endpoint 0.
104
         0xFF,                //class code, FF = vendor defined
105
         0xFF,                //subclass code, FF = vendor
106
         0xFF,                //protocol code, FF = vendor
107
         0x00,                //index of string descriptor for interface
108
109
   //endpoint descriptor
110
         USB_DESC_ENDPOINT_LEN, //length of descriptor
111
         USB_DESC_ENDPOINT_TYPE,     //constant ENDPOINT (0x05)
112
         0x81//0x81,              //endpoint number and direction (0x81 = EP1 IN)
113
         USB_EP2_TX_ENABLE,              //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt)
114
         USB_EP2_TX_SIZE & 0xFF,USB_EP11_TX_SIZE >> 8,         //maximum packet size supported
115
         0x01,              //polling interval in ms. (for interrupt transfers ONLY)
116
117
   //endpoint descriptor
118
         USB_DESC_ENDPOINT_LEN, //length of descriptor
119
         USB_DESC_ENDPOINT_TYPE,     //constant ENDPOINT (0x05)
120
         0x01//0x01,              //endpoint number and direction (0x01 = EP1 OUT)
121
         USB_EP1_RX_ENABLE,              //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt)
122
         USB_EP1_RX_SIZE & 0xFF,USB_EP1_RX_SIZE >> 8,         //maximum packet size supported
123
         0x01,              //polling interval in ms. (for interrupt transfers ONLY)
124
  };
125
126
   //****** BEGIN CONFIG DESCRIPTOR LOOKUP TABLES ********
127
   //since we can't make pointers to constants in certain pic16s, this is an offset table to find
128
   //  a specific descriptor in the above table.
129
130
   //NOTE: DO TO A LIMITATION OF THE CCS CODE, ALL HID INTERFACES MUST START AT 0 AND BE SEQUENTIAL
131
   //      FOR EXAMPLE, IF YOU HAVE 2 HID INTERFACES THEY MUST BE INTERFACE 0 AND INTERFACE 1
132
   #define USB_NUM_HID_INTERFACES   0
133
134
   //the maximum number of interfaces seen on any config
135
   //for example, if config 1 has 1 interface and config 2 has 2 interfaces you must define this as 2
136
   #define USB_MAX_NUM_INTERFACES   1
137
138
   //define how many interfaces there are per config.  [0] is the first config, etc.
139
   const char USB_NUM_INTERFACES[USB_NUM_CONFIGURATIONS]={1};
140
141
   #if (sizeof(USB_CONFIG_DESC) != USB_TOTAL_CONFIG_LEN)
142
      #error USB_TOTAL_CONFIG_LEN not defined correctly
143
   #endif
144
145
146
//////////////////////////////////////////////////////////////////
147
///
148
///   start device descriptors
149
///
150
//////////////////////////////////////////////////////////////////
151
152
   //device descriptor
153
   char const USB_DEVICE_DESC[] ={
154
         USB_DESC_DEVICE_LEN,          //the length of this report
155
         0x01,                //constant DEVICE (0x01)
156
         0x10,0x01,           //usb version in bcd
157
         0x00,                //class code (if 0, interface defines class.  FF is vendor defined)
158
         0x00,                //subclass code
159
         0x00,                //protocol code
160
         USB_MAX_EP0_PACKET_LENGTH,   //max packet size for endpoint 0. (SLOW SPEED SPECIFIES 8)
161
         USB_CONFIG_VID & 0xFF, ((USB_CONFIG_VID >> 8) & 0xFF), //vendor id       ==9, 10
162
         USB_CONFIG_PID & 0xFF, ((USB_CONFIG_PID >> 8) & 0xFF), //product id, don't use 0xffff       ==11, 12
163
         USB_CONFIG_VERSION & 0xFF, ((USB_CONFIG_VERSION >> 8) & 0xFF), //device release number  ==13,14
164
         0x01,                //index of string description of manufacturer. therefore we point to string_1 array (see below)
165
         0x02,                //index of string descriptor of the product
166
         0x00,                //index of string descriptor of serial number
167
         USB_NUM_CONFIGURATIONS   //number of possible configurations
168
   };
169
170
//////////////////////////////////////////////////////////////////
171
///
172
///   start string descriptors
173
///   String 0 is a special language string, and must be defined.  People in U.S.A. can leave this alone.
174
///
175
///   You must define the length else get_next_string_character() will not see the string
176
///   Current code only supports 10 strings (0 thru 9)
177
///
178
//////////////////////////////////////////////////////////////////
179
180
//the offset of the starting location of each string.
181
//offset[0] is the start of string 0, offset[1] is the start of string 1, etc.
182
const char USB_STRING_DESC_OFFSET[]={0,4,12};
183
184
#define USB_STRING_DESC_COUNT sizeof(USB_STRING_DESC_OFFSET)
185
186
// Here is where the "CCS" Manufacturer string and "CCS Bulk Demo" are stored.
187
// Strings are saved as unicode.
188
// These strings are mostly only displayed during the add hardware wizard.
189
// Once the operating system drivers have been installed it will usually display
190
// the name from the drivers .INF.
191
char const USB_STRING_DESC[]={
192
   //string 0
193
         4, //length of string index
194
         USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
195
         0x09,0x04,   //Microsoft Defined for US-English
196
   //string 1
197
         8, //length of string index
198
         USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
199
         'C',0,
200
         'C',0,
201
         'S',0,
202
   //string 2
203
         28, //length of string index
204
         USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
205
         '1',0,
206
         '3',0,
207
         '3',0,
208
         '7',0,
209
         ' ',0,
210
         't',0,
211
         'e',0,
212
         's',0,
213
         't',0,
214
         ' ',0,
215
         ':',0,
216
         ')',0,
217
         ' ',0
218
};
219
220
#ENDIF

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.