Giter Site home page Giter Site logo

vinhdeved / flash_stm32f103 Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 2.61 MB

Flash memory read, write and erase operations on MCU STM32F103C8T6. In this repository, i will give some information detail and perform how to do with that.

C 98.25% Assembly 0.27% Makefile 1.48%

flash_stm32f103's Introduction

⚡ Interact with Flash Memory in STM32F103 ⚡

Flash memory read, write and erase operations on MCU STM32F103C8T6. In this repository, i will give some information detail and perform how to do with that. These knowledge in small project will support for Next Topic: "Firmware Over The Air"

Memory Distribution

As we can see in the picture below, Flash memory is organized as a main block of up to 64 Kb (or 128 Kb) divided into 128 pages, each page 1 Kbytes (medium-density devices) and 1 information block (information block). Starting from address 0x0800 0000, program memory will be stored here

Image

📊 Analysis and Explain

🖋️ Write Data in Flash

Before writing data to flash memory, you need to initialize the flash interface and unlock with HAL_FLASH_Unlock() function. After that, you must erase the flash page where your data is stored. Erasing is typically done at the beginning and then you can proceed to write the new data. Next, modifying the erase init structure is very important because of suitable project. Some specific structure following below:

  • Type Erase

    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES
    

    The flash memory is divided into pages, and erasing is often done at the page level.

  • Page Address

    EraseInitStruct.PageAddress = StartPage
    

    The variable StartPage likely holds the address of the first page you want to erase.

  • NbPage( Number of Page )

    EraseInitStruct.NbPages = ((EndPage - StartPage)/FLASH_PAGE_SIZE) + 1
    

    Here, you define the number of pages to be erased. The calculation determines how many pages are between StartPage and EndPage. FLASH_PAGE_SIZE represents the size of one page in bytes.

    For example, if StartPage is 0x08010000 and EndPage is 0x0801FFFF, and FLASH_PAGE_SIZE is 1024 bytes, the number of pages to be erased would be ((0x0801FFFF - 0x08010000)/1024) + 1 = 64

Some specific of this structure is then likely passed to the HAL_FLASHEx_Erase(EraseInitStruct,.....) function to perform the actual ensure. Now, you can write your data which can use for/while loop thourgh each words by numberofword. As line code below, it uses type of word and add for 4 after looping because 1 word = 4 bytes (32-bits). So, it need move 4 address to the next suitable place to write data:

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartPageAddress, Data[sofar]);
StartPageAddress += 4;  
sofar++;

After successfully writing the data, we need to lock the flash memory to avoid unusual accesses.

📰 Read Data in Flash

To read data from flash memory, you simply need to access the memory location where your data is stored. RxBuf is the bucket which is the address 32 bit address and stores the data. You need to know the number of word to read from flash.

*RxBuf = *(__IO uint32_t *)StartPageAddress;

Result

In this project, we use STM32CubeIDE to interact with Flash Memory Information.

  • In picture 1, string array is the result for converting Flash Read Data saved to buffer.

Image

  • In picture 2, using feature Memory to observe any flash address and tradition style of hex. I choose 0x08004810 address for starting position and writing data string text.

Image

flash_stm32f103's People

Contributors

vinhdeved avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.