📄 sd.c
字号:
while (mmc_check_response() == MMC_ERR_RESP);
}
while ((mmc_read_response() & MMC_TRAN_STATE_MSK) != MMC_TRAN_STATE);
}
Mmc_set_multi_block(); /* data transmission in multiple blocks */
Mmc_set_write(); /* dir from uc to card */
Mmc_disable_flow_ctrl(); /* enable clock for command */
Mmc_reset_cmd_fifos();
Mmc_write_multiple_block_cmd(gl_ptr_mem); /* send write blocks */
gl_mem_tick = SD_RESP_TIME;
while (mmc_check_response() == MMC_ERR_RESP)
{
if (gl_mem_tick == SD_TIME_OUT)
{
return KO;
}
}
if ((mmc_read_response() & MMC_TRAN_STATE_MSK) != MMC_TRAN_STATE)
{
return KO;
}
else
{
Mmc_enable_flow_ctrl(); /* stop clock when FIFO 1&2 empty */
Mmc_enable_send(); /* send data immediately or after response */
return OK;
}
}
/*F**************************************************************************
* NAME: sd_write_close
*----------------------------------------------------------------------------
* PARAMS:
* global: gl_ptr_mem
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Memory card write close
* finish programming end of block
* release SD/MMC card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit sd_write_close (void)
{
while ((((Byte*)&gl_ptr_mem)[3] != 0x00) || ((((Byte*)&gl_ptr_mem)[2] & MMC_PAGE_MASK) != 0x00))
{
Mmc_wr_byte(0x00); /* dummy write to end of block */
gl_ptr_mem++;
}
Mmc_stop_tran_cmd();
Mmc_disable_flow_ctrl(); /* re-enable clock for CRC status */
gl_mem_tick = SD_RESP_TIME;
while (mmc_check_response() == MMC_ERR_RESP)
{
if (gl_mem_tick == SD_TIME_OUT)
{
return KO;
}
}
Mmc_reset_data_fifos();
sd_mem_busy = TRUE; /* memory is busy (programming) */
return OK;
}
/*F**************************************************************************
* NAME: sd_write_byte
*----------------------------------------------------------------------------
* PARAMS:
* b: data to write
* global: gl_ptr_mem
*
* return:
* write status: OK: write done
* KO: write not done
*----------------------------------------------------------------------------
* PURPOSE:
* Card byte write function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit sd_write_byte (Byte b)
{
gl_mem_tick = SD_WRITE_TIME;
while(Mmc_write_not_ready()) /* two fifos ares full */
{
if (gl_mem_tick == SD_TIME_OUT)
{
return KO;
}
}
Mmc_wr_byte(b); /* write 1 byte to card */
gl_ptr_mem++;
return OK; /* write done */
}
/*F**************************************************************************
* NAME: sd_write_sector
*----------------------------------------------------------------------------
* PARAMS:
* nb_sector: number of continuous sector to write
* global: gl_ptr_mem
*
* return:
* write status: OK: write done
* KO: write not done
*----------------------------------------------------------------------------
* PURPOSE:
* This function is an optimized function that writes nb-sector * 512 bytes
* from USB controller to SD/MMC card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* nb_sector always >= 1, can not be zero
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit sd_write_sector (Uint16 nb_sector)
{
Byte i;
do
{
for (i = 8; i != 0; i--) /* write 8x64b = 512b */
{
while (!Usb_rx_complete()); /* wait end of reception */
Mmc_wr_byte(Usb_read_byte()); /* write 16 bytes to FIFO */
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
while (Mmc_write_busy()); /* wait no more data in FIFO 1 */
Mmc_wr_byte(Usb_read_byte()); /* write 16 bytes to FIFO */
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
while (Mmc_write_busy()); /* wait no more data in FIFO 1 */
Mmc_wr_byte(Usb_read_byte()); /* write 16 bytes to FIFO */
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
while (Mmc_write_busy()); /* wait no more data in FIFO 1 */
Mmc_wr_byte(Usb_read_byte()); /* write 16 bytes to FIFO */
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Mmc_wr_byte(Usb_read_byte());
Usb_clear_RXOUT_PP(); /* usb read acknowledgement */
}
/* always end of page */
gl_ptr_mem += 512;
nb_sector--; /* 1 more sector written */
}
while (nb_sector != 0);
sd_mem_busy = TRUE;
return OK; /* write done */
}
/*F**************************************************************************
* NAME: sd_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:
* The following values are maximum. The number of cylinder is calculated
* with the real LBAs of the card.
* SD FORMAT PARAMETERS
* CAPACITY LBAs CYL HDs S/T CLUSTs S/C S/F FAT HID
* 4MB 8032 251 2 16 198 16 2 12 27
* 8MB 16224 507 2 16 1010 16 3 12 25
* 16MB 32448 507 2 32 1011 32 3 12 57
* 32MB 64896 507 4 32 2025 32 6 12 51
* 64MB 129792 507 8 32 4053 32 12 12 39
* 128MB 259584 1014 8 32 8106 32 32 16 95
* 256MB 519168 1014 16 32 16216 32 64 16 95
*
* MMC FORMAT PARAMETERS
* CAPACITY LBAs CYL HDs S/T CLUSTs S/C S/F FAT HID
* 4MB 8192 128 2 32 975 8 3 12 32
* 8MB 16384 256 2 32 1950 8 6 12 32
* 16MB 32768 512 2 32 3908 8 12 12 32
* 28MB 57344 448 4 32 13652 4 54 16 32
* 32MB 65536 512 4 32 15632 4 62 16 32
* 64MB 131072 512 8 32 31282 4 123 16 32
* 128MB 262144 1024 8 32 62581 4 245 16 32
* 256MB 524288 1024 16 32 62720 8 245 16 32
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
s_format * sd_format (void)
{
s_format pdata *tab;
tab = (s_format pdata *)gl_buffer;
if (sd_flag == SD)
{
code s_format sd_tab_format[]=
{
/* nb_cylinder, nb_head, nb_sector, nb_hidden, nb_sector_per_cluster */
{ (Uint16)0, (Byte)2, (Byte)16, (Byte)27, (Byte)16 }, /* 4MB */
{ (Uint16)0, (Byte)2, (Byte)16, (Byte)25, (Byte)16 }, /* 8MB */
{ (Uint16)0, (Byte)2, (Byte)32, (Byte)57, (Byte)32 }, /* 16MB */
{ (Uint16)0, (Byte)4, (Byte)32, (Byte)51, (Byte)32 }, /* 32MB */
{ (Uint16)0, (Byte)8, (Byte)32, (Byte)39, (Byte)32 }, /* 64MB */
{ (Uint16)0, (Byte)8, (Byte)32, (Byte)95, (Byte)32 }, /* 128MB */
{ (Uint16)0, (Byte)16, (Byte)32, (Byte)95, (Byte)32 }, /* 256MB */
};
/* -- SD Type Selection -- */
if (Sd_disk_size() <= SD_SIZE_4MB)
{
*tab = sd_tab_format[SD_4MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_4MB].nb_head *
sd_tab_format[SD_4MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= SD_SIZE_8MB)
{
*tab = sd_tab_format[SD_8MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_8MB].nb_head *
sd_tab_format[SD_8MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= SD_SIZE_16MB)
{
*tab = sd_tab_format[SD_16MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_16MB].nb_head *
sd_tab_format[SD_16MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= SD_SIZE_32MB)
{
*tab = sd_tab_format[SD_32MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_32MB].nb_head *
sd_tab_format[SD_32MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= SD_SIZE_64MB)
{
*tab = sd_tab_format[SD_64MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_64MB].nb_head *
sd_tab_format[SD_64MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= SD_SIZE_128MB)
{
*tab = sd_tab_format[SD_128MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_128MB].nb_head *
sd_tab_format[SD_128MB].nb_sector);
return tab;
}
else
{
*tab = sd_tab_format[SD_256MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[SD_256MB].nb_head *
sd_tab_format[SD_256MB].nb_sector);
return tab;
}
}
else
{
code s_format sd_tab_format[]=
{
/* nb_cylinder, nb_head, nb_sector, nb_hidden, nb_sector_per_cluster */
{ (Uint16)0, (Byte)2, (Byte)32, (Byte)32, (Byte)8 }, /* 4MB */
{ (Uint16)0, (Byte)2, (Byte)32, (Byte)32, (Byte)8 }, /* 8MB */
{ (Uint16)0, (Byte)2, (Byte)32, (Byte)32, (Byte)8 }, /* 16MB */
{ (Uint16)0, (Byte)4, (Byte)32, (Byte)32, (Byte)4 }, /* 28MB */
{ (Uint16)0, (Byte)4, (Byte)32, (Byte)32, (Byte)4 }, /* 32MB */
{ (Uint16)0, (Byte)8, (Byte)32, (Byte)32, (Byte)4 }, /* 64MB */
{ (Uint16)0, (Byte)8, (Byte)32, (Byte)32, (Byte)4 }, /* 128MB */
{ (Uint16)0, (Byte)16, (Byte)32, (Byte)32, (Byte)8 }, /* 256MB */
};
/* -- MMC Type Selection -- */
if (Sd_disk_size() <= MMC_SIZE_4MB)
{
*tab = sd_tab_format[MMC_4MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_4MB].nb_head *
sd_tab_format[MMC_4MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= MMC_SIZE_8MB)
{
*tab = sd_tab_format[MMC_8MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_8MB].nb_head *
sd_tab_format[MMC_8MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= MMC_SIZE_16MB)
{
*tab = sd_tab_format[MMC_16MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_16MB].nb_head *
sd_tab_format[MMC_16MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= MMC_SIZE_28MB)
{
*tab = sd_tab_format[MMC_28MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_28MB].nb_head *
sd_tab_format[MMC_28MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= MMC_SIZE_32MB)
{
*tab = sd_tab_format[MMC_32MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_32MB].nb_head *
sd_tab_format[MMC_32MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= MMC_SIZE_64MB)
{
*tab = sd_tab_format[MMC_64MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_64MB].nb_head *
sd_tab_format[MMC_64MB].nb_sector);
return tab;
}
if (Sd_disk_size() <= MMC_SIZE_128MB)
{
*tab = sd_tab_format[MMC_128MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_128MB].nb_head *
sd_tab_format[MMC_128MB].nb_sector);
return tab;
}
else
{
*tab = sd_tab_format[MMC_256MB];
/* calculation of cylinder number */
(*tab).nb_cylinder = (Sd_disk_size() + 1) /
(sd_tab_format[MMC_256MB].nb_head *
sd_tab_format[MMC_256MB].nb_sector);
return tab;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -