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

📄 flash.c

📁 at45081 bsp 源码
💻 C
📖 第 1 页 / 共 4 页
字号:
 	BYTE xdata ucCurrentState;
	ulong xdata p;
	BOOL bFirstNormal = FALSE;

	// init the read and write poiter
	m_pReadPtr = 0;
	m_pWritePtr = 0;
	m_pFirstReadPtr = 0;
	
	p = 0;
	while(1)
	{
     	// Get Flag station
		FLASH_ReadData( p, &ucCurrentState, 1);

        switch ( ucCurrentState )
		{
        	case (FLASH_WRITING):            
            {
				// write is in progress when power loss, set to Invalid
				ucCurrentState = FLASH_INVALID;
				FLASH_WriteData( p, &ucCurrentState, 1);
			}
			break;

            case (FLASH_NORMAL):
			{
				if( bFirstNormal == FALSE )
			   	{
					// Set the flag
					bFirstNormal = TRUE;

					// set the read pointer
					m_pReadPtr = p;

					// set the first read pointer
					m_pFirstReadPtr = p;
				}
			}

            case (FLASH_TRANSMITED):
            case (FLASH_INVALID):
			break;

            case ( FLASH_EMPTY ):
			{ 
				// set the write poiter
				m_pWritePtr = p;
			}
			break;

			default:
			break;
		} //switch ( tempFlag[0] )
		
		// Reach the end of the table
		if( ucCurrentState == FLASH_EMPTY )
		{
			break;
		}
		// notice for page alignment to accelerate read/write
		//
		//
		//
		//
		p += FLASH_RECORD_LEN;
	} // end of while

	// flash is almost out of space
	if( p > ((long)FLASH_PAGE_SIZE*FLASH_PAGE_DATA_NUM - (FLASH_RECORD_LEN * 4096L)  ) )
	{
		// no normal record
		if( m_pWritePtr == m_pReadPtr );
		{
			FLASH_EraseDataFlash();
			m_pWritePtr = 0;
			m_pReadPtr = 0;
			m_pFirstReadPtr = 0;
		}
	}

	return;
}

