Stm32 Flash read/write [library function]

Source: Internet
Author: User

one. Stm32 Flash is divided into1. Primary storage block: Used to save the specific program code and user Data , the primary storage block is divided into pages,a page size of 1KB. The range is within 128KB beginning with address 0x08000000. 2. Information BLOCK: For the User configuration information area that is responsible for the 2KB Boot program (Bootloader) and 512B that is shipped by Stm32 factory two. Operating Principleswrite operation to Flash to " Erase first write " principle;Stm32 's built-in flash programming operations are written in pages, and the operation must be written in 16- bit half -width data bit units, allowing cross-page writes, and writing non-16-bit data will cause stm32 internal bus errors . the internal RC oscillator must be turned on when the built-in flash reads and writes.
header file:
#ifndef __bsp_flash_h#define__bsp_flash_h#include "stm32f10x.h" typedef enum {FLASH_WRITE_OK = 1,flash_wrike_no = 0, FLASH_READ_OK = 1,flash_read_no = 0}flash_flag; extern unsigned char flash_write (unsigned short int * memory_data); extern unsigned char flash_read (unsigned short int * memory_data,unsigned short int n); #endif/* __led_h */


Read and Write functions:
#include "bsp_flash.h" Flash_flag flash_status;static u32 count = 0;/**---------------------------------------------- -----------------------------------* @brief Flash Write essential steps * @param input data start address * @retval function has not been executed------------------- -------------------------------------------------------------*/unsigned Char flash_write (unsigned short int * memory _data) {static int t;count = 0;/*enables or disables the Internal high speed oscillator (HSI). */rcc_hsicmd (ENABLE);/* Open Flas H erasable Controller */flash_unlock ();/* will FLASH three flag bits full */flash_clearflag (flash_flag_eop| flash_flag_pgerr| FLASH_FLAG_WRPRTERR); Flash_erasepage (0x8002000); t = sizeof ((const char *) memory_data), while (Count <= t) {/*flash is stored as one byte, 16-bit data must be address plus 2*/ Flash_programhalfword ((0x8002000 +count*2), * (Memory_data+count)); Count++;if (Count > t) {flash_status = Flash_wrike_no;return flash_status;}} /* Locks the FLASH program Erase Controller.*/flash_lock (); Rcc_hsicmd (DISABLE); Flash_status = Flash_write_ok;return flash_status;} /**---------------------------------------------------------------------------------* @brief Flash readout function * @param memory_data: Output storage address, n output number * @retval function has not been executed--------------------------------------------------------------------------------*/unsigned Char Flash_read (unsigned short int * memory_data,unsigned short int n) {count = 0;while (Count < N) {* (memory_data+count) = * (U16 *)   (0x8002000 + count*2);p rintf ("\ r%d \ r", * (Memory_data+count)); Read Count++;if (Count > N) {flash_status = Flash_read_no;return flash_status;}} Flash_erasepage (0x8002000); Flash_status = Flash_read_ok;return flash_status;} /*********************************************end of file**********************/



Stm32 Flash read/write [library function]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.