STM32F103-FLASH读写

2024-07-01 12:25:32 浏览数 (2)

直接上代码

flash.c

代码语言:javascript复制
#include "bsp_flash.h"




//从指定地址开始写入需要长度的数据    
void FlashWrite(u32 writeaddr,u32 * buffdata,u32 length)
{
        uint32_t i = 0;                                
        uint32_t erase_counter = 0x00;                //擦除的页数
        uint32_t address = 0x00;                    //写入的地址
        uint32_t numpage = 0x00;                    //写入页数
        FLASH_Status flash_status = FLASH_COMPLETE;    //记录每次擦除的结果
        
        address = writeaddr;
        
        //非法地址则返回
        if(writeaddr < WRITE_START_ADDR || writeaddr%4)
            return;
        //解锁
        FLASH_Unlock();
        //计算要擦除的页数
        numpage = (WRITE_END_ADDR - address ) / FLASH_PAGE_SIZE;
        //清除所有标志
        FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
        
        //按页擦除
        for(erase_counter = 0;(erase_counter < numpage) && (flash_status == FLASH_COMPLETE);erase_counter  )
        {
            flash_status = FLASH_ErasePage(address   (FLASH_PAGE_SIZE * erase_counter));
        }
        
        for(i = 0;(i < length) && (flash_status == FLASH_COMPLETE); i  )
        {
            flash_status = FLASH_ProgramWord(address,buffdata[i]);
            address = address   4;
        }
        //上锁
        FLASH_Lock();
        if(flash_status == FLASH_COMPLETE)
        {
            return TRUE;
        }
        return FALSE;
}

//从指定地址读取固定长度数据
void FlashRead(u32 readaddr,u32 * buffdata,u32 length)
{
        uint32_t i = 0;
        uint32_t address = 0x00;
        
        address = readaddr;
        
        for(i = 0;i < length; i  )
        {
            buffdata[i] = (*(__IO uint32_t*)address);
            address  = 4;
        }
    
}

flash.h

代码语言:javascript复制
#ifndef _BSP_FLASH_H_
#define _BSP_FLASH_H_


#include "stm32f10x.h"

#define TRUE                    1
#define FALSE                    0

/* STM32大容量产品每页大小2KByte,中、小容量产品每页大小1KByte */
#if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) || defined (STM32F10X_XL)
  #define FLASH_PAGE_SIZE    ((uint16_t)0x800)    //2048
#else
  #define FLASH_PAGE_SIZE    ((uint16_t)0x400)    //1024
#endif

//写入的起始地址 
#define WRITE_START_ADDR        ((uint32_t)0x08008000)
//写入结束地址
#define WRITE_END_ADDR            ((uint32_t)0x0800C000)

void FlashWrite(u32 writeaddr,u32 * buffdata,u32 length);
void FlashRead(u32 readaddr,u32 * buffdata,u32 length);

#endif

main.c

代码语言:javascript复制
#include "stm32f10x.h"
#include "flash.h"

int main(void)
{    
    u32 in_data[5]={11,22,33,44,55};//要写入的数据
    u32 out_data[5];//读存放
    int i;
    u8 STATUS=0;
    Usart1Init();//串口1配置
    
    STATUS=FlashWrite(0x08001800,in_data,5);
    Delay_ms(500);
    if(STATUS)
    {            
            FlashWrite(0x08001800,out_data,5);
            printf("rn The Five Data Is : rn");
            for(i=0;i<5;i  )
            {
                    printf("r %d r",out_data[i]);
            }
    }
    while(1);
}

0 人点赞