1.5wuling_zhuanjietou/SYSTEM/bsp_cpu_flash.c

223 lines
5.3 KiB
C
Raw Normal View History

2024-10-17 01:06:51 +00:00
/*
*********************************************************************************************************
*
* ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : cpu<EFBFBD>ڲ<EFBFBD>falsh<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>
* <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : bsp_cpu_flash.c
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> : V1.0
* ˵ <EFBFBD><EFBFBD> : <EFBFBD><EFBFBD><EFBFBD>дCPU<EFBFBD>ڲ<EFBFBD>Flash<EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>޸ļ<EFBFBD>¼ :
* <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> ˵<EFBFBD><EFBFBD>
*
*********************************************************************************************************
*/
#include "Sys.h"
#include "misc.h"
#include "stm32f10x_flash.h"
#include "bsp_cpu_flash.h"
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: bsp_GetSector
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
*********************************************************************************************************
*/
uint32_t bsp_GetSector(uint32_t _ulWrAddr)
{
uint32_t sector = 0;
sector = _ulWrAddr & SECTOR_MASK;
return sector;
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: bsp_ReadCpuFlash
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>ȡCPU Flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD>_ucpDst : Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* _ulFlashAddr : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
* _ulSize : <EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: 0=<EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>1=ʧ<EFBFBD><EFBFBD>
*********************************************************************************************************
*/
uint8_t bsp_ReadCpuFlash(uint32_t _ulFlashAddr, uint8_t *_ucpDst, uint32_t _ulSize)
{
uint32_t i;
/* <20><><EFBFBD><EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>оƬ<D0BE><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򲻸<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (_ulFlashAddr + _ulSize > FLASH_BASE_ADDR + FLASH_SIZE)
{
return 1;
}
/* <20><><EFBFBD><EFBFBD>Ϊ0ʱ<30><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַΪ<D6B7><CEAA><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (_ulSize == 0)
{
return 1;
}
for (i = 0; i < _ulSize; i++)
{
*_ucpDst++ = *(uint8_t *)_ulFlashAddr++;
}
return 0;
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: bsp_CmpCpuFlash
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: <EFBFBD>Ƚ<EFBFBD>Flashָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: _ulFlashAddr : Flash<EFBFBD><EFBFBD>ַ
* _ucpBuf : <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* _ulSize : <EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ:
* FLASH_IS_EQU 0 Flash<EFBFBD><EFBFBD><EFBFBD>ݺʹ<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* FLASH_REQ_WRITE 1 Flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>д
* FLASH_REQ_ERASE 2 Flash<EFBFBD><EFBFBD>Ҫ<EFBFBD>Ȳ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>д
* FLASH_PARAM_ERR 3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*********************************************************************************************************
*/
uint8_t bsp_CmpCpuFlash(uint32_t _ulFlashAddr, uint8_t *_ucpBuf, uint32_t _ulSize)
{
uint32_t i;
uint8_t ucIsEqu; /* <20><><EFBFBD>ȱ<EFBFBD>־ */
uint8_t ucByte;
/* <20><><EFBFBD><EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>оƬ<D0BE><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򲻸<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (_ulFlashAddr + _ulSize > FLASH_BASE_ADDR + FLASH_SIZE)
{
return FLASH_PARAM_ERR; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
}
/* <20><><EFBFBD><EFBFBD>Ϊ0ʱ<30><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ */
if (_ulSize == 0)
{
return FLASH_IS_EQU; /* Flash<73><68><EFBFBD>ݺʹ<DDBA>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
}
ucIsEqu = 1; /* <20>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽںʹ<DABA>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 */
for (i = 0; i < _ulSize; i++)
{
ucByte = *(uint8_t *)_ulFlashAddr;
if (ucByte != *_ucpBuf)
{
if (ucByte != 0xFF)
{
return FLASH_REQ_ERASE; /* <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д */
}
else
{
ucIsEqu = 0; /* <20><><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>Ҫд */
}
}
_ulFlashAddr++;
_ucpBuf++;
}
if (ucIsEqu == 1)
{
return FLASH_IS_EQU; /* Flash<73><68><EFBFBD>ݺʹ<DDBA>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD> */
}
else
{
return FLASH_REQ_WRITE; /* Flash<73><68><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>д */
}
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: bsp_WriteCpuFlash
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: д<EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>CPU <EFBFBD>ڲ<EFBFBD>Flash<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: _ulFlashAddr : Flash<EFBFBD><EFBFBD>ַ
* _ucpSrc : <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* _ulSize : <EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: 0-<EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>1-<EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2-дFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
*********************************************************************************************************
*/
uint8_t bsp_WriteCpuFlash(uint32_t _ulFlashAddr, uint8_t *_ucpSrc, uint32_t _ulSize)
{
uint32_t i;
uint8_t ucRet;
uint16_t usTemp,j;
FLASH_Status status = FLASH_COMPLETE;
/* <20><><EFBFBD><EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>оƬ<D0BE><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򲻸<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (_ulFlashAddr + _ulSize > FLASH_BASE_ADDR + FLASH_SIZE)
{
return 1;
}
/* <20><><EFBFBD><EFBFBD>Ϊ0 ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (_ulSize == 0)
{
return 0;
}
/* <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
// if ((_ulSize % 2) != 0)
// {
// return 1;
// }
if ((_ulSize % 2) != 0)
{
_ulSize= _ulSize+1;
}
ucRet = bsp_CmpCpuFlash(_ulFlashAddr, _ucpSrc, _ulSize);
if (ucRet == FLASH_IS_EQU)
{
return 0;
}
__set_PRIMASK(1); /* <20><><EFBFBD>ж<EFBFBD> */
/* FLASH <20><><EFBFBD><EFBFBD> */
FLASH_Unlock();
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
/* <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> */
// if (ucRet == FLASH_REQ_ERASE)
{
status = FLASH_ErasePage(bsp_GetSector(_ulFlashAddr));
// if (status != FLASH_COMPLETE)
// {
// return 2;
// }
}
j=1000;
while(j--);
/* <20><><EFBFBD>ֽ<EFBFBD>ģʽ<C4A3><CABD><EFBFBD>̣<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>Ч<EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD>԰<EFBFBD><D4B0>ֱ<EFBFBD><D6B1>̣<EFBFBD>һ<EFBFBD><D2BB>д<EFBFBD><D0B4>4<EFBFBD>ֽڣ<D6BD> */
for (i = 0; i < _ulSize / 2; i++)
{
//FLASH_ProgramByte(_ulFlashAddr++, *_ucpSrc++);
usTemp = _ucpSrc[2 * i];
usTemp |= (_ucpSrc[2 * i + 1] << 8);
status = FLASH_ProgramHalfWord(_ulFlashAddr, usTemp);
if (status != FLASH_COMPLETE)
{
break;
}
_ulFlashAddr += 2;
}
/* Flash <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹдFlash<73><68><EFBFBD>ƼĴ<C6BC><C4B4><EFBFBD> */
FLASH_Lock();
__set_PRIMASK(0); /* <20><><EFBFBD>ж<EFBFBD> */
if (status == FLASH_COMPLETE)
{
return 0;
}
return 2;
}
/***************************** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> www.armfly.com (END OF FILE) *********************************/