GlobLib
HAL and API libraries for MCUs and hardware.

This module contains functions for modifing internal flash memory. More...

Files

file  stm32f103cb_flash.h
 Header file for stm32f103cb FLASH.
 

Functions

mcu_error FLASH_clearPage (uint32_t pageNumber)
 Clear a page (1kb) of flash. More...
 
mcu_error FLASH_write (uint32_t pageNumber, uint32_t address, uint32_t data)
 Write a single word to flash. More...
 
mcu_error FLASH_writes (uint32_t pageNumber, uint32_t address, uint32_t *data, uint16_t size)
 Write multiple words to flash, address incremented automatically. More...
 
uint32_t FLASH_read (uint32_t pageNumber, uint32_t address)
 Read a single word from flash memory. More...
 
mcu_error FLASH_reads (uint32_t pageNumber, uint32_t address, uint32_t *data, uint16_t size)
 Read multiple words from flash. More...
 
mcu_error FLASH_readPage (uint32_t pageNumber, uint32_t *data)
 Read an entire page from flash. More...
 
uint8_t FLASH_pageEmpty (uint32_t pageNumber)
 Determine if a given page is empty. More...
 
uint8_t FLASH_firstEmptyPage (void)
 Get the first empty page is flash. More...
 

Detailed Description

This module contains functions for modifing internal flash memory.

The STM32f1x3 has 128kb of programmable flash memory. This space can also be used to store user values at runtime. It has the following restrictions

It can be benificial to occasionally clear all flash memory, this ensures the data occupying the flash memory is being used. This can be acheived with "make erase" from program directory.

Flash memory can only be changed from 1 to 0. This means each location needs to be reset with FLASH_clearPage() before it is writted again.

This module provides access to the flash memory. Typically pages toward the module (pages 120 - 127) should be used. The exact location of the free portion not used by code can be found by looking at the "text" section when the program is compiled.

Each page contains 256 words (32 bit) segments which can be used.

Author
Stuart Ianna
Version
0.1
Date
June 2018
Bug:
None
Todo:
Warning
Take note of where data is written. It is possible to overwrite the application code.
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Working example.
Notice every time the MCU is flashed or reset the first empty page is incremented by one. This occurs as the program only erases the data needed for the binary. Use "make erase" to clear enitre flash memory of MCU
#include <coms128.h>
uint32_t readData[3];
uint32_t writeData[2];
int main(void){
uint8_t pageBase;
//Serial setup
COMS(USART_1);
//Some random data
writeData[0] = 456;
writeData[1] = 789;
//Get the first empty page to use
pageBase = FLASH_firstEmptyPage();
//Should check device is not full
if(pageBase == 0){
printl("No free pages");
//Halt here
while (1);
}
//Print some data to word zero of that page
FLASH_write(pageBase,0,123);
//Print some more data to that page, starting at word 1
FLASH_writes(pageBase,1,writeData,sizeof(writeData));
//Read the data written, starting from word zero
FLASH_reads(pageBase,0,readData,sizeof(readData));
//Print out info
prints("First empty page is page");
printl(pageBase);
printl("Data read");
prints(readData[0]);
prints(readData[1]);
printl(readData[2]);
printl("Done");
while (1);
return 0;
}

Function Documentation

mcu_error FLASH_clearPage ( uint32_t  pageNumber)

Clear a page (1kb) of flash.

This needs to be used if the same memory location is used multiple times.

Flash memory can only be changed from 1 to 0. This means each location needs to be reset with FLASH_clearPage() before it is written again.

Parameters
pageNumberThe page to clear (0-127)
Warning
It is possible to remove application code with this function.
Returns
Error number as defined in mcu_error, E_FLASH_NOERROR if no error, E_FLASH_PAGE if page is out of range

Definition at line 22 of file stm32f103cb_flash.c.

uint8_t FLASH_firstEmptyPage ( void  )

Get the first empty page is flash.

Warning
This doesn't work in conjuction with the USB bootloader, as if the entire contents were erased, the bootloader would be destroyed.
Returns
The first empty page which can be used, returns 0 if no pages are empty.

Definition at line 158 of file stm32f103cb_flash.c.

uint8_t FLASH_pageEmpty ( uint32_t  pageNumber)

Determine if a given page is empty.

Parameters
pageNumberThe page to check. (0-127)
Warning
This function doesn't check page bounds
Returns
  • 1 if page is empty
  • 0 if page containes data

Definition at line 142 of file stm32f103cb_flash.c.

uint32_t FLASH_read ( uint32_t  pageNumber,
uint32_t  address 
)

Read a single word from flash memory.

Parameters
pageNumberThe page to read from. (0-127)
addressThe word in that page (0-255)
Returns
The data at the given address

Definition at line 88 of file stm32f103cb_flash.c.

mcu_error FLASH_readPage ( uint32_t  pageNumber,
uint32_t *  data 
)

Read an entire page from flash.

Parameters
pageNumberThe page to read from. (0-127)
dataPointer to a memory container large enough to hold the data
Warning
The size of data must be greater than 256 words ie. uint32_t data[256]
Returns
Error number as defined in mcu_error, E_FLASH_NOERROR if no error, E_FLASH_PAGE if page is out of range

Definition at line 119 of file stm32f103cb_flash.c.

mcu_error FLASH_reads ( uint32_t  pageNumber,
uint32_t  address,
uint32_t *  data,
uint16_t  size 
)

Read multiple words from flash.

Parameters
pageNumberThe page to read from. (0-127)
addressThe word in that page to start from (0-255)
dataPointer to a memory container large enough to hold the data
sizeThe size of parameter data
Warning
If size is larger than data, a memeory fault will occur
Returns
Error number as defined in mcu_error, E_FLASH_NOERROR if no error, E_FLASH_PAGE if page is out of range

Definition at line 96 of file stm32f103cb_flash.c.

mcu_error FLASH_write ( uint32_t  pageNumber,
uint32_t  address,
uint32_t  data 
)

Write a single word to flash.

A word can only be written to a location once before FLASH_clearPage() is used to reset the memory location

It is possible to use a word address greater than 255. This means that the data will be located in the next page.

Parameters
pageNumberThe page to write to (0-127)
addressThe word in that page (0-255)
dataThe data to write to the location
Warning
It is possible to remove application code with this function.
Returns
Error number as defined in mcu_error, E_FLASH_NOERROR if no error, E_FLASH_PAGE if page is out of range

Definition at line 41 of file stm32f103cb_flash.c.

mcu_error FLASH_writes ( uint32_t  pageNumber,
uint32_t  address,
uint32_t *  data,
uint16_t  size 
)

Write multiple words to flash, address incremented automatically.

A word can only be written to a location once before FLASH_clearPage() is used to reset the memory location

It is possible to use a word address greater than 255. This means that the data will be located in the next page.

Parameters
pageNumberThe page to write to (0-127)
addressThe word in that page (0-255)
dataPointer to the data to write to the location
sizeThe size of parameter data
Warning
It is possible to remove application code with this function.
If size is larger than data, a memeory fault will occur
Returns
Error number as defined in mcu_error, E_FLASH_NOERROR if no error, E_FLASH_PAGE if page is out of range

Definition at line 61 of file stm32f103cb_flash.c.