1、简介
STM8S内部的FLASH程序存储器和数据EEPROM是由一组通用寄存器来控制的;所以我们可以通过这些通用寄存器来编程或擦除存储器的内容、设置写保护、或者配置特定的低功耗模式。我们也可以自己对器件的 option byte 进行编程。在这里我们只简单的讲解如何对STM8S内部的数据存储区域(data memory)进行写操作、读操作、擦除操作。
2、存储架构图
STM8S内部存储包括:FLASH程序存储器(FLASH program memory)和数据EEPROM(DATA EEPROM);
(1)数据EEPROM又包括:
-
- 数据存储区域(DATA MEMORY):指定从地址0x00 4000开始,其中包括 [1 block] 的option byte,数据存储区域用于存储应用数据;
(2)FLASH程序存储器又包括:
-
- 用户启动区域(USER BOOT CODE):指定从地址0x00 8000开始,其中包括UBC option bytes和 [128 byte] 的中断矢量(interrupt vectors);
- 程序存储区域(MAIN PROGRAM):用于存储应用程序代码;
所以我们的操作是在DATA MEMORY里面,在该区域的指定地址进行读取内容、写入内容、擦除内容。
3、操作方法
(1)FLASH_ReadByte( ADDRESS ):读取地址ADDRESS处的1字节内容
(2)FLASH_ProgramByte( ADDRESS , DATA ):写1字节的内容到地址ADDRESS处
(3)FLASH_EraseByte( ADDRESS ):在地址ADDRESS处删除1字节的内容
4、操作步骤
使用操作前的配置
(1)配置选择编程时间,选择标准模式;
(2)解除数据存储区域的写保护;
说明:UBC和DATA MEMORY都有写保护,其中UBC的写保护是永远无法解锁的,而DATA MEMORY的写保护是可以通过连续写入两个MASS密钥值来解除该区域的写保护的;第一个密钥为0x56,第二个密钥为0xAE;只有当两个密钥输入正确(输入到寄存器FLASH_DUKR中)且输入密钥的先后顺序正确时,才会解除写保护;否则该区域在下一次系统复位之前都将一直保持写保护状态。
(3)读取、写入、擦除等操作;
5、代码实现
1 /* Includes ------------------------------------------------------------------*/ 2 #include "stm8s.h" 3 4 /* Private typedef -----------------------------------------------------------*/ 5 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus; 6 7 /* Private variables ---------------------------------------------------------*/ 8 __IO TestStatus OperationStatus; 9 10 11 /** 12 * @brief How to Read / Write / Erase one Byte on FLASH memory. 13 * @par Examples description 14 * - Read one byte at address 0x40A5 15 * - Write its complement value at address + 1 16 * - Check programed value 17 * - Erase 2 byte (address 40A5 & 40A6) 18 * - Check the 2 bytes value is 0x00. 19 * @param None 20 * @retval None 21 */ 22 void main(void) 23 { 24 25 uint8_t val = 0x00, val_comp = 0x00; 26 uint32_t add = 0x00; 27 28 /* Define FLASH programming time */ 29 //Standard programming time fixed at 1/2 tprog 30 FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD); 31 32 /* Unlock Data memory */ 33 //compare to two hardware key value: 0x56 and 0xAE 34 FLASH_Unlock(FLASH_MEMTYPE_DATA); 35 36 /* Read a byte at a specified address */ 37 add = 0x40A5; 38 //read one byte from the address 39 val = FLASH_ReadByte(add); 40 41 /* Program complement value (of previous read byte) at previous address + 1 */ 42 val_comp = (uint8_t)(~val); 43 //modify one byte at the address 44 FLASH_ProgramByte((add + 1), val_comp); 45 46 /* Check program action */ 47 val = FLASH_ReadByte((add + 1)); 48 if (val != val_comp) 49 { 50 /* Error */ 51 OperationStatus = FAILED; 52 /* OperationStatus = PASSED, if the data written/read to/from DATA EEPROM memory is correct */ 53 /* OperationStatus = FAILED, if the data written/read to/from DATA EEPROM memory is corrupted */ 54 while (1) 55 { 56 } 57 } 58 59 /* Erase byte at a specified address & address + 1 */ 60 //erase one byte at the address 61 FLASH_EraseByte(add); 62 FLASH_EraseByte((add + 1)); 63 /* Erase action */ 64 val = FLASH_ReadByte(add); 65 val_comp = FLASH_ReadByte((add + 1)); 66 if ((val != 0x00) & (val_comp != 0x00)) 67 { 68 /* Error */ 69 OperationStatus = FAILED; 70 /* OperationStatus = PASSED, if the data written/read to/from DATA EEPROM memory is correct */ 71 /* OperationStatus = FAILED, if the data written/read to/from DATA EEPROM memory is corrupted */ 72 while (1) 73 { 74 } 75 } 76 77 /* Pass */ 78 OperationStatus = PASSED; 79 /* OperationStatus = PASSED, if the data written/read to/from DATA EEPROM memory is correct */ 80 /* OperationStatus = FAILED, if the data written/read to/from DATA EEPROM memory is corrupted */ 81 while (1) 82 { 83 } 84 }