/****************************************************************************/
/* 函 数 名:  FLASH_InitConfig												*/
/* 功能描述:  init the flash status pointers or finish writing 				*/
/* 函数说明: 																*/
/* 调用函数: 																*/
/* 全局变量: 																*/
/* 输    入: 																*/
/* 返    回: TRUE -- Everything is cool... other -- something wrong			*/
/****************************************************************************/
BOOL FLASH_InitConfig( void )
{
	BOOL bSuccess;

	// 常用的设置信息 CONFIG_TYPE_COMMON
	bSuccess = ReadCommonConfig();

	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		// dial in restart
		g_pCommonConfig->DialInRestart.Mode = DIALIN_RESTART_ENABLE;
		strcpy(g_pCommonConfig->DialInRestart.PhoneNo, "13601719610" );

		// position report mode
		g_pCommonConfig->PositionMode.Mode = REPORT_MODE_DISTANCEANDTIME;
		g_pCommonConfig->PositionMode.Distance = 200;
		g_pCommonConfig->PositionMode.Time = 15;
		g_pCommonConfig->PositionMode.SleepTime = 1200;
//		g_pCommonConfig->PositionMode.PowerOffTime = 3600;

		// status report mode
		g_pCommonConfig->StatusMode.Mode = REPORT_MODE_DISTANCEANDTIME;
		g_pCommonConfig->StatusMode.Distance = 1000;
		g_pCommonConfig->StatusMode.Time = 600;
		g_pCommonConfig->StatusMode.SleepTime = 2400;
//		g_pCommonConfig->StatusMode.PowerOffTime = 7200;

		// ip address
		strcpy( g_pCommonConfig->ServerIPAddr.IP, "202.101.42.82");
		strcpy( g_pCommonConfig->ServerIPAddr.Port, "2020");

		// backup ip address
		strcpy( g_pCommonConfig->BackupServerIPAddr.IP, "202.101.42.82");
		strcpy( g_pCommonConfig->BackupServerIPAddr.Port, "2020");
		
		// other config
		strcpy( g_pCommonConfig->SMSCentre, "13801786447");
		strcpy( g_pCommonConfig->GMSCNo, "+8613800210500" );
		g_pCommonConfig->PointLeave		= 25;
		g_pCommonConfig->PointReach		= 50;
		g_pCommonConfig->StationRange	= 40;
		g_pCommonConfig->TerminalNo		= 0L;
		g_pCommonConfig->SleepTime		= 10;
		g_pCommonConfig->HibernateTime	= 1200;
		g_pCommonConfig->PowerOffTime	= 3600;

		// Write back to the flash
		if( WriteCommonConfig() == FALSE )
		{
			// Fail, write it again
			WriteCommonConfig();
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault common config produced!");
#endif
	}
	// check the common config
	else
	{
		// config is illeagle
		if( g_pCommonConfig->ServerIPAddr.IP[0] > '9' || 
			g_pCommonConfig->ServerIPAddr.IP[0] < '0')
		{
			// backup ip address
			strcpy( g_pCommonConfig->ServerIPAddr.IP, "202.101.42.82");
			strcpy( g_pCommonConfig->ServerIPAddr.Port, "2020");			
		}

		// config is illeagle
		if( g_pCommonConfig->BackupServerIPAddr.IP[0] > '9' || 
			g_pCommonConfig->BackupServerIPAddr.IP[0] < '0')
		{
			// backup ip address
			strcpy( g_pCommonConfig->BackupServerIPAddr.IP, "202.101.42.82");
			strcpy( g_pCommonConfig->BackupServerIPAddr.Port, "2020");			
		}
	}
	
	// 油量纪录 CONFIG_TYPE_GASVALUE
	bSuccess = FLASH_ReadConfig( CONFIG_TYPE_GASVALUE );
	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		g_uiGasValue = 0xFFFF;
		
		// Write it back to the flash, if fail,write again
		if( FLASH_WriteConfig( CONFIG_TYPE_GASVALUE) == FALSE )
		{
			FLASH_WriteConfig( CONFIG_TYPE_GASVALUE);
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault gas value produced!");
#endif
	}
	
	// 速度学习纪录 CONFIG_TYPE_RATESTUDY
	bSuccess = FLASH_ReadConfig( CONFIG_TYPE_RATESTUDY);
	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		g_uiRateStudy = 0xFFFF;

		// Write it back to the flash, if fail,write again
		if( FLASH_WriteConfig( CONFIG_TYPE_RATESTUDY) == FALSE )
		{
			FLASH_WriteConfig( CONFIG_TYPE_RATESTUDY);
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault rate study record produced!");
#endif
	}		
	
	// 里程记录表 CONFIG_TYPE_MILEMETER
	bSuccess = FLASH_ReadConfig( CONFIG_TYPE_MILEMETER);
	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		g_ulMileMeter = 0;
		
		// Write it back to the flash, if fail,write again
		if( FLASH_WriteConfig( CONFIG_TYPE_MILEMETER) == FALSE )
		{
			FLASH_WriteConfig( CONFIG_TYPE_MILEMETER);
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault milemeter produced!");
#endif
	}
	
	// 拨出记录表 CONFIG_TYPE_DIALOUT
	bSuccess = ReadDialOutConfig();
	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		uchar k;

		g_pDialOutSetting->Mode = DIAL_INOUT_LIMITTED;
		strcpy(g_pDialOutSetting->PhoneNo[0], "110");
		strcpy(g_pDialOutSetting->PhoneNo[1], "120");
		
		for( k = 2; k < MAX_DIALOUT_NUM; k++ )
		{
			strcpy( g_pDialOutSetting->PhoneNo[k], "");
		}

		// Write it back to flash, if fail, write it again
		if( WriteDialOutConfig() == FALSE )
		{
			WriteDialOutConfig();
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault dialout table produced!");
#endif
	}		
	// 拨入记录表 CONFIG_TYPE_DIALIN
	bSuccess = ReadDialInConfig();
	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		uchar k;
		
		g_pDialInSetting->Mode = DIAL_INOUT_LIMITTED;
		strcpy(g_pDialInSetting->PhoneNo[0], "13801786447");
		strcpy(g_pDialInSetting->PhoneNo[1], "13601719610");
		
		for( k = 2; k < MAX_DIALIN_NUM; k++ )
		{
			strcpy( g_pDialInSetting->PhoneNo[k], "");
		}
		
		// Write it back to flash, if fail, write it again
		if( WriteDialInConfig() == FALSE )
		{
			WriteDialInConfig();
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault dial in table produced!");
#endif
	}

	// 回复消息设置 CONFIG_TYPE_REPLYMSG
	g_ucReplyMessageNum = 0;
	for( g_ucUpdateReplyMessageIndex = 0; 
		g_ucUpdateReplyMessageIndex < MAX_REPLY_MESSAGE_NUM; 
		g_ucUpdateReplyMessageIndex++ )
	{
		bSuccess = ReadReplyMessage();
		if( bSuccess == TRUE )
		{
			g_ucReplyMessageNum++;
		}
	}

	// Get the default configuration if no message in the flash
	if( g_ucReplyMessageNum == 0 )
	{
		uchar k;
		
		for( k = 0; k < MAX_REPLY_MESSAGE_NUM; k++ )
		{
			g_pReplyMessage[k].Len = strlen(m_strReplyMessage[k]);
			if( g_pReplyMessage[k].Len == 0 )
			{
				g_pReplyMessage[k].Flag = REPLY_MESSAGE_EMPTY;
			}
			else
			{
				g_pReplyMessage[k].Flag = REPLY_MESSAGE_VALID;
				strcpy( g_pReplyMessage[k].MessageInfo.MessageText, 
					m_strReplyMessage[k]);

				// write it back to the flash
				g_ucUpdateReplyMessageIndex = k;
				WriteReplyMessage();
			}
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault reply messages produced!");
#endif
	}

	// 线路信息 CONFIG_TYPE_LINEINFO
	bSuccess = ReadLineInfo();
	// Get the default configuration if fail
	if( bSuccess != TRUE )
	{
		strcpy(g_pLineInfo->Name, "49路");

		g_pLineInfo->FirstHour = 5;
		g_pLineInfo->FirstMinute = 0;
		g_pLineInfo->LastHour = 23;
		g_pLineInfo->LastMinute = 30;

		strcpy( g_pLineInfo->Start, "万体馆");
		strcpy( g_pLineInfo->End, "汉口路");

		// write it back to the flash, if fail, try again
		if( WriteLineInfo() == FALSE )
		{
			WriteLineInfo();
		}
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nDefault Line info produced!");
#endif
	}
	
	// 站点信息 CONFIG_TYPE_STATIONINFO
	g_ucStationNum = 0;
	g_ucForwardStationNum = 0;
	for( g_ucUpdateStationIndex = 0; 
		g_ucUpdateStationIndex < MAX_STATION_NUM; 
		g_ucUpdateStationIndex++ )
	{
		bSuccess = ReadStationInfo();
		if( bSuccess == TRUE )
		{
			g_ucStationNum++;
			if( g_pStationInfo[g_ucUpdateStationIndex].Direction == STATION_DIRECTION_UP)
			{
				g_ucForwardStationNum++;
			}
		}
	}
	
	// Get the default configuration if no message in the flash
	if( g_ucStationNum < 2 )
	{
		g_bRequestAutoConfig = TRUE;
#if ( _PRINT_TO_PC_ == TRUE )
		print( "\nStation info empty, Need station info!");
#endif
	}
	else
	{
#if ( _PRINT_TO_PC_ == TRUE )
		sprintf( g_strDisplay, "\nGot %bu stations, %bu forward!",
			g_ucStationNum, g_ucForwardStationNum );
		print( g_strDisplay);
#endif		
	}

//////////////////////////////////////////////////////////////////////////
	// Test the record append , read, and delete
#if(0)
	if(1)
	{
		uchar k;

		for(k = 0; k < 100; k++ )
		{
			g_ulMileMeter = (ulong)k*10;
			FLASH_WriteConfig( CONFIG_TYPE_MILEMETER);
			FLASH_ReadConfig( CONFIG_TYPE_MILEMETER);
#if (_PRINT_TO_PC_ == TRUE)
		sprintf(g_strDisplay, "\n%bu: write:%lu, read=%lu", 
			k, (ulong)k*10, g_ulMileMeter);
		print( g_strDisplay );
#endif
		}
	}
#endif
//////////////////////////////////////////////////////////////////////////

	return TRUE;
}


/****************************************************************************/
/* 函 数 名:  SearchNextRecord												*/
/* 功能描述:  search the latest record with the specific state				*/
/* 函数说明: 																*/
/* 调用函数: 																*/
/* 全局变量: 																*/
/* 输    入: 																*/
/* 返    回: the address of the record found								*/
/****************************************************************************/
static ulong SearchNextRecord( ulong p, BYTE state )
{
	BYTE xdata ucCurrentState;

    while(1)
	{
		// point to the next record
		p += FLASH_RECORD_LEN;

     	// Get Flag station
		FLASH_ReadData( p, &ucCurrentState, 1);

		// Find the specific state record or meet an empty one
		if( ucCurrentState == state || ucCurrentState == FLASH_EMPTY )
	   	{
			return p;
		}

		// notice for page alignment to accelerate read/write
		//
		//
		//
		//
	};
}


/****************************************************************************/
/* 函 数 名:  FLASH_ReadFirstRecord											*/
/* 功能描述:  Read the first record that didn't transmited					*/
/* 函数说明: 																*/
/* 调用函数: 																*/
/* 全局变量: 																*/
/* 输    入: 																*/
/* 返    回: TRUE -- Everything is cool... FALSE -- something wrong			*/
/****************************************************************************/
BOOL FLASH_ReadFirstRecord( void )
{
	// Reset the read pointer
	m_pReadPtr = m_pFirstReadPtr;

	return FLASH_ReadRecord();
}


/****************************************************************************/
/* 函 数 名:  FLASH_ReadRecord										*/
/* 功能描述:  Read the first record that didn't transmited					*/
/* 函数说明: 																*/
/* 调用函数: 																*/
/* 全局变量: 																*/
/* 输    入: 																*/
/* 返    回: TRUE -- Everything is cool... FALSE -- something wrong			*/
/****************************************************************************/
BOOL FLASH_ReadRecord( void )
{
	FLASH_ReadData(m_pReadPtr,
		(BYTE *)&g_FlashReadRecord, FLASH_RECORD_LEN );
	
	if( g_FlashReadRecord.State == FLASH_NORMAL )
    {
		m_pReadPtr = SearchNextRecord( m_pReadPtr, FLASH_NORMAL );
		return TRUE;
	}
	else
   	{
		// Pointer to a abnormal state, search next one
		if( g_FlashReadRecord.State != FLASH_EMPTY )
		{
			m_pReadPtr = SearchNextRecord( m_pReadPtr, FLASH_NORMAL );
		}
		
		return FALSE;
	}
}


/****************************************************************************/
/* 函 数 名:  FLASH_DeleteFirstRecord										*/
/* 功能描述:  Update the record state from curent to the transmitted		*/
/* 函数说明: 																*/
/* 调用函数: 																*/
/* 全局变量: 																*/
/* 输    入: 																*/
/* 返    回: TRUE -- Everything is cool... FALSE -- something wrong			*/
/****************************************************************************/
BOOL FLASH_DeleteFirstRecord( void )
{
	BYTE ucCurrentState = FLASH_TRANSMITED;
	BYTE ucUpdatedState = 0xFF;

	FLASH_WriteData( m_pFirstReadPtr, &ucCurrentState, 1 );
	FLASH_ReadData( m_pFirstReadPtr, &ucUpdatedState, 1);
	if( ucUpdatedState != ucCurrentState )
	{
		FLASH_WriteData( m_pFirstReadPtr, &ucCurrentState, 1 );		

⌨️ 快捷键说明

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