00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00134 #ifndef __PIC_USB_H
00135 #define __PIC_USB_H
00136
00137 #include "pic_utils.h"
00138
00140 typedef enum _usb_state_type
00141 {
00143 st_POWERED,
00145 st_DEFAULT,
00147 st_ADDRESS,
00149 st_CONFIGURED
00150 } usb_state_type;
00151
00153 extern usb_state_type usb_state;
00154
00156 extern uns8 usb_address;
00157
00159 typedef struct _buffer_descriptor {
00160 uns8 stat,
00161 count;
00162 uns16 addr;
00163 } buffer_descriptor;
00164
00166 typedef struct _setup_data_packet {
00167 uns8 bmRequestType,
00168 bRequest;
00169 uns16 wValue,
00170 wIndex,
00171 wLength;
00172 } setup_data_packet;
00173
00174
00175
00176
00177
00178 #define DATA_STAGE_DIR 7 // 0= OUT or no data stage, 1= IN
00179
00180 #define REQUEST_TYPE1 6 // 00= USB standard request
00181 #define REQUEST_TYPE0 5 // 01= Request for a specific class
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 #define UOWN 7 // 0 if uC owns buffer
00192 #define DTS 6 // 1 data 1 packet, 0 data 0 packet
00193 #define KEN 5 // 1 USB will keep buffer indefinately, 0 USB will hand back buffer
00194 #define INCDIS 4 // 1 Address increment disabled (for SPP), 0 address increment enabled
00195 #define DTSEN 3 // 1 data toggle sync on, 0 data toggle sync off
00196 #define BSTALL 2 // 1 buffer stall enabled, 0 buffer stall disabled
00197 #define BC9 1 // bit 9 of buffer count
00198 #define BC8 0 // bit 8 of buffer count
00199
00200
00201
00202
00203 #define PID3 5 // received token pid value of last transfer
00204 #define PID2 4
00205 #define PID1 3
00206 #define PID0 2
00207
00208
00209
00211 typedef enum _control_mode_type {
00213 cm_IDLE,
00215 cm_CTRL_WRITE_DATA_STAGE,
00217 cm_CTRL_WRITE_DATA_STAGE_CLASS,
00219 cm_CTRL_READ_DATA_STAGE,
00221 cm_CTRL_READ_DATA_STAGE_CLASS,
00223 cm_CTRL_READ_AWAITING_STATUS,
00225 cm_CTRL_WRITE_SENDING_STATUS,
00226 } control_mode_type;
00227
00232 typedef enum _usb_status_type {
00233 us_IDLE,
00234 us_SET_ADDRESS
00235 } usb_status_type;
00236
00237
00238
00239
00240
00241 #define pid_OUT 0b00000001
00242 #define pid_IN 0b00001001
00243 #define pid_SOF 0b00000101
00244 #define pid_SETUP 0b00001101
00245
00246
00247
00248 #define pid_DATA0 0b00000011
00249 #define pid_DATA1 0b00001011
00250 #define pid_DATA2 0b00000111
00251 #define pid_MDATA 0b00001111
00252
00253
00254
00255 #define pid_ACK 0b00000010
00256 #define pid_NAK 0b00001010
00257 #define pid_STALL 0b00001110
00258 #define pid_NYET 0b00000110
00259
00260
00261
00262
00263 #define req_Get_Status 0x00
00264 #define req_Clear_Feature 0x01
00265 #define req_Set_Feature 0x03
00266 #define req_Set_Address 0x05
00267 #define req_Get_Descriptor 0x06
00268 #define req_Set_Descriptor 0x07
00269 #define req_Get_Configuration 0x08
00270 #define req_Set_Configuration 0x09
00271 #define req_Get_Interface 0x0a
00272 #define req_Set_Interface 0x0b
00273 #define req_Synch_Frame 0x0c
00274
00275
00276
00277 #define dt_DEVICE 0x01
00278 #define dt_CONFIGURATION 0x02
00279 #define dt_STRING 0x03
00280 #define dt_INTERFACE 0x04
00281 #define dt_ENDPOINT 0x05
00282 #define dt_DEVICE_QUALIFIER 0x06
00283 #define dt_OTHER_SPEED_CONFIG 0x07
00284 #define dt_INTERFACE_POWER 0x08
00285 #define dt_OTG 0x09
00286 #define dt_DEBUG 0x0a
00287 #define dt_INTERFACE_ASSOC 0x0b
00288 #define dt_HID 0x21
00289 #define dt_HID_REPORT 0x22
00290
00291 #define dt_CS_INTERFACE 0x24
00292
00294 typedef struct _device_descriptor {
00295 uns8 length,
00296 descriptor_type;
00297 uns16 usb_version;
00298 uns8 device_class,
00299 device_subclass,
00300 device_protocol;
00301 uns8 max_packet_size_ep0;
00302 uns16 vendor_id,
00303 product_id,
00304 device_release;
00305 uns8 manufacturer_string_id,
00306 product_string_id,
00307 serial_string_id,
00308 num_configurations;
00309 } device_descriptor;
00310
00312 typedef struct _configuration_descriptor {
00313 uns8 length,
00314 descriptor_type;
00315 uns16 total_length;
00316 uns8 num_interfaces,
00317 configuration_value,
00318 configuration_string_id,
00319 attributes,
00320
00321
00322 max_power;
00323 } configuration_descriptor;
00324
00326 typedef struct _interface_descriptor {
00327 uns8 length,
00328 descriptor_type,
00329 interface_number,
00330 alternate_setting,
00331 num_endpoints,
00332 interface_class,
00333 interface_subclass,
00334 interface_protocol,
00335 interface_string_id;
00336 } interface_descriptor;
00337
00339 typedef struct _endpoint_descriptor {
00340 uns8 length,
00341 descriptor_type,
00342 endpoint_address,
00343 attributes;
00344 uns16 max_packet_size;
00345 uns8 interval;
00346 } endpoint_descriptor;
00347
00349 typedef struct _hid_descriptor {
00350 uns8 length,
00351 descriptor_type;
00352 uns16 hid_spec;
00353 uns8 country_code,
00354 num_class_descriptors,
00355 class_descriptor_type;
00356 uns16 class_descriptor_length;
00357
00358 } hid_descriptor;
00359
00360 typedef struct _CDC_header_functional_descriptor {
00361 uns8 length,
00362 descriptor_type,
00363 descriptor_subtype;
00364 uns16 CDC_version;
00365 } CDC_header_functional_descriptor;
00366
00367 typedef struct _CDC_ACM_functional_descriptor {
00368 uns8 length,
00369 descriptor_type,
00370 descriptor_subtype,
00371 capabilities;
00372 } CDC_ACM_functional_descriptor;
00373
00374 typedef struct _CDC_union_functional_descriptor {
00375 uns8 length,
00376 descriptor_type,
00377 descriptor_subtype,
00378 master_interface,
00379 slave_interface;
00380 } CDC_union_functional_descriptor;
00381
00382 typedef struct _CDC_call_mgt_functional_descriptor {
00383 uns8 length,
00384 descriptor_type,
00385 descriptor_subtype,
00386 capabilities,
00387 data_interface;
00388 } CDC_call_mgt_functional_descriptor;
00389
00391 extern setup_data_packet usb_sdp;
00392
00394 extern control_mode_type control_mode;
00395
00397 #define usb_send_status_ack() usb_send_empty_data_pkt()
00398
00409 void usb_setup();
00410
00426 void usb_enable_module();
00427
00446 void usb_handle_isr();
00459 void usb_send_data(uns8 ep, uns8 *data, uns8 send_count, bit first);
00460
00467 void usb_send_empty_data_pkt();
00468
00476 void usb_stall_ep0();
00477
00484 usb_state_type usb_get_state();
00485
00507 void usb_handle_class_request_callback(setup_data_packet sdp);
00508
00521 void usb_handle_class_ctrl_read_callback();
00522
00537 void usb_handle_class_ctrl_write_callback(uns8 *data, uns16 count);
00538
00558 void usb_get_descriptor_callback(uns8 descriptor_type, uns8 descriptor_num,
00559 uns8 **rtn_descriptor_ptr, uns16 *rtn_descriptor_size);
00560
00578 void usb_ep_data_out_callback(uns8 end_point, uns8 *buffer_location, uns16 byte_count);
00579
00595 void usb_ep_data_in_callback(uns8 end_point, uns16 byte_count);
00596
00607 void usb_device_configured_callback();
00608
00618 void usb_SOF_callback(uns16 frame);
00619
00627 void turn_usb_ints_on();
00628
00629 #ifdef USB_EP1_OUT_SIZE
00630 #define USB_EP1
00631 #endif
00632 #ifdef USB_EP1_IN_SIZE
00633 #ifndef USB_EP1
00634 #define USB_EP1
00635 #endif
00636 #endif
00637 #ifdef USB_EP2_OUT_SIZE
00638 #define USB_EP2
00639 #endif
00640 #ifdef USB_EP2_IN_SIZE
00641 #ifndef USB_EP2
00642 #define USB_EP2
00643 #endif
00644 #endif
00645 #ifdef USB_EP3_OUT_SIZE
00646 #define USB_EP3
00647 #endif
00648 #ifdef USB_EP3_IN_SIZE
00649 #ifndef USB_EP3
00650 #define USB_EP3
00651 #endif
00652 #endif
00653 #ifdef USB_EP4_OUT_SIZE
00654 #define USB_EP4
00655 #endif
00656 #ifdef USB_EP4_IN_SIZE
00657 #ifndef USB_EP4
00658 #define USB_EP4
00659 #endif
00660 #endif
00661
00662 #ifdef USB_DEBUG_HIGH
00663 #ifndef USB_DEBUG
00664 #define USB_DEBUG
00665 #endif
00666 #endif
00667
00668
00669 #ifndef USB_HIGHEST_EP
00670 #error "pic_usb: USB_HIGHEST_EP not defined in config.h"
00671 #endif
00672
00673 #ifndef USB_SELF_POWERED
00674 #ifndef USB_BUS_POWERED
00675 #error "pic_usb: Must define either USB_SELF_POWERED or USB_BUS_POWERED in config.h"
00676 #endif
00677 #endif
00678
00679 #ifndef USB_EP0_OUT_O_SIZE
00680 #error "pic_usb: USB_EP0_OUT_O_SIZE not defined in config.h"
00681 #endif
00682 #ifndef USB_EP0_OUT_E_SIZE
00683 #error "pic_usb: USB_EP0_OUT_E_SIZE not defined in config.h"
00684 #endif
00685 #ifndef USB_EP0_IN_SIZE
00686 #error "pic_usb: USB_EP0_IN_SIZE not defined in config.h"
00687 #endif
00688 #ifndef USB_EP0_OUT_E_ADDR
00689 #error "pic_usb: USB_EP0_OUT_E_ADDR not defined in config.h"
00690 #endif
00691 #ifndef USB_EP0_OUT_O_ADDR
00692 #error "pic_usb: USB_EP0_OUT_O_ADDR not defined in config.h"
00693 #endif
00694 #ifndef USB_EP0_IN_ADDR
00695 #error "pic_usb: USB_EP0_IN_ADDR not defined in config.h"
00696 #endif
00697
00698 #endif