📄 pic_usb.h
字号:
add+=(endpoint*8); //point to BDxIST
i = (*add) & 0x80;
if (i) {return(0);}
else {
add+=2; //point to BDxIAL
buff_add=(long)0x100 + *add;
for (i=0;i<len;i++) {
*buff_add=*ptr;
debug(debug_txb,"%X ",*buff_add);
buff_add++;
ptr++;
}
add--;//point back to buffer counter;
*add=len; //set buffer count to what we said
add--;//point at status register;
if (tgl == TOGGLE) {
// printf("%X ",i);
i=*add;//get current status register. notice that bdstatus is dual nature, the bits have different meaning if
//you read or write to them. bits 7 and 6, which we want to modify now, are the same in both though
i&=0xC0; //clear bits 5-0;
i^=0x40; //toggle data0/1 bit
#IF USB_PUT_DTS
i|=0x88;
#ELSE
i|=0x80; //set UOWN bit. USB is now assigned to BD. do not set dts (data toggle synch)
#ENDIF
}
else if (tgl == DATA1) {
#IF USB_PUT_DTS
i=0xC8;
#ELSE
i=0xC0;
#ENDIF
} //DATA1, UOWN
else if (tgl == DATA0) {
#IF USB_PUT_DTS
i=0x88;
#ELSE
i=0x80;
#ENDIF
} //DATA0, UOWN
*add=i;//save changes
// printf("%LX %X",add,*add);
debug(debug_txb," %X", i);
}
}
/// END User Functions
/// BEGIN Hardware layer functions required by USB.C
/*******************************************************************************
/* usb_get_packet(endpoint, *ptr, max)
/*
/* Input: endpoint - endpoint to get data from
/* ptr - where to save data to local PIC RAM
/* max - max amount of data to receive from buffer
/*
/* Output: the amount of data taken from the buffer. If no data is available in the buffer,
/* or there was an error, will return FALSE.
/*
/* Summary: Gets a packet of data from the USB buffer and puts into local PIC RAM.
/* You could poll usb_get_packet() until it doesn't return 0, or you could
/* poll usb_epX_rx_status.rx (where X is endpoint number) until .rx = 1.
/* usb_kbhit(endpoint) (located in usb.c) will also poll usb_epX_rx_status.rx,
/* but is easier to read.
/*
/********************************************************************************/
int8 usb_get_packet(int8 endpoint, int8 * ptr, int8 max) {
int8 * buff = 0x1A1; //BD0OBC - EP0 OUT (in to PIC) byte count
int8 * al=0x100;
int8 len;
int8 i=0;
buff+=endpoint*8; //goto BDxOL
len=*buff;
buff++; //goto BDxAL
al+=*buff;
debug(debug_txb,"\r\nRX %X %X: ", endpoint,len);
while ((i<len)&&(i<max)) {
debug("%X ",*al);
*ptr=*al;
ptr++;
al++;
i++;
}
buff--; //goto bc
*buff=USB_MAX_EP0_PACKET_LENGTH;
buff--; //goto status
i=*buff;//get current status register. notice that bdstatus is dual nature, the bits have different meaning if
//you read or write to them. bits 7 and 6, which we want to modify now, are the same in both though
i &= 0xC0; //clear bits 5-0;
i ^= 0x40; //toggle data0/1 bit
#IF USB_GET_DTS
i |= 0x88; //set own and dts bit
#ELSE
i |= 0x80; //set own
#ENDIF
*buff=i;
debug(debug_txb," %X",i);
return(i);
}
/*******************************************************************************
/* usb_stall_ep(endpoint,direction)
/*
/* Input: endpoint - endpoint to stall.
/* direction - direction of endpoint. 0 == OUT or CONTROL, 1 == IN
/*
/* Summary: Stalls specified endpoint. If endpoint is stalled it will NAK any tokens
/* destined to that endpoint.
/*
/********************************************************************************/
void usb_stall_ep(int8 endpoint, int1 direction) {
int8 * addy=0x1A0;
int8 bd_stat;
if (endpoint < USB_MAX_ENDPOINTS) { //pic has only 3 endpoints. starts at 0 (0, 1, 2)
addy+=endpoint*8;
if (direction) {addy+=4;}
bd_stat=*addy;
bd_stat=(bd_stat & 0xC0) | 0x0C;
*addy=bd_stat;
if (direction) {
USB_endpoint_in_stalled[endpoint]=1;
}
else {
USB_endpoint_out_stalled[endpoint]=1;
}
//debug
if (direction) {endpoint=endpoint | 0x80;}
debug(debug_txb, " SE%X",endpoint | (direction << 7));;
}
}
/*******************************************************************************
/* usb_unstall_ep(endpoint, direction)
/*
/* Input: endpoint - endpoint to un-stall.
/* direction - direction of endpoint. 0 == OUT or CONTROL, 1 == IN
/*
/* Summary: Un-stalls endpoint.
/*
/********************************************************************************/
void usb_unstall_ep(int8 endpoint, int1 direction) {
int8 * addy=0x1A0;
int8 bd_stat;
if (endpoint < USB_MAX_ENDPOINTS) { //pic has only 3 endpoints. starts at 0 (0, 1, 2)
addy+=endpoint*8;
if (direction) {addy+=4;}
bd_stat=*addy;
bd_stat=(bd_stat & 0xC0) | 0x08;
*addy=bd_stat;
addy=UEP0_LOC + endpoint;
bit_clear(*addy,0);
if (direction) {
USB_endpoint_in_stalled[endpoint]=0;
}
else {
USB_endpoint_out_stalled[endpoint]=0;
}
//debug
if (direction) {endpoint=endpoint | 0x80;}
debug(debug_txb, " USE%X",endpoint | (direction << 7));
}
}
/*******************************************************************************
/* usb_endpoint_stalled(endpoint, direction)
/*
/* Input: endpoint - endpoint to check
/* direction - direction of endpoint. 0 == OUT or CONTROL, 1 == IN
/*
/* Output: returns a TRUE if endpoint is stalled, FALSE if it is not.
/*
/* Summary: Looks to see if an endpoint is stalled, or not.
/*
/********************************************************************************/
int1 usb_endpoint_stalled(int8 endpoint, int1 direction) {
if (direction) {
return(USB_endpoint_in_stalled[endpoint]);
}
else {
return(USB_endpoint_out_stalled[endpoint]);
}
}
/*******************************************************************************
/* usb_set_address(address)
/*
/* Input: address - address the host specified that we use
/*
/* Summary: Configures the USB Peripheral for the specified device address. The host
/* will now talk to use with the following address.
/*
/********************************************************************************/
void usb_set_address(int8 address) {
UADDR=address;
if (address) {
USWSTAT=(USWSTAT & 0xFC) | 2; //if we got an address other than 0 set the enumeration status to ADDRESSED
}
else {
USWSTAT=(USWSTAT & 0xFC); //unconfigured state
}
}
/*******************************************************************************
/* usb_set_configured(config)
/*
/* Input: config - Configuration to use. 0 to uncofigure device.
/*
/* Summary: Configures or unconfigures device. If configuring device it will
/* enable all the endpoints. If un-configuring device it will disable all
/* endpoints.
/*
/* NOTE: CCS only provides code to handle 1 configuration.
/*
/********************************************************************************/
void usb_set_configured(int config) {
USB_Curr_Config=config;
USB_dev_req=SET_CONFIG;
USWSTAT=USWSTAT & 0xFC; //clear config bits for being set in next line
if (config==0) {USWSTAT=USWSTAT | ADDRESS_STATE;} //if config=0 then set addressed state
else {USWSTAT=USWSTAT | CONFIG_STATE;} //else set configed state
//configure ep1 and ep2 enpoints. will user want these differently?
/* //endpoints initialized DATA0
#IF USB_GET_DTS
BD1OST = 0x88; //set own and dts bit
#ELSE
BD1OST = 0x80; //set own
#ENDIF
BD1OAL=USB_Buffer+0x10;
BD1OBC=8;
#IF USB_PUT_DTS
BD1IST = 0x08; //set dts bit
#ELSE
BD1IST = 0x00;
#ENDIF
BD1IAL=USB_Buffer+0x18;
#IF USB_GET_DTS
BD2OST = 0x88; //set own and dts bit
#ELSE
BD2OST = 0x80; //set own
#ENDIF
BD2OAL=USB_Buffer+0x20;
BD2OBC=8;
*/
//IN endpoints initialized DATA1, OUT endpoints initialized to DATA0
#IF USB_GET_DTS
BD1OST = 0x88; //set own and dts bit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -