⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smedia.c

📁 * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS * OR IMPLIED WARRANTIES, INC
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2002, Jan Szymanski.
 * All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Jan Szymanski.
 * 4. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.  
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 */
/*********************************************************/
/*                                                       */
/*  Module              : SMedia.C                       */
/*  Language used       : C for MSP430                   */
/*  Microprocessor      : MSP430F149                     */
/*                                                       */
/*  Function            : Low level drivers for          */
/*                        SmartMedia                     */
/* ------------------------------------------------------*/
/*  Revision History :                                   */ 
/*  Author				Date				    Reason                 */
/*  Jan Szymanski 6 December 2001	Initial Design         */
/*                                                       */
/*********************************************************/

/**********************************************************/
/*  Definitions                                           */
/**********************************************************/
/* Hardware lines used to controls SmartMedia:  */
/* P40-P47 is data D0-D7 */
/* P10 SMCS out */
/* P11 SMRD out */
/* P12 SMWR out */
/* P13 CLE out */
/* P14 ALE out */
/* P15 R/B in */   

//was #define PASS	1
//was #define FAIL	2
//was #define ECCERROR	3

/* smartMedia commands */
#define SM_RESET_C	0xff
#define SM_READ1_C	0x00		/* pointer 0-255 */
#define SM_READ2_C	0x01		/* pointer 256-511 */
#define SM_READ3_C	0x50		/* pointer 512-527 */
#define SM_READ_ID_C	0x90
#define SM_PAGE_PROGRAM_1C	0x80
#define SM_PAGE_PROGRAM_2C 	0x10
#define SM_BLOCK_ERASE_1C		0x60
#define SM_BLOCK_ERASE_2C		0xd0
#define SM_READ_STATUS_C	0x70
/**********************************************************/
/*  Local Functions Prototypes                            */
/**********************************************************/
void reset(void);
unsigned char SM_status_check(void);
unsigned char SM_read(void);
void SM_write(unsigned char c);
/**********************************************************/
/*  Local Variables                                       */
/**********************************************************/
unsigned char	err_flag;


/**********************************************************/
/*  Hardware functions definitions                        */
/**********************************************************/
#define SM_chip_enable	P1OUT &= 0xfe;	/* P10 is CS active low */	
#define SM_chip_disable	P1OUT |= 0x01;	/* CS unactive high */
#define SM_add_latch_en P1OUT |= 0x10;	/* P14 is ALE active high */
#define SM_add_latch_dis P1OUT &= 0xef;	/* ALE low */
#define SM_cmd_latch_en P1OUT |= 0x08;	/* P13 is CLE active high */
#define SM_cmd_latch_dis P1OUT &= 0xf7;	/* CLE low */

/**********************************************************/
/* Local Functions Implementations                        */
/**********************************************************/
unsigned char SM_read(void)
{
unsigned char c;
P4DIR = 0x00; /* change dir to inputs */
P1OUT &= 0xfd;  /* P11 is RD active low */
//c = P4IN;
P1OUT |= 0x02;  /* RD is high back */
c = P4IN;
P4DIR = 0xff; /* dir back to outputs */
return c;
}
/*********************************************************/
void SM_write(unsigned char c)
{
P4OUT = c;
P4DIR = 0xff; /* make sure dir is outputs */
//P4OUT = c;
P1OUT &= 0xfb;  /* P12 is WR active low */
//P4OUT = c;
P1OUT |= 0x04;  /* WR goes high back */
}
/*********************************************************/
void	reset(void)               
{
unsigned int	i;
SM_cmd_latch_en 
SM_write(SM_RESET_C); /* write command 0xff to SmartMedia */
SM_cmd_latch_dis
for(i=0; i<10000; i++);/* Waiting for tRST */
}
/*********************************************************/
unsigned char SM_status_check(void)
{
unsigned int i;
unsigned char status;

for(i=0; i<20000; i++)
	{
	SM_cmd_latch_en
	SM_write(SM_READ_STATUS_C);	/* read status command 0x70 */
	SM_cmd_latch_dis
  status= SM_read();
	if (status == 0xc0) break;
	}
if (status == 0xc0)	return  PASS;
	else return	FAIL;
}
/**********************************************************/
/* Public Functions                                       */
/**********************************************************/
/***********************************************************
Function:       SM_reset
Purpose: 				to reset SmartMedia	
Author:  				Jan Szymanski
Original Date:  27-03-2001
Date last Mod:
Comments:											
***********************************************************/
void SM_reset(void)
{
SM_add_latch_dis
SM_cmd_latch_dis
SM_chip_enable
reset();
SM_chip_disable
}
/* End of SM_reset ****************************************/

/***********************************************************
Function:       SM_read_info
Purpose: 				to read ID and other info of SmartMedia	
Author:  				Jan Szymanski
Original Date:  27-03-2001
Date last Mod:
Comments:				update global variable SM_info[10]											
***********************************************************/
// LCR_EXT unsigned int SM_info[10];
/*	[0]: device_maker		0xec:samsung, 0x98:toshiba
		[1]: device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
		[2]: blocks_disk		value: blocks in a disk
		[3]: pages_block		value: pages in a block
		[4]: page_size			value: page_size in bytes
		[5]: data_size			value: data_size in bytes
		[6]: spare_size			value: spare_size in bytes
		[7]: sector count		value: sectors per block
		[8]: Max Address                value: ADD3 Value
		[9]: Max Address for 64MB,128MB value: ADD4 Value           
		[10]:Block Size in bytes */
void SM_read_info(void)
{
unsigned char c; 
char m_code,d_code;

SM_chip_enable

SM_cmd_latch_en
SM_write(SM_READ_ID_C);	// command 0x90 to read ID
SM_cmd_latch_dis

SM_add_latch_en
SM_write(0x00);					// address 0x00
SM_add_latch_dis

c=SM_read();
SM_info[0]=c;	// device maker
//hexout_uart0(c);
c=SM_read();
SM_info[1]=c;// device type
//hexout_uart0(c);

SM_chip_disable

/* based on device maker and type other info can be updated */
switch(SM_info[1])
	{
	case 0xE6://8M not used
		SM_info[2]=1024;     // blocks_disk		value: blocks in a disk
		SM_info[3]=16;      // pages_block		value: pages in a block
		SM_info[4]=528;      // page_size		value: page_size in bytes
		SM_info[5]=512;      // data_size		value: data_size in bytes
		SM_info[6]=16;        // spare_size		value: spare_size in bytes
		SM_info[7]=16;        // sector count	value: sectors per block
		SM_info[8]=0x3f;      // Max Address          value: ADD3 value
		SM_info[9]=0x00;     // Max Address          value: ADD4 value
		SM_info[10]=0x2000;		// block size bytes = 8kB
    SM_info[11]=8;  // 8 MB
	break;

	case 0x73://16M
		SM_info[2]=1024;     // blocks_disk		value: blocks in a disk
		SM_info[3]=32;      // pages_block		value: pages in a block
		SM_info[4]=528;      // page_size		value: page_size in bytes
		SM_info[5]=512;      // data_size		value: data_size in bytes
		SM_info[6]=16;        // spare_size		value: spare_size in bytes
		SM_info[7]=32;        // sector count	value: sectors per block
		SM_info[8]=0x7f;     // Max Address          value: ADD3 value
		SM_info[9]=0x00;     // Max Address          value: ADD4 value 
		SM_info[10]=0x4000;		// block size bytes = 16kB
    SM_info[11]=16;  // size in MB
  break;
	case 0x75://32M
		SM_info[2]=2048;     // blocks_disk		value: blocks in a disk
		SM_info[3]=32;      // pages_block		value: pages in a block
		SM_info[4]=528;      // page_size		value: page_size in bytes
		SM_info[5]=512;      // data_size		value: data_size in bytes
		SM_info[6]=16;        // spare_size		value: spare_size in bytes
		SM_info[7]=32;        // sector count	value: sectors per block
		SM_info[8]=0xff;     // Max Address          value: ADD3 value
		SM_info[9]=0x00;     // Max Address          value: ADD4 value 
		SM_info[10]=0x4000;		// block size bytes = 16kB
    SM_info[11]=32;  // size in MB
	break; 

	case 0x76://64M
		SM_info[2]=4096;     // blocks_disk		value: blocks in a disk
		SM_info[3]=32;      // pages_block		value: pages in a block
		SM_info[4]=528;      // page_size		value: page_size in bytes
		SM_info[5]=512;      // data_size		value: data_size in bytes
		SM_info[6]=16;        // spare_size		value: spare_size in bytes
		SM_info[7]=32;        // sector count	value: sectors per block
		SM_info[8]=0xff;     // Max Address          value: ADD3 value
		SM_info[9]=0x01;     // Max Address          value: ADD4 value 
		SM_info[10]=0x4000;		// block size bytes = 16kB
    SM_info[11]=64;  // size in MB
  break;

	default:
	/* unrecognised size or error */
  	SM_info[2]=0;     // blocks_disk		value: blocks in a disk
		SM_info[3]=0;      // pages_block		value: pages in a block
		SM_info[4]=0;      // page_size		value: page_size in bytes
		SM_info[5]=0;      // data_size		value: data_size in bytes
		SM_info[6]=0;        // spare_size		value: spare_size in bytes
		SM_info[7]=0;        // sector count	value: sectors per block
		SM_info[8]=0;     // Max Address          value: ADD3 value
		SM_info[9]=0;     // Max Address          value: ADD4 value 
		SM_info[10]=0;		// block size bytes = 16kB
    SM_info[11]=0;  // size in MB
	break;	
 }
}
/* End of SM_read_info ************************************/

