讀寫STM32內(nèi)部flash讀寫代碼
由于老師的要求,所以寫了一份讀寫STM32內(nèi)部FLASH的代碼,這樣的話就可以把STM32里面沒有用來保存代碼段的部分用來存儲數(shù)據(jù)了。
由于《stm32flash編程手冊》是很久很久以前看的,現(xiàn)在也沒心情去仔細看那份手冊了。大概瀏覽了一下,只看到了STM32里面的flash是以16位為單位讀寫的。
為什么記住這個,因為之前想寫一個字節(jié)老是出錯,所以翻翻手冊,果然看到不能寫1個字節(jié)數(shù)據(jù)。而且還發(fā)現(xiàn),flash寫地址為奇數(shù)時也會出錯。所以這份代碼里面
寫flash的地址只能是偶數(shù)。瀏覽過程中還發(fā)現(xiàn),手冊里面說寫flash和擦除flash的時候會影響cpu的運行速度,沒仔細看(真心不想看)。其他具體手冊里面的資料的話沒有了,
看有沒有高手仔細看完,然后寫個總結(jié)什么的。。。
而且flash的讀寫保護也沒看清楚,好像這些保護都是針對頁大小來保護的話。這樣的話就可以只保護代碼段,這些放數(shù)據(jù)段不用保護了,這樣可以省下flash芯片,不錯吧。
廢話不多說,直接上代碼。。
flash讀寫代碼也不用初始化,拿來直接就用。哈哈。。。。。。。。。。
-------------------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個字節(jié)
while( 1 ) {
Flash_Read(secpos, tmp, FLASH_PAGE_SIZE); //讀出整個扇區(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);//寫入整個扇區(qū)
} else {
Flash_Write_Without_check(iAddress,buf,secremain);//寫已經(jīng)擦除了的,直接寫入扇區(qū)剩余區(qū)間.
}
if(iNumByteToWrite==secremain) //寫入結(jié)束了
break;
else {
secpos += FLASH_PAGE_SIZE;
secoff = 0;//偏移位置為0
buf += secremain; //指針偏移
iAddress += secremain;//寫地址偏移
iNumByteToWrite -= secremain; //字節(jié)數(shù)遞減
if(iNumByteToWrite>FLASH_PAGE_SIZE) secremain=FLASH_PAGE_SIZE;//下一個扇區(qū)還是寫不完
else secremain = iNumByteToWrite; //下一個扇區(qū)可以寫完了
}
}
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