目录
- 1. 配置
- 2. 时钟
- 3. 中断
- 4. 增加代码部分
- 4.1 stm32flash.c 内部代码
- 4.2 stm32flash.h 内部代码
- 4.3 例子
1. 配置
似乎没有与FLASH相关的…
2. 时钟
大同小异
3. 中断
中断:一定要打开
4. 增加代码部分
在此增加了 “stm32flash.c” 和 “stm32flash.h” 文件
4.1 stm32flash.c 内部代码
写入部分:
/*带检查的写入*/
#define FLASH_WAITETIME 1000#define STM32_FLASH_BASE 0x08000000 //STM32 FLASH的起始地址#define STM_SECTOR_SIZE 2048 //大容量STM32的扇区大小为2Kuint16_t STMFLASH_BUF[STM_SECTOR_SIZE/2];void STMFLASH_Write(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite)
{uint32_t secpos; //扇区地址uint16_t secoff; //扇区内偏移地址(16位字计算)uint16_t secremain; //扇区内剩余地址(16位字计算)uint16_t i;uint32_t offaddr; //去掉0X08000000后的地址if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*512))){return;//非法地址}HAL_FLASH_Unlock(); //解锁offaddr=WriteAddr-STM32_FLASH_BASE; //实际偏移地址.secpos=offaddr/STM_SECTOR_SIZE; //扇区地址 0~127 for STM32F103RBT6secoff=(offaddr%STM_SECTOR_SIZE)/2; //在扇区内的偏移(2个字节为基本单位.)secremain=STM_SECTOR_SIZE/2-secoff; //扇区剩余空间大小if(NumToWrite<=secremain){secremain=NumToWrite;//不大于该扇区范围}while(1){STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//读出整个扇区的内容for(i=0;i<secremain;i++) //校验数据{if(STMFLASH_BUF[secoff+i]!=0XFFFF){break;//需要擦除}}if(i<secremain) //需要擦除{FLASH_PageErase(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE); //擦除这个扇区FLASH_WaitForLastOperation(FLASH_WAITETIME); //等待上次操作完成CLEAR_BIT(FLASH->CR, FLASH_CR_PER); //清除CR寄存器的PER位,此操作应该在FLASH_PageErase()中完成!//但是HAL库里面并没有做,应该是HAL库bug!for(i=0;i<secremain;i++)//复制{STMFLASH_BUF[i+secoff]=pBuffer[i];}STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//写入整个扇区}else{FLASH_WaitForLastOperation(FLASH_WAITETIME); //等待上次操作完成STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//写已经擦除了的,直接写入扇区剩余区间.}if(NumToWrite==secremain){break;//写入结束了}else//写入未结束{secpos++; //扇区地址增1secoff=0; //偏移位置为0pBuffer+=secremain; //指针偏移WriteAddr+=secremain*2; //写地址偏移(16位数据地址,需要*2)NumToWrite-=secremain; //字节(16位)数递减if(NumToWrite>(STM_SECTOR_SIZE/2)){secremain=STM_SECTOR_SIZE/2;//下一个扇区还是写不完}else{secremain=NumToWrite;//下一个扇区可以写完了}}};HAL_FLASH_Lock(); //上锁
}/** 无检查写入* WriteAddr - 地址* pBuffer - 数据* NumToWrite - 长度** */
void STMFLASH_Write_NoCheck(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite)
{uint16_t i;for(i=0;i<NumToWrite;i++){HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,WriteAddr,pBuffer[i]);WriteAddr+=2; //地址增加2.}
}
读取部分:
/** 读取半字 16位* faddr - 地址* */
uint16_t STMFLASH_ReadHalfWord(uint32_t faddr)
{return *(__IO uint16_t*)faddr;
}/*读取len个16位数据*/
void STMFLASH_Read(uint32_t ReadAddr,uint16_t *pBuffer,uint16_t NumToRead)
{uint16_t i;for(i=0;i<NumToRead;i++){pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.ReadAddr+=2;//偏移2个字节.}
}
4.2 stm32flash.h 内部代码
#ifndef __STMFLASH_H
#define __STMFLASH_H#include "stm32f0xx_hal.h"
#include "main.h"void STMFLASH_Write(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite);
void STMFLASH_Write_NoCheck(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite);void STMFLASH_Read(uint32_t ReadAddr,uint16_t *pBuffer,uint16_t NumToRead);
uint16_t STMFLASH_ReadHalfWord(uint32_t faddr);#endif
4.3 例子
#define FLASH_0400_ADDR 0x0800F000 //地址宏 60Kuint16_t R_init_data = 16000;
uint16_t R_0400_F=16000;if(STMFLASH_ReadHalfWord(FLASH_0400_ADDR) > 50000) //读半字 2byte,16位数据,初始为0xFFFF(65535);
{STMFLASH_Write(FLASH_0400_ADDR, &R_init_data, 1); //写入16位数据,16000(0x3E80),长度1个半字,2byte(1个16位数据);
}
else
{R_0400_F = STMFLASH_ReadHalfWord(FLASH_0400_ADDR); //读取数据赋值
}