/***********************************************************
Function:       SM_page_read
Purpose: 				to read one page of SmartMedia	
Author:  				Jan Szymanski
Original Date:  28-06-2001
Date last Mod:
Comments:															
***********************************************************/
unsigned char	SM_page_read(unsigned char *page_data,unsigned long page_num)
{
unsigned char c;
unsigned long i;
unsigned int j;
unsigned char temp,temp1,temp2;
unsigned char *ptr;

temp=(unsigned char)page_num;
temp1=(page_num>>8);
temp2=(page_num>>16);

SM_chip_enable
reset();
SM_cmd_latch_en
SM_write(SM_READ1_C);	/*Main area data read command(00h)*/
SM_cmd_latch_dis
SM_add_latch_en

SM_write(0x00);
SM_write(temp);
SM_write(temp1);
if(SM_info[11]>=64) SM_write(temp2);
SM_add_latch_dis
	
// waiting for R/B
for(i=0; i<10000; i++)
	{ 
	//if(P4.PORT.BIT.B5==0) putchar_PC('0');
	//	else putchar_PC('1');
	if((P1IN &0x20)==0x20) break; /* P15 is R/B input */
	}	// waiting for ready 
/* short delay here */
//for(i=0; i<100; i++){nop;}

/* this part for debugging only */		
//putstring_PC((unsigned char*)("\r\n"));	
//hexout_PC(temp1);
//hexout_PC(temp);
//putstring_PC((unsigned char*)("\r\n"));	
/* end of debugging part */		 

//	for(i=0;i<SM_info[4];i++)
ptr=page_data;
for (j=0;j<528;j++)
	{
	c = SM_read();
	//hexout_uart0(c);
	*ptr++ = c;    /*Page data Read*/
  //*(page_data+j)=c;
	}

SM_chip_disable
return PASS;
}


/* End of SM_page_read ************************************/

/***********************************************************
Function:       SM_page_write
Purpose: 				to write one page of SmartMedia	
Author:  				Jan Szymanski
Original Date:  28-06-2001
Date last Mod:
Comments:															
***********************************************************/

unsigned char SM_page_write(unsigned char *write_data,unsigned int page_num)
{
unsigned int i;
unsigned char temp,temp1;
temp = (unsigned char)(page_num);
temp1 = (page_num>>8);

SM_chip_enable
reset();
SM_cmd_latch_en
SM_write(SM_PAGE_PROGRAM_1C);	/*Sequential Data Input command (80h)*/
SM_cmd_latch_dis

SM_add_latch_en
SM_write(0x00);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -