Reading and writing of EEPROM inside STM32 microcontroller

Publisher:Ziyu2022Latest update time:2018-07-17 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The STM32L series MCU provides an EEPROM storage area, but in fact, its FLASH is also an EEPROM type, but there is an area that is opened up for EEPROM operation. The STM32L EEPROM service life is designed to be more than 100,000 erase and write times, and the capacity is 2K-4K, which is ideal for parameter storage of general equipment. However, from the perspective of EEPROM usage, it is not suitable for data storage that is repeatedly modified. Generally, as a configuration parameter, the number of modifications is often relatively small.


The EEPROM and FLASH of STM32L are uniformly addressed and share the same read-write circuit. Therefore, when reading and writing EEPROM, all access and operations of STM32L core to FLASH will be suspended. Only when the operation of EEPROM is completed, the subsequent code will continue to be executed. During this period, only the read-write circuit of EEPROM works and the CPU is in a suspended state.


Read operation, like FLASH and memory, EEPROM data can be read directly using the bus read cycle, without the need for additional operations and settings.



#define EEPROM_BASE_ADDR    0x08080000    

#define EEPROM_BYTE_SIZE    0x0FFF  


The above defines the starting position and size of the EEPROM area. After the offset is given, it can be read out in byte/halfword/word/doubleword mode. However, it should be noted that the offset address is preferably aligned to four bytes to avoid bus access errors or incorrect access:

    


/*------------------------------------------------------------ 

 Func: EEPROM data is read out byte by byte 

 Note: 

-------------------------------------------------------------*/  

void EEPROM_ReadBytes(uint16 Addr,uint8 *Buffer,uint16 Length)  

{  

    uint8 *wAddr;  

    wAddr=(uint8 *)(EEPROM_BASE_ADDR+Addr);  

    while(Length--){  

        *Buffer++=*wAddr++;  

    }     

}  

    


/*------------------------------------------------------------ 

 Func: EEPROM data read 

 Note: 

-------------------------------------------------------------*/  

void EEPROM_ReadWords(uint16 Addr,uint16 *Buffer,uint16 Length)  

{  

    uint32 *wAddr;  

    wAddr=(uint32 *)(EEPROM_BASE_ADDR+Addr);  

    while(Length--){  

        *Buffer++=*wAddr++;  

    }     

}  



The above method uses byte and word mode to read out. In the latter method, only 16 bits are used in the storage space of a word, and the other 16 bits are not used to avoid alignment problems.


Programming EEPROM is much more complicated than reading. In essence, the erase operation is the same as the write operation. Erasing is just writing 0x00000000 in the corresponding place. However, in the implementation of STM32L, according to its manual, it seems that this erase and write are distinguished. When writing 0x00 or 0x0000 or 0x00000000, an erase operation is automatically performed. When the value is non-0, a so-called write operation is performed. The data writing process must first unlock the EEPROM, which is achieved by writing a special sequence to a special register, and then perform an erase operation before writing. The erase is performed by word/double word/page. It is recommended to use the page erase method. First read the parameters into the memory, modify them, then perform page erase, and finally write the parameters back. This method is more common, otherwise it is easy to cause address alignment or length problems. After the data is erased, it can be written. Each time a byte/half word/double word is written, it is necessary to determine whether it is written. This is related to the internal high-voltage erase circuit. It is meaningful to perform other operations only after the last operation is completed. Finally, the EEPROM is locked to protect the data.


 

The following is the unlock command code given in the manual:



#define PEKEY1  0x89ABCDEF      //FLASH_PEKEYR  

#define PEKEY2  0x02030405      //FLASH_PEKEYR  

    The following implements writing in byte and word mode respectively:   


/*------------------------------------------------------------ 

 Func: EEPROM data is written byte by byte 

 Note: 

-------------------------------------------------------------*/  

void EEPROM_WriteBytes(uint16 Addr,uint8 *Buffer,uint16 Length)  

{  

    uint8 *wAddr;  

    wAddr=(uint8 *)(EEPROM_BASE_ADDR+Addr);  

    DIS_INT  

    FLASH->PEKEYR=PEKEY1;                //unlock  

    FLASH->PEKEYR=PEKEY2;  

    while(FLASH->PECR&FLASH_PECR_PELOCK);  

    FLASH->PECR|=FLASH_PECR_FTDW;        //not fast write  

    while(Length--){  

        *wAddr++=*Buffer++;  

        while(FLASH->SR&FLASH_SR_BSY);  

    }  

    FLASH->PECR|=FLASH_PECR_PELOCK;  

    IN_INT  

}  

    


/*------------------------------------------------------------ 

 Func: EEPROM data is written word by word 

 Note: Use a word as a half word 

-------------------------------------------------------------*/  

void EEPROM_WriteWords(uint16 Addr,uint16 *Buffer,uint16 Length)  

{  

    uint32 *wAddr;  

    wAddr=(uint32 *)(EEPROM_BASE_ADDR+Addr);  

    DIS_INT  

    FLASH->PEKEYR=PEKEY1;                //unlock  

    FLASH->PEKEYR=PEKEY2;  

    while(FLASH->PECR&FLASH_PECR_PELOCK);  

    FLASH->PECR|=FLASH_PECR_FTDW;        //not fast write  

    while(Length--){  

        *wAddr++=*Buffer++;  

        while(FLASH->SR&FLASH_SR_BSY);  

    }  

    FLASH->PECR|=FLASH_PECR_PELOCK;  

    IN_INT  

}  


In the above code, the system interrupt DIS_INT is disabled before writing data, and the system interrupt EN_INT is enabled after writing is completed. This prevents the interrupt process from interrupting the write operation, causing CPU abnormality or lockup. Be sure to pay attention to this when using it. In the MDK environment, the two can be defined as follows:

    


#define EN_INT __enable_irq(); //System enables global interrupt  

#define DIS_INT __disable_irq(); //System turns off global interrupts  


Keywords:STM32 Reference address:Reading and writing of EEPROM inside STM32 microcontroller

Previous article:STM32 system learning - I2C (read and write EEPROM)
Next article:STM32 on-chip flash is used as EEPROM (power-off preservation)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号