📄 smc.c
字号:
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
Usb_write_byte(Smc_rd_byte());
if (begin_ping_pong)
{
begin_ping_pong = FALSE;
}
else
{
while (!Usb_tx_complete()); /* wait end of transfer */
Usb_clear_TXCMPL(); /* ack transfer */
}
Usb_set_TXRDY(); /* start usb transfer */
}
gl_ptr_mem++; /* new page */
Smc_CS_OFF();
if ( !(gl_ptr_mem & 0x1F) ) /* New block ? */
{
smc_read_open(gl_ptr_mem);
}
else
{
smc_current_physical_sector_addr++;
}
nb_sector--;
}
while (nb_sector != 0);
while (!Usb_tx_complete()); /* wait end of last transfer */
Usb_clear_TXCMPL(); /* ack transfer */
return OK;
}
/*F**************************************************************************
* NAME: smc_write_open
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS:
* pos: address of the the next write data
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory write update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit smc_write_open (Uint32 pos)
{
Byte i;
Byte j;
bit change_zone = FALSE;
if ( Smc_card_detect() == KO )
{
gl_mem_failure = TRUE;
Smc_CS_OFF();
return KO;
}
gl_ptr_mem = pos;
gl_cpt_page = 0;
/* Determine the logical block value */
smc_logical_block = (gl_ptr_mem >> 5);
smc_zone = 0;
while (smc_logical_block > 999)
{
smc_logical_block -= 1000;
smc_zone++;
}
Smc_CS_ON();
if (smc_zone != smc_old_zone)
{
change_zone = TRUE;
if (smc_lut_modified)
{ /* save old lut */
i = smc_old_zone;
smc_old_zone = smc_zone;
smc_zone = i;
smc_reassign_block();
smc_lut_modified = FALSE;
smc_zone = smc_old_zone;
}
else
{ /* update old zone value */
smc_old_zone = smc_zone;
}
smc_block_min = 0xFFFF;
smc_gl_buf_free_idx = 0;
}
if ( (smc_logical_block >= smc_block_min) && (smc_logical_block < (smc_block_min + SMC_BUFFER_SIZE - 5)))
{ /* we don't have to update the buffer */
smc_calc_logical_block();
smc_gl_buf_idx = smc_logical_block - smc_block_min;
}
else
{ /* we have to update the buffer */
/* Store the look up table block address */
smc_look_up_table_block = smc_lut_block[smc_zone];
/* If LUT have been modified */
if (smc_lut_modified)
{
smc_reassign_block(); /* update the LUT */
}
/* Calculate the address for LUT access */
gl_address = ((Uint32)(smc_look_up_table_block)<<5) + ((Uint32)(smc_logical_block) >> 8);
gl_address += (Uint32)(smc_lut_index[smc_zone])<<2;
/* Calculate the redundant block address value */
smc_calc_logical_block();
i = smc_logical_block & 0xFF;
/* For the current zone, initialize the number of spare block */
smc_spare_block = smc_spare_block_number[smc_zone];
/* Calculate the low and the high block stored in the buffer */
smc_block_min = smc_logical_block;
smc_block_max = ( (smc_logical_block + SMC_BUFFER_SIZE) > 999) ? (1000 - smc_logical_block) : SMC_BUFFER_SIZE;
/* Open the look-up table */
Smc_wait_busy();
if (smc_logical_block & 0x80)
{
Smc_send_command(SMC_READ_B_AREA_CMD); /* 2nd half page */
Smc_send_address( (smc_logical_block << 1) - 256);
}
else
{
Smc_send_command(SMC_READ_A_AREA_CMD); /* first half page */
Smc_send_address(smc_logical_block << 1);
}
Smc_send_address ( ((Byte*)&gl_address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&gl_address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ?*/
Smc_send_address ( ((Byte*)&gl_address)[1] ); /* 4th address cycle */
Smc_wait_busy();
for (j = 0; j < smc_block_max ; j++)
{
/* Read conversion table : 64 datas */
smc_buf[j].b[0] = Smc_rd_byte();
smc_buf[j].b[1] = Smc_rd_byte();
i++;
if (i == 0)
{
gl_address++;
Smc_read_open_A_area(gl_address, 0x00);
}
}
if (change_zone) /* If it is a new zone, then load the spare buffer */
{
/* Check for used block : first, read in the LUT the free physical block */
gl_address = ((Uint32)(smc_look_up_table_block)<<5) + 3;
gl_address += (Uint32)(smc_lut_index[smc_zone])<<2;
Smc_read_open_B_area(gl_address, 208);
for (i = 0; i <= (smc_spare_block); i++)
{
smc_buf_free[i] = (Uint16)(Smc_rd_byte()<<8);
smc_buf_free[i] += Smc_rd_byte();
}
}
smc_gl_buf_idx = 0; /* initialize index for main buffer */
smc_gl_buf_idx_max = 0; /* initialize the max index for the buffer */
}
/* if block is already assigned, then invert with a spare block */
smc_block_used = ( !(smc_buf[smc_gl_buf_idx].w & 0x8000) ) ? TRUE : FALSE;
if (smc_block_used)
{
if ((smc_buf_free[smc_gl_buf_free_idx] & 0x7FFF) == smc_look_up_table_block)
{
smc_gl_buf_free_idx++;
if (smc_gl_buf_free_idx >= smc_spare_block )
{
smc_gl_buf_free_idx = 0;
}
}
/* assign block to be deleted */
smc_block_to_be_deleted = smc_buf[smc_gl_buf_idx].w;
smc_buf[smc_gl_buf_idx].w = smc_buf_free[smc_gl_buf_free_idx];
smc_buf_free[smc_gl_buf_free_idx++] = smc_block_to_be_deleted | 0x8000;
/* increase the index for spare block buffer */
if (smc_gl_buf_free_idx >= smc_spare_block )
{
smc_gl_buf_free_idx = 0;
}
}
/* Mark block as assigned */
smc_buf[smc_gl_buf_idx].w &= 0x7FFF;
if (smc_gl_buf_idx > smc_gl_buf_idx_max)
smc_gl_buf_idx_max = smc_gl_buf_idx;
/* Update the current physical sector address */
smc_current_physical_sector_addr = ((Uint32)(smc_buf[smc_gl_buf_idx].w) << 5);
if ( smc_block_used ) /* Fisrt block already used block ? */
{
smc_copy_block_head(); /* Copy the first part of the block */
}
else
{
smc_init_spare(); /* else init spare data for new logical block */
}
smc_busy = FALSE; /* Clear flag busy */
smc_lut_modified = TRUE;
/* Set LUT as modified */
gl_address = ((Uint32)(smc_look_up_table_block)<<5);
gl_address += (Uint32)(smc_lut_index[smc_zone])<<2;
Smc_write_open_C_area(gl_address, 0x00);
Smc_wr_byte(0x00); /* Reset first byte */
Smc_send_command(SMC_PAGE_PROGRAM_CMD); /* Send program command to the device */
Smc_wait_busy();
return OK;
}
/*F**************************************************************************
* NAME: smc_write_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory write close: release SMC
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit smc_write_close (void)
{
Smc_CS_ON();
if (gl_cpt_page != 0) /* uncomplete write */
{
if (gl_cpt_page < 256) /* First half page */
{
for ( ; gl_cpt_page < 256; gl_cpt_page++)
{
gl_buffer[gl_cpt_page] = 0x00;
}
Smc_wait_busy();
Smc_write_open_A_area(smc_current_physical_sector_addr, 0x00);
smc_download_buffer(); /* Copy the buffer */
Smc_send_command(SMC_PAGE_PROGRAM_CMD); /* Program the device */
#ifdef SMC_ECC
smc_calculate_ecc();
ecc[0] = ecc1;
ecc[1] = ecc2;
ecc[2] = ecc3;
#endif
}
/* 2nd half page */
for ( ; gl_cpt_page < 512; gl_cpt_page++)
{
gl_buffer[gl_cpt_page] = 0x00;
}
Smc_wait_busy();
Smc_write_open_B_area(smc_current_physical_sector_addr, 0x00);
smc_download_buffer(); /* Copy the buffer */
#ifdef SMC_ECC
smc_calculate_ecc();
ecc[3] = ecc1;
ecc[4] = ecc2;
ecc[5] = ecc3;
#endif
smc_update_spare_data();
Smc_send_command(SMC_PAGE_PROGRAM_CMD); /* Send program command to the device */
gl_cpt_page = 0;
gl_ptr_mem++;
}
/* Copy last part of a block if the block was assigned */
if (smc_block_used)
{
if ( gl_ptr_mem & 0x1F )
smc_copy_block_tail();
smc_block_erase((Uint32)(smc_block_to_be_deleted) << 5); /* Erase old block */
}
Smc_wait_busy();
Smc_CS_OFF();
return OK;
}
/*F**************************************************************************
* NAME: smc_write_byte
*----------------------------------------------------------------------------
* PARAMS:
* b: data to write
*
* RETURN:
* write status: OK: write done
* KO: write not done
*
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory write function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -