155 lines
5.3 KiB
C
155 lines
5.3 KiB
C
|
/**
|
|||
|
****************************************************************************************************
|
|||
|
* @file bsp_flash.c
|
|||
|
* @author HF_ZQ
|
|||
|
* @brief STM32<EFBFBD>ڲ<EFBFBD>FLASH<EFBFBD><EFBFBD>д <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @license Copyright (c) 2020-2050
|
|||
|
****************************************************************************************************
|
|||
|
**/
|
|||
|
//#include "./SYSTEM/usart/usart.h"
|
|||
|
//#include "./SYSTEM/delay/delay.h"
|
|||
|
#include "bsp_flash.h"
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> (32λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* @param faddr : <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ַ (<EFBFBD>˵<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ4<EFBFBD><EFBFBD><EFBFBD><EFBFBD>!!)
|
|||
|
* @retval <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (32λ)
|
|||
|
*/
|
|||
|
uint32_t stmflash_read_word(uint32_t faddr)
|
|||
|
{
|
|||
|
return *(volatile uint32_t *)faddr;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡij<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param addr : lash<EFBFBD><EFBFBD>ַ
|
|||
|
* @retval 0~11,<EFBFBD><EFBFBD>addr<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
uint8_t stmflash_get_flash_sector(uint32_t addr)
|
|||
|
{
|
|||
|
if (addr < ADDR_FLASH_SECTOR_1) return FLASH_SECTOR_0;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_2) return FLASH_SECTOR_1;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_3) return FLASH_SECTOR_2;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_4) return FLASH_SECTOR_3;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_5) return FLASH_SECTOR_4;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_6) return FLASH_SECTOR_5;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_7) return FLASH_SECTOR_6;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_8) return FLASH_SECTOR_7;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_9) return FLASH_SECTOR_8;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_10) return FLASH_SECTOR_9;
|
|||
|
else if (addr < ADDR_FLASH_SECTOR_11) return FLASH_SECTOR_10;
|
|||
|
return FLASH_SECTOR_11;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>FLASH ָ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>, д<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* @note <EFBFBD><EFBFBD>ΪSTM32F4<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>̫<EFBFBD><EFBFBD>,û<EFBFBD>취<EFBFBD><EFBFBD><EFBFBD>ر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D><EFBFBD><EFBFBD><EFBFBD>д.
|
|||
|
* <EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OTP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD>Ч!<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>дOTP<EFBFBD><EFBFBD>!
|
|||
|
* OTP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>Χ:0X1FFF7800~0X1FFF7A0F(ע<EFBFBD>⣺<EFBFBD><EFBFBD><EFBFBD><EFBFBD>16<EFBFBD>ֽڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OTP<EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* @param waddr : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ (<EFBFBD>˵<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ4<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>!!,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!)
|
|||
|
* @param pbuf : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @param length : Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>(32λ)<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD>32λ<EFBFBD><EFBFBD><EFBFBD>ݵĸ<EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* @retval <EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void stmflash_write(uint32_t waddr, uint32_t *pbuf, uint32_t length)
|
|||
|
{
|
|||
|
FLASH_EraseInitTypeDef flasheraseinit;
|
|||
|
HAL_StatusTypeDef FlashStatus=HAL_OK;
|
|||
|
|
|||
|
uint32_t addrx = 0;
|
|||
|
uint32_t endaddr = 0;
|
|||
|
uint32_t sectorerror=0;
|
|||
|
|
|||
|
if (waddr < STM32_FLASH_BASE || waddr % 4 || /* д<><D0B4><EFBFBD><EFBFBD>ַС<D6B7><D0A1> STM32_FLASH_BASE, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>Ƿ<EFBFBD>. */
|
|||
|
waddr > (STM32_FLASH_BASE + STM32_FLASH_SIZE)) /* д<><D0B4><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD> STM32_FLASH_BASE + STM32_FLASH_SIZE, <20>Ƿ<EFBFBD>. */
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
HAL_FLASH_Unlock(); /* <20><><EFBFBD><EFBFBD> */
|
|||
|
FLASH->ACR &= ~(1 << 10); /* FLASH<53><48><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>!!! */
|
|||
|
|
|||
|
addrx = waddr; /* д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ */
|
|||
|
endaddr = waddr + length * 4; /* д<><D0B4><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>ַ */
|
|||
|
|
|||
|
if (addrx < 0X1FFF0000) /* ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD><E6B4A2>,<2C><><EFBFBD><EFBFBD>Ҫִ<D2AA>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!! */
|
|||
|
{
|
|||
|
while (addrx < endaddr) /* ɨ<><C9A8>һ<EFBFBD><D2BB><EFBFBD>ϰ<EFBFBD>.(<28>Է<EFBFBD>FFFFFFFF<46>ĵط<C4B5>,<2C>Ȳ<EFBFBD><C8B2><EFBFBD>) */
|
|||
|
{
|
|||
|
if (stmflash_read_word(addrx) != 0XFFFFFFFF) /* <20>з<EFBFBD>0XFFFFFFFF<46>ĵط<C4B5>,Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
{
|
|||
|
flasheraseinit.TypeErase=FLASH_TYPEERASE_SECTORS; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
flasheraseinit.Sector=stmflash_get_flash_sector(addrx); /* Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
flasheraseinit.NbSectors=1; /* һ<><D2BB>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
flasheraseinit.VoltageRange=FLASH_VOLTAGE_RANGE_3; /* <20><>ѹ<EFBFBD><D1B9>Χ<EFBFBD><CEA7>VCC=2.7~3.6V֮<EFBFBD><EFBFBD>!! */
|
|||
|
|
|||
|
if(HAL_FLASHEx_Erase(&flasheraseinit, §orerror) != HAL_OK)
|
|||
|
{
|
|||
|
break;/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
addrx += 4;
|
|||
|
}
|
|||
|
FLASH_WaitForLastOperation(FLASH_WAITETIME); /* <20>ȴ<EFBFBD><C8B4>ϴβ<CFB4><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
FlashStatus=FLASH_WaitForLastOperation(FLASH_WAITETIME); /* <20>ȴ<EFBFBD><C8B4>ϴβ<CFB4><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
|||
|
if (FlashStatus==HAL_OK)
|
|||
|
{
|
|||
|
while (waddr < endaddr) /* д<><D0B4><EFBFBD><EFBFBD> */
|
|||
|
{
|
|||
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, waddr, *pbuf) != HAL_OK) /* д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
{
|
|||
|
break; /* д<><D0B4><EFBFBD>쳣 */
|
|||
|
}
|
|||
|
|
|||
|
waddr += 4;
|
|||
|
pbuf++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
FLASH->ACR |= 1 << 10; /* FLASH<53><48><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>fetch */
|
|||
|
|
|||
|
HAL_FLASH_Lock(); /* <20><><EFBFBD><EFBFBD> */
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param raddr : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
|
|||
|
* @param pbuf : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @param length: Ҫ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(32)<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>4<EFBFBD><EFBFBD><EFBFBD>ֽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @retval <EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void stmflash_read(uint32_t raddr, uint32_t *pbuf, uint32_t length)
|
|||
|
{
|
|||
|
uint32_t i;
|
|||
|
|
|||
|
for (i = 0; i < length; i++)
|
|||
|
{
|
|||
|
pbuf[i] = stmflash_read_word(raddr); /* <20><>ȡ4<C8A1><34><EFBFBD>ֽ<EFBFBD>. */
|
|||
|
raddr += 4; /* ƫ<><C6AB>4<EFBFBD><34><EFBFBD>ֽ<EFBFBD>. */
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/******************************************************************************************/
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD> */
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(д1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* @param waddr : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
|
|||
|
* @param wdata : Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @retval <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void test_write(uint32_t waddr, uint32_t wdata)
|
|||
|
{
|
|||
|
stmflash_write(waddr, &wdata, 1); /* д<><D0B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> */
|
|||
|
}
|
|||
|
|