00001
00014 #ifndef __PIC_USB_H
00015 #define __PIC_USB_H
00016
00017 #include "pic_utils.h"
00018
00019 typedef enum _usb_state_type
00020 {
00021 st_POWERED,
00022 st_DEFAULT,
00023 st_ADDRESS,
00024 st_CONFIGURED
00025 } usb_state_type;
00026
00027 extern usb_state_type usb_state;
00028 extern uns8 usb_address;
00029
00030 typedef struct _buffer_descriptor {
00031 uns8 stat,
00032 count;
00033 uns16 addr;
00034 } buffer_descriptor;
00035
00036 typedef struct _setup_data_packet {
00037 uns8 bmRequestType,
00038 bRequest;
00039 uns16 wValue,
00040 wIndex,
00041 wLength;
00042 } setup_data_packet;
00043
00044
00045
00046
00047
00048
00049
00050 #define DATA_STAGE_DIR 7 // 0= OUT or no data stage, 1= IN
00051
00052 #define REQUEST_TYPE1 6 // 00= USB standard request
00053 #define REQUEST_TYPE0 5 // 01= Request for a specific class
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #define UOWN 7 // 0 if uC owns buffer
00067 #define DTS 6 // 1 data 1 packet, 0 data 0 packet
00068 #define KEN 5 // 1 USB will keep buffer indefinately, 0 USB will hand back buffer
00069 #define INCDIS 4 // 1 Address increment disabled (for SPP), 0 address increment enabled
00070 #define DTSEN 3 // 1 data toggle sync on, 0 data toggle sync off
00071 #define BSTALL 2 // 1 buffer stall enabled, 0 buffer stall disabled
00072 #define BC9 1 // bit 9 of buffer count
00073 #define BC8 0 // bit 8 of buffer count
00074
00075
00076
00077
00078 #define PID3 5 // received token pid value of last transfer
00079 #define PID2 4
00080 #define PID1 3
00081 #define PID0 2
00082
00083
00084
00085
00086
00087
00088
00089 #define pid_OUT 0b00000001
00090 #define pid_IN 0b00001001
00091 #define pid_SOF 0b00000101
00092 #define pid_SETUP 0b00001101
00093
00094
00095
00096 #define pid_DATA0 0b00000011
00097 #define pid_DATA1 0b00001011
00098 #define pid_DATA2 0b00000111
00099 #define pid_MDATA 0b00001111
00100
00101
00102
00103 #define pid_ACK 0b00000010
00104 #define pid_NAK 0b00001010
00105 #define pid_STALL 0b00001110
00106 #define pid_NYET 0b00000110
00107
00108
00109
00110
00111 #define req_Get_Status 0x00
00112 #define req_Clear_Feature 0x01
00113 #define req_Set_Feature 0x03
00114 #define req_Set_Address 0x05
00115 #define req_Get_Descriptor 0x06
00116 #define req_Set_Descriptor 0x07
00117 #define req_Get_Configuration 0x08
00118 #define req_Set_Configuration 0x09
00119 #define req_Get_Interface 0x0a
00120 #define req_Set_Interface 0x0b
00121 #define req_Synch_Frame 0x0c
00122
00123
00124
00125 #define dt_DEVICE 0x01
00126 #define dt_CONFIGURATION 0x02
00127 #define dt_STRING 0x03
00128 #define dt_INTERFACE 0x04
00129 #define dt_ENDPOINT 0x05
00130 #define dt_DEVICE_QUALIFIER 0x06
00131 #define dt_OTHER_SPEED_CONFIG 0x07
00132 #define dt_INTERFACE_POWER 0x08
00133 #define dt_OTG 0x09
00134 #define dt_DEBUG 0x0a
00135 #define dt_INTERFACE_ASSOC 0x0b
00136 #define dt_HID 0x21
00137 #define dt_HID_REPORT 0x22
00138
00139 #define dt_CS_INTERFACE 0x24
00140
00141
00142 typedef struct _device_descriptor {
00143 uns8 length,
00144 descriptor_type;
00145 uns16 usb_version;
00146 uns8 device_class,
00147 device_subclass,
00148 device_protocol;
00149 uns8 max_packet_size_ep0;
00150 uns16 vendor_id,
00151 product_id,
00152 device_release;
00153 uns8 manufacturer_string_id,
00154 product_string_id,
00155 serial_string_id,
00156 num_configurations;
00157 } device_descriptor;
00158
00159 typedef struct _configuration_descriptor {
00160 uns8 length,
00161 descriptor_type;
00162 uns16 total_length;
00163 uns8 num_interfaces,
00164 configuration_value,
00165 configuration_string_id,
00166 attributes,
00167
00168
00169 max_power;
00170 } configuration_descriptor;
00171
00172
00173 typedef struct _interface_descriptor {
00174 uns8 length,
00175 descriptor_type,
00176 interface_number,
00177 alternate_setting,
00178 num_endpoints,
00179 interface_class,
00180 interface_subclass,
00181 interface_protocol,
00182 interface_string_id;
00183 } interface_descriptor;
00184
00185 typedef struct _endpoint_descriptor {
00186 uns8 length,
00187 descriptor_type,
00188 endpoint_address,
00189 attributes;
00190 uns16 max_packet_size;
00191 uns8 interval;
00192 } endpoint_descriptor;
00193
00194 typedef struct _hid_descriptor {
00195 uns8 length,
00196 descriptor_type;
00197 uns16 hid_spec;
00198 uns8 country_code,
00199 num_class_descriptors,
00200 class_descriptor_type;
00201 uns16 class_descriptor_length;
00202
00203 } hid_descriptor;
00204
00205 typedef struct _CDC_header_functional_descriptor {
00206 uns8 length,
00207 descriptor_type,
00208 descriptor_subtype;
00209 uns16 CDC_version;
00210 } CDC_header_functional_descriptor;
00211
00212 typedef struct _CDC_ACM_functional_descriptor {
00213 uns8 length,
00214 descriptor_type,
00215 descriptor_subtype,
00216 capabilities;
00217 } CDC_ACM_functional_descriptor;
00218
00219 typedef struct _CDC_union_functional_descriptor {
00220 uns8 length,
00221 descriptor_type,
00222 descriptor_subtype,
00223 master_interface,
00224 slave_interface;
00225 } CDC_union_functional_descriptor;
00226
00227 typedef struct _CDC_call_mgt_functional_descriptor {
00228 uns8 length,
00229 descriptor_type,
00230 descriptor_subtype,
00231 capabilities,
00232 data_interface;
00233 } CDC_call_mgt_functional_descriptor;
00234
00235
00236
00237 typedef enum _control_mode_type {
00238 cm_IDLE,
00239 cm_CTRL_WRITE_DATA_STAGE,
00240 cm_CTRL_WRITE_DATA_STAGE_CLASS,
00241 cm_CTRL_READ_DATA_STAGE,
00242 cm_CTRL_READ_DATA_STAGE_CLASS,
00243 cm_CTRL_READ_AWAITING_STATUS,
00244 cm_CTRL_WRITE_SENDING_STATUS,
00245 cm_SENDING_STATUS,
00246 } control_mode_type;
00247
00248 typedef enum _usb_status_type {
00249 us_IDLE,
00250 us_SET_ADDRESS
00251 } usb_status_type;
00252
00253 typedef enum _direction_type {
00254 IN,
00255 OUT
00256 } direction_type;
00257
00258 extern setup_data_packet usb_sdp;
00259 extern control_mode_type control_mode;
00260
00261 #define usb_send_status_ack() usb_send_empty_data_pkt()
00262 void usb_setup();
00263 void usb_handle_isr();
00264 void usb_enable_module();
00265 void usb_send_data(uns8 ep, uns8 *data, uns8 send_count, bit first);
00266 void usb_send_empty_data_pkt();
00267 void usb_stall_ep0();
00268 usb_state_type usb_get_state();
00269
00270 void usb_handle_ctrl_read_class();
00271 void usb_handle_ctrl_write_class(uns8 *data, uns16 count);
00272 void usb_handle_class_request(setup_data_packet sdp);
00273
00274 void usb_get_descriptor_callback(uns8 descriptor_type, uns8 descriptor_num,
00275 uns8 **rtn_descriptor_ptr, uns16 *rtn_descriptor_size);
00276
00277
00278 void usb_ep_data_out_callback(uns8 end_point, uns8 *buffer_location, uns16 byte_count);
00279 void usb_ep_data_in_callback(uns8 end_point, uns16 byte_count);
00280
00281
00282 void usb_device_configured_callback();
00283
00284
00285 void usb_SOF_callback(uns16 frame);
00286
00287
00288 #ifdef USB_EP1_OUT_SIZE
00289 #define USB_EP1
00290 #endif
00291 #ifdef USB_EP1_IN_SIZE
00292 #ifndef USB_EP1
00293 #define USB_EP1
00294 #endif
00295 #endif
00296 #ifdef USB_EP2_OUT_SIZE
00297 #define USB_EP2
00298 #endif
00299 #ifdef USB_EP2_IN_SIZE
00300 #ifndef USB_EP2
00301 #define USB_EP2
00302 #endif
00303 #endif
00304 #ifdef USB_EP3_OUT_SIZE
00305 #define USB_EP3
00306 #endif
00307 #ifdef USB_EP3_IN_SIZE
00308 #ifndef USB_EP3
00309 #define USB_EP3
00310 #endif
00311 #endif
00312 #ifdef USB_EP4_OUT_SIZE
00313 #define USB_EP4
00314 #endif
00315 #ifdef USB_EP4_IN_SIZE
00316 #ifndef USB_EP4
00317 #define USB_EP4
00318 #endif
00319 #endif
00320
00321
00322 #endif