📄 smc.c
字号:
{
start = 0x00;
end = 0x80; /* 256 bytes for gl_buffer <-> 128 blocks */
free_bloc_pos = (Uint16)(last_physical_used_block[smc_zone]);
address = (Uint32)(lut_block[smc_zone]) << 5;
do
{
smc_init_buffer(); /* Reinitialize the buffer */
temp_address = (Uint32)(smc_zone) << 15; /* We start at the beginning */
block = (Uint16)(smc_zone) << 10;
Smc_wait_busy();
for (j = SMC_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block */
{
Smc_send_command (SMC_READ_C_AREA_CMD); /* Read Spare data value */
Smc_send_address ( 0x05 );
Smc_send_address ( ((Byte*)&temp_address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&temp_address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ? */
Smc_send_address ( ((Byte*)&temp_address)[1] );/* 4th address cycle */
Smc_wait_busy();
byte_5 = Smc_rd_byte(); /* Block status byte */
if (byte_5 == 0xFF) /* If not a bad block */
{
block2.b[0] = Smc_rd_byte(); /* Read logical block address */
block2.b[1] = Smc_rd_byte();
if ( (block2.b[0] & 0xF8) == 0x10)
{
block2.w = (block2.w & 0x0FFF) >> 1;
if ( (block2.w < end) && (block2.w >= start))
{ /* Save logical block value in the buffer */
gl_buffer[2 * block2.b[1]] = block >> 8;
gl_buffer[2 * block2.b[1] + 1] = block;
}
}
}
temp_address += 32;
block++;
}
/* affect to the free physical block a fictive logical block */
/* free physical block => gl_buffer[x] = 0xFF */
temp_address = (Uint32)(free_bloc_pos) << 5;
for (i = 0; i <= 0xFE; i+=2)
{
if (gl_buffer[i] == 0xFF)
{
do /* Search free physical block */
{
temp_address += 32;
if (temp_address >= ((Uint32)(smc_zone + 1) << 15) )
temp_address = (Uint32)(smc_zone) << 15;
Smc_send_command(SMC_READ_C_AREA_CMD);
Smc_send_address(0x05);
Smc_send_address ( ((Byte*)&temp_address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&temp_address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ?*/
Smc_send_address ( ((Byte*)&temp_address)[1] );/* 4th address cycle */
Smc_wait_busy();
byte_5 = Smc_rd_byte(); /* Invalid/Valid block */
byte_6 = Smc_rd_byte(); /* Used/Unused block */
}
while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
free_bloc_pos = temp_address >> 5;
gl_buffer[i] = (free_bloc_pos >> 8) + 0x80;
gl_buffer[i + 1] = free_bloc_pos;
}
}
/* Write first part of LUT */
Smc_send_command(SMC_READ_A_AREA_CMD); /* First half array */
Smc_send_command(SMC_SEQUENTIAL_DATA_INPUT_CMD);
Smc_send_address (0x00);
Smc_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ? */
Smc_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
/* Write 256 bytes from the buffer */
smc_download_buffer();
/* Valid the page programmation */
Smc_send_command(SMC_PAGE_PROGRAM_CMD);
smc_init_buffer(); /* reinitialize the global buffer */
start += 0x80; /* process next 128 logical block */
end += 0x80;
block = (Uint16)(smc_zone) << 10;
temp_address = (Uint32)(smc_zone) << 15; /* restart from the begin of zone */
Smc_wait_busy();
for (j = SMC_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block */
{
Smc_send_command (SMC_READ_C_AREA_CMD); /* Read Spare data value */
Smc_send_address ( 0x05 );
Smc_send_address ( ((Byte*)&temp_address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&temp_address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ? */
Smc_send_address ( ((Byte*)&temp_address)[1] );/* 4th address cycle */
Smc_wait_busy();
byte_5 = Smc_rd_byte(); /* Block status byte */
if (byte_5 == 0xFF) /* If not a bad block */
{
block2.b[0] = Smc_rd_byte(); /* Read logical block address */
block2.b[1] = Smc_rd_byte();
if ( (block2.b[0] & 0xF8) == 0x10)
{
block2.w = (block2.w & 0x0FFF) >> 1;
if ( (block2.w < end) && (block2.w >= start))
{ /* Save logical block value in the buffer */
gl_buffer[2 * block2.b[1]] = block >> 8;
gl_buffer[2 * block2.b[1] + 1] = block;
}
}
}
temp_address += 32;
block++;
}
temp_address = (Uint32)(free_bloc_pos) << 5;
for (i = 0; i <= 0xFE; i += 2)
{
if (gl_buffer[i] == 0xFF)
{
do
{
temp_address += 32;
if (temp_address >= ((Uint32)(smc_zone + 1) << 15) )
temp_address = (Uint32)(smc_zone) << 15;
Smc_send_command(SMC_READ_C_AREA_CMD);
Smc_send_address(0x05);
Smc_send_address ( ((Byte*)&temp_address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&temp_address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ? */
Smc_send_address ( ((Byte*)&temp_address)[1] );/* 4th address cycle */
Smc_wait_busy();
byte_5 = Smc_rd_byte(); /* Invalid/Valid block */
byte_6 = Smc_rd_byte(); /* Used/Unused block */
}
while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
free_bloc_pos = temp_address >> 5;
gl_buffer[i] = (free_bloc_pos >> 8) + 0x80;
gl_buffer[i + 1] = free_bloc_pos;
}
}
/* Write second part of LUT */
Smc_send_command(SMC_READ_B_AREA_CMD); /* Second half array */
Smc_send_command(SMC_SEQUENTIAL_DATA_INPUT_CMD);
Smc_send_address(0x00);
Smc_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ?*/
Smc_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
smc_download_buffer();
/* Write redundant data */
Smc_wr_byte(0xFF); Smc_wr_byte(0xFF); Smc_wr_byte(0xFF);
Smc_wr_byte(0xFF); Smc_wr_byte(0xFF);
Smc_wr_byte(0xFF);
Smc_wr_byte(0xE8); Smc_wr_byte(0xFF); /* Logical block value */
Smc_wr_byte(0xFF); Smc_wr_byte(0xFF); Smc_wr_byte(0xFF); /* ECC area 2 */
Smc_wr_byte(0xE8); Smc_wr_byte(0xFF); /* Logical block value */
Smc_wr_byte(0xFF); Smc_wr_byte(0xFF); Smc_wr_byte(0xFF); /* ECC area 1 */
Smc_send_command(SMC_PAGE_PROGRAM_CMD);
start += 0x80;
end += 0x80;
/* Increase the address of the conversion table */
address++;
}
while (start < SMC_BLOCK_PER_ZONE);
}
}
Smc_wait_busy();
}
/*F**************************************************************************
* NAME: smc_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* SMC initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit smc_init (void)
{
P2 = SMC_IDLE_STATE_ADD;
if ( Smc_card_detect() == KO )
{
gl_mem_failure = TRUE;
Smc_CS_OFF();
return KO;
}
Smc_CS_ON();
Smc_send_command(SMC_RESET_CMD);
/* Read ID code */
Smc_wait_busy();
do
{
Smc_send_command(SMC_READ_ID_CMD);
Smc_send_address(0x00);
Smc_wait_busy();
} while (Smc_rd_byte() == 0x00);
switch (Smc_rd_byte()) /* Device code */
{
case 0x73 : /* 16 Mbyte */
smc_device_type = SMC_SIZE_16MB;
smc_zone_max = 1;
smc_disk_size = SMC_SECTOR_SIZE_16MB;
smc_64 = 0;
break;
case 0x75 : /* 32 Mbyte */
smc_device_type = SMC_SIZE_32MB;
smc_zone_max = 2;
smc_disk_size = SMC_SECTOR_SIZE_32MB;
smc_64 = 0;
break;
case 0x76 : /* 64 Mbyte */
smc_device_type = SMC_SIZE_64MB;
smc_zone_max = 4;
smc_disk_size = SMC_SECTOR_SIZE_64MB;
smc_64 = 1; /* 4 address cycles */
break;
case 0x79 : /* 128 Mbyte */
smc_device_type = SMC_SIZE_128MB;
smc_zone_max = 8;
smc_disk_size = SMC_SECTOR_SIZE_128MB;
smc_64 = 1; /* 4 address cycles */
break;
}
// fat_format(0);
read_spare_byte();
return OK;
}
/*F**************************************************************************
* NAME: smc_read_open
*----------------------------------------------------------------------------
* PARAMS:
* pos: address of the logic sector to read (size 512 bytes)
*
* return:
* Update memory for reading
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory read update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit smc_read_open (Uint32 pos)
{
Uint16 physical_block;
Smc_CS_ON();
if ( Smc_card_detect() == KO )
{
gl_mem_failure = TRUE;
return 0xFF;
}
if (lut_modified)
{
smc_reassign_block();
lut_modified = FALSE;
}
gl_ptr_mem = pos;
gl_cpt_page = 0;
/* Determine the logical block value */
logical_block = (gl_ptr_mem >> 5);
/* Determinate zone */
smc_zone = logical_block / 1000;
/* Each zone have 1000 data blocks */
logical_block = logical_block - (1000 * (Uint16)(smc_zone));
/* Calculate the address where are the physical block value */
address = ((Uint32)(lut_block[smc_zone])<<5) + ((Uint32)(logical_block) >> 8);
address += ((Uint32)(lut_index[smc_zone])<<2);
/* Open the look-up table */
Smc_wait_busy();
if (logical_block & 0x80)
{
Smc_send_command(SMC_READ_B_AREA_CMD); /* 2nd half page */
Smc_send_address( (logical_block << 1) - 256);
}
else
{
Smc_send_command(SMC_READ_A_AREA_CMD); /* first half page */
Smc_send_address(logical_block << 1);
}
Smc_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ?*/
Smc_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
Smc_wait_busy();
/* Read the physical block number */
((Byte*)&physical_block)[0] = Smc_rd_byte();
((Byte*)&physical_block)[1] = Smc_rd_byte();
/* Calculate the physical sector address */
current_physical_sector_addr = ((Uint32)(physical_block) << 5) + (gl_ptr_mem & 0x1F);
Smc_CS_OFF();
return OK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -