Hallo Ich versuche gerade eine USB-Kommunikation mit einem PIC18F2550 zm laufen zu bringen. PIC seitig basierend auf mit dem Microchip USB-Stack v.2.3a. Auf dem PC verwende ich LibUSB-Win32 (libusb-win32-device-bin-0.1.12.1). Compiliert wird mit C18 für den PIC und mit MinGW für die PC-Anwendung. Die Zeile bytesread1=usb_bulk_read(dev, 0x81, (char*)&tmp1[0], 64, 5000); empfängt die richtige Anzahl von Bytes. Leider sind die "empfangenen?" Daten in tmp1 immer null. Gesendet wird der Wert 70. Vielleicht hat jemand eine Idee? Hier der Output
1 | Warte auf Taste 's' |
2 | s
|
3 | Taste 's' gedr³ckt |
4 | Geschrieben s |
5 | |
6 | : Gelesen EP1 : 000000 : Bytes = 6 |
7 | |
8 | Gelesen EP1 : 000 : Bytes = 3 |
9 | |
10 | Gelesen EP1 : 000 : Bytes = 3 |
11 | |
12 | Gelesen EP1 : 000 : Bytes = 3 |
13 | |
14 | Gelesen EP1 : 000 : Bytes = 3 |
15 | |
16 | Gelesen EP1 : 000 : Bytes = 3 |
17 | |
18 | Gelesen EP1 : 000 : Bytes = 3 |
19 | |
20 | Gelesen EP1 : 000 : Bytes = 3 |
21 | |
22 | Gelesen EP1 : 000 : Bytes = 3 |
23 | |
24 | |
25 | ENDE
|
Hier der Quellcode der Windowsanwendung und ein Auszug aus der Firmware.
1 | |
2 | #include <windows.h> |
3 | #include <process.h> /* _beginthread, _endthread */ |
4 | #include <stddef.h> |
5 | #include <stdlib.h> |
6 | #include <conio.h> |
7 | #include <E:\Projekte\Kamera_Sonde\Kameramodul_Pic_USB\libusb-win32-device-bin-0.1.12.1\include\usb.h> |
8 | |
9 | |
10 | #define USB_DEBUG 2
|
11 | |
12 | /* the device's vendor and product id */
|
13 | #define MY_VID 0x1234
|
14 | #define MY_PID 0x5678
|
15 | |
16 | /* the device's endpoints */
|
17 | #define EP1_IN 0x81
|
18 | #define EP1_OUT 0x01
|
19 | #define EP2_IN 0x82
|
20 | #define EP2_OUT 0x02
|
21 | |
22 | |
23 | #define BUF_SIZE (64)
|
24 | |
25 | usb_dev_handle *dev = NULL; /* the device handle */ |
26 | |
27 | char tmp[BUF_SIZE]; |
28 | char tmp1[BUF_SIZE]; |
29 | char tmp2[BUF_SIZE]; |
30 | |
31 | void *context1 = NULL; |
32 | void *context2 = NULL; |
33 | |
34 | int bytesread1; |
35 | int bytesread2; |
36 | |
37 | usb_dev_handle *open_dev(void); |
38 | |
39 | |
40 | usb_dev_handle *open_dev(void) |
41 | {
|
42 | struct usb_bus *bus; |
43 | struct usb_device *dev; |
44 | |
45 | for(bus = usb_get_busses(); bus; bus = bus->next) |
46 | {
|
47 | for(dev = bus->devices; dev; dev = dev->next) |
48 | {
|
49 | if( (dev->descriptor.idVendor == MY_VID) && (dev->descriptor.idProduct == MY_PID) ) |
50 | {
|
51 | printf("got it open\n\n"); |
52 | return usb_open(dev); |
53 | }
|
54 | }
|
55 | }
|
56 | return NULL; |
57 | }
|
58 | |
59 | |
60 | |
61 | int main(void) |
62 | {
|
63 | usb_set_debug(255); |
64 | |
65 | int i= 0; |
66 | unsigned char j = 0; // 48 -> 122 |
67 | |
68 | usb_init(); /* initialize the library */ |
69 | usb_find_busses(); /* find all busses */ |
70 | usb_find_devices(); /* find all connected devices */ |
71 | |
72 | i = 0; |
73 | j= 48; |
74 | |
75 | while (i<BUF_SIZE-1) |
76 | |
77 | {
|
78 | tmp[i] = 66; |
79 | tmp1[i] = 66; |
80 | tmp2[i] = 66; |
81 | // printf("i = %i \t tmp = %c \t tmp1 = %c \t tmp2 = %c \n", i, tmp[i], tmp1[i], tmp2[i]);
|
82 | i+=1; |
83 | j+=1; |
84 | if (j==123) j = 48; |
85 | }
|
86 | tmp[BUF_SIZE-1] = '\0'; |
87 | tmp1[BUF_SIZE-1] = '\0'; |
88 | tmp2[BUF_SIZE-1] = '\0'; |
89 | |
90 | printf("\n"); |
91 | |
92 | |
93 | if(!(dev = open_dev())) |
94 | {
|
95 | printf("error: device not found!\n"); |
96 | return 0; |
97 | }
|
98 | |
99 | if(usb_set_configuration(dev, 1) < 0) |
100 | {
|
101 | printf("error: setting config 1 failed\n"); |
102 | usb_close(dev); |
103 | return 0; |
104 | }
|
105 | |
106 | if(usb_claim_interface(dev, 0) < 0) |
107 | {
|
108 | printf("error: claiming interface 0 failed\n"); |
109 | usb_close(dev); |
110 | return 0; |
111 | }
|
112 | |
113 | |
114 | |
115 | char Taste; |
116 | printf("Warte auf Taste 's'\n"); |
117 | while (Taste = getchar(), Taste == 's') ; |
118 | printf("Taste 's' gedrückt\n"); |
119 | |
120 | |
121 | tmp[0] = 's'; |
122 | |
123 | int wrote; |
124 | wrote=usb_bulk_write(dev, EP1_OUT, tmp, 1, 5000); |
125 | |
126 | if(wrote != 1) |
127 | {
|
128 | printf("error: bulk write failed %d\n", wrote); |
129 | }
|
130 | else
|
131 | {
|
132 | printf("Geschrieben %c \n\n: ", tmp[0]); |
133 | }
|
134 | |
135 | for (j=1; j < 10; j++) |
136 | {
|
137 | bytesread1=usb_bulk_read(dev, 0x81, (char*)&tmp1[0], 64, 5000); |
138 | printf("Gelesen EP1 \t: "); |
139 | for (i=0; i<bytesread1; i++) printf("%i", tmp1[i]); |
140 | printf(" : Bytes = %d\n", bytesread1); |
141 | puts(tmp1); |
142 | }
|
143 | |
144 | usb_release_interface(dev, 0); |
145 | usb_close(dev); |
146 | |
147 | printf("\n ENDE \n"); |
148 | |
149 | return 0; |
150 | }
|
Firmwareauszug
1 | #pragma udata USB_VARS_CONTROL // RAM=usb5
|
2 | |
3 | BYTE IN_Control[USB_Control_EP_SIZE]; // Puffergröße definiert in usb_config.h |
4 | BYTE OUT_Control[USB_Control_EP_SIZE]; |
5 | |
6 | #define PUFFER_ANZAHL 4
|
7 | |
8 | #pragma udata USB_VARS_IN_DATA // RAM=usb6
|
9 | |
10 | BYTE IN_Data [PUFFER_ANZAHL][USB_Data_EP_SIZE]; // [PUFFER_ANZAHL] * [USB_Data_EP_SIZE] = 256 Bytes = 1 Bank |
11 | |
12 | #pragma udata USB_VARS_OUT_DATA //RAM=usb7
|
13 | |
14 | BYTE OUT_Data [USB_Data_EP_SIZE]; |
15 | |
16 | #pragma udata
|
17 | |
18 | unsigned char IN_Data_Belegung[PUFFER_ANZAHL]; |
19 | unsigned char IN_Control_Belegung; |
20 | unsigned char OUT_Control_Belegung; |
21 | |
22 | #define RS232_sendbuffer_maxcount 32 // Physikalische Puffergröße
|
23 | BYTE RS232_sendbuffer [RS232_sendbuffer_maxcount]; // Bytearray |
24 | BYTE RS232_sendbuffer_count; |
25 | |
26 | |
27 | #define RS232_readbuffer_maxcount 256 // Physikalische Puffergröße
|
28 | BYTE *RS232_readbuffer = IN_Data; // Bytearray |
29 | BYTE RS232_readbuffer_count; |
30 | |
31 | unsigned char Sende_USB; |
32 | unsigned char puffer_akt; |
33 | unsigned short long Bildlaenge; |
34 | BYTE puffererror; |
35 | BYTE CAM_Status; |
36 | |
37 | |
38 | USB_HANDLE USB_Control_Out_Handle = 0; |
39 | USB_HANDLE USB_Control_In_Handle = 0; |
40 | USB_HANDLE USB_Data_In_Handle = 0; |
41 | USB_HANDLE USB_Data_Out_Handle = 0; |
42 | |
43 | |
44 | /** PRIVATE PROTOTYPES *********************************************/
|
45 | |
46 | void YourHighPriorityISRCode(void); |
47 | void YourLowPriorityISRCode(void); |
48 | |
49 | /** VECTOR REMAPPING ***********************************************/
|
50 | |
51 | #define REMAPPED_RESET_VECTOR_ADDRESS 0x800
|
52 | #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x808
|
53 | #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x818
|
54 | |
55 | //These are your actual interrupt handling routines.
|
56 | #pragma interrupt YourHighPriorityISRCode
|
57 | void YourHighPriorityISRCode() |
58 | {
|
59 | #if defined(USB_INTERRUPT)
|
60 | USBDeviceTasks(); |
61 | #endif
|
62 | } //This return will be a "retfie fast", since this is in a #pragma interrupt section |
63 | |
64 | #pragma code
|
65 | |
66 | void main(void) |
67 | {
|
68 | USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware |
69 | |
70 | #if defined(USB_INTERRUPT) // defined in usb_config.h
|
71 | USBDeviceAttach(); |
72 | #endif
|
73 | |
74 | TRISAbits.TRISA0=1; |
75 | TRISB = 0x1F; |
76 | |
77 | PORTBbits.RB7 = 0; |
78 | PORTBbits.RB6 = 0; |
79 | PORTBbits.RB5 = 0; |
80 | |
81 | CAM_Status = 100; |
82 | |
83 | while(1) |
84 | {
|
85 | |
86 | PORTBbits.RB5 = 1; |
87 | |
88 | if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) continue; |
89 | |
90 | if(!USBHandleBusy(USB_Control_Out_Handle)) // Check if the endpoint has received any data from the host. |
91 | {
|
92 | |
93 | if((!USBHandleBusy(USB_Control_In_Handle)) && (CAM_Status == 100)) |
94 | {
|
95 | CAM_Status = 0; // Initiere das Senden von SYNC () |
96 | |
97 | PORTBbits.RB6 = 1; |
98 | |
99 | IN_Control[0] = 70; |
100 | IN_Control[1] = 70; |
101 | IN_Control[2] = 70; |
102 | IN_Control[3] = 70; |
103 | IN_Control[4] = 70; |
104 | IN_Control[5] = 70; |
105 | USB_Control_In_Handle = USBGenWrite(USB_Control_EP_NUM,(BYTE*)&IN_Control[0], 6); |
106 | }
|
107 | USB_Control_Out_Handle = USBGenRead(USB_Control_EP_NUM,(BYTE*)&OUT_Control[0],USB_Control_EP_SIZE); |
108 | }
|
109 | |
110 | |
111 | |
112 | if((!USBHandleBusy(USB_Control_In_Handle)) && (CAM_Status != 100)) |
113 | {
|
114 | PORTBbits.RB7 = 1; |
115 | IN_Control[0] = 70; |
116 | IN_Control[1] = 70; |
117 | IN_Control[2] = 70; |
118 | USB_Control_In_Handle = USBGenWrite (USB_Control_EP_NUM,(BYTE*)&IN_Control[0], 3); |
119 | }
|
120 | } //end while (1) |
121 | } //end main |
122 | |
123 | |
124 | // ****************************************************************
|
125 | // ************** USB Callback Functions
|
126 | // ****************************************************************
|
127 | |
128 | |
129 | void USBCBInitEP(void) |
130 | {
|
131 | USBEnableEndpoint(USB_Control_EP_NUM,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP); |
132 | USBEnableEndpoint(USB_Data_EP_NUM,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP); |
133 | |
134 | USB_Control_Out_Handle = USBGenRead(USB_Control_EP_NUM,(BYTE*)&OUT_Control[0], USB_Control_EP_SIZE); |
135 | USB_Data_Out_Handle = USBGenRead(USB_Data_EP_NUM,(BYTE*)&OUT_Data[0], USB_Data_EP_SIZE); |
136 | }
|