00001
00104 #ifndef __PIC_USB_H
00105 #define __PIC_USB_H
00106
00107 #include "pic_utils.h"
00108
00110 typedef enum _usb_state_type
00111 {
00113 st_POWERED,
00115 st_DEFAULT,
00117 st_ADDRESS,
00119 st_CONFIGURED
00120 } usb_state_type;
00121
00123 extern usb_state_type usb_state;
00124
00126 extern uns8 usb_address;
00127
00129 typedef struct _buffer_descriptor {
00130 uns8 stat,
00131 count;
00132 uns16 addr;
00133 } buffer_descriptor;
00134
00136 typedef struct _setup_data_packet {
00137 uns8 bmRequestType,
00138 bRequest;
00139 uns16 wValue,
00140 wIndex,
00141 wLength;
00142 } setup_data_packet;
00143
00144
00145
00146
00147
00148 #define DATA_STAGE_DIR 7 // 0= OUT or no data stage, 1= IN
00149
00150 #define REQUEST_TYPE1 6 // 00= USB standard request
00151 #define REQUEST_TYPE0 5 // 01= Request for a specific class
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 #define UOWN 7 // 0 if uC owns buffer
00162 #define DTS 6 // 1 data 1 packet, 0 data 0 packet
00163 #define KEN 5 // 1 USB will keep buffer indefinately, 0 USB will hand back buffer
00164 #define INCDIS 4 // 1 Address increment disabled (for SPP), 0 address increment enabled
00165 #define DTSEN 3 // 1 data toggle sync on, 0 data toggle sync off
00166 #define BSTALL 2 // 1 buffer stall enabled, 0 buffer stall disabled
00167 #define BC9 1 // bit 9 of buffer count
00168 #define BC8 0 // bit 8 of buffer count
00169
00170
00171
00172
00173 #define PID3 5 // received token pid value of last transfer
00174 #define PID2 4
00175 #define PID1 3
00176 #define PID0 2
00177
00178
00179
00181 typedef enum _control_mode_type {
00183 cm_IDLE,
00185 cm_CTRL_WRITE_DATA_STAGE,
00187 cm_CTRL_WRITE_DATA_STAGE_CLASS,
00189 cm_CTRL_READ_DATA_STAGE,
00191 cm_CTRL_READ_DATA_STAGE_CLASS,
00193 cm_CTRL_READ_AWAITING_STATUS,
00195 cm_CTRL_WRITE_SENDING_STATUS,
00196 } control_mode_type;
00197
00202 typedef enum _usb_status_type {
00203 us_IDLE,
00204 us_SET_ADDRESS
00205 } usb_status_type;
00206
00207
00208
00209
00210
00211 #define pid_OUT 0b00000001
00212 #define pid_IN 0b00001001
00213 #define pid_SOF 0b00000101
00214 #define pid_SETUP 0b00001101
00215
00216
00217
00218 #define pid_DATA0 0b00000011
00219 #define pid_DATA1 0b00001011
00220 #define pid_DATA2 0b00000111
00221 #define pid_MDATA 0b00001111
00222
00223
00224
00225 #define pid_ACK 0b00000010
00226 #define pid_NAK 0b00001010
00227 #define pid_STALL 0b00001110
00228 #define pid_NYET 0b00000110
00229
00230
00231
00232
00233 #define req_Get_Status 0x00
00234 #define req_Clear_Feature 0x01
00235 #define req_Set_Feature 0x03
00236 #define req_Set_Address 0x05
00237 #define req_Get_Descriptor 0x06
00238 #define req_Set_Descriptor 0x07
00239 #define req_Get_Configuration 0x08
00240 #define req_Set_Configuration 0x09
00241 #define req_Get_Interface 0x0a
00242 #define req_Set_Interface 0x0b
00243 #define req_Synch_Frame 0x0c
00244
00245
00246
00247 #define dt_DEVICE 0x01
00248 #define dt_CONFIGURATION 0x02
00249 #define dt_STRING 0x03
00250 #define dt_INTERFACE 0x04
00251 #define dt_ENDPOINT 0x05
00252 #define dt_DEVICE_QUALIFIER 0x06
00253 #define dt_OTHER_SPEED_CONFIG 0x07
00254 #define dt_INTERFACE_POWER 0x08
00255 #define dt_OTG 0x09
00256 #define dt_DEBUG 0x0a
00257 #define dt_INTERFACE_ASSOC 0x0b
00258 #define dt_HID 0x21
00259 #define dt_HID_REPORT 0x22
00260
00261 #define dt_CS_INTERFACE 0x24
00262
00264 typedef struct _device_descriptor {
00265 uns8 length,
00266 descriptor_type;
00267 uns16 usb_version;
00268 uns8 device_class,
00269 device_subclass,
00270 device_protocol;
00271 uns8 max_packet_size_ep0;
00272 uns16 vendor_id,
00273 product_id,
00274 device_release;
00275 uns8 manufacturer_string_id,
00276 product_string_id,
00277 serial_string_id,
00278 num_configurations;
00279 } device_descriptor;
00280
00282 typedef struct _configuration_descriptor {
00283 uns8 length,
00284 descriptor_type;
00285 uns16 total_length;
00286 uns8 num_interfaces,
00287 configuration_value,
00288 configuration_string_id,
00289 attributes,
00290
00291
00292 max_power;
00293 } configuration_descriptor;
00294
00296 typedef struct _interface_descriptor {
00297 uns8 length,
00298 descriptor_type,
00299 interface_number,
00300 alternate_setting,
00301 num_endpoints,
00302 interface_class,
00303 interface_subclass,
00304 interface_protocol,
00305 interface_string_id;
00306 } interface_descriptor;
00307
00309 typedef struct _endpoint_descriptor {
00310 uns8 length,
00311 descriptor_type,
00312 endpoint_address,
00313 attributes;
00314 uns16 max_packet_size;
00315 uns8 interval;
00316 } endpoint_descriptor;
00317
00319 typedef struct _hid_descriptor {
00320 uns8 length,
00321 descriptor_type;
00322 uns16 hid_spec;
00323 uns8 country_code,
00324 num_class_descriptors,
00325 class_descriptor_type;
00326 uns16 class_descriptor_length;
00327
00328 } hid_descriptor;
00329
00330 typedef struct _CDC_header_functional_descriptor {
00331 uns8 length,
00332 descriptor_type,
00333 descriptor_subtype;
00334 uns16 CDC_version;
00335 } CDC_header_functional_descriptor;
00336
00337 typedef struct _CDC_ACM_functional_descriptor {
00338 uns8 length,
00339 descriptor_type,
00340 descriptor_subtype,
00341 capabilities;
00342 } CDC_ACM_functional_descriptor;
00343
00344 typedef struct _CDC_union_functional_descriptor {
00345 uns8 length,
00346 descriptor_type,
00347 descriptor_subtype,
00348 master_interface,
00349 slave_interface;
00350 } CDC_union_functional_descriptor;
00351
00352 typedef struct _CDC_call_mgt_functional_descriptor {
00353 uns8 length,
00354 descriptor_type,
00355 descriptor_subtype,
00356 capabilities,
00357 data_interface;
00358 } CDC_call_mgt_functional_descriptor;
00359
00361 extern setup_data_packet usb_sdp;
00362
00364 extern control_mode_type control_mode;
00365
00367 #define usb_send_status_ack() usb_send_empty_data_pkt()
00368
00379 void usb_setup();
00380
00396 void usb_enable_module();
00397
00416 void usb_handle_isr();
00429 void usb_send_data(uns8 ep, uns8 *data, uns8 send_count, bit first);
00430
00437 void usb_send_empty_data_pkt();
00438
00446 void usb_stall_ep0();
00447
00454 usb_state_type usb_get_state();
00455
00477 void usb_handle_class_request_callback(setup_data_packet sdp);
00478
00491 void usb_handle_class_ctrl_read_callback();
00492
00507 void usb_handle_class_ctrl_write_callback(uns8 *data, uns16 count);
00508
00528 void usb_get_descriptor_callback(uns8 descriptor_type, uns8 descriptor_num,
00529 uns8 **rtn_descriptor_ptr, uns16 *rtn_descriptor_size);
00530
00548 void usb_ep_data_out_callback(uns8 end_point, uns8 *buffer_location, uns16 byte_count);
00549
00565 void usb_ep_data_in_callback(uns8 end_point, uns16 byte_count);
00566
00577 void usb_device_configured_callback();
00578
00588 void usb_SOF_callback(uns16 frame);
00589
00597 void turn_usb_ints_on();
00598
00599 #ifdef USB_EP1_OUT_SIZE
00600 #define USB_EP1
00601 #endif
00602 #ifdef USB_EP1_IN_SIZE
00603 #ifndef USB_EP1
00604 #define USB_EP1
00605 #endif
00606 #endif
00607 #ifdef USB_EP2_OUT_SIZE
00608 #define USB_EP2
00609 #endif
00610 #ifdef USB_EP2_IN_SIZE
00611 #ifndef USB_EP2
00612 #define USB_EP2
00613 #endif
00614 #endif
00615 #ifdef USB_EP3_OUT_SIZE
00616 #define USB_EP3
00617 #endif
00618 #ifdef USB_EP3_IN_SIZE
00619 #ifndef USB_EP3
00620 #define USB_EP3
00621 #endif
00622 #endif
00623 #ifdef USB_EP4_OUT_SIZE
00624 #define USB_EP4
00625 #endif
00626 #ifdef USB_EP4_IN_SIZE
00627 #ifndef USB_EP4
00628 #define USB_EP4
00629 #endif
00630 #endif
00631
00632 #ifdef USB_DEBUG_HIGH
00633 #ifndef USB_DEBUG
00634 #define USB_DEBUG
00635 #endif
00636 #endif
00637
00638
00639 #ifndef USB_HIGHEST_EP
00640 #error "pic_usb: USB_HIGHEST_EP not defined in config.h"
00641 #endif
00642
00643 #ifndef USB_SELF_POWERED
00644 #ifndef USB_BUS_POWERED
00645 #error "pic_usb: Must define either USB_SELF_POWERED or USB_BUS_POWERED in config.h"
00646 #endif
00647 #endif
00648
00649 #ifndef USB_EP0_OUT_SIZE
00650 #error "pic_usb: USB_EP0_OUT_SIZE not defined in config.h"
00651 #endif
00652 #ifndef USB_EP0_IN_SIZE
00653 #error "pic_usb: USB_EP0_IN_SIZE not defined in config.h"
00654 #endif
00655 #ifndef USB_EP0_OUT_ADDR
00656 #error "pic_usb: USB_EP0_OUT_ADDR not defined in config.h"
00657 #endif
00658 #ifndef USB_EP0_IN_ADDR
00659 #error "pic_usb: USB_EP0_IN_ADDR not defined in config.h"
00660 #endif
00661
00662 #endif