讀寫(xiě)STM32內(nèi)部flash讀寫(xiě)代碼
由于老師的要求,所以寫(xiě)了一份讀寫(xiě)STM32內(nèi)部FLASH的代碼,這樣的話(huà)就可以把STM32里面沒(méi)有用來(lái)保存代碼段的部分用來(lái)存儲(chǔ)數(shù)據(jù)了。
由于《stm32flash編程手冊(cè)》是很久很久以前看的,現(xiàn)在也沒(méi)心情去仔細(xì)看那份手冊(cè)了。大概瀏覽了一下,只看到了STM32里面的flash是以16位為單位讀寫(xiě)的。
為什么記住這個(gè),因?yàn)橹跋雽?xiě)一個(gè)字節(jié)老是出錯(cuò),所以翻翻手冊(cè),果然看到不能寫(xiě)1個(gè)字節(jié)數(shù)據(jù)。而且還發(fā)現(xiàn),flash寫(xiě)地址為奇數(shù)時(shí)也會(huì)出錯(cuò)。所以這份代碼里面
寫(xiě)flash的地址只能是偶數(shù)。瀏覽過(guò)程中還發(fā)現(xiàn),手冊(cè)里面說(shuō)寫(xiě)flash和擦除flash的時(shí)候會(huì)影響cpu的運(yùn)行速度,沒(méi)仔細(xì)看(真心不想看)。其他具體手冊(cè)里面的資料的話(huà)沒(méi)有了,
看有沒(méi)有高手仔細(xì)看完,然后寫(xiě)個(gè)總結(jié)什么的。。。
而且flash的讀寫(xiě)保護(hù)也沒(méi)看清楚,好像這些保護(hù)都是針對(duì)頁(yè)大小來(lái)保護(hù)的話(huà)。這樣的話(huà)就可以只保護(hù)代碼段,這些放數(shù)據(jù)段不用保護(hù)了,這樣可以省下flash芯片,不錯(cuò)吧。
廢話(huà)不多說(shuō),直接上代碼。。
flash讀寫(xiě)代碼也不用初始化,拿來(lái)直接就用。哈哈。。。。。。。。。。
-------------------DataFlash.c----------------------------------
#include "DataFlash.h"
uint16_t Flash_Write_Without_check(uint32_t iAddress, uint8_t *buf, uint16_t iNumByteToWrite) {
uint16_t i;
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
i = 0;
// FLASH_UnlockBank1();
while((i < iNumByteToWrite) && (FLASHStatus == FLASH_COMPLETE))
{
FLASHStatus = FLASH_ProgramHalfWord(iAddress, *(uint16_t*)buf);
i = i+2;
iAddress = iAddress + 2;
buf = buf + 2;
}
return iNumByteToWrite;
}
/**
* @brief Programs a half word at a specified Option Byte Data address.
* @note This function can be used for all STM32F10x devices.
* @param Address: specifies the address to be programmed.
* @param buf: specifies the data to be programmed.
* @param iNbrToWrite: the number to write into flash
* @retval if success return the number to write, -1 if error
*
*/
int Flash_Write(uint32_t iAddress, uint8_t *buf, uint32_t iNbrToWrite) {
/* Unlock the Flash Bank1 Program Erase controller */
uint32_t secpos;
uint32_t iNumByteToWrite = iNbrToWrite;
uint16_t secoff;
uint16_t secremain;
uint16_t i = 0;
uint8_t tmp[FLASH_PAGE_SIZE];
FLASH_UnlockBank1();
secpos=iAddress & (~(FLASH_PAGE_SIZE -1 )) ;//扇區(qū)地址
secoff=iAddress & (FLASH_PAGE_SIZE -1); //在扇區(qū)內(nèi)的偏移
secremain=FLASH_PAGE_SIZE-secoff; //扇區(qū)剩余空間大小
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
if(iNumByteToWrite<=secremain) secremain = iNumByteToWrite;//不大于4096個(gè)字節(jié)
while( 1 ) {
Flash_Read(secpos, tmp, FLASH_PAGE_SIZE); //讀出整個(gè)扇區(qū)
for(i=0;i
}
if(i
if(FLASHStatus != FLASH_COMPLETE)
return -1;
for(i=0;i
}
Flash_Write_Without_check(secpos ,tmp ,FLASH_PAGE_SIZE);//寫(xiě)入整個(gè)扇區(qū)
} else {
Flash_Write_Without_check(iAddress,buf,secremain);//寫(xiě)已經(jīng)擦除了的,直接寫(xiě)入扇區(qū)剩余區(qū)間.
}
if(iNumByteToWrite==secremain) //寫(xiě)入結(jié)束了
break;
else {
secpos += FLASH_PAGE_SIZE;
secoff = 0;//偏移位置為0
buf += secremain; //指針偏移
iAddress += secremain;//寫(xiě)地址偏移
iNumByteToWrite -= secremain; //字節(jié)數(shù)遞減
if(iNumByteToWrite>FLASH_PAGE_SIZE) secremain=FLASH_PAGE_SIZE;//下一個(gè)扇區(qū)還是寫(xiě)不完
else secremain = iNumByteToWrite; //下一個(gè)扇區(qū)可以寫(xiě)完了
}
}
FLASH_LockBank1();
return iNbrToWrite;
}
/**
* @brief Programs a half word at a specified Option Byte Data address.
* @note This function can be used for all STM32F10x devices.
* @param Address: specifies the address to be programmed.
* @param buf: specifies the data to be programmed.
* @param iNbrToWrite: the number to read from flash
* @retval if success return the number to write, without error
*
*/
int Flash_Read(uint32_t iAddress, uint8_t *buf, int32_t iNbrToRead) {
int i = 0;
while(i < iNbrToRead ) {
*(buf + i) = *(__IO uint8_t*) iAddress++;
i++;
}
return i;
}
-------------------DataFlash.h----------------------------------
#ifndef __DATAFLASH_H__
#define __DATAFLASH_H__
#include "stm32f10x.h"
#include "stm32f10x_flash.h"
#if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) || defined (STM32F10X_XL)
#define FLASH_PAGE_SIZE ((uint16_t)0x800)
#else
#define FLASH_PAGE_SIZE ((uint16_t)0x400)
#endif
int Flash_Read(uint32_t iAddress, uint8_t *buf, int32_t iNbrToRead) ;
int Flash_Write(uint32_t iAddress, uint8_t *buf, uint32_t iNbrToWrite);
#endif