📄 sdk7a404_cf_driver.c
字号:
* Otherwise, set init to TRUE and save the CF device register set
* addresses. Return a pointer to the CF config structure to the
* caller.
*
* Parameters:
* ipbase: CompactFlash device address
* arg : Not used
*
* Outputs: None
*
* Returns: The pointer to a CompactFlash config structure or NULL
*
* Notes: None
*
**********************************************************************/
INT_32 cf_open(void *ipbase,
INT_32 arg)
{
INT_32 status = 0;
if ((cfcfg.init == FALSE) && ((INT_32) ipbase == CF_BASE))
{
/* Device is valid and not previously initialized */
cfcfg.init = TRUE;
/* Save address of registers and interface */
cfcfg.rregptr =
(CF_STATUS_READ_REG_T *) ((INT_32) ipbase + CF_OFFSET_CM);
cfcfg.wregptr =
(CF_STATUS_WRITE_REG_T *) ((INT_32) ipbase + CF_OFFSET_CM);
/* Return pointer to CF configuration structure */
status = (INT_32) &cfcfg;
}
return status;
}
/***********************************************************************
*
* Function: cf_close
*
* Purpose: Close the CompactFlash interface
*
* Processing:
* If init is not TRUE, then return _ERROR to the caller as the
* device was not previously opened. Otherwise, set init to FALSE,
* and return _NO_ERROR to the caller.
*
* Parameters:
* devid: Pointer to CF config structure
*
* Outputs: None
*
* Returns: The status of the close operation
*
* Notes: None
*
**********************************************************************/
STATUS cf_close(INT_32 devid)
{
CF_CFG_T *cfcfgptr = (CF_CFG_T *) devid;
STATUS status = _ERROR;
if (cfcfgptr->init == TRUE)
{
status = _NO_ERROR;
cfcfgptr->init = FALSE;
}
return status;
}
/***********************************************************************
*
* Function: cf_ioctl
*
* Purpose: CF configuration block
*
* Processing:
* This function is a large case block. Based on the passed function
* and option values, set or get the appropriate CF parameter.
*
* Parameters:
* devid: Pointer to CF config structure
* cmd: ioctl command
* arg: ioctl argument
*
* Outputs: None
*
* Returns: The status of the ioctl operation
*
* Notes: None
*
**********************************************************************/
STATUS cf_ioctl(INT_32 devid,
INT_32 cmd,
INT_32 arg)
{
UNS_32 tmp;
CF_STATUS_READ_REG_T *rptr;
CF_STATUS_WRITE_REG_T *wptr;
CF_CFG_T *cfcfgptr = (CF_CFG_T *) devid;
STATUS status = _ERROR;
if (cfcfgptr->init == TRUE)
{
status = _NO_ERROR;
rptr = cfcfgptr->rregptr;
wptr = cfcfgptr->wregptr;
switch (cmd)
{
case CF_READ_BLOCKS:
/* Issue command to read blocks */
status = cf_start_read((CF_BLOCKS_T *) arg, wptr);
break;
case CF_WRITE_BLOCKS:
/* Issue command to write blocks */
status = cf_start_write((CF_BLOCKS_T *) arg, wptr);
break;
case CF_ERASE_SECTOR:
/* Erase a single sector */
cf_set_seccmd((UNS_32) arg, CFC_E_SECT, wptr);
break;
case CF_GET_STATUS:
/* Return a CF interface status */
switch (arg)
{
case CF_CARD_DETECT:
/* Returns 1 if a CF card is inserted in the
EVB, otherwise 0 */
/* Read the CF status and mask off some bits */
tmp = (UNS_32) rptr->dup_sts_da & CF_DETECT_MASK;
if (tmp == CF_DETECT_VALUE)
{
/* Card has been detected */
status = 1;
}
else
{
/* No card detected */
status = 0;
}
break;
case CF_CARD_READY:
/* Returns 1 if the card is ready for a new
command, otherwise 0 */
if ((rptr->dup_sts_da & CF_RDY) != 0)
{
status = 1;
}
else
{
status = 0;
}
break;
case CF_CARD_BUSY:
/* Returns 1 if the card is busy, otherwise 0 */
if ((rptr->dup_sts_da & CF_BUSY) != 0)
{
status = 1;
}
else
{
status = 0;
}
break;
default:
/* Unsupported parameter */
status = SMA_BAD_PARAMS;
break;
}
break;
default:
/* Unsupported parameter */
status = SMA_BAD_PARAMS;
}
}
return status;
}
/***********************************************************************
*
* Function: cf_read
*
* Purpose: CompactFlash read function
*
* Processing:
* If the init flag for the CF structure is FALSE or the number of
* bytes to read is not 512, return 0 to the caller. Otherwise, loop
* until max_bytes equals 0. Inside the loop, read the data from
* the CF card into the user buffer, increment the user buffer
* pointer, decrement max_bytes by two, and increment bytes. When
* max_bytes expires to 0, return bytes to the caller.
*
* Parameters:
* devid: Pointer to CF config structure
* buffer: Pointer to data buffer to copy to
* max_bytes: Number of bytes to read
*
* Outputs: None
*
* Returns: Number of bytes actually read
*
* Notes: None
*
**********************************************************************/
INT_32 cf_read(INT_32 devid,
void *buffer,
INT_32 max_bytes)
{
CF_STATUS_READ_REG_T *rptr;
CF_CFG_T *cfcfgptr = (CF_CFG_T *) devid;
UNS_8 *data = (UNS_8 *) buffer;
UNS_16 tmpdata;
INT_32 secbytes, bytes = 0;
if ((cfcfgptr->init == TRUE) && (max_bytes >= CF_SECT_SIZE))
{
/* Limit read size to size of sector */
rptr = cfcfgptr->rregptr;
/* Loop until n_bytes expires to 0 */
secbytes = 0;
while (max_bytes > 0)
{
tmpdata = rptr->data;
*data = (UNS_8) (tmpdata & 0xFF);
data++;
*data = (UNS_8) (tmpdata >> 8);
data++;
max_bytes = max_bytes - 2;
bytes = bytes + 2;
secbytes = secbytes + 2;
if ((secbytes >= CF_SECT_SIZE) && (max_bytes > 0))
{
/* Small delay to allow next sector to load */
// for (secbytes = 0; secbytes < 100; secbytes++);
secbytes = 0;
while (cf_ioctl(devid, CF_GET_STATUS,
CF_CARD_BUSY) == 1);
}
}
}
return bytes;
}
/***********************************************************************
*
* Function: cf_write
*
* Purpose: CompactFlash write function
*
* Processing:
* If the init flag for the CF structure is FALSE or the number of
* bytes to write is not 512, return 0 to the caller. Otherwise,
* loop until n_bytes equals 0. Inside the loop, write the data from
* the user buffer into the CF card, increment the user buffer
* pointer, decrementn_bytes by two, and increment bytes. When
* n_bytes expires to 0, return bytes to the caller.
*
* Parameters:
* devid: Pointer to CF config structure
* buffer: Pointer to data buffer to copy from
* n_bytes: Number of bytes to write
*
* Outputs: None
*
* Returns: Number of bytes actually written
*
* Notes: None
*
**********************************************************************/
INT_32 cf_write(INT_32 devid,
void *buffer,
INT_32 n_bytes)
{
CF_STATUS_WRITE_REG_T *wptr;
CF_CFG_T *cfcfgptr = (CF_CFG_T *) devid;
UNS_8 *data = (UNS_8 *) buffer;
UNS_16 tmpdata;
volatile UNS_16 dummy;
INT_32 bytes = 0;
if ((cfcfgptr->init == TRUE) && (n_bytes >= CF_SECT_SIZE))
{
wptr = cfcfgptr->wregptr;
/* Loop until n_bytes expires to 0 */
while (n_bytes > 0)
{
tmpdata = ((UNS_16) data[bytes] |
(UNS_16) (data[bytes + 1] << 8));
wptr->data = tmpdata;
dummy = * (volatile UNS_16 *) CF_DUMMY_RD;
n_bytes = n_bytes - 2;
bytes = bytes + 2;
}
}
return bytes;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -