📄 usbio.c
字号:
// send a USBCB to the host indicating to stop waiting for data
usbcb.ulCount = 0x0;
usbcb.ulCommand = USBIO_CLOSE;
usbcb.ulData = (ULONG)fd;
// wait for host to take the USBCB
Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// return success
return 1;
}
//////////////////////////////////////////////////////////////////////////////
// int usbio_write(int fd, unsigned char *buf, int size)
//////////////////////////////////////////////////////////////////////////////
int usbio_write( int fd, unsigned char *buf, int size )
{
int bytes_written = 0x0; // track bytes written overall
int bytes_current = 0x0; // track bytes written current
int bytes_left = size; // track bytes left to write
int Result; // result
bool bClosed = false; // closed flag
bool bError = false; // error flag
// init buffers
UsbcbBuffer.Data = &usbcb;
UsbcbBuffer.ElementCount = sizeof(usbcb);
UsbcbBuffer.ElementWidth = 1;
UsbcbBuffer.CallbackParameter = NULL;
UsbcbBuffer.pNext = NULL;
DataBuffer.ElementWidth = 1;
DataBuffer.CallbackParameter = NULL;
DataBuffer.pNext = NULL;
// loop until all the bytes are written or we get closed or error
while ( (bytes_written < size) && !bClosed && !bError )
{
// limit each write to MAX_DATA_BYTES_EZEXTENDER bytes
if ( bytes_left > MAX_DATA_BYTES_EZEXTENDER)
bytes_current = MAX_DATA_BYTES_EZEXTENDER;
else
bytes_current = bytes_left;
// setup USBCB
usbcb.ulCommand = USBIO_WRITE; // command
usbcb.ulData = fd; // file descriptor
usbcb.ulCount = bytes_current; // how many data bytes to write this time
// wait for host to take the USBCB
Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// update data buffer count and pointer
DataBuffer.ElementCount = bytes_current;
DataBuffer.Data = buf + bytes_written;
// wait for host to take the data
Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&DataBuffer);
// host will send back USBCB containing actual bytes written which can differ
// from what was requested and flag indicating if file closed or error occured
Result = adi_dev_Read(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// update bytes written and bytes left
bytes_written += usbcb.ulCount;
bytes_left = size - bytes_written;
// check for closed or error, this would break us out of the loop
if ( 0x0 == usbcb.ulData )
bClosed = true;
else if ( 0xffffffff == usbcb.ulData )
bError = true;
}
// if closed and we didn't write any data return 0x0
if ( bClosed && (usbcb.ulCount == 0x0) )
return 0x0;
// else if error return negative value
else if ( bError )
return -1;
// else return total bytes written
else
return bytes_written;
}
//////////////////////////////////////////////////////////////////////////////
// int usbio_read(int fd, unsigned char *buf, int size)
//////////////////////////////////////////////////////////////////////////////
int usbio_read( int fd, unsigned char *buf, int size )
{
int bytes_read = 0x0; // track bytes read overall
int bytes_current = 0x0; // track bytes read current
int bytes_left = size; // track bytes left to read
int Result; // result
bool bEOF = false; // EOF flag
bool bError = false; // error flag
// init USBCB buffer
UsbcbBuffer.Data = &usbcb;
UsbcbBuffer.ElementCount = sizeof(usbcb);
UsbcbBuffer.ElementWidth = 1;
UsbcbBuffer.CallbackParameter = NULL;
UsbcbBuffer.pNext = NULL;
DataBuffer.ElementWidth = 1;
DataBuffer.CallbackParameter = NULL;
DataBuffer.pNext = NULL;
// loop until all the bytes are read or we get EOF or error
while ( (bytes_read < size) && !bEOF && !bError )
{
// limit each read to MAX_DATA_BYTES_EZEXTENDER bytes
if ( bytes_left > MAX_DATA_BYTES_EZEXTENDER)
bytes_current = MAX_DATA_BYTES_EZEXTENDER;
else
bytes_current = bytes_left;
// setup the USBCB
usbcb.ulCommand = USBIO_READ; // command
usbcb.ulData = fd; // file descriptor
usbcb.ulCount = bytes_current; // how many data bytes to read this time
// wait for host to take the USBCB
Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// host will send back USBCB containing actual bytes read which can differ
// from what was requested and flag indicating if end-of-file or error occured
Result = adi_dev_Read(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// if there's data to read, get it
if ( usbcb.ulCount )
{
// update data buffer count and pointer
DataBuffer.Data = buf + bytes_read;
DataBuffer.ElementCount = usbcb.ulCount; // actual bytes to read
// then wait for the host to send the data
Result = adi_dev_Read(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&DataBuffer);
}
// update bytes read and bytes left
bytes_read += usbcb.ulCount;
bytes_left = size - bytes_read;
// check for EOF or error, this would break us out of the loop
if ( 0x0 == usbcb.ulData )
bEOF = true;
else if ( 0xffffffff == usbcb.ulData )
bError = true;
// for now if STDIN, just get data one time because we're requesting
// more than they'll probably enter
if ( USBIO_STDIN_FD == fd )
bEOF = true;
}
// if EOF and we didn't read any data return 0x0
if ( bEOF && (usbcb.ulCount == 0x0) )
return 0x0;
// else if error return negative value
else if ( bError )
return -1;
// else return total bytes read
else
return bytes_read;
}
//////////////////////////////////////////////////////////////////////////////
// long usbio_seek(int fd, long offset, int whence)
//////////////////////////////////////////////////////////////////////////////
long usbio_seek( int fd, long offset, int whence )
{
int Result; // result
// init buffers
UsbcbBuffer.Data = &usbcb;
UsbcbBuffer.ElementCount = sizeof(usbcb);
UsbcbBuffer.ElementWidth = 1;
UsbcbBuffer.CallbackParameter = NULL;
UsbcbBuffer.pNext = NULL;
// determine the origin
if ( 0x0 == whence )
usbcb.ulCommand = USBIO_SEEK_SET;
else if ( 0x1 == whence )
usbcb.ulCommand = USBIO_SEEK_CUR;
else if ( 0x2 == whence )
usbcb.ulCommand = USBIO_SEEK_END;
usbcb.ulData = fd; // file descriptor
usbcb.ulCount = offset; // how many bytes from origin
// wait for host to take the USBCB
Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// host will send back USBCB containing new file position and status
Result = adi_dev_Read(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// if there was no error return the new file position
if ( 0x0 == usbcb.ulData )
return usbcb.ulCount;
// else there was an error
else
return -1;
}
/******************************************************************************
Routine Description:
Checks to see if a command is supported by this firmware. The host can use
this to verify that a command it wants to execute is supported ahead of time.
Arguments:
ADI_DEV_DEVICE_HANDLE dh - device handle
u32 u32Command - command we are querying on
Return Value:
unsigned int - return status
******************************************************************************/
unsigned int QuerySupport( ADI_DEV_DEVICE_HANDLE devhandle, u32 u32Command )
{
int Result; // result
// init buffer
UsbcbBuffer.Data = &usbcb;
UsbcbBuffer.ElementCount = sizeof(usbcb);
UsbcbBuffer.ElementWidth = 1;
UsbcbBuffer.CallbackParameter = NULL;
UsbcbBuffer.pNext = NULL;
// check for a supported command
if ( QUERY_SUPPORT == u32Command || GET_FW_VERSION == u32Command ||
USBIO_START == u32Command || MEMORY_READ == u32Command ||
MEMORY_WRITE == u32Command )
usbcb.ulData = TRUE;
else
usbcb.ulData = FALSE;
// send a USBCB to the host with the result
usbcb.ulCount = 0x0;
usbcb.ulCommand = QUERY_REPLY;
// wait for host to take the USBCB
Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);
// return success
return Result;
}
/******************************************************************************
Routine Description:
Reads data from Blackfin memory and sends it back to the host.
Arguments:
ADI_DEV_DEVICE_HANDLE dh - device handle
u8 *p8Address - Blackfin address to read from
u32 u32Count - number of bytes to read
Return Value:
unsigned int - return status
******************************************************************************/
unsigned int ReadMemory( ADI_DEV_DEVICE_HANDLE devhandle, u8 *p8Address, u32 u32Count )
{
DataBuffer.Data = p8Address;
DataBuffer.ElementCount = u32Count;
DataBuffer.ElementWidth = 1;
DataBuffer.CallbackParameter = NULL;
DataBuffer.ProcessedFlag = FALSE;
DataBuffer.ProcessedElementCount = 0;
DataBuffer.pNext = NULL;
// send the data back to the host
return adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&DataBuffer);
}
/******************************************************************************
Routine Description:
Receives data from the host and writes it to Blackfin memory.
Arguments:
ADI_DEV_DEVICE_HANDLE dh - device handle
u8 *p8Address - Blackfin address to write to
u32 u32Count - number of bytes to write
Return Value:
unsigned int - return status
******************************************************************************/
unsigned int WriteMemory( ADI_DEV_DEVICE_HANDLE devhandle, u8 *p8Address, u32 u32Count )
{
DataBuffer.Data = p8Address;
DataBuffer.ElementCount = u32Count;
DataBuffer.ElementWidth = 1;
DataBuffer.CallbackParameter = NULL;
DataBuffer.ProcessedFlag = FALSE;
DataBuffer.ProcessedElementCount = 0;
DataBuffer.pNext = NULL;
// get the data from the host
return adi_dev_Read(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&DataBuffer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -