00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _USB_DRV_H_
00043 #define _USB_DRV_H_
00044
00045
00046
00047
00048 typedef enum endpoint_parameter{ep_num, ep_type, ep_direction, ep_size, ep_bank, nyet_status} t_endpoint_parameter;
00049
00053
00054
00055
00056 #define MAX_EP_NB 7
00057
00058 #define EP_CONTROL 0
00059 #define EP_1 1
00060 #define EP_2 2
00061 #define EP_3 3
00062 #define EP_4 4
00063 #define EP_5 5
00064 #define EP_6 6
00065 #define EP_7 7
00066
00067 #define PIPE_CONTROL 0
00068 #define PIPE_0 0
00069 #define PIPE_1 1
00070 #define PIPE_2 2
00071 #define PIPE_3 3
00072 #define PIPE_4 4
00073 #define PIPE_5 5
00074 #define PIPE_6 6
00075 #define PIPE_7 7
00076
00077
00078 #define MSK_EP_DIR 0x7F
00079 #define MSK_UADD 0x7F
00080 #define MSK_EPTYPE 0xC0
00081 #define MSK_EPSIZE 0x70
00082 #define MSK_EPBK 0x0C
00083 #define MSK_DTSEQ 0x0C
00084 #define MSK_NBUSYBK 0x03
00085 #define MSK_CURRBK 0x03
00086 #define MSK_DAT 0xFF // UEDATX
00087 #define MSK_BYCTH 0x07 // UEBCHX
00088 #define MSK_BYCTL 0xFF // UEBCLX
00089 #define MSK_EPINT 0x7F // UEINT
00090 #define MSK_HADDR 0xFF // UHADDR
00091
00092
00093 #define MSK_PNUM 0x07 // UPNUM
00094 #define MSK_PRST 0x7F // UPRST
00095 #define MSK_PTYPE 0xC0 // UPCFG0X
00096 #define MSK_PTOKEN 0x30
00097 #define MSK_PEPNUM 0x0F
00098 #define MSK_PSIZE 0x70 // UPCFG1X
00099 #define MSK_PBK 0x0C
00100
00101 #define MSK_NBUSYBK 0x03
00102
00103 #define MSK_ERROR 0x1F
00104
00105 #define MSK_PTYPE 0xC0 // UPCFG0X
00106 #define MSK_PTOKEN 0x30
00107 #define MSK_TOKEN_SETUP 0x30
00108 #define MSK_TOKEN_IN 0x10
00109 #define MSK_TOKEN_OUT 0x20
00110 #define MSK_PEPNUM 0x0F
00111
00112 #define MSK_PSIZE 0x70 // UPCFG1X
00113 #define MSK_PBK 0x0C
00114
00115
00116
00117
00118 #define TYPE_CONTROL 0
00119 #define TYPE_ISOCHRONOUS 1
00120 #define TYPE_BULK 2
00121 #define TYPE_INTERRUPT 3
00122
00123
00124 #define DIRECTION_OUT 0
00125 #define DIRECTION_IN 1
00126
00127
00128 #define SIZE_8 0
00129 #define SIZE_16 1
00130 #define SIZE_32 2
00131 #define SIZE_64 3
00132 #define SIZE_128 4
00133 #define SIZE_256 5
00134 #define SIZE_512 6
00135 #define SIZE_1024 7
00136
00137
00138
00139 #define ONE_BANK 0
00140 #define TWO_BANKS 1
00141
00142
00143 #define NYET_ENABLED 0
00144 #define NYET_DISABLED 1
00145
00146
00147 #define TOKEN_SETUP 0
00148 #define TOKEN_IN 1
00149 #define TOKEN_OUT 2
00150
00151 #define Is_ep_addr_in(x) ( (x&0x80)? TRUE : FALSE)
00152
00153
00157 #define Usb_build_ep_config0(type, dir, nyet) ((type<<6) | (nyet<<1) | (dir))
00158 #define Usb_build_ep_config1(size, bank ) ((size<<4) | (bank<<2) )
00159 #define usb_configure_endpoint(num, type, dir, size, bank, nyet) \
00160 ( Usb_select_endpoint(num), \
00161 usb_config_ep(Usb_build_ep_config0(type, dir, nyet),\
00162 Usb_build_ep_config1(size, bank) ))
00163
00164 #define Host_build_pipe_config0(type, token, ep_num) ((type<<6) | (token<<4) | (ep_num))
00165 #define Host_build_pipe_config1(size, bank ) ((size<<4) | (bank<<2) )
00166 #define host_configure_pipe(num, type, token,ep_num, size, bank, freq) \
00167 ( Host_select_pipe(num), \
00168 Host_set_interrupt_frequency(freq), \
00169 host_config_pipe(Host_build_pipe_config0(type, token, ep_num),\
00170 Host_build_pipe_config1(size, bank) ))
00172
00177 #define Usb_enable_regulator() (REGCR &= ~(1<<REGDIS))
00179 #define Usb_disable_regulator() (REGCR |= (1<<REGDIS))
00181 #define Is_usb_regulator_enabled() ((REGCR & (1<<REGDIS)) ? FALSE : TRUE)
00183
00187
00189 #define Usb_enable() (USBCON |= ((1<<USBE) ))
00191 #define Usb_disable() (USBCON &= ~((1<<USBE)))
00192 #define Is_usb_enabled() ((USBCON & (1<<USBE)) ? TRUE : FALSE)
00193
00194 #define Usb_enable_device() (USBCON |= (1<<USBE))
00195 #define Usb_disable_device() (USBCON &= ~(1<<USBE))
00196 #define Usb_reset_macro_only() (UDCON &= ~(1<<RSTCPU))
00197 #define Usb_reset_all_system() (UDCON |= (1<<RSTCPU))
00198
00199 #if (VBUS_SENSING_IO == ENABLED)
00201 #define Usb_vbus_sense_init() (VBUS_SENSE_DDR &= ~(1<<VBUS_SENSE_IO), \
00202 VBUS_SENSE_PORT &= ~(1<<VBUS_SENSE_IO))
00204 #define Is_usb_vbus_on() (((VBUS_SENSE_PIN&(1<<VBUS_SENSE_IO)) != 0) ? TRUE : FALSE)
00206 #define Is_usb_vbus_off() (((VBUS_SENSE_PIN&(1<<VBUS_SENSE_IO)) == 0) ? TRUE : FALSE)
00207 #endif
00208
00210 #define Usb_freeze_clock() (USBCON |= (1<<FRZCLK))
00211 #define Usb_unfreeze_clock() (USBCON &= ~(1<<FRZCLK))
00212 #define Is_usb_clock_freezed() ((USBCON & (1<<FRZCLK)) ? TRUE : FALSE)
00213
00214 #define Ps2_enable_device() (PS2CON |= (1<<PS2EN))
00215 #define Ps2_disable_device() (PS2CON &= ~(1<<PS2EN))
00216
00217 #define Usb_direct_drive_usb_enable() (UPOE |= (1<<UPWE1), UPOE &= ~(1<<UPWE0))
00218 #define Usb_direct_drive_ps2_enable() (UPOE |= ((1<<UPWE1) | (1<<UPWE0)))
00219 #define Usb_direct_drive_disable() (UPOE &= ~((1<<UPWE1) | (1<<UPWE0)))
00220 #define Usb_drive_dp_high() (UPOE |= (1<<UPDRV1))
00221 #define Usb_drive_dp_low() (UPOE &= ~(1<<UPDRV1))
00222 #define Usb_drive_dm_high() (UPOE |= (1<<UPDRV0))
00223 #define Usb_drive_dm_low() (UPOE &= ~(1<<UPDRV0))
00224 #define Ps2_drive_sck_high() (UPOE |= (1<<UPDRV1))
00225 #define Ps2_drive_sck_low() (UPOE &= ~(1<<UPDRV1))
00226 #define Ps2_drive_data_high() (UPOE |= (1<<UPDRV0))
00227 #define Ps2_drive_data_low() (UPOE &= ~(1<<UPDRV0))
00228
00229
00231 #define Usb_get_general_interrupt() (USBINT & (USBCON & MSK_IDTE_VBUSTE))
00233 #define Usb_ack_all_general_interrupt() (USBINT = ~(USBCON & MSK_IDTE_VBUSTE))
00234 #define Usb_ack_cache_id_transition(x) ((x) &= ~(1<<IDTI))
00235 #define Usb_ack_cache_vbus_transition(x) ((x) &= ~(1<<VBUSTI))
00236 #define Is_usb_cache_id_transition(x) (((x) & (1<<IDTI)) )
00237 #define Is_usb_cache_vbus_transition(x) (((x) & (1<<VBUSTI)))
00238
00240
00241
00246 #define Usb_initiate_remote_wake_up() (UDCON |= (1<<RMWKUP))
00248 #define Usb_detach() (UDCON |= (1<<DETACH))
00250 #define Usb_attach() (UDCON &= ~(1<<DETACH))
00252 #define Is_usb_pending_remote_wake_up() ((UDCON & (1<<RMWKUP)) ? TRUE : FALSE)
00254 #define Is_usb_detached() ((UDCON & (1<<DETACH)) ? TRUE : FALSE)
00255
00257 #define Usb_get_device_interrupt() (UDINT & (1<<UDIEN))
00259 #define Usb_ack_all_device_interrupt() (UDINT = ~(1<<UDIEN))
00260
00262 #define Usb_enable_remote_wake_up_interrupt() (UDIEN |= (1<<UPRSME))
00264 #define Usb_disable_remote_wake_up_interrupt() (UDIEN &= ~(1<<UPRSME))
00265 #define Is_remote_wake_up_interrupt_enabled() ((UDIEN & (1<<UPRSME)) ? TRUE : FALSE)
00267 #define Usb_ack_remote_wake_up_start() (UDINT = ~(1<<UPRSMI))
00269 #define Is_usb_remote_wake_up_start() ((UDINT & (1<<UPRSMI)) ? TRUE : FALSE)
00270
00272 #define Usb_enable_resume_interrupt() (UDIEN |= (1<<EORSME))
00274 #define Usb_disable_resume_interrupt() (UDIEN &= ~(1<<EORSME))
00275 #define Is_resume_interrupt_enabled() ((UDIEN & (1<<EORSME)) ? TRUE : FALSE)
00277 #define Usb_ack_resume() (UDINT = ~(1<<EORSMI))
00279 #define Is_usb_resume() ((UDINT & (1<<EORSMI)) ? TRUE : FALSE)
00280
00282 #define Usb_enable_wake_up_interrupt() (UDIEN |= (1<<WAKEUPE))
00284 #define Usb_disable_wake_up_interrupt() (UDIEN &= ~(1<<WAKEUPE))
00285 #define Is_wake_up_interrupt_enabled() ((UDIEN & (1<<WAKEUPE)) ? TRUE : FALSE)
00287 #define Usb_ack_wake_up() (UDINT = ~(1<<WAKEUPI))
00289 #define Is_usb_wake_up() ((UDINT & (1<<WAKEUPI)) ? TRUE : FALSE)
00290
00292 #define Usb_enable_reset_interrupt() (UDIEN |= (1<<EORSTE))
00294 #define Usb_disable_reset_interrupt() (UDIEN &= ~(1<<EORSTE))
00295 #define Is_reset_interrupt_enabled() ((UDIEN & (1<<EORSTE)) ? TRUE : FALSE)
00297 #define Usb_ack_reset() (UDINT = ~(1<<EORSTI))
00299 #define Is_usb_reset() ((UDINT & (1<<EORSTI)) ? TRUE : FALSE)
00300
00302 #define Usb_enable_sof_interrupt() (UDIEN |= (1<<SOFE))
00304 #define Usb_disable_sof_interrupt() (UDIEN &= ~(1<<SOFE))
00305 #define Is_sof_interrupt_enabled() ((UDIEN & (1<<SOFE)) ? TRUE : FALSE)
00307 #define Usb_ack_sof() (UDINT = ~(1<<SOFI))
00309 #define Is_usb_sof() ((UDINT & (1<<SOFI)) ? TRUE : FALSE)
00310
00312 #define Usb_enable_suspend_interrupt() (UDIEN |= (1<<SUSPE))
00314 #define Usb_disable_suspend_interrupt() (UDIEN &= ~(1<<SUSPE))
00316 #define Is_suspend_interrupt_enabled() ((UDIEN & (1<<SUSPE)) ? TRUE : FALSE)
00318 #define Usb_ack_suspend() (UDINT = ~(1<<SUSPI))
00320 #define Is_usb_suspend() ((UDINT & (1<<SUSPI)) ? TRUE : FALSE)
00321
00323 #define Usb_enable_address() (UDADDR |= (1<<ADDEN))
00325 #define Usb_disable_address() (UDADDR &= ~(1<<ADDEN))
00327 #define Is_usb_addressed() ((UDADDR & (1<<ADDEN)) ? TRUE : FALSE)
00329 #define Usb_configure_address(addr) (UDADDR = (UDADDR & (1<<ADDEN)) | ((U8)addr & MSK_UADD))
00330
00332 #define Usb_frame_number() ((U16)((((U16)UDFNUMH) << 8) | ((U16)UDFNUML)))
00334 #define Is_usb_frame_number_crc_error() ((UDMFN & (1<<FNCERR)) ? TRUE : FALSE)
00336
00337
00338
00339
00344 #define Usb_select_endpoint(ep) (UENUM = (U8)ep )
00345
00347 #define Usb_get_selected_endpoint() (UENUM )
00348
00350 #define Usb_reset_endpoint(ep) (UERST = 1 << (U8)ep, UERST = 0)
00351
00353 #define Usb_enable_endpoint() (UECONX |= (1<<EPEN))
00355 #define Usb_enable_stall_handshake() (UECONX |= (1<<STALLRQ))
00357 #define Usb_reset_data_toggle() (UECONX |= (1<<RSTDT))
00359 #define Usb_disable_endpoint() (UECONX &= ~(1<<EPEN))
00361 #define Usb_disable_stall_handshake() (UECONX |= (1<<STALLRQC))
00363 #define Usb_select_epnum_for_cpu() (UECONX &= ~(1<<EPNUMS))
00365 #define Is_usb_endpoint_enabled() ((UECONX & (1<<EPEN)) ? TRUE : FALSE)
00367 #define Is_usb_endpoint_stall_requested() ((UECONX & (1<<STALLRQ)) ? TRUE : FALSE)
00368
00370 #define Usb_configure_endpoint_type(type) (UECFG0X = (UECFG0X & ~(MSK_EPTYPE)) | ((U8)type << 6))
00372 #define Usb_configure_endpoint_direction(dir) (UECFG0X = (UECFG0X & ~(1<<EPDIR)) | ((U8)dir))
00373
00375 #define Usb_configure_endpoint_size(size) (UECFG1X = (UECFG1X & ~MSK_EPSIZE) | ((U8)size << 4))
00377 #define Usb_configure_endpoint_bank(bank) (UECFG1X = (UECFG1X & ~MSK_EPBK) | ((U8)bank << 2))
00379 #define Usb_allocate_memory() (UECFG1X |= (1<<ALLOC))
00381 #define Usb_unallocate_memory() (UECFG1X &= ~(1<<ALLOC))
00382
00384 #define Usb_ack_overflow_interrupt() (UESTA0X &= ~(1<<OVERFI))
00386 #define Usb_ack_underflow_interrupt() (UESTA0X &= ~(1<<UNDERFI))
00388 #define Usb_ack_zlp() (UESTA0X &= ~(1<<ZLPSEEN))
00390 #define Usb_data_toggle() ((UESTA0X&MSK_DTSEQ) >> 2)
00392 #define Usb_nb_busy_bank() (UESTA0X & MSK_NBUSYBK)
00394 #define Is_usb_one_bank_busy() ((UESTA0X & MSK_NBUSYBK) == 0 ? FALSE : TRUE)
00396 #define Is_endpoint_configured() ((UESTA0X & (1<<CFGOK)) ? TRUE : FALSE)
00398 #define Is_usb_overflow() ((UESTA0X & (1<<OVERFI)) ? TRUE : FALSE)
00400 #define Is_usb_underflow() ((UESTA0X & (1<<UNDERFI)) ? TRUE : FALSE)
00402 #define Is_usb_zlp() ((UESTA0X & (1<<ZLPSEEN)) ? TRUE : FALSE)
00403
00405 #define Usb_control_direction() ((UESTA1X & (1<<CTRLDIR)) >> 2)
00407 #define Usb_current_bank() ( UESTA1X & MSK_CURRBK)
00408
00410 #define Usb_ack_fifocon() (UEINTX &= ~(1<<FIFOCON))
00412 #define Usb_ack_nak_in() (UEINTX &= ~(1<<NAKINI))
00414 #define Usb_ack_nak_out() (UEINTX &= ~(1<<NAKOUTI))
00416 #define Usb_ack_receive_setup() (UEINTX &= ~(1<<RXSTPI))
00418 #define Usb_ack_receive_out() (UEINTX &= ~(1<<RXOUTI), Usb_ack_fifocon())
00420 #define Usb_ack_stalled() (MSK_STALLEDI= 0)
00422 #define Usb_ack_in_ready() (UEINTX &= ~(1<<TXINI), Usb_ack_fifocon())
00424 #define Usb_kill_last_in_bank() (UENTTX |= (1<<RXOUTI))
00426 #define Is_usb_read_enabled() (UEINTX&(1<<RWAL))
00428 #define Is_usb_write_enabled() (UEINTX&(1<<RWAL))
00430 #define Is_usb_read_control_enabled() (UEINTX&(1<<TXINI))
00432 #define Is_usb_nak_in_sent() (UEINTX&(1<<NAKINI))
00434 #define Is_usb_nak_out_sent() (UEINTX&(1<<NAKOUTI))
00436 #define Is_usb_receive_setup() (UEINTX&(1<<RXSTPI))
00438 #define Is_usb_receive_out() (UEINTX&(1<<RXOUTI))
00440 #define Is_usb_in_ready() (UEINTX&(1<<TXINI))
00442 #define Usb_send_in() (UEINTX &= ~(1<<FIFOCON))
00444 #define Usb_send_control_in() (UEINTX &= ~(1<<TXINI))
00446 #define Usb_free_out_bank() (UEINTX &= ~(1<<FIFOCON))
00448 #define Usb_ack_control_out() (UEINTX &= ~(1<<RXOUTI))
00449
00451 #define Usb_enable_flow_error_interrupt() (UEIENX |= (1<<FLERRE))
00453 #define Usb_enable_nak_in_interrupt() (UEIENX |= (1<<NAKINE))
00455 #define Usb_enable_nak_out_interrupt() (UEIENX |= (1<<NAKOUTE))
00457 #define Usb_enable_receive_setup_interrupt() (UEIENX |= (1<<RXSTPE))
00459 #define Usb_enable_receive_out_interrupt() (UEIENX |= (1<<RXOUTE))
00461 #define Usb_enable_stalled_interrupt() (UEIENX |= (1<<STALLEDE))
00463 #define Usb_enable_in_ready_interrupt() (UEIENX |= (1<<TXIN))
00465 #define Usb_disable_flow_error_interrupt() (UEIENX &= ~(1<<FLERRE))
00467 #define Usb_disable_nak_in_interrupt() (UEIENX &= ~(1<<NAKINE))
00469 #define Usb_disable_nak_out_interrupt() (UEIENX &= ~(1<<NAKOUTE))
00471 #define Usb_disable_receive_setup_interrupt() (UEIENX &= ~(1<<RXSTPE))
00473 #define Usb_disable_receive_out_interrupt() (UEIENX &= ~(1<<RXOUTE))
00475 #define Usb_disable_stalled_interrupt() (UEIENX &= ~(1<<STALLEDE))
00477 #define Usb_disable_in_ready_interrupt() (UEIENX &= ~(1<<TXIN))
00478
00480 #define Usb_read_byte() (UEDATX)
00482 #define Usb_write_byte(byte) (UEDATX = (U8)byte)
00483
00485 #define Usb_byte_counter() Usb_byte_counter_8()
00486 #define Usb_byte_counter_8() ((U8)UEBCLX)
00487
00489 #define Usb_interrupt_flags() (UEINT)
00491 #define Is_usb_endpoint_event() (Usb_interrupt_flags() != 0x00)
00493
00494
00495
00502
00503 #define wSWAP(x) \
00504 ( (((x)>>8)&0x00FF) \
00505 | (((x)<<8)&0xFF00) \
00506 )
00507
00508
00516 #if !defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
00517 #error YOU MUST Define the Endian Type of target: LITTLE_ENDIAN or BIG_ENDIAN
00518 #endif
00519 #ifdef LITTLE_ENDIAN
00520 #define Usb_write_word_enum_struc(x) (x)
00521 #else //BIG_ENDIAN
00522 #define Usb_write_word_enum_struc(x) (wSWAP(x))
00523 #endif
00524
00525
00527
00528
00529
00530 U8 usb_config_ep (U8, U8);
00531 U8 usb_select_enpoint_interrupt (void);
00532 U16 usb_get_nb_byte_epw (void);
00533 U8 usb_send_packet (U8 , U8*, U8);
00534 U8 usb_read_packet (U8 , U8*, U8);
00535 void usb_halt_endpoint (U8);
00536 void usb_reset_endpoint (U8);
00537 U8 usb_init_device (void);
00538
00539
00540 #endif // _USB_DRV_H_
00541