MM32Flash读写操作(兼容STM32)
Flash基础描述
思维导图
编程实现读写操作
主函数结构
#include "delay.h"
#include "sys.h"
#include "led.h"
#include "uart.h"
#include "flash.h"
void flashwrite(char* data);
void flashread();
/********************************************************************************************************
**函数信息 :main(void)
**功能描述 :
**输入参数 :无
**输出参数 :无
********************************************************************************************************/
int main(void)
{
u8 t = 0;
char data[] = {"Hello,world."}; //将要写入的数据
delay_init();
LED_Init();
uart_initwBaudRate(9600);
flashwrite(data);
flashread();
while(1) //LED,无限循环
{
printf("program ok
");
flashread(); //从flash读写入的数据
LED1_TOGGLE();
LED2_TOGGLE();
LED3_TOGGLE();
LED4_TOGGLE();
delay_ms(1000);
}
}
Flash写操作
#include "flash.h"
#include "uart.h"
#include "flash.h"
void flashwrite(char* data)
{
u32 baseaddr = 0x08002ffc; //查看Flash地址,定义将要写入的起始地址
int i = 0;
/* Porgram FLASH Bank1 ********************************************************/
/* Unlock the Flash Bank1 Program Erase controller */
FLASH->KEYR = 0x45670123; //解锁
FLASH->KEYR = 0xCDEF89AB;
/* Clear All pending flags */
FLASH->SR = 0x00000001|0x00000004|0x00000010;
//PG(Programming) = 1,选择编程操作
FLASH->CR |= 0x1;
while(i < 12) //循环写入数据
{
*(__IO uint16_t*)baseaddr = *data | *(data + 1)<<8;
baseaddr = baseaddr + 2;
data += 2;
i++;
}
//Lock flash
FLASH->CR |= 0x00000080;
}
Flash读操作
#include "flash.h"
#include "uart.h"
#include "flash.h"
void flashread()
{
/* Porgram FLASH Bank1 ********************************************************/
u32 baseaddr = 0x08002ffc; //查看Flash地址,定义将要读取的起始地址
char out[15];
int i = 0;
while(i < 15) //循环读取数据
{
out[i] = *(__IO uint8_t*)baseaddr;
baseaddr = baseaddr + 1;
printf("%c",out[i]); //通过uart打印输出到串口助手窗口
i++;
}
printf("
");
}
实现结果
参考资料
[1]. MM32 miniboard资料