📄 df.c
字号:
return OK; // Write done
}
//!
//! @brief This function is optimized and writes nb-sector * 512 Bytes from
//! DataFlash memory to USB HOST controller
//!
//! DATA FLOW is: DF => USB
//!
//!
//! NOTE:
//! - This function should ne used only when using the USB controller in HOST mode
//! - First call must be preceded by a call to the df_read_open() function,
//! - The USB PIPE IN must have been previously selected,
//! - USB ping-pong buffers are free,
//! - As 512 is always a sub-multiple of page size, there is no need to check
//! page end for each Bytes,
//! - Interrupts are disabled during transfer to avoid timer interrupt,
//! - nb_sector always >= 1, cannot be zero.
//!
//! @warning code:?? bytes (function code length)
//!
//! @param nb_sector number of contiguous sectors to read [IN]
//!
//! @return bit
//! The read succeeded -> OK
//!/
bit df_host_read_sector (Uint16 nb_sector)
{
U8 i;
do
{
for (i = 8; i != 0; i--)
{
Disable_interrupt(); // Global disable.
// Principle: send any Byte to get a Byte.
// Spi_write_dummy(): send any Byte + 1st step to clear the SPIF bit.
// Spi_read_data(): get the Byte + final step to clear the SPIF bit.
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Spi_write_dummy(); Usb_write_byte(Spi_read_data());
Enable_interrupt(); // Global re-enable.
//#
//# Send the USB FIFO IN content to the USB Host.
//#
Host_send_out(); // Send the FIFO from the USB Host.
// Wait until the tx is done so that we may write to the FIFO IN again.
while(Is_host_out_sent()==FALSE);
Host_ack_out_sent();
}
gl_ptr_mem += 512; // increment global address pointer
nb_sector--; // 1 more sector read
#if (DF_NB_MEM == 1) // end of page ?
#if (DF_PAGE_SIZE == 512)
Df_desel_all();
if (nb_sector != 0)
df_read_open(gl_ptr_mem>>9);
#else
if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
{
Df_desel_all();
if (nb_sector != 0)
df_read_open(gl_ptr_mem>>9);
}
#endif
#endif
}
while (nb_sector != 0);
return OK; // Read done.
}
//! @brief This function read one DF sector and load it into a ram buffer
//!
//! DATA FLOW is: DF => RAM
//!
//!
//! NOTE:
//! - First call must be preceded by a call to the df_read_open() function,
//!
//! @param *ram pointer to ram buffer
//!
//! @return bit
//! The read succeeded -> OK
//!/
bit df_read_sector_2_ram(U8 *ram)
{
U16 i;
for(i=0;i<DF_SECTOR_SIZE;i++)
{
Spi_write_dummy();
*ram=Spi_read_data();
ram++;
}
gl_ptr_mem += 512; // Update the memory pointer.
return OK;
}
//! @brief This function write one DF sector from a ram buffer
//!
//! DATA FLOW is: RAM => DF
//!
//!
//! NOTE:
//! - First call must be preceded by a call to the df_write_open() function,
//!
//! @param *ram pointer to ram buffer
//!
//! @return bit
//! The read succeeded -> OK
//!
bit df_write_sector_from_ram(U8 *ram)
{
U16 i;
for(i=0;i<DF_SECTOR_SIZE;i++)
{
Spi_write_data(*ram);
*ram++;
}
Spi_ack_write(); // Final step to clear the SPIF bit.
gl_ptr_mem += 512; // Update the memory pointer.
return OK;
}
/*F**************************************************************************
* NAME: df_format
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* Address of the format parameter structure in code
*----------------------------------------------------------------------------
* PURPOSE:
* This function is called by the fat_format function and returns a pointer
* to a table containing the format parameters.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* DF FORMAT PARAMETERS
* CAPACITY LBAs CYL HDs S/T CLUSTs S/C S/F FAT HID
* 4MB 8160 255 4 8 8 15
* 8MB 16320 510 4 8 8 12 12 15
* 16MB 32640 510 4 16 4080 8 12 12 15
* 32MB 65280 510 8 16 8160 8 12 16 15
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
s_format * df_format (void)
{
/* -- DF Type Selection -- */
switch (DF_DISK_SIZE)
{
case DF_SIZE_4MB:
return (s_format*)&df_tab_format[DF_4MB];
break;
case DF_SIZE_8MB:
return (s_format*)&df_tab_format[DF_8MB];
break;
case DF_SIZE_16MB:
return (s_format*)&df_tab_format[DF_16MB];
break;
case DF_SIZE_32MB:
return (s_format*)&df_tab_format[DF_32MB];
break;
default:
break;
}
return(NULL);
}
#ifdef DF_BYTE_ACCESS
//!
//! @brief This function performs a single Byte read from DF memory.
//!
//! NOTE:
//! - First call must be preceded by a call to the df_read_open() function,
//!
//! @warning for debug purpose only (not fully validated)
//!
//! @param gl_ptr_mem memory ptr on DF [global][IN/OUT]
//!
//! @return Byte the read Byte
//!/
Byte df_read_byte (void)
{
//#
//# Check if end of page.
//# If so, eventually select the next DF and open the new page.
//#
if ((MSB3(gl_ptr_mem) == 0x00) && ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00))
{
Df_desel_all(); // Unselect all memories.
// Select the current DF memory and open the new page.
// NOTE: df_read_open() input parameter is expressed in 512B-sector address.
df_read_open(gl_ptr_mem>>9);
}
//#
//# Read Byte.
//#
gl_ptr_mem++;
Spi_write_dummy(); // Dummy write to read data
return Spi_read_data(); // Read current data.
}
//!
//! @brief This function writes a Byte at the current location in DF memory.
//!
//! NOTE:
//! - First call must be preceded by a call to the df_write_open() function,
//!
//! @warning for debug purpose only (not fully validated)
//!
//! @param b Byte to program [IN]
//! @param gl_ptr_mem memory ptr on DF [global][IN/OUT]
//!
//! @return bit
//! The write succeeded -> OK
//!/
bit df_write_byte (const Byte b)
{
if (df_mem_busy)
{ // memory busy
// Being here, we know that we previously launch a page programming
// => we have to access a new page.
//#
//# Access the next page (eventually first select the next DF memory).
//#
// NOTE: df_write_open() input param is expressed in 512B-sector address.
df_write_open(gl_ptr_mem>>9);
//#
//# Write the Byte to memory.
//#
Spi_write_data(b); // (.) Write current Byte
// (.) 1st step to clear the SPIF bit.
Spi_ack_write(); // Final step to clear the SPIF bit.
gl_ptr_mem++;
}
else
{ // memory ready
//#
//# Write the Byte to memory.
//#
Spi_write_data(b); // (.) Write current Byte
// (.) 1st step to clear the SPIF bit.
Spi_ack_write(); // Final step to clear the SPIF bit.
gl_ptr_mem++;
// Check if end of page
if ((MSB3(gl_ptr_mem) == 0x00) && ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00))
{
Df_desel_all(); // Launch page programming
df_mem_busy = TRUE; // memory is busy
}
}
return OK; // Write done.
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -