/**
******************************************************************************
* @file NorFlash.c
* @author YqZhao
* @version V1.0.0
* @date 26-April-2013
* @brief This file provides firmware functions to manage the following
* functionalities of the NorFlash FSMC peripheral:
* - Initialization FSMC bus of NorFlash
* - Write datas of the NorFlash
* - Read datas of the NorFlash
* - Read ID
* - Erase the section of the NorFlash
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
* 1. FSMC pins configuration about NorFlash in User.h
*
* 2. Initialise and configure the NorFlash using S29glInit
* PLS refer to the stm32f2xx_fsmc.c file
*
* 3. Erase the desired section using s29glSectorErase if writing data needed
*
*
* 4. Write the desired data using s29glWrite.
*
* 5. Read the desired data using s29glRead.
*
* 6. If needed read the ID using S29gReadId.
* @endverbatim
*
******************************************************************************
* @attention
* SectionNum Address ________ __________
0 0x000000 ________| | ________ 16bits
1 0x020000 ________| | Data
2 0x040000 | |
| |
| |
| |
255 0x1FE0000 ________| |
|_________________ |
*
******************************************************************************
*/
#include "include.h"
#ifndef WP_NORFLASH
#define WP_NORFLASH PE_O.BitSet.v01
#endif
#ifndef RESET_NORFLASH
#define RESET_NORFLASH PE_O.BitSet.v01
#endif
/*******************************************************************************
Function: Nor Flash Erase Operation
Param : sectorNum stands for the beigin of section
sectorCount stans for how many sections need erase
Return : NOR_Status
NOR_SUCCESS = 0, // Stands for success
NOR_ONGOING, // Stands for ongoing something
NOR_ERROR, // Stands for error
NOR_TIMEOUT // Stands for time out
Comment : When need write data must erase the section respectively first
*******************************************************************************/
NOR_Status s29glSectorErase( int sectorNum, int sectorCount)
{
int ii,jj;
uint16_t wTemp1,wTemp2;
volatile uint16_t* pFlash;
unsigned int timeout;
for ( ii = 0; ii < sectorCount; ii++ )
{
pFlash = ( uint16_t * ) ( NOR_FLASH_START_ADDR+( sectorNum + ii ) * ( 128*1024 ) );
NOR_WRITE(ADDR_SHIFT(0x00), 0xf0);/* RESET ????*/
NOR_WRITE(ADDR_SHIFT(0x555),0xaa);
NOR_WRITE(ADDR_SHIFT(0x2aa),0x55);
NOR_WRITE(ADDR_SHIFT(0x555),0x80);
NOR_WRITE(ADDR_SHIFT(0x555),0xaa);
NOR_WRITE(ADDR_SHIFT(0x2aa),0x55);
*pFlash = 0x30;
wTemp1 = *pFlash & 0x40;
timeout = 0xf000000;
while ( 1 )
{
FEED_DOG();
wTemp2 = *pFlash & 0x40;
if ( wTemp1 == wTemp2 )
{
break;
}
else
{
wTemp1 = wTemp2;
}
if ( timeout-- <= 0 )
{
return ( NOR_TIMEOUT );
}
}
NOR_WRITE(ADDR_SHIFT(0x00), 0xf0f0);/* RESET ????*/
for ( jj = 0; jj < 128*1024/2; jj++ )/* Maybe too long time */
{
if ( *pFlash != 0xffff )
{
return ( NOR_ERROR );
}
pFlash ++;
}
}
return(NOR_SUCCESS);
}
/*******************************************************************************
Function: Nor Flash Write Operation
Param : address stands for the beigin of address
buffer stans for the datas be written into the NorFlash
length stans for how many bytes need to write NorFlash
Return : NOR_Status
NOR_SUCCESS = 0, // Stands for success
NOR_ONGOING, // Stands for ongoing something
NOR_ERROR, // Stands for error
NOR_TIMEOUT // Stands for time out
Comment :
*******************************************************************************/
NOR_Status s29glWrite( uint32_t address, uint8_t * buffer, int length )
{
uint16_t ii;
uint32_t timeout;
uint16_t *pBuffer=(uint16_t *)buffer;
volatile uint16_t *pFlash;
uint16_t wTemp1, wTemp2;
pFlash =(uint16_t *)(NOR_FLASH_START_ADDR + address);
for ( ii = 0; ii <(length>>1); ii++ )
{
NOR_WRITE(ADDR_SHIFT(0x555),0xaa);
NOR_WRITE(ADDR_SHIFT(0x2aa),0x55);
NOR_WRITE(ADDR_SHIFT(0x555),0xa0);
*pFlash = *pBuffer;
wTemp1 = *pFlash & 0x40;
timeout = 0x5000000;
while ( 1 )
{
FEED_DOG();
wTemp2 = *pFlash & 0x40;
if ( wTemp1 == wTemp2 )
{
break;
}
else
{
wTemp1 = wTemp2;
}
if ( timeout-- <= 0 )
{
return ( NOR_TIMEOUT );
}
}
NOR_WRITE(ADDR_SHIFT(0x00), 0xf0);
if ( *pFlash != *pBuffer )
{
return ( NOR_ERROR );
}
pFlash ++;
pBuffer ++;
}
return (NOR_SUCCESS);
}
/*******************************************************************************
Function: Nor Flash Read Operation
Param : address stands for the beigin of address
buffer stans for the datas to be read our of the NorFlash
length stans for how many bytes need to read NorFlash
Return : No
Comment :
*******************************************************************************/
void s29glRead(uint32_t address, uint8_t * buffer, int length )
{
uint16_t *pBuffer=(uint16_t *)buffer;
volatile uint16_t *pFlash;
uint32_t ii;
NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
NOR_WRITE((NOR_FLASH_START_ADDR + address), 0x00F0);
pFlash =(uint16_t *)(NOR_FLASH_START_ADDR + address);
for( ii=0; ii<(length>>1); ii++)
{
*pBuffer++ = *pFlash++;
}
}
/*******************************************************************************
Function: Nor Flash initiation Operation
Param : NOR_ID save the ID read form chip
Return : No
Comment :
*******************************************************************************/
void S29gReadId(NOR_IDTypeDef* NOR_ID)
{
NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
NOR_WRITE(ADDR_SHIFT(0x0555), 0x0090);
NOR_ID->Manufacturer_Code = *(vu16 *) ADDR_SHIFT(0x0000);
NOR_ID->Device_Code1 = *(vu16 *) ADDR_SHI
小波思基
- 粉丝: 85
- 资源: 1万+
最新资源
- Centos7.x通过RPM包升级OpenSSH9.6最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos7.x通过RPM包升级OpenSSH9.9最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos7.x通过RPM包升级OpenSSH9.8最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos7.x通过RPM包升级OpenSSH9.7最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- 机器人开发的操作案例练习
- Centos6.x通过RPM包升级OpenSSH9.7最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos6.x通过RPM包升级OpenSSH9.8最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos6.x通过RPM包升级OpenSSH9.9最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- 软考冲刺的基本内容和操作
- Centos8.x通过RPM包升级OpenSSH9.8(openssl-3.0) 升级有风险,前务必做好快照,以免升级后出现异常影响业务
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0