最近在写机器人的姿态解算,需要把一些数据保存到stm32f4的内部Flash中。于是调了一下午Flash,整理出了下面这个demo程序。发现stm32f4的功能还挺强的,
8位、16位、32位、64位的数据都能读写。而且还能写入负数,虽然库里提供的函数写数据的类型是无符号的,我也不知道这是什么原因。
flash.c
#include "flash.h"
/****************************************************************************
* 功 能: 获取地址Address对应的sector编号
* 入口参数:地址
* 出口参数:sector编号
* 说 明:无
* 调用方法:无
****************************************************************************/
uint16_t Flash_GetSector(uint32_t Address)
{
uint16_t sector = 0;
if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
{
sector = FLASH_Sector_0;
}
else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
{
sector = FLASH_Sector_1;
}
else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
{
sector = FLASH_Sector_2;
}
else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
{
sector = FLASH_Sector_3;
}
else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
{
sector = FLASH_Sector_4;
}
else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
{
sector = FLASH_Sector_5;
}
else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
{
sector = FLASH_Sector_6;
}
else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
{
sector = FLASH_Sector_7;
}
else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
{
sector = FLASH_Sector_8;
}
else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
{
sector = FLASH_Sector_9;
}
else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
{
sector = FLASH_Sector_10;
}
else/*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11))*/
{
sector = FLASH_Sector_11;
}
return sector;
}
/****************************************************************************
* 功 能: 擦除指定扇区
* 入口参数:SectorNum 扇区号
* 出口参数:无
* 说 明:无
* 调用方法:无
****************************************************************************/
void Flash_EraseSector(uint16_t SectorNum)
{
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
if (FLASH_EraseSector(SectorNum, VoltageRange_3) != FLASH_COMPLETE) while (1);
FLASH_Lock();
}
/****************************************************************************
* 功 能: 写入长度为length的32位数据
* 入口参数:address:地址
length: 数据长度
data_32:要写入的数据指针
* 出口参数:无
* 说 明:无
* 调用方法:无
****************************************************************************/
void Flash_Write32BitDatas(uint32_t address, uint16_t length, int32_t* data_32)
{
uint16_t StartSector, EndSector,i;
FLASH_Unlock(); //解锁FLASH后才能向FLASH中写数据。
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
StartSector = Flash_GetSector(address); //获取FLASH的Sector编号
EndSector = Flash_GetSector(address+4*length);
for (i = StartSector; i < EndSector; i += 8) //每次FLASH编号增加8,可参考上边FLASH Sector的定义。
{
if (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE) while (1);
}
for(i=0; i
#ifndef __FLASH_H__
#define __FLASH_H__
#include "stm32f4xx.h"
/* Base address of the Flash sectors */
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
#define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_9 //用户起始地址,暂定为第十个扇区的起始地址
//API
uint16_t Flash_GetSector(uint32_t Address);
void Flash_EraseSector(uint16_t SectorNum);
void Flash_Write32BitDatas(uint32_t address, uint16_t length, int32_t* data_32);
void Flash_Read32BitDatas(uint32_t address, uint16_t length, int32_t* data_32);
void Flash_Write16BitDatas(uint32_t address, uint16_t length, int16_t* data_16);
void Flash_Read16BitDatas(uint32_t address, uint16_t length, int16_t* data_16);
void Flash_Write8BitDatas(uint32_t address, uint16_t length, int8_t* data_8);
void Flash_Read8BitDatas(uint32_t address, uint16_t length, int8_t* data_8);
#endif
#include "usart.h"
/****************************************************************************
* 名 称:void USART_Configuration(void)
* 功 能: UART4配置
* 入口参数:无
* 出口参数:无
* 说 明:无
* 调用方法:无
****************************************************************************/
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4);
USART_InitStructure.USART_BaudRate = 19200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(UART4,&USART_InitStructure);
USART_Cmd(UART4,ENABLE);
}
/****************************************************************************
* 名 称:int fputc(int ch, FILE *f)
* 功 能: 重定向printf到串口
* 入口参数: 略
* 出口参数:略
* 说 明:无
* 调用方法:无
****************************************************************************/
int fputc(int ch, FILE *f)
{
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
USART_SendData(UART4, (uint8_t) ch);
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
return ch;
}
/****************************************************************************
* 名 称:int fgetc(FILE *f)
* 功 能: 重定向scanf到串口
* 入口参数:略
* 出口参数:略
* 说 明:无
* 调用方法:无
****************************************************************************/
int fgetc(FILE *f)
{
int ch;
while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(UART4);
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
USART_SendData(UART4, (uint8_t) ch);
return ch;
}
usart.h
#ifndef __PRINTF_H__
#define __PRINTF_H__
#include
#include "stm32f4xx.h"
//供调用的API
void USART_Configuration(void);
#endif
main.c
#include "stm32f4xx.h"
#include "flash.h"
#include "usart.h"
void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* Enable GPIO clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* Enable USART clock */
}
int32_t data_32_1[]={-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
int32_t data_32_2[10];
int16_t data_16_1[]={1,2,3,4,5,6,7,8,9,10};
int16_t data_16_2[]={0,0,0,0,0,0,0,0,0,0};
int8_t data_8_1[]={1,2,3,4,5,6,7,8,9,10};
int8_t data_8_2[]={0,0,0,0,0,0,0,0,0,0};
int main()
{
uint8_t j;
RCC_Configuration();
USART_Configuration();
printf("\r\n32位数据:");
printf("\r\n擦除前:");
Flash_Read32BitDatas(FLASH_USER_START_ADDR,10,data_32_2);
for(j=0; j<10; j++)
{
printf("%d ",data_32_2[j]);
}
Flash_EraseSector(FLASH_Sector_9);
printf("\r\n擦除后:");
Flash_Read32BitDatas(FLASH_USER_START_ADDR,10,data_32_2);
for(j=0; j<10; j++)
{
printf("%d ",data_32_2[j]);
}
printf("\r\n读写Flash后:");
Flash_Write32BitDatas(FLASH_USER_START_ADDR, 10, data_32_1);
Flash_Read32BitDatas(FLASH_USER_START_ADDR,10,data_32_2);
for(j=0; j<10; j++)
{
printf("%d ",data_32_2[j]);
}
printf("\r\n16位数据:");
printf("\r\n擦除前:");
Flash_Read16BitDatas(FLASH_USER_START_ADDR,10,data_16_2);
for(j=0; j<10; j++)
{
printf("%d ",data_16_2[j]);
}
Flash_EraseSector(FLASH_Sector_9);
printf("\r\n擦除后:");
Flash_Read16BitDatas(FLASH_USER_START_ADDR,10,data_16_2);
for(j=0; j<10; j++)
{
printf("%d ",data_16_2[j]);
}
printf("\r\n读写Flash后:");
Flash_Write16BitDatas(FLASH_USER_START_ADDR, 10, data_16_1);
Flash_Read16BitDatas(FLASH_USER_START_ADDR,10,data_16_2);
for(j=0; j<10; j++)
{
printf("%d ",data_16_2[j]);
}
printf("\r\n8位数据:");
printf("\r\n擦除前:");
Flash_Read8BitDatas(FLASH_USER_START_ADDR,10,data_8_2);
for(j=0; j<10; j++)
{
printf("%d ",data_8_2[j]);
}
Flash_EraseSector(FLASH_Sector_9);
printf("\r\n擦除后:");
Flash_Read8BitDatas(FLASH_USER_START_ADDR,10,data_8_2);
for(j=0; j<10; j++)
{
printf("%d ",data_8_2[j]);
}
printf("\r\n读写Flash后:");
Flash_Write8BitDatas(FLASH_USER_START_ADDR, 10, data_8_1);
Flash_Read8BitDatas(FLASH_USER_START_ADDR,10,data_8_2);
for(j=0; j<10; j++)
{
printf("%d ",data_8_2[j]);
}
while(1);
}