📄 smc_drv.c
字号:
/*C**************************************************************************
* $RCSfile: smc_drv.c,v $
*----------------------------------------------------------------------------
* Copyright (c) 2002 Atmel.
*----------------------------------------------------------------------------
* RELEASE: $Name: DEMO_FAT_1_9_9 $
* REVISION: $Revision: 1.2 $
* FILE_CVSID: $Id: smc_drv.c,v 1.2 2002/08/28 09:45:08 njourdan Exp $
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the SMC driver routines
*
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "..\src\system\config.h" /* system configuration */
#include "..\..\lib_demob\board.h" /* board definition */
#include "smc_drv.h" /* smc driver definition */
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
/*_____ D E C L A R A T I O N ______________________________________________*/
extern code Byte smc_cis_table[]; /* CIS table */
extern pdata Byte gl_buffer[]; /* global buffer of 256 bytes */
extern xdata Byte volatile smc_data; /* Data */
extern xdata Byte smc_send_cmd; /* Command */
extern xdata Byte smc_send_add; /* Address */
extern data Uint32 gl_ptr_mem; /* memory data pointer */
extern data Uint32 address; /* general address variable */
extern xdata Byte smc_zone_max; /* max zone number (from 1 to 8) */
extern xdata Uint32 smc_disk_size; /* Size of the disk in sector - 1 */
extern xdata Byte smc_device_type; /* Give the size of device in Mbytes */
extern data Uint16 look_up_table_block; /* Look up table block address */
extern idata Uint16 redundant_logical_block_value; /* Logical block value in the redundant area */
extern data Uint32 current_physical_sector_addr; /* give the address of the current sector */
extern bdata bit block_used;
extern bdata bit smc_busy;
extern bdata bit smc_64; /* Indicate that the SMC capacity >= 64 Mbytes */
/* ECC variable */
extern xdata Byte ecc1, ecc2, ecc3;
extern xdata Byte ecc[6];
extern xdata Uint16 buf_free[24];
extern xdata Uint16 block_to_be_deleted;
extern xdata Uint16 block_min;
extern data Byte smc_zone;
extern xdata Uint16 lut_block[SMC_ZONE_MAX]; /* LUT address in block value */
extern xdata union16 buf[SMC_BUFFER_SIZE]; /* Buffer for write operation : contains physical block address */
extern xdata Byte smc_spare_block_number[SMC_ZONE_MAX]; /* Number of free spare block in each zone */
extern Byte gl_buf_free_idx ;
extern Byte gl_buf_idx;
extern xdata Byte lut_index[SMC_ZONE_MAX];
extern xdata Uint16 logical_block;
extern idata Byte gl_buf_idx_max;
extern xdata Uint16 spare_block;
/*F**************************************************************************
* NAME: smc_check_status
*----------------------------------------------------------------------------
* PARAMS:
*
* RETURN: SMARTMEDIA status
*
*----------------------------------------------------------------------------
* PURPOSE: Check the status of the device after a program or an erase
* operation
*
*****************************************************************************
* NOTE:
*
*
*****************************************************************************/
bit smc_check_status(void)
{
Smc_wait_busy();
/* Status Type Command */
Smc_send_command(SMC_READ_STATUS_CMD);
if ( (Smc_rd_byte() & 0x01) == 0x00)
{
return OK;
}
else
{
return KO;
}
}
/*F**************************************************************************
* NAME: smc_calc_logical_block
*----------------------------------------------------------------------------
* PARAMS:
*
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE: This function calculate the logical block value (used in spare
* data area)
*
*****************************************************************************
* NOTE:
*
*
*****************************************************************************/
void smc_calc_logical_block()
{
volatile Byte dummy;
bdata bit parity_bit;
redundant_logical_block_value = (logical_block << 1) + 0x1000;
/* Parity bit calculation */
dummy = redundant_logical_block_value;
parity_bit = P;
dummy = ((Byte*)&redundant_logical_block_value)[0];
if (P) parity_bit = ~parity_bit;
if (parity_bit)
redundant_logical_block_value++;
}
/*F**************************************************************************
* NAME: smc_update_spare_data
*----------------------------------------------------------------------------
* PARAMS:
*
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE: This function update the value of the logical block on the spare
* data area
*
*****************************************************************************
* NOTE:
*
*
*****************************************************************************/
void smc_update_spare_data(void)
{
#ifndef SMC_ECC
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(redundant_logical_block_value >> 8);
Smc_wr_byte(redundant_logical_block_value );
Smc_wr_byte(0xFF);
Smc_wr_byte(0xFF);
Smc_wr_byte(0xFF);
Smc_wr_byte(redundant_logical_block_value >> 8 );
Smc_wr_byte(redundant_logical_block_value );
Smc_wr_byte(0xFF);
Smc_wr_byte(0xFF);
Smc_wr_byte(0xFF);
#else
Smc_wr_byte(0xFF); /* User Data area */
Smc_wr_byte(0xFF); /* User Data area */
Smc_wr_byte(0xFF); /* User Data area */
Smc_wr_byte(0xFF); /* User Data area */
Smc_wr_byte(0xFF); /* User status area */
Smc_wr_byte(0xFF); /* Block status area */
Smc_wr_byte(redundant_logical_block_value >> 8); /* Block address area 1 */
Smc_wr_byte(redundant_logical_block_value ); /* Block address area 1 */
Smc_wr_byte(ecc[3]); /* ECC area 2 */
Smc_wr_byte(ecc[4]); /* ECC area 2 */
Smc_wr_byte(ecc[5]); /* ECC area 2 */
Smc_wr_byte(redundant_logical_block_value >> 8 ); /* Block address area 2 */
Smc_wr_byte(redundant_logical_block_value ); /* Block address area 2 */
Smc_wr_byte(ecc[0]); /* ECC area 1 */
Smc_wr_byte(ecc[1]); /* ECC area 1 */
Smc_wr_byte(ecc[2]); /* ECC area 1 */
#endif
}
/*F**************************************************************************
* NAME: smc_init_buffer
*----------------------------------------------------------------------------
* PARAMS:
*
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE: This function initialize the global buffer at 0xFF
*
*
*****************************************************************************
* NOTE:
*
*
*****************************************************************************/
void smc_init_buffer(void)
{
Byte i;
char pdata *ptr = gl_buffer;
for (i = 4; i != 0; i--)
{
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
*(ptr++) = 0xFF;
}
}
/*F**************************************************************************
* NAME: smc_init_spare
*----------------------------------------------------------------------------
* PARAMS:
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE: Init logical value in redundant data when a block is opened for
* the first time and the sector inside the block is not 0.
*
*****************************************************************************
* NOTE:
*
*
*****************************************************************************/
void smc_init_spare(void)
{
Byte j;
/* Calculate the current physical sector */
for (j = (gl_ptr_mem & 0x1F); j != 0 ; j--)
{
Smc_wait_busy();
/* Write open on spare data area */
Smc_send_command (SMC_READ_C_AREA_CMD);
Smc_send_command (SMC_SEQUENTIAL_DATA_INPUT_CMD);
Smc_send_address (0x00);
Smc_send_address ( ((Byte*)¤t_physical_sector_addr)[3] ); /* 2nd address cycle */
Smc_send_address ( ((Byte*)¤t_physical_sector_addr)[2] ); /* 3rd address cycle */
if (smc_64) /* Size of card >= 64Mbytes ?*/
Smc_send_address ( ((Byte*)¤t_physical_sector_addr)[1] );/* 4th address cycle */
/* Update spare data */
smc_update_spare_data();
/* Send program command */
Smc_send_command (SMC_PAGE_PROGRAM_CMD);
current_physical_sector_addr++;
}
Smc_wait_busy();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -