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