CH552/projects/examples/DataFlash/DataFlash.C

65 lines
2.8 KiB
C++
Raw Normal View History

2018-10-17 10:44:45 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name : DataFlash.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554 DataFlash<EFBFBD>ֽڶ<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
#include "..\Public\CH554.H"
#include "..\Public\Debug.H"
#include "DataFlash.H"
/*******************************************************************************
* Function Name : WriteDataFlash(UINT8 Addr,PUINT8 buf,UINT8 len)
* Description : DataFlashд
* Input : UINT8 Addr<EFBFBD><EFBFBD>PUINT16 buf,UINT8 len
* Output : None
* Return : UINT8 i <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
UINT8 WriteDataFlash(UINT8 Addr,PUINT8 buf,UINT8 len)
{
UINT8 i;
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA; //<2F><><EFBFBD>밲ȫģʽ
GLOBAL_CFG |= bDATA_WE; //ʹ<><CAB9>DataFlashд
SAFE_MOD = 0; //<2F>˳<EFBFBD><CBB3><EFBFBD>ȫģʽ
ROM_ADDR_H = DATA_FLASH_ADDR >> 8;
Addr <<= 1;
for(i=0;i<len;i++)
{
ROM_ADDR_L = Addr + i*2;
ROM_DATA_L = *(buf+i);
if ( ROM_STATUS & bROM_ADDR_OK ) { // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Ч
ROM_CTRL = ROM_CMD_WRITE; // д<><D0B4>
}
if((ROM_STATUS ^ bROM_ADDR_OK) > 0) return i; // <20><><EFBFBD><EFBFBD>״̬,0x00=success, 0x02=unknown command(bROM_CMD_ERR)
}
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA; //<2F><><EFBFBD>밲ȫģʽ
GLOBAL_CFG &= ~bDATA_WE; //<2F><><EFBFBD><EFBFBD>DataFlashд<68><D0B4><EFBFBD><EFBFBD>
SAFE_MOD = 0; //<2F>˳<EFBFBD><CBB3><EFBFBD>ȫģʽ
return i;
}
/*******************************************************************************
* Function Name : ReadDataFlash(UINT8 Addr,UINT8 len,PUINT8 buf)
* Description : <EFBFBD><EFBFBD>DataFlash
* Input : UINT8 Addr UINT8 len PUINT8 buf
* Output : None
* Return : UINT8 i <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
UINT8 ReadDataFlash(UINT8 Addr,UINT8 len,PUINT8 buf)
{
UINT8 i;
ROM_ADDR_H = DATA_FLASH_ADDR >> 8;
Addr <<= 1;
for(i=0;i<len;i++){
ROM_ADDR_L = Addr + i*2; //Addr<64><72><EFBFBD><EFBFBD>Ϊż<CEAA><C5BC>ַ
ROM_CTRL = ROM_CMD_READ;
// if ( ROM_STATUS & bROM_CMD_ERR ) return( 0xFF ); // unknown command
*(buf+i) = ROM_DATA_L;
}
return i;
}