Use internal flash in STM32F4 to save explored maze

No matter how stable your mouse runs in maze, it always comes with the risk to crash during either search run or speed run. If it crashes at search run, you probably just pick it up and have it reset then search again. However, if the mouse successfully search the maze then crashes in the speed run, or the mouse got hardware reset or powered off by accident, no doubt the explored wall information for all previous runs will be all gone.

In order to prevent this from happening, I utilized the internal flash for my GreenGiant V2.2 mouse last year to save explored maze just in case. Link is here

However, the codes are not exactly same for STM32F4 library, and it becomes a little bit tricky to set it up although the whole process is still easy.

Here I am posting the code I use on my GreenGiant V4.1 mouse to explain how to use it. I am using use the flash to save the explored wall information.

Sample Code:

#include “stm32f4xx.h”

#include “stm32f4xx_flash.h”

uint32_t startAddress = 0x080E0000;//starting from 896KB, the beginning of last sector
//0x8010000 is 64KB
//0x8040000 is 256KB
//0x8080000 is 512KB
//0x80C0000 is 768KB

// /* 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 */

void writeFlash(void)
uint32_t i, j;
DI;//not to execute time consuming code in interrupt for controller

FLASH_Unlock();// you need to unlcok flash first
/* Clear All pending flags */

//you need to erase entire sector before write anything
FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);

//VoltageRange_1        ((uint8_t)0x00)  /*!< Device operating range: 1.8V to 2.1V */
//VoltageRange_2        ((uint8_t)0x01)  /*!<Device operating range: 2.1V to 2.7V */
//VoltageRange_3        ((uint8_t)0x02)  /*!<Device operating range: 2.7V to 3.6V */
//VoltageRange_4        ((uint8_t)0x03)  /*!<Device operating range: 2.7V to 3.6V +   External Vpp */
//mSize = 16 for full size micromouse maze
//int16_t mazeWalls[mSize][mSize]  to hold wall information.

//start to copy wall information into flash, the address must exceed every 32bit

//for STM32F4 even if you are only writing 16 bit or 8 bit data

//You only need to exceed 16bit per address by calling the same

//function in STM32F103
for(i=0; i<mSize; i++)
for(j=0; j<mSize; j++)
FLASH_ProgramHalfWord((startAddress + (i*mSize+j)*4),mazeWalls[i][j]);

FLASH_Lock();//lock flash at the end


//when you need to recover the maze information form flash to ram

//just simply call this function. the reading speed for flash is faster

//than write
void readFlash(void)
u32 i, j;
for(i=0; i<mSize; i++)
for(j=0; j<mSize; j++)
mazeWalls[i][j] = *(int16_t *)(startAddress + (i*mSize+j)*4);


Notice stm32f4 is no longer use 1KB or 2KB per page/sector like stm32F103, and it takes nearly 1 second to erase entire 128KB sector since other smaller sectors are occupied by code already. Therefore having the mouse not to do anything else during the flashing process is highly recommended.

The same sample code is available to download here. Link

4 thoughts on “Use internal flash in STM32F4 to save explored maze

  1. If i may ask what license are you using? because i don’t think that you can fit the mouse code on the 32 k limited edition…

  2. I didn’t use free version since the since of the code is limited.
    May be Professional, I don’t remember the exactly version. Should be anything except the free one.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.